KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.iapi.sql.dictionary.IndexRowGenerator
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.sql.dictionary.ColumnDescriptorList;
25 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
26
27 import org.apache.derby.iapi.sql.execute.ExecutionContext;
28 import org.apache.derby.iapi.sql.execute.ExecIndexRow;
29 import org.apache.derby.iapi.sql.execute.ExecRow;
30 import org.apache.derby.iapi.sql.execute.ExecutionFactory;
31
32 import org.apache.derby.iapi.types.RowLocation;
33 import org.apache.derby.iapi.types.DataTypeDescriptor;
34
35 import org.apache.derby.iapi.services.io.Formatable;
36 import org.apache.derby.iapi.services.io.FormatIdUtil;
37 import org.apache.derby.iapi.services.io.StoredFormatIds;
38
39 import org.apache.derby.iapi.services.sanity.SanityManager;
40
41 import org.apache.derby.iapi.services.context.ContextService;
42
43 import org.apache.derby.iapi.error.StandardException;
44
45 import org.apache.derby.catalog.IndexDescriptor;
46 import org.apache.derby.catalog.types.IndexDescriptorImpl;
47
48 import java.io.ObjectInput JavaDoc;
49 import java.io.ObjectOutput JavaDoc;
50 import java.io.IOException JavaDoc;
51 import org.apache.derby.iapi.services.io.FormatableBitSet;
52
53 /**
54  * This class extends IndexDescriptor for internal use by the
55  * DataDictionary.
56  */

