KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > T_ConsistencyChecker


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.util.T_ConsistencyChecker
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.derbyTesting.functionTests.util;
23
24
25 import org.apache.derby.iapi.error.StandardException;
26
27 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;
28 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
29 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
30 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
31 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor;
32 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList;
33 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
34
35 import org.apache.derby.iapi.sql.depend.DependencyManager;
36
37 import org.apache.derby.iapi.reference.SQLState;
38
39 import org.apache.derby.iapi.sql.execute.ExecRow;
40 import org.apache.derby.iapi.sql.execute.ExecutionContext;
41
42 import org.apache.derby.iapi.types.DataValueFactory;
43 import org.apache.derby.iapi.types.DataTypeDescriptor;
44
45 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
46
47 import org.apache.derby.iapi.store.access.TransactionController;
48 import org.apache.derby.iapi.types.RowLocation;
49 import org.apache.derby.iapi.store.access.ScanController;
50 import org.apache.derby.iapi.store.access.ConglomerateController;
51
52 import org.apache.derby.iapi.services.context.ContextService;
53
54 import org.apache.derby.iapi.services.io.FormatableBitSet;
55
56 /**
57  * This class has methods for corrupting a database.
58  * IT MUST NOT BE DISTRIBUTED WITH THE PRODUCT.
59  *
60  * NOTE: The entry points to this class are all static,
61  * for easy access via the query language. Each of the
62  * static methods instantiates an object from the class
63  * and calls methods off of that object. This allows
64  * the sharing of code across the various static methods.
65  */

