KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > fulltext > dql > iterator > RankAbstractNavigation


1 package com.daffodilwoods.daffodildb.server.sql99.fulltext.dql.iterator;
2
3 import com.daffodilwoods.database.resource.DException;
4 import com.daffodilwoods.daffodildb.utils.field.FieldBase;
5 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference;
6 import com.daffodilwoods.database.utility.*;
7 import com.daffodilwoods.daffodildb.utils.GetByteComparator;
8
9 /**
10  * <p>Title: RankAbstractNavigation</p>
11  * <p>Description: This class performs the common navigation for the Rank 1,
12  * Rank 2 and Rank 3 iterator used for retrival of the records for the full-text
13  * index column.</p>
14  * <p>Copyright: Copyright (c) 2003</p>
15  * <p>Company: </p>
16  * @author not attributable
17  * @version 1.0
18  */

19
20 public abstract class RankAbstractNavigation extends FTException {
21
22   /**
23    * This abstract method is overriden by Rank1, Rank2 and Rank3 iterators,
24    * Which parforms the locations checking of the words according to rank condition.
25    * @return
26    * @throws DException
27    */

28   public abstract boolean alignLocationForward() throws DException;
29   public abstract boolean alignLocationBackward() throws DException;
30
31   /**
32    * This method is called when iterator's first and next methods are called.
33    * Aligns all the iterator depending on the rank condition. Firstly, navigates
34    * navigates all the full-text iterators to get the common documentid. Then,
35    * transfers the call to find out a set of words whose locations satisfy the
36    * corresponding rank condition.
37    * @return
38    * @throws DException
39    */

40
41   private boolean alignFullTextAndLocationsForward() throws DException {
42     if (!alignFullTextForward()) {
43       return false;
44     }
45     return alignLocationForward() ? true : next();
46   }
47
48   /**
49    * This method sets the value of the pk of the particular word/term from the
50    * Token Table to get its location(s) from the Location Table. Then, calls
51    * first method for the first location iterator and transfers the call to
52    * abstract method to align location iterators to find out a set of words
53    * whose locations satisfy the corresponding rank condition.
54    * @return
55    * @throws DException
56    */

57 // }*/
58

59   /**
60    * This method is called when iterator's last and previous methods are called.
61    * Aligns all the iterator depending on the rank condition. Firstly, navigates
62    * navigates all the full-text iterators to get the common documentid. Then,
63    * transfers the call to find out a set of words whose locations satisfy the
64    * corresponding rank condition.
65    * @return
66    * @throws DException
67    */

68   private boolean alignFullTextAndLocationsBackward() throws DException {
69     if (!alignFullTextBackward())
70       return false;
71     return alignLocationBackward() ? true : previous();
72   }
73
74   /**
75    * This method sets the value of the pk of the particular word/term from the
76    * Token Table to get its location(s) from the Location Table. Then, calls
77    * first method for the first location iterator and transfers the call to
78    * abstract method to align location iterators transfers the call to find out
79    * a set of words whose locations satisfy the corresponding rank condition.
80    * @return
81    * @throws DException
82    */

83
84 public static boolean testing;
85
86   public boolean first() throws DException {
87 testing = true;
88     for(int i = 0 ; i < fullTextIndex.length ; i++)
89       if(!fullTextIndex[i].first()) {
90         return false;
91       }
92     boolean b = alignFullTextAndLocationsForward();
93     return b;
94   }
95
96   public boolean next() throws DException {
97     for(int i = 0 ; i < fullTextIndex.length ; i++)
98       if(!fullTextIndex[i].next()) {
99         return false;
100       }
101       else {
102       }
103     boolean b = alignFullTextAndLocationsForward();
104     return b;
105   }
106
107   public boolean last() throws DException {
108 testing = true;
109     for(int i = 0 ; i < fullTextIndex.length ; i++)
110       if(!fullTextIndex[i].last()) {
111         return false;
112       }
113     return alignFullTextAndLocationsBackward();
114   }
115
116   public boolean previous() throws DException {
117     for(int i = 0 ; i < fullTextIndex.length ; i++)
118       if(!fullTextIndex[i].previous())
119         return false;
120     return alignFullTextAndLocationsBackward();
121   }
122
123
124   /**
125    * This method is called when iterator's first and next methods are called.
126    * This method checks whether given full-text token iterator have a common
127    * document id, this is the primary condition of first three rank iterators,
128    * in other words, there must be a record in the table having all the parsed
129    * words simultaneously.
130    * @return true if there exists a row in the table having all the words in the
131    * same document, false, otherwise.
132    * @throws DException
133    */

134   public boolean alignFullTextForward() throws DException {
135     int maxIndex = getMaxIndex();
136     return (maxIndex == -1) ? true : alignForward(maxIndex);
137   }
138
139   /**
140    * Checks whether the first row of all full-text iterators have the same
141    * documentid.
142    * @return returns -1, if all full-text iterators having the same documentid,
143    * otherwise, index of the full text iterator having the maximum documentid.
144    * @throws DException
145    */

146   private int getMaxIndex() throws DException {
147     Object JavaDoc value = fullTextIndex[0].getColumnValues(documentIdColumn);
148     Object JavaDoc maxValue = value;
149     int maxIndex = 0;
150     boolean haveSameDocumentId = true;
151     for(int i = 1; i < fullTextIndex.length; i++){
152       Object JavaDoc nValue = fullTextIndex[i].getColumnValues(documentIdColumn);
153       int cmp = compare(value,nValue);
154       if( cmp < 0){
155         maxIndex = i;
156         maxValue = nValue;
157       }
158       else if ( cmp > 0)
159         haveSameDocumentId = false;
160     }
161     if(haveSameDocumentId && maxValue == value)
162       return -1;
163     return maxIndex;
164   }
165
166   private int getMinIndex() throws DException {
167     Object JavaDoc value = fullTextIndex[0].getColumnValues(documentIdColumn);
168     Object JavaDoc maxValue = value;
169     int maxIndex = 0;
170     boolean haveSameDocumentId = true;
171     for(int i = 1; i < fullTextIndex.length; i++){
172       Object JavaDoc nValue = fullTextIndex[i].getColumnValues(documentIdColumn);
173       int cmp = compare(value,nValue);
174       if( cmp > 0){
175         maxIndex = i;
176         maxValue = nValue;
177       }
178       else if ( cmp < 0)
179         haveSameDocumentId = false;
180     }
181     if(haveSameDocumentId && maxValue == value)
182       return -1;
183     return maxIndex;
184   }
185
186   /**
187    * This method is called when iterator's first and next methods are called.
188    * Checks the successive enteries of the full text iterators untill a common
189    * documentid found, if document having all words found (same documentid ),
190    * returns true, otherwise false.
191    * @param index
192    * @return
193    * @throws DException
194    */

195   private boolean alignForward(int index) throws DException {
196     Object JavaDoc value = fullTextIndex[index].getColumnValues(documentIdColumn);
197     while(true){
198       for(int i = 0; i < fullTextIndex.length; i++){
199         if(compare(value,fullTextIndex[i].getColumnValues(documentIdColumn)) == 0)
200           continue;
201         if(!fullTextIndex[i].seekFromTopRelative(new Object JavaDoc[] {keyWords[i], value} ))
202           return false;
203         Object JavaDoc nValue = fullTextIndex[i].getColumnValues(documentIdColumn);
204         value = compare(nValue,value) > 0 ? nValue : value;
205       }
206       boolean b = haveSameDocumentId();
207       if(b)
208         return true;
209     }
210   }
211
212   /**
213    * This method is called when iterator's last and previous methods are called.
214    * This method checks whether given full-text token iterator have a common
215    * document id, this is the primary condition of first three rank iterators,
216    * in other words, there must be a record in the table having all the parsed
217    * words simultaneously.
218    * @return true if there exists a row in the table having all the words in the
219    * same document, false, otherwise.
220    * @throws DException
221    */

222   public boolean alignFullTextBackward() throws DException {
223     int maxIndex = getMinIndex();
224     return (maxIndex == -1) ? true : alignBackward(maxIndex);
225   }
226
227   /**
228    * This method is called when iterator's last and previous methods are called.
229    * Checks the successive enteries of the full text iterators untill a common
230    * documentid found, if document having all words found (same documentid ),
231    * returns true, otherwise false.
232    * @param index
233    * @return
234    * @throws DException
235    */

236   private boolean alignBackward(int index) throws DException {
237     Object JavaDoc value = fullTextIndex[index].getColumnValues(documentIdColumn);
238     while(true){
239       for(int i = 0; i < fullTextIndex.length; i++){
240          if(compare(value,fullTextIndex[i].getColumnValues(documentIdColumn)) == 0)
241            continue;
242         if(!fullTextIndex[i].seekFromBottomRelative(new Object JavaDoc[] {keyWords[i], value} ))
243           return false;
244         Object JavaDoc nValue = fullTextIndex[i].getColumnValues(documentIdColumn);
245         value = compare(nValue,value) < 0 ? nValue : value;
246       }
247       boolean b = haveSameDocumentId();
248       if(b)
249         return true;
250     }
251   }
252
253   /**
254    * This method sets the value of the pk of the particular word/term from the
255    * Token Table to get its location(s) from the Location Table. Location table
256    * has the foriegn key relation with Token Table. The value of Pk is set to
257    * reference of the bve i.e. (fid = ?) to .
258    * The set of the PK values corresponding to aligned token iterators is
259    * maintained that will seek the records of location iterators, first
260    * indexing on fpk an then retrieving the locations.
261    * @throws DException
262    */

263   public Object JavaDoc[] setValues() throws DException {
264     int length = fullTextIndex.length;
265     Object JavaDoc[] pkValues = new Object JavaDoc[length];
266     for(int i = 0; i < length; i++){
267       pkValues[i] = fullTextIndex[i].getColumnValues(pkColumn);
268       locationIndex[i].setConditionVariableValue(new _Reference[]{fpkReference[i]} , new Object JavaDoc[] {pkValues[i]}, 0);
269     }
270     return pkValues;
271   }
272
273   /**
274    * Compares the two long values, returns > 0 if ob1>ob2, < 0 if ob1<ob2 and
275    * 0 if ob1=ob2.
276    * @param ob1
277    * @param ob2
278    * @return
279    * @throws DException
280    */

281   public FieldBase[] fields(_Reference[] references) throws com.daffodilwoods.database.resource.DException {
282     FieldBase[] fbsToRet = new FieldBase[references.length];
283     for (int i = 0; i < references.length; i++) {
284       fbsToRet[i] = field(references[i]);
285     }
286     return fbsToRet;
287   }
288
289   public FieldBase[] fields(int[] columns) throws com.daffodilwoods.database.resource.DException {
290     return (FieldBase[]) getColumnValues(columns);
291   }
292
293   private boolean haveSameDocumentId() throws DException {
294     Object JavaDoc value = fullTextIndex[0].getColumnValues(documentIdColumn);
295     for (int i = 1; i < fullTextIndex.length; i++) {
296       Object JavaDoc nValue = fullTextIndex[i].getColumnValues(documentIdColumn);
297       if ( compare(value , nValue) != 0)
298         return false;
299     }
300     return true;
301   }
302
303   public int compare(Object JavaDoc ob1, Object JavaDoc ob2 ) throws DException{
304     return GetByteComparator.ftComparator.compare(ob1,ob2);
305   }
306   public Object JavaDoc getColumnValues() throws DException {
307     return fullTextIndex[0].getColumnValues();
308   }
309
310   public Object JavaDoc getColumnValues(int[] columns) throws DException {
311     return fullTextIndex[0].getColumnValues(columns);
312   }
313
314   public Object JavaDoc getColumnValues(_Reference[] references) throws DException {
315     Object JavaDoc[] fbsToRet = new Object JavaDoc[references.length];
316     for (int i = 0; i < references.length; i++) {
317       fbsToRet[i] = getColumnValues(references[i]);
318     }
319     return fbsToRet;
320   }
321
322   public Object JavaDoc getColumnValues(_Reference reference) throws DException {
323     return fullTextIndex[0].getColumnValues(reference);
324   }
325
326   public FieldBase field(_Reference reference) throws com.daffodilwoods.database.resource.DException {
327     return fullTextIndex[0].field(reference);
328   }
329 }
330
Popular Tags