57 public class IndexRowGenerator implements IndexDescriptor, Formatable
58 {
59     IndexDescriptor id;
60     private ExecutionFactory ef;
61
62     /**
63      * Constructor for an IndexRowGeneratorImpl
64      *
65      * @param indexType The type of index
66      * @param isUnique True means the index is unique
67      * @param baseColumnPositions An array of column positions in the base
68      * table. Each index column corresponds to a
69      * column position in the base table.
70      * @param isAscending An array of booleans telling asc/desc on each
71      * column.
72      * @param numberOfOrderedColumns In the future, it will be possible
73      * to store non-ordered columns in an
74      * index. These will be useful for
75      * covered queries.
76      */

77     public IndexRowGenerator(String JavaDoc indexType,
78                                 boolean isUnique,
79                                 int[] baseColumnPositions,
80                                 boolean[] isAscending,
81                                 int numberOfOrderedColumns)
82     {
83         id = new IndexDescriptorImpl(indexType,
84                                     isUnique,
85                                     baseColumnPositions,
86                                     isAscending,
87                                     numberOfOrderedColumns);
88
89         if (SanityManager.DEBUG)
90         {
91             SanityManager.ASSERT(baseColumnPositions != null,
92                 "baseColumnPositions are null");
93         }
94     }
95
96     /**
97      * Constructor for an IndexRowGeneratorImpl
98      *
99      * @param indexDescriptor An IndexDescriptor to delegate calls to
100      */

101     public IndexRowGenerator(IndexDescriptor indexDescriptor)
102     {
103         id = indexDescriptor;
104     }
105
106     /**
107      * Get a template for the index row, to be used with getIndexRow.
108      *
109      * @return A row template for the index row.
110      */

111     public ExecIndexRow getIndexRowTemplate()
112     {
113         return getExecutionFactory().getIndexableRow(
114                                         id.baseColumnPositions().length + 1);
115     }
116
117     /**
118      * Get an index row for this index given a row from the base table
119      * and the RowLocation of the base row. This method can be used
120      * to get the new index row for inserts, and the old and new index
121      * rows for deletes and updates. For updates, the result row has
122      * all the old column values followed by all of the new column values,
123      * so you must form a row using the new column values to pass to
124      * this method to get the new index row.
125      *
126      * @param baseRow A row in the base table
127      * @param rowLocation The RowLocation of the row in the base table
128      * @param indexRow A template for the index row. It must have the
129      * correct number of columns.
130      * @param bitSet If non-null, then baseRow is a partial row and the
131      * set bits in bitSet represents the column mapping for
132      * the partial row to the complete base row. <B> WARNING:
133      * </B> ONE based!!!
134      *
135      * @exception StandardException Thrown on error
136      */

137     public void getIndexRow(ExecRow baseRow,
138                             RowLocation rowLocation,
139                             ExecIndexRow indexRow,
140                             FormatableBitSet bitSet)
141                         throws StandardException
142     {
143         /*
144         ** Set the columns in the index row that are based on columns in
145         ** the base row.
146         */

147         int[] baseColumnPositions = id.baseColumnPositions();
148         int colCount = baseColumnPositions.length;
149
150         if (bitSet == null)
151         {
152             /*
153             ** Set the columns in the index row that are based on columns in
154             ** the base row.
155             */

156             for (int i = 0; i < colCount ; i++)
157             {
158                 indexRow.setColumn(i + 1,
159                         baseRow.getColumn(baseColumnPositions[i]));
160             }
161         }
162         else
163         {
164             if (SanityManager.DEBUG)
165             {
166                 SanityManager.ASSERT(!bitSet.get(0), "element zero of the bitSet passed into getIndexRow() is not false, bitSet should be 1 based");
167             }
168  
169             /*
170             ** Set the columns in the index row that are based on columns in
171             ** the base row.
172             */

173             for (int i = 0; i < colCount; i++)
174             {
175                 int fullColumnNumber = baseColumnPositions[i];
176                 int partialColumnNumber = 0;
177                 for (int index = 1; index <= fullColumnNumber; index++)
178                 {
179                     if (bitSet.get(index))
180                     {
181                         partialColumnNumber++;
182                     }
183                 }
184                 indexRow.setColumn(i + 1,
185                             baseRow.getColumn(partialColumnNumber));
186             }
187         }
188
189         /* Set the row location in the last column of the index row */
190         indexRow.setColumn(colCount + 1, rowLocation);
191     }
192
193     /**
194      * Get a NULL Index Row for this index. This is useful to create objects
195      * that need to be passed to ScanController.
196      *
197      * @param columnList ColumnDescriptors describing the base table.
198      * @param rowLocation empty row location.
199      *
200      * @exception StandardException thrown on error.
201      */

202     public ExecIndexRow getNullIndexRow(ColumnDescriptorList columnList,
203                                         RowLocation rowLocation)
204                 throws StandardException
205     {
206         int[] baseColumnPositions = id.baseColumnPositions();
207         int i;
208         ExecIndexRow indexRow = getIndexRowTemplate();
209
210         for (i = 0; i < baseColumnPositions.length; i++)
211         {
212             DataTypeDescriptor dtd =
213                 columnList.elementAt(baseColumnPositions[i] - 1).getType();
214             indexRow.setColumn(i + 1, dtd.getNull());
215         }
216
217         indexRow.setColumn(i + 1, rowLocation);
218         return indexRow;
219     }
220
221     /**
222      * Return true iff a change to a set of columns changes the index for this
223      * IndexRowGenerator.
224      *
225      * @param changedColumnIds - holds the 1 based column ids for the changed
226      * columns.
227      * @return true iff a change to one of the columns in changedColumnIds
228      * effects this index.
229      */

230     public boolean indexChanged(int[] changedColumnIds)
231     {
232         int[] baseColumnPositions = id.baseColumnPositions();
233
234         for (int ix = 0; ix < changedColumnIds.length; ix++)
235         {
236             for (int iy = 0; iy < baseColumnPositions.length; iy++)
237             {
238                 if (changedColumnIds[ix] == baseColumnPositions[iy])
239                     return true;
240             }
241         }
242         return false;
243     }
244
245          
246     /**
247      * Get the IndexDescriptor that this IndexRowGenerator is based on.
248      */

249     public IndexDescriptor getIndexDescriptor()
250     {
251         return id;
252     }
253
254     /** Zero-argument constructor for Formatable interface */
255     public IndexRowGenerator()
256     {
257     }
258
259     /** @see IndexDescriptor#isUnique */
260     public boolean isUnique()
261     {
262         return id.isUnique();
263     }
264
265     /** @see IndexDescriptor#baseColumnPositions */
266     public int[] baseColumnPositions()
267     {
268         return id.baseColumnPositions();
269     }
270
271     /** @see IndexDescriptor#getKeyColumnPosition */
272     public Integer JavaDoc getKeyColumnPosition(Integer JavaDoc heapColumnPosition)
273     {
274         return id.getKeyColumnPosition(heapColumnPosition);
275     }
276
277     /** @see IndexDescriptor#getKeyColumnPosition */
278     public int getKeyColumnPosition(int heapColumnPosition)
279     {
280         return id.getKeyColumnPosition(heapColumnPosition);
281     }
282
283     /** @see IndexDescriptor#numberOfOrderedColumns */
284     public int numberOfOrderedColumns()
285     {
286         return id.numberOfOrderedColumns();
287     }
288
289     /** @see IndexDescriptor#indexType */
290     public String JavaDoc indexType()
291     {
292         return id.indexType();
293     }
294
295     public String JavaDoc toString()
296     {
297         return id.toString();
298     }
299
300     /** @see IndexDescriptor#isAscending */
301     public boolean isAscending(Integer JavaDoc keyColumnPosition)
302     {
303         return id.isAscending(keyColumnPosition);
304     }
305
306     /** @see IndexDescriptor#isDescending */
307     public boolean isDescending(Integer JavaDoc keyColumnPosition)
308     {
309         return id.isDescending(keyColumnPosition);
310     }
311
312     /** @see IndexDescriptor#isAscending */
313     public boolean[] isAscending()
314     {
315         return id.isAscending();
316     }
317
318     /** @see IndexDescriptor#setBaseColumnPositions */
319     public void setBaseColumnPositions(int[] baseColumnPositions)
320     {
321         id.setBaseColumnPositions(baseColumnPositions);
322     }
323
324     /** @see IndexDescriptor#setIsAscending */
325     public void setIsAscending(boolean[] isAscending)
326     {
327         id.setIsAscending(isAscending);
328     }
329
330     /** @see IndexDescriptor#setNumberOfOrderedColumns */
331     public void setNumberOfOrderedColumns(int numberOfOrderedColumns)
332     {
333         id.setNumberOfOrderedColumns(numberOfOrderedColumns);
334     }
335
336     /**
337      * Test for value equality
338      *
339      * @param other The other indexrowgenerator to compare this one with
340      *
341      * @return true if this indexrowgenerator has the same value as other
342      */

343
344     public boolean equals(Object JavaDoc other)
345     {
346         return id.equals(other);
347     }
348
349     /**
350       @see java.lang.Object#hashCode
351       */

352     public int hashCode()
353     {
354         return id.hashCode();
355     }
356
357     private ExecutionFactory getExecutionFactory()
358     {
359         if (ef == null)
360         {
361             ExecutionContext ec;
362
363             ec = (ExecutionContext)
364                     ContextService.getContext(ExecutionContext.CONTEXT_ID);
365             ef = ec.getExecutionFactory();
366         }
367         return ef;
368     }
369
370     ////////////////////////////////////////////////////////////////////////////
371
//
372
// EXTERNALIZABLE
373
//
374
////////////////////////////////////////////////////////////////////////////
375

376     /**
377      * @see java.io.Externalizable#readExternal
378      *
379      * @exception IOException Thrown on read error
380      * @exception ClassNotFoundException Thrown on read error
381      */

382     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
383     {
384         id = (IndexDescriptor)in.readObject();
385     }
386
387     /**
388      *
389      * @exception IOException Thrown on write error
390      */

391     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
392     {
393         out.writeObject(id);
394     }
395
396     /* TypedFormat interface */
397     public int getTypeFormatId()
398     {
399         return StoredFormatIds.INDEX_ROW_GENERATOR_V01_ID;
400     }
401
402 }
403
Popular Tags