DEPOT(3)

okay bestie, sit DOWN ๐Ÿ’– because we're about to talk about DEPOT(3) โ€” the manual page for Quick Database Manager's main character, Depot. This is part 1 of 3, so hydrate, stretch, and let's get into it. ๐Ÿซถ

๐ŸŒธ NAME ๐ŸŒธ

Depot โ€” the basic API of QDBM. that's it, that's the name, she's simple and she slays. โœจ


๐Ÿ’… SYNOPSIS ๐Ÿ’…

here's the whole cast list, no cuts, no edits:

#include <depot.h>
#include <stdlib.h>

extern const char *dpversion;

extern int dpecode;

const char *dperrmsg(int ecode);

DEPOT *dpopen(const char *name, int omode, int bnum);

int dpclose(DEPOT *depot);

int dpput(DEPOT *depot, const char *kbuf, int ksiz, const char *vbuf, int vsiz, int dmode);

int dpout(DEPOT *depot, const char *kbuf, int ksiz);

char *dpget(DEPOT *depot, const char *kbuf, int ksiz, int start, int max, int *sp);

int dpgetwb(DEPOT *depot, const char *kbuf, int ksiz, int start, int max, char *vbuf);

int dpvsiz(DEPOT *depot, const char *kbuf, int ksiz);

int dpiterinit(DEPOT *depot);

char *dpiternext(DEPOT *depot, int *sp);

int dpsetalign(DEPOT *depot, int align);

int dpsetfbpsiz(DEPOT *depot, int size);

int dpsync(DEPOT *depot);

int dpoptimize(DEPOT *depot, int bnum);

char *dpname(DEPOT *depot);

int dpfsiz(DEPOT *depot);

int dpbnum(DEPOT *depot);

int dpbusenum(DEPOT *depot);

int dprnum(DEPOT *depot);

int dpwritable(DEPOT *depot);

int dpfatalerror(DEPOT *depot);

int dpinode(DEPOT *depot);

time_t dpmtime(DEPOT *depot);

int dpfdesc(DEPOT *depot);

int dpremove(const char *name);

int dprepair(const char *name);

int dpexportdb(DEPOT *depot, const char *name);

int dpimportdb(DEPOT *depot, const char *name);

char *dpsnaffle(const char *name, const char *kbuf, int ksiz, int *sp);

int dpinnerhash(const char *kbuf, int ksiz);

int dpouterhash(const char *kbuf, int ksiz);

int dpprimenum(int num);

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

Depot is the basic API of QDBM. like, the moment. almost every feature you'd want for managing a database that QDBM offers is implemented right here by Depot. every other API is literally just a wrapper around Depot, no cap. and out of all the QDBM APIs, Depot is the fastest. fastest in all of them. she's not competing, she's winning. ๐Ÿ‘‘

to use Depot you gotta include depot.h and stdlib.h in your source files. bestie, the usual move is to drop this near the top of the file:

#include <depot.h>
#include <stdlib.h>

so, a pointer to DEPOT is what you use as your database handle. it's giving very much the same energy as how some file I/O routines in stdio.h use a pointer to FILE. you open a handle with dpopen and you close it with dpclose. ๐ŸŽ€

๐Ÿšจ and listen: you should not refer directly to any member of the handle. like, don't go poking around in her business. and if a fatal error happens in a database, any access method through that handle except dpclose will not work and will return error status. it's over at that point, fr fr.

a process is allowed to use multiple database handles at the same time, but here's the catch: handles of the same database file should not be used. ๐Ÿ™ˆ


โญ the external variables โญ

the external variable dpversion is the string holding the version info:

extern const char *dpversion;

the external variable dpecode gets assigned the error code of the last error that happened. peek at depot.h for the deets on all the error codes:

extern int dpecode;
  • the initial value of this variable is DP_NOERR.
  • the other values are: DP_EFATAL, DP_EMODE, DP_EBROKEN, DP_EKEEP, DP_ENOITEM, DP_EALLOC, DP_EMAP, DP_EOPEN, DP_ECLOSE, DP_ETRUNC, DP_ESYNC, DP_ESTAT, DP_ESEEK, DP_EREAD, DP_EWRITE, DP_ELOCK, DP_EUNLINK, DP_EMKDIR, DP_ERMDIR, and DP_EMISC.

๐Ÿ’œ dperrmsg

the function dperrmsg is how you get a message string that corresponds to an error code. (iykyk, error codes alone are not the vibe.)

const char *dperrmsg(int ecode);
  • ecode specifies an error code.
  • the return value is the message string of that error code.
  • the region of the return value is not writable. hands off. ๐Ÿ™…โ€โ™€๏ธ

๐Ÿ”ฅ dpopen

the function dpopen is how you get yourself a database handle. the origin story. ๐ŸŒฑ

