CompactIFile
Compresses a data file, then rebuilds to regenerate the indexes.
Short Name
CMPIFIL()
Type
ISAM function
Declaration
COUNT CompactIFile(pIFIL ifilptr)
Description
CompactIFile() removes any deleted records from the data file pointed to by ifilptr. Up to twice the data file size is needed to perform this operation. This function calls RebuildIFile to rebuild the associated indexes. CompactIFile() produces a compressed data file containing only active records and new optimized indexes. By calling this function, remote client processes can compact and rebuild files in the client/server model.
InitISAM() must be called prior to calling CompactIFile(). The file(s) pointed to by ifilptr must be closed at the time of the call to CompactIFile().
CompactIFile() also supports two features that the file rebuild function RebuildIFile() supports:
These features are used in the same way they are used when calling RebuildIFile():
myifil.tfilno = updateIFIL + purgeIFIL;
To enable or disable compression when calling CompactIFile(), set the chgcompressIFILoption bit in the tfilno member of the IFIL structure whose address you pass to CMPIFIL(). When this bit is set, CMPIFIL() switches the compression state of the file: compressed files become un-compressed, un-compressed files become compressed.
Serial Number Reassignment
When a serial number (SRLSEG or SCHSRL) field reaches the maximum value, FairCom DB simply increments the serial number such that it wraps around and continues with 0, 1, 2, and so on. This can cause a duplicate key error when an attempt is made to add a record. However, a record having that serial number already exists. To provide an option to recover from this situation, FairCom DB now permits the ISAM file compact functions CMPIFIL(), CMPIFILX(), and CMPIFILX8() to assign new serial numbers to the records in the compacted version of a file. Prior to this modification, the compact always copied the records with their original serial numbers to the new compacted file, so the new file used the same serial numbers as the old file.
The caller of the file compact function indicates that the serial numbers are to be reassigned by specifying a special value in the tfilno field of the IFIL structure that it passes to the compact function.
Prior to FairCom DB V9.5, several option values were defined such as updateIFIL, purgeIFIL, and badpartIFIL, that can be specified. These values are specified by adding them together. For example: myifil.tfilno = updateIFIL + purgeIFIL. A new approach simplifies checking of these options. The following option values are now specified in ctport.h:
#define updateIFILoption 0x0002
#define purgeIFILoption 0x0004
#define badpartIFILoption 0x0008
#define redosrlIFILoption 0x0010
#define chgcompressIFILoption 0x0020 - set compression on/off for new file
#define setencryptIFILoption 0x0040 - use current ctSETENCRYPT state for new file
These values can now be OR-ed together and, using the setIFILoptions() macro, assigned to the tfilno field of the IFIL structure passed to the compact or rebuild function. For example, to indicate that you want to assign new serial numbers, do the following:
myifil.tfilno = setIFILoptions(redosrlIFILoption);
CMPIFIL(&myifil);
If you want to also use more than one option when compacting or rebuilding a file, OR them in to the value. For example:
/* assign new serial numbers and update IFIL resource. */
myifil.tfilno = setIFILoptions(redosrlIFILoption | updateIFILoption);
CMPIFIL(&myifil);
While it is possible to rebuild a mirrored file in c-tree, it is not possible to compact such a file.
Updates for V11
Beginning in FairCom DB V11, several improvements were made to the API function and the compact utility. The file compact utility (ctcmpcif) and the CompactIFile() API function now better handle file encryption The following improvements were made:
NINT rc;
myifil.tfilno = setIFILoptions(setencryptIFILoption);
rc = ctSETENCRYPT(ctAES32, NULL, 32);
rc = CMPIFIL(&myifil);
-encrypt=cipher - Create the compacted file using the specified cipher:
Note: If an index file does not exist, the original data file's encryption attributes are used when creating that index file.
To change the encryption attributes of a file using the compact API function from a client, you must add the option CHANGE_ENCRYPTION_ON_COMPACT YES to ctsrvr.cfg. Otherwise, the operation fails with error NSUP_ERR (454, not supported).
Online Compact - new for V13
Files can now be compacted while the file is open in ctSHARED mode. To enable this request, call CMPIFIL() (CompactIFile) using the new onlineIFILoption bit:
myifil.tfilno = setIFILoptions(onlineIFILoption);
This carries out a more limited form of file compact, referred to as defragmentation, while the file remains available to other users. Defragmentation attempts to reorganize the records within the file to minimize file size. When defragmentation is completed, the size of the file is reduced, if possible.
If the file is under ctTRNLOG control, a ctSHARED mode index rebuild will automatically follow a successful ctSHARED mode compact.
LIMITATIONS: Server Only. The calling user should have the target file initially closed with no active transactions. Other CMPIFIL options specified using setIFILoptions()are not available in combination with onlineIFILoption. Defragmentation is not supported for memory files, super files, partition host files, segmented files, or index files.
NOTES: Defragmentation works similar to a regular user normally updating records within the file. Due to the type and extent of changes during defragmentation, it may highlight artifacts of multiuser interference not frequently observed by applications. Applications using files without transaction control, or using physical order scans, or using more relaxed locking protocols may see increased rates of these artifacts when defragmentation is in progress. This may appear as unexpected missing or even duplicate records (which a future scan doesn't encounter).
Return
A zero (0) return indicates successful operation. A non-zero return indicates an error, check isam_err. The error returns are similar OpenCtFile() and RebuildIFile(). See FairCom DB Error Codes for a complete listing of valid FairCom DB error values.
Example
extern IFIL customer;
COUNT retval;
main() {
if (retval = InitISAM(10,10,16))
printf("\nInitISAM error = %d",retval);
if (!(CompactIFile(&customer)))
printf("\nSuccessful compact");
else
printf("\nCompactIFile isam_err = %d",isam_err);
CloseISAM();
}
Limitations
See also