KickJava   Java API By Example, From Geeks To Geeks.

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


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.daffodildb.server.datasystem.interfaces._IndexTable;
6 import com.daffodilwoods.daffodildb.server.sql99.fulltext.dml._FullTextIndexInformation;
7 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._Table;
8 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem._IndexInformation;
9 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.booleanvalueexpression;
10 import com.daffodilwoods.daffodildb.server.sql99.utils.parser.ConditionParser;
11 import com.daffodilwoods.daffodildb.server.sql99.utils._VariableValues;
12 import com.daffodilwoods.daffodildb.server.sql99.utils.VariableValues;
13 import com.daffodilwoods.daffodildb.server.sql99.utils.ConditionVariableValue;
14 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.condition.IndexedFilterIterator;
15 import com.daffodilwoods.database.sqlinitiator._Order;
16 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.ColumnObjectsTable;
17 import com.daffodilwoods.daffodildb.server.sql99.common.GeneralPurposeStaticClass;
18 import com.daffodilwoods.daffodildb.server.sql99.common.Datatypes;
19 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference;
20 import com.daffodilwoods.daffodildb.server.sql99.common.ColumnDetails;
21 import com.daffodilwoods.daffodildb.server.sql99.common.TableDetails;
22 import com.daffodilwoods.daffodildb.utils.field.FieldBase;
23
24 /**
25  * It Represents plan,which is generated whenever search pattern is single word.
26  * It provides functionality to get ResultSet on search pattern.
27  * <p>Title: </p>
28  * <p>Description: </p>
29  * <p>Copyright: Copyright (c) 2003</p>
30  * <p>Company: </p>
31  * @author not attributable
32  * @version 1.0
33  */