DEPOT *dpopen(const char *name, int omode, int bnum);
  • name specifies the name of a database file.
  • omode specifies the connection mode: DP_OWRITER as a writer, DP_OREADER as a reader.
  • if the mode is DP_OWRITER, the following may be added by bitwise or:
    • DP_OCREAT, which means it creates a new database if not exist.
    • DP_OTRUNC, which means it creates a new database regardless if one exists.
  • both DP_OREADER and DP_OWRITER can be added to by bitwise or:
    • DP_ONOLCK, which means it opens a database file without file locking.
    • DP_OLCKNB, which means locking is performed without blocking.
  • DP_OCREAT can be added to by bitwise or:
    • DP_OSPARSE, which means it creates a database file as a sparse file.
  • bnum specifies 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 it 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 the number of all records to store.
  • the return value is the database handle, or NULL if it's not successful. ๐Ÿ˜ญ

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. and if DP_ONOLCK is used, the application is responsible for exclusion control โ€” that's on you, bestie, don't come crying to Depot. ๐Ÿ’€


๐ŸŽ€ dpclose

the function dpclose is how you close a database handle. the goodbye scene. ๐Ÿฅบ

int dpclose(DEPOT *depot);
  • depot specifies a database handle.
  • if successful, the return value is true, else, it's false.
  • because the region of a closed handle is released, it becomes impossible to use the handle. she's gone. ๐Ÿฆ‹
  • updating a database is assured to be written when the handle is closed.
๐Ÿšจ important one: if a writer opens a database but does not close it appropriately, the database will be broken. like actually broken. do not do this.

โœจ dpput

the function dpput is how you store a record.

int dpput(DEPOT *depot, const char *kbuf, int ksiz, const char *vbuf, int vsiz, int dmode);
  • depot specifies a database handle connected as a writer.
  • kbuf specifies the pointer to the region of a key.
  • ksiz specifies the size of the region of the key. if it is negative, the size is assigned with strlen(kbuf).
  • vbuf specifies the pointer to the region of a value.
  • vsiz specifies the size of the region of the value. if it is negative, the size is assigned with strlen(vbuf).
  • dmode specifies behavior when the key overlaps, by the following values:
    • DP_DOVER, which means the specified value overwrites the existing one.
    • DP_DKEEP, which means the existing value is kept.
    • DP_DCAT, which means the specified value is concatenated at the end of the existing value.
  • if successful, the return value is true, else, it's false.

๐ŸŒธ dpout

the function dpout is how you delete a record. rip to that record. ๐Ÿชฆ

int dpout(DEPOT *depot, const char *kbuf, int ksiz);
  • depot specifies a database handle connected as a writer.
  • kbuf specifies the pointer to the region of a key.
  • ksiz specifies the size of the region of the key. if it is negative, the size is assigned with strlen(kbuf).
  • if successful, the return value is true, else, it's false.
  • false is returned when no record corresponds to the specified key. so like, nothing to delete, nothing to celebrate. ๐Ÿคทโ€โ™€๏ธ

๐Ÿ’– dpget

the function dpget is how you retrieve a record.

char *dpget(DEPOT *depot, const char *kbuf, int ksiz, int start, int max, int *sp);
  • depot specifies a database handle.
  • kbuf specifies the pointer to the region of a key.
  • ksiz specifies the size of the region of the key. if it is negative, the size is assigned with strlen(kbuf).
  • start specifies the offset address of the beginning of the region of the value to be read.
  • max specifies the max size to be read. if it is negative, the size to read is unlimited.
  • sp specifies the pointer to a variable to which the size of the region of the return value is assigned. if it is NULL, it is not used.
  • if successful, the return value is the pointer to the region of the value of the corresponding record, else, it's NULL.
  • NULL is returned when no record corresponds to the specified key, or the size of the value of the corresponding record is less than start.
  • 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 malloc call, it should be released with the free call if it is no longer in use. free your memory bestie, it's self care. ๐Ÿ’…

๐Ÿ“ dpgetwb

the function dpgetwb is how you retrieve a record and write the value into a buffer.

int dpgetwb(DEPOT *depot, const char *kbuf, int ksiz, int start, int max, char *vbuf);
  • depot specifies a database handle.
  • kbuf specifies the pointer to the region of a key.
  • ksiz specifies the size of the region of the key. if it is negative, the size is assigned with strlen(kbuf).
  • start specifies the offset address of the beginning of the region of the value to be read.
  • max specifies the max size to be read. it should be equal to or less than the size of the writing buffer. ๐Ÿ“
  • vbuf specifies the pointer to a buffer into which the value of the corresponding record is written.
  • if successful, the return value is the size of the written data, else, it's -1.
  • -1 is returned when no record corresponds to the specified key, or the size of the value of the corresponding record is less than start.
  • heads up: no additional zero code is appended at the end of the region of the writing buffer. not like dpget's little bonus. so don't assume a string terminator, okay? ๐Ÿ™ˆ

