Create Index (31)
The Create Index operation (B_BUILD_INDEX) adds a key to an existing file.
Parameters
 
Prerequisites
 
1”n/a” stands for “not applicable”
2”rounded up” means that the page size is rounded up to the next size supported by the file version. For example, 512 is rounded up to 1,024, 2,560 is rounded up to 4,096, and so forth.
3The maximum number of index segments that can be used with the Relational Engine is 119. For the MicroKernel Engine, the maximum number is 204 for a page size of 4,096, and 420 for page sizes 8,192 and 16,384.
 
See also the status codes 26: The number of keys specified is invalid and 29: The key length is invalid, both in Status Codes and Messages.
Procedure
1
2
3
For each segment in the key, store a 16-byte key specification block in the data buffer. Use the same structure as defined in Table 8. Store the information for the key position and the key length as integers. If you are rebuilding the system-defined log key (also called system data), the data buffer must be at least 16 bytes long and initialized to zeroes.
4
 
 
5
16 * (# of segments)
If the new key specifies an ACS other than the default, use the following formula to determine the correct data buffer length:
16 * (# of segments) + 265
6
*Note: Key numbers must be unique to the file. They must also be valid. (The value of each key number must be less than the maximum number of key segments allowed for the file’s page size.)
Details
The MicroKernel Engine allows you to assign specific key numbers when creating a key. This capability complements the ability to delete a key and not have the MicroKernel Engine renumber all keys that have a key number greater than that of the deleted key. If an application drops an index and instructs the MicroKernel Engine not to renumber higher-numbered keys, and a user then clones the affected file without assigning specific key numbers, the cloned file has different key numbers than the original.
If you define an ACS in the data buffer, the MicroKernel Engine first checks for an existing ACS (using the name you specified) before adding it to the file. If the MicroKernel Engine finds an existing ACS with the name you specified, the MicroKernel Engine does not duplicate the ACS definition in the file, but does associate the ACS with the new key.
If you specify the Use Named ACS attribute in the Key Flags word, the MicroKernel Engine uses the ACS name supplied in the data buffer to locate an ACS of the same name within the file, then assigns that ACS to the new key.
If a file is opened by more than one MicroKernel Engine client and one of the clients starts a Create Index process, remote clients can perform Get and Step operations on the open file while the MicroKernel Engine client creates the key.
If the key being created is not an AUTOINCREMENT key, the Get and Step operations of remote clients can have lock biases, and when the Create Index process is completed, you can update and delete the locked records without issuing additional read operations. This is possible because the MicroKernel Engine does not have to change the images of the records in order to create the key.
However, if the key being created is an AUTOINCREMENT key, the MicroKernel Engine has to both build the index and change every record with a zero value in the appropriate field. Remote clients that perform Get or Step operations without a lock bias before or during the key creation can receive Status Code 80 when they execute an update or delete operation after the successful completion of the key creation.
Also, the MicroKernel Engine returns Status Code 84 if a client attempts to create an AUTOINCREMENT key while another client has locked a record. Similarly, the MicroKernel Engine returns Status Code 85 if a client attempts to execute a Get or Step operation with a lock bias during index creation for an AUTOINCREMENT key by another client.
Result
The MicroKernel Engine immediately adds the new key to the file. The time required for this operation depends on the total number of records to be indexed, the size of the file, and the length of the new index.
If the Create Index operation is successful, the number of the new key is either the number you specified or one of the following:
You can use the new key to access your data as soon as the operation completes.
If the Create Index operation is unsuccessful, the MicroKernel Engine drops whatever portion of the new index it has already built. The file pages allocated to the new index prior to the error are placed on a list of free space for the file and reused when you insert records or create another key.
If the operation fails during the creation of an AUTOINCREMENT key, any values that have already been altered remain altered. The MicroKernel Engine can return the following status codes:
If processing is interrupted during the creation of a key, you can access the data in the file through the file’s other keys. However, the MicroKernel Engine returns a nonzero status code if you try to access data by the incomplete index. To correct this problem, drop the incomplete index with a Drop Index operation (32) and reissue the Create Index operation.
Positioning
The Create Index operation has no effect on any file currency information.