KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > fulltext > dml > DaffodilFullTextDML


1 package com.daffodilwoods.daffodildb.server.sql99.fulltext.dml;
2
3 import com.daffodilwoods.fulltext.common._FullTextDML;
4 import com.daffodilwoods.fulltext.common._FullTextDocumentParser;
5 import com.daffodilwoods.fulltext.common._FullTextModifications;
6 import com.daffodilwoods.fulltext.common._FullTextAdapter;
7 import com.daffodilwoods.database.resource.DException;
8 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._DatabaseUser;
9 import com.daffodilwoods.daffodildb.utils.field.FieldBase;
10 import com.daffodilwoods.daffodildb.utils.FieldUtility;
11 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.Datatype;
12 import com.daffodilwoods.fulltext.common._Tokenizer;
13 import java.util.TreeSet JavaDoc;
14 import java.util.TreeMap JavaDoc;
15 import com.daffodilwoods.fulltext.common._Token;
16 import java.util.Iterator JavaDoc;
17 import com.daffodilwoods.database.utility.*;
18
19 /**
20  * <p>Title: DaffodilFullTextDML </p>
21  * <p>Description: This Class acts as a interface between updation of actual
22  * full-text indexed enabled table and corresponding updation on the internal
23  * tables required for full-text search.</p>
24  * <p>Copyright: Copyright (c) 2003</p>
25  * <p>Company: </p>
26  * @author not attributable
27  * @version 1.0
28  */

29 public class DaffodilFullTextDML
30     implements _FullTextDML {
31
32   /**
33    * Reference variable for getting tokenizer
34    */

35   private _FullTextDocumentParser documentParser;
36   /**
37    * Dml object corresponding to TOken table
38    */

39   private _FullTextModifications tokenModification;
40   /**
41    * Dml Object corresponding to Location Table.
42    */

43   private _FullTextModifications locationModification;
44   /**
45    * Index of Full-text enabled column in table
46    */

47   private int[] columnIndex;
48   /**
49    * variable to retrive objects used in full-text process.
50    */

51   private _FullTextIndexInformation fullTextIndexInformation;
52
53   public DaffodilFullTextDML(_FullTextDocumentParser documentParser0,
54                              _FullTextAdapter adapter) throws DException {
55     documentParser = documentParser0;
56     tokenModification = adapter.getTokenTable();
57     locationModification = adapter.getLocationTable();
58     columnIndex = adapter.getColumnIndex();
59   }
60
61   /**
62    * Inserts a value of full-text enabled column into token and location
63    * tables for a particular values passed.
64    * For a particular word i.e. term is added into token table only once for
65    * same document. For mutiple terms in the same document, only the enteries
66    * are made into location table with exact location of word in the given
67    * parsed string. Tokens are hashed using the comparator. For
68    * token table insertion last rowid of token table is retrived to insert next
69    * succesive rowid into pk column of token table.
70    * @param user name of the user for which insert is made
71    * @param columnValue parsed string from which tokens to be extracted.
72    * @param pk document id i.e the rowid of the full-text enabled table
73    * @throws DException
74    */

75
76   public void insert(_DatabaseUser user, Object JavaDoc columnValue, Object JavaDoc pk) throws
77       DException {
78     FieldBase[] objs = (FieldBase[]) columnValue;
79     TreeMap JavaDoc map = new TreeMap JavaDoc(new Comparator());
80     long startlocation = 0;
81     for (int i = 0; i < objs.length; i++) {
82       if (!objs[i].isNull()) {
83         _Tokenizer tokenizer = documentParser.getTokenizer(objs[i],
84             startlocation);
85         _Token token=null;
86         long[] loc;
87         while (tokenizer.hasMoreToken()) {
88           token = tokenizer.nextToken();
89           _Token tkn = (_Token) map.get(token);
90           if (tkn == null) {
91             map.put(token, token);
92           }
93           else {
94              loc = token.getLocation();
95             tkn.addLocation(loc);
96           }
97         }
98
99         if(token != null){
100           loc = token.getLocation();
101           startlocation = loc[loc.length - 1] + 1;
102         }
103       }
104     }
105     Iterator JavaDoc iter = map.keySet().iterator();
106     while (iter.hasNext()) {
107       _Token token = (_Token) iter.next();
108       Object JavaDoc lastPK = getLastPK();
109       tokenModification.insert(user, new Object JavaDoc[] {lastPK, token.getTerm(),
110                                pk});
111       long[] locations = token.getLocation();
112       for (int j = 0; j < locations.length; j++) {
113         locationModification.insert(user, new Object JavaDoc[] {lastPK,
114                                     FieldUtility.getField(Datatype.LONG,
115             new Long JavaDoc(locations[j])), pk});
116       }
117     }
118   }
119
120   /**
121    * This method is specifically required for TOKEN table.
122    * This method finds out the last row-id of the token table.
123    * and calculates the rowId (i.e. lastrowid+1) to be used as PK for token
124    * table.
125    * @return pk value to be inserted into token table
126    * @throws DException
127    */

128   private synchronized Object JavaDoc getLastPK() throws DException {
129     Object JavaDoc lastPK = tokenModification.getLastPK();
130     Long JavaDoc result = new Long JavaDoc( ( (Long JavaDoc) ( (FieldBase) lastPK).getObject()).
131                            longValue() + 1);
132     return FieldUtility.getField(Datatype.LONG, result);
133   }
134
135   /**
136    * Updation = Deletion of old version of record to be updated
137    * + Insertion of record with new version of record
138    * @param user
139    * @param oldColumnValue
140    * @param newColumnValue
141    * @param pk
142    * @throws DException
143    */

144   public void update(_DatabaseUser user, Object JavaDoc oldColumnValue,
145                      Object JavaDoc newColumnValue, Object JavaDoc pk) throws DException {
146     Object JavaDoc[] newValues = (Object JavaDoc[]) newColumnValue;
147     Object JavaDoc[] oldValues = (Object JavaDoc[]) oldColumnValue;
148     boolean isSame = true;
149     for (int i = 0; i < newValues.length; i++) {
150       if (!newValues[i].equals(oldValues[i])) {
151         isSame = false;
152         break;
153       }
154     }
155     if (isSame)
156       return;
157     delete(user, pk);
158     insert(user, newColumnValue, pk);
159   }
160
161   /**
162    * Deletes the records from the token and location table
163    * for the document-id passed as argument.
164    * @param user
165    * @param pk
166    * @throws DException
167    */

168   public void delete(_DatabaseUser user, Object JavaDoc pk) throws DException {
169     tokenModification.delete(user, pk);
170     locationModification.delete(user, pk);
171   }
172
173   /**
174    * Determines the full-text enabled column Index
175    * @return
176    */

177   public int[] getColumnIndex() {
178     return this.columnIndex;
179
180   }
181
182   /**
183    * Dml Object stores one instance of index informations
184    * @param fullTextIndexInformation0
185    */

186   public void setFullTextIndexInformation(_FullTextIndexInformation
187                                           fullTextIndexInformation0) {
188     this.fullTextIndexInformation = fullTextIndexInformation0;
189   }
190
191   /**
192    * Determines the indexes information creted for token table/location table
193    * @return
194    */

195   public _FullTextIndexInformation getFullTextIndexInformation() {
196     return this.fullTextIndexInformation;
197   }
198 }
199
Popular Tags