๐Ÿ’œ dpvsiz

the function dpvsiz is how you get the size of the value of a record.

int dpvsiz(DEPOT *depot, const char *kbuf, int ksiz);
  • depot specifies a database handle.
  • kbuf specifies the pointer to the region of a key.
  • ksiz specifies the size of the region of the key. if it is negative, the size is assigned with strlen(kbuf).
  • if successful, the return value is the size of the value of the corresponding record, else, it's -1.
  • and the serve? because this function does not read the entity of a record, it's faster than dpget. ate. ๐Ÿ”ฅ

๐ŸŒˆ dpiterinit

the function dpiterinit is how you initialize the iterator of a database handle.

int dpiterinit(DEPOT *depot);
  • depot specifies a database handle.
  • if successful, the return value is true, else, it's false.
  • the iterator is used in order to access the key of every record stored in a database. like a little tour guide through your whole database. ๐ŸŽŸ๏ธ

โญ dpiternext

the function dpiternext is used in order to get the next key of the iterator.

char *dpiternext(DEPOT *depot, int *sp);

depot is ur database handle, obvi ๐Ÿ’…. sp is the pointer to a variable where the size of the region of the return value gets dropped off โ€” and if it's NULL, bestie, we just don't use it, no biggie. If it works out, the return value is the pointer to the region of the next key, otherwise it's NULL. And NULL shows up when there's no record left to get out of the iterator, so likeโ€ฆ that's the end, girlie ๐ŸŒธ.

Because an extra zero code gets appended at the end of the region of the return value, you can treat the return value as a character string โœจ. And because that region is allocated with the malloc call, you should release it with the free call once it's no longer in use โ€” don't leave it hanging, that's not the vibe ๐Ÿซถ.

You can access every record by iterating this function, fr fr. 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. It's giving "random order, deal with it" ๐Ÿ’€.


The function dpsetalign is used in order to set alignment of a database handle.

int dpsetalign(DEPOT *depot, int align);

depot specifies a database handle connected as a writer. align specifies the size of alignment. If successful, the return value is true, else, it is false. 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, iykyk.

  • 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. No cap, it just forgets ๐Ÿ˜ญ.

The function dpsetfbpsiz is used in order to set the size of the free block pool of a database handle.

int dpsetfbpsiz(DEPOT *depot, int size);

depot specifies a database handle connected as a writer. size specifies the size of the free block pool of a database. If successful, the return value is true, else, it is false. The default size of the free block pool is 16. If the size is greater, the space efficiency of overwriting values is improved โ€” but with the time efficiency sacrificed โš–๏ธ. Trade-offs, bestie, it's giving balance.


The function dpsync is used in order to synchronize updating contents with the file and the device.

int dpsync(DEPOT *depot);

depot specifies a database handle connected as a writer. If successful, the return value is true, else, it is false. This function is useful when another process uses the connected database file ๐Ÿ‘‘.


The function dpoptimize is used in order to optimize a database.

int dpoptimize(DEPOT *depot, int bnum);

depot specifies a database handle connected as a writer. bnum specifies the number of the elements of the bucket array. If it is not more than 0, the default value is specified. If successful, the return value is true, else, it is false.

In an alternating succession of deleting and storing with overwrite or concatenate, dispensable regions accumulate ๐Ÿ—‘๏ธ. This function is useful to do away with them โ€” it's the cleanup era, periodt โœจ.


The function dpname is used in order to get the name of a database.

char *dpname(DEPOT *depot);

depot specifies a database handle. If successful, the return value is the pointer to the region of the name of the database, else, it is NULL. Because the region of the return value is allocated with the malloc call, it should be released with the free call if it is no longer in use ๐Ÿซถ.


The function dpfsiz is used in order to get the size of a database file.

int dpfsiz(DEPOT *depot);

depot specifies a database handle. If successful, the return value is the size of the database file, else, it is -1.


The function dpbnum is used in order to get the number of the elements of the bucket array.

int dpbnum(DEPOT *depot);

depot specifies a database handle. If successful, the return value is the number of the elements of the bucket array, else, it is -1.


The function dpbusenum is used in order to get the number of the used elements of the bucket array.

int dpbusenum(DEPOT *depot);

depot specifies a database handle. If successful, the return value is the number of the used elements of the bucket array, else, it is -1. Heads up bestie, this function is inefficient because it accesses all elements of the bucket array ๐Ÿ˜ฎโ€๐Ÿ’จ โ€” it's doing the most, lowkey.


The function dprnum is used in order to get the number of the records stored in a database.

int dprnum(DEPOT *depot);

