Product Documentation

c-treeDB API for C++ - Developers Guide

Previous Topic

Next Topic

Starting a new batch operation

The way a new batch operation is started will depend on the type of batch operation you are performing:

  • Retrieving records by partial key
  • Retrieving records by index range
  • Retrieving records by physical order
  • Deleting a group of records
  • Inserting a group of records

Each of these situations is discussed below.

In This Section

Retrieving records by partial key

Retrieving records by index range

Retrieving records by physical order

Deleting a group of records

Inserting a group of records

Previous Topic

Next Topic

Retrieving records by partial key

All records with key matching a partial target key are loaded into a buffer region maintained internally by c-treeDB. If the selected records do not fit in the buffer, those that fit are loaded, and subsequent calls will retrieve the remaining records.

The following steps must be taken to perform a batch retrieval operation based on a partial key:

  1. Clear a record buffer by calling the CTRecord:Clear() method.
  2. Use CTRecord::SetFieldAs...() methods to set the fields that form the partial target key that will be used to select a group of records.
  3. Call the CTRecord::SetBatch() method, with the CTBATCH_GET mode, to start a new record retrieval batch operation.
  4. Call the CTRecord::NextBatch() method repeatedly until all related records are retrieved. CTRecord::NextBatch() returns false to indicate no more records are available.
  5. When you are done with the batch records, call the CTRecord::EndBatch() method to terminate the batch operation. Please note that another batch operation can only start after the current batch operation is terminated.

To start a new batch operation to retrieve records, you must first establish the partial target key that will identify the group of related records. This is accomplished by clearing a record buffer and setting the fields that form the partial target key. For example, if an invoice item table has one index with segments based on invoice number and item number fields, the following partial code will create a partial target key that will select all invoice item records associated with a particular invoice number:

Example

// set the partial target key

hRecord.Clear();

hRecord.SetFieldAsSigned("Invoice", Invoice);

After preparing the partial target key, a new batch operation is started by calling the CTRecord::SetBatch() method. Continuing from the example above, a new batch operation is started by performing the following call:

Example

try

{

// set the batch operation

hRecord.SetBatch(CTBATCH_GET, sizeof(Invoice), 0);

// retrieve and display all records

while (hRecord.NextBatch())

PrintRecord(hRecord);

// terminate batch operations

hRecord.EndBatch();

}

catch (CTException &err)

{

printf("Batch operation failed with error %d\n", err.GetErrorCode());

}

CTRecord::SetBatch() takes as first argument a valid record handle. The second argument is the batch mode that will direct how the batch operation will be performed. The chapters below describe the available batch modes in detail.

You must provide the length of the SetBatch()’s target key in bytes. The length of the target key will indicate how many bytes of the target key should be used to create the partial target key. The last parameter is the size of the buffer used internally by c-treeDB to handle batch operations. A zero value for the last parameter is an indication that the default value size should be used. The default buffer size is calculated as the size of the fixed portion of the record multiplied by 128.

Previous Topic

Next Topic

Retrieving records by index range

All records that match an index range expression are loaded into a buffer region maintained internally by c-treeDB. If the selected records do not fit in the buffer, those that fit are loaded, and subsequent calls will retrieve the remaining records.

The following steps must be taken to perform an index range batch retrieval of records:

  1. Establish an index range by calling the CTRecord::RangeOn() method.
  2. Call the CTRecord::SetBatch() method with the CTBATCH_RANGE mode to start a new record retrieval batch operation.
  3. Call the CTRecord::NextBatch() method repeatedly until all related records are retrieved. CTRecord::NextBatch() returns false to indicate no more records are available
  4. When you are done with the batch records, call the CTRecord::EndBatch() method to terminate the batch operation.
  5. Call the CTRecord::RangeOff() method to terminate index range operations.

To start a new batch operation to retrieve records, you must first establish the index range operation that will identify the group of related records.

Example:

try

{

// build target key to be used in index range

TEXT lRange[32];

VRLEN len = (VRLEN)sizeof(lRange);

NINT op = CTIX_EQ;

hRecord.Clear();

hRecord.SetFieldAsSigned("Invoice", Invoice);

hRecord.BuildTargetKey(CTFIND_EQ, (pVOID)lRange, &len);

// set the index range based on the target key

hRecord.RangeOn(1, (pVOID)lRange, NULL, CTIX_EQ&op);

}

catch (CTException &err)

{

printf("Index range operation failed with error %d\n", err.GetErrorCode());

}

After setting the index range operation, you may start a new batch operation by calling the CTRecord::SetBatch() method.

Example:

try

{

// set the batch operation

hRecord.SetBatch(CTBATCH_RANGE, 0, 0);

// retrieve and display all records

while (hRecord.NextBatch())

PrintRecord(hRecord);

// terminate batch operations

hRecord.EndBatch();

// terminate the index range

hRecord.RangeOff();

}

catch (CTException &err)

{

printf("Batch operation failed with error %d\n", err.GetErrorCode());

}

Notice when retrieving records by index range SetBatch()’s targetLen parameter is set to zero. The last parameter is the size of the buffer used internally by c-treeDB to handle batch operations. A zero value for the last parameter is an indication that the default value size should be used. The default buffer size is calculated as the size of the fixed portion of the record multiplied by 128.

Previous Topic

Next Topic

Retrieving records by physical order

