# qdbm::Depot Class Reference

C++ API of Depot, the basic API of QDBM. More...

Inheritance diagram for qdbm::Depot:

List of all members.

## Public Member Functions

| Depot (const char *name, int omode=Depot::OREADER, int bnum=-1) throw (Depot_error)
| Get the database handle.
virtual | ~Depot () throw ()
| Release the resources.
virtual void | close () throw (Depot_error)
| Close the database handle.
virtual bool | put (const char *kbuf, int ksiz, const char *vbuf, int vsiz, int dmode=Depot::DOVER) throw (Depot_error)
| Store a record.
virtual bool | out (const char *kbuf, int ksiz) throw (Depot_error)
| Delete a record.
virtual char * | get (const char *kbuf, int ksiz, int start=0, int max=-1, int *sp=0) throw (Depot_error)
| Retrieve a record.
virtual int | getwb (const char *kbuf, int ksiz, int start, int max, char *vbuf) throw (Depot_error)
| Retrieve a record and write the value into a buffer.
virtual int | vsiz (const char *kbuf, int ksiz) throw (Depot_error)
| Get the size of the value of a record.
virtual void | iterinit () throw (Depot_error)
| Initialize the iterator of the database handle.
virtual char * | iternext (int *sp=0) throw (Depot_error)
| Get the next key of the iterator.
virtual void | setalign (int align) throw (Depot_error)
| Set alignment of the database handle.
virtual void | setfbpsiz (int size) throw (Depot_error)
| Set the size of the free block pool.
virtual void | sync () throw (Depot_error)
| Synchronize updating contents with the file and the device.
virtual void | optimize (int bnum=-1) throw (Depot_error)
| Optimize the database.
virtual char * | name () throw (Depot_error)
| Get the name of the database.
virtual int | fsiz () throw (Depot_error)
| Get the size of the database file.
virtual int | bnum () throw (Depot_error)
| Get the number of the elements of the bucket array.
virtual int | busenum () throw (Depot_error)
| Get the number of the used elements of the bucket array.
virtual int | rnum () throw (Depot_error)
| Get the number of the records stored in the database.
virtual bool | writable () throw (Depot_error)
| Check whether the database handle is a writer or not.
virtual bool | fatalerror () throw (Depot_error)
| Check whether the database has a fatal error or not.
virtual int | inode () throw (Depot_error)
| Get the inode number of the database file.
virtual time_t | mtime () throw (Depot_error)
| Get the last modified time of the database.
virtual int | fdesc () throw (Depot_error)
| Get the file descriptor of the database file.
virtual void | storerec (const Datum &key, const Datum &val, bool replace=true) throw (Depot_error)
| Store a record.
virtual void | deleterec (const Datum &key) throw (Depot_error)
| Delete a record.
virtual Datum | fetchrec (const Datum &key) throw (Depot_error)
| Fetch a record.
virtual Datum | firstkey () throw (Depot_error)
| Get the first key.
virtual Datum | nextkey () throw (Depot_error)
| Get the next key.
virtual bool | error () throw (Depot_error)
| Check whether a fatal error occured or not.

## Static Public Member Functions

static const char * | version () throw ()
| Get the version information.
static void | remove (const char *name) throw (Depot_error)
| Remove a database file.
static char * | snaffle (const char *name, const char *kbuf, int ksiz, int *sp) throw (Depot_error)
| Retrieve a record directly from a database file.

## Public Attributes

bool | silent
| whether to repress frequent exceptions

## Static Public Attributes