66 public class T_ConsistencyChecker
67 {
68     private DataDictionary dd;
69     private TransactionController tc;
70     private LanguageConnectionContext lcc;
71     private DataValueFactory dvf;
72     private ExecutionContext ec;
73     private String JavaDoc indexName;
74     private String JavaDoc schemaName;
75     private String JavaDoc tableName;
76     private ConglomerateDescriptor id;
77     private SchemaDescriptor sd;
78     private TableDescriptor td;
79
80     T_ConsistencyChecker(String JavaDoc schemaName, String JavaDoc tableName, String JavaDoc indexName)
81         throws StandardException
82     {
83         this.schemaName = schemaName;
84         this.tableName = tableName;
85         this.indexName = indexName;
86     }
87
88     /**
89      * Delete the first row from the heap, without
90      * deleting it from the indexes on the table.
91      *
92      * @param schemaName The schema name.
93      * @param tableName The table name.
94      *
95      * @return Nothing.
96      *
97      * @exception StandardException Thrown on error
98      */

99     public static void deleteFirstHeapRow(String JavaDoc schemaName, String JavaDoc tableName)
100         throws StandardException
101     {
102         T_ConsistencyChecker t_cc = new T_ConsistencyChecker(schemaName, tableName, null);
103         t_cc.getContexts();
104         t_cc.getDescriptors();
105
106         /* Open a scan on the heap */
107         ScanController heapScan = t_cc.openUnqualifiedHeapScan();
108
109         // Move to the 1st row in the heap
110
heapScan.next();
111
112         // Delete the 1st row in the heap
113
heapScan.delete();
114
115         heapScan.close();
116     }
117
118     /**
119      * Get the first row from the heap and insert it into
120      * the heap again, without
121      * inserting it from the indexes on the table.
122      *
123      * @param schemaName The schema name.
124      * @param tableName The table name.
125      *
126      * @return Nothing.
127      *
128      * @exception StandardException Thrown on error
129      */

130     public static void reinsertFirstHeapRow(String JavaDoc schemaName, String JavaDoc tableName)
131         throws StandardException
132     {
133         T_ConsistencyChecker t_cc = new T_ConsistencyChecker(schemaName, tableName, null);
134         t_cc.getContexts();
135         t_cc.getDescriptors();
136
137         /* Open a scan on the heap */
138         ScanController heapScan = t_cc.openUnqualifiedHeapScan();
139
140         // Move to the 1st row in the heap
141
heapScan.next();
142
143         // Fetch the 1st row
144
ExecRow firstRow = t_cc.getHeapRowOfNulls();
145         heapScan.fetch(firstRow.getRowArray());
146         heapScan.close();
147
148         // Insert another copy of the 1st row into the heap
149
ConglomerateController heapCC = t_cc.openHeapCC();
150         heapCC.insert(firstRow.getRowArray());
151         heapCC.close();
152     }
153
154     /**
155      * Set all of the columns in the first row from
156      * the heap to null, without
157      * updating the indexes on the table.
158      *
159      * @param schemaName The schema name.
160      * @param tableName The table name.
161      *
162      * @return Nothing.
163      *
164      * @exception StandardException Thrown on error
165      */

166     public static void nullFirstHeapRow(String JavaDoc schemaName, String JavaDoc tableName)
167         throws StandardException
168     {
169         T_ConsistencyChecker t_cc = new T_ConsistencyChecker(schemaName, tableName, null);
170         t_cc.getContexts();
171         t_cc.getDescriptors();
172
173         /* Open a scan on the heap */
174         ScanController heapScan = t_cc.openUnqualifiedHeapScan();
175
176         // Move to the 1st row in the heap
177
heapScan.next();
178
179         // Get the RowLocation
180
RowLocation baseRL = heapScan.newRowLocationTemplate();
181         heapScan.fetchLocation(baseRL);
182
183         // Replace the current row with nulls
184
heapScan.replace(
185             t_cc.getHeapRowOfNulls().getRowArray(),
186             (FormatableBitSet) null);
187
188         heapScan.close();
189     }
190
191     /**
192      * Get the first row from the heap and insert it into
193      * the specified index, with a bad row location, without
194      * inserting it into the heap or the other indexes on the table.
195      *
196      * @param schemaName The schema name.
197      * @param tableName The table name.
198      * @param indexName The specified index.
199      *
200      * @return Nothing.
201      *
202      * @exception StandardException Thrown on error
203      */

204     public static void insertBadRowLocation(String JavaDoc schemaName, String JavaDoc tableName, String JavaDoc indexName)
205         throws StandardException
206     {
207         T_ConsistencyChecker t_cc = new T_ConsistencyChecker(schemaName, tableName, indexName);
208         t_cc.getContexts();
209         t_cc.getDescriptors();
210
211
212         /* Open a scan on the heap */
213         ScanController heapScan = t_cc.openUnqualifiedHeapScan();
214
215         // Get the RowLocation
216
RowLocation baseRL = heapScan.newRowLocationTemplate();
217         RowLocation badRL = heapScan.newRowLocationTemplate();
218         heapScan.close();
219
220         /* Open a scan on the index */
221         ExecRow indexRow = t_cc.getIndexTemplateRow(baseRL);
222         ScanController indexScan = t_cc.openUnqualifiedIndexScan();
223
224         // Move to the 1st row in the index
225
indexScan.next();
226
227         // Fetch the 1st row
228
indexScan.fetch(indexRow.getRowArray());
229         indexScan.close();
230
231         // Insert another copy of the 1st row into the index with a bad row location
232
int keyLength =
233                 t_cc.getIndexDescriptor().getIndexDescriptor().baseColumnPositions().length;
234         indexRow.setColumn(keyLength + 1, badRL);
235
236         ConglomerateController indexCC = t_cc.openIndexCC();
237         indexCC.insert(indexRow.getRowArray());
238         indexCC.close();
239     }
240
241     /**
242      * Swap the values in the specified columns of the
243      * first row from the heap, without
244      * updating the indexes on the table.
245      *
246      * @param schemaName The schema name.
247      * @param tableName The table name.
248      * @param firstColumn First column #.
249      * @param secondColumn Second column #.
250      *
251      * @return Nothing.
252      *
253      * @exception StandardException Thrown on error
254      */

255     public static void swapColumnsInFirstHeapRow(String JavaDoc schemaName, String JavaDoc tableName,
256                                                  int firstColumn, int secondColumn)
257         throws StandardException
258     {
259     }
260
261     /* Get the various contexts */
262     private void getContexts()
263         throws StandardException
264     {
265         lcc = (LanguageConnectionContext)
266             ContextService.getContext(LanguageConnectionContext.CONTEXT_ID);
267         tc = lcc.getTransactionExecute();
268
269         dd = lcc.getDataDictionary();
270
271         dvf = lcc.getDataValueFactory();
272
273         ec = (ExecutionContext)
274                 (ContextService.getContext(ExecutionContext.CONTEXT_ID));
275     }
276
277     /* Get the various descriptors */
278     private void getDescriptors()
279         throws StandardException
280     {
281         sd = dd.getSchemaDescriptor(schemaName, tc, true);
282         td = dd.getTableDescriptor(tableName, sd);
283
284         if (td == null)
285         {
286             throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND, schemaName + "." + tableName);
287         }
288
289         if (indexName != null)
290         {
291             id = dd.getConglomerateDescriptor(indexName, sd, true);
292             if (id == null)
293             {
294                 throw StandardException.newException(SQLState.LANG_INDEX_NOT_FOUND, indexName);
295             }
296         }
297     }
298
299     /* Get a heap row full of nulls */
300     private ExecRow getHeapRowOfNulls()
301         throws StandardException
302     {
303         ConglomerateController baseCC;
304         ExecRow baseRow;
305
306         /* Open the heap for reading */
307         baseCC = tc.openConglomerate(
308                     td.getHeapConglomerateId(), false, 0,
309                     TransactionController.MODE_TABLE,
310                     TransactionController.ISOLATION_SERIALIZABLE);
311
312         /* Get a row template for the base table */
313         baseRow = ec.getExecutionFactory().getValueRow(td.getNumberOfColumns());
314
315         /* Fill the row with nulls of the correct type */
316         ColumnDescriptorList cdl = td.getColumnDescriptorList();
317         int cdlSize = cdl.size();
318
319         for (int index = 0; index < cdlSize; index++)
320         {
321             ColumnDescriptor cd = (ColumnDescriptor) cdl.elementAt(index);
322             DataTypeDescriptor dts = cd.getType();
323             baseRow.setColumn(cd.getPosition(),
324                                     dts.getNull());
325         }
326
327         baseCC.close();
328         return baseRow;
329     }
330
331     /* Open an unqualified scan on the heap for update */
332     private ScanController openUnqualifiedHeapScan()
333         throws StandardException
334     {
335         ScanController heapScan;
336
337         heapScan = tc.openScan(td.getHeapConglomerateId(),
338                                 false, // hold
339
TransactionController.OPENMODE_FORUPDATE, // forUpdate
340
TransactionController.MODE_TABLE,
341                                 TransactionController.ISOLATION_SERIALIZABLE,
342                                 (FormatableBitSet) null,
343                                 null, // startKeyValue
344
0, // not used with null start posn.
345
null, // qualifier
346
null, // stopKeyValue
347
0); // not used with null stop posn.
348

349         return heapScan;
350     }
351
352     /* Open the heap conglomerate for update */
353     private ConglomerateController openHeapCC()
354         throws StandardException
355     {
356         ConglomerateController heapCC;
357
358         heapCC = tc.openConglomerate(
359                         td.getHeapConglomerateId(),
360                         false,
361                         TransactionController.OPENMODE_FORUPDATE, // forUpdate
362
TransactionController.MODE_TABLE,
363                         TransactionController.ISOLATION_SERIALIZABLE);
364
365
366         return heapCC;
367     }
368
369     /* Get a template row for the specified index */
370     private ExecRow getIndexTemplateRow(RowLocation baseRL)
371         throws StandardException
372     {
373         int[] baseColumnPositions;
374         int baseColumns = 0;
375         ExecRow indexScanTemplate;
376
377         baseColumnPositions =
378                 id.getIndexDescriptor().baseColumnPositions();
379         baseColumns = baseColumnPositions.length;
380
381         FormatableBitSet indexColsBitSet = new FormatableBitSet();
382         for (int i = 0; i < baseColumns; i++)
383         {
384             indexColsBitSet.grow(baseColumnPositions[i]);
385             indexColsBitSet.set(baseColumnPositions[i] - 1);
386         }
387
388         /* Get a row template */
389         indexScanTemplate = ec.getExecutionFactory().getValueRow(baseColumns + 1);
390
391         /* Fill the row with nulls of the correct type */
392         for (int column = 0; column < baseColumns; column++)
393         {
394             /* Column positions in the data dictionary are one-based */
395             ColumnDescriptor cd = td.getColumnDescriptor(baseColumnPositions[column]);
396             DataTypeDescriptor dts = cd.getType();
397             indexScanTemplate.setColumn(column + 1,
398                                     dts.getNull());
399         }
400
401         /* Set the row location in the last column of the index row */
402         indexScanTemplate.setColumn(baseColumns + 1, baseRL);
403
404         return indexScanTemplate;
405     }
406
407     /* Open an unqualified scan on the index for update */
408     private ScanController openUnqualifiedIndexScan()
409         throws StandardException
410     {
411         ScanController indexScan;
412
413         indexScan = tc.openScan(id.getConglomerateNumber(),
414                                 false, // hold
415
TransactionController.OPENMODE_FORUPDATE, // forUpdate
416
TransactionController.MODE_TABLE,
417                                 TransactionController.ISOLATION_SERIALIZABLE,
418                                 (FormatableBitSet) null,
419                                 null, // startKeyValue
420
0, // not used with null start posn.
421
null, // qualifier
422
null, // stopKeyValue
423
0); // not used with null stop posn.
424

425         return indexScan;
426     }
427
428     /* Open the index conglomerate for update */
429     private ConglomerateController openIndexCC()
430         throws StandardException
431     {
432         ConglomerateController indexCC;
433
434         indexCC = tc.openConglomerate(
435                         id.getConglomerateNumber(),
436                         false,
437                         TransactionController.OPENMODE_FORUPDATE, // forUpdate
438
TransactionController.MODE_TABLE,
439                         TransactionController.ISOLATION_SERIALIZABLE);
440
441
442         return indexCC;
443     }
444
445     /* Return the ConglomerateDescriptor for the index */
446     private ConglomerateDescriptor getIndexDescriptor()
447     {
448         return id;
449     }
450
451
452     // following methods are originally from a different class - used in the test store/backupRestore1
453
// original comment for that class:
454
/**
455      * This class provides static methods for checking the consistency of database
456      * objects like tables.
457      */

