NCBI C++ ToolKit
Classes | Macros | Typedefs | Enumerations | Functions | Variables
mdb.c File Reference

Lightning memory-mapped database library. More...

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/uio.h>
#include <sys/mman.h>
#include <fcntl.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 <signal.h>
#include "lmdb.h"
#include "midl.h"
+ Include dependency graph for mdb.c:

Go to the source code of this file.

Go to the SVN repository for this file.

Classes

struct  MDB_rxbody
 The information we store in a single slot of the reader table. More...
 
struct  MDB_reader
 The actual reader record, with cacheline padding. More...
 
struct  MDB_txbody
 The header for the reader table. More...
 
struct  MDB_txninfo
 The actual reader table definition. More...
 
struct  MDB_page
 Common header for all page types. More...
 
struct  MDB_node
 Header for a single key/data pair within a page. More...
 
struct  MDB_db
 Information about a single database in the environment. More...
 
struct  MDB_meta
 Meta page content. More...
 
union  MDB_metabuf
 Buffer for a stack-allocated meta page. More...
 
struct  MDB_dbx
 Auxiliary DB info. More...
 
struct  MDB_txn
 A database transaction. More...
 
struct  MDB_cursor
 Cursors are used for all DB operations. More...
 
struct  MDB_xcursor
 Context for sorted-dup records. More...
 
struct  MDB_pgstate
 State of FreeDB old pages, stored in the MDB_env. More...
 
struct  MDB_env
 The database environment. More...
 
struct  MDB_ntxn
 Nested transaction. More...
 
struct  MDB_name
 Filename - string of mdb_nchar_t[]. More...
 
struct  mdb_copy
 State needed for a double-buffering compacting copy. More...
 

Macros

#define _GNU_SOURCE   1
 
#define MDB_PID_T   pid_t
 
#define MDB_THR_T   pthread_t
 
#define CACHEFLUSH(addr, bytes, cache)
 
#define MDB_USE_POSIX_MUTEX   1
 
#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 ESECT
 
#define CALL_CONV
 
#define MDB_DEVEL   0
 Features under development. More...
 
#define mdb_func_   "<mdb_unknown>"
 Wrapper around __func__, which is a C99 feature. More...
 
#define MDB_NO_ROOT   (MDB_LAST_ERRCODE + 10)
 
#define MDB_USE_ROBUST   1
 Some platforms define the EOWNERDEAD error code even though they don't support Robust Mutexes. More...
 
#define THREAD_RET   void *
 
#define THREAD_CREATE(thr, start, arg)   pthread_create(&thr,NULL,start,arg)
 
#define THREAD_FINISH(thr)   pthread_join(thr,NULL)
 
#define Z   "z"
 printf format modifier for size_t More...
 
#define MDB_PIDLOCK   1
 For MDB_LOCK_FORMAT: True if readers take a pid lock in the lockfile. More...
 
#define LOCK_MUTEX0(mutex)   pthread_mutex_lock(mutex)
 Lock the reader or writer mutex. More...
 
#define UNLOCK_MUTEX(mutex)   pthread_mutex_unlock(mutex)
 Unlock the reader or writer mutex. More...
 
#define mdb_mutex_consistent(mutex)   pthread_mutex_consistent(mutex)
 Mark mutex-protected data as repaired, after death of previous owner. More...
 
#define ErrCode()   errno
 Get the error code for the last failed system function. More...
 
#define HANDLE   int
 An abstraction for a file handle. More...
 
#define INVALID_HANDLE_VALUE   (-1)
 A value for an invalid file handle. More...
 
#define GET_PAGESIZE(x)   ((x) = sysconf(_SC_PAGE_SIZE))
 Get the size of a memory page for the system. More...
 
#define MNAME_LEN   (sizeof(pthread_mutex_t))
 
#define LOCK_MUTEX(rc, env, mutex)   ((rc) = LOCK_MUTEX0(mutex))
 
#define mdb_mutex_failed(env, mutex, rc)   (rc)
 
#define MDB_DSYNC   O_SYNC
 A flag for opening a file and requesting synchronous data writes. More...
 
#define MDB_FDATASYNC   fdatasync
 Function for flushing the data of a file. More...
 
#define MDB_MSYNC(addr, len, flags)   msync(addr,len,flags)
 
#define MS_SYNC   1
 
#define MS_ASYNC   0
 
#define MDB_DEBUG   0
 Enable debug output. More...
 
#define DPRINTF(args)   ((void) 0)
 
#define DPUTS(arg)   DPRINTF(("%s", arg))
 Print a debug string. More...
 
#define DDBI(mc)    (((mc)->mc_flags & C_SUB) ? -(int)(mc)->mc_dbi : (int)(mc)->mc_dbi)
 Debuging output value of a cursor DBI: Negative in a sub-cursor. More...
 
#define MAX_PAGESIZE   (PAGEBASE ? 0x10000 : 0x8000)
 The maximum size of a database page. More...
 
#define MDB_MINKEYS   2
 The minimum number of keys required in a database page. More...
 
#define MDB_MAGIC   0xBEEFC0DE
 A stamp that identifies a file as an LMDB file. More...
 
#define MDB_DATA_VERSION   ((MDB_DEVEL) ? 999 : 1)
 The version number for a database's datafile format. More...
 
#define MDB_LOCK_VERSION   1
 The version number for a database's lockfile format. More...
 
#define MDB_MAXKEYSIZE   ((MDB_DEVEL) ? 0 : 511)
 The max size of a key we can write, or 0 for computed max. More...
 
#define ENV_MAXKEY(env)   (MDB_MAXKEYSIZE)
 The maximum size of a key we can write to the environment. More...
 
#define MAXDATASIZE   0xffffffffUL
 The maximum size of a data item. More...
 
#define DKBUF
 
#define DKEY(x)   0
 
