The way a new batch operation is started will depend on the type of batch operation you are performing:
Each of these situations is discussed below.
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:
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.
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:
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.
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:
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.
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:
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.
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:
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.