KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > fulltext > dql > condition > PhraseRepresentation


1 package com.daffodilwoods.daffodildb.server.sql99.fulltext.dql.condition;
2
3 import com.daffodilwoods.database.resource.DException;
4 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator._Iterator;
5 import com.daffodilwoods.database.sqlinitiator._Order;
6 import com.daffodilwoods.daffodildb.server.sql99.fulltext.dml.
7
    _FullTextIndexInformation;
8 import com.daffodilwoods.daffodildb.server.sql99.fulltext.dql.iterator.
9
    Rank1FTIterator;
10 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference;
11 import com.daffodilwoods.daffodildb.utils.field.FieldBase;
12 import com.daffodilwoods.daffodildb.server.sql99.fulltext.dql.iterator.
13
    Rank2FTIterator;
14 import com.daffodilwoods.daffodildb.server.sql99.fulltext.dql.iterator.
15
    Rank3FTIterator;
16 import com.daffodilwoods.daffodildb.server.sql99.fulltext.dql.iterator.
17
    Rank4FTIterator;
18 import com.daffodilwoods.daffodildb.server.sql99.common.
19
    GeneralPurposeStaticClass;
20 import com.daffodilwoods.daffodildb.server.sql99.common.Datatypes;
21 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.set.
22
    UnionAllIterator;
23 import com.daffodilwoods.daffodildb.server.sql99.fulltext.dql.iterator.
24
    AnyKeyWordsIterator;
25
26 /**
27  * It represents plan which is generated when search pattern is a sentence.i.e
28  * combination of more than one word. eg 'Daffodil is a software company'
29  * It provides functionality of getting ResultSet.which provides those documnets
30  * in which search pattern is present.
31  * <p>Title: </p>
32  * <p>Description: </p>
33  * <p>Copyright: Copyright (c) 2003</p>
34  * <p>Company: </p>
35  * @author not attributable
36  * @version 1.0
37  */

