PreviousBtrieve API Guide (9.1 revision 1) Next

Create Index (31)

Show this topic in Library frames

The Create Index operation (B_BUILD_INDEX) adds a key to an existing file.

Parameters

 
Op Code
Pos Block
Data Buf
Data Buf Len
Key Buffer
Key Number
Sent

 

Returned
           

Prerequisites
Procedure
  1. Set the Operation Code to 31.
  2. Pass the Position Block for the file to which to add the key.
  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 2-1. 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. To define an ACS for the new key, perform one of the following steps:
    • To use the default ACS, which is the first ACS already defined in the file, specify the Use Default ACS attribute in the Key Flags word.
    • To define a new ACS, specify the Use Numbered ACS attribute in the Key Flags word and set the ACS Number field to zero (0). In addition, store the 265-byte ACS after the last key specification block in the Data Buffer.
    • To specify an existing ACS by name, specify the Use Named ACS attribute in the Key Flags word and set the ACS Number field to zero (0). In addition, store the name of the ACS at the beginning of the 265-byte block after the last key specification block in the Data Buffer. (The remainder of the ACS block after the name is ignored.) The name must be in one of the following formats:
      ACS Type
      Length (in Bytes)
      Description
      User-defined ACS
      1
      Signature 0xAC
      "
      8
      ACS table name
      ISR
      1
      Signature 0xAE
      "
      16
      ISR table name
  5. Set the Data Buffer Length parameter to the number of bytes in the Data Buffer. For a new key with no ACS (or one that uses the default ACS), use the following formula to determine the correct Data Buffer Length:
  6. 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

  7. To assign a specific Key Number to the key being created, add the desired key number to 0x80, and place the sum in the Key Number parameter. If you are rebuilding the system-defined log key (also called system data), specify 0xFD (that is, key number 125 plus 128).

  8. 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 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 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 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 first checks for an existing ACS (using the name you specified) before adding it to the file. If the MicroKernel finds an existing ACS with the name you specified, the MicroKernel 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 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 and a client initiates a Create Index process, remote clients can perform Get and Step operations on the same file while the MicroKernel 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 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 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 returns Status Code 84 if a client attempts to create an AUTOINCREMENT key while another client has locked a record. Similarly, the MicroKernel 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 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 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 can return the following status codes:

22

The data buffer length is too short.

27

The key position is invalid.

41

The MicroKernel does not allow the attempted operation.

45

The specified key flags are invalid.

49

The extended data type is invalid.

56

An index is incomplete.

84

The record or page is locked.

85

The file is locked.

104

The MicroKernel does not recognize the locale.

134

The MicroKernel cannot read the International Sorting Rule.

135

The specified International Sort Rule table is corrupt or otherwise invalid.

136

The MicroKernel cannot find the specified Alternate Collating Sequence in the file.

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 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.


Chapter contents
Publication contents

Prev topic: Create (14)
Next topic: Delete (4)