1 /* $NetBSD: lmdb.h,v 1.3 2021/08/14 16:14:57 christos Exp $ */ 2 3 /** @file lmdb.h 4 * @brief Lightning memory-mapped database library 5 * 6 * @mainpage Lightning Memory-Mapped Database Manager (LMDB) 7 * 8 * @section intro_sec Introduction 9 * LMDB is a Btree-based database management library modeled loosely on the 10 * BerkeleyDB API, but much simplified. The entire database is exposed 11 * in a memory map, and all data fetches return data directly 12 * from the mapped memory, so no malloc's or memcpy's occur during 13 * data fetches. As such, the library is extremely simple because it 14 * requires no page caching layer of its own, and it is extremely high 15 * performance and memory-efficient. It is also fully transactional with 16 * full ACID semantics, and when the memory map is read-only, the 17 * database integrity cannot be corrupted by stray pointer writes from 18 * application code. 19 * 20 * The library is fully thread-aware and supports concurrent read/write 21 * access from multiple processes and threads. Data pages use a copy-on- 22 * write strategy so no active data pages are ever overwritten, which 23 * also provides resistance to corruption and eliminates the need of any 24 * special recovery procedures after a system crash. Writes are fully 25 * serialized; only one write transaction may be active at a time, which 26 * guarantees that writers can never deadlock. The database structure is 27 * multi-versioned so readers run with no locks; writers cannot block 28 * readers, and readers don't block writers. 29 * 30 * Unlike other well-known database mechanisms which use either write-ahead 31 * transaction logs or append-only data writes, LMDB requires no maintenance 32 * during operation. Both write-ahead loggers and append-only databases 33 * require periodic checkpointing and/or compaction of their log or database 34 * files otherwise they grow without bound. LMDB tracks free pages within 35 * the database and re-uses them for new write operations, so the database 36 * size does not grow without bound in normal use. 37 * 38 * The memory map can be used as a read-only or read-write map. It is 39 * read-only by default as this provides total immunity to corruption. 40 * Using read-write mode offers much higher write performance, but adds 41 * the possibility for stray application writes thru pointers to silently 42 * corrupt the database. Of course if your application code is known to 43 * be bug-free (...) then this is not an issue. 44 * 45 * If this is your first time using a transactional embedded key/value 46 * store, you may find the \ref starting page to be helpful. 47 * 48 * @section caveats_sec Caveats 49 * Troubleshooting the lock file, plus semaphores on BSD systems: 50 * 51 * - A broken lockfile can cause sync issues. 52 * Stale reader transactions left behind by an aborted program 53 * cause further writes to grow the database quickly, and 54 * stale locks can block further operation. 55 * 56 * Fix: Check for stale readers periodically, using the 57 * #mdb_reader_check function or the \ref mdb_stat_1 "mdb_stat" tool. 58 * Stale writers will be cleared automatically on some systems: 59 * - Windows - automatic 60 * - Linux, systems using POSIX mutexes with Robust option - automatic 61 * - not on BSD, systems using POSIX semaphores. 62 * Otherwise just make all programs using the database close it; 63 * the lockfile is always reset on first open of the environment. 64 * 65 * - On BSD systems or others configured with MDB_USE_POSIX_SEM, 66 * startup can fail due to semaphores owned by another userid. 67 * 68 * Fix: Open and close the database as the user which owns the 69 * semaphores (likely last user) or as root, while no other 70 * process is using the database. 71 * 72 * Restrictions/caveats (in addition to those listed for some functions): 73 * 74 * - Only the database owner should normally use the database on 75 * BSD systems or when otherwise configured with MDB_USE_POSIX_SEM. 76 * Multiple users can cause startup to fail later, as noted above. 77 * 78 * - There is normally no pure read-only mode, since readers need write 79 * access to locks and lock file. Exceptions: On read-only filesystems 80 * or with the #MDB_NOLOCK flag described under #mdb_env_open(). 81 * 82 * - An LMDB configuration will often reserve considerable \b unused 83 * memory address space and maybe file size for future growth. 84 * This does not use actual memory or disk space, but users may need 85 * to understand the difference so they won't be scared off. 86 * 87 * - By default, in versions before 0.9.10, unused portions of the data 88 * file might receive garbage data from memory freed by other code. 89 * (This does not happen when using the #MDB_WRITEMAP flag.) As of 90 * 0.9.10 the default behavior is to initialize such memory before 91 * writing to the data file. Since there may be a slight performance 92 * cost due to this initialization, applications may disable it using 93 * the #MDB_NOMEMINIT flag. Applications handling sensitive data 94 * which must not be written should not use this flag. This flag is 95 * irrelevant when using #MDB_WRITEMAP. 96 * 97 * - A thread can only use one transaction at a time, plus any child 98 * transactions. Each transaction belongs to one thread. See below. 99 * The #MDB_NOTLS flag changes this for read-only transactions. 100 * 101 * - Use an MDB_env* in the process which opened it, not after fork(). 102 * 103 * - Do not have open an LMDB database twice in the same process at 104 * the same time. Not even from a plain open() call - close()ing it 105 * breaks fcntl() advisory locking. (It is OK to reopen it after 106 * fork() - exec*(), since the lockfile has FD_CLOEXEC set.) 107 * 108 * - Avoid long-lived transactions. Read transactions prevent 109 * reuse of pages freed by newer write transactions, thus the 110 * database can grow quickly. Write transactions prevent 111 * other write transactions, since writes are serialized. 112 * 113 * - Avoid suspending a process with active transactions. These 114 * would then be "long-lived" as above. Also read transactions 115 * suspended when writers commit could sometimes see wrong data. 116 * 117 * ...when several processes can use a database concurrently: 118 * 119 * - Avoid aborting a process with an active transaction. 120 * The transaction becomes "long-lived" as above until a check 121 * for stale readers is performed or the lockfile is reset, 122 * since the process may not remove it from the lockfile. 123 * 124 * This does not apply to write transactions if the system clears 125 * stale writers, see above. 126 * 127 * - If you do that anyway, do a periodic check for stale readers. Or 128 * close the environment once in a while, so the lockfile can get reset. 129 * 130 * - Do not use LMDB databases on remote filesystems, even between 131 * processes on the same host. This breaks flock() on some OSes, 132 * possibly memory map sync, and certainly sync between programs 133 * on different hosts. 134 * 135 * - Opening a database can fail if another process is opening or 136 * closing it at exactly the same time. 137 * 138 * @author Howard Chu, Symas Corporation. 139 * 140 * @copyright Copyright 2011-2021 Howard Chu, Symas Corp. All rights reserved. 141 * 142 * Redistribution and use in source and binary forms, with or without 143 * modification, are permitted only as authorized by the OpenLDAP 144 * Public License. 145 * 146 * A copy of this license is available in the file LICENSE in the 147 * top-level directory of the distribution or, alternatively, at 148 * <http://www.OpenLDAP.org/license.html>. 149 * 150 * @par Derived From: 151 * This code is derived from btree.c written by Martin Hedenfalk. 152 * 153 * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se> 154 * 155 * Permission to use, copy, modify, and distribute this software for any 156 * purpose with or without fee is hereby granted, provided that the above 157 * copyright notice and this permission notice appear in all copies. 158 * 159 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 160 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 161 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 162 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 163 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 164 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 165 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 166 */ 167 #ifndef _LMDB_H_ 168 #define _LMDB_H_ 169 170 #include <sys/types.h> 171 172 #ifdef __cplusplus 173 extern "C" { 174 #endif 175 176 /** Unix permissions for creating files, or dummy definition for Windows */ 177 #ifdef _MSC_VER 178 typedef int mdb_mode_t; 179 #else 180 typedef mode_t mdb_mode_t; 181 #endif 182 183 /** An abstraction for a file handle. 184 * On POSIX systems file handles are small integers. On Windows 185 * they're opaque pointers. 186 */ 187 #ifdef _WIN32 188 typedef void *mdb_filehandle_t; 189 #else 190 typedef int mdb_filehandle_t; 191 #endif 192 193 /** @defgroup mdb LMDB API 194 * @{ 195 * @brief OpenLDAP Lightning Memory-Mapped Database Manager 196 */ 197 /** @defgroup Version Version Macros 198 * @{ 199 */ 200 /** Library major version */ 201 #define MDB_VERSION_MAJOR 0 202 /** Library minor version */ 203 #define MDB_VERSION_MINOR 9 204 /** Library patch version */ 205 #define MDB_VERSION_PATCH 29 206 207 /** Combine args a,b,c into a single integer for easy version comparisons */ 208 #define MDB_VERINT(a,b,c) (((a) << 24) | ((b) << 16) | (c)) 209 210 /** The full library version as a single integer */ 211 #define MDB_VERSION_FULL \ 212 MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH) 213 214 /** The release date of this library version */ 215 #define MDB_VERSION_DATE "March 16, 2021" 216 217 /** A stringifier for the version info */ 218 #define MDB_VERSTR(a,b,c,d) "LMDB " #a "." #b "." #c ": (" d ")" 219 220 /** A helper for the stringifier macro */ 221 #define MDB_VERFOO(a,b,c,d) MDB_VERSTR(a,b,c,d) 222 223 /** The full library version as a C string */ 224 #define MDB_VERSION_STRING \ 225 MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE) 226 /** @} */ 227 228 /** @brief Opaque structure for a database environment. 229 * 230 * A DB environment supports multiple databases, all residing in the same 231 * shared-memory map. 232 */ 233 typedef struct MDB_env MDB_env; 234 235 /** @brief Opaque structure for a transaction handle. 236 * 237 * All database operations require a transaction handle. Transactions may be 238 * read-only or read-write. 239 */ 240 typedef struct MDB_txn MDB_txn; 241 242 /** @brief A handle for an individual database in the DB environment. */ 243 typedef unsigned int MDB_dbi; 244 245 /** @brief Opaque structure for navigating through a database */ 246 typedef struct MDB_cursor MDB_cursor; 247 248 /** @brief Generic structure used for passing keys and data in and out 249 * of the database. 250 * 251 * Values returned from the database are valid only until a subsequent 252 * update operation, or the end of the transaction. Do not modify or 253 * free them, they commonly point into the database itself. 254 * 255 * Key sizes must be between 1 and #mdb_env_get_maxkeysize() inclusive. 256 * The same applies to data sizes in databases with the #MDB_DUPSORT flag. 257 * Other data items can in theory be from 0 to 0xffffffff bytes long. 258 */ 259 typedef struct MDB_val { 260 size_t mv_size; /**< size of the data item */ 261 void *mv_data; /**< address of the data item */ 262 } MDB_val; 263 264 /** @brief A callback function used to compare two keys in a database */ 265 typedef int (MDB_cmp_func)(const MDB_val *a, const MDB_val *b); 266 267 /** @brief A callback function used to relocate a position-dependent data item 268 * in a fixed-address database. 269 * 270 * The \b newptr gives the item's desired address in 271 * the memory map, and \b oldptr gives its previous address. The item's actual 272 * data resides at the address in \b item. This callback is expected to walk 273 * through the fields of the record in \b item and modify any 274 * values based at the \b oldptr address to be relative to the \b newptr address. 275 * @param[in,out] item The item that is to be relocated. 276 * @param[in] oldptr The previous address. 277 * @param[in] newptr The new address to relocate to. 278 * @param[in] relctx An application-provided context, set by #mdb_set_relctx(). 279 * @todo This feature is currently unimplemented. 280 */ 281 typedef void (MDB_rel_func)(MDB_val *item, void *oldptr, void *newptr, void *relctx); 282 283 /** @defgroup mdb_env Environment Flags 284 * @{ 285 */ 286 /** mmap at a fixed address (experimental) */ 287 #define MDB_FIXEDMAP 0x01 288 /** no environment directory */ 289 #define MDB_NOSUBDIR 0x4000 290 /** don't fsync after commit */ 291 #define MDB_NOSYNC 0x10000 292 /** read only */ 293 #define MDB_RDONLY 0x20000 294 /** don't fsync metapage after commit */ 295 #define MDB_NOMETASYNC 0x40000 296 /** use writable mmap */ 297 #define MDB_WRITEMAP 0x80000 298 /** use asynchronous msync when #MDB_WRITEMAP is used */ 299 #define MDB_MAPASYNC 0x100000 300 /** tie reader locktable slots to #MDB_txn objects instead of to threads */ 301 #define MDB_NOTLS 0x200000 302 /** don't do any locking, caller must manage their own locks */ 303 #define MDB_NOLOCK 0x400000 304 /** don't do readahead (no effect on Windows) */ 305 #define MDB_NORDAHEAD 0x800000 306 /** don't initialize malloc'd memory before writing to datafile */ 307 #define MDB_NOMEMINIT 0x1000000 308 /** @} */ 309 310 /** @defgroup mdb_dbi_open Database Flags 311 * @{ 312 */ 313 /** use reverse string keys */ 314 #define MDB_REVERSEKEY 0x02 315 /** use sorted duplicates */ 316 #define MDB_DUPSORT 0x04 317 /** numeric keys in native byte order: either unsigned int or size_t. 318 * The keys must all be of the same size. */ 319 #define MDB_INTEGERKEY 0x08 320 /** with #MDB_DUPSORT, sorted dup items have fixed size */ 321 #define MDB_DUPFIXED 0x10 322 /** with #MDB_DUPSORT, dups are #MDB_INTEGERKEY-style integers */ 323 #define MDB_INTEGERDUP 0x20 324 /** with #MDB_DUPSORT, use reverse string dups */ 325 #define MDB_REVERSEDUP 0x40 326 /** create DB if not already existing */ 327 #define MDB_CREATE 0x40000 328 /** @} */ 329 330 /** @defgroup mdb_put Write Flags 331 * @{ 332 */ 333 /** For put: Don't write if the key already exists. */ 334 #define MDB_NOOVERWRITE 0x10 335 /** Only for #MDB_DUPSORT<br> 336 * For put: don't write if the key and data pair already exist.<br> 337 * For mdb_cursor_del: remove all duplicate data items. 338 */ 339 #define MDB_NODUPDATA 0x20 340 /** For mdb_cursor_put: overwrite the current key/data pair */ 341 #define MDB_CURRENT 0x40 342 /** For put: Just reserve space for data, don't copy it. Return a 343 * pointer to the reserved space. 344 */ 345 #define MDB_RESERVE 0x10000 346 /** Data is being appended, don't split full pages. */ 347 #define MDB_APPEND 0x20000 348 /** Duplicate data is being appended, don't split full pages. */ 349 #define MDB_APPENDDUP 0x40000 350 /** Store multiple data items in one call. Only for #MDB_DUPFIXED. */ 351 #define MDB_MULTIPLE 0x80000 352 /* @} */ 353 354 /** @defgroup mdb_copy Copy Flags 355 * @{ 356 */ 357 /** Compacting copy: Omit free space from copy, and renumber all 358 * pages sequentially. 359 */ 360 #define MDB_CP_COMPACT 0x01 361 /* @} */ 362 363 /** @brief Cursor Get operations. 364 * 365 * This is the set of all operations for retrieving data 366 * using a cursor. 367 */ 368 typedef enum MDB_cursor_op { 369 MDB_FIRST, /**< Position at first key/data item */ 370 MDB_FIRST_DUP, /**< Position at first data item of current key. 371 Only for #MDB_DUPSORT */ 372 MDB_GET_BOTH, /**< Position at key/data pair. Only for #MDB_DUPSORT */ 373 MDB_GET_BOTH_RANGE, /**< position at key, nearest data. Only for #MDB_DUPSORT */ 374 MDB_GET_CURRENT, /**< Return key/data at current cursor position */ 375 MDB_GET_MULTIPLE, /**< Return up to a page of duplicate data items 376 from current cursor position. Move cursor to prepare 377 for #MDB_NEXT_MULTIPLE. Only for #MDB_DUPFIXED */ 378 MDB_LAST, /**< Position at last key/data item */ 379 MDB_LAST_DUP, /**< Position at last data item of current key. 380 Only for #MDB_DUPSORT */ 381 MDB_NEXT, /**< Position at next data item */ 382 MDB_NEXT_DUP, /**< Position at next data item of current key. 383 Only for #MDB_DUPSORT */ 384 MDB_NEXT_MULTIPLE, /**< Return up to a page of duplicate data items 385 from next cursor position. Move cursor to prepare 386 for #MDB_NEXT_MULTIPLE. Only for #MDB_DUPFIXED */ 387 MDB_NEXT_NODUP, /**< Position at first data item of next key */ 388 MDB_PREV, /**< Position at previous data item */ 389 MDB_PREV_DUP, /**< Position at previous data item of current key. 390 Only for #MDB_DUPSORT */ 391 MDB_PREV_NODUP, /**< Position at last data item of previous key */ 392 MDB_SET, /**< Position at specified key */ 393 MDB_SET_KEY, /**< Position at specified key, return key + data */ 394 MDB_SET_RANGE, /**< Position at first key greater than or equal to specified key. */ 395 MDB_PREV_MULTIPLE /**< Position at previous page and return up to 396 a page of duplicate data items. Only for #MDB_DUPFIXED */ 397 } MDB_cursor_op; 398 399 /** @defgroup errors Return Codes 400 * 401 * BerkeleyDB uses -30800 to -30999, we'll go under them 402 * @{ 403 */ 404 /** Successful result */ 405 #define MDB_SUCCESS 0 406 /** key/data pair already exists */ 407 #define MDB_KEYEXIST (-30799) 408 /** key/data pair not found (EOF) */ 409 #define MDB_NOTFOUND (-30798) 410 /** Requested page not found - this usually indicates corruption */ 411 #define MDB_PAGE_NOTFOUND (-30797) 412 /** Located page was wrong type */ 413 #define MDB_CORRUPTED (-30796) 414 /** Update of meta page failed or environment had fatal error */ 415 #define MDB_PANIC (-30795) 416 /** Environment version mismatch */ 417 #define MDB_VERSION_MISMATCH (-30794) 418 /** File is not a valid LMDB file */ 419 #define MDB_INVALID (-30793) 420 /** Environment mapsize reached */ 421 #define MDB_MAP_FULL (-30792) 422 /** Environment maxdbs reached */ 423 #define MDB_DBS_FULL (-30791) 424 /** Environment maxreaders reached */ 425 #define MDB_READERS_FULL (-30790) 426 /** Too many TLS keys in use - Windows only */ 427 #define MDB_TLS_FULL (-30789) 428 /** Txn has too many dirty pages */ 429 #define MDB_TXN_FULL (-30788) 430 /** Cursor stack too deep - internal error */ 431 #define MDB_CURSOR_FULL (-30787) 432 /** Page has not enough space - internal error */ 433 #define MDB_PAGE_FULL (-30786) 434 /** Database contents grew beyond environment mapsize */ 435 #define MDB_MAP_RESIZED (-30785) 436 /** Operation and DB incompatible, or DB type changed. This can mean: 437 * <ul> 438 * <li>The operation expects an #MDB_DUPSORT / #MDB_DUPFIXED database. 439 * <li>Opening a named DB when the unnamed DB has #MDB_DUPSORT / #MDB_INTEGERKEY. 440 * <li>Accessing a data record as a database, or vice versa. 441 * <li>The database was dropped and recreated with different flags. 442 * </ul> 443 */ 444 #define MDB_INCOMPATIBLE (-30784) 445 /** Invalid reuse of reader locktable slot */ 446 #define MDB_BAD_RSLOT (-30783) 447 /** Transaction must abort, has a child, or is invalid */ 448 #define MDB_BAD_TXN (-30782) 449 /** Unsupported size of key/DB name/data, or wrong DUPFIXED size */ 450 #define MDB_BAD_VALSIZE (-30781) 451 /** The specified DBI was changed unexpectedly */ 452 #define MDB_BAD_DBI (-30780) 453 /** The last defined error code */ 454 #define MDB_LAST_ERRCODE MDB_BAD_DBI 455 /** @} */ 456 457 /** @brief Statistics for a database in the environment */ 458 typedef struct MDB_stat { 459 unsigned int ms_psize; /**< Size of a database page. 460 This is currently the same for all databases. */ 461 unsigned int ms_depth; /**< Depth (height) of the B-tree */ 462 size_t ms_branch_pages; /**< Number of internal (non-leaf) pages */ 463 size_t ms_leaf_pages; /**< Number of leaf pages */ 464 size_t ms_overflow_pages; /**< Number of overflow pages */ 465 size_t ms_entries; /**< Number of data items */ 466 } MDB_stat; 467 468 /** @brief Information about the environment */ 469 typedef struct MDB_envinfo { 470 void *me_mapaddr; /**< Address of map, if fixed */ 471 size_t me_mapsize; /**< Size of the data memory map */ 472 size_t me_last_pgno; /**< ID of the last used page */ 473 size_t me_last_txnid; /**< ID of the last committed transaction */ 474 unsigned int me_maxreaders; /**< max reader slots in the environment */ 475 unsigned int me_numreaders; /**< max reader slots used in the environment */ 476 } MDB_envinfo; 477 478 /** @brief Return the LMDB library version information. 479 * 480 * @param[out] major if non-NULL, the library major version number is copied here 481 * @param[out] minor if non-NULL, the library minor version number is copied here 482 * @param[out] patch if non-NULL, the library patch version number is copied here 483 * @retval "version string" The library version as a string 484 */ 485 char *mdb_version(int *major, int *minor, int *patch); 486 487 /** @brief Return a string describing a given error code. 488 * 489 * This function is a superset of the ANSI C X3.159-1989 (ANSI C) strerror(3) 490 * function. If the error code is greater than or equal to 0, then the string 491 * returned by the system function strerror(3) is returned. If the error code 492 * is less than 0, an error string corresponding to the LMDB library error is 493 * returned. See @ref errors for a list of LMDB-specific error codes. 494 * @param[in] err The error code 495 * @retval "error message" The description of the error 496 */ 497 char *mdb_strerror(int err); 498 499 /** @brief Create an LMDB environment handle. 500 * 501 * This function allocates memory for a #MDB_env structure. To release 502 * the allocated memory and discard the handle, call #mdb_env_close(). 503 * Before the handle may be used, it must be opened using #mdb_env_open(). 504 * Various other options may also need to be set before opening the handle, 505 * e.g. #mdb_env_set_mapsize(), #mdb_env_set_maxreaders(), #mdb_env_set_maxdbs(), 506 * depending on usage requirements. 507 * @param[out] env The address where the new handle will be stored 508 * @return A non-zero error value on failure and 0 on success. 509 */ 510 int mdb_env_create(MDB_env **env); 511 512 /** @brief Open an environment handle. 513 * 514 * If this function fails, #mdb_env_close() must be called to discard the #MDB_env handle. 515 * @param[in] env An environment handle returned by #mdb_env_create() 516 * @param[in] path The directory in which the database files reside. This 517 * directory must already exist and be writable. 518 * @param[in] flags Special options for this environment. This parameter 519 * must be set to 0 or by bitwise OR'ing together one or more of the 520 * values described here. 521 * Flags set by mdb_env_set_flags() are also used. 522 * <ul> 523 * <li>#MDB_FIXEDMAP 524 * use a fixed address for the mmap region. This flag must be specified 525 * when creating the environment, and is stored persistently in the environment. 526 * If successful, the memory map will always reside at the same virtual address 527 * and pointers used to reference data items in the database will be constant 528 * across multiple invocations. This option may not always work, depending on 529 * how the operating system has allocated memory to shared libraries and other uses. 530 * The feature is highly experimental. 531 * <li>#MDB_NOSUBDIR 532 * By default, LMDB creates its environment in a directory whose 533 * pathname is given in \b path, and creates its data and lock files 534 * under that directory. With this option, \b path is used as-is for 535 * the database main data file. The database lock file is the \b path 536 * with "-lock" appended. 537 * <li>#MDB_RDONLY 538 * Open the environment in read-only mode. No write operations will be 539 * allowed. LMDB will still modify the lock file - except on read-only 540 * filesystems, where LMDB does not use locks. 541 * <li>#MDB_WRITEMAP 542 * Use a writeable memory map unless MDB_RDONLY is set. This uses 543 * fewer mallocs but loses protection from application bugs 544 * like wild pointer writes and other bad updates into the database. 545 * This may be slightly faster for DBs that fit entirely in RAM, but 546 * is slower for DBs larger than RAM. 547 * Incompatible with nested transactions. 548 * Do not mix processes with and without MDB_WRITEMAP on the same 549 * environment. This can defeat durability (#mdb_env_sync etc). 550 * <li>#MDB_NOMETASYNC 551 * Flush system buffers to disk only once per transaction, omit the 552 * metadata flush. Defer that until the system flushes files to disk, 553 * or next non-MDB_RDONLY commit or #mdb_env_sync(). This optimization 554 * maintains database integrity, but a system crash may undo the last 555 * committed transaction. I.e. it preserves the ACI (atomicity, 556 * consistency, isolation) but not D (durability) database property. 557 * This flag may be changed at any time using #mdb_env_set_flags(). 558 * <li>#MDB_NOSYNC 559 * Don't flush system buffers to disk when committing a transaction. 560 * This optimization means a system crash can corrupt the database or 561 * lose the last transactions if buffers are not yet flushed to disk. 562 * The risk is governed by how often the system flushes dirty buffers 563 * to disk and how often #mdb_env_sync() is called. However, if the 564 * filesystem preserves write order and the #MDB_WRITEMAP flag is not 565 * used, transactions exhibit ACI (atomicity, consistency, isolation) 566 * properties and only lose D (durability). I.e. database integrity 567 * is maintained, but a system crash may undo the final transactions. 568 * Note that (#MDB_NOSYNC | #MDB_WRITEMAP) leaves the system with no 569 * hint for when to write transactions to disk, unless #mdb_env_sync() 570 * is called. (#MDB_MAPASYNC | #MDB_WRITEMAP) may be preferable. 571 * This flag may be changed at any time using #mdb_env_set_flags(). 572 * <li>#MDB_MAPASYNC 573 * When using #MDB_WRITEMAP, use asynchronous flushes to disk. 574 * As with #MDB_NOSYNC, a system crash can then corrupt the 575 * database or lose the last transactions. Calling #mdb_env_sync() 576 * ensures on-disk database integrity until next commit. 577 * This flag may be changed at any time using #mdb_env_set_flags(). 578 * <li>#MDB_NOTLS 579 * Don't use Thread-Local Storage. Tie reader locktable slots to 580 * #MDB_txn objects instead of to threads. I.e. #mdb_txn_reset() keeps 581 * the slot reserved for the #MDB_txn object. A thread may use parallel 582 * read-only transactions. A read-only transaction may span threads if 583 * the user synchronizes its use. Applications that multiplex many 584 * user threads over individual OS threads need this option. Such an 585 * application must also serialize the write transactions in an OS 586 * thread, since LMDB's write locking is unaware of the user threads. 587 * <li>#MDB_NOLOCK 588 * Don't do any locking. If concurrent access is anticipated, the 589 * caller must manage all concurrency itself. For proper operation 590 * the caller must enforce single-writer semantics, and must ensure 591 * that no readers are using old transactions while a writer is 592 * active. The simplest approach is to use an exclusive lock so that 593 * no readers may be active at all when a writer begins. 594 * <li>#MDB_NORDAHEAD 595 * Turn off readahead. Most operating systems perform readahead on 596 * read requests by default. This option turns it off if the OS 597 * supports it. Turning it off may help random read performance 598 * when the DB is larger than RAM and system RAM is full. 599 * The option is not implemented on Windows. 600 * <li>#MDB_NOMEMINIT 601 * Don't initialize malloc'd memory before writing to unused spaces 602 * in the data file. By default, memory for pages written to the data 603 * file is obtained using malloc. While these pages may be reused in 604 * subsequent transactions, freshly malloc'd pages will be initialized 605 * to zeroes before use. This avoids persisting leftover data from other 606 * code (that used the heap and subsequently freed the memory) into the 607 * data file. Note that many other system libraries may allocate 608 * and free memory from the heap for arbitrary uses. E.g., stdio may 609 * use the heap for file I/O buffers. This initialization step has a 610 * modest performance cost so some applications may want to disable 611 * it using this flag. This option can be a problem for applications 612 * which handle sensitive data like passwords, and it makes memory 613 * checkers like Valgrind noisy. This flag is not needed with #MDB_WRITEMAP, 614 * which writes directly to the mmap instead of using malloc for pages. The 615 * initialization is also skipped if #MDB_RESERVE is used; the 616 * caller is expected to overwrite all of the memory that was 617 * reserved in that case. 618 * This flag may be changed at any time using #mdb_env_set_flags(). 619 * </ul> 620 * @param[in] mode The UNIX permissions to set on created files and semaphores. 621 * This parameter is ignored on Windows. 622 * @return A non-zero error value on failure and 0 on success. Some possible 623 * errors are: 624 * <ul> 625 * <li>#MDB_VERSION_MISMATCH - the version of the LMDB library doesn't match the 626 * version that created the database environment. 627 * <li>#MDB_INVALID - the environment file headers are corrupted. 628 * <li>ENOENT - the directory specified by the path parameter doesn't exist. 629 * <li>EACCES - the user didn't have permission to access the environment files. 630 * <li>EAGAIN - the environment was locked by another process. 631 * </ul> 632 */ 633 int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode); 634 635 /** @brief Copy an LMDB environment to the specified path. 636 * 637 * This function may be used to make a backup of an existing environment. 638 * No lockfile is created, since it gets recreated at need. 639 * @note This call can trigger significant file size growth if run in 640 * parallel with write transactions, because it employs a read-only 641 * transaction. See long-lived transactions under @ref caveats_sec. 642 * @param[in] env An environment handle returned by #mdb_env_create(). It 643 * must have already been opened successfully. 644 * @param[in] path The directory in which the copy will reside. This 645 * directory must already exist and be writable but must otherwise be 646 * empty. 647 * @return A non-zero error value on failure and 0 on success. 648 */ 649 int mdb_env_copy(MDB_env *env, const char *path); 650 651 /** @brief Copy an LMDB environment to the specified file descriptor. 652 * 653 * This function may be used to make a backup of an existing environment. 654 * No lockfile is created, since it gets recreated at need. 655 * @note This call can trigger significant file size growth if run in 656 * parallel with write transactions, because it employs a read-only 657 * transaction. See long-lived transactions under @ref caveats_sec. 658 * @param[in] env An environment handle returned by #mdb_env_create(). It 659 * must have already been opened successfully. 660 * @param[in] fd The filedescriptor to write the copy to. It must 661 * have already been opened for Write access. 662 * @return A non-zero error value on failure and 0 on success. 663 */ 664 int mdb_env_copyfd(MDB_env *env, mdb_filehandle_t fd); 665 666 /** @brief Copy an LMDB environment to the specified path, with options. 667 * 668 * This function may be used to make a backup of an existing environment. 669 * No lockfile is created, since it gets recreated at need. 670 * @note This call can trigger significant file size growth if run in 671 * parallel with write transactions, because it employs a read-only 672 * transaction. See long-lived transactions under @ref caveats_sec. 673 * @param[in] env An environment handle returned by #mdb_env_create(). It 674 * must have already been opened successfully. 675 * @param[in] path The directory in which the copy will reside. This 676 * directory must already exist and be writable but must otherwise be 677 * empty. 678 * @param[in] flags Special options for this operation. This parameter 679 * must be set to 0 or by bitwise OR'ing together one or more of the 680 * values described here. 681 * <ul> 682 * <li>#MDB_CP_COMPACT - Perform compaction while copying: omit free 683 * pages and sequentially renumber all pages in output. This option 684 * consumes more CPU and runs more slowly than the default. 685 * Currently it fails if the environment has suffered a page leak. 686 * </ul> 687 * @return A non-zero error value on failure and 0 on success. 688 */ 689 int mdb_env_copy2(MDB_env *env, const char *path, unsigned int flags); 690 691 /** @brief Copy an LMDB environment to the specified file descriptor, 692 * with options. 693 * 694 * This function may be used to make a backup of an existing environment. 695 * No lockfile is created, since it gets recreated at need. See 696 * #mdb_env_copy2() for further details. 697 * @note This call can trigger significant file size growth if run in 698 * parallel with write transactions, because it employs a read-only 699 * transaction. See long-lived transactions under @ref caveats_sec. 700 * @param[in] env An environment handle returned by #mdb_env_create(). It 701 * must have already been opened successfully. 702 * @param[in] fd The filedescriptor to write the copy to. It must 703 * have already been opened for Write access. 704 * @param[in] flags Special options for this operation. 705 * See #mdb_env_copy2() for options. 706 * @return A non-zero error value on failure and 0 on success. 707 */ 708 int mdb_env_copyfd2(MDB_env *env, mdb_filehandle_t fd, unsigned int flags); 709 710 /** @brief Return statistics about the LMDB environment. 711 * 712 * @param[in] env An environment handle returned by #mdb_env_create() 713 * @param[out] stat The address of an #MDB_stat structure 714 * where the statistics will be copied 715 */ 716 int mdb_env_stat(MDB_env *env, MDB_stat *stat); 717 718 /** @brief Return information about the LMDB environment. 719 * 720 * @param[in] env An environment handle returned by #mdb_env_create() 721 * @param[out] stat The address of an #MDB_envinfo structure 722 * where the information will be copied 723 */ 724 int mdb_env_info(MDB_env *env, MDB_envinfo *stat); 725 726 /** @brief Flush the data buffers to disk. 727 * 728 * Data is always written to disk when #mdb_txn_commit() is called, 729 * but the operating system may keep it buffered. LMDB always flushes 730 * the OS buffers upon commit as well, unless the environment was 731 * opened with #MDB_NOSYNC or in part #MDB_NOMETASYNC. This call is 732 * not valid if the environment was opened with #MDB_RDONLY. 733 * @param[in] env An environment handle returned by #mdb_env_create() 734 * @param[in] force If non-zero, force a synchronous flush. Otherwise 735 * if the environment has the #MDB_NOSYNC flag set the flushes 736 * will be omitted, and with #MDB_MAPASYNC they will be asynchronous. 737 * @return A non-zero error value on failure and 0 on success. Some possible 738 * errors are: 739 * <ul> 740 * <li>EACCES - the environment is read-only. 741 * <li>EINVAL - an invalid parameter was specified. 742 * <li>EIO - an error occurred during synchronization. 743 * </ul> 744 */ 745 int mdb_env_sync(MDB_env *env, int force); 746 747 /** @brief Close the environment and release the memory map. 748 * 749 * Only a single thread may call this function. All transactions, databases, 750 * and cursors must already be closed before calling this function. Attempts to 751 * use any such handles after calling this function will cause a SIGSEGV. 752 * The environment handle will be freed and must not be used again after this call. 753 * @param[in] env An environment handle returned by #mdb_env_create() 754 */ 755 void mdb_env_close(MDB_env *env); 756 757 /** @brief Set environment flags. 758 * 759 * This may be used to set some flags in addition to those from 760 * #mdb_env_open(), or to unset these flags. If several threads 761 * change the flags at the same time, the result is undefined. 762 * @param[in] env An environment handle returned by #mdb_env_create() 763 * @param[in] flags The flags to change, bitwise OR'ed together 764 * @param[in] onoff A non-zero value sets the flags, zero clears them. 765 * @return A non-zero error value on failure and 0 on success. Some possible 766 * errors are: 767 * <ul> 768 * <li>EINVAL - an invalid parameter was specified. 769 * </ul> 770 */ 771 int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff); 772 773 /** @brief Get environment flags. 774 * 775 * @param[in] env An environment handle returned by #mdb_env_create() 776 * @param[out] flags The address of an integer to store the flags 777 * @return A non-zero error value on failure and 0 on success. Some possible 778 * errors are: 779 * <ul> 780 * <li>EINVAL - an invalid parameter was specified. 781 * </ul> 782 */ 783 int mdb_env_get_flags(MDB_env *env, unsigned int *flags); 784 785 /** @brief Return the path that was used in #mdb_env_open(). 786 * 787 * @param[in] env An environment handle returned by #mdb_env_create() 788 * @param[out] path Address of a string pointer to contain the path. This 789 * is the actual string in the environment, not a copy. It should not be 790 * altered in any way. 791 * @return A non-zero error value on failure and 0 on success. Some possible 792 * errors are: 793 * <ul> 794 * <li>EINVAL - an invalid parameter was specified. 795 * </ul> 796 */ 797 int mdb_env_get_path(MDB_env *env, const char **path); 798 799 /** @brief Return the filedescriptor for the given environment. 800 * 801 * This function may be called after fork(), so the descriptor can be 802 * closed before exec*(). Other LMDB file descriptors have FD_CLOEXEC. 803 * (Until LMDB 0.9.18, only the lockfile had that.) 804 * 805 * @param[in] env An environment handle returned by #mdb_env_create() 806 * @param[out] fd Address of a mdb_filehandle_t to contain the descriptor. 807 * @return A non-zero error value on failure and 0 on success. Some possible 808 * errors are: 809 * <ul> 810 * <li>EINVAL - an invalid parameter was specified. 811 * </ul> 812 */ 813 int mdb_env_get_fd(MDB_env *env, mdb_filehandle_t *fd); 814 815 /** @brief Set the size of the memory map to use for this environment. 816 * 817 * The size should be a multiple of the OS page size. The default is 818 * 10485760 bytes. The size of the memory map is also the maximum size 819 * of the database. The value should be chosen as large as possible, 820 * to accommodate future growth of the database. 821 * This function should be called after #mdb_env_create() and before #mdb_env_open(). 822 * It may be called at later times if no transactions are active in 823 * this process. Note that the library does not check for this condition, 824 * the caller must ensure it explicitly. 825 * 826 * The new size takes effect immediately for the current process but 827 * will not be persisted to any others until a write transaction has been 828 * committed by the current process. Also, only mapsize increases are 829 * persisted into the environment. 830 * 831 * If the mapsize is increased by another process, and data has grown 832 * beyond the range of the current mapsize, #mdb_txn_begin() will 833 * return #MDB_MAP_RESIZED. This function may be called with a size 834 * of zero to adopt the new size. 835 * 836 * Any attempt to set a size smaller than the space already consumed 837 * by the environment will be silently changed to the current size of the used space. 838 * @param[in] env An environment handle returned by #mdb_env_create() 839 * @param[in] size The size in bytes 840 * @return A non-zero error value on failure and 0 on success. Some possible 841 * errors are: 842 * <ul> 843 * <li>EINVAL - an invalid parameter was specified, or the environment has 844 * an active write transaction. 845 * </ul> 846 */ 847 int mdb_env_set_mapsize(MDB_env *env, size_t size); 848 849 /** @brief Set the maximum number of threads/reader slots for the environment. 850 * 851 * This defines the number of slots in the lock table that is used to track readers in the 852 * the environment. The default is 126. 853 * Starting a read-only transaction normally ties a lock table slot to the 854 * current thread until the environment closes or the thread exits. If 855 * MDB_NOTLS is in use, #mdb_txn_begin() instead ties the slot to the 856 * MDB_txn object until it or the #MDB_env object is destroyed. 857 * This function may only be called after #mdb_env_create() and before #mdb_env_open(). 858 * @param[in] env An environment handle returned by #mdb_env_create() 859 * @param[in] readers The maximum number of reader lock table slots 860 * @return A non-zero error value on failure and 0 on success. Some possible 861 * errors are: 862 * <ul> 863 * <li>EINVAL - an invalid parameter was specified, or the environment is already open. 864 * </ul> 865 */ 866 int mdb_env_set_maxreaders(MDB_env *env, unsigned int readers); 867 868 /** @brief Get the maximum number of threads/reader slots for the environment. 869 * 870 * @param[in] env An environment handle returned by #mdb_env_create() 871 * @param[out] readers Address of an integer to store the number of readers 872 * @return A non-zero error value on failure and 0 on success. Some possible 873 * errors are: 874 * <ul> 875 * <li>EINVAL - an invalid parameter was specified. 876 * </ul> 877 */ 878 int mdb_env_get_maxreaders(MDB_env *env, unsigned int *readers); 879 880 /** @brief Set the maximum number of named databases for the environment. 881 * 882 * This function is only needed if multiple databases will be used in the 883 * environment. Simpler applications that use the environment as a single 884 * unnamed database can ignore this option. 885 * This function may only be called after #mdb_env_create() and before #mdb_env_open(). 886 * 887 * Currently a moderate number of slots are cheap but a huge number gets 888 * expensive: 7-120 words per transaction, and every #mdb_dbi_open() 889 * does a linear search of the opened slots. 890 * @param[in] env An environment handle returned by #mdb_env_create() 891 * @param[in] dbs The maximum number of databases 892 * @return A non-zero error value on failure and 0 on success. Some possible 893 * errors are: 894 * <ul> 895 * <li>EINVAL - an invalid parameter was specified, or the environment is already open. 896 * </ul> 897 */ 898 int mdb_env_set_maxdbs(MDB_env *env, MDB_dbi dbs); 899 900 /** @brief Get the maximum size of keys and #MDB_DUPSORT data we can write. 901 * 902 * Depends on the compile-time constant #MDB_MAXKEYSIZE. Default 511. 903 * See @ref MDB_val. 904 * @param[in] env An environment handle returned by #mdb_env_create() 905 * @return The maximum size of a key we can write 906 */ 907 int mdb_env_get_maxkeysize(MDB_env *env); 908 909 /** @brief Set application information associated with the #MDB_env. 910 * 911 * @param[in] env An environment handle returned by #mdb_env_create() 912 * @param[in] ctx An arbitrary pointer for whatever the application needs. 913 * @return A non-zero error value on failure and 0 on success. 914 */ 915 int mdb_env_set_userctx(MDB_env *env, void *ctx); 916 917 /** @brief Get the application information associated with the #MDB_env. 918 * 919 * @param[in] env An environment handle returned by #mdb_env_create() 920 * @return The pointer set by #mdb_env_set_userctx(). 921 */ 922 void *mdb_env_get_userctx(MDB_env *env); 923 924 /** @brief A callback function for most LMDB assert() failures, 925 * called before printing the message and aborting. 926 * 927 * @param[in] env An environment handle returned by #mdb_env_create(). 928 * @param[in] msg The assertion message, not including newline. 929 */ 930 typedef void MDB_assert_func(MDB_env *env, const char *msg); 931 932 /** Set or reset the assert() callback of the environment. 933 * Disabled if liblmdb is built with NDEBUG. 934 * @note This hack should become obsolete as lmdb's error handling matures. 935 * @param[in] env An environment handle returned by #mdb_env_create(). 936 * @param[in] func An #MDB_assert_func function, or 0. 937 * @return A non-zero error value on failure and 0 on success. 938 */ 939 int mdb_env_set_assert(MDB_env *env, MDB_assert_func *func); 940 941 /** @brief Create a transaction for use with the environment. 942 * 943 * The transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit(). 944 * @note A transaction and its cursors must only be used by a single 945 * thread, and a thread may only have a single transaction at a time. 946 * If #MDB_NOTLS is in use, this does not apply to read-only transactions. 947 * @note Cursors may not span transactions. 948 * @param[in] env An environment handle returned by #mdb_env_create() 949 * @param[in] parent If this parameter is non-NULL, the new transaction 950 * will be a nested transaction, with the transaction indicated by \b parent 951 * as its parent. Transactions may be nested to any level. A parent 952 * transaction and its cursors may not issue any other operations than 953 * mdb_txn_commit and mdb_txn_abort while it has active child transactions. 954 * @param[in] flags Special options for this transaction. This parameter 955 * must be set to 0 or by bitwise OR'ing together one or more of the 956 * values described here. 957 * <ul> 958 * <li>#MDB_RDONLY 959 * This transaction will not perform any write operations. 960 * </ul> 961 * @param[out] txn Address where the new #MDB_txn handle will be stored 962 * @return A non-zero error value on failure and 0 on success. Some possible 963 * errors are: 964 * <ul> 965 * <li>#MDB_PANIC - a fatal error occurred earlier and the environment 966 * must be shut down. 967 * <li>#MDB_MAP_RESIZED - another process wrote data beyond this MDB_env's 968 * mapsize and this environment's map must be resized as well. 969 * See #mdb_env_set_mapsize(). 970 * <li>#MDB_READERS_FULL - a read-only transaction was requested and 971 * the reader lock table is full. See #mdb_env_set_maxreaders(). 972 * <li>ENOMEM - out of memory. 973 * </ul> 974 */ 975 int mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **txn); 976 977 /** @brief Returns the transaction's #MDB_env 978 * 979 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 980 */ 981 MDB_env *mdb_txn_env(MDB_txn *txn); 982 983 /** @brief Return the transaction's ID. 984 * 985 * This returns the identifier associated with this transaction. For a 986 * read-only transaction, this corresponds to the snapshot being read; 987 * concurrent readers will frequently have the same transaction ID. 988 * 989 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 990 * @return A transaction ID, valid if input is an active transaction. 991 */ 992 size_t mdb_txn_id(MDB_txn *txn); 993 994 /** @brief Commit all the operations of a transaction into the database. 995 * 996 * The transaction handle is freed. It and its cursors must not be used 997 * again after this call, except with #mdb_cursor_renew(). 998 * @note Earlier documentation incorrectly said all cursors would be freed. 999 * Only write-transactions free cursors. 1000 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1001 * @return A non-zero error value on failure and 0 on success. Some possible 1002 * errors are: 1003 * <ul> 1004 * <li>EINVAL - an invalid parameter was specified. 1005 * <li>ENOSPC - no more disk space. 1006 * <li>EIO - a low-level I/O error occurred while writing. 1007 * <li>ENOMEM - out of memory. 1008 * </ul> 1009 */ 1010 int mdb_txn_commit(MDB_txn *txn); 1011 1012 /** @brief Abandon all the operations of the transaction instead of saving them. 1013 * 1014 * The transaction handle is freed. It and its cursors must not be used 1015 * again after this call, except with #mdb_cursor_renew(). 1016 * @note Earlier documentation incorrectly said all cursors would be freed. 1017 * Only write-transactions free cursors. 1018 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1019 */ 1020 void mdb_txn_abort(MDB_txn *txn); 1021 1022 /** @brief Reset a read-only transaction. 1023 * 1024 * Abort the transaction like #mdb_txn_abort(), but keep the transaction 1025 * handle. #mdb_txn_renew() may reuse the handle. This saves allocation 1026 * overhead if the process will start a new read-only transaction soon, 1027 * and also locking overhead if #MDB_NOTLS is in use. The reader table 1028 * lock is released, but the table slot stays tied to its thread or 1029 * #MDB_txn. Use mdb_txn_abort() to discard a reset handle, and to free 1030 * its lock table slot if MDB_NOTLS is in use. 1031 * Cursors opened within the transaction must not be used 1032 * again after this call, except with #mdb_cursor_renew(). 1033 * Reader locks generally don't interfere with writers, but they keep old 1034 * versions of database pages allocated. Thus they prevent the old pages 1035 * from being reused when writers commit new data, and so under heavy load 1036 * the database size may grow much more rapidly than otherwise. 1037 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1038 */ 1039 void mdb_txn_reset(MDB_txn *txn); 1040 1041 /** @brief Renew a read-only transaction. 1042 * 1043 * This acquires a new reader lock for a transaction handle that had been 1044 * released by #mdb_txn_reset(). It must be called before a reset transaction 1045 * may be used again. 1046 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1047 * @return A non-zero error value on failure and 0 on success. Some possible 1048 * errors are: 1049 * <ul> 1050 * <li>#MDB_PANIC - a fatal error occurred earlier and the environment 1051 * must be shut down. 1052 * <li>EINVAL - an invalid parameter was specified. 1053 * </ul> 1054 */ 1055 int mdb_txn_renew(MDB_txn *txn); 1056 1057 /** Compat with version <= 0.9.4, avoid clash with libmdb from MDB Tools project */ 1058 #define mdb_open(txn,name,flags,dbi) mdb_dbi_open(txn,name,flags,dbi) 1059 /** Compat with version <= 0.9.4, avoid clash with libmdb from MDB Tools project */ 1060 #define mdb_close(env,dbi) mdb_dbi_close(env,dbi) 1061 1062 /** @brief Open a database in the environment. 1063 * 1064 * A database handle denotes the name and parameters of a database, 1065 * independently of whether such a database exists. 1066 * The database handle may be discarded by calling #mdb_dbi_close(). 1067 * The old database handle is returned if the database was already open. 1068 * The handle may only be closed once. 1069 * 1070 * The database handle will be private to the current transaction until 1071 * the transaction is successfully committed. If the transaction is 1072 * aborted the handle will be closed automatically. 1073 * After a successful commit the handle will reside in the shared 1074 * environment, and may be used by other transactions. 1075 * 1076 * This function must not be called from multiple concurrent 1077 * transactions in the same process. A transaction that uses 1078 * this function must finish (either commit or abort) before 1079 * any other transaction in the process may use this function. 1080 * 1081 * To use named databases (with name != NULL), #mdb_env_set_maxdbs() 1082 * must be called before opening the environment. Database names are 1083 * keys in the unnamed database, and may be read but not written. 1084 * 1085 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1086 * @param[in] name The name of the database to open. If only a single 1087 * database is needed in the environment, this value may be NULL. 1088 * @param[in] flags Special options for this database. This parameter 1089 * must be set to 0 or by bitwise OR'ing together one or more of the 1090 * values described here. 1091 * <ul> 1092 * <li>#MDB_REVERSEKEY 1093 * Keys are strings to be compared in reverse order, from the end 1094 * of the strings to the beginning. By default, Keys are treated as strings and 1095 * compared from beginning to end. 1096 * <li>#MDB_DUPSORT 1097 * Duplicate keys may be used in the database. (Or, from another perspective, 1098 * keys may have multiple data items, stored in sorted order.) By default 1099 * keys must be unique and may have only a single data item. 1100 * <li>#MDB_INTEGERKEY 1101 * Keys are binary integers in native byte order, either unsigned int 1102 * or size_t, and will be sorted as such. 1103 * The keys must all be of the same size. 1104 * <li>#MDB_DUPFIXED 1105 * This flag may only be used in combination with #MDB_DUPSORT. This option 1106 * tells the library that the data items for this database are all the same 1107 * size, which allows further optimizations in storage and retrieval. When 1108 * all data items are the same size, the #MDB_GET_MULTIPLE, #MDB_NEXT_MULTIPLE 1109 * and #MDB_PREV_MULTIPLE cursor operations may be used to retrieve multiple 1110 * items at once. 1111 * <li>#MDB_INTEGERDUP 1112 * This option specifies that duplicate data items are binary integers, 1113 * similar to #MDB_INTEGERKEY keys. 1114 * <li>#MDB_REVERSEDUP 1115 * This option specifies that duplicate data items should be compared as 1116 * strings in reverse order. 1117 * <li>#MDB_CREATE 1118 * Create the named database if it doesn't exist. This option is not 1119 * allowed in a read-only transaction or a read-only environment. 1120 * </ul> 1121 * @param[out] dbi Address where the new #MDB_dbi handle will be stored 1122 * @return A non-zero error value on failure and 0 on success. Some possible 1123 * errors are: 1124 * <ul> 1125 * <li>#MDB_NOTFOUND - the specified database doesn't exist in the environment 1126 * and #MDB_CREATE was not specified. 1127 * <li>#MDB_DBS_FULL - too many databases have been opened. See #mdb_env_set_maxdbs(). 1128 * </ul> 1129 */ 1130 int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi); 1131 1132 /** @brief Retrieve statistics for a database. 1133 * 1134 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1135 * @param[in] dbi A database handle returned by #mdb_dbi_open() 1136 * @param[out] stat The address of an #MDB_stat structure 1137 * where the statistics will be copied 1138 * @return A non-zero error value on failure and 0 on success. Some possible 1139 * errors are: 1140 * <ul> 1141 * <li>EINVAL - an invalid parameter was specified. 1142 * </ul> 1143 */ 1144 int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat); 1145 1146 /** @brief Retrieve the DB flags for a database handle. 1147 * 1148 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1149 * @param[in] dbi A database handle returned by #mdb_dbi_open() 1150 * @param[out] flags Address where the flags will be returned. 1151 * @return A non-zero error value on failure and 0 on success. 1152 */ 1153 int mdb_dbi_flags(MDB_txn *txn, MDB_dbi dbi, unsigned int *flags); 1154 1155 /** @brief Close a database handle. Normally unnecessary. Use with care: 1156 * 1157 * This call is not mutex protected. Handles should only be closed by 1158 * a single thread, and only if no other threads are going to reference 1159 * the database handle or one of its cursors any further. Do not close 1160 * a handle if an existing transaction has modified its database. 1161 * Doing so can cause misbehavior from database corruption to errors 1162 * like MDB_BAD_VALSIZE (since the DB name is gone). 1163 * 1164 * Closing a database handle is not necessary, but lets #mdb_dbi_open() 1165 * reuse the handle value. Usually it's better to set a bigger 1166 * #mdb_env_set_maxdbs(), unless that value would be large. 1167 * 1168 * @param[in] env An environment handle returned by #mdb_env_create() 1169 * @param[in] dbi A database handle returned by #mdb_dbi_open() 1170 */ 1171 void mdb_dbi_close(MDB_env *env, MDB_dbi dbi); 1172 1173 /** @brief Empty or delete+close a database. 1174 * 1175 * See #mdb_dbi_close() for restrictions about closing the DB handle. 1176 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1177 * @param[in] dbi A database handle returned by #mdb_dbi_open() 1178 * @param[in] del 0 to empty the DB, 1 to delete it from the 1179 * environment and close the DB handle. 1180 * @return A non-zero error value on failure and 0 on success. 1181 */ 1182 int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del); 1183 1184 /** @brief Set a custom key comparison function for a database. 1185 * 1186 * The comparison function is called whenever it is necessary to compare a 1187 * key specified by the application with a key currently stored in the database. 1188 * If no comparison function is specified, and no special key flags were specified 1189 * with #mdb_dbi_open(), the keys are compared lexically, with shorter keys collating 1190 * before longer keys. 1191 * @warning This function must be called before any data access functions are used, 1192 * otherwise data corruption may occur. The same comparison function must be used by every 1193 * program accessing the database, every time the database is used. 1194 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1195 * @param[in] dbi A database handle returned by #mdb_dbi_open() 1196 * @param[in] cmp A #MDB_cmp_func function 1197 * @return A non-zero error value on failure and 0 on success. Some possible 1198 * errors are: 1199 * <ul> 1200 * <li>EINVAL - an invalid parameter was specified. 1201 * </ul> 1202 */ 1203 int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp); 1204 1205 /** @brief Set a custom data comparison function for a #MDB_DUPSORT database. 1206 * 1207 * This comparison function is called whenever it is necessary to compare a data 1208 * item specified by the application with a data item currently stored in the database. 1209 * This function only takes effect if the database was opened with the #MDB_DUPSORT 1210 * flag. 1211 * If no comparison function is specified, and no special key flags were specified 1212 * with #mdb_dbi_open(), the data items are compared lexically, with shorter items collating 1213 * before longer items. 1214 * @warning This function must be called before any data access functions are used, 1215 * otherwise data corruption may occur. The same comparison function must be used by every 1216 * program accessing the database, every time the database is used. 1217 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1218 * @param[in] dbi A database handle returned by #mdb_dbi_open() 1219 * @param[in] cmp A #MDB_cmp_func function 1220 * @return A non-zero error value on failure and 0 on success. Some possible 1221 * errors are: 1222 * <ul> 1223 * <li>EINVAL - an invalid parameter was specified. 1224 * </ul> 1225 */ 1226 int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp); 1227 1228 /** @brief Set a relocation function for a #MDB_FIXEDMAP database. 1229 * 1230 * @todo The relocation function is called whenever it is necessary to move the data 1231 * of an item to a different position in the database (e.g. through tree 1232 * balancing operations, shifts as a result of adds or deletes, etc.). It is 1233 * intended to allow address/position-dependent data items to be stored in 1234 * a database in an environment opened with the #MDB_FIXEDMAP option. 1235 * Currently the relocation feature is unimplemented and setting 1236 * this function has no effect. 1237 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1238 * @param[in] dbi A database handle returned by #mdb_dbi_open() 1239 * @param[in] rel A #MDB_rel_func function 1240 * @return A non-zero error value on failure and 0 on success. Some possible 1241 * errors are: 1242 * <ul> 1243 * <li>EINVAL - an invalid parameter was specified. 1244 * </ul> 1245 */ 1246 int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel); 1247 1248 /** @brief Set a context pointer for a #MDB_FIXEDMAP database's relocation function. 1249 * 1250 * See #mdb_set_relfunc and #MDB_rel_func for more details. 1251 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1252 * @param[in] dbi A database handle returned by #mdb_dbi_open() 1253 * @param[in] ctx An arbitrary pointer for whatever the application needs. 1254 * It will be passed to the callback function set by #mdb_set_relfunc 1255 * as its \b relctx parameter whenever the callback is invoked. 1256 * @return A non-zero error value on failure and 0 on success. Some possible 1257 * errors are: 1258 * <ul> 1259 * <li>EINVAL - an invalid parameter was specified. 1260 * </ul> 1261 */ 1262 int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx); 1263 1264 /** @brief Get items from a database. 1265 * 1266 * This function retrieves key/data pairs from the database. The address 1267 * and length of the data associated with the specified \b key are returned 1268 * in the structure to which \b data refers. 1269 * If the database supports duplicate keys (#MDB_DUPSORT) then the 1270 * first data item for the key will be returned. Retrieval of other 1271 * items requires the use of #mdb_cursor_get(). 1272 * 1273 * @note The memory pointed to by the returned values is owned by the 1274 * database. The caller need not dispose of the memory, and may not 1275 * modify it in any way. For values returned in a read-only transaction 1276 * any modification attempts will cause a SIGSEGV. 1277 * @note Values returned from the database are valid only until a 1278 * subsequent update operation, or the end of the transaction. 1279 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1280 * @param[in] dbi A database handle returned by #mdb_dbi_open() 1281 * @param[in] key The key to search for in the database 1282 * @param[out] data The data corresponding to the key 1283 * @return A non-zero error value on failure and 0 on success. Some possible 1284 * errors are: 1285 * <ul> 1286 * <li>#MDB_NOTFOUND - the key was not in the database. 1287 * <li>EINVAL - an invalid parameter was specified. 1288 * </ul> 1289 */ 1290 int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data); 1291 1292 /** @brief Store items into a database. 1293 * 1294 * This function stores key/data pairs in the database. The default behavior 1295 * is to enter the new key/data pair, replacing any previously existing key 1296 * if duplicates are disallowed, or adding a duplicate data item if 1297 * duplicates are allowed (#MDB_DUPSORT). 1298 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1299 * @param[in] dbi A database handle returned by #mdb_dbi_open() 1300 * @param[in] key The key to store in the database 1301 * @param[in,out] data The data to store 1302 * @param[in] flags Special options for this operation. This parameter 1303 * must be set to 0 or by bitwise OR'ing together one or more of the 1304 * values described here. 1305 * <ul> 1306 * <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not 1307 * already appear in the database. This flag may only be specified 1308 * if the database was opened with #MDB_DUPSORT. The function will 1309 * return #MDB_KEYEXIST if the key/data pair already appears in the 1310 * database. 1311 * <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key 1312 * does not already appear in the database. The function will return 1313 * #MDB_KEYEXIST if the key already appears in the database, even if 1314 * the database supports duplicates (#MDB_DUPSORT). The \b data 1315 * parameter will be set to point to the existing item. 1316 * <li>#MDB_RESERVE - reserve space for data of the given size, but 1317 * don't copy the given data. Instead, return a pointer to the 1318 * reserved space, which the caller can fill in later - before 1319 * the next update operation or the transaction ends. This saves 1320 * an extra memcpy if the data is being generated later. 1321 * LMDB does nothing else with this memory, the caller is expected 1322 * to modify all of the space requested. This flag must not be 1323 * specified if the database was opened with #MDB_DUPSORT. 1324 * <li>#MDB_APPEND - append the given key/data pair to the end of the 1325 * database. This option allows fast bulk loading when keys are 1326 * already known to be in the correct order. Loading unsorted keys 1327 * with this flag will cause a #MDB_KEYEXIST error. 1328 * <li>#MDB_APPENDDUP - as above, but for sorted dup data. 1329 * </ul> 1330 * @return A non-zero error value on failure and 0 on success. Some possible 1331 * errors are: 1332 * <ul> 1333 * <li>#MDB_MAP_FULL - the database is full, see #mdb_env_set_mapsize(). 1334 * <li>#MDB_TXN_FULL - the transaction has too many dirty pages. 1335 * <li>EACCES - an attempt was made to write in a read-only transaction. 1336 * <li>EINVAL - an invalid parameter was specified. 1337 * </ul> 1338 */ 1339 int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data, 1340 unsigned int flags); 1341 1342 /** @brief Delete items from a database. 1343 * 1344 * This function removes key/data pairs from the database. 1345 * If the database does not support sorted duplicate data items 1346 * (#MDB_DUPSORT) the data parameter is ignored. 1347 * If the database supports sorted duplicates and the data parameter 1348 * is NULL, all of the duplicate data items for the key will be 1349 * deleted. Otherwise, if the data parameter is non-NULL 1350 * only the matching data item will be deleted. 1351 * This function will return #MDB_NOTFOUND if the specified key/data 1352 * pair is not in the database. 1353 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1354 * @param[in] dbi A database handle returned by #mdb_dbi_open() 1355 * @param[in] key The key to delete from the database 1356 * @param[in] data The data to delete 1357 * @return A non-zero error value on failure and 0 on success. Some possible 1358 * errors are: 1359 * <ul> 1360 * <li>EACCES - an attempt was made to write in a read-only transaction. 1361 * <li>EINVAL - an invalid parameter was specified. 1362 * </ul> 1363 */ 1364 int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data); 1365 1366 /** @brief Create a cursor handle. 1367 * 1368 * A cursor is associated with a specific transaction and database. 1369 * A cursor cannot be used when its database handle is closed. Nor 1370 * when its transaction has ended, except with #mdb_cursor_renew(). 1371 * It can be discarded with #mdb_cursor_close(). 1372 * A cursor in a write-transaction can be closed before its transaction 1373 * ends, and will otherwise be closed when its transaction ends. 1374 * A cursor in a read-only transaction must be closed explicitly, before 1375 * or after its transaction ends. It can be reused with 1376 * #mdb_cursor_renew() before finally closing it. 1377 * @note Earlier documentation said that cursors in every transaction 1378 * were closed when the transaction committed or aborted. 1379 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1380 * @param[in] dbi A database handle returned by #mdb_dbi_open() 1381 * @param[out] cursor Address where the new #MDB_cursor handle will be stored 1382 * @return A non-zero error value on failure and 0 on success. Some possible 1383 * errors are: 1384 * <ul> 1385 * <li>EINVAL - an invalid parameter was specified. 1386 * </ul> 1387 */ 1388 int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor); 1389 1390 /** @brief Close a cursor handle. 1391 * 1392 * The cursor handle will be freed and must not be used again after this call. 1393 * Its transaction must still be live if it is a write-transaction. 1394 * @param[in] cursor A cursor handle returned by #mdb_cursor_open() 1395 */ 1396 void mdb_cursor_close(MDB_cursor *cursor); 1397 1398 /** @brief Renew a cursor handle. 1399 * 1400 * A cursor is associated with a specific transaction and database. 1401 * Cursors that are only used in read-only 1402 * transactions may be re-used, to avoid unnecessary malloc/free overhead. 1403 * The cursor may be associated with a new read-only transaction, and 1404 * referencing the same database handle as it was created with. 1405 * This may be done whether the previous transaction is live or dead. 1406 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1407 * @param[in] cursor A cursor handle returned by #mdb_cursor_open() 1408 * @return A non-zero error value on failure and 0 on success. Some possible 1409 * errors are: 1410 * <ul> 1411 * <li>EINVAL - an invalid parameter was specified. 1412 * </ul> 1413 */ 1414 int mdb_cursor_renew(MDB_txn *txn, MDB_cursor *cursor); 1415 1416 /** @brief Return the cursor's transaction handle. 1417 * 1418 * @param[in] cursor A cursor handle returned by #mdb_cursor_open() 1419 */ 1420 MDB_txn *mdb_cursor_txn(MDB_cursor *cursor); 1421 1422 /** @brief Return the cursor's database handle. 1423 * 1424 * @param[in] cursor A cursor handle returned by #mdb_cursor_open() 1425 */ 1426 MDB_dbi mdb_cursor_dbi(MDB_cursor *cursor); 1427 1428 /** @brief Retrieve by cursor. 1429 * 1430 * This function retrieves key/data pairs from the database. The address and length 1431 * of the key are returned in the object to which \b key refers (except for the 1432 * case of the #MDB_SET option, in which the \b key object is unchanged), and 1433 * the address and length of the data are returned in the object to which \b data 1434 * refers. 1435 * See #mdb_get() for restrictions on using the output values. 1436 * @param[in] cursor A cursor handle returned by #mdb_cursor_open() 1437 * @param[in,out] key The key for a retrieved item 1438 * @param[in,out] data The data of a retrieved item 1439 * @param[in] op A cursor operation #MDB_cursor_op 1440 * @return A non-zero error value on failure and 0 on success. Some possible 1441 * errors are: 1442 * <ul> 1443 * <li>#MDB_NOTFOUND - no matching key found. 1444 * <li>EINVAL - an invalid parameter was specified. 1445 * </ul> 1446 */ 1447 int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data, 1448 MDB_cursor_op op); 1449 1450 /** @brief Store by cursor. 1451 * 1452 * This function stores key/data pairs into the database. 1453 * The cursor is positioned at the new item, or on failure usually near it. 1454 * @note Earlier documentation incorrectly said errors would leave the 1455 * state of the cursor unchanged. 1456 * @param[in] cursor A cursor handle returned by #mdb_cursor_open() 1457 * @param[in] key The key operated on. 1458 * @param[in] data The data operated on. 1459 * @param[in] flags Options for this operation. This parameter 1460 * must be set to 0 or one of the values described here. 1461 * <ul> 1462 * <li>#MDB_CURRENT - replace the item at the current cursor position. 1463 * The \b key parameter must still be provided, and must match it. 1464 * If using sorted duplicates (#MDB_DUPSORT) the data item must still 1465 * sort into the same place. This is intended to be used when the 1466 * new data is the same size as the old. Otherwise it will simply 1467 * perform a delete of the old record followed by an insert. 1468 * <li>#MDB_NODUPDATA - enter the new key/data pair only if it does not 1469 * already appear in the database. This flag may only be specified 1470 * if the database was opened with #MDB_DUPSORT. The function will 1471 * return #MDB_KEYEXIST if the key/data pair already appears in the 1472 * database. 1473 * <li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key 1474 * does not already appear in the database. The function will return 1475 * #MDB_KEYEXIST if the key already appears in the database, even if 1476 * the database supports duplicates (#MDB_DUPSORT). 1477 * <li>#MDB_RESERVE - reserve space for data of the given size, but 1478 * don't copy the given data. Instead, return a pointer to the 1479 * reserved space, which the caller can fill in later - before 1480 * the next update operation or the transaction ends. This saves 1481 * an extra memcpy if the data is being generated later. This flag 1482 * must not be specified if the database was opened with #MDB_DUPSORT. 1483 * <li>#MDB_APPEND - append the given key/data pair to the end of the 1484 * database. No key comparisons are performed. This option allows 1485 * fast bulk loading when keys are already known to be in the 1486 * correct order. Loading unsorted keys with this flag will cause 1487 * a #MDB_KEYEXIST error. 1488 * <li>#MDB_APPENDDUP - as above, but for sorted dup data. 1489 * <li>#MDB_MULTIPLE - store multiple contiguous data elements in a 1490 * single request. This flag may only be specified if the database 1491 * was opened with #MDB_DUPFIXED. The \b data argument must be an 1492 * array of two MDB_vals. The mv_size of the first MDB_val must be 1493 * the size of a single data element. The mv_data of the first MDB_val 1494 * must point to the beginning of the array of contiguous data elements. 1495 * The mv_size of the second MDB_val must be the count of the number 1496 * of data elements to store. On return this field will be set to 1497 * the count of the number of elements actually written. The mv_data 1498 * of the second MDB_val is unused. 1499 * </ul> 1500 * @return A non-zero error value on failure and 0 on success. Some possible 1501 * errors are: 1502 * <ul> 1503 * <li>#MDB_MAP_FULL - the database is full, see #mdb_env_set_mapsize(). 1504 * <li>#MDB_TXN_FULL - the transaction has too many dirty pages. 1505 * <li>EACCES - an attempt was made to write in a read-only transaction. 1506 * <li>EINVAL - an invalid parameter was specified. 1507 * </ul> 1508 */ 1509 int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data, 1510 unsigned int flags); 1511 1512 /** @brief Delete current key/data pair 1513 * 1514 * This function deletes the key/data pair to which the cursor refers. 1515 * This does not invalidate the cursor, so operations such as MDB_NEXT 1516 * can still be used on it. 1517 * Both MDB_NEXT and MDB_GET_CURRENT will return the same record after 1518 * this operation. 1519 * @param[in] cursor A cursor handle returned by #mdb_cursor_open() 1520 * @param[in] flags Options for this operation. This parameter 1521 * must be set to 0 or one of the values described here. 1522 * <ul> 1523 * <li>#MDB_NODUPDATA - delete all of the data items for the current key. 1524 * This flag may only be specified if the database was opened with #MDB_DUPSORT. 1525 * </ul> 1526 * @return A non-zero error value on failure and 0 on success. Some possible 1527 * errors are: 1528 * <ul> 1529 * <li>EACCES - an attempt was made to write in a read-only transaction. 1530 * <li>EINVAL - an invalid parameter was specified. 1531 * </ul> 1532 */ 1533 int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags); 1534 1535 /** @brief Return count of duplicates for current key. 1536 * 1537 * This call is only valid on databases that support sorted duplicate 1538 * data items #MDB_DUPSORT. 1539 * @param[in] cursor A cursor handle returned by #mdb_cursor_open() 1540 * @param[out] countp Address where the count will be stored 1541 * @return A non-zero error value on failure and 0 on success. Some possible 1542 * errors are: 1543 * <ul> 1544 * <li>EINVAL - cursor is not initialized, or an invalid parameter was specified. 1545 * </ul> 1546 */ 1547 int mdb_cursor_count(MDB_cursor *cursor, size_t *countp); 1548 1549 /** @brief Compare two data items according to a particular database. 1550 * 1551 * This returns a comparison as if the two data items were keys in the 1552 * specified database. 1553 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1554 * @param[in] dbi A database handle returned by #mdb_dbi_open() 1555 * @param[in] a The first item to compare 1556 * @param[in] b The second item to compare 1557 * @return < 0 if a < b, 0 if a == b, > 0 if a > b 1558 */ 1559 int mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b); 1560 1561 /** @brief Compare two data items according to a particular database. 1562 * 1563 * This returns a comparison as if the two items were data items of 1564 * the specified database. The database must have the #MDB_DUPSORT flag. 1565 * @param[in] txn A transaction handle returned by #mdb_txn_begin() 1566 * @param[in] dbi A database handle returned by #mdb_dbi_open() 1567 * @param[in] a The first item to compare 1568 * @param[in] b The second item to compare 1569 * @return < 0 if a < b, 0 if a == b, > 0 if a > b 1570 */ 1571 int mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b); 1572 1573 /** @brief A callback function used to print a message from the library. 1574 * 1575 * @param[in] msg The string to be printed. 1576 * @param[in] ctx An arbitrary context pointer for the callback. 1577 * @return < 0 on failure, >= 0 on success. 1578 */ 1579 typedef int (MDB_msg_func)(const char *msg, void *ctx); 1580 1581 /** @brief Dump the entries in the reader lock table. 1582 * 1583 * @param[in] env An environment handle returned by #mdb_env_create() 1584 * @param[in] func A #MDB_msg_func function 1585 * @param[in] ctx Anything the message function needs 1586 * @return < 0 on failure, >= 0 on success. 1587 */ 1588 int mdb_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx); 1589 1590 /** @brief Check for stale entries in the reader lock table. 1591 * 1592 * @param[in] env An environment handle returned by #mdb_env_create() 1593 * @param[out] dead Number of stale slots that were cleared 1594 * @return 0 on success, non-zero on failure. 1595 */ 1596 int mdb_reader_check(MDB_env *env, int *dead); 1597 /** @} */ 1598 1599 #ifdef __cplusplus 1600 } 1601 #endif 1602 /** @page tools LMDB Command Line Tools 1603 The following describes the command line tools that are available for LMDB. 1604 \li \ref mdb_copy_1 1605 \li \ref mdb_dump_1 1606 \li \ref mdb_load_1 1607 \li \ref mdb_stat_1 1608 */ 1609 1610 #endif /* _LMDB_H_ */ 1611