KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.iapi.sql.dictionary.IndexLister
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.services.context.ContextManager;
25
26 import org.apache.derby.iapi.error.StandardException;
27
28 import org.apache.derby.iapi.sql.depend.Dependent;
29
30 import org.apache.derby.iapi.services.context.ContextManager;
31
32 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
33 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
34 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
35 import org.apache.derby.iapi.sql.dictionary.IndexRowGenerator;
36
37 import org.apache.derby.iapi.sql.depend.Dependent;
38 import org.apache.derby.iapi.sql.depend.DependencyManager;
39 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;
40 import org.apache.derby.iapi.services.sanity.SanityManager;
41
42 import org.apache.derby.iapi.error.StandardException;
43
44 import java.util.Enumeration JavaDoc;
45 import java.util.Vector JavaDoc;
46
47 /**
48  * This interface gathers up some tasty information about the indices on a
49  * table from the DataDictionary.
50  *
51  */

52 public class IndexLister
53 {
54     ////////////////////////////////////////////////////////////////////////
55
//
56
// STATE
57
//
58
////////////////////////////////////////////////////////////////////////
59

60     private TableDescriptor tableDescriptor;
61     private IndexRowGenerator[] indexRowGenerators;
62     private long[] indexConglomerateNumbers;
63     private String JavaDoc[] indexNames;
64     // the following 3 are the compact arrays, without duplicate indexes
65
private IndexRowGenerator[] distinctIndexRowGenerators;
66     private long[] distinctIndexConglomerateNumbers;
67     private String JavaDoc[] distinctIndexNames;
68
69     ////////////////////////////////////////////////////////////////////////
70
//
71
// CONSTRUCTORS
72
//
73
////////////////////////////////////////////////////////////////////////
74

75     /**
76       * Make an IndexLister
77       *
78       * @param tableDescriptor Describes the table in question.
79       *
80       */

81     public IndexLister( TableDescriptor tableDescriptor )
82     {
83         this.tableDescriptor = tableDescriptor;
84     }
85
86
87     ////////////////////////////////////////////////////////////////////////
88
//
89
// INDEXLISTER METHODS
90
//
91
////////////////////////////////////////////////////////////////////////
92

93     /**
94       * Returns an array of all the index row generators on a table.
95       *
96       * @return an array of index row generators
97       *
98       * @exception StandardException Thrown on error
99       */

100     public IndexRowGenerator[] getIndexRowGenerators()
101                     throws StandardException
102     {
103         if ( indexRowGenerators == null ) { getAllIndexes(); }
104         return indexRowGenerators;
105     }
106
107     /**
108       * Returns an array of all the index conglomerate ids on a table.
109       *
110       * @return an array of index conglomerate ids
111       *
112       * @exception StandardException Thrown on error
113       */

114     public long[] getIndexConglomerateNumbers()
115                     throws StandardException
116     {
117         if ( indexConglomerateNumbers == null ) { getAllIndexes(); }
118         return indexConglomerateNumbers;
119     }
120
121     /**
122       * Returns an array of all the index names on a table.
123       *
124       * @return an array of index names
125       *
126       * @exception StandardException Thrown on error
127       */

128     public String JavaDoc[] getIndexNames() throws StandardException
129     {
130         if ( indexNames == null ) { getAllIndexes(); }
131         return indexNames;
132     }
133
134     /**
135       * Returns an array of distinct index row generators on a table,
136       * erasing entries for duplicate indexes (which share same conglomerate).
137       *
138       * @return an array of index row generators
139       *
140       * @exception StandardException Thrown on error
141       */

142     public IndexRowGenerator[] getDistinctIndexRowGenerators()
143                     throws StandardException
144     {
145         if ( distinctIndexRowGenerators == null ) { getAllIndexes(); }
146         return distinctIndexRowGenerators;
147     }
148
149     /**
150       * Returns an array of distinct index conglomerate ids on a table.
151       * erasing entries for duplicate indexes (which share same conglomerate).
152       *
153       * @return an array of index conglomerate ids
154       *
155       * @exception StandardException Thrown on error
156       */

157     public long[] getDistinctIndexConglomerateNumbers()
158                     throws StandardException
159     {
160         if ( distinctIndexConglomerateNumbers == null ) { getAllIndexes(); }
161         return distinctIndexConglomerateNumbers;
162     }
163
164     /**
165       * Returns an array of index names for all distinct indexes on a table.
166       * erasing entries for duplicate indexes (which share same conglomerate).
167       *
168       * @return an array of index names
169       *
170       * @exception StandardException Thrown on error
171       */

172     public String JavaDoc[] getDistinctIndexNames() throws StandardException
173     {
174         if ( indexNames == null ) { getAllIndexes(); }
175         return indexNames;
176     }
177
178     ////////////////////////////////////////////////////////////////////////
179
//
180
// MINIONS
181
//
182
////////////////////////////////////////////////////////////////////////
183

184     /**
185       * Reads all the indices on the table and populates arrays with the
186       * corresponding index row generators and index conglomerate ids.
187       *
188       *
189       * @exception StandardException Thrown on error
190       */

191     private void getAllIndexes()
192                     throws StandardException
193     {
194         int indexCount = 0;
195
196         ConglomerateDescriptor[] cds =
197                           tableDescriptor.getConglomerateDescriptors();
198
199         /* from one end of work space, we record distinct conglomerate
200          * numbers for comparison while we iterate; from the other end of
201          * work space, we record duplicate indexes' indexes in "cds" array,
202          * so that we can skip them in later round.
203          */

204         long[] workSpace = new long[cds.length - 1]; // 1 heap
205
int distinctIndexCount = 0, duplicateIndex = workSpace.length - 1;
206
207         for (int i = 0; i < cds.length; i++)
208         {
209             // first count the number of indices.
210
ConglomerateDescriptor cd = cds[i];
211
212             if ( ! cd.isIndex())
213                 continue;
214
215             int k;
216             long thisCongNum = cd.getConglomerateNumber();
217
218             for (k = 0; k < distinctIndexCount; k++)
219             {
220                 if (workSpace[k] == thisCongNum)
221                 {
222                     workSpace[duplicateIndex--] = i;
223                     break;
224                 }
225             }
226             if (k == distinctIndexCount) // first appearence
227
workSpace[distinctIndexCount++] = thisCongNum;
228
229             indexCount++;
230         }
231
232         indexRowGenerators = new IndexRowGenerator[ indexCount ];
233         indexConglomerateNumbers = new long[ indexCount ];
234         indexNames = new String JavaDoc[ indexCount ];
235         distinctIndexRowGenerators = new IndexRowGenerator[ distinctIndexCount ];
236         distinctIndexConglomerateNumbers = new long[ distinctIndexCount ];
237         distinctIndexNames = new String JavaDoc[ distinctIndexCount ];
238
239         int duplicatePtr = workSpace.length - 1;
240         for ( int i = 0, j = -1, k = -1; i < cds.length; i++ )
241         {
242             ConglomerateDescriptor cd = cds[i];
243
244             if ( ! cd.isIndex())
245                 continue;
246
247             indexRowGenerators[++j] =
248                 (IndexRowGenerator)cd.getIndexDescriptor();
249             indexConglomerateNumbers[j] = cd.getConglomerateNumber();
250             if (!(cd.isConstraint()))
251             {
252                 // only fill index name if it is not a constraint.
253
indexNames[j] = cd.getConglomerateName();
254             }
255
256             if (duplicatePtr > duplicateIndex && i == (int) workSpace[duplicatePtr])
257                 duplicatePtr--;
258             else
259             {
260                 distinctIndexRowGenerators[++k] = indexRowGenerators[j];
261                 distinctIndexConglomerateNumbers[k] = indexConglomerateNumbers[j];
262                 distinctIndexNames[k] = indexNames[j];
263             }
264         }
265     }
266 }
267
Popular Tags