LMDB
lmdb.h
Go to the documentation of this file.
1 
156 #ifndef _LMDB_H_
157 #define _LMDB_H_
158 
159 #include <sys/types.h>
160 
161 #ifdef __cplusplus
162 extern "C" {
163 #endif
164 
166 #ifdef _MSC_VER
167 typedef int mdb_mode_t;
168 #else
169 typedef mode_t mdb_mode_t;
170 #endif
171 
176 #ifdef _WIN32
177 typedef void *mdb_filehandle_t;
178 #else
179 typedef int mdb_filehandle_t;
180 #endif
181 
190 #define MDB_VERSION_MAJOR 0
191 
192 #define MDB_VERSION_MINOR 9
193 
194 #define MDB_VERSION_PATCH 17
195 
197 #define MDB_VERINT(a,b,c) (((a) << 24) | ((b) << 16) | (c))
198 
200 #define MDB_VERSION_FULL \
201  MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH)
202 
204 #define MDB_VERSION_DATE "November 30, 2015"
205 
207 #define MDB_VERSTR(a,b,c,d) "LMDB " #a "." #b "." #c ": (" d ")"
208 
210 #define MDB_VERFOO(a,b,c,d) MDB_VERSTR(a,b,c,d)
211 
213 #define MDB_VERSION_STRING \
214  MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE)
215 
222 typedef struct MDB_env MDB_env;
223 
229 typedef struct MDB_txn MDB_txn;
230 
232 typedef unsigned int MDB_dbi;
233 
235 typedef struct MDB_cursor MDB_cursor;
236 
248 typedef struct MDB_val {
249  size_t mv_size;
250  void *mv_data;
251 } MDB_val;
252 
254 typedef int (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
255 
270 typedef void (MDB_rel_func)(MDB_val *item, void *oldptr, void *newptr, void *relctx);
271 
276 #define MDB_FIXEDMAP 0x01
277 
278 #define MDB_NOSUBDIR 0x4000
279 
280 #define MDB_NOSYNC 0x10000
281 
282 #define MDB_RDONLY 0x20000
283 
284 #define MDB_NOMETASYNC 0x40000
285 
286 #define MDB_WRITEMAP 0x80000
287 
288 #define MDB_MAPASYNC 0x100000
289 
290 #define MDB_NOTLS 0x200000
291 
292 #define MDB_NOLOCK 0x400000
293 
294 #define MDB_NORDAHEAD 0x800000
295 
296 #define MDB_NOMEMINIT 0x1000000
297 
303 #define MDB_REVERSEKEY 0x02
304 
305 #define MDB_DUPSORT 0x04
306 
308 #define MDB_INTEGERKEY 0x08
309 
310 #define MDB_DUPFIXED 0x10
311 
312 #define MDB_INTEGERDUP 0x20
313 
314 #define MDB_REVERSEDUP 0x40
315 
316 #define MDB_CREATE 0x40000
317 
323 #define MDB_NOOVERWRITE 0x10
324 
328 #define MDB_NODUPDATA 0x20
329 
330 #define MDB_CURRENT 0x40
331 
334 #define MDB_RESERVE 0x10000
335 
336 #define MDB_APPEND 0x20000
337 
338 #define MDB_APPENDDUP 0x40000
339 
340 #define MDB_MULTIPLE 0x80000
341 /* @} */
342 
349 #define MDB_CP_COMPACT 0x01
350 /* @} */
351 
357 typedef enum MDB_cursor_op {
384 } MDB_cursor_op;
385 
392 #define MDB_SUCCESS 0
393 
394 #define MDB_KEYEXIST (-30799)
395 
396 #define MDB_NOTFOUND (-30798)
397 
398 #define MDB_PAGE_NOTFOUND (-30797)
399 
400 #define MDB_CORRUPTED (-30796)
401 
402 #define MDB_PANIC (-30795)
403 
404 #define MDB_VERSION_MISMATCH (-30794)
405 
406 #define MDB_INVALID (-30793)
407 
408 #define MDB_MAP_FULL (-30792)
409 
410 #define MDB_DBS_FULL (-30791)
411 
412 #define MDB_READERS_FULL (-30790)
413 
414 #define MDB_TLS_FULL (-30789)
415 
416 #define MDB_TXN_FULL (-30788)
417 
418 #define MDB_CURSOR_FULL (-30787)
419 
420 #define MDB_PAGE_FULL (-30786)
421 
422 #define MDB_MAP_RESIZED (-30785)
423 
431 #define MDB_INCOMPATIBLE (-30784)
432 
433 #define MDB_BAD_RSLOT (-30783)
434 
435 #define MDB_BAD_TXN (-30782)
436 
437 #define MDB_BAD_VALSIZE (-30781)
438 
439 #define MDB_BAD_DBI (-30780)
440 
441 #define MDB_LAST_ERRCODE MDB_BAD_DBI
442 
445 typedef struct MDB_stat {
446  unsigned int ms_psize;
448  unsigned int ms_depth;
450  size_t ms_leaf_pages;
452  size_t ms_entries;
453 } MDB_stat;
454 
456 typedef struct MDB_envinfo {
457  void *me_mapaddr;
458  size_t me_mapsize;
459  size_t me_last_pgno;
460  size_t me_last_txnid;
461  unsigned int me_maxreaders;
462  unsigned int me_numreaders;
463 } MDB_envinfo;
464 
472 char *mdb_version(int *major, int *minor, int *patch);
473 
484 char *mdb_strerror(int err);
485 
497 int mdb_env_create(MDB_env **env);
498 
618 int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode);
619 
634 int mdb_env_copy(MDB_env *env, const char *path);
635 
649 int mdb_env_copyfd(MDB_env *env, mdb_filehandle_t fd);
650 
673 int mdb_env_copy2(MDB_env *env, const char *path, unsigned int flags);
674 
692 int mdb_env_copyfd2(MDB_env *env, mdb_filehandle_t fd, unsigned int flags);
693 
700 int mdb_env_stat(MDB_env *env, MDB_stat *stat);
701 
708 int mdb_env_info(MDB_env *env, MDB_envinfo *stat);
709 
729 int mdb_env_sync(MDB_env *env, int force);
730 
739 void mdb_env_close(MDB_env *env);
740 
755 int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
756 
767 int mdb_env_get_flags(MDB_env *env, unsigned int *flags);
768 
781 int mdb_env_get_path(MDB_env *env, const char **path);
782 
793 int mdb_env_get_fd(MDB_env *env, mdb_filehandle_t *fd);
794 
827 int mdb_env_set_mapsize(MDB_env *env, size_t size);
828 
846 int mdb_env_set_maxreaders(MDB_env *env, unsigned int readers);
847 
858 int mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers);
859 
878 int mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
879 
888 
895 int mdb_env_set_userctx(MDB_env *env, void *ctx);
896 
902 void *mdb_env_get_userctx(MDB_env *env);
903 
910 typedef void MDB_assert_func(MDB_env *env, const char *msg);
911 
920 
955 int mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn);
956 
962 
972 size_t mdb_txn_id(MDB_txn *txn);
973 
990 int mdb_txn_commit(MDB_txn *txn);
991 
1000 void mdb_txn_abort(MDB_txn *txn);
1001 
1019 void mdb_txn_reset(MDB_txn *txn);
1020 
1035 int mdb_txn_renew(MDB_txn *txn);
1036 
1038 #define mdb_open(txn,name,flags,dbi) mdb_dbi_open(txn,name,flags,dbi)
1039 
1040 #define mdb_close(env,dbi) mdb_dbi_close(env,dbi)
1041 
1109 int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
1110 
1123 int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
1124 
1132 int mdb_dbi_flags(MDB_txn *txn, MDB_dbi dbi, unsigned int *flags);
1133 
1150 void mdb_dbi_close(MDB_env *env, MDB_dbi dbi);
1151 
1161 int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del);
1162 
1182 int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
1183 
1205 int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
1206 
1225 int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
1226 
1241 int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx);
1242 
1269 int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
1270 
1318 int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
1319  unsigned int flags);
1320 
1343 int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
1344 
1367 int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
1368 
1375 void mdb_cursor_close(MDB_cursor *cursor);
1376 
1393 int mdb_cursor_renew(MDB_txn *txn, MDB_cursor *cursor);
1394 
1400 
1405 MDB_dbi mdb_cursor_dbi(MDB_cursor *cursor);
1406 
1426 int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1427  MDB_cursor_op op);
1428 
1487 int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1488  unsigned int flags);
1489 
1507 int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
1508 
1521 int mdb_cursor_count(MDB_cursor *cursor, size_t *countp);
1522 
1533 int mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
1534 
1545 int mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
1546 
1553 typedef int (MDB_msg_func)(const char *msg, void *ctx);
1554 
1562 int mdb_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx);
1563 
1570 int mdb_reader_check(MDB_env *env, int *dead);
1573 #ifdef __cplusplus
1574 }
1575 #endif
1576 
1584 #endif /* _LMDB_H_ */
Statistics for a database in the environment.
Definition: lmdb.h:445
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:9777
size_t me_mapsize
Definition: lmdb.h:458
void mdb_cursor_close(MDB_cursor *cursor)
Close a cursor handle.
Definition: mdb.c:7460
int mdb_env_set_assert(MDB_env *env, MDB_assert_func *func)
Definition: mdb.c:9367
Definition: lmdb.h:370
int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data)
Delete items from a database.
Definition: mdb.c:8251
Definition: lmdb.h:364
int mdb_env_set_mapsize(MDB_env *env, size_t size)
Set the size of the memory map to use for this environment.
Definition: mdb.c:3963
Definition: lmdb.h:371
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:9471
Definition: lmdb.h:373
char * mdb_version(int *major, int *minor, int *patch)
Return the LMDB library version information.
Definition: mdb.c:1380
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:254
size_t ms_entries
Definition: lmdb.h:452
int mdb_cursor_count(MDB_cursor *cursor, size_t *countp)
Return count of duplicates for current key.
Definition: mdb.c:7428
int mdb_txn_commit(MDB_txn *txn)
Commit all the operations of a transaction into the database.
Definition: mdb.c:3354
int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data, unsigned int flags)
Store by cursor.
Definition: mdb.c:6278
int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat)
Retrieve statistics for a database.
Definition: mdb.c:9576
void mdb_dbi_close(MDB_env *env, MDB_dbi dbi)
Close a database handle. Normally unnecessary. Use with care:
Definition: mdb.c:9593
size_t ms_overflow_pages
Definition: lmdb.h:451
void * mdb_env_get_userctx(MDB_env *env)
Get the application information associated with the MDB_env.
Definition: mdb.c:9361
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:4015
size_t mv_size
Definition: lmdb.h:249
MDB_txn * mdb_cursor_txn(MDB_cursor *cursor)
Return the cursor's transaction handle.
Definition: mdb.c:7475
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:9768
unsigned int ms_psize
Definition: lmdb.h:446
Opaque structure for a transaction handle.
Definition: mdb.c:1037
int mdb_env_info(MDB_env *env, MDB_envinfo *stat)
Return information about the LMDB environment.
Definition: mdb.c:9430
Opaque structure for navigating through a database.
Definition: mdb.c:1136
int mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs)
Set the maximum number of named databases for the environment.
Definition: mdb.c:3997
int mdb_env_get_maxkeysize(MDB_env *env)
Get the maximum size of keys and MDB_DUPSORT data we can write.
Definition: mdb.c:9796
MDB_env * mdb_txn_env(MDB_txn *txn)
Returns the transaction's MDB_env.
Definition: mdb.c:2867
Information about the environment.
Definition: lmdb.h:456
void * mv_data
Definition: lmdb.h:250
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:910
void * me_mapaddr
Definition: lmdb.h:457
Definition: lmdb.h:377
Definition: lmdb.h:362
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:1685
MDB_dbi mdb_cursor_dbi(MDB_cursor *cursor)
Return the cursor's database handle.
Definition: mdb.c:7482
int mdb_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx)
Dump the entries in the reader lock table.
Definition: mdb.c:9802
size_t ms_branch_pages
Definition: lmdb.h:449
int mdb_env_copyfd(MDB_env *env, mdb_filehandle_t fd)
Copy an LMDB environment to the specified file descriptor.
int mdb_env_get_path(MDB_env *env, const char **path)
Return the path that was used in mdb_env_open().
Definition: mdb.c:9378
Definition: lmdb.h:368
int mdb_env_get_flags(MDB_env *env, unsigned int *flags)
Get environment flags.
Definition: mdb.c:9342
int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del)
Empty or delete+close a database.
Definition: mdb.c:9708
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.
unsigned int me_maxreaders
Definition: lmdb.h:461
size_t ms_leaf_pages
Definition: lmdb.h:450
int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode)
Open an environment handle.
Definition: mdb.c:4691
int mdb_env_copy(MDB_env *env, const char *path)
Copy an LMDB environment to the specified path.
Definition: mdb.c:9324
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:270
void mdb_txn_reset(MDB_txn *txn)
Reset a read-only transaction.
Definition: mdb.c:2985
Definition: lmdb.h:376
int mdb_dbi_flags(MDB_txn *txn, MDB_dbi dbi, unsigned int *flags)
Retrieve the DB flags for a database handle.
Definition: mdb.c:9609
Definition: lmdb.h:381
Definition: lmdb.h:358
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:9786
size_t me_last_pgno
Definition: lmdb.h:459
Definition: lmdb.h:380
int mdb_txn_renew(MDB_txn *txn)
Renew a read-only transaction.
Definition: mdb.c:2749
int mdb_reader_check(MDB_env *env, int *dead)
Check for stale entries in the reader lock table.
Definition: mdb.c:9880
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:1691
int mdb_cursor_renew(MDB_txn *txn, MDB_cursor *cursor)
Renew a cursor handle.
Definition: mdb.c:7411
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:9759
Generic structure used for passing keys and data in and out of the database.
Definition: lmdb.h:248
unsigned int MDB_dbi
A handle for an individual database in the DB environment.
Definition: lmdb.h:232
int mdb_env_sync(MDB_env *env, int force)
Flush the data buffers to disk.
Definition: mdb.c:2450
size_t me_last_txnid
Definition: lmdb.h:460
int mdb_filehandle_t
Definition: lmdb.h:179
int mdb_env_get_fd(MDB_env *env, mdb_filehandle_t *fd)
Return the filedescriptor for the given environment.
Definition: mdb.c:9388
size_t mdb_txn_id(MDB_txn *txn)
Return the transaction's ID.
Definition: mdb.c:2874
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:2766
MDB_cursor_op
Cursor Get operations.
Definition: lmdb.h:357
Definition: lmdb.h:378
int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op op)
Retrieve by cursor.
Definition: mdb.c:6074
int mdb_env_create(MDB_env **env)
Create an LMDB environment handle.
Definition: mdb.c:3854
unsigned int me_numreaders
Definition: lmdb.h:462
void mdb_txn_abort(MDB_txn *txn)
Abandon all the operations of the transaction instead of saving them.
Definition: mdb.c:2998
mode_t mdb_mode_t
Definition: lmdb.h:169
Definition: lmdb.h:382
Opaque structure for a database environment.
Definition: mdb.c:1194
Definition: lmdb.h:367
Definition: lmdb.h:363
Definition: lmdb.h:359
int mdb_env_stat(MDB_env *env, MDB_stat *stat)
Return statistics about the LMDB environment.
Definition: mdb.c:9417
char * mdb_strerror(int err)
Return a string describing a given error code.
Definition: mdb.c:1413
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:4006
Definition: lmdb.h:383
int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff)
Set environment flags.
Definition: mdb.c:9330
int mdb_env_set_userctx(MDB_env *env, void *ctx)
Set application information associated with the MDB_env.
Definition: mdb.c:9352
int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags)
Delete current key/data pair.
Definition: mdb.c:6808
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:9259
Definition: lmdb.h:361
int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data)
Get items from a database.
Definition: mdb.c:5548
unsigned int ms_depth
Definition: lmdb.h:448
int( MDB_msg_func)(const char *msg, void *ctx)
A callback function used to print a message from the library.
Definition: lmdb.h:1553
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:8740
void mdb_env_close(MDB_env *env)
Close the environment and release the memory map.
Definition: mdb.c:4940
int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor)
Create a cursor handle.
Definition: mdb.c:7377