34
35 public class WordRepresentation implements _Word {//,Comparable
36
/**
37     * this word reprsents object that is to be searched.
38     */

39    FieldBase word ;
40
41    String JavaDoc condition;
42    /**
43     * column name against which search column is searched.
44     */

45    String JavaDoc columnName;
46    /**
47     * It represents condition on table (token table )in which all valid words
48     * are present.Condition is like term = 'word'.
49     */

50    booleanvalueexpression termCondition;
51    /**
52     * This represents condition on table (Location table) in which position of all
53     * valid words are present. Condition is like fpk = ?.
54     */

55    booleanvalueexpression locationCondition;
56    private _Order order;
57
58    public WordRepresentation(FieldBase word0) throws DException{
59       word = word0;
60       condition =new String JavaDoc((byte[]) word.getObject());
61       termCondition = GeneralPurposeStaticClass.getCondition("term = '"+condition+"'",Datatypes.VARCHAR);
62       locationCondition = GeneralPurposeStaticClass.getCondition("fpk = ?",Datatypes.LONG);
63    }
64
65    public WordRepresentation[] getWord(){
66       return new WordRepresentation[]{this};
67    }
68    /**
69     * This method used to get Type of plan.It is required by plan merger to merge
70     * two plan either by AND and OR logical operator.
71     * plan can be of following type
72     * 1.singleword
73     * 2.AndWords
74     * 3.OrWords
75     * 4.notFullTextPredicate
76     * 5.andFullTextPredicate
77     * 6.orfullTextPredicate
78     * 7.notFullTextPredicate
79     * 8.optimizedFullTextPredicate
80     * 9.AndFulltext
81     * 10. AllKeywordSortedPredicate
82     * 11. AnyKeywordSortedPredicate
83     * @return singleWord type
84     */

85    public int getType(){
86       return PredicateConstant.singleWord;
87    }
88    /**
89     * It is required to get columnName of searched column against which search
90     * pattern is searched.
91     * @return columnName
92     * @throws DException
93     */

94    public String JavaDoc getColumn()throws DException{
95       return columnName;
96    }
97    /**
98     * This method used to get object that is to be searched.
99     * eg contains(firstName,'Daffodil')
100     * @return Object
101     * @throws DException
102     */

103    public Object JavaDoc getKeyWords() throws DException{
104       return word;
105    }
106
107    public double getCost(Object JavaDoc obj) throws DException{
108       return 0.0;
109    }
110    /**
111     * It is used to set columnName of searched column against which search
112     * pattern is searched.
113     * @param colName
114     * @throws DException
115     */

116    public void setColumn(String JavaDoc colName) throws DException{
117       columnName = colName;
118    }
119    /**
120     * It allows user to take resultset on the word. Resultset is formed by
121     * applying indexed condition term = 'word' on token table. It ensures only
122     * those documents are returned in which the desired word is present.
123     * @param indexTable
124     * @return _Iterator
125     * @throws DException
126     */

127    public _Iterator execute(Object JavaDoc indexTable) throws DException {
128       _FullTextIndexInformation fti = getFullTextInformation(indexTable);
129       _Iterator tokenIterator = getTokenIterator(fti,indexTable);
130       return GeneralPurposeStaticClass.getIndexedFilteredIterator(tokenIterator, termCondition);
131    }
132
133    /**
134     * This method is used to get Fulltext Index information on column name against
135     * which search pattern is searched.
136     * @param indexTable
137     * @return fullTextIndex
138     * @throws DException
139     */

140    private _FullTextIndexInformation getFullTextInformation(Object JavaDoc indexTable) throws DException{
141       _FullTextIndexInformation[] fullTextIndexInfo = ( (_IndexTable) indexTable).
142             getFullTextIndexInformation();
143       for(int i = 0;i<fullTextIndexInfo.length;i++){
144          String JavaDoc[] columnNames = fullTextIndexInfo[i].getColumnName();
145          if ((columnName.equalsIgnoreCase(fullTextIndexInfo[i].getIndexName())) || (columnNames.length ==1 && (columnName.equalsIgnoreCase(columnNames[0]))))
146             return fullTextIndexInfo[i];
147       }
148       throw new DException("DSE0",new Object JavaDoc[]{"Invalid Column = "+columnName});
149    }
150
151    /**
152     * It is required to get Resultset on Token table with token condition.
153     */

154    public _Iterator getTokenIterator(Object JavaDoc indexTable) throws DException{
155       return execute(indexTable);
156    }
157    /**
158     * It is required to get ResultSet on table in which all valid word(token) are inserted
159     * @param fti
160     * @param indexTable
161     * @return
162     * @throws DException
163     */

164    private _Iterator getTokenIterator(_FullTextIndexInformation fti,Object JavaDoc indexTable) throws DException{
165       _IndexTable token = (_IndexTable)((_IndexTable)indexTable).getDatabase().getTable(fti.getTokenTableName());
166       _IndexInformation[] indexInfo = token.getIndexInformations();
167       for (int i = 0; i < indexInfo.length; i++) {
168          String JavaDoc[] cName = indexInfo[i].getColumns();
169          if(cName[0].equalsIgnoreCase("term")){ // use static constant
170
return token.getIterator(i);
171          }
172       }
173       throw new DException("DSE0",new Object JavaDoc[]{"Invalid FullTextIndex - "+fti});
174    }
175    /**
176     * It is required to get unknown references from from location condition.which is
177     * '?'
178     * @return
179     * @throws DException
180     */

181    public _Reference getFpkReference() throws DException{
182       return locationCondition.getReferences(null)[0];
183    }
184
185    /**
186     * It is required to get resultset on location table with location condition.which
187     * provides those documents which satisfied location condition
188     * @param indexTable
189     * @return
190     * @throws DException
191     */

192    public _Iterator getLocationIterator(Object JavaDoc indexTable) throws DException{
193       _FullTextIndexInformation fti = getFullTextInformation(indexTable);
194       _Iterator locationIterator = getLocationIterator(fti,indexTable);
195       return GeneralPurposeStaticClass.getIndexedFilteredIterator (locationIterator, locationCondition);
196    }
197
198    /**
199     * It is required to get Resultset on location table which provides all records.
200     * ResultSet will be taken on fpk column of location table.
201     * @param fti
202     * @param indexTable
203     * @return
204     * @throws DException
205     */

206    private _Iterator getLocationIterator(_FullTextIndexInformation fti,Object JavaDoc indexTable) throws DException{
207       _IndexTable location = (_IndexTable)((_IndexTable)indexTable).getDatabase().getTable(fti.getLocationTableName());
208       _IndexInformation[] indexInfo = location.getIndexInformations();
209       for (int i = 0; i < indexInfo.length; i++) {
210          String JavaDoc[] cName = indexInfo[i].getColumns();
211          if(cName[0].equalsIgnoreCase("fpk")){ // use static constant
212
return location.getIterator(i);
213          }
214       }
215       throw new DException("DSE0",new Object JavaDoc[]{"Invalid FullTextIndex - "+fti});
216    }
217
218    public double getCost() throws DException {
219       return 0.0;
220    }
221    public double getEstimatedRow() throws DException {
222       return 0.0;
223    }
224    public void setOrder(_Order order) throws DException {
225       this.order = order;
226    }
227    public _Order getOrder() throws DException {
228       return order;
229    }
230    /**
231     * It is required to get ResultSet which provide documents in which search
232     * pattern are present.It provides documents in sorted order.
233     * @param indexTable
234     * @return
235     * @throws DException
236     */

237    public _Iterator executeForSortedResult(Object JavaDoc indexTable) throws DException{
238       return execute(indexTable);
239    }
240    /**
241     * It is used to check equality of two search pattern.It is required by plan
242     * merger during merging of plan of type single word AND andwords,
243     * singleword OR orwords.
244     * @param obj
245     * @return
246     */

247
248    public boolean equals(Object JavaDoc obj){
249       try {
250          return new String JavaDoc((byte[])word.getObject()).equalsIgnoreCase( new String JavaDoc((byte[])((WordRepresentation) obj).word.getObject()));
251       }
252       catch (DException ex) {
253          throw new RuntimeException JavaDoc(ex.getMessage());
254       }
255    }
256
257 }
258
Popular Tags