Author: Mikio Hirabayashi
Description: specifications of C++ API of QDBM
Keywords: QDBM, C++, Depot, Curia, Villa

# Specifications of QDBM for C++

Copyright (C) 2000-2006 Mikio Hirabayashi
Last Update: Thu, 26 Oct 2006 15:00:20 +0900
[API] [Japanese]

## 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 occurs.

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 provided. 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 occurs.

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

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

While APIs for C are thread-safe unless plural threads do not share a database handle, APIs for C++ are thread-safe even if plural threads share a handle. This API is implemented depending on the POSIX thread package. If you use another thread package, you should write your own C++ wrapper.

When `put' overwriting an existing record is cancelled or `get' retrieving a missing record, failure of the operation is noticed by exception. If you dislike such behavior, set the `silent' flag to be true. Then, failure of the operation is noticed by the return value.

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

## Installation

### Preparation

Make sure that GCC 3.3 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.so' and so on 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 `/usr/local/bin'.

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 city", -1, Villa::DDUP);

        try {

          // set the cursor at the top of candidates
          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'. For example, the following command is executed to build `sample' from `sample.cc'.

    g++ -I/usr/local/include -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.

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.