171 #include <sys/types.h>
172 #include <inttypes.h>
181 typedef int mdb_mode_t;
183 typedef mode_t mdb_mode_t;
187 # define MDB_FMT_Z "I"
189 # define MDB_FMT_Z "z"
192 #if !defined(MDB_RPAGE_CACHE) || (defined(MDB_VL32) && !(MDB_RPAGE_CACHE))
194 #define MDB_RPAGE_CACHE 1
203 typedef size_t mdb_size_t;
204 # define MDB_SIZE_MAX SIZE_MAX
206 # define MDB_PRIy(t) MDB_FMT_Z #t
208 # define MDB_SCNy(t) MDB_FMT_Z #t
210 typedef uint64_t mdb_size_t;
211 # define MDB_SIZE_MAX UINT64_MAX
212 # define MDB_PRIy(t) PRI##t##64
213 # define MDB_SCNy(t) SCN##t##64
214 # define mdb_env_create mdb_env_create_vl32
222 typedef void *mdb_filehandle_t;
224 typedef int mdb_filehandle_t;
235 #define MDB_VERSION_MAJOR 1
237 #define MDB_VERSION_MINOR 0
239 #define MDB_VERSION_PATCH 0
242 #define MDB_VERINT(a,b,c) (((a) << 24) | ((b) << 16) | (c))
245 #define MDB_VERSION_FULL \
246 MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH)
249 #define MDB_VERSION_DATE "June 30, 2026"
252 #define MDB_VERSTR(a,b,c,d) "LMDB " #a "." #b "." #c ": (" d ")"
255 #define MDB_VERFOO(a,b,c,d) MDB_VERSTR(a,b,c,d)
258 #define MDB_VERSION_STRING \
259 MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE)
348 #define MDB_FIXEDMAP 0x01
350 #define MDB_ENCRYPT 0x2000U
352 #define MDB_NOSUBDIR 0x4000
354 #define MDB_NOSYNC 0x10000
356 #define MDB_RDONLY 0x20000
358 #define MDB_NOMETASYNC 0x40000
360 #define MDB_WRITEMAP 0x80000
362 #define MDB_MAPASYNC 0x100000
364 #define MDB_NOTLS 0x200000
366 #define MDB_NOLOCK 0x400000
368 #define MDB_NORDAHEAD 0x800000
370 #define MDB_NOMEMINIT 0x1000000
372 #define MDB_PREVSNAPSHOT 0x2000000
374 #define MDB_REMAP_CHUNKS 0x4000000
381 #define MDB_REVERSEKEY 0x02
383 #define MDB_DUPSORT 0x04
387 #define MDB_INTEGERKEY 0x08
389 #define MDB_DUPFIXED 0x10
391 #define MDB_INTEGERDUP 0x20
393 #define MDB_REVERSEDUP 0x40
395 #define MDB_CREATE 0x40000
402 #define MDB_NOOVERWRITE 0x10
407 #define MDB_NODUPDATA 0x20
409 #define MDB_CURRENT 0x40
413 #define MDB_RESERVE 0x10000
415 #define MDB_APPEND 0x20000
417 #define MDB_APPENDDUP 0x40000
419 #define MDB_MULTIPLE 0x80000
428 #define MDB_CP_COMPACT 0x01
473 #define MDB_SUCCESS 0
475 #define MDB_KEYEXIST (-30799)
477 #define MDB_NOTFOUND (-30798)
479 #define MDB_PAGE_NOTFOUND (-30797)
481 #define MDB_CORRUPTED (-30796)
483 #define MDB_PANIC (-30795)
485 #define MDB_VERSION_MISMATCH (-30794)
487 #define MDB_INVALID (-30793)
489 #define MDB_MAP_FULL (-30792)
491 #define MDB_DBS_FULL (-30791)
493 #define MDB_READERS_FULL (-30790)
495 #define MDB_TLS_FULL (-30789)
497 #define MDB_TXN_FULL (-30788)
499 #define MDB_CURSOR_FULL (-30787)
501 #define MDB_PAGE_FULL (-30786)
503 #define MDB_MAP_RESIZED (-30785)
512 #define MDB_INCOMPATIBLE (-30784)
514 #define MDB_BAD_RSLOT (-30783)
516 #define MDB_BAD_TXN (-30782)
518 #define MDB_BAD_VALSIZE (-30781)
520 #define MDB_BAD_DBI (-30780)
522 #define MDB_PROBLEM (-30779)
524 #define MDB_BAD_CHECKSUM (-30778)
526 #define MDB_CRYPTO_FAIL (-30777)
528 #define MDB_ENV_ENCRYPTION (-30776)
530 #define MDB_TXN_PENDING (-30775)
532 #define MDB_CANT_ROLLBACK (-30774)
534 #define MDB_DBIS_BUSY (-30773)
536 #define MDB_SHORT_WRITE (-30772)
538 #define MDB_ENV_BUSY (-30771)
540 #define MDB_IS_READONLY (-30770)
542 #define MDB_ADDR_BUSY (-30769)
544 #define MDB_LAST_ERRCODE MDB_ADDR_BUSY
575 char *
mdb_version(
int *major,
int *minor,
int *patch);
1303 #define mdb_open(txn,name,flags,dbi) mdb_dbi_open(txn,name,flags,dbi)
1305 #define mdb_close(env,dbi) mdb_dbi_close(env,dbi)
1588 unsigned int flags);
1765 unsigned int flags);
1899 void *
mdb_modload(
const char *file,
const char *symname,
int() MDB_str2key_func(const char *passwd, MDB_val *key)
A function for converting a string into an encryption key.
Definition: lmdb.h:1866
void mdb_modunload(void *handle)
Unload a dynamically loaded module.
Definition: module.c:72
MDB_crypto_funcs *() MDB_crypto_hooks(void)
The function that returns the MDB_crypto_funcs structure.
Definition: lmdb.h:1888
void mdb_modsetup(MDB_env *env, MDB_crypto_funcs *mcf_ptr, const char *passphrase)
Set an environment to use the given crypto functions.
Definition: module.c:82
void * mdb_modload(const char *file, const char *symname, MDB_crypto_funcs **mcf_ptr, char **errmsg)
Load a dynamically loadable module.
Definition: module.c:26
A structure for dynamically loaded crypto modules.
Definition: lmdb.h:1873
Opaque structure for navigating through a database.
Definition: mdb.c:1562
Opaque structure for a database environment.
Definition: mdb.c:1653
Opaque structure for a transaction handle.
Definition: mdb.c:1414
int mdb_env_incr_dump(MDB_env *env, const char *path, size_t txnid)
Perform incremental dump of an LMDB environment to the specified file.
Definition: mdb.c:11908
void mdb_txn_reset(MDB_txn *txn)
Reset a non-nested read-only transaction.
Definition: mdb.c:3812
int() MDB_msg_func(const char *msg, void *ctx)
A callback function used to print a message from the library.
Definition: lmdb.h:1834
int mdb_env_set_encrypt(MDB_env *env, MDB_enc_func *func, const MDB_val *key, unsigned int size)
Set encryption on an environment.
Definition: mdb.c:12099
char * mdb_version(int *major, int *minor, int *patch)
Return the LMDB library version information.
Definition: mdb.c:1887
MDB_cursor_op
Cursor Get operations.
Definition: lmdb.h:436
int mdb_cursor_count(MDB_cursor *cursor, mdb_size_t *countp)
Return count of duplicates for current key.
Definition: mdb.c:9719
int mdb_env_info(MDB_env *env, MDB_envinfo *stat)
Return information about the LMDB environment.
Definition: mdb.c:12200
int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data, unsigned int flags)
Store by cursor.
Definition: mdb.c:9076
int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags)
Delete current key/data pair.
Definition: mdb.c:9193
int mdb_env_get_flags(MDB_env *env, unsigned int *flags)
Get environment flags.
Definition: mdb.c:12062
MDB_dbi mdb_cursor_dbi(MDB_cursor *cursor)
Return the cursor's database handle.
Definition: mdb.c:9786
int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode)
Open an environment handle.
Definition: mdb.c:6231
int mdb_reader_check(MDB_env *env, int *dead)
Check for stale entries in the reader lock table.
Definition: mdb.c:12705
int mdb_env_copy2(MDB_env *env, const char *path, unsigned int flags)
Copy an LMDB environment to the specified path, with options.
Definition: mdb.c:11752
int mdb_env_incr_loadfd(MDB_env *env, mdb_filehandle_t fd)
Reload an incremental dump of an LMDB environment from the specified file descriptor.
void mdb_env_close(MDB_env *env)
Close the environment and release the memory map.
Definition: mdb.c:6564
void * mdb_env_get_userctx(MDB_env *env)
Get the application information associated with the MDB_env.
Definition: mdb.c:12081
int mdb_env_copyfd(MDB_env *env, mdb_filehandle_t fd)
Copy an LMDB environment to the specified file descriptor.
int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op op)
Retrieve by cursor.
Definition: mdb.c:8301
int mdb_env_set_mapsize(MDB_env *env, mdb_size_t size)
Set the size of the memory map to use for this environment.
Definition: mdb.c:5153
int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data, unsigned int flags)
Store items into a database.
Definition: mdb.c:11082
int mdb_env_copyfd2(MDB_env *env, mdb_filehandle_t fd, unsigned int flags)
Copy an LMDB environment to the specified file descriptor, with options.
void mdb_dbi_close(MDB_env *env, MDB_dbi dbi)
Close a database handle. Normally unnecessary. Use with care:
Definition: mdb.c:12380
char * mdb_strerror(int err)
Return a string describing a given error code.
Definition: mdb.c:1931
int mdb_env_copy(MDB_env *env, const char *path)
Copy an LMDB environment to the specified path.
Definition: mdb.c:11769
int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
Set a custom key comparison function for a database.
Definition: mdb.c:12584
int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel)
Set a relocation function for a MDB_FIXEDMAP database.
Definition: mdb.c:12602
int mdb_txn_renew(MDB_txn *txn)
Renew a non-nested read-only transaction.
Definition: mdb.c:3437
int mdb_env_set_assert(MDB_env *env, MDB_assert_func *func)
Definition: mdb.c:12087
int mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers)
Get the maximum number of threads/reader slots for the environment.
Definition: mdb.c:5215
void mdb_txn_abort(MDB_txn *txn)
Abandon all the operations of the transaction instead of saving them.
Definition: mdb.c:3845
int mdb_txn_prepare(MDB_txn *txn)
Prepare to commit all the operations of a transaction into the database.
Definition: mdb.c:4628
mdb_size_t mdb_txn_id(MDB_txn *txn)
Return the transaction's ID.
Definition: mdb.c:3644
MDB_txn * mdb_cursor_txn(MDB_cursor *cursor)
Return the cursor's transaction handle.
Definition: mdb.c:9779
int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx)
Set a context pointer for a MDB_FIXEDMAP database's relocation function.
Definition: mdb.c:12611
int mdb_env_incr_dumpfd(MDB_env *env, mdb_filehandle_t fd, size_t txnid)
Perform incremental dump of an LMDB environment to the specified file descriptor.
int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff)
Set environment flags.
Definition: mdb.c:12050
int mdb_txn_commit(MDB_txn *txn)
Commit all the operations of a transaction into the database.
Definition: mdb.c:4621
int mdb_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx)
Dump the entries in the reader lock table.
Definition: mdb.c:12627
int mdb_env_sync(MDB_env *env, int force)
Flush the data buffers to disk.
Definition: mdb.c:3141
int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data)
Get items from a database.
Definition: mdb.c:7747
int mdb_env_set_checksum(MDB_env *env, MDB_sum_func *func, unsigned int size)
Set checksums on an environment.
Definition: mdb.c:12122
void MDB_assert_func(MDB_env *env, const char *msg)
A callback function for most LMDB assert() failures, called before printing the message and aborting.
Definition: lmdb.h:1085
int mdb_dbi_flags(MDB_txn *txn, MDB_dbi dbi, unsigned int *flags)
Retrieve the DB flags for a database handle.
Definition: mdb.c:12397
int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor)
Create a cursor handle.
Definition: mdb.c:9667
int mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs)
Set the maximum number of named databases for the environment.
Definition: mdb.c:5195
int mdb_env_create(MDB_env **env)
Create an LMDB environment handle.
Definition: mdb.c:4998
int mdb_env_set_pagesize(MDB_env *env, int size)
Set the size of DB pages in bytes.
Definition: mdb.c:12155
int mdb_env_get_maxkeysize(MDB_env *env)
Get the maximum size of keys and MDB_DUPSORT data we can write.
Definition: mdb.c:12621
int mdb_cursor_is_db(MDB_cursor *cursor)
Check if the cursor is pointing to a named database record.
Definition: mdb.c:9792
int mdb_txn_flags(MDB_txn *txn, unsigned int *flags)
Retrieve the transaction's flags.
Definition: mdb.c:3650
int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data)
Delete items from a database.
Definition: mdb.c:10585
int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del)
Empty or delete+close a database.
Definition: mdb.c:12508
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.
Definition: mdb.c:2212
int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi)
Open a database in the environment.
Definition: mdb.c:12241
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.
Definition: mdb.c:2218
int mdb_env_get_path(MDB_env *env, const char **path)
Return the path that was used in mdb_env_open().
Definition: mdb.c:12135
int mdb_cursor_renew(MDB_txn *txn, MDB_cursor *cursor)
Renew a cursor handle.
Definition: mdb.c:9702
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.
Definition: mdb.c:12593
void mdb_cursor_close(MDB_cursor *cursor)
Close a cursor handle.
Definition: mdb.c:9757
int mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn)
Create a transaction for use with the environment.
Definition: mdb.c:3481
int mdb_env_set_maxreaders(MDB_env *env, unsigned int readers)
Set the maximum number of threads/reader slots for the environment.
Definition: mdb.c:5205
int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat)
Retrieve statistics for a database.
Definition: mdb.c:12363
MDB_env * mdb_txn_env(MDB_txn *txn)
Returns the transaction's MDB_env.
Definition: mdb.c:3637
int mdb_env_get_fd(MDB_env *env, mdb_filehandle_t *fd)
Return the filedescriptor for the given environment.
Definition: mdb.c:12145
int mdb_env_set_userctx(MDB_env *env, void *ctx)
Set application information associated with the MDB_env.
Definition: mdb.c:12072
int mdb_env_stat(MDB_env *env, MDB_stat *stat)
Return statistics about the LMDB environment.
Definition: mdb.c:12187
int mdb_env_rollback(MDB_env *env, mdb_size_t txnid)
Rollback the last committed transaction in the environment.
Definition: mdb.c:4637
@ MDB_PREV_MULTIPLE
Definition: lmdb.h:463
@ MDB_SET
Definition: lmdb.h:460
@ MDB_NEXT_DUP
Definition: lmdb.h:450
@ MDB_GET_BOTH_RANGE
Definition: lmdb.h:441
@ MDB_NEXT_MULTIPLE
Definition: lmdb.h:452
@ MDB_GET_CURRENT
Definition: lmdb.h:442
@ MDB_SET_KEY
Definition: lmdb.h:461
@ MDB_FIRST_DUP
Definition: lmdb.h:438
@ MDB_FIRST
Definition: lmdb.h:437
@ MDB_LAST_DUP
Definition: lmdb.h:447
@ MDB_GET_MULTIPLE
Definition: lmdb.h:443
@ MDB_NEXT
Definition: lmdb.h:449
@ MDB_PREV
Definition: lmdb.h:456
@ MDB_PREV_DUP
Definition: lmdb.h:457
@ MDB_NEXT_NODUP
Definition: lmdb.h:455
@ MDB_PREV_NODUP
Definition: lmdb.h:459
@ MDB_LAST
Definition: lmdb.h:446
@ MDB_GET_BOTH
Definition: lmdb.h:440
@ MDB_SET_RANGE
Definition: lmdb.h:462
Information about the environment.
Definition: lmdb.h:559
Statistics for a database in the environment.
Definition: lmdb.h:548
unsigned int me_maxreaders
Definition: lmdb.h:564
mdb_size_t me_mapsize
Definition: lmdb.h:561
mdb_size_t me_last_pgno
Definition: lmdb.h:562
int mcf_sumsize
Definition: lmdb.h:1879
mdb_size_t ms_entries
Definition: lmdb.h:555
int mcf_esumsize
Definition: lmdb.h:1878
int mcf_keysize
Definition: lmdb.h:1877
unsigned int ms_psize
Definition: lmdb.h:549
mdb_size_t ms_branch_pages
Definition: lmdb.h:552
void * mv_data
Definition: lmdb.h:295
size_t mv_size
Definition: lmdb.h:294
mdb_size_t me_last_txnid
Definition: lmdb.h:563
unsigned int ms_depth
Definition: lmdb.h:551
mdb_size_t ms_leaf_pages
Definition: lmdb.h:553
void * me_mapaddr
Definition: lmdb.h:560
unsigned int me_numreaders
Definition: lmdb.h:565
mdb_size_t ms_overflow_pages
Definition: lmdb.h:554
void() MDB_rel_func(MDB_val *item, void *oldptr, void *newptr, void *relctx)
A callback function used to relocate a position-dependent data item in a fixed-address database.
Definition: lmdb.h:315
int() MDB_enc_func(const MDB_val *src, MDB_val *dst, const MDB_val *key, int encdec)
A callback function used to encrypt/decrypt pages in the env.
Definition: lmdb.h:330
int() MDB_cmp_func(const MDB_val *a, const MDB_val *b)
A callback function used to compare two keys in a database.
Definition: lmdb.h:299
unsigned int MDB_dbi
A handle for an individual database in the DB environment.
Definition: lmdb.h:277
void() MDB_sum_func(const MDB_val *src, MDB_val *dst, const MDB_val *key)
A callback function used to checksum pages in the env.
Definition: lmdb.h:341
Generic structure used for passing keys and data in and out of the database.
Definition: lmdb.h:293