Product Documentation

c-treeDB API for C++ - Developers Guide

Previous Topic

Next Topic

Opening a table

A table must be opened before any data operations within it can take place. Use the CTTable::Open() method to open a table.

// open a table

CTTable ATable(ADatabase);

try

{

ATable.Open("MyTable", CTOPEN_NORMAL);

}

catch (CTException &err)

{

printf("Open table failed with error %d\n", err.GetErrorCode());

}

After opening the table, usual operations like add, update, delete, and search for records can be done. Record operations are described in detail in "Working with Records".

The CTTable::Open() method take as parameters a table name and the table open mode.

c-treeDB File
Open Mode

c-treeDB .NET
File Open Mode


Explanation

CTOPEN_NORMAL

NORMAL_OPEN

Use this mode if no other open modes apply.

CTOPEN_DATAONLY

DATAONLY_OPEN

Open only the data table. Used to rebuild a table that may or may not be missing indexes.

  • Caution: Updates made to a data file with this file mode will not have any necessary updates reflected in the associated index files.

CTOPEN_EXCLUSIVE

EXCLUSIVE_OPEN

This mode opens the table and the associated index as exclusive. If this mode is used, only one user can open a table at a time. If an application already has the file(s) open in any mode, no other application can open the table as CTOPEN_EXCLUSIVE. Once an application opens a table as CTOPEN_EXCLUSIVE, no other application can open it. Reads and writes are cached for the files opened with this file mode since there are no integrity issues with only one process in the file.

CTOPEN_PERMANENT

PERMANENT_OPEN

Many operating systems and/or C compiler run-time libraries limit the number of files that can be opened at one time. A permanent file open causes the table and index files to be opened and stay open until the program executes a file close. A non-permanent file open causes the table data and index files to be opened, but allows them to be transparently closed and reopened to allow other data and index files to be used. When it is necessary for a data and index file to be temporarily closed, FairCom DB selects the least recently used file. This file remains closed until it is used, at which time it will be automatically reopened. Non-permanent mode is the default and should be used unless you have a special circumstance. This strategy causes FairCom DB to use all available file descriptors.

CTOPEN_CORRUPT

CORRUPT_OPEN

This mode opens tables with corrupted indexes, or in certain cases, tables with corrupted data.

Corrupt indexes: With c-treeDB this mode is usually used in conjunction with the ctdbAlterTable() function to perform a rebuild: open the table with CTOPEN_CORRUPT mode then call ctdbAlterTable() with the CTDB_ALTER_INDEX action to force the rebuild of all indexes of the table.

You can also call ctdbAlterTable() with the (CTDB_ALTER_INDEX | CTDB_ALTER_PURGEDUP) actions to purge any duplicate records that may cause the index rebuild to fail.

Corrupt table: If a table becomes corrupt, the table may be opened with CTOPEN_CORRUPT mode and then ctdbAlterTable() is invoked with the CTDB_ALL_FULL action to try to recover the table. Note that this is a last-ditch effort that could cause data loss. Please contact FairCom before you do this.

CTOPEN_CHECKLOCK

CHECKLOCK_OPEN

Tables opened with this mode require a record lock before a record can be updated. If a lock is not obtained, the error code DADV_ERR is returned.

CTOPEN_CHECKREAD

CHECKREAD_OPEN

Tables opened with this mode require a record lock as records are read. Obtain at least a read lock on a record before it can be read, otherwise the function will return error code DADV_ERR.

CTOPEN_READONLY

READONLY_OPEN

Opens the table in READONLY mode and does not allow any modifications to the table structure or data records.

CTOPEN_UNLOCKONCLOSE

UNLOCKONCLOSE_OPEN

Enables ctdbCloseTable() to explicitly call ctdbUnLockTable().

Note: c-treeDB .NET users can find the open modes listed in the OPEN_MODE enum.

In This Section

Opening a table with password

Closing a table

Previous Topic

Next Topic

Opening a table with password

If a table was created with a password, every time that table is open, we need to specify the correct password for the open table operation to succeed. After the table object is created, but before the table is opened, the table password property must be set.

// opening a table with password

CTTable ATable(ADatabase);


// set the table password

ATable.SetPassword("MyPassword");


// open the table

try

{

ATable.Open("MyTable", CTOPEN_NORMAL);

}

catch (CTException &err)

{

printf("Open table failed with error %d\n", err.GetErrorCode());

}

Previous Topic

Next Topic

Closing a table

After a successful open table, and if the table object is no longer needed, the table should be closed to allow all c-treeDB, c-tree Plus and operating systems buffers to be flushed to disk. It is very good programming practice to always close every open table before the process or thread is terminated.

// close the table

try

{

ATable.Close();

}

catch (CTException &err)

{

printf("Close table failed with error %s\n", err.GetErrorCode());

}

TOCIndex