Product Documentation

c-treeDB API for C# - Developers Guide

Previous Topic

Next Topic

Adding or Deleting Indexes

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

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

CTTable.AddIndex() 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 forms a particular index. CTTable.AddSegment() overloaded methods will accomplish the task of adding segments to an index.

// create a new table object

CTTable ATable = new CTTable(ADatabase);


// add two fields to the table record definition

ATable.AddField("Field1", FIELD_TYPE.INTEGER, 4);

ATable.AddField("Field2", FIELD_TYPE.CHARS, 30);


// add index 0 - the first index

ATable.AddIndex("MyIndex1", KEY_TYPE.FIXED_INDEX, true, false);


// add index 0 segments

ATable.AddSegment("MyIndex1", "Field1", SEG_MODE.SCHSEG_SEG);


// add index 1 - the second index

ATable.AddIndex("MyIndex2", CTINDEX_FIXED, false, false);


// add index 1 segments

ATable.AddSegment("MyIndex2", "Field2", SEG_MODE.SCHSEG_SEG);

ATable.AddSegment("MyIndex2", "Field1", SEG_MODE.SCHSEG_SEG);


// create the table

try

{

ATable.Create("MyTable", CREATE_MODE.NORMAL_CREATE);

}

catch (CTException err)

{

Console.Write("Create table failed with error {0}\n", err.GetErrorCode());

}

The CTTable.AddIndex() method takes an 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 defined in the KEY_TYPE enum:

Index Key Type

Description

FIXED_INDEX

Fixed length key

LEADING_INDEX

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

PADDING_INDEX

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

LEADPAD_INDEX

Variable length keys for which much leading character duplication is expected

ERROR_INDEX

Index type error

The add and insert segment functions require a segment mode to be passed as the last parameter. Please refer to Segment Modes describing the valid segment modes.

An index can be deleted from the table index definition by calling one of CTTable.DelIndex() overloaded methods.

// create a new table object

CTTable ATable = new CTTable(ADatabase);


// add two fields to the table record definition

ATable.AddField("Field1", FIELD_TYPE.INTEGER, 4);

ATable.AddField("Field2", FIELD_TYPE.CHARS, 30);


// add index 0 - the first index

ATable.AddIndex("MyIndex1", KEY_TYPE.FIXED_INDEX, true, false);


// add index 0 segments

ATable.AddSegment("MyIndex1", "Field1", SEG_MODE.SCHSEG_SEG);


// add index 1 - the second index

ATable.AddIndex("MyIndex2", KEY_TYPE.FIXED_INDEX, false, false);


// add index 1 segments

ATable.AddSegment("MyIndex2", "Field2", SEG_MODE.SCHSEG_SEG);

ATable.AddSegment("MyIndex2", "Field1", SEG_MODE.SCHSEG_SEG);


// delete index 0

ATable.DelIndex("MyIndex1");


// create the table

try

{

ATable.Create("MyTable", CREATE_MODE.NORMAL_CREATE);

}

catch (CTException err)

{

Console.Write("Create table failed with error {0}\n", err.GetErrorCode());

}

TOCIndex