#define P_INVALID   (~(pgno_t)0)
 An invalid page number. More...
 
#define F_ISSET(w, f)   (((w) & (f)) == (f))
 Test if the flags f are set in a flag word w. More...
 
#define EVEN(n)   (((n) + 1U) & -2) /* sign-extending -2 to match n+1U */
 Round n up to an even number. More...
 
#define DEFAULT_MAPSIZE   1048576
 Default size of memory map. More...
 
#define DEFAULT_READERS   126
 Number of slots in the reader table. More...
 
#define CACHELINE   64
 The size of a CPU cache line in bytes. More...
 
#define mr_txnid   mru.mrx.mrb_txnid
 shorthand for mrb_txnid More...
 
#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_rmutex   mt1.mtb.mtb_rmutex
 
#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
 Lockfile format signature: version, features and field layout. More...
 
#define mp_pgno   mp_p.p_pgno
 
#define mp_next   mp_p.p_next
 
#define P_BRANCH   0x01
 branch page More...
 
#define P_LEAF   0x02
 leaf page More...
 
#define P_OVERFLOW   0x04
 overflow page More...
 
#define P_META   0x08
 meta page More...
 
#define P_DIRTY   0x10
 dirty page, also set for P_SUBP pages More...
 
#define P_LEAF2   0x20
 for MDB_DUPFIXED records More...
 
#define P_SUBP   0x40
 for MDB_DUPSORT sub-pages More...
 
#define P_LOOSE   0x4000
 page was dirtied then freed, can be reused More...
 
#define P_KEEP   0x8000
 leave this page alone during spill More...
 
#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))
 Size of the page header, excluding dynamic data at the end. More...
 
#define METADATA(p)   ((void *)((char *)(p) + PAGEHDRSZ))
 Address of first usable data byte in a page, after the header. More...
 
#define PAGEBASE   ((MDB_DEVEL) ? PAGEHDRSZ : 0)
 ITS#7713, change PAGEBASE to handle 65536 byte pages. More...
 
#define NUMKEYS(p)   (((p)->mp_lower - (PAGEHDRSZ-PAGEBASE)) >> 1)
 Number of nodes on a page. More...
 
#define SIZELEFT(p)   (indx_t)((p)->mp_upper - (p)->mp_lower)
 The amount of space remaining in the page. More...
 
#define PAGEFILL(env, p)
 The percentage of space used in the page, in tenths of a percent. More...
 
#define FILL_THRESHOLD   250
 The minimum page fill factor, in tenths of a percent. More...
 
#define IS_LEAF(p)   F_ISSET((p)->mp_flags, P_LEAF)
 Test if a page is a leaf page. More...
 
#define IS_LEAF2(p)   F_ISSET((p)->mp_flags, P_LEAF2)
 Test if a page is a LEAF2 page. More...
 
#define IS_BRANCH(p)   F_ISSET((p)->mp_flags, P_BRANCH)
 Test if a page is a branch page. More...
 
#define IS_OVERFLOW(p)   F_ISSET((p)->mp_flags, P_OVERFLOW)
 Test if a page is an overflow page. More...
 
#define IS_SUBP(p)   F_ISSET((p)->mp_flags, P_SUBP)
 Test if a page is a sub page. More...
 
#define OVPAGES(size, psize)   ((PAGEHDRSZ-1 + (size)) / (psize) + 1)
 The number of overflow pages needed to store the given size. More...
 
#define NEXT_LOOSE_PAGE(p)   (*(MDB_page **)((p) + 2))
 Link in MDB_txn. More...
 
#define F_BIGDATA   0x01
 data put on overflow page More...
 
#define F_SUBDATA   0x02
 data is a sub-database More...
 
#define F_DUPDATA   0x04
 data has duplicates More...
 
#define NODE_ADD_FLAGS   (F_DUPDATA|F_SUBDATA|MDB_RESERVE|MDB_APPEND)
 valid flags for mdb_node_add() More...
 
#define NODESIZE   offsetof(MDB_node, mn_data)
 Size of the node header, excluding dynamic data at the end. More...
 
#define PGNO_TOPWORD   ((pgno_t)-1 > 0xffffffffu ? 32 : 0)
 Bit position of top word in page number, for shifting mn_flags. More...
 
#define INDXSIZE(k)   (NODESIZE + ((k) == NULL ? 0 : (k)->mv_size))
 Size of a node in a branch page with a given key. More...
 
#define LEAFSIZE(k, d)   (NODESIZE + (k)->mv_size + (d)->mv_size)
 Size of a node in a leaf page with a given key and data. More...
 
#define NODEPTR(p, i)   ((MDB_node *)((char *)(p) + (p)->mp_ptrs[i] + PAGEBASE))
 Address of node i in page p. More...
 
#define NODEKEY(node)   (void *)((node)->mn_data)
 Address of the key for the node. More...
 
#define NODEDATA(node)   (void *)((char *)(node)->mn_data + (node)->mn_ksize)
 Address of the data for a node. More...
 
#define NODEPGNO(node)
 Get the page number pointed to by a branch node. More...
 
#define SETPGNO(node, pgno)
 Set the page number in a branch node. More...
 
#define NODEDSZ(node)   ((node)->mn_lo | ((unsigned)(node)->mn_hi << 16))
 Get the size of the data in a leaf node. More...
 
#define SETDSZ(node, size)
 Set the size of the data for a leaf node. More...
 
#define NODEKSZ(node)   ((node)->mn_ksize)
 The size of a key in a node. More...
 
#define COPY_PGNO(dst, src)
 Copy a page number from src to dst. More...
 
#define LEAF2KEY(p, i, ks)   ((char *)(p) + PAGEHDRSZ + ((i)*(ks)))
 The address of a key in a LEAF2 page. More...
 