38
39 public class PhraseRepresentation
40     implements _Word {
41   /**
42    * This represents list of word that form a complete search pattern
43    */

44   WordRepresentation[] wordList;
45   private _Order order;
46
47   /**
48    * It represents name of column against which searched column is searched
49    */

50   String JavaDoc columnName;
51
52   public PhraseRepresentation(WordRepresentation[] wordList) {
53     this.wordList = wordList;
54   }
55
56   /**
57    * This method used to get Type of plan.It is required by plan merger to merge
58    * two plan either by AND and OR logical operator.
59    */

60   public int getType() {
61     return PredicateConstant.phrase;
62   }
63
64   /**
65    * It is required to get columnName of searched column against which search
66    * pattern is searched.
67    * @return columnName
68    * @throws DException
69    */

70   public String JavaDoc getColumn() throws DException {
71     return columnName = wordList[0].getColumn();
72   }
73
74   /**
75    * It is used to set columnName of searched column against which search
76    * pattern is searched.
77    * @param colName
78    * @throws DException
79    */

80   public void setColumn(String JavaDoc colName) throws DException {
81     for (int i = 0; i < wordList.length; i++) {
82       wordList[i].setColumn(colName);
83     }
84   }
85
86   /**
87    * It is used to get list of word that form a search pattern
88    * @return
89    * @throws DException
90    */

91   public WordRepresentation[] getWord() throws DException {
92     return wordList;
93   }
94
95   public double getCost() throws DException {
96     return 0.0;
97   }
98
99   public double getCost(Object JavaDoc session) throws DException {
100     return 0.0;
101   }
102
103   /**
104    * It is required for plan execution.It gives resultset,which provides those
105    * documents in which search pattern is present.For each individual word in
106    * search pattern we take resultset from token table and location table and also take
107    * unknown references of location condtion, then make final resultset.
108    * @param session
109    * @return
110    * @throws DException
111    */

112   public _Iterator execute(Object JavaDoc session) throws DException {
113     int length = wordList.length;
114     _Iterator tokenIterator[] = new _Iterator[length];
115     _Iterator locationIterator[] = new _Iterator[length];
116     _Reference fpkReference[] = new _Reference[length];
117     FieldBase[] keyWords = new FieldBase[length];
118     for (int i = 0; i < length; i++) {
119       tokenIterator[i] = wordList[i].getTokenIterator(session);
120       locationIterator[i] = wordList[i].getLocationIterator(session);
121       fpkReference[i] = wordList[i].getFpkReference();
122       keyWords[i] = (FieldBase) wordList[i].getKeyWords();
123     }
124     /** All 4 iterators are used to handle the case like:
125      * phrase: "daffodil software ltd"
126      * it should fetch records which has all the 3 words above
127      * records with exact match
128      * records with any word
129      * records with words without 0 gap
130      * and records with any sequence of above words.
131  */

132     _Iterator rank1 = new Rank1FTIterator(tokenIterator, locationIterator,
133                                           fpkReference, keyWords);
134     _Iterator rank2 = new Rank2FTIterator(tokenIterator, locationIterator,
135                                           fpkReference, keyWords);
136
137     _Iterator rank3 = new Rank3FTIterator(tokenIterator, locationIterator,
138                                           fpkReference, keyWords);
139     _Iterator rank4 = new Rank4FTIterator(tokenIterator, locationIterator,
140                                           fpkReference, keyWords);
141     return getUnionIterator(rank1, rank2, rank3, rank4);
142   }
143
144   private _Iterator getUnionIterator(_Iterator rank1, _Iterator rank2,
145                                      _Iterator rank3, _Iterator rank4) throws
146       DException {
147     int[] appropriateDataType = new int[] {
148         Datatypes.LONGSIZE};
149     _Reference[] leftReferences = new _Reference[] {
150         GeneralPurposeStaticClass.getColumnDetails("documentId")};
151     _Reference[] rightReferences = new _Reference[] {
152         GeneralPurposeStaticClass.getColumnDetails("documentId")};
153     _Reference[] orderLeftColumnDetails = new _Reference[] {
154         GeneralPurposeStaticClass.getColumnDetails("documentId")};
155     _Reference[] orderRightColumnDetails = new _Reference[] {
156         GeneralPurposeStaticClass.getColumnDetails("documentId")};
157             UnionAllIterator iter = new UnionAllIterator(rank1, rank2, leftReferences,
158                                                  rightReferences,
159                                                  appropriateDataType,
160                                                  appropriateDataType,
161                                                  orderLeftColumnDetails,
162                                                  orderRightColumnDetails);
163     _Reference[] leftReferences1 = new _Reference[] {
164         GeneralPurposeStaticClass.getColumnDetails("documentId")};
165     _Reference[] rightReferences1 = new _Reference[] {
166         GeneralPurposeStaticClass.getColumnDetails("documentId")};
167     _Reference[] orderLeftColumnDetails1 = new _Reference[] {
168         GeneralPurposeStaticClass.getColumnDetails("documentId")};
169     _Reference[] orderRightColumnDetails1 = new _Reference[] {
170         GeneralPurposeStaticClass.getColumnDetails("documentId")};
171     UnionAllIterator iter1 = new UnionAllIterator(rank3, rank4, leftReferences1,
172                                                   rightReferences1,
173                                                   appropriateDataType,
174                                                   appropriateDataType,
175                                                   orderLeftColumnDetails1,
176                                                   orderRightColumnDetails1);
177     _Reference[] leftReferences2 = new _Reference[] {
178         GeneralPurposeStaticClass.getColumnDetails("documentId")};
179     _Reference[] rightReferences2 = new _Reference[] {
180         GeneralPurposeStaticClass.getColumnDetails("documentId")};
181     _Reference[] orderLeftColumnDetails2 = new _Reference[] {
182         GeneralPurposeStaticClass.getColumnDetails("documentId")};
183     _Reference[] orderRightColumnDetails2 = new _Reference[] {
184         GeneralPurposeStaticClass.getColumnDetails("documentId")};
185     return new UnionAllIterator(iter, iter1, leftReferences2, rightReferences2,
186                                 appropriateDataType, appropriateDataType,
187                                 orderLeftColumnDetails2,
188                                 orderRightColumnDetails2);
189
190   }
191
192   public double getEstimatedRow() throws DException {
193     return 1;
194   }
195
196   public void setOrder(_Order order) throws DException {
197     this.order = order;
198   }
199
200   public _Order getOrder() throws DException {
201     return order;
202   }
203
204   /**
205    * It is required to get ResultSet which provide documents in which search
206    * pattern are present.It provides documents in sorted order.
207    * @param indexTable
208    * @return
209    * @throws DException
210    */

211   public _Iterator executeForSortedResult(Object JavaDoc indexTable) throws DException {
212     _Iterator tokenIterator[] = getTokenIterator(indexTable);
213     FieldBase keyWord[] = getKeyWords(indexTable);
214     AnyKeyWordsIterator iter = new AnyKeyWordsIterator(
215         tokenIterator, keyWord);
216     return iter;
217
218   }
219
220   /**
221    * This method is required to get resultset on every word in serach pattern which is
222    * to be searched. ResultSet of every word provide desired document
223    * in which that search pattern is present.
224    * @param session
225    * @return _Iterator[]
226    * @throws DException
227    */

228   private _Iterator[] getTokenIterator(Object JavaDoc session) throws DException {
229     int length = wordList.length;
230     _Iterator tokenIterator[] = new _Iterator[length];
231     for (int i = 0; i < length; i++) {
232       tokenIterator[i] = wordList[i].getTokenIterator(session);
233     }
234     return tokenIterator;
235   }
236
237   /**
238    * Returns the single words.
239    * @param session
240    * @return FieldBase
241    * @throws DException
242    */

243   private FieldBase[] getKeyWords(Object JavaDoc session) throws DException {
244     int length = wordList.length;
245     FieldBase[] KeyWords = new FieldBase[length];
246     for (int i = 0; i < length; i++) {
247       KeyWords[i] = (FieldBase) wordList[i].getKeyWords();
248     }
249     return KeyWords;
250   }
251
252 }
253
Popular Tags