static const int | ENOERR
| error code: no error
static const int | EFATAL
| error code: with fatal error
static const int | EMODE
| error code: invalid mode
static const int | EBROKEN
| error code: broken database file
static const int | EKEEP
| error code: existing record
static const int | ENOITEM
| error code: no item found
static const int | EALLOC
| error code: memory allocation error
static const int | EMAP
| error code: memory mapping error
static const int | EOPEN
| error code: open error
static const int | ECLOSE
| error code: close error
static const int | ETRUNC
| error code: trunc error
static const int | ESYNC
| error code: sync error
static const int | ESTAT
| error code: stat error
static const int | ESEEK
| error code: seek error
static const int | EREAD
| error code: read error
static const int | EWRITE
| error code: write error
static const int | ELOCK
| error code: lock error
static const int | EUNLINK
| error code: unlink error
static const int | EMKDIR
| error code: mkdir error
static const int | ERMDIR
| error code: rmdir error
static const int | EMISC
| error code: miscellaneous error
static const int | OREADER
| open mode: open as a reader
static const int | OWRITER
| open mode: open as a writer
static const int | OCREAT
| open mode: writer creating
static const int | OTRUNC
| open mode: writer truncating
static const int | ONOLCK
| open mode: open without locking
static const int | OLCKNB
| open mode: lock without blocking
static const int | OSPARSE
| open mode: create as a sparse file
static const int | DOVER
| write mode: overwrite the existing value
static const int | DKEEP
| write mode: keep the existing value
static const int | DCAT
| write mode: concatenate values

## Detailed Description

C++ API of Depot, the basic API of QDBM.

## Constructor & Destructor Documentation

qdbm::Depot::Depot | ( | const char * | name,
|  | int | omode = `Depot::OREADER`,
|  | int | bnum = `-1` |
| ) |  |  | throw (Depot_error)

Get the database handle.

Parameters:

  | name | the name of a database file.

  omode

  the connection mode: `DepotOWRITER' as a writer, `DepotOREADER' as a reader. If the mode is `DepotOWRITER', the following may be added by bitwise or: `DepotOCREAT', which means it creates a new database if not exist, `DepotOTRUNC', which means it creates a new database regardless if one exists. Both of `DepotOREADER' and `DepotOWRITER' can be added to by bitwise or: `DepotONOLCK', which means it opens a database file without file locking, or `DepotOLCKNB', which means locking is performed without blocking. `DepotOCREAT' can be added to by bitwise or: `DepotOSPARSE', which means it creates a database file as a sparse file.

  | bnum | the number of elements of the bucket array. If it is not more than 0, the default value is specified. The size of a bucket array is determined on creating, and can not be changed except for by optimization of the database. Suggested size of a bucket array is about from 0.5 to 4 times of the number of all records to store.

Exceptions:

  | Depot_error | if an error occurs.

