KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > inside > ix > IndexTransaction


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.inside.ix;
22
23 import com.db4o.*;
24 import com.db4o.foundation.*;
25
26 /**
27  * Index root holder for a field and a transaction.
28  * @exclude
29  */

30 public class IndexTransaction implements Visitor4{
31     
32     final Index4 i_index;
33     final Transaction i_trans;
34     int i_version;
35     private Tree i_root;
36     
37     IndexTransaction(Transaction a_trans, Index4 a_index){
38         i_trans = a_trans;
39         i_index = a_index;
40     }
41     
42     public boolean equals(Object JavaDoc obj) {
43         return i_trans == ((IndexTransaction)obj).i_trans;
44     }
45     
46     /**
47      */

48     public void add(int id, Object JavaDoc value){
49         patch(new IxAdd(this, id, value));
50     }
51     
52     public void remove(int id, Object JavaDoc value){
53         patch(new IxRemove(this, id, value));
54     }
55     
56     private void patch(IxPatch patch){
57         i_root = Tree.add(i_root,patch);
58     }
59
60     
61 // public void add(IxPatch a_patch){
62
// i_root = Tree.add(i_root, a_patch);
63
// }
64

65     public Tree getRoot(){
66         return i_root;
67     }
68     
69     public void commit(){
70         i_index.commit(this);
71     }
72     
73     public void rollback(){
74         i_index.rollback(this);
75     }
76     
77     void merge(IndexTransaction a_ft){
78         Tree otherRoot = a_ft.getRoot();
79         if(otherRoot != null){
80             otherRoot.traverseFromLeaves(this);
81         }
82     }
83     
84     /**
85      * Visitor functionality for merge:<br>
86      * Add
87      */

88     public void visit(Object JavaDoc obj){
89         if(obj instanceof IxPatch){
90             IxPatch tree = (IxPatch)obj;
91             if(tree.hasQueue()){
92                 Queue4 queue = tree.detachQueue();
93                 while((tree = (IxPatch)queue.next()) != null){
94                     tree.detachQueue();
95                     addPatchToRoot(tree);
96                 }
97             }else{
98                 addPatchToRoot(tree);
99             }
100         }
101     }
102     
103     private void addPatchToRoot(IxPatch tree){
104         if(tree._version != i_version){
105             tree.beginMerge();
106             tree.handler().prepareComparison(tree.handler().comparableObject(i_trans, tree._value));
107             if(i_root == null){
108                 i_root = tree;
109             } else{
110                 i_root = i_root.add(tree);
111             }
112         }
113     }
114     
115     int countLeaves(){
116         if(i_root == null){
117             return 0;
118         }
119         final int[] leaves ={0};
120         i_root.traverse(new Visitor4() {
121             public void visit(Object JavaDoc a_object) {
122                 leaves[0] ++;
123             }
124         });
125         return leaves[0];
126     }
127
128     public void setRoot(Tree a_tree) {
129         i_root = a_tree;
130     }
131     
132     public String JavaDoc toString(){
133         if(! Debug4.prettyToStrings){
134             return super.toString();
135         }
136         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
137         sb.append("IxFieldTransaction ");
138         sb.append(System.identityHashCode(this));
139         if(i_root == null){
140             sb.append("\n Empty");
141         }else{
142             i_root.traverse(new Visitor4() {
143                 public void visit(Object JavaDoc a_object) {
144                     sb.append("\n");
145                     sb.append(a_object.toString());
146                 }
147             });
148         }
149         return sb.toString();
150     }
151
152     
153     
154     
155 }
156
Popular Tags