KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > fulltext > database > DaffodilFullTextIndexCreator


1 package com.daffodilwoods.daffodildb.server.sql99.fulltext.database;
2
3 import com.daffodilwoods.fulltext.common._FullTextCreator;
4 import com.daffodilwoods.database.resource.DException;
5 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._IndexDatabase;
6 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.Datatype;
7 import com.daffodilwoods.database.general.QualifiedIdentifier;
8 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem._IndexInformation;
9 import com.daffodilwoods.daffodildb.server.datadictionarysystem.IndexInformation;
10 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.ColumnInformation;
11 import java.util.ArrayList JavaDoc;
12 import com.daffodilwoods.daffodildb.server.datadictionarysystem.SystemTables;
13 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._DatabaseUser;
14
15 public class DaffodilFullTextIndexCreator implements _FullTextCreator{
16
17    /**
18     * Create Token Table (pk long,term Varchar(50),documentId long)
19     * Create Index on term + documentId
20     * Create Location Table(fpk foriegn key of pk of Token Table,location long,documentId long)
21     * Create index on fpk,location,documentId
22     */

23
24    /**
25     * An instance of Index database that perfoms the actual data system level
26     * tables and index creation.
27     */

28    private _IndexDatabase indexDataBase;
29    public final static Integer JavaDoc SQLIdentifierSize = new Integer JavaDoc(128);
30
31    public DaffodilFullTextIndexCreator(_IndexDatabase indexDataBase0){
32       indexDataBase = indexDataBase0;
33    }
34
35    /**
36     * This method performs token and location tables and required indexes
37     * creation on both of the tables. For actual creation of tables and
38     * indexes transfers the call to index data base instance.
39     * @param tableName having the column on which full text index is created
40     * @param indexName name of the index
41     * @param columnName name of the column on whcih full text index is created
42     * @return an array having the token and location table names
43     * @throws DException
44     */

45    public Object JavaDoc createFullTextIndex(Object JavaDoc tableName,String JavaDoc indexName,String JavaDoc[] columnName) throws DException{
46       QualifiedIdentifier identifier = (QualifiedIdentifier)tableName;
47       String JavaDoc tokenTableName = createTokenTable(identifier,indexName);
48       String JavaDoc locationTableName = createLocationTable(identifier,indexName);
49       return new String JavaDoc[]{tokenTableName,locationTableName};
50    }
51
52    /**
53     * This method performs the token table creation and an composite index on
54     * term and documentid column.
55     * @param table having the column on which full text index is created
56     * @param indexName name of the index
57     * @return token table name
58     * @throws DException
59     */

60    private String JavaDoc createTokenTable(QualifiedIdentifier table,String JavaDoc indexName) throws DException{
61       String JavaDoc tableName = indexName+"_token_"+System.currentTimeMillis();
62       QualifiedIdentifier token = new QualifiedIdentifier(table.catalog,table.schema,tableName);
63       indexDataBase.createTable(token,createTokenTableStructure());
64       createIndexOnToken(token,tableName);
65       return tableName;
66    }
67
68    /**
69     * Performs the composite index creation on token table on columns term
70     * and documentid. The composite index on term and document id performs
71     * fast retrieval of the records from the token table.
72     * @param table qualified name of token table on which index to create
73     * @param tableName table name of token table on which index to create
74     * @throws DException
75     */

76    private void createIndexOnToken(QualifiedIdentifier table,String JavaDoc tableName) throws DException{
77       String JavaDoc indexName = tableName+"_termdocumentidindex";
78       String JavaDoc indexTableName = indexName+"_termdocumentidtable";
79       String JavaDoc[] columns = new String JavaDoc[]{"term","documentid"};
80       int[] columnIndexes = new int[]{1,2};
81       boolean[] orderType = new boolean[]{true,true};
82
83       ArrayList JavaDoc tablesToLock = new ArrayList JavaDoc();
84       QualifiedIdentifier tableName1 = new QualifiedIdentifier(table.catalog, table.schema,tableName);
85       tablesToLock.add(tableName1);
86       tablesToLock.add(SystemTables.DATABASEINDEXINFO);
87       tablesToLock.add(SystemTables.DATABASEINDEXCOLUMNS);
88       _DatabaseUser user = indexDataBase.getDatabaseUser(tablesToLock);
89       try{
90       _IndexInformation indexInfo = new IndexInformation(table,indexName,indexTableName,columns,columnIndexes,orderType,0,0,0,0,true,false);
91       ((IndexInformation)indexInfo).setDefault();
92       indexDataBase.createPermanantIndex(table, indexName, indexInfo, user);
93
94       indexName = tableName+"_documentidindex";
95       indexTableName = indexName+"_documentidtable";
96       columns = new String JavaDoc[]{"documentid"};
97       columnIndexes = new int[]{2};
98       orderType = new boolean[]{true};
99       indexInfo = new IndexInformation(table,indexName,indexTableName,columns,columnIndexes,orderType,0,0,0,0,false,false);
100       ((IndexInformation)indexInfo).setDefault();
101       indexDataBase.createPermanantIndex(table, indexName, indexInfo, user);
102
103       indexName = tableName+"_pkindex";
104       indexTableName = indexName+"_pktable";
105       columns = new String JavaDoc[]{"pk"};
106       columnIndexes = new int[]{0};
107       orderType = new boolean[]{true};
108       indexInfo = new IndexInformation(table,indexName,indexTableName,columns,columnIndexes,orderType,0,0,0,0,false,false);
109       ((IndexInformation)indexInfo).setDefault();
110       indexDataBase.createPermanantIndex(table, indexName, indexInfo, user);
111       user.writeToFile();
112       }catch(DException ex){
113           user.rollback();
114       }finally{
115         user.releaseCluster();
116       }
117    }
118
119    /**
120     * This method performs the location table creation and three seperate index
121     * on all of the columns {fpk, location, documentid} of the location table.
122     * term and documentid column.
123     * @param table having the column on which full text index is created
124     * @param indexName name of the index
125     * @return token table name
126     * @throws DException
127     */

128    private String JavaDoc createLocationTable(QualifiedIdentifier table,String JavaDoc indexName) throws DException{
129       String JavaDoc tableName = indexName+"_location_"+System.currentTimeMillis();
130       QualifiedIdentifier location = new QualifiedIdentifier(table.catalog,table.schema,tableName);
131       indexDataBase.createTable(location,createLocationTableStructure());
132       createIndexOnLocation(location,tableName);
133       return tableName;
134    }
135
136    /**
137     * Creates seperate indexes on each of the columns of location table.
138     * The index on column 'fpk' performs fast retrieval of token corresonding
139     * records from the location table. The index on column 'documentid' performs
140     * fast retrieval of records while delete operation on actual table. The index
141     * on column 'location' performs fast retrieval of records while rank iterators
142     * operation.
143     * @param table qualified name of token table on which index to create
144     * @param tableName table name of token table on which index to create
145     * @throws DException
146     */

147    private void createIndexOnLocation(QualifiedIdentifier table,String JavaDoc tableName) throws DException{
148       String JavaDoc indexName = tableName+"_fpklocationindex";
149       String JavaDoc indexTableName = indexName+"_fpklocationtable";
150       String JavaDoc[] columns = new String JavaDoc[]{"fpk","location"};
151       int[] columnIndexes = new int[]{0,1};
152       boolean[] orderType = new boolean[]{true,true};
153       ArrayList JavaDoc tablesToLock = new ArrayList JavaDoc();
154       QualifiedIdentifier tab = new QualifiedIdentifier(table.catalog,table.schema,tableName);
155       tablesToLock.add(tab);
156       tablesToLock.add(SystemTables.DATABASEINDEXINFO);
157       tablesToLock.add(SystemTables.DATABASEINDEXCOLUMNS);
158       _DatabaseUser user = indexDataBase.getDatabaseUser(tablesToLock);
159       try{
160
161       _IndexInformation indexInfo = new IndexInformation(table,indexName,indexTableName,columns,columnIndexes,orderType,0,0,0,0,false,false);
162       ((IndexInformation)indexInfo).setDefault();
163       indexDataBase.createPermanantIndex(table, indexName, indexInfo, user);
164
165
166       indexName = tableName+"_documentidindex";
167       indexTableName = indexName+"_documentidtable";
168       columns = new String JavaDoc[]{"documentid"};
169       columnIndexes = new int[]{2};
170       orderType = new boolean[]{true};
171       indexInfo = new IndexInformation(table,indexName,indexTableName,columns,columnIndexes,orderType,0,0,0,0,false,false);
172       ((IndexInformation)indexInfo).setDefault();
173       indexDataBase.createPermanantIndex(table, indexName, indexInfo,user);
174       user.writeToFile();
175       }catch(DException ex){
176           user.rollback();
177       }finally{
178         user.releaseCluster();
179       }
180
181    }
182
183    /**
184     * Returns the structure of token table.
185     * @return
186     */

187    private ColumnInformation createTokenTableStructure(){
188      ColumnInformation ci = new ColumnInformation();
189      ci.setObjects( new Object JavaDoc[][]{
190        {"pk",new Long JavaDoc(Datatype.LONG),new Integer JavaDoc(Datatype.LONGSIZE),new Long JavaDoc(0) },
191         {"term",new Long JavaDoc(Datatype.VARCHAR),SQLIdentifierSize, new Long JavaDoc(1)},
192        {"documentId" ,new Long JavaDoc(Datatype.LONG),new Integer JavaDoc(Datatype.LONGSIZE),new Long JavaDoc(2) },
193
194      });
195      return ci;
196    }
197
198    /**
199     * Returns the structure of location table.
200     * @return
201     */

202    private ColumnInformation createLocationTableStructure(){
203      ColumnInformation ci = new ColumnInformation();
204      ci.setObjects( new Object JavaDoc[][]{
205         {"fpk",new Long JavaDoc(Datatype.LONG),new Integer JavaDoc(Datatype.LONGSIZE),new Long JavaDoc(0) },
206         {"location",new Long JavaDoc(Datatype.LONG),new Integer JavaDoc(Datatype.LONGSIZE),new Long JavaDoc(1) },
207        {"documentId" ,new Long JavaDoc(Datatype.LONG),new Integer JavaDoc(Datatype.LONGSIZE),new Long JavaDoc(2) },
208
209      });
210      return ci;
211    }
212 }
213
Popular Tags