Note:
  While connecting as a writer, an exclusive lock is invoked to the database file. While connecting as a reader, a shared lock is invoked to the database file. The thread blocks until the lock is achieved. If `DepotONOLCK' is used, the application is responsible for exclusion control.

virtual qdbm::Depot::~Depot | ( |  | ) | throw ()`[virtual]`

Release the resources.

Note:
  If the database handle is not closed yet, it is closed.

## Member Function Documentation

virtual int qdbm::Depot::bnum | ( |  | ) | throw (Depot_error)`[virtual]`

Get the number of the elements of the bucket array.

Returns:
  the number of the elements of the bucket array.

Exceptions:

  | Depot_error | if an error occurs.

virtual int qdbm::Depot::busenum | ( |  | ) | throw (Depot_error)`[virtual]`

Get the number of the used elements of the bucket array.

Returns:
  the number of the used elements of the bucket array.

Exceptions:

  | Depot_error | if an error occurs.

Note:
  This function is inefficient because it accesses all elements of the bucket array.

virtual void qdbm::Depot::close | ( |  | ) | throw (Depot_error)`[virtual]`

Close the database handle.

Exceptions:

  | Depot_error | if an error occurs.

Note:
  Updating a database is assured to be written when the handle is closed. If a writer opens a database but does not close it appropriately, the database will be broken.

Implements qdbm::ADBM.

virtual void qdbm::Depot::deleterec | ( | const Datum & | key | ) | throw (Depot_error)`[virtual]`

Delete a record.

Parameters:

  | key | reference to a key object.

Exceptions:

  | Depot_error | if an error occurs or no record corresponds.

Implements qdbm::ADBM.

virtual bool qdbm::Depot::error | ( |  | ) | throw (Depot_error)`[virtual]`

Check whether a fatal error occured or not.

Returns:
  true if the database has a fatal error, false if not.

Exceptions:

  | Depot_error | if an error occurs.

Implements qdbm::ADBM.

virtual bool qdbm::Depot::fatalerror | ( |  | ) | throw (Depot_error)`[virtual]`

Check whether the database has a fatal error or not.

Returns:
  true if the database has a fatal error, false if not.

Exceptions:

  | Depot_error | if an error occurs.

virtual int qdbm::Depot::fdesc | ( |  | ) | throw (Depot_error)`[virtual]`

Get the file descriptor of the database file.

Returns:
  the file descriptor of the database file.

Exceptions:

  | Depot_error | if an error occurs.

Note:
  Handling the file descriptor of a database file directly is not suggested.

virtual Datum qdbm::Depot::fetchrec | ( | const Datum & | key | ) | throw (Depot_error)`[virtual]`

Fetch a record.

Parameters:

  | key | reference to a key object.

Returns:
  a temporary instance of the value of the corresponding record.

Exceptions:

  | Depot_error | if an error occurs or no record corresponds.

Implements qdbm::ADBM.

virtual Datum qdbm::Depot::firstkey | ( |  | ) | throw (Depot_error)`[virtual]`

Get the first key.

Returns:
  a temporary instance of the key of the first record.

Exceptions:

  | Depot_error | if an error occurs or no record corresponds.

Implements qdbm::ADBM.

virtual int qdbm::Depot::fsiz | ( |  | ) | throw (Depot_error)`[virtual]`

Get the size of the database file.

Returns:
  the size of the database file.

Exceptions:

  | Depot_error | if an error occurs.

virtual char* qdbm::Depot::get | ( | const char * | kbuf,
|  | int | ksiz,
|  | int | start = `0`,
|  | int | max = `-1`,
|  | int * | sp = `0` |
| ) |  |  | throw (Depot_error)`[virtual]`

Retrieve a record.

Parameters:

  | kbuf | the pointer to the region of a key.
  | ksiz | the size of the region of the key. If it is negative, the size is assigned with `stdstrlen(kbuf)'.
  | start | the offset address of the beginning of the region of the value to be read.
  | max | the max size to read with. If it is negative, the size is unlimited.
  | sp | the pointer to a variable to which the size of the region of the return value is assigned. If it is 0, it is not used.

Returns:
  the pointer to the region of the value of the corresponding record. If the silent flag is true and no record corresponds, 0 is returned instead of exception.

Exceptions:

  | Depot_error | if an error occurs, no record corresponds, or the size of the value of the corresponding record is less than `start'.

Note:
  Because an additional zero code is appended at the end of the region of the return value, the return value can be treated as a character string. Because the region of the return value is allocated with the `stdmalloc' call, it should be released with the `stdfree' call if it is no longer in use.

virtual int qdbm::Depot::getwb | ( | const char * | kbuf,
|  | int | ksiz,
|  | int | start,
|  | int | max,
|  | char * | vbuf |
| ) |  |  | throw (Depot_error)`[virtual]`

Retrieve a record and write the value into a buffer.

Parameters:

  | kbuf | the pointer to the region of a key.
  | ksiz | the size of the region of the key. If it is negative, the size is assigned with `stdstrlen(kbuf)'.
  | start | the offset address of the beginning of the region of the value to be read.
  | max | the max size to be read. It shuld be equal to or less than the size of the writing buffer.
  | vbuf | the pointer to a buffer into which the value of the corresponding record is written.

Returns:
  the return value is the size of the written data. If the silent flag is true and no record corresponds, -1 is returned instead of exception.

Exceptions:

  | Depot_error | if an error occurs, no record corresponds, or the size of the value of the corresponding record is less than `start'.

Note:
  No additional zero code is appended at the end of the region of the writing buffer.

virtual int qdbm::Depot::inode | ( |  | ) | throw (Depot_error)`[virtual]`

Get the inode number of the database file.

Returns:
  the inode number of the database file.

Exceptions:

  | Depot_error | if an error occurs.

virtual void qdbm::Depot::iterinit | ( |  | ) | throw (Depot_error)`[virtual]`

Initialize the iterator of the database handle.

Exceptions:

  | Depot_error | if an error occurs.

