QDBM provides API for C++. This encapsulates the basic API and the extended API of QDBM, and make them thread-safe.
The basic API for C++ realizes a database with a file. Constructors of the class `Depot' open a database file. The member function `close' is used in order to close the database. If the instance is destroyed without closing the database explicitly, the destructor closes the database. The member function `put' is used in order to store a record. The member function `out' is used in order to delete a record. The member function `get' is used in order to retrieve a record. Besides, most operations like ones of the basic API for C is available. Each member functions throws an instance of the class `Depot_error' if an error occures.
The extended API for C++ realizes a database with a directory and multiple files. Constructors of the class `Curia' open a database directory. The member function `close' is used in order to close the database. If the instance is destroyed without closing the database explicitly, the destructor closes the database. The member function `put' is used in order to store a record. The member function `out' is used in order to delete a record. The member function `get' is used in order to retrieve a record. Operations for managing large objects are also privided. Besides, most operations like ones of the extended API for C is available. Each member functions throws an instance of the class `Curia_error' if an error occures.
Both of the class `Depot' and the class `Curia' are derived classes of the class `ADBM'. This class is an interface for abstraction of database managers compatible with DBM of UNIX standard. This class does only declare pure virtual functions. Each member functions throws an instance of the class `DBM_error'. In this framework, a key and a value of each record are expressed by the class `Datum'. An instance of `Datum' has the pointer to the region of data and its size. When you choose the one of three API, `Depot' is suggested if performance is weighted, `Curia' is suggested if scalability is weighted, , `ADBM' is suggested if elegance and maintenance are weighted.
For more information about the APIs, read documents in the sub directory `xapidoc' and each header file.
Make sure that GCC 3.2 or later version is installed. and Make sure that QDBM is installed under `/usr/local'.
Change the current working directory to the subdirectory named `plus'.
cd plus
Run the configuration script.
./configure
Build programs.
make
Perform self-diagnostic test.
make check
Install programs This operation must be carried out by the root user.
make install
When a series of work finishes, header files, `xdepot.h', `xcuria.h' and `xadbm.h' will be installed in `/usr/local/include', a library `libxqdbm.a' will be installed in `/usr/local/lib', executable commands `xdptest' and `xcrtest' will be installed in `/usr/local/bin'.
On Windows (Cygwin), you should follow the procedures below for installation.
Run the configuration script.
./configure
Build programs.
make win
Perform self-diagnostic test.
make check
Install programs. As well, perform `make uninstall-win' to uninstall them.
make install-win
On Windows, an import library `libxqdbm.dll.a' is created, and a dynamic linking library `jqdbm.dll' is created. `xqdbm.dll' is installed into such a system directory as `C:\WINNT\SYSTEM32'.
The following example stores and retrieves a phone number, using the name as the key.
#include <xdepot.h> #include <cstdlib> #include <iostream> using namespace std; using namespace qdbm; const char* NAME = "mikio"; const char* NUMBER = "000-1234-5678"; const char* DBNAME = "book"; int main(int argc, char** argv){ try { // open the database Depot depot(DBNAME, Depot::OWRITER | Depot::OCREAT); // store the record depot.put(NAME, -1, NUMBER, -1); // retrieve the record char* val = depot.get(NAME, -1); cout << "Name: " << NAME << endl; cout << "Number: " << val << endl; free(val); // close the database depot.close(); } catch(Depot_error& e){ cout << e.message() << endl; return 1; } return 0; }
The following example is a transcription of the one above, using the class `ADBM'.
#include <xadbm.h> #include <xdepot.h> #include <iostream> using namespace std; using namespace qdbm; const char* NAME = "mikio"; const char* NUMBER = "000-1234-5678"; const char* DBNAME = "book"; int main(int argc, char** argv){ try { // open the database Depot depot(DBNAME, Depot::OWRITER | Depot::OCREAT); ADBM& dbm = depot; // prepare the record Datum key(NAME); Datum val(NUMBER); // store the record dbm.storerec(key, val); // retrieve the record Datum* res = dbm.fetchrec(key); cout << "Name: " << NAME << endl; cout << "Number: " << res->ptr() << endl; delete res; // close the database dbm.close(); } catch(DBM_error& e){ cout << e.message() << endl; return 1; } return 0; }
For building a program using C++ API of QDBM, the program should be linked with the libraries, `xqdbm' and `qdbm'. Besides, `g++' needs an option `-fexceptions' to handle exceptions. For example, the following command is executed to build `sample' from `sample.cc'.
g++ -I/usr/local/include -fexceptions -o sample sample.cc -L/usr/local/lib -lxqdbm -lqdbm
Depot and Curia have restrictions that two or more handles of the same database file should not be used by a process at the same time. So, when a database is used by two or more threads, open the database in the main thread and pass the handle to each thread.
This project does not create any library of shared objects, because the current version (3.2.2) of GCC cannot handle exceptions generated by codes in shared objects.
Because QDBM does not provite mechanism that serialize generic objects and store them into a database, if you want it, you should extend the class `ADBM'.