All records of a table are loaded by physical order into a buffer region maintained internally by c-treeDB. If the selected records do not fit in the buffer, those that fit are loaded, and subsequent calls will retrieve the remaining records.

The following steps must be taken to perform a physical order batch retrieval of records:

  1. Call the CTRecord:::SetBatch() method with CTBATCH_PHYS mode to start a new record retrieval batch operation.
  2. Continue to call the CTRecord::NextBatch() method repeatedly until all related records are retrieved. CTRecord::NextBatch() returns false to indicate no more records are available.
  3. When you are done with the batch records, call the CTRecord::EndBatch() method to terminate the batch operation.

Example

try

{

// set the batch operation */

hRecord.SetBatch(CTBATCH_PHYS, 0, 0);

// retrieve and display all records

while (hRecord.NextBatch())

PrintRecord(hRecord);

// terminate batch operations

hRecord.EndBatch();

}

catch (CTException &err)

{

printf("Batch operation failed with error %d\n", err.GetErrorCode());

}

Notice when retrieving records by index range SetBatch()’s targetLen parameter is set to zero since no partial key is need for this operation. The last parameter is the size of the buffer used internally by c-treeDB code to handle batch operations. A zero value for this parameter is an indication that the default value size should be used. The default buffer size is calculated as the size of the fixed portion of the record multiplied by 128.

Previous Topic

Next Topic

Deleting a group of records

If the intended batch operation is to delete a group of selected records, you need to initially set the partial target key to select the group of related records and then start the batch operation to delete the selected records.

Even if no records are retrieved with the delete operation, CTRecord::EndBatch() must be called to terminate the current batch operation.

The following steps must be taken to perform a batch delete record operation:

  1. Clear a record buffer by calling the CTRecord::Clear() method.
  2. Use the CTRecord::SetFieldAs...() methods to set the fields that form the partial target key that will be used to select a group of records.
  3. Call the CTRecord::SetBatch() method with the CTBATCH_DEL mode to delete a group of related records.
  4. Call the CTRecord::EndBatch() method to terminate the delete record batch operation.

Example

try

{

// set the partial target key

hRecord.Clearh();

hRecord.SetFieldAsSigned("Invoice", Invoice);

// set the batch operation

hRecord.SetBatch(CTBATCH_DEL, sizeof(Invoice), 0);

// end the batch operation

hRecord.EndBatch();

}

catch (CTException &err)

{

printf("Batch operation failed with error %d\n", err.GetErrorCode());

}

You must provide the length of SetBatch()’s Target key in bytes. The length of the target key indicates how many bytes of the target key should be used to create the partial target key. The last parameter is the size of the buffer used internally by c-treeDB to handle batch operations. A zero value for the last parameter indicates the default value size should be used. The default buffer size is calculated as the size of the fixed portion of the record multiplied by 128.

Previous Topic

Next Topic

Inserting a group of records

A group of new records are loaded into a buffer region maintained internally by c-treeDB and this group of records are inserted into a table.

When the batch buffer fills up, the group of records stored in the batch buffer are inserted into the table. If CTRecord::EndBatch() is called and the batch buffer still contains records, a new insert record operation is performed for the remaining records before the batch operation is terminated.

For transaction controlled files, the batch insertion operation is treated as one all or nothing operation. If no explicit transaction is started, each insertion of records with will start and end its own transaction. Even if an explicit transaction is started, each insertion operation is treated independently through safe points.

Currently, all records insertion operations do not perform any conversion of record images, key values and record position for heterogeneous client/server implementations.

The following steps must be taken to perform a batch insert record operation:

  1. Call the CTRecord::SetBatch() method, with CTBATCH_INS mode, to insert a group of records.
  2. For each record to be inserted perform the following operations:
    1. Call the CTRecord::Clear() method to clear a record buffer.
    2. For each field in the record call one of the CTRecord::SetFieldAs...() methods to set the field data.
    3. Call the CTRecord::InsertBatch() method to insert the record into the batch buffer.
  3. Call the CTRecord::EndBatch() method to indicate no more records will be inserted.

Example

try

{

// set the batch operation

hRecord.SetBatch(CTBATCH_INS, 0, 0);

// prepare the first record

hRecord.Clear();

hRecord.SetFieldAsSigned("Invoice", Invoice); // invoice number

hRecord.SetFieldAsSigned("ItemNbr", 1); // invoice item number

hRecord.SetFieldAsSigned("Quantity", 100); // item quantity

hRecord.SetFieldAsSigned("ItemCode", 1001); // item code

hRecord.InsertBatch(); // insert record in batch

// prepare the second record

hRecord.Clear();

hRecord.SetFieldAsSigned("Invoice", Invoice); // invoice number

hRecord.SetFieldAsSigned("ItemNbr", 2); // invoice item number

hRecord.SetFieldAsSigned("Quantity", 200); // item quantity

hRecord.SetFieldAsSigned("ItemCode", 1002); // item code

hRecord.InsertBatch(); // insert record in batch

// terminate the batch operation

hRecord.EndBatch();

}

catch (CTException &err)

{

printf("Batch operation failed with error %d\n", err.GetErrorCode());

}

Notice when inserting a group of records SetBatch()’s targetLen parameter is set to zero as no partial key is needed for this operation. The last parameter is the size of the buffer used internally by c-treeDB to handle batch operations. A zero value for this parameter indicates the default value size should be used. The default buffer size is calculated as the size of the fixed portion of the record multiplied by 128.

TOCIndex