Note:
  The iterator is used in order to access the key of every record stored in a database.

virtual char* qdbm::Depot::iternext | ( | int * | sp = `0` | ) | throw (Depot_error)`[virtual]`

Get the next key of the iterator.

Parameters:

  | sp | the pointer to a variable to which the size of the region of the return value is assigned. If it is 0, it is not used.

Returns:
  the pointer to the region of the next key. If the silent flag is true and no record corresponds, 0 is returned instead of exception.

Exceptions:

  | Depot_error | if an error occurs or no record is to be get out of the iterator.

Note:
  Because an additional zero code is appended at the end of the region of the return value, the return value can be treated as a character string. Because the region of the return value is allocated with the `stdmalloc' call, it should be released with the `stdfree' call if it is no longer in use. It is possible to access every record by iteration of calling this function. However, it is not assured if updating the database is occurred while the iteration. Besides, the order of this traversal access method is arbitrary, so it is not assured that the order of storing matches the one of the traversal access.

virtual time_t qdbm::Depot::mtime | ( |  | ) | throw (Depot_error)`[virtual]`

Get the last modified time of the database.

Returns:
  the last modified time the database.

Exceptions:

  | Depot_error | if an error occurs.

virtual char* qdbm::Depot::name | ( |  | ) | throw (Depot_error)`[virtual]`

Get the name of the database.

Returns:
  the pointer to the region of the name of the database.

Exceptions:

  | Depot_error | if an error occurs.

Note:
  Because the region of the return value is allocated with the `stdmalloc' call, it should be released with the `stdfree' call if it is no longer in use.

virtual Datum qdbm::Depot::nextkey | ( |  | ) | throw (Depot_error)`[virtual]`

Get the next key.

Returns:
  a temporary instance of the key of the next record.

Exceptions:

  | Depot_error | if an error occurs or no record corresponds.

Implements qdbm::ADBM.

virtual void qdbm::Depot::optimize | ( | int | bnum = `-1` | ) | throw (Depot_error)`[virtual]`

Optimize the database.

Parameters:

  | bnum | the number of the elements of the bucket array. If it is not more than 0, the default value is specified.

Exceptions:

  | Depot_error | if an error occurs.

Note:
  In an alternating succession of deleting and storing with overwrite or concatenate, dispensable regions accumulate. This function is useful to do away with them.

virtual bool qdbm::Depot::out | ( | const char * | kbuf,
|  | int | ksiz |
| ) |  |  | throw (Depot_error)`[virtual]`

Delete a record.

Parameters:

  | kbuf | the pointer to the region of a key.
  | ksiz | the size of the region of the key. If it is negative, the size is assigned with `stdstrlen(kbuf)'.

Returns:
  always true. However, if the silent flag is true and no record corresponds, false is returned instead of exception.

Exceptions:

  | Depot_error | if an error occurs or no record corresponds.

virtual bool qdbm::Depot::put | ( | const char * | kbuf,
|  | int | ksiz,
|  | const char * | vbuf,
|  | int | vsiz,
|  | int | dmode = `Depot::DOVER` |
| ) |  |  | throw (Depot_error)`[virtual]`

Store a record.

Parameters:

  | kbuf | the pointer to the region of a key.
  | ksiz | the size of the region of the key. If it is negative, the size is assigned with `stdstrlen(kbuf)'.
  | vbuf | the pointer to the region of a value.
  | vsiz | the size of the region of the value. If it is negative, the size is assigned with `stdstrlen(vbuf)'.
  | dmode | behavior when the key overlaps, by the following values: `DepotDOVER', which means the specified value overwrites the existing one, `DepotDKEEP', which means the existing value is kept, `DepotDCAT', which means the specified value is concatenated at the end of the existing value.

Returns:
  always true. However, if the silent flag is true and replace is cancelled, false is returned instead of exception.

Exceptions:

  | Depot_error | if an error occurs or replace is cancelled.

static void qdbm::Depot::remove | ( | const char * | name | ) | throw (Depot_error)`[static]`

Remove a database file.

Parameters:

  | name | the name of a database file.

Exceptions:

  | Depot_error | if an error occurs.

virtual int qdbm::Depot::rnum | ( |  | ) | throw (Depot_error)`[virtual]`

Get the number of the records stored in the database.

Returns:
  the number of the records stored in the database.

Exceptions:

  | Depot_error | if an error occurs.

virtual void qdbm::Depot::setalign | ( | int | align | ) | throw (Depot_error)`[virtual]`

Set alignment of the database handle.

Parameters:

  | align | the basic size of alignment.

Exceptions:

  | Depot_error | if an error occurs.

Note:
  If alignment is set to a database, the efficiency of overwriting values is improved. The size of alignment is suggested to be average size of the values of the records to be stored. If alignment is positive, padding whose size is multiple number of the alignment is placed. If alignment is negative, as `vsiz' is the size of a value, the size of padding is calculated with `(vsiz / pow(2, abs(align) - 1))'. Because alignment setting is not saved in a database, you should specify alignment every opening a database.

