KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > catalog > types > IndexDescriptorImpl


1 /*
2
3    Derby - Class org.apache.derby.catalog.types.IndexDescriptorImpl
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.catalog.types;
23
24 import org.apache.derby.catalog.IndexDescriptor;
25
26 import org.apache.derby.iapi.services.io.Formatable;
27 import org.apache.derby.iapi.services.io.StoredFormatIds;
28
29 import org.apache.derby.iapi.reference.SQLState;
30
31 import org.apache.derby.iapi.services.io.FormatableHashtable;
32 import org.apache.derby.iapi.services.io.FormatableIntHolder;
33 import org.apache.derby.iapi.sql.dictionary.IndexRowGenerator;
34
35 import java.io.ObjectInput JavaDoc;
36 import java.io.ObjectOutput JavaDoc;
37 import java.io.IOException JavaDoc;
38
39 /** @see org.apache.derby.iapi.sql.dictionary.IndexRowGenerator */
40 public class IndexDescriptorImpl implements IndexDescriptor, Formatable
41 {
42     /********************************************************
43     **
44     ** This class implements Formatable. That means that it
45     ** can write itself to and from a formatted stream. If
46     ** you add more fields to this class, make sure that you
47     ** also write/read them with the writeExternal()/readExternal()
48     ** methods.
49     **
50     ** If, inbetween releases, you add more fields to this class,
51     ** then you should bump the version number emitted by the getTypeFormatId()
52     ** method.
53     **
54     ********************************************************/

55
56     private boolean isUnique;
57     private int[] baseColumnPositions;
58     private boolean[] isAscending;
59     private int numberOfOrderedColumns;
60     private String JavaDoc indexType;
61
62     /**
63      * Constructor for an IndexDescriptorImpl
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 IndexDescriptorImpl(String JavaDoc indexType,
78                                 boolean isUnique,
79                                 int[] baseColumnPositions,
80                                 boolean[] isAscending,
81                                 int numberOfOrderedColumns)
82     {
83         this.indexType = indexType;
84         this.isUnique = isUnique;
85         this.baseColumnPositions = baseColumnPositions;
86         this.isAscending = isAscending;
87         this.numberOfOrderedColumns = numberOfOrderedColumns;
88     }
89
90     /** Zero-argument constructor for Formatable interface */
91     public IndexDescriptorImpl()
92     {
93     }
94
95     /** @see IndexDescriptor#isUnique */
96     public boolean isUnique()
97     {
98         return isUnique;
99     }
100
101     /** @see IndexDescriptor#baseColumnPositions */
102     public int[] baseColumnPositions()
103     {
104         return baseColumnPositions;
105     }
106
107     /** @see IndexDescriptor#getKeyColumnPosition */
108     public Integer JavaDoc getKeyColumnPosition(Integer JavaDoc heapColumnPosition)
109     {
110         return new Integer JavaDoc(getKeyColumnPosition(heapColumnPosition.intValue()));
111     }
112
113     /** @see IndexDescriptor#getKeyColumnPosition */
114     public int getKeyColumnPosition(int heapColumnPosition)
115     {
116         /* Return 0 if column is not in the key */
117         int keyPosition = 0;
118
119         for (int index = 0; index < baseColumnPositions.length; index++)
120         {
121             /* Return 1-based key column position if column is in the key */
122             if (baseColumnPositions[index] == heapColumnPosition)
123             {
124                 keyPosition = index + 1;
125                 break;
126             }
127         }
128
129         return keyPosition;
130     }
131
132     /** @see IndexDescriptor#numberOfOrderedColumns */
133     public int numberOfOrderedColumns()
134     {
135         return numberOfOrderedColumns;
136     }
137
138     /** @see IndexDescriptor#indexType */
139     public String JavaDoc indexType()
140     {
141         return indexType;
142     }
143
144     /** @see IndexDescriptor#isAscending */
145     public boolean isAscending(Integer JavaDoc keyColumnPosition)
146     {
147         int i = keyColumnPosition.intValue() - 1;
148         if (i < 0 || i >= baseColumnPositions.length)
149             return false;
150         return isAscending[i];
151     }
152
153     /** @see IndexDescriptor#isDescending */
154     public boolean isDescending(Integer JavaDoc keyColumnPosition)
155     {
156         int i = keyColumnPosition.intValue() - 1;
157         if (i < 0 || i >= baseColumnPositions.length)
158             return false;
159         return ! isAscending[i];
160     }
161
162     /** @see IndexDescriptor#isAscending */
163     public boolean[] isAscending()
164     {
165         return isAscending;
166     }
167
168     /** @see IndexDescriptor#setBaseColumnPositions */
169     public void setBaseColumnPositions(int[] baseColumnPositions)
170     {
171         this.baseColumnPositions = baseColumnPositions;
172     }
173
174     /** @see IndexDescriptor#setIsAscending */
175     public void setIsAscending(boolean[] isAscending)
176     {
177         this.isAscending = isAscending;
178     }
179
180     /** @see IndexDescriptor#setNumberOfOrderedColumns */
181     public void setNumberOfOrderedColumns(int numberOfOrderedColumns)
182     {
183         this.numberOfOrderedColumns = numberOfOrderedColumns;
184     }
185
186     public String JavaDoc toString()
187     {
188         String JavaDoc uniqueness;
189         String JavaDoc cols;
190
191         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(60);
192
193         if (isUnique)
194             sb.append("UNIQUE ");
195
196         sb.append(indexType);
197
198         sb.append(" (");
199
200
201         for (int i = 0; i < baseColumnPositions.length; i++)
202         {
203             if (i > 0)
204                 sb.append(", ");
205             sb.append(baseColumnPositions[i]);
206             if (! isAscending[i])
207                 sb.append(" DESC");
208         }
209
210         sb.append(")");
211
212         return sb.toString();
213     }
214
215     /* Externalizable interface */
216
217     /**
218      * @see java.io.Externalizable#readExternal
219      *
220      * @exception IOException Thrown on read error
221      */

222     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
223     {
224         FormatableHashtable fh = (FormatableHashtable)in.readObject();
225         isUnique = fh.getBoolean("isUnique");
226         int bcpLength = fh.getInt("keyLength");
227         baseColumnPositions = new int[bcpLength];
228         isAscending = new boolean[bcpLength];
229         for (int i = 0; i < bcpLength; i++)
230         {
231             baseColumnPositions[i] = fh.getInt("bcp" + i);
232             isAscending[i] = fh.getBoolean("isAsc" + i);
233         }
234         numberOfOrderedColumns = fh.getInt("orderedColumns");
235         indexType = (String JavaDoc)fh.get("indexType");
236     }
237
238     /**
239      * @see java.io.Externalizable#writeExternal
240      *
241      * @exception IOException Thrown on write error
242      */

243     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
244     {
245         FormatableHashtable fh = new FormatableHashtable();
246         fh.putBoolean("isUnique", isUnique);
247         fh.putInt("keyLength", baseColumnPositions.length);
248         for (int i = 0; i < baseColumnPositions.length; i++)
249         {
250             fh.putInt("bcp" + i, baseColumnPositions[i]);
251             fh.putBoolean("isAsc" + i, isAscending[i]);
252         }
253         fh.putInt("orderedColumns", numberOfOrderedColumns);
254         fh.put("indexType", indexType);
255         out.writeObject(fh);
256     }
257
258     /* TypedFormat interface */
259     public int getTypeFormatId()
260     {
261         return StoredFormatIds.INDEX_DESCRIPTOR_IMPL_V02_ID;
262     }
263
264     /**
265      * Test for value equality
266      *
267      * @param other The other indexrowgenerator to compare this one with
268      *
269      * @return true if this indexrowgenerator has the same value as other
270      */

271
272     public boolean equals(Object JavaDoc other)
273     {
274         /* Assume not equal until we know otherwise */
275         boolean retval = false;
276
277         /* Equal only if comparing the same class */
278         if (other instanceof IndexDescriptorImpl)
279         {
280             IndexDescriptorImpl id = (IndexDescriptorImpl) other;
281
282             /*
283             ** Check all the fields for equality except for the array
284             ** elements (this is hardest, so save for last)
285             */

286             if ((id.isUnique == this.isUnique) &&
287                 (id.baseColumnPositions.length ==
288                                         this.baseColumnPositions.length) &&
289                 (id.numberOfOrderedColumns == this.numberOfOrderedColumns) &&
290                 (id.indexType.equals(this.indexType)))
291             {
292                 /*
293                 ** Everything but array elements known to be true -
294                 ** Assume equal, and check whether array elements are equal.
295                 */

296                 retval = true;
297
298                 for (int i = 0; i < this.baseColumnPositions.length; i++)
299                 {
300                     /* If any array element is not equal, return false */
301                     if ((id.baseColumnPositions[i] !=
302                         this.baseColumnPositions[i]) || (id.isAscending[i] !=
303                         this.isAscending[i]))
304                     {
305                         retval = false;
306                         break;
307                     }
308                 }
309             }
310         }
311
312         return retval;
313     }
314
315     /**
316       @see java.lang.Object#hashCode
317       */

318     public int hashCode()
319     {
320         int retval;
321
322         retval = isUnique ? 1 : 2;
323         retval *= numberOfOrderedColumns;
324         for (int i = 0; i < baseColumnPositions.length; i++)
325         {
326             retval *= baseColumnPositions[i];
327         }
328         retval *= indexType.hashCode();
329
330         return retval;
331     }
332 }
333
Popular Tags