#define MDB_GET_KEY(node, keyptr)
 Set the node's key into keyptr, if requested. More...
 
#define MDB_GET_KEY2(node, key)   { key.mv_size = NODEKSZ(node); key.mv_data = NODEKEY(node); }
 Set the node's key into key. More...
 
#define MDB_VALID   0x8000
 DB handle is valid, for me_dbflags. More...
 
#define PERSISTENT_FLAGS   (0xffff & ~(MDB_VALID))
 
#define VALID_FLAGS
 mdb_dbi_open() flags More...
 
#define FREE_DBI   0
 Handle for the DB used to track free pages. More...
 
#define MAIN_DBI   1
 Handle for the default DB. More...
 
#define CORE_DBS   2
 Number of DBs in metapage (free and main) - also hardcoded elsewhere. More...
 
#define NUM_METAS   2
 Number of meta pages - also hardcoded elsewhere. More...
 
#define mm_psize   mm_dbs[FREE_DBI].md_pad
 The size of pages used in this DB. More...
 
#define mm_flags   mm_dbs[FREE_DBI].md_flags
 Any persistent environment flags. More...
 
#define DB_DIRTY   0x01
 DB was written in this txn. More...
 
#define DB_STALE   0x02
 Named-DB record is older than txnID. More...
 
#define DB_NEW   0x04
 Named-DB handle opened in this txn. More...
 
#define DB_VALID   0x08
 DB handle is valid, see also MDB_VALID. More...
 
#define DB_USRVALID   0x10
 As DB_VALID, but not set for FREE_DBI. More...
 
#define DB_DUPDATA   0x20
 DB is MDB_DUPSORT data. More...
 
#define MDB_TXN_BEGIN_FLAGS   MDB_RDONLY
 mdb_txn_begin() flags More...
 
#define MDB_TXN_RDONLY   MDB_RDONLY
 read-only transaction More...
 
#define MDB_TXN_WRITEMAP   MDB_WRITEMAP
 copy of MDB_env flag in writers More...
 
#define MDB_TXN_FINISHED   0x01
 txn is finished or never began More...
 
#define MDB_TXN_ERROR   0x02
 txn is unusable after an error More...
 
#define MDB_TXN_DIRTY   0x04
 must write, even if dirty list is empty More...
 
#define MDB_TXN_SPILLS   0x08
 txn or a parent has spilled pages More...
 
#define MDB_TXN_HAS_CHILD   0x10
 txn has an MDB_txn. More...
 
#define MDB_TXN_BLOCKED   (MDB_TXN_FINISHED|MDB_TXN_ERROR|MDB_TXN_HAS_CHILD)
 most operations on the txn are currently illegal More...
 
#define CURSOR_STACK   32
 Enough space for 2^32 nodes with minimum of 2 keys per node. More...
 
#define C_INITIALIZED   0x01
 cursor has been initialized and is valid More...
 
#define C_EOF   0x02
 No more data. More...
 
#define C_SUB   0x04
 Cursor is a sub-cursor. More...
 
#define C_DEL   0x08
 last op was a cursor_del More...
 
#define C_UNTRACK   0x40
 Un-track cursor when closing. More...
 
#define XCURSOR_INITED(mc)    ((mc)->mc_xcursor && ((mc)->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED))
 Check if there is an inited xcursor. More...
 
#define XCURSOR_REFRESH(mc, top, mp)
 Update the xcursor's sub-page pointer, if any, in mc. More...
 
#define MDB_FATAL_ERROR   0x80000000U
 Failed to update the meta page. More...
 
#define MDB_ENV_ACTIVE   0x20000000U
 Some fields are initialized. More...
 
#define MDB_ENV_TXKEY   0x10000000U
 me_txkey is set More...
 
#define MDB_FSYNCONLY   0x08000000U
 fdatasync is unreliable More...
 
#define me_pglast   me_pgstate.mf_pglast
 
#define me_pghead   me_pgstate.mf_pghead
 
#define me_rmutex   me_txns->mti_rmutex
 Shared reader lock. More...
 
#define me_wmutex   me_txns->mti_wmutex
 Shared writer lock. More...
 
#define MDB_COMMIT_PAGES   64
 max number of pages to commit in one writev() call More...
 
#define MAX_WRITE   (0x40000000U >> (sizeof(ssize_t) == 4))
 max bytes to write in one call More...
 
#define TXN_DBI_EXIST(txn, dbi, validity)    ((txn) && (dbi)<(txn)->mt_numdbs && ((txn)->mt_dbflags[dbi] & (validity)))
 Check txn and dbi arguments to a function. More...
 
#define TXN_DBI_CHANGED(txn, dbi)    ((txn)->mt_dbiseqs[dbi] != (txn)->mt_env->me_dbiseqs[dbi])
 Check for misused dbi handles. More...
 
#define MDB_END_NAMES
 
#define MDB_END_OPMASK   0x0F
 mask for mdb_txn_end() operation number More...
 
#define MDB_END_UPDATE   0x10
 update env state (DBIs) More...
 
#define MDB_END_FREE   0x20
 free txn unless it is MDB_env. More...
 
#define MDB_END_SLOT   MDB_NOTLS
 release any reader slot if MDB_NOTLS More...
 
#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
 newkey is not new More...
 
#define mdb_env_close0(env, excl)   mdb_env_close1(env)
 
#define mdb_cmp_clong   mdb_cmp_cint
 Compare two items pointing at size_t's of unknown alignment. More...
 
#define mdb_cassert(mc, expr)   mdb_assert0((mc)->mc_txn->mt_env, expr, #expr)
 assert(3) variant in cursor context More...
 
#define mdb_tassert(txn, expr)   mdb_assert0((txn)->mt_env, expr, #expr)
 assert(3) variant in transaction context More...
 