depot specifies a database handle. If successful, the return value is the number of the records stored in the database, else, it is -1.


The function dpwritable is used in order to check whether a database handle is a writer or not.

int dpwritable(DEPOT *depot);

depot specifies a database handle. The return value is true if the handle is a writer, false if not.


The function dpfatalerror is used in order to check whether a database has a fatal error or not.

int dpfatalerror(DEPOT *depot);

depot specifies a database handle. The return value is true if the database has a fatal error, false if not ๐Ÿ’€.


The function dpinode is used in order to get the inode number of a database file.

int dpinode(DEPOT *depot);

depot specifies a database handle. The return value is the inode number of the database file.


The function dpmtime is used in order to get the last modified time of a database.

time_t dpmtime(DEPOT *depot);

depot specifies a database handle. The return value is the last modified time of the database โฐ.


The function dpfdesc is used in order to get the file descriptor of a database file.

int dpfdesc(DEPOT *depot);

depot specifies a database handle. The return value is the file descriptor of the database file.

Handling the file descriptor of a database file directly is not suggested ๐Ÿ™ˆ. Like, just don't, bestie.

The function dpremove is used in order to remove a database file.

int dpremove(const char *name);

name specifies the name of a database file. If successful, the return value is true, else, it is false.


The function dprepair is used in order to repair a broken database file.

int dprepair(const char *name);

name specifies the name of a database file. If successful, the return value is true, else, it is false.

There is no guarantee that all records in a repaired database file correspond to the original or expected state ๐Ÿ˜ญ. It's giving "we tried our best".

The function dpexportdb is used in order to dump all records as endian independent data.

int dpexportdb(DEPOT *depot, const char *name);

depot specifies a database handle. name specifies the name of an output file. If successful, the return value is true, else, it is false.


The function dpimportdb is used in order to load all records from endian independent data.

int dpimportdb(DEPOT *depot, const char *name);

depot specifies a database handle connected as a writer. The database of the handle must be empty. name specifies the name of an input file. If successful, the return value is true, else, it is false.


The function dpsnaffle is used in order to retrieve a record directly from a database file.

char *dpsnaffle(const char *name, const char *kbuf, int ksiz, int *sp);

name specifies the name of a database file. kbuf specifies the pointer to the region of a key. ksiz specifies the size of the region of the key โ€” and if it is negative, the size is assigned with strlen(kbuf). sp specifies the pointer to a variable to which the size of the region of the return value is assigned. If it is NULL, it is not used.

If successful, the return value is the pointer to the region of the value of the corresponding record, else, it is NULL. NULL is returned when no record corresponds to the specified key ๐Ÿซ .

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 malloc call, it should be released with the free call if it is no longer in use.

Although this function can be used even while the database file is locked by another process, it is not assured that recent updated is reflected ๐Ÿ‘€.

The function dpinnerhash is a hash function used inside Depot.

int dpinnerhash(const char *kbuf, int ksiz);

kbuf specifies the pointer to the region of a key. ksiz specifies the size of the region of the key โ€” if it is negative, the size is assigned with strlen(kbuf). The return value is the hash value of 31 bits length computed from the key. This function is useful when an application calculates the state of the inside bucket array ๐Ÿ”ฅ.


The function dpouterhash is a hash function which is independent from the hash functions used inside Depot.

int dpouterhash(const char *kbuf, int ksiz);

kbuf specifies the pointer to the region of a key. ksiz specifies the size of the region of the key โ€” if it is negative, the size is assigned with strlen(kbuf). The return value is the hash value of 31 bits length computed from the key. This function is useful when an application uses its own hash algorithm outside Depot ๐ŸŽ€.


The function dpprimenum is used in order to get a natural prime number not less than a number.

int dpprimenum(int num);

num specified a natural number. The return value is a natural prime number not less than the specified number. This function is useful when an application determines the size of a bucket array of its own hash algorithm ๐Ÿ‘‘.


If QDBM was built with POSIX thread enabled, the global variable dpecode is treated as thread specific data, and functions of Depot are reentrant. In that case, they are thread-safe as long as a handle is not accessed by threads at the same time, on the assumption that errno, malloc, and so on are thread-safe ๐Ÿงš.

โœจ SEE ALSO โœจ

okay bestie, when you're done here and you still want more, the whole squad is right there waiting for you ๐Ÿ’– go bother them next:

that's the lineup, fr fr โ€” nine little pages, all of them in section 3, all of them one man command away ๐Ÿซถ


and that's a wrap on this whole thing, no cap ๐ŸŽ€ this page is DEPOT(3), straight out of the Man Page collection, dated 2004-04-22 โ€” yes it's older than most of us and it still ate ๐Ÿ’… go read something, go build something, go be the main character of your own terminal ๐ŸŒˆ๐Ÿ’œ