KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > sql > dictionary > CatalogRowFactory


1 /*
2
3    Derby - Class org.apache.derby.iapi.sql.dictionary.CatalogRowFactory
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.sql.dictionary;
23
24 import org.apache.derby.iapi.reference.Property;
25 import org.apache.derby.iapi.util.StringUtil;
26
27 import org.apache.derby.iapi.services.context.ContextService;
28 import org.apache.derby.iapi.services.monitor.Monitor;
29 import org.apache.derby.iapi.services.sanity.SanityManager;
30 import org.apache.derby.iapi.error.StandardException;
31 import org.apache.derby.iapi.types.DataValueFactory;
32 import org.apache.derby.iapi.sql.execute.ExecutionFactory;
33 import org.apache.derby.iapi.sql.execute.ExecIndexRow;
34 import org.apache.derby.iapi.sql.execute.ExecRow;
35 import org.apache.derby.iapi.sql.execute.ExecutionContext;
36 import org.apache.derby.iapi.types.DataTypeDescriptor;
37 import org.apache.derby.iapi.types.DataValueFactory;
38 import org.apache.derby.iapi.types.RowLocation;
39 import org.apache.derby.iapi.store.raw.RawStoreFactory;
40 import org.apache.derby.iapi.services.uuid.UUIDFactory;
41 import org.apache.derby.catalog.UUID;
42 import java.util.Properties JavaDoc;
43
44 /**
45  * Superclass of all row factories.
46  *
47  *
48  * @version 0.2
49  * @author Rick Hillegas
50  * @author Manish Khettry
51  */

