KickJava   Java API By Example, From Geeks To Geeks.

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


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  * <p>Title: AnyKeyWordsIterator</p>
14  * <p>Description: This Class handles the documents that have at least
15  * (including all words) one word in search criterion in the same document.
16  * If none of the parsed words is present, that particular document is rejected.
17  * </p>
18  * <p>Copyright: Copyright (c) 2003</p>
19  * <p>Company: </p>
20  * @author not attributable
21  * @version 1.0
22  */

23 public class AnyKeyWordsIterator extends AnyWordsAbstractIterator {
24
25   public AnyKeyWordsIterator(_Iterator[] fullTextIndex0,FieldBase[] keyWord0) throws DException {
26     fullTextIndex = fullTextIndex0;
27     pkColumn = GeneralPurposeStaticClass.getColumnDetails("pk");
28     documentIdColumn = GeneralPurposeStaticClass.getColumnDetails("documentId");
29     keyWords =keyWord0;
30   }
31
32     /**
33      * This method returns the first record from the full-text indexed table
34      * have at least one word in the search criterion.
35      * @return true if valid row is there otherwise false
36      * @throws DException
37      */

38     public boolean first() throws DException {
39       boolean allFalse = true;
40       for (int i = 0; i < fullTextIndex.length; i++)
41         if (fullTextIndex[i].first())
42           allFalse = false;
43       direction = true;
44       if(allFalse) {
45         state = AFTERLAST;
46         return false;
47       }
48       initializeIteratorIndexWithSmallest();
49       state = VALIDSTATE;
50       return true;
51     }
52
53     /**
54      * This method returns the next valid record from the full-text indexed
55      * table have at least one word in the search criterion.
56      * @return true if valid row is there otherwise false
57      * @throws DException
58      */

59     public boolean next() throws DException {
60       switch(state) {
61           case INVALIDSTATE :
62               throw new DException("DSE4116",null);
63           case BEFOREFIRST :
64               return first();
65           case AFTERLAST :
66               return false;
67       }
68       boolean next = direction ? nextWithForwardDirection() :
69           nextWithOppositeDirection();
70       state = next ? VALIDSTATE : AFTERLAST;
71       return next;
72     }
73     /**
74      * This method is called when a record is to be retrieved from table
75      * after forward fetching of records. This method returns false if all
76      * the iterators having no record further otherwise false whether all the
77      * iterators corresponds to same document id (skipped from rank4 iterator)
78      * @return true if record found, false otherwise.
79      * @throws DException
80      */

81     private boolean nextWithForwardDirection() throws DException {
82       boolean[] smallest = getSmallest();
83       for (int i = 0; i < fullTextIndex.length; i++)
84         if (smallest[i])
85           fullTextIndex[i].next();
86       boolean allFalse = allFalse();
87       if(allFalse)
88         return false;
89       initializeIteratorIndexWithSmallest();
90       return true;
91     }
92
93     /**
94      * This method handles direction revert operation - when records are
95      * fetching towards upwards and there is call to get a record in the
96      * downward direction.
97      * @return
98      * @throws DException
99      */

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

122     public boolean previous() throws DException {
123       switch (state) {
124         case INVALIDSTATE:
125           throw new DException("DSE4116", null);
126         case AFTERLAST:
127           return last();
128         case BEFOREFIRST:
129           return false;
130       }
131       boolean prev = !direction ? previousWithBackwardDirection()
132           : previousWithOppositeDirection();
133       state = prev ? VALIDSTATE : BEFOREFIRST;
134       return prev;
135     }
136
137     /**
138      * This method is called when a record is to be retrieved from table
139      * after backward fetching (moving upward) of records. This method
140      * returns false if all the iterators having no record further.
141      * @return
142      * @throws DException
143      */

144     private boolean previousWithBackwardDirection() throws DException {
145       boolean[] largest = getLargest();
146       for (int i = 0; i < fullTextIndex.length; i++){
147         if (largest[i])
148            fullTextIndex[i].previous();
149       }
150       if(allFalse())
151         return false;
152       initializeIteratorIndexWithLargest();
153       return true;
154     }
155
156     /**
157      * This method handles direction revert operation - when records are
158      * fetching towards downwards and there is call to get a record in the
159      * upward direction.
160      * @return
161      * @throws DException
162      */

163     private boolean previousWithOppositeDirection() throws DException {
164       boolean allFalse = true;
165       for (int i = 0; i < fullTextIndex.length; i++)
166         if(fullTextIndex[i].previous())
167           allFalse = false;
168       if (allFalse) {
169         return false;
170       }
171       if (allSameWithLargest())
172         return true;
173       boolean[] largest = getLargest();
174       for (int i = 0; i < fullTextIndex.length; i++)
175         if (!largest[i])
176           fullTextIndex[i].next();
177       return true;
178     }
179
180
181     /**
182      * This method returns the last valid record from the full-text indexed
183      * table have at least one word in the search criterion.
184      * @return true if valid row is there otherwise false
185      * @throws DException
186      */

187     public boolean last() throws DException {
188       boolean allFalse = true;
189        for (int i = 0; i < fullTextIndex.length; i++)
190          if (fullTextIndex[i].last())
191            allFalse = false;
192        direction = false;
193        if(allFalse) {
194          state = BEFOREFIRST;
195          return false;
196        }
197        initializeIteratorIndexWithLargest();
198        state = VALIDSTATE;
199        return true;
200     }
201
202   /**
203    * This method is called when there exists any record during
204    * forward fetching. It initializes the index of the partcular
205    * full-text iterator that has the minimum document id.
206    * @throws DException
207    */

208   private void initializeIteratorIndexWithSmallest() throws DException {
209     Object JavaDoc min = null;
210     for (int i = 0; i < fullTextIndex.length; i++) {
211       Object JavaDoc nValue = null;
212       try {
213         nValue = fullTextIndex[i].getColumnValues(documentIdColumn);
214       }
215       catch (DException ex) {
216         continue;
217       }
218       if (min == null) {
219         min = nValue;
220         iterIndex = i;
221         continue;
222       }
223       int cmp = compare(nValue, min);
224       if (cmp < 0) {
225         iterIndex = i;
226         min = nValue;
227       }
228     }
229   }
230
231   /**
232    * This method is called when there exists any record during
233    * backward fetching. It initializes the index of the partcular
234    * full-text iterator that has the maximum document id.
235    * @throws DException
236    */

237   private void initializeIteratorIndexWithLargest() throws DException {
238     Object JavaDoc max = null;
239     for (int i = 0; i < fullTextIndex.length; i++) {
240       Object JavaDoc nValue = null;
241       try {
242         nValue = fullTextIndex[i].getColumnValues(documentIdColumn);
243       }
244       catch (DException ex) {
245         continue;
246       }
247       if (max == null) {
248         max = nValue;
249         iterIndex = i;
250         continue;
251       }
252       int cmp = compare(nValue, max);
253       if (cmp > 0) {
254         iterIndex = i;
255         max = nValue;
256       }
257     }
258   }
259   public void setSpecificUnderlyingReferences(_Reference[] specificUnderlyingReferences) throws DException{
260       }
261
262   public String JavaDoc toString(){
263       String JavaDoc str = "AnyKeyWordsIterator";
264       str += "[FullTextIndex"+fullTextIndex[0]
265           +"]";
266       return str;
267   }
268 }
269
Popular Tags