458
459     /**
460      * Run all of the consistency checkers which do not take parameters.
461      * Actually, just run the ones that "make sense" to run. Today,
462      * that is:
463      * countOpens()
464      *
465      * @return String If an inconsistency is found, and if DEBUG is on,
466      * then a string will be returned with more info.
467      * If DEBUG is off, then a simple string will be
468      * returned stating whether or not there are open scans.
469      *
470      * @exception StandardException Thrown on error
471      * @exception java.sql.SQLException Thrown on error
472      */

473     public static String JavaDoc runConsistencyChecker() throws StandardException, java.sql.SQLException JavaDoc
474     {
475         return countOpens() + countDependencies();
476     }
477
478     /**
479      * Check to make sure that there are no open conglomerates, scans or sorts.
480      *
481      * @return String If an inconsistency is found, and if DEBUG is on,
482      * then a string will be returned with more info.
483      * If DEBUG is off, then a simple string will be
484      * returned stating whether or not there are open scans.
485      *
486      * @exception StandardException Thrown on error
487      */

488     public static String JavaDoc countOpens() throws StandardException
489     {
490         int numOpens = 0;
491         LanguageConnectionContext lcc;
492         String JavaDoc output = "No open scans, etc.\n";
493         TransactionController tc;
494
495         lcc = (LanguageConnectionContext)
496             ContextService.getContext(LanguageConnectionContext.CONTEXT_ID);
497         tc = lcc.getTransactionExecute();
498
499         numOpens = tc.countOpens(TransactionController.OPEN_TOTAL);
500
501         if (numOpens > 0)
502         {
503             output = numOpens + " conglomerates/scans/sorts found open\n";
504
505         }
506
507         return output;
508     }
509
510     /**
511      * Check to make sure that there are no active dependencies (stored or
512      * in memory).
513      *
514      * @return String If an inconsistency is found, and if DEBUG is on,
515      * then a string will be returned with more info.
516      * If DEBUG is off, then a simple string will be
517      * returned stating whether or not there are open scans.
518      *
519      * @exception StandardException Thrown on error
520      * @exception java.sql.SQLException Thrown on error
521      */

522     public static String JavaDoc countDependencies() throws StandardException, java.sql.SQLException JavaDoc
523     {
524         int numDependencies = 0;
525         DataDictionary dd;
526         DataDictionaryContext ddc;
527         DependencyManager dm;
528         StringBuffer JavaDoc debugBuf = new StringBuffer JavaDoc();
529
530         ddc = (DataDictionaryContext)
531                 (ContextService.getContext(DataDictionaryContext.CONTEXT_ID));
532
533         dd = ddc.getDataDictionary();
534         dm = dd.getDependencyManager();
535
536         numDependencies = dm.countDependencies();
537
538         if (numDependencies > 0)
539         {
540             debugBuf.append(numDependencies + " dependencies found");
541         }
542         else
543         {
544             debugBuf.append("No outstanding dependencies.\n");
545         }
546
547         return debugBuf.toString();
548     }
549 }
550
Popular Tags