KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > IndexSetChanger


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.IndexSetChanger
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.impl.sql.execute;
23
24 import org.apache.derby.iapi.services.sanity.SanityManager;
25 import org.apache.derby.iapi.error.StandardException;
26 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
27 import org.apache.derby.iapi.sql.dictionary.IndexRowGenerator;
28 import org.apache.derby.iapi.sql.execute.ExecRow;
29 import org.apache.derby.iapi.sql.execute.ExecutionFactory;
30
31 import org.apache.derby.iapi.sql.Activation;
32
33 import org.apache.derby.iapi.store.access.ConglomerateController;
34 import org.apache.derby.iapi.store.access.DynamicCompiledOpenConglomInfo;
35 import org.apache.derby.iapi.store.access.StaticCompiledOpenConglomInfo;
36 import org.apache.derby.iapi.store.access.TransactionController;
37
38 import org.apache.derby.iapi.types.RowLocation;
39
40 import org.apache.derby.iapi.services.io.FormatableBitSet;
41
42 /**
43   Perform Index maintenace associated with DML operations for a table's
44   indexes.
45   */

46 public class IndexSetChanger
47 {
48     //
49
//Index row generators.
50
IndexRowGenerator[] irgs;
51     //
52
//Index conglomerate ids. indexCIDS[ix] is the conglomerate id
53
//for the index with IndexRowGenerator irgs[ix].
54
long[] indexCIDS;
55     private DynamicCompiledOpenConglomInfo[] indexDCOCIs;
56     private StaticCompiledOpenConglomInfo[] indexSCOCIs;
57     String JavaDoc[] indexNames;
58     ConglomerateController baseCC;
59     FormatableBitSet baseRowReadMap;
60
61     // TransactionController for management of temporary conglomerates
62
TransactionController tc;
63
64     TemporaryRowHolderImpl rowHolder;
65
66     IndexChanger[] indexChangers;
67
68     // Lock mode for the indexes
69
private int lockMode;
70
71     //Set on open.
72
boolean[] fixOnUpdate;
73     
74     boolean isOpen = false;
75     
76     //
77
//Name for the set of no indexes
78
private static final int NO_INDEXES = 0;
79     //
80
//Name for the set of indexes we change on a update operation
81
private static final int UPDATE_INDEXES = 1;
82     //
83
//Name for the set of all indexes.
84
private static final int ALL_INDEXES = 2;
85     
86     //
87
//To start, no indexes are open.
88
private int whatIsOpen = NO_INDEXES;
89
90     private int isolationLevel;
91     private Activation activation;
92
93     /**
94       Create a new IndexSetChanger
95
96       @param irgs the IndexRowGenerators for the table's indexes. We use
97         positions in this array as local id's for indexes.
98       @param indexCIDS the conglomerate ids for the table's indexes.
99         indexCIDS[ix] corresponds to the same index as irgs[ix].
100       @param indexSCOCIs the SCOCIs for the table's idexes.
101         indexSCOCIs[ix] corresponds to the same index as irgs[ix].
102       @param indexDCOCIs the DCOCIs for the table's idexes.
103         indexDCOCIs[ix] corresponds to the same index as irgs[ix].
104       @param baseCC a ConglomerateController for the base table.
105       @param tc a TransactionController for managing temporary conglomerates
106       @param lockMode The lock mode (granularity) for the indexes.
107       @param baseRowReadMap Map of columns read in. 1 based.
108       @param isolationLevel Isolation level to use
109       @param activation Current activation
110       @exception StandardException Thrown on error
111       */

112     public IndexSetChanger(IndexRowGenerator[] irgs,
113                            long[] indexCIDS,
114                            StaticCompiledOpenConglomInfo[] indexSCOCIs,
115                            DynamicCompiledOpenConglomInfo[] indexDCOCIs,
116                            String JavaDoc[] indexNames,
117                            ConglomerateController baseCC,
118                            TransactionController tc,
119                            int lockMode,
120                            FormatableBitSet baseRowReadMap,
121                            int isolationLevel,
122                            Activation activation)
123          throws StandardException
124     {
125         this.irgs = irgs;
126         this.indexCIDS = indexCIDS;
127         this.indexSCOCIs = indexSCOCIs;
128         this.indexDCOCIs = indexDCOCIs;
129         this.indexNames = indexNames;
130         this.baseCC = baseCC;
131         this.tc = tc;
132         this.lockMode = lockMode;
133         this.baseRowReadMap = baseRowReadMap;
134         this.isolationLevel = isolationLevel;
135         this.activation = activation;
136
137         if (SanityManager.DEBUG)
138         {
139             SanityManager.ASSERT(indexCIDS != null, "indexCIDS is null");
140         }
141
142         indexChangers = new IndexChanger[irgs.length];
143     }
144     
145     /**
146       Open this IndexSetchanger.
147
148       @param fixOnUpdate indicates which indexes to correct due
149         to an update. The entries in this array must be in the
150         same order as the entries in the irgs array that was
151         passed to the constructor.
152
153       @exception StandardException Thrown on error
154       */

155     public void open(boolean[] fixOnUpdate)
156          throws StandardException
157     {
158         if (SanityManager.DEBUG)
159             SanityManager.ASSERT( ! isOpen, "IndexSetChanger already open");
160
161         this.fixOnUpdate = fixOnUpdate;
162         isOpen = true;
163     }
164
165     /**
166      * Set the row holder for all underlying changers to use.
167      * If the row holder is set, underlying changers wont bother
168      * saving copies of rows needed for deferred
169      * processing. Also, it will never close the
170      * passed in rowHolder.
171      *
172      * @param rowHolder the row holder
173      */

174     public void setRowHolder(TemporaryRowHolderImpl rowHolder)
175     {
176         this.rowHolder = rowHolder;
177     }
178
179     /**
180       Open the indexes that must be fixed if they are not already
181       open.
182
183       @param whatToOpen must be one of ALL_INDEXES or UPDATE_INDEXES.
184       @exception StandardException Thrown on error
185       */

186     private void openIndexes(int whatToOpen)
187          throws StandardException
188     {
189         if (SanityManager.DEBUG)
190             SanityManager.ASSERT( isOpen, "IndexSetChanger closed");
191
192         if (whatIsOpen >= whatToOpen) return;
193             
194         for (int ix = 0; ix < indexChangers.length; ix++)
195         {
196             if (whatToOpen == UPDATE_INDEXES &&
197                 !fixOnUpdate[ix])
198                 continue;
199             
200             /* Instantiate an index changer, if it doesn't exist,
201              * otherwise we propagate the CC for the heap to
202              * the index changer.
203              */

204             if (indexChangers[ix] == null)
205             {
206                 /* DataDictionary doesn't have compiled info. */
207                 indexChangers[ix] =
208                     new IndexChanger(irgs[ix],
209                                      indexCIDS[ix],
210                                      (indexSCOCIs == null) ?
211                                          (StaticCompiledOpenConglomInfo) null :
212                                             indexSCOCIs[ix],
213                                      (indexDCOCIs == null) ?
214                                          (DynamicCompiledOpenConglomInfo) null :
215                                             indexDCOCIs[ix],
216                                      (indexNames == null) ? null :
217                                                             indexNames[ix],
218                                      baseCC,
219                                      tc,
220                                      lockMode,
221                                      baseRowReadMap,
222                                      isolationLevel,
223                                      activation);
224                 indexChangers[ix].setRowHolder(rowHolder);
225             }
226             else
227             {
228                 indexChangers[ix].setBaseCC(baseCC);
229             }
230             indexChangers[ix].open();
231         }
232         whatIsOpen = whatToOpen;
233     }
234
235     /**
236       Perform index maintenance associated with deleting a row
237       from a table.
238
239       @param baseRow the deleted row.
240       @param baseRowLocation the deleted row's base conglomerate
241          location
242       @exception StandardException Thrown on error
243       */

244     public void delete(ExecRow baseRow,
245                        RowLocation baseRowLocation)
246          throws StandardException
247     {
248         openIndexes(ALL_INDEXES);
249         for (int ix = 0; ix < indexChangers.length; ix++)
250             indexChangers[ix].delete(baseRow,baseRowLocation);
251     }
252
253     /**
254       Perform index maintenance associated with insering a row
255       into a table.
256
257       @param baseRow the row.
258       @param baseRowLocation the row's base conglomerate
259          location
260       @exception StandardException Thrown on error
261       */

262     public void insert(ExecRow baseRow,
263                        RowLocation baseRowLocation)
264          throws StandardException
265     {
266         openIndexes(ALL_INDEXES);
267         for (int ix = 0; ix < indexChangers.length; ix++)
268             indexChangers[ix].insert(baseRow,baseRowLocation);
269     }
270
271     /**
272       Perform index maintenance associated with updating a row
273       in a table.
274
275       @param oldBaseRow the old image of the row.
276       @param newBaseRow the new image of the row.
277       @param baseRowLocation the row's base conglomerate
278          location
279       @exception StandardException Thrown on error
280       */

281     public void update(ExecRow oldBaseRow,
282                        ExecRow newBaseRow,
283                        RowLocation baseRowLocation)
284          throws StandardException
285     {
286         openIndexes(UPDATE_INDEXES);
287         for (int ix = 0; ix < indexChangers.length; ix++)
288             if (fixOnUpdate[ix])
289                 indexChangers[ix].update(oldBaseRow,
290                                          newBaseRow,
291                                          baseRowLocation);
292     }
293
294     /**
295      * Propagate the heap's ConglomerateController to
296      * all of the underlying index changers.
297      *
298      * @param baseCC The heap's ConglomerateController.
299      */

300     public void setBaseCC(ConglomerateController baseCC)
301     {
302         for (int ix = 0; ix < indexChangers.length; ix++)
303         {
304             if (indexChangers[ix] != null)
305             {
306                 indexChangers[ix].setBaseCC(baseCC);
307             }
308         }
309         this.baseCC = baseCC;
310     }
311
312     /**
313       Finish processing the changes for this IndexSetChanger. This means
314       doing the deferred inserts for updates of unique indexes.
315
316       @exception StandardException Thrown on error
317      */

318     public void finish()
319         throws StandardException
320     {
321         for (int ix = 0; ix < indexChangers.length; ix++)
322         {
323             if (indexChangers[ix] != null)
324             {
325                 indexChangers[ix].finish();
326             }
327         }
328     }
329         
330     /**
331       Close this IndexSetChanger.
332
333       @exception StandardException Thrown on error
334       */

335     public void close()
336         throws StandardException
337     {
338         whatIsOpen = NO_INDEXES;
339         for (int ix = 0; ix < indexChangers.length; ix++)
340         {
341             if (indexChangers[ix] != null)
342             {
343                 indexChangers[ix].close();
344             }
345         }
346         fixOnUpdate = null;
347         isOpen = false;
348         rowHolder = null;
349     }
350
351     /**
352       Create a string describing the state of this IndexSetChanger
353       */

354     public String JavaDoc toString()
355     {
356         if (SanityManager.DEBUG)
357         {
358             String JavaDoc whatIsOpen_s = null;
359             switch (whatIsOpen)
360             {
361             case NO_INDEXES:
362                 whatIsOpen_s = "No open indexes ";
363                 break;
364             case UPDATE_INDEXES:
365                 whatIsOpen_s = "Update indexes open ";
366                 break;
367             case ALL_INDEXES:
368                 whatIsOpen_s = "All indexes open ";
369                 break;
370             default:
371                 SanityManager.THROWASSERT("bad whatIsOpen value "+whatIsOpen);
372                 break;
373             }
374
375             String JavaDoc fixOnUpdate_s = "fixOnUpdate=(";
376             for (int ix = 0; ix < fixOnUpdate.length; ix++)
377             {
378                 if (ix > 0)
379                     fixOnUpdate_s+=",";
380
381                 fixOnUpdate_s += fixOnUpdate[ix];
382             }
383             fixOnUpdate_s +=")";
384
385             String JavaDoc indexDesc_s = "\n";
386             for (int ix = 0; ix < indexCIDS.length; ix++)
387             {
388                 if (indexChangers[ix] == null)
389                     indexDesc_s += " Index["+ix+"] cid="+
390                         indexCIDS[ix]+" closed. \n";
391                 else
392                     indexDesc_s +=
393                         " "+
394                         indexChangers[ix].toString() + "\n";
395             }
396
397             return "IndexSetChanger: "+
398                 whatIsOpen_s+
399                 fixOnUpdate_s+
400                 indexDesc_s;
401         }
402
403         return null;
404     }
405 }
406
Popular Tags