52
53 public abstract class CatalogRowFactory
54 {
55     ///////////////////////////////////////////////////////////////////////////
56
//
57
// STATE
58
//
59
///////////////////////////////////////////////////////////////////////////
60

61
62     protected String JavaDoc[] indexNames;
63     protected int[][] indexColumnPositions;
64     protected boolean[] indexUniqueness;
65
66     protected UUID tableUUID;
67     protected UUID heapUUID;
68     protected UUID[] indexUUID;
69
70     protected DataValueFactory dvf;
71     private final ExecutionFactory ef;
72     private UUIDFactory uuidf;
73
74     private boolean convertIdToLower;
75     private int indexCount;
76     private int columnCount;
77     private String JavaDoc catalogName;
78
79     ///////////////////////////////////////////////////////////////////////////
80
//
81
// CONSTRUCTORS
82
//
83
///////////////////////////////////////////////////////////////////////////
84

85     public CatalogRowFactory(UUIDFactory uuidf,
86                              ExecutionFactory ef,
87                              DataValueFactory dvf,
88                              boolean convertIdToLower)
89                                  
90     {
91         this.uuidf = uuidf;
92         this.dvf = dvf;
93         this.ef = ef;
94         this.convertIdToLower = convertIdToLower;
95     }
96
97     /**
98       * Gets a ExecutionFactory
99       *
100       * @return an execution factory
101       */

102     public ExecutionFactory getExecutionFactory() {return ef;}
103
104     /**
105       * Get the UUID factory
106       *
107       * @return the UUID factory
108       */

109     public UUIDFactory getUUIDFactory() { return uuidf; }
110
111     /* Override the following methods in sub-classes if they have any
112      * indexes.
113      */

114
115      /**
116        * Get the UUID of this catalog. This is the hard-coded uuid for
117        * this catalog that is generated for releases starting with Plato (1.3).
118        * Earlier releases generated their own UUIDs for system objectss on
119        * the fly.
120        *
121        * @return the name of this catalog
122        */

123     public UUID getCanonicalTableUUID() { return tableUUID; }
124
125      /**
126        * Get the UUID of the heap underlying this catalog. See getCanonicalTableUUID()
127        * for a description of canonical uuids.
128        *
129        * @return the uuid of the heap
130        */

131     public UUID getCanonicalHeapUUID() { return heapUUID; }
132
133      /**
134        * Get the UUID of the numbered index. See getCanonicalTableUUID()
135        * for a description of canonical uuids.
136        *
137        * @param indexNumber The (0-based) index number.
138        *
139        * @return the uuid of the heap
140        */

141     public UUID getCanonicalIndexUUID( int indexNumber )
142     {
143         return indexUUID[indexNumber];
144     }
145
146     /**
147      * Get the number of columns in the index for the specified index number.
148      *
149      * @param indexNum The (0-based) index number.
150      *
151      * @return int The number of columns in the index for the specifed index number.
152      */

153     public int getIndexColumnCount(int indexNum)
154     {
155         return indexColumnPositions[indexNum].length;
156     }
157
158     /**
159      * Get the name for the heap conglomerate underlying this catalog.
160      * See getCanonicalTableUUID() for a description of canonical uuids.
161      *
162      * @return String The name for the heap conglomerate.
163      */

164     public String JavaDoc getCanonicalHeapName() { return catalogName + convertIdCase( "_HEAP"); }
165
166     /**
167      * Get the name for the specified index number.
168      *
169      * @param indexNum The (0-based) index number.
170      *
171      * @return String The name for the specified index number.
172      */

173     public String JavaDoc getIndexName(int indexNum)
174     {
175         return indexNames[indexNum];
176     }
177
178     /**
179      * Return whether or not the specified index is unique.
180      *
181      * @param indexNumber The (0-based) index number.
182      *
183      * @return boolean Whether or not the specified index is unique.
184      */

185     public boolean isIndexUnique(int indexNumber)
186     {
187         return (indexUniqueness != null ? indexUniqueness[indexNumber] : true);
188     }
189
190     /**
191       * Gets the DataValueFactory for this connection.
192       *
193       * @return the data value factory for this connection
194       */

195     public DataValueFactory getDataValueFactory() { return dvf; }
196
197     /**
198       * Generate an index name based on the index number.
199       *
200       * @param indexNumber Number of index
201       *
202       * @return the following index name: CatalogName + "_INDEX" + (indexNumber+1)
203       */

204     public String JavaDoc generateIndexName( int indexNumber )
205     {
206         indexNumber++;
207         return catalogName + convertIdCase( "_INDEX") + indexNumber;
208     }
209
210     /** get the number of indexes on this catalog */
211     public int getNumIndexes() { return indexCount; }
212
213     /** get the name of the catalog */
214     public String JavaDoc getCatalogName() { return catalogName; };
215
216     /**
217       * Initialize info, including array of index names and array of
218       * index column counts. Called at constructor time.
219       *
220       * @param columnCount number of columns in the base table.
221       * @param catalogName name of the catalog (the case might have to be converted).
222       * @param indexColumnPositions 2 dim array of ints specifying the base
223       * column positions for each index.
224       * @param indexUniqueness Uniqueness of the indices
225       * @param uuidStrings Array of stringified UUIDs for table and its conglomerates
226       *
227       */

228     public void initInfo(int columnCount,
229                              String JavaDoc catalogName,
230                              int[][] indexColumnPositions,
231                              boolean[] indexUniqueness,
232                              String JavaDoc[] uuidStrings)
233                              
234     {
235         indexCount = (indexColumnPositions != null) ?
236                              indexColumnPositions.length : 0;
237
238         this.catalogName = convertIdCase(catalogName);
239         this.columnCount = columnCount;
240
241         UUIDFactory uf = getUUIDFactory();
242         this.tableUUID = uf.recreateUUID(uuidStrings[0] );
243         this.heapUUID = uf.recreateUUID( uuidStrings[1] );
244
245         if (indexCount > 0)
246         {
247             indexNames = new String JavaDoc[indexCount];
248             indexUUID = new UUID[indexCount];
249             for (int ictr = 0; ictr < indexCount; ictr++)
250             {
251                 indexNames[ictr] = generateIndexName(ictr);
252                 indexUUID[ictr] = uf.recreateUUID(uuidStrings[ictr + 2 ]);
253             }
254             this.indexColumnPositions = indexColumnPositions;
255             this.indexUniqueness = indexUniqueness;
256  
257         }
258     }
259
260     /**
261      * Get the Properties associated with creating the heap.
262      *
263      * @return The Properties associated with creating the heap.
264      */

265     public Properties JavaDoc getCreateHeapProperties()
266     {
267         Properties JavaDoc properties = new Properties JavaDoc();
268         // default properties for system tables:
269
properties.put(Property.PAGE_SIZE_PARAMETER,"1024");
270         properties.put(RawStoreFactory.PAGE_RESERVED_SPACE_PARAMETER,"0");
271         properties.put(RawStoreFactory.MINIMUM_RECORD_SIZE_PARAMETER,"1");
272         return properties;
273     }
274
275     /**
276      * Get the Properties associated with creating the specified index.
277      *
278      * @param indexNumber The specified index number.
279      *
280      * @return The Properties associated with creating the specified index.
281      */

282     public Properties JavaDoc getCreateIndexProperties(int indexNumber)
283     {
284         Properties JavaDoc properties = new Properties JavaDoc();
285         // default properties for system indexes:
286
properties.put(Property.PAGE_SIZE_PARAMETER,"1024");
287         return properties;
288     }
289
290     /**
291       * Get the index number for the primary key index on this catalog.
292       *
293       * @return a 0-based number
294       *
295       */

296     public int getPrimaryKeyIndexNumber()
297     {
298         if (SanityManager.DEBUG)
299             SanityManager.NOTREACHED();
300         return 0;
301     }
302
303     /**
304      * Get the number of columns in the heap.
305      *
306      * @return The number of columns in the heap.
307      */

308     public final int getHeapColumnCount()
309     {
310         return columnCount;
311     }
312
313     protected String JavaDoc convertIdCase( String JavaDoc id)
314     {
315         if( convertIdToLower)
316             return StringUtil.SQLToLowerCase(id);
317         else
318             return id;
319     }
320     
321
322     /**
323      * Return an empty row for this conglomerate.
324      */

325     public ExecRow makeEmptyRow() throws StandardException
326     {
327         return this.makeRow(null, null);
328     }
329
330     /**
331      * most subclasses should provide this method. One or two oddball cases in
332      * Replication and SysSTATEMENTSRowFactory don't. For those we call makeRow
333      * with the additional arguments.
334      */

335     public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent) throws StandardException
336     {
337         if (SanityManager.DEBUG) { SanityManager.THROWASSERT( "Should not get here." ); }
338         return null;
339     }
340
341     // abstract classes that should be implemented by subclasses.
342

343     /** builds a tuple descriptor from a row */
344     public abstract TupleDescriptor
345         buildDescriptor(ExecRow row,
346                         TupleDescriptor parentTuple,
347                         DataDictionary dataDictionary)
348         throws StandardException;
349
350     /** builds a column list for the catalog */
351     public abstract SystemColumn[] buildColumnList();
352
353     /** Return the column positions for a given index number */
354     public int[] getIndexColumnPositions(int indexNumber)
355     {
356         return indexColumnPositions[indexNumber];
357     }
358 }
359
Popular Tags