KickJava   Java API By Example, From Geeks To Geeks.

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


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.daffodildb.server.sql99.dql.iterator._Iterator;
7 import com.daffodilwoods.daffodildb.server.sql99.common.GeneralPurposeStaticClass;
8 import com.daffodilwoods.database.utility.*;
9 import com.daffodilwoods.daffodildb.utils.GetByteComparator;
10 import java.util.Arrays JavaDoc;
11
12 /**
13  * This class handles the navigation of the rows that are returned by applying
14  * the RANK FOUR RETRIEVAL CONDITION i.e. ANY WORDS ( BUT NOT ALL)
15  * This Class implements the Rank4 specific navigation methods and checks the
16  * parsed words in any of the documents provided there must not be all the words
17  * in the same document.
18  * <p>Title: Rank4FTIterator</p>
19  * <p>Description: </p>
20  * <p>Copyright: Copyright (c) 2003</p>
21  * <p>Company: </p>
22  * @author not attributable
23  * @version 1.0
24  */

25 public class Rank4FTIterator extends AnyWordsAbstractIterator {
26
27   public Rank4FTIterator(_Iterator[] fullTextIndex0, _Iterator[] locationIndex0,_Reference[] fpkReference0,FieldBase[] keyWord0) throws DException {
28     fullTextIndex = fullTextIndex0;
29     locationIndex = locationIndex0;
30     pkColumn = GeneralPurposeStaticClass.getColumnDetails("pk");
31     documentIdColumn = GeneralPurposeStaticClass.getColumnDetails("documentId");
32     locationColumn = GeneralPurposeStaticClass.getColumnDetails("location");
33     fpkReference = fpkReference0;
34     keyWords =keyWord0;
35     state = INVALIDSTATE;
36   }
37
38 public static boolean testing;
39
40   /**
41    * Returns true if there exists a row in the iterator that has aleast one set
42    * of the locations of the word that must violate the above 3 rank conditions.
43    * But there must exists at least one word out of the of the parsed words, false otherwise.
44    * @return
45    * @throws DException
46    */

47     public boolean first() throws DException {
48 testing = true;
49       boolean allFalse = true;
50       for (int i = 0; i < fullTextIndex.length; i++)
51         if (fullTextIndex[i].first())
52           allFalse = false;
53       direction = true;
54       state = VALIDSTATE;
55       boolean first = allFalse ? false : allSameWithSmallest() ? next() : true;
56       state = first ? VALIDSTATE : AFTERLAST;
57       return first;
58     }
59
60     /**
61      * This method returns the next valid record from the full-text indexed
62      * table have at least one word in the search criterion.
63      * @return true if valid row is there otherwise false
64      * @throws DException
65      */

66     public boolean next() throws DException {
67       switch(state) {
68           case INVALIDSTATE :
69               throw new DException("DSE4116",null);
70           case BEFOREFIRST :
71               return first();
72           case AFTERLAST :
73               return false;
74       }
75       boolean next = direction ? nextWithForwardDirection() :
76           nextWithOppositeDirection();
77       state = next ? VALIDSTATE : AFTERLAST;
78       return next;
79     }
80
81     /**
82      * This method is called when a record is to be retrieved from table
83      * after forward fetching of records. This method returns false if all
84      * the iterators having no record further. IF all iterators have the
85      * next record, iterator's index with smallest document id is maintained
86      * if all the iterators have the same document id, next record is fetched
87      * from above iterator.
88      * @return
89      * @throws DException
90      */

91     private boolean nextWithForwardDirection() throws DException {
92       boolean[] smallest = getSmallest();
93       for (int i = 0; i < fullTextIndex.length; i++)
94         if (smallest[i])
95           fullTextIndex[i].next();
96       boolean allFalse = allFalse();
97       return allFalse ? false : allSameWithSmallest() ? next() : true;
98     }
99
100     /**
101      * This method handles direction revert operation - when records are
102      * fetching towards upwards and there is call to get a next record.
103      * This method reverts back all of the iterator to the
104      * record position having as same that of before call to previous method.
105      * @return
106      * @throws DException
107      */

108     private boolean nextWithOppositeDirection() throws DException {
109       boolean allFalse = true;
110       for (int i = 0; i < fullTextIndex.length; i++)
111         if(fullTextIndex[i].next())
112           allFalse = false;
113       if (allFalse)
114         return false;
115       if (allSameWithSmallest())
116         return nextWithOppositeDirection();
117       boolean[] smallest = getSmallest();
118       for (int i = 0; i < fullTextIndex.length; i++)
119         if (!smallest[i])
120           fullTextIndex[i].previous();
121       return true;
122     }
123
124     /**
125      * This method returns the previous valid record from the full-text indexed
126      * table have at least one word in the search criterion.
127      * @return true if valid row is there otherwise false
128      * @throws DException
129      */

130     public boolean previous() throws DException {
131       switch (state) {
132         case INVALIDSTATE:
133           throw new DException("DSE4116", null);
134         case AFTERLAST:
135           return last();
136         case BEFOREFIRST:
137           return false;
138       }
139       boolean prev = !direction ? previousWithBackwardDirection()
140           : previousWithOppositeDirection();
141       state = prev ? VALIDSTATE : BEFOREFIRST;
142       return prev;
143     }
144
145     /**
146      * This method is called when a record is to be retrieved from table
147      * after backward fetching of records. This method returns false if all
148      * the iterators having no record further. If all iterators have the
149      * previous record, iterator's index with largest document id is maintained.
150      * if all the iterators have the same document id, previous record is
151      * fetched from above iterator.
152      * @return
153      * @throws DException
154      */

155     private boolean previousWithBackwardDirection() throws DException {
156       boolean[] largest = getLargest();
157       for (int i = 0; i < fullTextIndex.length; i++){
158         if (largest[i])
159            fullTextIndex[i].previous();
160       }
161       return allFalse() ? false : allSameWithLargest() ? previous() : true;
162     }
163
164     /**
165      * This method handles direction revert operation - when records are
166      * fetching towards downwards and there is call to get a record in the
167      * upward direction. This method reverts back all of the iterator to the
168      * record position having as same that of before call to next method.
169      * @return
170      * @throws DException
171      */

172     private boolean previousWithOppositeDirection() throws DException {
173       boolean allFalse = true;
174       for (int i = 0; i < fullTextIndex.length; i++)
175         if(fullTextIndex[i].previous())
176           allFalse = false;
177       if (allFalse)
178         return false;
179       if (allSameWithLargest())
180         return previousWithOppositeDirection();
181       boolean[] largest = getLargest();
182       for (int i = 0; i < fullTextIndex.length; i++)
183         if (!largest[i])
184           fullTextIndex[i].next();
185       return true;
186     }
187
188     /**
189      * Returns true if there exists a row in the iterator that has atleast one set
190      * of the locations of the word that must violate the above 3 rank conditions.
191      * But there must exists at least one word out of the of the parsed words, false otherwise.
192      * @return
193      * @throws DException
194      */

195     public boolean last() throws DException {
196       boolean allFalse = true;
197       for (int i = 0; i < fullTextIndex.length; i++)
198         if (fullTextIndex[i].last())
199           allFalse = false;
200       direction = false;
201       state = VALIDSTATE;
202       boolean last = allFalse ? false : allSameWithLargest() ? previous() : true;
203       state = last ? VALIDSTATE : BEFOREFIRST;
204       return last;
205     }
206
207     public void setSpecificUnderlyingReferences(_Reference[] specificUnderlyingReferences) throws DException{
208         }
209
210     public String JavaDoc toString(){
211         String JavaDoc str = "Rank4FTIterator";
212         str += "[FullTextIndex"+fullTextIndex[0]
213             +"]";
214         return str;
215     }
216 }
217
Popular Tags