1 21 22 package org.apache.derby.iapi.db; 23 24 import org.apache.derby.iapi.error.StandardException; 25 import org.apache.derby.iapi.error.PublicAPI; 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.ConstraintDescriptor; 34 import org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList; 35 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor; 36 37 import org.apache.derby.iapi.sql.depend.DependencyManager; 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.DataValueDescriptor; 43 import org.apache.derby.iapi.types.DataValueFactory; 44 45 46 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 47 import org.apache.derby.iapi.sql.conn.ConnectionUtil; 48 49 import org.apache.derby.iapi.store.access.TransactionController; 50 import org.apache.derby.iapi.types.RowLocation; 51 import org.apache.derby.iapi.store.access.ScanController; 52 import org.apache.derby.iapi.store.access.ConglomerateController; 53 import org.apache.derby.iapi.store.access.RowUtil; 54 55 import org.apache.derby.iapi.services.sanity.SanityManager; 56 57 import org.apache.derby.iapi.reference.SQLState; 58 59 import org.apache.derby.iapi.services.io.FormatableBitSet; 60 61 import java.sql.SQLException ; 62 63 71 public class ConsistencyChecker 72 { 73 74 75 private ConsistencyChecker() { 76 } 77 78 116 public static boolean checkTable(String schemaName, String tableName) 117 throws SQLException 118 { 119 DataDictionary dd; 120 TableDescriptor td; 121 long baseRowCount = -1; 122 TransactionController tc; 123 ConglomerateDescriptor heapCD; 124 ConglomerateDescriptor indexCD; 125 ExecRow baseRow; 126 ExecRow indexRow; 127 RowLocation rl = null; 128 RowLocation scanRL = null; 129 ScanController scan = null; 130 int[] baseColumnPositions; 131 int baseColumns = 0; 132 DataValueFactory dvf; 133 long indexRows; 134 ConglomerateController baseCC = null; 135 ConglomerateController indexCC = null; 136 ExecutionContext ec; 137 SchemaDescriptor sd; 138 ConstraintDescriptor constraintDesc; 139 140 LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC(); 141 tc = lcc.getTransactionExecute(); 142 143 try { 144 145 dd = lcc.getDataDictionary(); 146 147 dvf = lcc.getDataValueFactory(); 148 149 ec = lcc.getExecutionContext() ; 150 151 sd = dd.getSchemaDescriptor(schemaName, tc, true); 152 td = dd.getTableDescriptor(tableName, sd); 153 154 if (td == null) 155 { 156 throw StandardException.newException( 157 SQLState.LANG_TABLE_NOT_FOUND, 158 schemaName + "." + tableName); 159 } 160 161 162 if (td.getTableType() == TableDescriptor.VIEW_TYPE) 163 { 164 return true; 165 } 166 167 168 baseCC = tc.openConglomerate( 169 td.getHeapConglomerateId(), false, 0, 170 TransactionController.MODE_TABLE, 171 TransactionController.ISOLATION_SERIALIZABLE); 172 173 174 baseCC.checkConsistency(); 175 176 heapCD = td.getConglomerateDescriptor(td.getHeapConglomerateId()); 177 178 179 baseRow = ec.getExecutionFactory().getValueRow(td.getNumberOfColumns()); 180 181 182 ColumnDescriptorList cdl = td.getColumnDescriptorList(); 183 int cdlSize = cdl.size(); 184 185 for (int index = 0; index < cdlSize; index++) 186 { 187 ColumnDescriptor cd = (ColumnDescriptor) cdl.elementAt(index); 188 baseRow.setColumn(cd.getPosition(), 189 cd.getType().getNull()); 190 } 191 192 193 ConglomerateDescriptor[] cds = td.getConglomerateDescriptors(); 194 for (int index = 0; index < cds.length; index++) 195 { 196 indexCD = cds[index]; 197 198 if ( ! indexCD.isIndex()) 199 continue; 200 201 202 indexCC = 203 tc.openConglomerate( 204 indexCD.getConglomerateNumber(), 205 false, 206 0, 207 TransactionController.MODE_TABLE, 208 TransactionController.ISOLATION_SERIALIZABLE); 209 210 indexCC.checkConsistency(); 211 indexCC.close(); 212 indexCC = null; 213 214 215 216 if (indexCD.isConstraint()) 217 { 218 constraintDesc = dd.getConstraintDescriptor(td, indexCD.getUUID()); 219 if (constraintDesc == null) 220 { 221 throw StandardException.newException( 222 SQLState.LANG_OBJECT_NOT_FOUND, 223 "CONSTRAINT for INDEX", 224 indexCD.getConglomerateName()); 225 } 226 } 227 228 234 if (baseRowCount < 0) 235 { 236 scan = tc.openScan(heapCD.getConglomerateNumber(), 237 false, 0, TransactionController.MODE_TABLE, 240 TransactionController.ISOLATION_SERIALIZABLE, 241 RowUtil.EMPTY_ROW_BITSET, 242 null, 0, null, null, 0); 248 249 rl = scan.newRowLocationTemplate(); 250 scanRL = scan.newRowLocationTemplate(); 251 252 for (baseRowCount = 0; scan.next(); baseRowCount++) 253 ; 254 255 scan.close(); 256 scan = null; 257 } 258 259 baseColumnPositions = 260 indexCD.getIndexDescriptor().baseColumnPositions(); 261 baseColumns = baseColumnPositions.length; 262 263 FormatableBitSet indexColsBitSet = new FormatableBitSet(); 264 for (int i = 0; i < baseColumns; i++) 265 { 266 indexColsBitSet.grow(baseColumnPositions[i]); 267 indexColsBitSet.set(baseColumnPositions[i] - 1); 268 } 269 270 271 indexRow = ec.getExecutionFactory().getValueRow(baseColumns + 1); 272 273 274 for (int column = 0; column < baseColumns; column++) 275 { 276 277 ColumnDescriptor cd = td.getColumnDescriptor(baseColumnPositions[column]); 278 indexRow.setColumn(column + 1, 279 cd.getType().getNull()); 280 } 281 282 283 indexRow.setColumn(baseColumns + 1, rl); 284 285 286 scan = tc.openScan(indexCD.getConglomerateNumber(), 287 false, 0, TransactionController.MODE_TABLE, 290 TransactionController.ISOLATION_SERIALIZABLE, 291 (FormatableBitSet) null, 292 null, 0, null, null, 0); 298 DataValueDescriptor[] baseRowIndexOrder = 299 new DataValueDescriptor[baseColumns]; 300 DataValueDescriptor[] baseObjectArray = baseRow.getRowArray(); 301 302 for (int i = 0; i < baseColumns; i++) 303 { 304 baseRowIndexOrder[i] = baseObjectArray[baseColumnPositions[i] - 1]; 305 } 306 307 308 for (indexRows = 0; scan.fetchNext(indexRow.getRowArray()); indexRows++) 309 { 310 314 RowLocation baseRL = (RowLocation) indexRow.getColumn(baseColumns + 1); 315 316 boolean base_row_exists = 317 baseCC.fetch( 318 baseRL, baseObjectArray, indexColsBitSet); 319 320 321 if (! base_row_exists) 322 { 323 String indexName = indexCD.getConglomerateName(); 324 throw StandardException.newException(SQLState.LANG_INCONSISTENT_ROW_LOCATION, 325 (schemaName + "." + tableName), 326 indexName, 327 baseRL.toString(), 328 indexRow.toString()); 329 } 330 331 332 for (int column = 0; column < baseColumns; column++) 333 { 334 DataValueDescriptor indexColumn = 335 indexRow.getColumn(column + 1); 336 DataValueDescriptor baseColumn = 337 baseRowIndexOrder[column]; 338 339 343 if (indexColumn.compare(baseColumn) != 0) 344 { 345 ColumnDescriptor cd = 346 td.getColumnDescriptor( 347 baseColumnPositions[column]); 348 349 361 362 throw StandardException.newException( 363 SQLState.LANG_INDEX_COLUMN_NOT_EQUAL, 364 indexCD.getConglomerateName(), 365 td.getSchemaName(), 366 td.getName(), 367 baseRL.toString(), 368 cd.getColumnName(), 369 indexColumn.toString(), 370 baseColumn.toString(), 371 indexRow.toString()); 372 } 373 } 374 } 375 376 377 scan.close(); 378 scan = null; 379 380 384 if (indexRows != baseRowCount) 385 { 386 throw StandardException.newException(SQLState.LANG_INDEX_ROW_COUNT_MISMATCH, 387 indexCD.getConglomerateName(), 388 td.getSchemaName(), 389 td.getName(), 390 Long.toString(indexRows), 391 Long.toString(baseRowCount)); 392 } 393 } 394 395 ConstraintDescriptorList constraintDescList = 396 dd.getConstraintDescriptors(td); 397 for (int index = 0; index < constraintDescList.size(); index++) 398 { 399 constraintDesc = constraintDescList.elementAt(index); 400 if (constraintDesc.hasBackingIndex()) 401 { 402 ConglomerateDescriptor conglomDesc; 403 404 conglomDesc = td.getConglomerateDescriptor( 405 constraintDesc.getConglomerateId()); 406 if (conglomDesc == null) 407 { 408 throw StandardException.newException( 409 SQLState.LANG_OBJECT_NOT_FOUND, 410 "INDEX for CONSTRAINT", 411 constraintDesc.getConstraintName()); 412 } 413 } 414 } 415 416 } 417 catch (StandardException se) 418 { 419 throw PublicAPI.wrapStandardException(se); 420 } 421 finally 422 { 423 try 424 { 425 426 if (baseCC != null) 427 { 428 baseCC.close(); 429 baseCC = null; 430 } 431 if (indexCC != null) 432 { 433 indexCC.close(); 434 indexCC = null; 435 } 436 if (scan != null) 437 { 438 scan.close(); 439 scan = null; 440 } 441 } 442 catch (StandardException se) 443 { 444 throw PublicAPI.wrapStandardException(se); 445 } 446 } 447 448 return true; 449 } 450 } 451 | Popular Tags |