LMDB
lmdb.h
Go to the documentation of this file.
1 
159 #ifndef _LMDB_H_
160 #define _LMDB_H_
161 
162 #include <sys/types.h>
163 
164 #ifdef __cplusplus
165 extern "C" {
166 #endif
167 
169 #ifdef _MSC_VER
170 typedef int mdb_mode_t;
171 #else
172 typedef mode_t mdb_mode_t;
173 #endif
174 
179 #ifdef _WIN32
180 typedef void *mdb_filehandle_t;
181 #else
182 typedef int mdb_filehandle_t;
183 #endif
184 
193 #define MDB_VERSION_MAJOR 0
194 
195 #define MDB_VERSION_MINOR 9
196 
197 #define MDB_VERSION_PATCH 18
198 
200 #define MDB_VERINT(a,b,c) (((a) << 24) | ((b) << 16) | (c))
201 
203 #define MDB_VERSION_FULL \
204  MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH)
205 
207 #define MDB_VERSION_DATE "February 5, 2016"
208 
210 #define MDB_VERSTR(a,b,c,d) "LMDB " #a "." #b "." #c ": (" d ")"
211 
213 #define MDB_VERFOO(a,b,c,d) MDB_VERSTR(a,b,c,d)
214 
216 #define MDB_VERSION_STRING \
217  MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE)
218 
225 typedef struct MDB_env MDB_env;
226 
232 typedef struct MDB_txn MDB_txn;
233 
235 typedef unsigned int MDB_dbi;
236 
238 typedef struct MDB_cursor MDB_cursor;
239 
251 typedef struct MDB_val {
252  size_t mv_size;
253  void *mv_data;
254 } MDB_val;
255 
257 typedef int (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
258 
273 typedef void (MDB_rel_func)(MDB_val *item, void *oldptr, void *newptr, void *relctx);
274 
279 #define MDB_FIXEDMAP 0x01
280 
281 #define MDB_NOSUBDIR 0x4000
282 
283 #define MDB_NOSYNC 0x10000
284 
285 #define MDB_RDONLY 0x20000
286 
287 #define MDB_NOMETASYNC 0x40000
288 
289 #define MDB_WRITEMAP 0x80000
290 
291 #define MDB_MAPASYNC 0x100000
292 
293 #define MDB_NOTLS 0x200000
294 
295 #define MDB_NOLOCK 0x400000
296 
297 #define MDB_NORDAHEAD 0x800000
298 
299 #define MDB_NOMEMINIT 0x1000000
300 
306 #define MDB_REVERSEKEY 0x02
307 
308 #define MDB_DUPSORT 0x04
309 
311 #define MDB_INTEGERKEY 0x08
312 
313 #define MDB_DUPFIXED 0x10
314 
315 #define MDB_INTEGERDUP 0x20
316 
317 #define MDB_REVERSEDUP 0x40
318 
319 #define MDB_CREATE 0x40000
320 
326 #define MDB_NOOVERWRITE 0x10
327 
331 #define MDB_NODUPDATA 0x20
332 
333 #define MDB_CURRENT 0x40
334 
337 #define MDB_RESERVE 0x10000
338 
339 #define MDB_APPEND 0x20000
340 
341 #define MDB_APPENDDUP 0x40000
342 
343 #define MDB_MULTIPLE 0x80000
344 /* @} */
345 
352 #define MDB_CP_COMPACT 0x01
353 /* @} */
354 
360 typedef enum MDB_cursor_op {
387 } MDB_cursor_op;
388 
395 #define MDB_SUCCESS 0
396 
397 #define MDB_KEYEXIST (-30799)
398 
399 #define MDB_NOTFOUND (-30798)
400 
401 #define MDB_PAGE_NOTFOUND (-30797)
402 
403 #define MDB_CORRUPTED (-30796)
404 
405 #define MDB_PANIC (-30795)
406 
407 #define MDB_VERSION_MISMATCH (-30794)
408 
409 #define MDB_INVALID (-30793)
410 
411 #define MDB_MAP_FULL (-30792)
412 
413 #define MDB_DBS_FULL (-30791)
414 
415 #define MDB_READERS_FULL (-30790)
416 
417 #define MDB_TLS_FULL (-30789)
418 
419 #define MDB_TXN_FULL (-30788)
420 
421 #define MDB_CURSOR_FULL (-30787)
422 
423 #define MDB_PAGE_FULL (-30786)
424 
425 #define MDB_MAP_RESIZED (-30785)
426 
434 #define MDB_INCOMPATIBLE (-30784)
435 
436 #define MDB_BAD_RSLOT (-30783)
437 
438 #define MDB_BAD_TXN (-30782)
439 
440 #define MDB_BAD_VALSIZE (-30781)
441 
442 #define MDB_BAD_DBI (-30780)
443 
444 #define MDB_LAST_ERRCODE MDB_BAD_DBI
445 
448 typedef struct MDB_stat {
449  unsigned int ms_psize;
451  unsigned int ms_depth;
453  size_t ms_leaf_pages;
455  size_t ms_entries;
456 } MDB_stat;
457 
459 typedef struct MDB_envinfo {
460  void *me_mapaddr;
461  size_t me_mapsize;
462  size_t me_last_pgno;
463  size_t me_last_txnid;
464  unsigned int me_maxreaders;
465  unsigned int me_numreaders;
466 } MDB_envinfo;
467 
475 char *mdb_version(int *major, int *minor, int *patch);
476 
487 char *mdb_strerror(int err);
488 
500 int mdb_env_create(MDB_env **env);
501 
623 int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode);
624 
639 int mdb_env_copy(MDB_env *env, const char *path);
640 
654 int mdb_env_copyfd(MDB_env *env, mdb_filehandle_t fd);
655 
678 int mdb_env_copy2(MDB_env *env, const char *path, unsigned int flags);
679 
697 int mdb_env_copyfd2(MDB_env *env, mdb_filehandle_t fd, unsigned int flags);
698 
705 int mdb_env_stat(MDB_env *env, MDB_stat *stat);
706 
713 int mdb_env_info(MDB_env *env, MDB_envinfo *stat);
714 
734 int mdb_env_sync(MDB_env *env, int force);
735 
744 void mdb_env_close(MDB_env *env);
745 
760 int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
761 
772 int mdb_env_get_flags(MDB_env *env, unsigned int *flags);
773 
786 int mdb_env_get_path(MDB_env *env, const char **path);
787 
798 int mdb_env_get_fd(MDB_env *env, mdb_filehandle_t *fd);
799 
832 int mdb_env_set_mapsize(MDB_env *env, size_t size);
833 
851 int mdb_env_set_maxreaders(MDB_env *env, unsigned int readers);
852 
863 int mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers);
864 
883 int mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs);
884 
893 
900 int mdb_env_set_userctx(MDB_env *env, void *ctx);
901 
907 void *mdb_env_get_userctx(MDB_env *env);
908 
915 typedef void MDB_assert_func(MDB_env *env, const char *msg);
916 
925 
960 int mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn);
961 
967 
977 size_t mdb_txn_id(MDB_txn *txn);
978 
995 int mdb_txn_commit(MDB_txn *txn);
996 
1005 void mdb_txn_abort(MDB_txn *txn);
1006 
1024 void mdb_txn_reset(MDB_txn *txn);
1025 
1040 int mdb_txn_renew(MDB_txn *txn);
1041 
1043 #define mdb_open(txn,name,flags,dbi) mdb_dbi_open(txn,name,flags,dbi)
1044 
1045 #define mdb_close(env,dbi) mdb_dbi_close(env,dbi)
1046 
1114 int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
1115 
1128 int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
1129 
1137 int mdb_dbi_flags(MDB_txn *txn, MDB_dbi dbi, unsigned int *flags);
1138 
1155 void mdb_dbi_close(MDB_env *env, MDB_dbi dbi);
1156 
1166 int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del);
1167 
1187 int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
1188 
1210 int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
1211 
1230 int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
1231 
1246 int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx);
1247 
1274 int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
1275 
1323 int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
1324  unsigned int flags);
1325 
1348 int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
1349 
1372 int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
1373 
1380 void mdb_cursor_close(MDB_cursor *cursor);
1381 
1398 int mdb_cursor_renew(MDB_txn *txn, MDB_cursor *cursor);
1399 
1405 
1410 MDB_dbi mdb_cursor_dbi(MDB_cursor *cursor);
1411 
1431 int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1432  MDB_cursor_op op);
1433 
1493 int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
1494  unsigned int flags);
1495 
1513 int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
1514 
1527 int mdb_cursor_count(MDB_cursor *cursor, size_t *countp);
1528 
1539 int mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
1540 
1551 int mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
1552 
1559 typedef int (MDB_msg_func)(const char *msg, void *ctx);
1560 
1568 int mdb_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx);
1569 
1576 int mdb_reader_check(MDB_env *env, int *dead);
1579 #ifdef __cplusplus
1580 }
1581 #endif
1582 
1590 #endif /* _LMDB_H_ */
Statistics for a database in the environment.
Definition: lmdb.h:448
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:9802
size_t me_mapsize
Definition: lmdb.h:461
void mdb_cursor_close(MDB_cursor *cursor)
Close a cursor handle.
Definition: mdb.c:7472
int mdb_env_set_assert(MDB_env *env, MDB_assert_func *func)
Definition: mdb.c:9381
Definition: lmdb.h:373
int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data)
Delete items from a database.
Definition: mdb.c:8263
Definition: lmdb.h:367
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:374
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:9485
Definition: lmdb.h:376
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:257
size_t ms_entries
Definition: lmdb.h:455
int mdb_cursor_count(MDB_cursor *cursor, size_t *countp)
Return count of duplicates for current key.
Definition: mdb.c:7440
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:6290
int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat)
Retrieve statistics for a database.
Definition: mdb.c:9601
void mdb_dbi_close(MDB_env *env, MDB_dbi dbi)
Close a database handle. Normally unnecessary. Use with care:
Definition: mdb.c:9618
size_t ms_overflow_pages
Definition: lmdb.h:454
void * mdb_env_get_userctx(MDB_env *env)
Get the application information associated with the MDB_env.
Definition: mdb.c:9375
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:252
MDB_txn * mdb_cursor_txn(MDB_cursor *cursor)
Return the cursor&#39;s transaction handle.
Definition: mdb.c:7487
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:9793
unsigned int ms_psize
Definition: lmdb.h:449
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:9444
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:9821
MDB_env * mdb_txn_env(MDB_txn *txn)
Returns the transaction&#39;s MDB_env.
Definition: mdb.c:2867
Information about the environment.
Definition: lmdb.h:459
void * mv_data
Definition: lmdb.h:253
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:915
void * me_mapaddr
Definition: lmdb.h:460
Definition: lmdb.h:380
Definition: lmdb.h:365
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&#39;s database handle.
Definition: mdb.c:7494
int mdb_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx)
Dump the entries in the reader lock table.
Definition: mdb.c:9827
size_t ms_branch_pages
Definition: lmdb.h:452
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:9392
Definition: lmdb.h:371
int mdb_env_get_flags(MDB_env *env, unsigned int *flags)
Get environment flags.
Definition: mdb.c:9356
int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del)
Empty or delete+close a database.
Definition: mdb.c:9733
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:464
size_t ms_leaf_pages
Definition: lmdb.h:453
int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode)
Open an environment handle.
Definition: mdb.c:4693
int mdb_env_copy(MDB_env *env, const char *path)
Copy an LMDB environment to the specified path.
Definition: mdb.c:9338
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:273
void mdb_txn_reset(MDB_txn *txn)
Reset a read-only transaction.
Definition: mdb.c:2985
Definition: lmdb.h:379
int mdb_dbi_flags(MDB_txn *txn, MDB_dbi dbi, unsigned int *flags)
Retrieve the DB flags for a database handle.
Definition: mdb.c:9634
Definition: lmdb.h:384
Definition: lmdb.h:361
int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx)
Set a context pointer for a MDB_FIXEDMAP database&#39;s relocation function.
Definition: mdb.c:9811
size_t me_last_pgno
Definition: lmdb.h:462
Definition: lmdb.h:383
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:9905
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:7423
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:9784
Generic structure used for passing keys and data in and out of the database.
Definition: lmdb.h:251
unsigned int MDB_dbi
A handle for an individual database in the DB environment.
Definition: lmdb.h:235
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:463
int mdb_filehandle_t
Definition: lmdb.h:182
int mdb_env_get_fd(MDB_env *env, mdb_filehandle_t *fd)
Return the filedescriptor for the given environment.
Definition: mdb.c:9402
size_t mdb_txn_id(MDB_txn *txn)
Return the transaction&#39;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:360
Definition: lmdb.h:381
int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op op)
Retrieve by cursor.
Definition: mdb.c:6086
int mdb_env_create(MDB_env **env)
Create an LMDB environment handle.
Definition: mdb.c:3854
unsigned int me_numreaders
Definition: lmdb.h:465
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:172
Definition: lmdb.h:385
Opaque structure for a database environment.
Definition: mdb.c:1194
Definition: lmdb.h:370
Definition: lmdb.h:366
Definition: lmdb.h:362
int mdb_env_stat(MDB_env *env, MDB_stat *stat)
Return statistics about the LMDB environment.
Definition: mdb.c:9431
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:386
int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff)
Set environment flags.
Definition: mdb.c:9344
int mdb_env_set_userctx(MDB_env *env, void *ctx)
Set application information associated with the MDB_env.
Definition: mdb.c:9366
int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags)
Delete current key/data pair.
Definition: mdb.c:6820
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:9271
Definition: lmdb.h:364
int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data)
Get items from a database.
Definition: mdb.c:5558
unsigned int ms_depth
Definition: lmdb.h:451
int( MDB_msg_func)(const char *msg, void *ctx)
A callback function used to print a message from the library.
Definition: lmdb.h:1559
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:8752
void mdb_env_close(MDB_env *env)
Close the environment and release the memory map.
Definition: mdb.c:4946
int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor)
Create a cursor handle.
Definition: mdb.c:7389