#define mdb_eassert(env, expr)   mdb_assert0(env, expr, #expr)
 assert(3) variant in environment context More...
 
#define mdb_assert0(env, expr, expr_txt)
 
#define DO_PWRITE(rc, fd, ptr, size, len, pos)
 
#define MDB_NAME(str)   str
 mdb_nchar_t[] string literal More...
 
#define mdb_name_cpy   strcpy
 Copy name (mdb_nchar_t string) More...
 
#define MDB_SUFFLEN   9
 Max string length in mdb_suffixes[]. More...
 
#define mdb_fname_destroy(fname)    do { if ((fname).mn_alloced) free((fname).mn_val); } while (0)
 Destroy fname from mdb_fname_init() More...
 
#define MDB_CLOEXEC   0
 
#define MDB_ERRCODE_ROFS   EROFS
 
#define CHANGEABLE   (MDB_NOSYNC|MDB_NOMETASYNC|MDB_MAPASYNC|MDB_NOMEMINIT)
 Only a subset of the Environment Flags flags can be changed at runtime. More...
 
#define CHANGELESS
 
#define MDB_NOSPILL   0x8000
 Do not spill pages to disk if txn is getting full, may fail instead. More...
 
#define WITH_CURSOR_TRACKING(mn, act)
 Perform act while tracking temporary cursor mn. More...
 
#define MDB_WBUF   (1024*1024)
 
#define MDB_EOF   0x10
 mdb_env_copyfd1() is done reading More...
 
#define DO_WRITE(rc, fd, ptr, w2, len)   len = write(fd, ptr, w2); rc = (len >= 0)
 
#define DO_WRITE(rc, fd, ptr, w2, len)   len = write(fd, ptr, w2); rc = (len >= 0)
 

Typedefs

typedef pthread_mutex_t mdb_mutex_t[1]
 Shared mutex/semaphore as the original is stored. More...
 
typedef pthread_mutex_t * mdb_mutexref_t
 Reference to an mdb_mutex_t. More...
 
typedef MDB_ID pgno_t
 A page number in the database. More...
 
typedef MDB_ID txnid_t
 A transaction ID. More...
 
typedef uint16_t indx_t
 Used for offsets within a single page. More...
 
typedef struct MDB_rxbody MDB_rxbody
 The information we store in a single slot of the reader table. More...
 
typedef struct MDB_reader MDB_reader
 The actual reader record, with cacheline padding. More...
 
typedef struct MDB_txbody MDB_txbody
 The header for the reader table. More...
 
typedef struct MDB_txninfo MDB_txninfo
 The actual reader table definition. More...
 
typedef struct MDB_page MDB_page
 Common header for all page types. More...
 
typedef struct MDB_node MDB_node
 Header for a single key/data pair within a page. More...
 
typedef struct MDB_db MDB_db
 Information about a single database in the environment. More...
 
typedef struct MDB_meta MDB_meta
 Meta page content. More...
 
typedef union MDB_metabuf MDB_metabuf
 Buffer for a stack-allocated meta page. More...
 
typedef struct MDB_dbx MDB_dbx
 Auxiliary DB info. More...
 
typedef struct MDB_xcursor MDB_xcursor
 Context for sorted-dup records. More...
 
typedef struct MDB_pgstate MDB_pgstate
 State of FreeDB old pages, stored in the MDB_env. More...
 
typedef struct MDB_ntxn MDB_ntxn
 Nested transaction. More...
 
typedef char mdb_nchar_t
 Character type for file names: char on Unix, wchar_t on Windows. More...
 
typedef struct MDB_name MDB_name
 Filename - string of mdb_nchar_t[]. More...
 
typedef struct mdb_copy mdb_copy
 State needed for a double-buffering compacting copy. More...
 

Enumerations

enum  {
  MDB_END_COMMITTED , MDB_END_EMPTY_COMMIT , MDB_END_ABORT , MDB_END_RESET ,
  MDB_END_RESET_TMP , MDB_END_FAIL_BEGIN , MDB_END_FAIL_BEGINCHILD
}
 
enum  Pidlock_op { Pidset = F_SETLK , Pidcheck = F_GETLK }
 
enum  mdb_fopen_type {
  MDB_O_RDONLY = O_RDONLY , MDB_O_RDWR = O_RDWR |O_CREAT , MDB_O_META = O_WRONLY| O_SYNC | 0 , MDB_O_COPY = O_WRONLY|O_CREAT|O_EXCL| 0 ,
  MDB_O_MASK = MDB_O_RDWR| 0 | MDB_O_RDONLY|MDB_O_META|MDB_O_COPY , MDB_O_LOCKS = MDB_O_RDWR| 0 | ((MDB_O_MASK+1) & ~MDB_O_MASK)
}
 File type, access mode etc. More...
 

Functions

static int mdb_page_alloc (MDB_cursor *mc, int num, MDB_page **mp)
 Allocate page numbers and memory for writing. More...
 
static int mdb_page_new (MDB_cursor *mc, uint32_t flags, int num, MDB_page **mp)
 Allocate and initialize new pages for a database. More...
 
static int mdb_page_touch (MDB_cursor *mc)
 Touch a page: make it dirty and re-insert into tree with updated pgno. More...
 
static void mdb_txn_end (MDB_txn *txn, unsigned mode)
 End a transaction, except successful commit of a nested transaction. More...
 
static int mdb_page_get (MDB_cursor *mc, pgno_t pgno, MDB_page **ret, int *lvl)
 Find the address of the page corresponding to a given page number. More...
 
static int mdb_page_search_root (MDB_cursor *mc, MDB_val *key, int flags)
 Finish mdb_page_search() / mdb_page_search_lowest(). More...
 
static int mdb_page_search (MDB_cursor *mc, MDB_val *key, int flags)
 Search for the page a given key should be in. More...
 
