Provided by: lmdb-doc_0.9.10-1_all
NAME
mdb.c - memory-mapped database library
SYNOPSIS
#include <sys/types.h> #include <sys/stat.h> #include <sys/param.h> #include <sys/uio.h> #include <sys/mman.h> #include <fcntl.h> #include <assert.h> #include <errno.h> #include <limits.h> #include <stddef.h> #include <inttypes.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <unistd.h> #include <netinet/in.h> #include <resolv.h> #include <pthread.h> #include 'lmdb.h' #include 'midl.h' Data Structures struct MDB_rxbody struct MDB_reader struct MDB_txbody struct MDB_txninfo struct MDB_page struct MDB_node struct MDB_db struct MDB_meta union MDB_metabuf struct MDB_dbx struct MDB_txn Opaque structure for a transaction handle. struct MDB_cursor Opaque structure for navigating through a database. struct MDB_xcursor struct MDB_pgstate struct MDB_env Opaque structure for a database environment. struct MDB_ntxn Macros #define _GNU_SOURCE 1 #define MDB_PID_T pid_t #define VGMEMP_CREATE(h, r, z) #define VGMEMP_ALLOC(h, a, s) #define VGMEMP_FREE(h, a) #define VGMEMP_DESTROY(h) #define VGMEMP_DEFINED(a, s) #define BYTE_ORDER __BYTE_ORDER #define LITTLE_ENDIAN __LITTLE_ENDIAN #define BIG_ENDIAN __BIG_ENDIAN #define Z 'z' #define MDB_PIDLOCK 1 #define LOCK_MUTEX_R(env) pthread_mutex_lock(&(env)->me_txns->mti_mutex) #define UNLOCK_MUTEX_R(env) pthread_mutex_unlock(&(env)->me_txns->mti_mutex) #define LOCK_MUTEX_W(env) pthread_mutex_lock(&(env)->me_txns->mti_wmutex) #define UNLOCK_MUTEX_W(env) pthread_mutex_unlock(&(env)->me_txns->mti_wmutex) #define ErrCode() errno #define HANDLE int #define INVALID_HANDLE_VALUE (-1) #define GET_PAGESIZE(x) ((x) = sysconf(_SC_PAGE_SIZE)) #define MNAME_LEN (sizeof(pthread_mutex_t)) #define MDB_DSYNC O_DSYNC #define MDB_FDATASYNC fdatasync #define MDB_MSYNC(addr, len, flags) msync(addr,len,flags) #define MS_SYNC 1 #define MS_ASYNC 0 #define MDB_DEBUG 0 #define DPRINTF(args) ((void) 0) #define DPUTS(arg) DPRINTF(('%s', arg)) #define DDBI(mc) (((mc)->mc_flags & C_SUB) ? -(int)(mc)->mc_dbi : (int)(mc)->mc_dbi) #define MAX_PAGESIZE 0x8000 The maximum size of a database page. #define MDB_MINKEYS 2 #define MDB_MAGIC 0xBEEFC0DE #define MDB_DATA_VERSION 1 #define MDB_LOCK_VERSION 1 #define MDB_MAXKEYSIZE 511 The maximum size of a key in the database. #define MAXDATASIZE 0xffffffffUL The maximum size of a data item. #define DKBUF #define DKEY(x) 0 #define P_INVALID (~(pgno_t)0) #define F_ISSET(w, f) (((w) & (f)) == (f)) #define DEFAULT_MAPSIZE 1048576 #define DEFAULT_READERS 126 #define CACHELINE 64 #define mr_txnid mru.mrx.mrb_txnid #define mr_pid mru.mrx.mrb_pid #define mr_tid mru.mrx.mrb_tid #define mti_magic mt1.mtb.mtb_magic #define mti_format mt1.mtb.mtb_format #define mti_mutex mt1.mtb.mtb_mutex #define mti_rmname mt1.mtb.mtb_rmname #define mti_txnid mt1.mtb.mtb_txnid #define mti_numreaders mt1.mtb.mtb_numreaders #define mti_wmutex mt2.mt2_wmutex #define MDB_LOCK_FORMAT #define mp_pgno mp_p.p_pgno #define mp_next mp_p.p_next #define P_BRANCH 0x01 #define P_LEAF 0x02 #define P_OVERFLOW 0x04 #define P_META 0x08 #define P_DIRTY 0x10 #define P_LEAF2 0x20 #define P_SUBP 0x40 #define P_KEEP 0x8000 #define mp_lower mp_pb.pb.pb_lower #define mp_upper mp_pb.pb.pb_upper #define mp_pages mp_pb.pb_pages #define PAGEHDRSZ ((unsigned) offsetof(MDB_page, mp_ptrs)) #define METADATA(p) ((void *)((char *)(p) + PAGEHDRSZ)) #define NUMKEYS(p) (((p)->mp_lower - PAGEHDRSZ) >> 1) #define SIZELEFT(p) (indx_t)((p)->mp_upper - (p)->mp_lower) #define PAGEFILL(env, p) #define FILL_THRESHOLD 250 #define IS_LEAF(p) F_ISSET((p)->mp_flags, P_LEAF) #define IS_LEAF2(p) F_ISSET((p)->mp_flags, P_LEAF2) #define IS_BRANCH(p) F_ISSET((p)->mp_flags, P_BRANCH) #define IS_OVERFLOW(p) F_ISSET((p)->mp_flags, P_OVERFLOW) #define IS_SUBP(p) F_ISSET((p)->mp_flags, P_SUBP) #define OVPAGES(size, psize) ((PAGEHDRSZ-1 + (size)) / (psize) + 1) #define mn_lo mn_offset[BYTE_ORDER!=LITTLE_ENDIAN] #define mn_hi mn_offset[BYTE_ORDER==LITTLE_ENDIAN] #define F_BIGDATA 0x01 #define F_SUBDATA 0x02 #define F_DUPDATA 0x04 #define NODE_ADD_FLAGS (F_DUPDATA|F_SUBDATA|MDB_RESERVE|MDB_APPEND) #define NODESIZE offsetof(MDB_node, mn_data) #define PGNO_TOPWORD ((pgno_t)-1 > 0xffffffffu ? 32 : 0) #define INDXSIZE(k) (NODESIZE + ((k) == NULL ? 0 : (k)->mv_size)) #define LEAFSIZE(k, d) (NODESIZE + (k)->mv_size + (d)->mv_size) #define NODEPTR(p, i) ((MDB_node *)((char *)(p) + (p)->mp_ptrs[i])) #define NODEKEY(node) (void *)((node)->mn_data) #define NODEDATA(node) (void *)((char *)(node)->mn_data + (node)->mn_ksize) #define NODEPGNO(node) #define SETPGNO(node, pgno) #define NODEDSZ(node) ((node)->mn_lo | ((unsigned)(node)->mn_hi << 16)) #define SETDSZ(node, size) #define NODEKSZ(node) ((node)->mn_ksize) #define COPY_PGNO(dst, src) #define LEAF2KEY(p, i, ks) ((char *)(p) + PAGEHDRSZ + ((i)*(ks))) #define MDB_GET_KEY(node, keyptr) #define MDB_GET_KEY2(node, key) { key.mv_size = NODEKSZ(node); key.mv_data = NODEKEY(node); } #define MDB_VALID 0x8000 #define PERSISTENT_FLAGS (0xffff & ~(MDB_VALID)) #define VALID_FLAGS #define FREE_DBI 0 #define MAIN_DBI 1 #define mm_psize mm_dbs[0].md_pad #define mm_flags mm_dbs[0].md_flags #define DB_DIRTY 0x01 #define DB_STALE 0x02 #define DB_NEW 0x04 #define DB_VALID 0x08 #define MDB_TXN_RDONLY 0x01 #define MDB_TXN_ERROR 0x02 #define MDB_TXN_DIRTY 0x04 #define MDB_TXN_SPILLS 0x08 #define CURSOR_STACK 32 #define C_INITIALIZED 0x01 #define C_EOF 0x02 #define C_SUB 0x04 #define C_DEL 0x08 #define C_SPLITTING 0x20 #define C_UNTRACK 0x40 #define MDB_FATAL_ERROR 0x80000000U #define MDB_ENV_ACTIVE 0x20000000U #define MDB_ENV_TXKEY 0x10000000U #define MDB_LIVE_READER 0x08000000U #define me_pglast me_pgstate.mf_pglast #define me_pghead me_pgstate.mf_pghead #define MDB_COMMIT_PAGES 64 #define MAX_WRITE (0x80000000U >> (sizeof(ssize_t) == 4)) #define MDB_PS_MODIFY 1 #define MDB_PS_ROOTONLY 2 #define MDB_PS_FIRST 4 #define MDB_PS_LAST 8 #define MDB_SPLIT_REPLACE MDB_APPENDDUP #define mdb_env_close0(env, excl) mdb_env_close1(env) #define mdb_txn_reset0(txn, act) mdb_txn_reset0(txn) #define DO_PWRITE(rc, fd, ptr, size, len, pos) #define MDB_ERRCODE_ROFS EROFS #define MDB_CLOEXEC 0 #define LOCKNAME '/lock.mdb' #define DATANAME '/data.mdb' #define LOCKSUFF '-lock' #define CHANGEABLE (MDB_NOSYNC|MDB_NOMETASYNC|MDB_MAPASYNC|MDB_NOMEMINIT) #define CHANGELESS #define DO_WRITE(rc, fd, ptr, w2, len) len = write(fd, ptr, w2); rc = (len >= 0) #define MDB_NOSPILL 0x8000 Typedefs typedef MDB_ID pgno_t typedef MDB_ID txnid_t typedef uint16_t indx_t Enumerations enum Pidlock_op { Pidset = F_SETLK, Pidcheck = F_GETLK } Functions static int mdb_page_alloc (MDB_cursor *mc, int num, MDB_page **mp) static int mdb_page_new (MDB_cursor *mc, uint32_t flags, int num, MDB_page **mp) static int mdb_page_touch (MDB_cursor *mc) static int mdb_page_get (MDB_txn *txn, pgno_t pgno, MDB_page **mp, int *lvl) static int mdb_page_search_root (MDB_cursor *mc, MDB_val *key, int modify) static int mdb_page_search (MDB_cursor *mc, MDB_val *key, int flags) static int mdb_page_merge (MDB_cursor *csrc, MDB_cursor *cdst) static int mdb_page_split (MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno, unsigned int nflags) static int mdb_env_read_header (MDB_env *env, MDB_meta *meta) static int mdb_env_pick_meta (const MDB_env *env) static int mdb_env_write_meta (MDB_txn *txn) static void mdb_env_close0 (MDB_env *env, int excl) static MDB_node * mdb_node_search (MDB_cursor *mc, MDB_val *key, int *exactp) static int mdb_node_add (MDB_cursor *mc, indx_t indx, MDB_val *key, MDB_val *data, pgno_t pgno, unsigned int flags) static void mdb_node_del (MDB_page *mp, indx_t indx, int ksize) static void mdb_node_shrink (MDB_page *mp, indx_t indx) static int mdb_node_move (MDB_cursor *csrc, MDB_cursor *cdst) static int mdb_node_read (MDB_txn *txn, MDB_node *leaf, MDB_val *data) static size_t mdb_leaf_size (MDB_env *env, MDB_val *key, MDB_val *data) static size_t mdb_branch_size (MDB_env *env, MDB_val *key) static int mdb_rebalance (MDB_cursor *mc) static int mdb_update_key (MDB_cursor *mc, MDB_val *key) static void mdb_cursor_pop (MDB_cursor *mc) static int mdb_cursor_push (MDB_cursor *mc, MDB_page *mp) static int mdb_cursor_del0 (MDB_cursor *mc, MDB_node *leaf) static int mdb_cursor_sibling (MDB_cursor *mc, int move_right) static int mdb_cursor_next (MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op) static int mdb_cursor_prev (MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op) static int mdb_cursor_set (MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op, int *exactp) static int mdb_cursor_first (MDB_cursor *mc, MDB_val *key, MDB_val *data) static int mdb_cursor_last (MDB_cursor *mc, MDB_val *key, MDB_val *data) static void mdb_cursor_init (MDB_cursor *mc, MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx) static void mdb_xcursor_init0 (MDB_cursor *mc) static void mdb_xcursor_init1 (MDB_cursor *mc, MDB_node *node) static int mdb_drop0 (MDB_cursor *mc, int subs) static void mdb_default_cmp (MDB_txn *txn, MDB_dbi dbi) char * mdb_version (int *major, int *minor, int *patch) Return the mdb library version information. char * mdb_strerror (int err) Return a string describing a given error code. int mdb_cmp (MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b) Compare two data items according to a particular database. int mdb_dcmp (MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b) Compare two data items according to a particular database. static MDB_page * mdb_page_malloc (MDB_txn *txn, unsigned num) static void mdb_page_free (MDB_env *env, MDB_page *mp) static void mdb_dpage_free (MDB_env *env, MDB_page *dp) static void mdb_dlist_free (MDB_txn *txn) static int mdb_pages_xkeep (MDB_cursor *mc, unsigned pflags, int all) static int mdb_page_flush (MDB_txn *txn, int keep) static int mdb_page_spill (MDB_cursor *m0, MDB_val *key, MDB_val *data) static txnid_t mdb_find_oldest (MDB_txn *txn) static void mdb_page_dirty (MDB_txn *txn, MDB_page *mp) static void mdb_page_copy (MDB_page *dst, MDB_page *src, unsigned int psize) static int mdb_page_unspill (MDB_txn *txn, MDB_page *mp, MDB_page **ret) int mdb_env_sync (MDB_env *env, int force) Flush the data buffers to disk. static int mdb_cursor_shadow (MDB_txn *src, MDB_txn *dst) static void mdb_cursors_close (MDB_txn *txn, unsigned merge) static void mdb_txn_reset0 (MDB_txn *txn, const char *act) static int mdb_reader_pid (MDB_env *env, enum Pidlock_op op, MDB_PID_T pid) static int mdb_txn_renew0 (MDB_txn *txn) int mdb_txn_renew (MDB_txn *txn) Renew a read-only transaction. int mdb_txn_begin (MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret) Create a transaction for use with the environment. MDB_env * mdb_txn_env (MDB_txn *txn) Returns the transaction's MDB_env. static void mdb_dbis_update (MDB_txn *txn, int keep) void mdb_txn_reset (MDB_txn *txn) Reset a read-only transaction. void mdb_txn_abort (MDB_txn *txn) Abandon all the operations of the transaction instead of saving them. static int mdb_freelist_save (MDB_txn *txn) int mdb_txn_commit (MDB_txn *txn) Commit all the operations of a transaction into the database. static int mdb_env_init_meta (MDB_env *env, MDB_meta *meta) int mdb_env_create (MDB_env **env) Create an MDB environment handle. static int mdb_env_map (MDB_env *env, void *addr, int newsize) int mdb_env_set_mapsize (MDB_env *env, size_t size) Set the size of the memory map to use for this environment. int mdb_env_set_maxdbs (MDB_env *env, MDB_dbi dbs) Set the maximum number of named databases for the environment. int mdb_env_set_maxreaders (MDB_env *env, unsigned int readers) Set the maximum number of threads/reader slots for the environment. int mdb_env_get_maxreaders (MDB_env *env, unsigned int *readers) Get the maximum number of threads/reader slots for the environment. static int mdb_env_open2 (MDB_env *env) static void mdb_env_reader_dest (void *ptr) static int mdb_env_share_locks (MDB_env *env, int *excl) static int mdb_env_excl_lock (MDB_env *env, int *excl) static int mdb_env_setup_locks (MDB_env *env, char *lpath, int mode, int *excl) int mdb_env_open (MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode) Open an environment handle. int mdb_env_copyfd (MDB_env *env, HANDLE fd) int mdb_env_copy (MDB_env *env, const char *path) Copy an MDB environment to the specified path. void mdb_env_close (MDB_env *env) Close the environment and release the memory map. static int mdb_cmp_long (const MDB_val *a, const MDB_val *b) static int mdb_cmp_int (const MDB_val *a, const MDB_val *b) static int mdb_cmp_cint (const MDB_val *a, const MDB_val *b) static int mdb_cmp_memn (const MDB_val *a, const MDB_val *b) static int mdb_cmp_memnr (const MDB_val *a, const MDB_val *b) static int mdb_page_search_lowest (MDB_cursor *mc) static int mdb_ovpage_free (MDB_cursor *mc, MDB_page *mp) int mdb_get (MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data) Get items from a database. int mdb_cursor_get (MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op) Retrieve by cursor. static int mdb_cursor_touch (MDB_cursor *mc) int mdb_cursor_put (MDB_cursor *mc, MDB_val *key, MDB_val *data, unsigned int flags) Store by cursor. int mdb_cursor_del (MDB_cursor *mc, unsigned int flags) Delete current key/data pair. int mdb_cursor_open (MDB_txn *txn, MDB_dbi dbi, MDB_cursor **ret) Create a cursor handle. int mdb_cursor_renew (MDB_txn *txn, MDB_cursor *mc) Renew a cursor handle. int mdb_cursor_count (MDB_cursor *mc, size_t *countp) Return count of duplicates for current key. void mdb_cursor_close (MDB_cursor *mc) Close a cursor handle. MDB_txn * mdb_cursor_txn (MDB_cursor *mc) Return the cursor's transaction handle. MDB_dbi mdb_cursor_dbi (MDB_cursor *mc) Return the cursor's database handle. static void mdb_cursor_copy (const MDB_cursor *csrc, MDB_cursor *cdst) int mdb_del (MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data) Delete items from a database. int mdb_put (MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data, unsigned int flags) Store items into a database. int mdb_env_set_flags (MDB_env *env, unsigned int flag, int onoff) Set environment flags. int mdb_env_get_flags (MDB_env *env, unsigned int *arg) Get environment flags. int mdb_env_get_path (MDB_env *env, const char **arg) Return the path that was used in mdb_env_open(). int mdb_env_get_fd (MDB_env *env, mdb_filehandle_t *arg) Return the filedescriptor for the given environment. static int mdb_stat0 (MDB_env *env, MDB_db *db, MDB_stat *arg) int mdb_env_stat (MDB_env *env, MDB_stat *arg) Return statistics about the MDB environment. int mdb_env_info (MDB_env *env, MDB_envinfo *arg) Return information about the MDB environment. int mdb_dbi_open (MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi) Open a database in the environment. int mdb_stat (MDB_txn *txn, MDB_dbi dbi, MDB_stat *arg) Retrieve statistics for a database. void mdb_dbi_close (MDB_env *env, MDB_dbi dbi) Close a database handle. int mdb_dbi_flags (MDB_txn *txn, MDB_dbi dbi, unsigned int *flags) Retrieve the DB flags for a database handle. int mdb_drop (MDB_txn *txn, MDB_dbi dbi, int del) Empty or delete+close a database. int mdb_set_compare (MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp) Set a custom key comparison function for a database. int mdb_set_dupsort (MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp) Set a custom data comparison function for a MDB_DUPSORT database. int mdb_set_relfunc (MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel) Set a relocation function for a MDB_FIXEDMAP database. int mdb_set_relctx (MDB_txn *txn, MDB_dbi dbi, void *ctx) Set a context pointer for a MDB_FIXEDMAP database's relocation function. int mdb_env_get_maxkeysize (MDB_env *env) Get the maximum size of a key for the environment. int mdb_reader_list (MDB_env *env, MDB_msg_func *func, void *ctx) Dump the entries in the reader lock table. static int mdb_pid_insert (MDB_PID_T *ids, MDB_PID_T pid) int mdb_reader_check (MDB_env *env, int *dead) Check for stale entries in the reader lock table. Variables static char *const mdb_errstr []
Detailed Description
memory-mapped database library A Btree-based database management library modeled loosely on the BerkeleyDB API, but much simplified.
Macro Definition Documentation
#define mr_txnid mru.mrx.mrb_txnid shorthand for mrb_txnid #define mn_lo mn_offset[BYTE_ORDER!=LITTLE_ENDIAN] lo and hi are used for data size on leaf nodes and for child pgno on branch nodes. On 64 bit platforms, flags is also used for pgno. (Branch nodes have no flags). They are in host byte order in case that lets some accesses be optimized into a 32-bit word access. #define mn_hi mn_offset[BYTE_ORDER==LITTLE_ENDIAN] part of dsize or pgno #define mm_psize mm_dbs[0].md_pad The size of pages used in this DB #define mm_flags mm_dbs[0].md_flags Any persistent environment flags. Environment Flags #define MDB_FATAL_ERROR 0x80000000U Failed to update the meta page. Probably an I/O error. #define MDB_ENV_ACTIVE 0x20000000U Some fields are initialized. #define MDB_ENV_TXKEY 0x10000000U me_txkey is set #define MDB_LIVE_READER 0x08000000U Have liveness lock in reader table #define DO_PWRITE(rc, fd, ptr, size, len, pos) Value: do { len = pwrite(fd, ptr, size, pos); rc = (len >= 0); } while(0)
Author
Generated automatically by Doxygen for MDB from the source code.