KickJava   Java API By Example, From Geeks To Geeks.

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


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.QE;
24 import com.db4o.foundation.*;
25 import com.db4o.inside.freespace.*;
26
27 /**
28  * Index Path to represent a list of traversed index tree entries,
29  * used by IxTraverser
30  */

31 class IxPath implements ShallowClone,Visitor4 {
32
33     int i_comparisonResult;
34
35     int[] i_lowerAndUpperMatch;
36     int i_upperNull = -1;
37
38     IxPath i_next;
39
40     IxTraverser i_traverser;
41     IxTree i_tree;
42     
43     Visitor4 _visitor;
44
45     IxPath(IxTraverser a_traverser, IxPath a_next, IxTree a_tree,
46         int a_comparisonResult, int[] lowerAndUpperMatch) {
47         i_traverser = a_traverser;
48         i_next = a_next;
49         i_tree = a_tree;
50         i_comparisonResult = a_comparisonResult;
51         i_lowerAndUpperMatch = lowerAndUpperMatch;
52     }
53     
54     void add(Visitor4 visitor) {
55         if (i_comparisonResult == 0 && i_traverser.i_take[QE.EQUAL]) {
56             i_tree.visit(visitor, i_lowerAndUpperMatch);
57         }
58     }
59
60     void addPrecedingToCandidatesTree(Visitor4 visitor) {
61         _visitor = visitor;
62         if (i_tree._preceding != null) {
63             if (i_next == null || i_next.i_tree != i_tree._preceding) {
64                 i_tree._preceding.traverse(this);
65             }
66         }
67         if (i_lowerAndUpperMatch != null) {
68             int[] lowerAndUpperMatch = new int[] { i_upperNull,
69                 i_lowerAndUpperMatch[0] - 1};
70             i_tree.visit(visitor, lowerAndUpperMatch);
71         } else {
72             if (i_comparisonResult < 0) {
73                 visit(i_tree);
74             }
75         }
76     }
77
78     void addSubsequentToCandidatesTree(Visitor4 visitor) {
79         _visitor = visitor;
80         if (i_tree._subsequent != null) {
81             if (i_next == null || i_next.i_tree != i_tree._subsequent) {
82                 i_tree._subsequent.traverse(this);
83             }
84         }
85         if (i_lowerAndUpperMatch != null) {
86             int[] lowerAndUpperMatch = new int[] { i_lowerAndUpperMatch[1] + 1,
87                 ((IxFileRange) i_tree)._entries - 1};
88             i_tree.visit(visitor, lowerAndUpperMatch);
89         } else {
90             if (i_comparisonResult > 0) {
91                 visit(i_tree);
92             }
93         }
94     }
95
96     IxPath append(IxPath a_head, IxPath a_tail) {
97         if (a_head == null) {
98             return this;
99         }
100         i_next = a_head;
101         return a_tail;
102     }
103
104     IxPath append(IxTree a_tree, int a_comparisonResult, int[] lowerAndUpperMatch) {
105         i_next = new IxPath(i_traverser, null, a_tree, a_comparisonResult, lowerAndUpperMatch);
106         i_next.i_tree = a_tree;
107         return i_next;
108     }
109
110     boolean carriesTheSame(IxPath a_path) {
111         return i_tree == a_path.i_tree;
112     }
113
114     private void checkUpperNull() {
115         if (i_upperNull == -1) {
116             i_upperNull = 0;
117             i_traverser.i_handler.prepareComparison(null);
118             int res = i_tree.compare(null);
119             if(res != 0){
120                 return;
121             }
122             int[] nullMatches = i_tree.lowerAndUpperMatch();
123             if (nullMatches[0] == 0) {
124                 i_upperNull = nullMatches[1] + 1;
125             } else {
126                 i_upperNull = 0;
127             }
128         }
129     }
130     
131     public void visitMatch(FreespaceVisitor visitor){
132         if(i_next != null){
133             i_next.visitMatch(visitor);
134         }
135         if(visitor.visited()){
136             return;
137         }
138         if (i_comparisonResult != 0){
139             return;
140         }
141         
142         if (i_lowerAndUpperMatch == null) {
143             i_tree.freespaceVisit(visitor, 0);
144             return;
145         }
146         
147         if(i_lowerAndUpperMatch[1] < i_lowerAndUpperMatch[0]){
148             return;
149         }
150         
151         int ix = i_lowerAndUpperMatch[0];
152         if(ix >= 0){
153             i_tree.freespaceVisit(visitor, ix);
154         }
155     }
156     
157     public void visitPreceding(FreespaceVisitor visitor){
158         if(i_next != null){
159             i_next.visitPreceding(visitor);
160             if(visitor.visited()){
161                 return;
162             }
163         }
164         if (i_lowerAndUpperMatch != null) {
165             int ix = i_lowerAndUpperMatch[0] - 1;
166             if(ix >= 0){
167                 i_tree.freespaceVisit(visitor, ix);
168             }
169         }else{
170             if (i_comparisonResult < 0) {
171                 i_tree.freespaceVisit(visitor, 0);
172             }
173         }
174         if(visitor.visited()){
175             return;
176         }
177         if(i_tree._preceding != null){
178             if (i_next == null || i_next.i_tree != i_tree._preceding) {
179                 ((IxTree)i_tree._preceding).visitLast(visitor);
180             }
181         }
182     }
183     
184     public void visitSubsequent(FreespaceVisitor visitor){
185         if(i_next != null){
186             i_next.visitSubsequent(visitor);
187             if(visitor.visited()){
188                 return;
189             }
190         }
191         if (i_lowerAndUpperMatch != null) {
192             int ix = i_lowerAndUpperMatch[1] + 1;
193             if(ix < ((IxFileRange) i_tree)._entries){
194                 i_tree.freespaceVisit(visitor, ix);
195             }
196         }else{
197             if (i_comparisonResult > 0) {
198                 i_tree.freespaceVisit(visitor, 0);
199             }
200         }
201         if(visitor.visited()){
202             return;
203         }
204         if(i_tree._subsequent != null){
205             if (i_next == null || i_next.i_tree != i_tree._subsequent) {
206                 ((IxTree)i_tree._subsequent).visitFirst(visitor);
207             }
208         }
209     }
210
211     int countMatching() {
212         if (i_comparisonResult == 0) {
213             if (i_lowerAndUpperMatch == null) {
214                 if (i_tree instanceof IxRemove) {
215                     return 0;
216                 }
217                 return 1;
218             }
219             return i_lowerAndUpperMatch[1] - i_lowerAndUpperMatch[0] + 1;
220         }
221         return 0;
222     }
223
224     int countPreceding(boolean a_takenulls) {
225         int preceding = 0;
226         if (i_tree._preceding != null) {
227             if (i_next == null || i_next.i_tree != i_tree._preceding) {
228                 preceding += i_tree._preceding.size();
229             }
230         }
231         if (i_lowerAndUpperMatch != null) {
232             if(a_takenulls) {
233                 i_upperNull = 0;
234             }else {
235                 checkUpperNull();
236             }
237             preceding += i_lowerAndUpperMatch[0] - i_upperNull;
238         } else {
239             if (i_comparisonResult < 0 && !(i_tree instanceof IxRemove)) {
240                 preceding++;
241             }
242         }
243         return preceding;
244     }
245
246     int countSubsequent() {
247         int subsequent = 0;
248         if (i_tree._subsequent != null) {
249             if (i_next == null || i_next.i_tree != i_tree._subsequent) {
250                 subsequent += i_tree._subsequent.size();
251             }
252         }
253         if (i_lowerAndUpperMatch != null) {
254             subsequent += ((IxFileRange) i_tree)._entries
255                 - i_lowerAndUpperMatch[1] - 1;
256         } else {
257             if (i_comparisonResult > 0 && !(i_tree instanceof IxRemove)) {
258                 subsequent++;
259             }
260         }
261         return subsequent;
262     }
263
264     public Object JavaDoc shallowClone() {
265         int[] lowerAndUpperMatch=null;
266         if(i_lowerAndUpperMatch!=null) {
267             lowerAndUpperMatch=new int[]{i_lowerAndUpperMatch[0],i_lowerAndUpperMatch[1]};
268         }
269         IxPath ret=new IxPath(i_traverser,i_next,i_tree,i_comparisonResult,lowerAndUpperMatch);
270         ret.i_upperNull=i_upperNull;
271         ret._visitor=_visitor;
272         return ret;
273     }
274
275     public String JavaDoc toString() {
276         if(! Debug4.prettyToStrings){
277             return super.toString();
278         }
279         return i_tree.toString();
280     }
281
282     public void visit(Object JavaDoc a_object) {
283         ((Visitor4) a_object).visit(_visitor);
284     }
285
286 }
287
Popular Tags