static int mdb_page_merge (MDB_cursor *csrc, MDB_cursor *cdst)
 Merge one page into another. More...
 
static int mdb_page_split (MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno, unsigned int nflags)
 Split a page and insert a new node. More...
 
static int mdb_env_read_header (MDB_env *env, MDB_meta *meta)
 Read the environment parameters of a DB environment before mapping it into memory. More...
 
static MDB_metamdb_env_pick_meta (const MDB_env *env)
 Check both meta pages to see which one is newer. More...
 
static int mdb_env_write_meta (MDB_txn *txn)
 Update the environment info to commit a transaction. More...
 
static void mdb_env_close1 (MDB_env *env)
 Destroy resources from mdb_env_open(), clear our readers & DBIs. More...
 
static MDB_nodemdb_node_search (MDB_cursor *mc, MDB_val *key, int *exactp)
 Search for key within a page, using binary search. More...
 
static int mdb_node_add (MDB_cursor *mc, indx_t indx, MDB_val *key, MDB_val *data, pgno_t pgno, unsigned int flags)
 Add a node to the page pointed to by the cursor. More...
 
static void mdb_node_del (MDB_cursor *mc, int ksize)
 Delete the specified node from a page. More...
 
static void mdb_node_shrink (MDB_page *mp, indx_t indx)
 Compact the main page after deleting a node on a subpage. More...
 
static int mdb_node_move (MDB_cursor *csrc, MDB_cursor *cdst, int fromleft)
 Move a node from csrc to cdst. More...
 
static int mdb_node_read (MDB_cursor *mc, MDB_node *leaf, MDB_val *data)
 Return the data associated with a given node. More...
 
static size_t mdb_leaf_size (MDB_env *env, MDB_val *key, MDB_val *data)
 Calculate the size of a leaf node. More...
 
static size_t mdb_branch_size (MDB_env *env, MDB_val *key)
 Calculate the size of a branch node. More...
 
static int mdb_rebalance (MDB_cursor *mc)
 Rebalance the tree after a delete operation. More...
 
static int mdb_update_key (MDB_cursor *mc, MDB_val *key)
 Replace the key for a branch node with a new key. More...
 
static void mdb_cursor_pop (MDB_cursor *mc)
 Pop a page off the top of the cursor's stack. More...
 
static int mdb_cursor_push (MDB_cursor *mc, MDB_page *mp)
 Push a page onto the top of the cursor's stack. More...
 
static int mdb_cursor_del0 (MDB_cursor *mc)
 Complete a delete operation started by mdb_cursor_del(). More...
 
static int mdb_del0 (MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data, unsigned flags)
 
static int mdb_cursor_sibling (MDB_cursor *mc, int move_right)
 Find a sibling for a page. More...
 
static int mdb_cursor_next (MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op)
 Move the cursor to the next data item. More...
 
static int mdb_cursor_prev (MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op)
 Move the cursor to the previous data item. More...
 
static int mdb_cursor_set (MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op, int *exactp)
 Set the cursor on a specific data item. More...
 
static int mdb_cursor_first (MDB_cursor *mc, MDB_val *key, MDB_val *data)
 Move the cursor to the first item in the database. More...
 
static int mdb_cursor_last (MDB_cursor *mc, MDB_val *key, MDB_val *data)
 Move the cursor to the last item in the database. More...
 
