How do I create an index using the FairCom c-treeACE CTDB API?

141 views Asked by At

Here is my code based on the CTDB tutorial.

#include <stdio.h>
#include <tchar.h>
#include "ctdbsdk.h" /* c-tree headers */

#define END_OF_FILE INOT_ERR  /* INOT_ERR is ctree's 101 error. See cterrc.h */

CTHANDLE hSession;
CTHANDLE hDatabase;
CTHANDLE hTable;
CTHANDLE hRecord;


VOID Handle_Error(CTSTRING errmsg)
{
    printf("\nERROR: [%d] - %s \n", ctdbGetError(hSession), errmsg);
    printf("*** Execution aborted *** \n");
    printf("Press <ENTER> key to exit...");

    ctdbLogout(hSession);

    ctdbFreeRecord(hRecord);
    ctdbFreeTable(hTable);
    ctdbFreeSession(hSession);
    getchar();
    exit(1);
}



// Open the table, if it exists. Otherwise create and open the table
VOID Define(VOID)
{
    CTHANDLE hField1, hField2, hField3, hField4;
    CTHANDLE hField5, hField6, hField7;

    printf("DEFINE\n");

    /* allocate a table handle */
    if ((hTable = ctdbAllocTable(hDatabase)) == NULL)
        Handle_Error("Define(); ctdbAllocTable()");

    /* open table */
    printf("\tOpen table...\n");
    if (ctdbOpenTable(hTable, "custmast", CTOPEN_NORMAL)) {
        /* define table fields */
        printf("\tAdd fields...\n");
        hField1 = ctdbAddField(hTable, "cm_custnumb", CT_INT8U, 0);
        hField2 = ctdbAddField(hTable, "cm_custzipc", CT_FSTRING, 9);
        hField3 = ctdbAddField(hTable, "cm_custstat", CT_FSTRING, 2);
        hField4 = ctdbAddField(hTable, "cm_custratg", CT_FSTRING, 1);
        hField5 = ctdbAddField(hTable, "cm_custname", CT_STRING, 47);
        hField6 = ctdbAddField(hTable, "cm_custaddr", CT_STRING, 47);
        hField7 = ctdbAddField(hTable, "cm_custcity", CT_STRING, 47);

        if (!hField1 || !hField2 || !hField3 || !hField4 ||
            !hField5 || !hField6 || !hField7)
            Handle_Error("Define(); ctdbAddField()");

        /* create table */
        printf("\tCreate table...\n");
        if (ctdbCreateTable(hTable, "custmast", CTCREATE_NORMAL))
            Handle_Error("Define(); ctdbCreateTable()");

        if (ctdbOpenTable(hTable, "custmast", CTOPEN_NORMAL))
            Handle_Error("Define(); ctdbOpenTable()");
    }
}


NINT main(NINT argc, pTEXT argv[])
{
    CTDBRET  retval;

    printf("INIT\n");

    if ((hSession = ctdbAllocSession(CTSESSION_CTDB)) == NULL) /* allocate session handle */
        Handle_Error("Initialize(): ctdbAllocSession()");

    printf("\tLogon to server...\n");
    if (ctdbLogon(hSession, "FAIRCOMS", "ADMIN", "ADMIN")) /* connect to server */
        Handle_Error("Initialize(): ctdbLogon()");

    hDatabase = ctdbAllocDatabase(hSession);
    if (ctdbConnect(hDatabase, "FTS") == CTDBRET_OK) {
        printf("Connected to DB\n");
    } else {
        printf("Creating DB\n");
        if (ctdbCreateDatabase(hSession, "FTS", "")) {
            Handle_Error(hSession, "ctdbCreateDatabase()");
        }
        ctdbConnect(hDatabase, "FTS");
    }


    Define();

    printf("\tClose table...\n");
    if (ctdbCloseTable(hTable))
        Handle_Error("Done(): ctdbCloseTable()");

    printf("\tLogout...\n");
    if (ctdbLogout(hSession))
        Handle_Error("Done(): ctdbLogout()");

    ctdbFreeRecord(hRecord);
    ctdbFreeTable(hTable);
    ctdbFreeDatabase(hDatabase);
    ctdbFreeSession(hSession);

    printf("\nPress <ENTER> key to exit . . .\n");
    getchar();

    return(0);
}
1

There are 1 answers

0
Ray Hulha On BEST ANSWER

Simply add this before you call ctdbCreateTable:

    CTHANDLE hIndex;
    if ((hIndex = ctdbAddIndex(hTable, "cm_custnumb_idx", CTINDEX_FIXED, NO, NO)) == NULL)
        Handle_Error("Define(); ctdbAddIndex()");

    CTHANDLE hSeg;
    if ((hSeg = ctdbAddSegment(hIndex, hField1, CTSEG_SCHSEG)) == NULL)
        Handle_Error("Define(); ctdbAddSegment()");