virtual void qdbm::Depot::setfbpsiz | ( | int | size | ) | throw (Depot_error)`[virtual]`

Set the size of the free block pool.

Parameters:

  | size | the size of the free block pool of a database.

Exceptions:

  | Depot_error | if an error occurs.

Note:
  The default size of the free block pool is 16. If the size is greater, the space efficiency of overwriting values is improved with the time efficiency sacrificed.

static char* qdbm::Depot::snaffle | ( | const char * | name,
|  | const char * | kbuf,
|  | int | ksiz,
|  | int * | sp |
| ) |  |  | throw (Depot_error)`[static]`

Retrieve a record directly from a database file.

Parameters:

  | name | the name of a database file.
  | kbuf | the pointer to the region of a key.
  | ksiz | the size of the region of the key. If it is negative, the size is assigned with `stdstrlen(kbuf)'.
  | sp | the pointer to a variable to which the size of the region of the return value is assigned. If it is 0, it is not used.

Returns:
  the pointer to the region of the value of the corresponding record.

Exceptions:

  | Depot_error | if an error occurs or no record corresponds.

Note:
  Because an additional zero code is appended at the end of the region of the return value, the return value can be treated as a character string. Because the region of the return value is allocated with the `stdmalloc' call, it should be released with the `stdfree' call if it is no longer in use. Although this method can be used even while the database file is locked by another process, it is not assured that recent updated is reflected.

virtual void qdbm::Depot::storerec | ( | const Datum & | key,
|  | const Datum & | val,
|  | bool | replace = `true` |
| ) |  |  | throw (Depot_error)`[virtual]`

Store a record.

Parameters:

  | key | reference to a key object.
  | val | reference to a value object.
  | replace | whether the existing value is to be overwritten or not.

Exceptions:

  | Depot_error | if an error occurs or replace is cancelled.

Implements qdbm::ADBM.

virtual void qdbm::Depot::sync | ( |  | ) | throw (Depot_error)`[virtual]`

Synchronize updating contents with the file and the device.

Exceptions:

  | Depot_error | if an error occurs.

Note:
  This function is useful when another process uses the connected database file.

static const char* qdbm::Depot::version | ( |  | ) | throw ()`[static]`

Get the version information.

Returns:
  the string of the version information.

virtual int qdbm::Depot::vsiz | ( | const char * | kbuf,
|  | int | ksiz |
| ) |  |  | throw (Depot_error)`[virtual]`

Get the size of the value of a record.

Parameters:

  | kbuf | the pointer to the region of a key.
  | ksiz | the size of the region of the key. If it is negative, the size is assigned with `stdstrlen(kbuf)'.

Returns:
  the size of the value of the corresponding record. If the silent flag is true and no record corresponds, -1 is returned instead of exception.

Exceptions:

  | Depot_error | if an error occurs or no record corresponds.

Note:
  Because this function does not read the entity of a record, it is faster than `get'.

virtual bool qdbm::Depot::writable | ( |  | ) | throw (Depot_error)`[virtual]`

Check whether the database handle is a writer or not.

Returns:
  true if the handle is a writer, false if not.

Exceptions:

  | Depot_error | if an error occurs.

The documentation for this class was generated from the following file:

- xdepot.h