Previous Topic

Next Topic

Adding or deleting indices

Indices and index segments are key-based search tools that make record seeking faster and more efficient. An index is a mapping table that contains keys describing certain records and pointers to those records. An index segment describes the table field from which the keys are used.

Indices are added to the table definition in the order they are declared. The c-treeDB API also includes a set of functions that will allow an index to be deleted from the table index definition.

ctdbAddIndex() will add a new index at the end of the table index definition. For each index added to the table, one or more index segments should also be added to define which field combination form a particular index. ctdbAddSegment(), ctdbAddSegmentByName(), and ctdbAddSegmentByNbr() accomplish the task of adding segments to an index.

/* allocate a new table handle */
CTHANDLE hTable = ctdbAllocTable(hDatabase);

/* add two fields to the table record definition */
ctdbAddField(hTable, “Field1”, CT_INTEGER, 4);
ctdbAddField(hTable, “Field2”, CT_CHAR, 30);
ctdbDelFieldByName(hTable, “Field2”);

/* add index 0 - the first index */
ctdbAddIndex(hTable, “MyIndex1”, CTINDEX_FIXED, YES, NO);

/* add index 0 segments */
ctdbAddIndexByName(hTable, 0, “Field1”, CTSEG_SCHSEG);

/* add index 1 - the second index */
ctdbAddIndex(hTable, “MyIndex2”, CTINDEX_FIXED, NO, NO);

/* add index 1 segments */
ctdbAddSegmentByName(hTable, 1, “Field2”, CTSEG_SCHSEG);
ctdbAddSegmentByName(hTable, 1, “Field1”, CTSEG_SCHSEG);

/* create the table */
ctdbCreateTable(hTable, “MyTable”, CTCREATE_NORMAL);

ctdbAddIndex() takes a table handle, index name, index type, and two Boolean flags indicating if the index accepts duplicate keys and if the index should process null keys. The valid index types are:

c-treeDB
Index Type

c-treeDB.NET
Index Type


Description

CTINDEX_FIXED

FIXED_INDEX

Fixed length key

CTINDEX_LEADING

LEADING_INDEX

Fixed length keys that are likely to have leading character duplication among the key values

CTINDEX_PADDING

PADDING_INDEX

Variable length keys for which not much leading character duplication is expected.

CTINDEX_LEADPAD

LEADPAD_INDEX

Variable length keys for which much leading character duplication is expected.

CTINDEX_ERROR

ERROR_INDEX

Index type error.

Note: c-treeDB.NET Index Key Types are defined in the KEY_TYPE enum.The add and insert segment functions require a segment mode as the last parameter. Please refer to "Segment Modes", which describes the valid segment modes. An index can be deleted from the table index definition by calling ctdbDelIndex().

/* allocate a new table handle */
CTHANDLE hTable = ctdbAllocTable(hDatabase);
/* add two fields to the table record definition */
ctdbAddField(hTable, “Field1”, CT_INTEGER, 4);
ctdbAddField(hTable, “Field2”, CT_CHAR, 30);
ctdbDelFieldByName(hTable, “Field2”);
/* add index 0 - the first index */
ctdbAddIndex(hTable, “MyIndex1”, CTINDEX_FIXED, YES, NO);
/* add index 0 segments */
ctdbAddIndexByName(hTable, 0, “Field1”, CTSEG_SCHSEG);
/* add index 1 - the second index */
ctdbAddIndex(hTable, “MyIndex2”, CTINDEX_FIXED, NO, NO);
/* add index 1 segments */
ctdbAddSegmentByName(hTable, 1, “Field2”, CTSEG_SCHSEG);
ctdbAddSegmentByName(hTable, 1, “Field1”, CTSEG_SCHSEG);
/* delete index 1 */
ctdbDelIndex(hTable, 1);
/* create the table */
ctdbCreateTable(hTable, “MyTable”, CTCREATE_NORMAL);