static void mdb_cursor_init (MDB_cursor *mc, MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
 Initialize a cursor for a given transaction and database. More...
 
static void mdb_xcursor_init0 (MDB_cursor *mc)
 Initial setup of a sorted-dups cursor. More...
 
static void mdb_xcursor_init1 (MDB_cursor *mc, MDB_node *node)
 Final setup of a sorted-dups cursor. More...
 
static void mdb_xcursor_init2 (MDB_cursor *mc, MDB_xcursor *src_mx, int new_dupdata)
 Fixup a sorted-dups cursor due to underlying update. More...
 
static int mdb_drop0 (MDB_cursor *mc, int subs)
 Add all the DB's pages to the free list. More...
 
static void mdb_default_cmp (MDB_txn *txn, MDB_dbi dbi)
 Set the default comparison functions for a database. More...
 
static int mdb_reader_check0 (MDB_env *env, int rlocked, int *dead)
 As mdb_reader_check(). More...
 
char * mdb_version (int *major, int *minor, int *patch)
 Return the library version info. More...
 
char * mdb_strerror (int err)
 Return a string describing a given error code. More...
 
static void mdb_assert_fail (MDB_env *env, const char *expr_txt, const char *func, const char *file, int line)
 
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. More...
 
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. More...
 
static MDB_pagemdb_page_malloc (MDB_txn *txn, unsigned num)
 Allocate memory for a page. More...
 
static void mdb_page_free (MDB_env *env, MDB_page *mp)
 Free a single page. More...
 
static void mdb_dpage_free (MDB_env *env, MDB_page *dp)
 Free a dirty page. More...
 
static void mdb_dlist_free (MDB_txn *txn)
 Return all dirty pages to dpage list. More...
 
static int mdb_page_loose (MDB_cursor *mc, MDB_page *mp)
 Loosen or free a single page. More...
 
static int mdb_pages_xkeep (MDB_cursor *mc, unsigned pflags, int all)
 Set or clear P_KEEP in dirty, non-overflow, non-sub pages watched by txn. More...
 
static int mdb_page_flush (MDB_txn *txn, int keep)
 Flush (some) dirty pages to the map, after clearing their dirty flag. More...
 
static int mdb_page_spill (MDB_cursor *m0, MDB_val *key, MDB_val *data)
 Spill pages from the dirty list back to disk. More...
 
static txnid_t mdb_find_oldest (MDB_txn *txn)
 Find oldest txnid still referenced. More...
 
static void mdb_page_dirty (MDB_txn *txn, MDB_page *mp)
 Add a page to the txn's dirty list. More...
 
static void mdb_page_copy (MDB_page *dst, MDB_page *src, unsigned int psize)
 Copy the used portions of a non-overflow page. More...
 
static int mdb_page_unspill (MDB_txn *txn, MDB_page *mp, MDB_page **ret)
 Pull a page off the txn's spill list, if present. More...
 
int mdb_env_sync (MDB_env *env, int force)
 Flush the data buffers to disk. More...
 
static int mdb_cursor_shadow (MDB_txn *src, MDB_txn *dst)
 Back up parent txn's cursors, then grab the originals for tracking. More...
 
static void mdb_cursors_close (MDB_txn *txn, unsigned merge)
 Close this write txn's cursors, give parent txn's cursors back to parent. More...
 
static int mdb_reader_pid (MDB_env *env, enum Pidlock_op op, pid_t pid)
 Set or check a pid lock. More...
 
static int mdb_txn_renew0 (MDB_txn *txn)
 Common code for mdb_txn_begin() and mdb_txn_renew(). More...
 
int mdb_txn_renew (MDB_txn *txn)
 Renew a read-only transaction. More...
 
int mdb_txn_begin (MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret)
 Create a transaction for use with the environment. More...
 
MDB_envmdb_txn_env (MDB_txn *txn)
 Returns the transaction's MDB_env. More...
 
size_t mdb_txn_id (MDB_txn *txn)
 Return the transaction's ID. More...
 
static void mdb_dbis_update (MDB_txn *txn, int keep)
 Export or close DBI handles opened in this txn. More...
 
void mdb_txn_reset (MDB_txn *txn)
 Reset a read-only transaction. More...
 
void mdb_txn_abort (MDB_txn *txn)
 Abandon all the operations of the transaction instead of saving them. More...
 
static int mdb_freelist_save (MDB_txn *txn)
 Save the freelist as of this transaction to the freeDB. More...
 
int mdb_txn_commit (MDB_txn *txn)
 Commit all the operations of a transaction into the database. More...
 
static void mdb_env_init_meta0 (MDB_env *env, MDB_meta *meta)
 Fill in most of the zeroed MDB_meta for an empty database environment. More...
 
static int mdb_env_init_meta (MDB_env *env, MDB_meta *meta)
 Write the environment parameters of a freshly created DB environment. More...
 
int mdb_env_create (MDB_env **env)
 Create an LMDB environment handle. More...
 
static int mdb_env_map (MDB_env *env, void *addr)
 
int mdb_env_set_mapsize (MDB_env *env, size_t size)
 Set the size of the memory map to use for this environment. More...
 
int mdb_env_set_maxdbs (MDB_env *env, MDB_dbi dbs)
 Set the maximum number of named databases for the environment. More...
 
int mdb_env_set_maxreaders (MDB_env *env, unsigned int readers)
 Set the maximum number of threads/reader slots for the environment. More...
 
int mdb_env_get_maxreaders (MDB_env *env, unsigned int *readers)
 Get the maximum number of threads/reader slots for the environment. More...
 
static int mdb_fsize (int fd, size_t *size)
 
static int mdb_fname_init (const char *path, unsigned envflags, MDB_name *fname)
 Set up filename + scratch area for filename suffix, for opening files. More...
 
static int mdb_fopen (const MDB_env *env, MDB_name *fname, enum mdb_fopen_type which, mdb_mode_t mode, int *res)
 Open an LMDB file. More...
 
static int mdb_env_open2 (MDB_env *env)
 Further setup required for opening an LMDB environment. More...
 
static void mdb_env_reader_dest (void *ptr)
 Release a reader thread's slot in the reader lock table. More...
 
static int mdb_env_share_locks (MDB_env *env, int *excl)
 Downgrade the exclusive lock on the region back to shared. More...
 
static int mdb_env_excl_lock (MDB_env *env, int *excl)
 Try to get exclusive lock, otherwise shared. More...
 
static int mdb_env_setup_locks (MDB_env *env, MDB_name *fname, int mode, int *excl)
 Open and/or initialize the lock region for the environment. More...
 
int mdb_env_open (MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode)
 Open an environment handle. More...
 
void mdb_env_close (MDB_env *env)
 Close the environment and release the memory map. More...
 
static int mdb_cmp_long (const MDB_val *a, const MDB_val *b)
 Compare two items pointing at aligned size_t's. More...
 
static int mdb_cmp_int (const MDB_val *a, const MDB_val *b)
 Compare two items pointing at aligned unsigned int's. More...
 
static int mdb_cmp_cint (const MDB_val *a, const MDB_val *b)
 Compare two items pointing at unsigned ints of unknown alignment. More...
 
static int mdb_cmp_memn (const MDB_val *a, const MDB_val *b)
 Compare two items lexically. More...
 
static int mdb_cmp_memnr (const MDB_val *a, const MDB_val *b)
 Compare two items in reverse byte order. More...
 
static int mdb_page_search_lowest (MDB_cursor *mc)
 Search for the lowest key under the current branch page. More...
 
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. More...
 
int mdb_cursor_get (MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op)
 Retrieve by cursor. More...
 
static int mdb_cursor_touch (MDB_cursor *mc)
 Touch all the pages in the cursor stack. More...
 
int mdb_cursor_put (MDB_cursor *mc, MDB_val *key, MDB_val *data, unsigned int flags)
 Store by cursor. More...
 
int mdb_cursor_del (MDB_cursor *mc, unsigned int flags)
 Delete current key/data pair. More...
 
int mdb_cursor_open (MDB_txn *txn, MDB_dbi dbi, MDB_cursor **ret)
 Create a cursor handle. More...
 
int mdb_cursor_renew (MDB_txn *txn, MDB_cursor *mc)
 Renew a cursor handle. More...
 
int mdb_cursor_count (MDB_cursor *mc, size_t *countp)
 Return count of duplicates for current key. More...
 
void mdb_cursor_close (MDB_cursor *mc)
 Close a cursor handle. More...
 
MDB_txnmdb_cursor_txn (MDB_cursor *mc)
 Return the cursor's transaction handle. More...
 
MDB_dbi mdb_cursor_dbi (MDB_cursor *mc)
 Return the cursor's database handle. More...
 
static void mdb_cursor_copy (const MDB_cursor *csrc, MDB_cursor *cdst)
 Copy the contents of a cursor. More...
 
int mdb_del (MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data)
 Delete items from a database. More...
 
int mdb_put (MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data, unsigned int flags)
 Store items into a database. More...
 
static void * mdb_env_copythr (void *arg)
 Dedicated writer thread for compacting copy. More...
 
static int mdb_env_cthr_toggle (mdb_copy *my, int adjust)
 Give buffer and/or MDB_EOF to writer thread, await unused buffer. More...
 
static int mdb_env_cwalk (mdb_copy *my, pgno_t *pg, int flags)
 Depth-first tree traversal for compacting copy. More...
 
static int mdb_env_copyfd1 (MDB_env *env, int fd)
 Copy environment with compaction. More...
 
static int mdb_env_copyfd0 (MDB_env *env, int fd)
 Copy environment as-is. More...
 
int mdb_env_copyfd2 (MDB_env *env, int fd, unsigned int flags)
 Copy an LMDB environment to the specified file descriptor, with options. More...
 
int mdb_env_copyfd (MDB_env *env, int fd)
 Copy an LMDB environment to the specified file descriptor. More...
 
int mdb_env_copy2 (MDB_env *env, const char *path, unsigned int flags)
 Copy an LMDB environment to the specified path, with options. More...
 
int mdb_env_copy (MDB_env *env, const char *path)
 Copy an LMDB environment to the specified path. More...
 
int mdb_env_set_flags (MDB_env *env, unsigned int flag, int onoff)
 Set environment flags. More...
 
int mdb_env_get_flags (MDB_env *env, unsigned int *arg)
 Get environment flags. More...
 
int mdb_env_set_userctx (MDB_env *env, void *ctx)
 Set application information associated with the MDB_env. More...
 
void * mdb_env_get_userctx (MDB_env *env)
 Get the application information associated with the MDB_env. More...
 
int mdb_env_set_assert (MDB_env *env, MDB_assert_func *func)
 Set or reset the assert() callback of the environment. More...
 
int mdb_env_get_path (MDB_env *env, const char **arg)
 Return the path that was used in mdb_env_open(). More...
 
int mdb_env_get_fd (MDB_env *env, mdb_filehandle_t *arg)
 Return the filedescriptor for the given environment. More...
 
static int mdb_stat0 (MDB_env *env, MDB_db *db, MDB_stat *arg)
 Common code for mdb_stat() and mdb_env_stat(). More...
 
int mdb_env_stat (MDB_env *env, MDB_stat *arg)
 Return statistics about the LMDB environment. More...
 
int mdb_env_info (MDB_env *env, MDB_envinfo *arg)
 Return information about the LMDB environment. More...
 
int mdb_dbi_open (MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi)
 Open a database in the environment. More...
 
int mdb_stat (MDB_txn *txn, MDB_dbi dbi, MDB_stat *arg)
 Retrieve statistics for a database. More...
 
void mdb_dbi_close (MDB_env *env, MDB_dbi dbi)
 Close a database handle. More...
 
int mdb_dbi_flags (MDB_txn *txn, MDB_dbi dbi, unsigned int *flags)
 Retrieve the DB flags for a database handle. More...
 
int mdb_drop (MDB_txn *txn, MDB_dbi dbi, int del)
 Empty or delete+close a database. More...
 
int mdb_set_compare (MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
 Set a custom key comparison function for a database. More...
 
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. More...
 
int mdb_set_relfunc (MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel)
 Set a relocation function for a MDB_FIXEDMAP database. More...
 
int mdb_set_relctx (MDB_txn *txn, MDB_dbi dbi, void *ctx)
 Set a context pointer for a MDB_FIXEDMAP database's relocation function. More...
 
int mdb_env_get_maxkeysize (MDB_env *env)
 Get the maximum size of keys and MDB_DUPSORT data we can write. More...
 
int mdb_reader_list (MDB_env *env, MDB_msg_func *func, void *ctx)
 Dump the entries in the reader lock table. More...
 
static int mdb_pid_insert (pid_t *ids, pid_t pid)
 Insert pid into list if not already present. More...
 
int mdb_reader_check (MDB_env *env, int *dead)
 Check for stale entries in the reader lock table. More...
 

Variables

static char *const mdb_errstr []
 Table of descriptions for LMDB Return Codes. More...
 
static const mdb_nchar_t *const mdb_suffixes [2][2]
 Filename suffixes [datafile,lockfile][without,with MDB_NOSUBDIR]. More...
 

Detailed Description

Lightning memory-mapped database library.

A Btree-based database management library modeled loosely on the BerkeleyDB API, but much simplified.

Definition in file mdb.c.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE   1

Definition at line 36 of file mdb.c.

◆ BIG_ENDIAN

#define BIG_ENDIAN   __BIG_ENDIAN

Definition at line 184 of file mdb.c.

◆ BYTE_ORDER

#define BYTE_ORDER   __BYTE_ORDER

Definition at line 176 of file mdb.c.

◆ CACHEFLUSH

#define CACHEFLUSH (   addr,
  bytes,
  cache 
)

Definition at line 84 of file mdb.c.

◆ CALL_CONV

#define CALL_CONV

Definition at line 214 of file mdb.c.

◆ DO_PWRITE

#define DO_PWRITE (   rc,
  fd,
  ptr,
  size,
  len,
  pos 
)
Value:
do { \
len = pwrite(fd, ptr, size, pos); \
if (len == -1 && ErrCode() == EINTR) continue; \
rc = (len >= 0); break; } while(1)
#define ErrCode()
Get the error code for the last failed system function.
Definition: mdb.c:377
int len
const struct ncbi::grid::netcache::search::fields::SIZE size

