# Specification of QDBM for C++

Copyright (C) 2000-2003 Mikio Hirabayashi
Last Update: Thu, 20 Nov 2003 22:44:08 +0900
[API] [Japanese] [Home]

## Table of Contents

1. Overview
2. Installation
3. Examples
4. Bugs

## Overview

QDBM provides API for C++. This encapsulates the basic API, the extended API and the advanced API of QDBM, and make them thread-safe.

The basic API for C++ realizes a hash 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 are available. Each member functions throws an instance of the class `Depot_error' if an error occures.

The extended API for C++ realizes a hash 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 are available. Each member functions throws an instance of the class `Curia_error' if an error occures.

The advanced API for C++ realizes a B+ tree database with a file. Constructors of the class `Villa' 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 advanced API for C are available. Each member functions throws an instance of the class `Villa_error' if an error occures.

`Depot', `Curia', and `Villa' 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 four APIs, `Depot' is suggested if performance is weighted, `Curia' is suggested if scalability is weighted, `Villa' is suggested if ordering access is required, `ADBM' is suggested if elegance and maintenance are weighted. Besides, a database file is not compatible with each API.

You should include the following header files in source files of applications: `xdepot.h' for `Depot', `xcuria.h' for `Curia', `xvilla.h' for `Villa', and `xadbm.h' for `ADBM'. Besides, each class is packaged in the name space `qdbm'.

For more information about the APIs, read documents in the sub directory `xapidoc' and each header file.

## Installation

### Preparation

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 sub directory named `plus'.

    cd plus

### Usual Steps

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', `xvilla.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', `xcrtest', and `xvltest' will be installed in `/usr/local/bin'.

To uninstall them, execute the following command after `./configure'. This operation must be carried out by the root user.

    make uninstall

### For Windows

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-win

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'.

Build of the C++ API using MinGW is not supported.

### For Mac OS X

On Mac OS X (Darwin), you should follow the procedures below for installation.

Run the configuration script.

    ./configure

Build programs.

    make mac

Perform self-diagnostic test.

    make check-mac

Install programs. As well, perform `make uninstall-mac' to uninstall them.

    make install-mac

On Mac OS X, `libxqdbm.dylib' and so on are created instead of `libxqdbm.so' and so on.

### For HP-UX

On HP-UX, you should follow the procedures below for installation.

Run the configuration script.

    ./configure

Build programs.

    make hpux

Perform self-diagnostic test.

    make check-hpux

Install programs. As well, perform `make uninstall-hpux' to uninstall them.

    make install-hpux

On HP-UX, `libxqdbm.sl' is created instead of `libxqdbm.so' and so on.

## Examples

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){
        cerr << e << 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
        const Datum& res = dbm.fetchrec(key);
        cout << "Name: " << NAME << endl;
        cout << "Number: " << res << endl;

        // close the database
        dbm.close();

      } catch(DBM_error& e){
        cerr << e << endl;
        return 1;
      }
      return 0;
    }

The following example performs forward matching search for strings, using the class `Villa'.

    #include <xvilla.h>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>

    using namespace std;
    using namespace qdbm;

    const char* DBNAME = "words";
    const char* PREFIX = "apple";

    int main(int argc, char** argv){
      try {

        // open the database
        Villa villa(DBNAME, Villa::OWRITER | Villa::OCREAT);

        // store records
        villa.put("applet", -1, "little application", -1, Villa::DDUP);
        villa.put("aurora", -1, "polar wonderwork", -1, Villa::DDUP);
        villa.put("apple", -1, "delicious fruit", -1, Villa::DDUP);
        villa.put("amigo", -1, "good friend", -1, Villa::DDUP);
        villa.put("apple", -1, "big ciry", -1, Villa::DDUP);

        try {

          // set the cursor at the top of cantidates
          villa.curjump(PREFIX, -1);

          // scan with the cursor
          for(;;){
            char* key = villa.curkey();
            if(strstr(key, PREFIX) != key){
              free(key);
              break;
            }
            char* val = villa.curval();
            cout << key << ": " << val << endl;
            free(key);
            free(val);
            villa.curnext();
          }

        } catch(Villa_error& e){
          if(e.code() != Villa::ENOITEM) throw e;
        }

        // close the database
        villa.close();

      } catch(Villa_error& e){
        cerr << e << 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

## Bugs

QDBM has 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 provide mechanism that serialize generic objects and store them into a database, if you want it, you should extend the class `ADBM'. Besides, if the comparing function of `Villa' compares deserialized objects, the database can be used as an object-oriented database.