Rebuilding Indexes and Compacting Data FilesDepending on the file options in effect for a c-tree index, a catastrophic failure may leave the index in a state that requires the index to be re-created and its key values reconstructed from the data file. This operation is known as rebuilding the index. Because TRNLOG indexes are recovered using the server’s automatic recovery, they generally do not need to be rebuilt after a catastrophic failure, unless the index file is damaged. PREIMG and non-transaction indexes do require rebuilding following a catastrophic failure because any cached updates that had not been written to disk at the time of the failure are lost, and the data and index files may be out of sync. A file open attempt that fails with error FCRP_ERR (14) indicates that the file’s update flag is set, meaning that it was updated and not closed properly and could be missing updates or the data and index files could be out of sync. In this situation, the file can be restored to a clean state by rebuilding the indexes. If a data file and its indexes are created and maintained using c-tree ISAM-level functions, the ISAM file definitions (IFIL, IIDX, and ISEG structures) determine how key values are constructed from data record images. In this case, the RebuildIFile(), RebuildIFileXtd(), or RebuildIFileXtd8() functions or the ctrbldif utility can be used to rebuild the indexes using the specified ISAM file definitions. An ISAM rebuild performs the following operations on the data and index files:
An ISAM rebuild can optionally mark records containing unwanted duplicate key values as deleted. This feature is useful when an index exists that does not allow duplicates and the rebuild unexpectedly finds more than one record in the data file having the same key value. See the c-tree Plus Function Reference Guide for details on using this and other ISAM rebuild options. If a data file and its indexes are created and maintained using c-tree low-level functions, the application determines how key values are constructed from data record images. For this reason, the application must implement its own rebuild functionality. This can be accomplished by writing a utility that reads all records from the data file and for each record constructs key values using the application’s key formation logic and adds the key values to the indexes. The primary purpose of a rebuild is to fix errors that occur in index files. When an error occurs in a data file (for example, due to a catastrophic failure that damages the data file), it may be appropriate to create a new copy of the data file containing the active records that can be read from the damaged data file. This is done by compacting the data file. This procedure is known as compacting because it typically produces a data file smaller than the original data file because the new file contains only active records (no deleted records). If a data file and its indexes are created and maintained using c-tree ISAM-level functions, the data file can be compacted using the ISAM functions CompactIFile() or CompactIFileXtd() or the ctcmpcif utility. These functions scan the original data file, producing a new data file containing only active records and new optimized indexes. The compact operation automatically calls RebuildIFile() to rebuild the indexes after creating the compacted data file. Because the compact produces a new data file, up to twice the data file size is needed to perform this operation. If a data file and its indexes are created and maintained using c-tree low-level functions, the application must implement its own compact functionality. This can be accomplished by writing a utility that creates a new data file matching the original data file definitions, reads all active records from the data file and for each record constructs key values using the application’s key formation logic and adds the key values to the indexes. |
|||