◆ DO_WRITE [1/2]

#define DO_WRITE (   rc,
  fd,
  ptr,
  w2,
  len 
)    len = write(fd, ptr, w2); rc = (len >= 0)

◆ DO_WRITE [2/2]

#define DO_WRITE (   rc,
  fd,
  ptr,
  w2,
  len 
)    len = write(fd, ptr, w2); rc = (len >= 0)

◆ ESECT

#define ESECT

Definition at line 208 of file mdb.c.

◆ LITTLE_ENDIAN

#define LITTLE_ENDIAN   __LITTLE_ENDIAN

Definition at line 181 of file mdb.c.

◆ MDB_ENV_ACTIVE

#define MDB_ENV_ACTIVE   0x20000000U

Some fields are initialized.

Definition at line 1266 of file mdb.c.

◆ MDB_ENV_TXKEY

#define MDB_ENV_TXKEY   0x10000000U

me_txkey is set

Definition at line 1268 of file mdb.c.

◆ MDB_ERRCODE_ROFS

#define MDB_ERRCODE_ROFS   EROFS

◆ MDB_FATAL_ERROR

#define MDB_FATAL_ERROR   0x80000000U

Failed to update the meta page.

Probably an I/O error.

Definition at line 1264 of file mdb.c.

◆ MDB_FSYNCONLY

