A table handle is required before most table operations can take place. A table handle is allocated with ctdbAllocTable() and freed with ctdbFreeTable().
When allocating a table handle you need to provide a handle to a database so the new table handle can be properly associated with that database. The database handle must have been previously allocated by calling ctdbAllocDatabase(), but need not be connected.
CTHANDLE hSession = ctdbAllocSession(CTSESSION_CTDB);
CTHANDLE hDatabase = ctdbAllocDatabase(hSession);
CTHANDLE hTable = ctdbAllocTable(hDatabase);
if (!hSession || !hDatabase || !hTable) {
printf("Session, database or table handle allocation failed\n");
exit(1);
}
When the table handle is no longer needed it should be released by calling ctdbFreeTable(). A table handle must be allocated and released by ctdbAllocTable() and ctdbFreeTable().
Do not use the C runtime library functions malloc() and free() on table handles (or any FairCom DB handles).
If you release an active table's handle (the handle of a table that is currently open), ctdbFreeTable() automatically closes the table and resets all of the record buffers in the record handles that have been associated with the table.
It is possible to create or open a table without database support by providing the session handle when allocating the table handle. Here is an example without error checking:
CTHANDLE hSession;
CTHANDLE hTable;
/* Allocate a new session handle */
hSession = ctdbAllocSession(CTSESSION_CTREE);
/* Logon to session */
ctdbLogon(hSession, "FAIRCOMS", "ADMIN", "ADMIN");
/* Allocate a new table handle, note the use of a session handle, not a database handle */
hTable = ctdbAllocTable(hSession);
/* Override the default path where FairCom DB API will look for the table files. */
ctdbSetTablePath(hTable, "c:\\MyDocuments");
/* Open the table */
ctdbOpenTable(hTable, "mytable", CTOPEN_NORMAL);
Note that FairCom DB API will try to open the table files from the server directory (client/server applications) or from the execution directory (stand-alone applications). If you wish to override this default location, call ctdbSetTablePath() to specify your own directory before you call ctdbOpenTable(). The same "default directory" principle applies when creating a table without database support:
CTHANDLE hSession;
CTHANDLE hTable;
/* Allocate a new session handle without session dictionary support*/
hSession = ctdbAllocSession(CTSESSION_CTREE);
/* Logon to session */
ctdbLogon(hSession, "FAIRCOMS", "ADMIN", "ADMIN");
/* Allocate a new table handle, note the use of a session handle, not a database handle */
hTable = ctdbAllocTable(hSession);
/* add fields to table */
ctdbAddField(hTable, "Field0", CT_INT2, 2);
ctdbAddField(hTable, "Field1", CT_FSTRING, 30);
/* Override the default path where FairCom DB API will write the table files. */
ctdbSetTablePath(hTable, "c:\\MyDocuments");
/* Create the table in the directory specified above */
ctdbCreateTable(hTable, "mytable", CTCREATE_NORMAL);