#define MDB_FSYNCONLY   0x08000000U

fdatasync is unreliable

Definition at line 1270 of file mdb.c.

◆ MDB_PID_T

#define MDB_PID_T   pid_t

Definition at line 67 of file mdb.c.

◆ MDB_THR_T

#define MDB_THR_T   pthread_t

Definition at line 68 of file mdb.c.

◆ MDB_USE_POSIX_MUTEX

#define MDB_USE_POSIX_MUTEX   1

Definition at line 141 of file mdb.c.

◆ me_pghead

#define me_pghead   me_pgstate.mf_pghead

Definition at line 1297 of file mdb.c.

◆ me_pglast

#define me_pglast   me_pgstate.mf_pglast

Definition at line 1296 of file mdb.c.

◆ me_rmutex

#define me_rmutex   me_txns->mti_rmutex

Shared reader lock.

Definition at line 1315 of file mdb.c.

◆ me_wmutex

#define me_wmutex   me_txns->mti_wmutex

Shared writer lock.

Definition at line 1316 of file mdb.c.

◆ mm_flags

#define mm_flags   mm_dbs[FREE_DBI].md_flags

Any persistent environment flags.

Environment Flags

Definition at line 1048 of file mdb.c.

◆ mm_psize

#define mm_psize   mm_dbs[FREE_DBI].md_pad

The size of pages used in this DB.

Definition at line 1046 of file mdb.c.

◆ mp_lower

#define mp_lower   mp_pb.pb.pb_lower

Definition at line 829 of file mdb.c.

◆ mp_next

#define mp_next   mp_p.p_next

Definition at line 807 of file mdb.c.

◆ mp_pages

#define mp_pages   mp_pb.pb_pages

Definition at line 831 of file mdb.c.

◆ mp_pgno

#define mp_pgno   mp_p.p_pgno

Definition at line 806 of file mdb.c.

◆ mp_upper

#define mp_upper   mp_pb.pb.pb_upper

Definition at line 830 of file mdb.c.

◆ mr_pid

#define mr_pid   mru.mrx.mrb_pid

Definition at line 707 of file mdb.c.

◆ mr_tid

#define mr_tid   mru.mrx.mrb_tid

Definition at line 708 of file mdb.c.

◆ mr_txnid

#define mr_txnid   mru.mrx.mrb_txnid

shorthand for mrb_txnid

Definition at line 706 of file mdb.c.

◆ mti_format

#define mti_format   mt1.mtb.mtb_format

Definition at line 759 of file mdb.c.

◆ mti_magic

#define mti_magic   mt1.mtb.mtb_magic

Definition at line 758 of file mdb.c.

◆ mti_numreaders

#define mti_numreaders   mt1.mtb.mtb_numreaders

Definition at line 763 of file mdb.c.

◆ mti_rmname

#define mti_rmname   mt1.mtb.mtb_rmname

Definition at line 761 of file mdb.c.

◆ mti_rmutex

#define mti_rmutex   mt1.mtb.mtb_rmutex

Definition at line 760 of file mdb.c.

◆ mti_txnid

#define mti_txnid   mt1.mtb.mtb_txnid

Definition at line 762 of file mdb.c.

◆ mti_wmutex

#define mti_wmutex   mt2.mt2_wmutex

Definition at line 772 of file mdb.c.

◆ VGMEMP_ALLOC

#define VGMEMP_ALLOC (   h,
  a,
 
)

Definition at line 159 of file mdb.c.

◆ VGMEMP_CREATE

#define VGMEMP_CREATE (   h,
  r,
 
)

Definition at line 158 of file mdb.c.

◆ VGMEMP_DEFINED

#define VGMEMP_DEFINED (   a,
 
)

Definition at line 162 of file mdb.c.

◆ VGMEMP_DESTROY

#define VGMEMP_DESTROY (   h)

Definition at line 161 of file mdb.c.

◆ VGMEMP_FREE

#define VGMEMP_FREE (   h,
  a 
)

Definition at line 160 of file mdb.c.

Modified on Wed May 22 11:35:51 2024 by modify_doxy.py rev. 669887