mdb.h 38.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/** @file mdb.h
 *	@brief memory-mapped database library
 *
 *	@mainpage	MDB Memory-Mapped Database Manager
 *	MDB is a Btree-based database management library modeled loosely on the
 *	BerkeleyDB API, but much simplified. The entire database is exposed
 *	in a read-only memory map, and all data fetches return data directly
 *	from the mapped memory, so no malloc's or memcpy's occur during
 *	data fetches. As such, the library is extremely simple because it
 *	requires no page caching layer of its own, and it is extremely high
 *	performance and memory-efficient. It is also fully transactional with
 *	full ACID semantics, and because the memory map is read-only, the
 *	database integrity cannot be corrupted by stray pointer writes from
 *	application code.
 *
 *	The library is fully thread-aware and supports concurrent read/write
 *	access from multiple processes and threads. Data pages use a copy-on-
 *	write strategy so no active data pages are ever overwritten, which
 *	also provides resistance to corruption and eliminates the need of any
 *	special recovery procedures after a system crash. Writes are fully
 *	serialized; only one write transaction may be active at a time, which
 *	guarantees that writers can never deadlock. The database structure is
 *	multi-versioned so readers run with no locks; writers cannot block
 *	readers, and readers don't block writers.
 *
 *	Unlike other well-known database mechanisms which use either write-ahead
 *	transaction logs or append-only data writes, MDB requires no maintenance
 *	during operation. Both write-ahead loggers and append-only databases
 *	require periodic checkpointing and/or compaction of their log or database
 *	files otherwise they grow without bound. MDB tracks free pages within
 *	the database and re-uses them for new write operations, so the database
 *	size does not grow without bound in normal use.
 *
 *	@author	Howard Chu, Symas Corporation.
 *
 *	@copyright Copyright 2011 Howard Chu, Symas Corp. All rights reserved.
Howard Chu's avatar
Howard Chu committed
37
38
39
40
41
42
43
44
45
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 *
46
 * 	@par Derived From:
Howard Chu's avatar
Howard Chu committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 * This code is derived from btree.c written by Martin Hedenfalk.
 *
 * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
Howard Chu's avatar
Howard Chu committed
63
64
65
66
67
#ifndef _MDB_H_
#define _MDB_H_

#include <sys/types.h>

68
69
70
/** @defgroup public Public API
 *	@{
 */
71
72
73
74
/** @defgroup Version Version Macros
 *	@{
 */
/** Library major version */
75
#define MDB_VERSION_MAJOR	0
76
/** Library minor version */
Howard Chu's avatar
Howard Chu committed
77
#define MDB_VERSION_MINOR	9
78
/** Library patch version */
79
#define MDB_VERSION_PATCH	0
80
81

/** Combine args a,b,c into a single integer for easy version comparisons */
82
#define MDB_VERINT(a,b,c)	(((a) << 24) | ((b) << 16) | (c))
83
84

/** The full library version as a single integer */
Howard Chu's avatar
Howard Chu committed
85
86
#define MDB_VERSION_FULL	\
	MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH)
87
88

/** The release date of this library version */
Howard Chu's avatar
Howard Chu committed
89
#define MDB_VERSION_DATE	"September 1, 2011"
90
91

/** A stringifier for the version info */
Howard Chu's avatar
Howard Chu committed
92
#define MDB_VERSTR(a,b,c,d)	"MDB " #a "." #b "." #c ": (" #d ")"
93
94

/** A helper for the stringifier macro */
Howard Chu's avatar
Howard Chu committed
95
#define MDB_VERFOO(a,b,c,d)	MDB_VERSTR(a,b,c,d)
96
97

/** The full library version as a C string */
Howard Chu's avatar
Howard Chu committed
98
99
#define	MDB_VERSION_STRING	\
	MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE)
100
/**	@} */
101

Howard Chu's avatar
Howard Chu committed
102
103
104
105
/** Opaque structure for a database environment. A DB environment supports multiple
 * databases, all residing in the same shared-memory map.
 */
typedef struct MDB_env MDB_env;
106
107
108
109

/** Opaque structure for a transaction handle. All database operations require
 * a transaction handle. Transactions may be read-only or read-write.
 */
Howard Chu's avatar
Howard Chu committed
110
typedef struct MDB_txn MDB_txn;
111
112

/** A handle for an individual database in the DB environment. */
Howard Chu's avatar
Howard Chu committed
113
114
typedef unsigned int	MDB_dbi;

Howard Chu's avatar
Howard Chu committed
115
116
117
/** Opaque structure for navigating through a database */
typedef struct MDB_cursor MDB_cursor;

118
/** Generic structure used for passing keys and data in and out of the database. */
Howard Chu's avatar
Howard Chu committed
119
typedef struct MDB_val {
120
121
	size_t		 mv_size;	/**< size of the data item */
	void		*mv_data;	/**< address of the data item */
Howard Chu's avatar
Howard Chu committed
122
123
} MDB_val;

124
/** A callback function used to compare two keys in a database */
Howard Chu's avatar
Howard Chu committed
125
typedef int  (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
126
127

/** A callback function used to relocate a position-dependent data item
Howard Chu's avatar
Howard Chu committed
128
129
130
131
132
 * in a fixed-address database. The \b newptr gives the item's current address in
 * the memory map, and \b oldptr gives its previous address. This callback is
 * expected to walk through the fields of the record in newptr and modify any
 * values based at the \b oldptr address to be relative to the \b newptr address.
 * @todo This feature is currently unimplemented.
133
 */
Howard Chu's avatar
Howard Chu committed
134
135
typedef void (MDB_rel_func)(void *newptr, void *oldptr, size_t size);

136
/** @defgroup	mdb_env	Environment Flags
Howard Chu's avatar
Howard Chu committed
137
138
139
140
141
142
143
144
145
146
 *	@{
 */
	/** mmap at a fixed address */
#define MDB_FIXEDMAP	0x01
	/** don't fsync after commit */
#define MDB_NOSYNC		0x10000
	/** read only */
#define MDB_RDONLY		0x20000
/** @} */

147
/**	@defgroup	mdb_open	Database Flags
Howard Chu's avatar
Howard Chu committed
148
149
150
151
152
153
 *	@{
 */
	/** use reverse string keys */
#define MDB_REVERSEKEY	0x02
	/** use sorted duplicates */
#define MDB_DUPSORT		0x04
Howard Chu's avatar
Howard Chu committed
154
	/** numeric keys in native byte order.
155
	 *  The keys must all be of the same size. */
Howard Chu's avatar
Howard Chu committed
156
157
158
159
160
#define MDB_INTEGERKEY	0x08
	/** with #MDB_DUPSORT, sorted dup items have fixed size */
#define MDB_DUPFIXED	0x10
	/** with #MDB_DUPSORT, dups are numeric in native byte order */
#define MDB_INTEGERDUP	0x20
Howard Chu's avatar
Howard Chu committed
161
162
	/** with #MDB_DUPSORT, use reverse string dups */
#define MDB_REVERSEDUP	0x40
Howard Chu's avatar
Howard Chu committed
163
164
165
	/** create DB if not already existing */
#define MDB_CREATE		0x40000
/** @} */
Howard Chu's avatar
Howard Chu committed
166

167
/**	@defgroup mdb_put	Write Flags
168
169
 *	@{
 */
170
/** For put: Don't write if the key already exists. */
Howard Chu's avatar
Howard Chu committed
171
#define MDB_NOOVERWRITE	0x10
172
173
174
/** Only for #MDB_DUPSORT<br>
 * For put: don't write if the key and data pair already exist.<br>
 * For mdb_cursor_del: remove all duplicate data items.
175
 */
Howard Chu's avatar
Howard Chu committed
176
#define MDB_NODUPDATA	0x20
177
178
/** For mdb_cursor_put: overwrite the current key/data pair */
#define MDB_CURRENT	0x40
179
180
181
182
183
/*	@} */

/** Cursor operations */
typedef enum MDB_cursor_op {
	MDB_FIRST,				/**< Position at first key/data item */
184
185
	MDB_FIRST_DUP,			/**< Position at first data item of current key.
								Only for #MDB_DUPSORT */
186
187
188
189
190
	MDB_GET_BOTH,			/**< Position at key/data pair. Only for #MDB_DUPSORT */
	MDB_GET_BOTH_RANGE,		/**< position at key, nearest data. Only for #MDB_DUPSORT */
	MDB_GET_MULTIPLE,		/**< Return all the duplicate data items at the current
								 cursor position. Only for #MDB_DUPFIXED */
	MDB_LAST,				/**< Position at last key/data item */
191
192
	MDB_LAST_DUP,			/**< Position at last data item of current key.
								Only for #MDB_DUPSORT */
193
194
195
196
197
198
199
200
201
202
203
	MDB_NEXT,				/**< Position at next data item */
	MDB_NEXT_DUP,			/**< Position at next data item of current key.
								Only for #MDB_DUPSORT */
	MDB_NEXT_MULTIPLE,		/**< Return all duplicate data items at the next
								cursor position. Only for #MDB_DUPFIXED */
	MDB_NEXT_NODUP,			/**< Position at first data item of next key.
								Only for #MDB_DUPSORT */
	MDB_PREV,				/**< Position at previous data item */
	MDB_PREV_DUP,			/**< Position at previous data item of current key.
								Only for #MDB_DUPSORT */
	MDB_PREV_NODUP,			/**< Position at last data item of previous key.
204
								Only for #MDB_DUPSORT */
205
206
	MDB_SET,				/**< Position at specified key */
	MDB_SET_RANGE			/**< Position at first key greater than or equal to specified key. */
Howard Chu's avatar
Howard Chu committed
207
208
} MDB_cursor_op;

209
210
211
212
213
214
/** @defgroup  errors	Return Codes
 *
 *	BerkeleyDB uses -30800 to -30999, we'll go under them
 *	@{
 */
	/**	Successful result */
Howard Chu's avatar
Howard Chu committed
215
#define MDB_SUCCESS	 0
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
	/** key/data pair already exists */
#define MDB_KEYEXIST	(-30799)
	/** key/data pair not found (EOF) */
#define MDB_NOTFOUND	(-30798)
	/** Requested page not found - this usually indicates corruption */
#define MDB_PAGE_NOTFOUND	(-30797)
	/** Located page was wrong type */
#define MDB_CORRUPTED	(-30796)
	/** Update of meta page failed, probably I/O error */
#define MDB_PANIC		(-30795)
	/** Environment version mismatch */
#define MDB_VERSION_MISMATCH	(-30794)
/** @} */

/** Statistics for a database in the environment */
Howard Chu's avatar
Howard Chu committed
231
typedef struct MDB_stat {
232
233
234
235
236
237
238
	unsigned int	ms_psize;			/**< Size of a database page.
											This is currently the same for all databases. */
	unsigned int	ms_depth;			/**< Depth (height) of the B-tree */
	unsigned long	ms_branch_pages;	/**< Number of internal (non-leaf) pages */
	unsigned long	ms_leaf_pages;		/**< Number of leaf pages */
	unsigned long	ms_overflow_pages;	/**< Number of overflow pages */
	unsigned long	ms_entries;			/**< Number of data items */
Howard Chu's avatar
Howard Chu committed
239
240
} MDB_stat;

241
242
243
244
245
246
	/** Return the mdb library version information.
	 * @param[out] major if non-NULL, the library major version number is copied here
	 * @param[out] minor if non-NULL, the library minor version number is copied here
	 * @param[out] patch if non-NULL, the library patch version number is copied here
	 * @retval "version string" The library version as a string
	 */
Howard Chu's avatar
Howard Chu committed
247
char *mdb_version(int *major, int *minor, int *patch);
248
249
250
251
252
253
254
255
256
257

	/** Return a string describing a given error code.
	 * This function is a superset of the ANSI C X3.159-1989 (ANSI C) strerror(3)
	 * function. If the error code is greater than or equal to 0, then the string
	 * returned by the system function strerror(3) is returned. If the error code
	 * is less than 0, an error string corresponding to the MDB library error is
	 * returned. See @ref errors for a list of MDB-specific error codes.
	 * @param[in] err The error code
	 * @retval "error message" The description of the error
	 */
258
char *mdb_strerror(int err);
259
260
261
262
263
264
265
266
267
268
269

	/** Create an MDB environment handle.
	 * This function allocates memory for a #MDB_env structure. To release
	 * the allocated memory and discard the handle, call #mdb_env_close().
	 * Before the handle may be used, it must be opened using #mdb_env_open().
	 * Various other options may also need to be set before opening the handle,
	 * e.g. #mdb_env_set_mapsize(), #mdb_env_set_maxreaders(), #mdb_env_set_maxdbs(),
	 * depending on usage requirements.
	 * @param[out] env The address where the new handle will be stored
	 * @return A non-zero error value on failure and 0 on success.
	 */
Howard Chu's avatar
Howard Chu committed
270
int  mdb_env_create(MDB_env **env);
271
272
273
274

	/** Open an environment handle.
	 * If this function fails, #mdb_env_close() must be called to discard the #MDB_env handle.
	 * @param[in] env An environment handle returned by #mdb_env_create()
Howard Chu's avatar
Howard Chu committed
275
276
	 * @param[in] path The directory in which the database files reside. This
	 * directory must already exist and be writable.
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
	 * @param[in] flags Special options for this environment. This parameter
	 * must be set to 0 or by bitwise OR'ing together one or more of the
	 * values described here.
	 * <ul>
	 *	<li>#MDB_FIXEDMAP
	 *      use a fixed address for the mmap region. This flag must be specified
	 *      when creating the environment, and is stored persistently in the environment.
	 *		If successful, the memory map will always reside at the same virtual address
	 *		and pointers used to reference data items in the database will be constant
	 *		across multiple invocations. This option may not always work, depending on
	 *		how the operating system has allocated memory to shared libraries and other uses.
	 *		The feature is highly experimental.
	 *	<li>#MDB_NOSYNC
	 *		Don't perform a synchronous flush after committing a transaction. This means
	 *		transactions will exhibit the ACI (atomicity, consistency, and isolation)
	 *		properties, but not D (durability); that is database integrity will be
	 *		maintained but it is possible some number of the most recently committed
	 *		transactions may be undone after a system crash. The number of transactions
	 *		at risk is governed by how often the system flushes dirty buffers to disk
	 *		and how often #mdb_env_sync() is called. This flag may be changed
	 *		at any time using #mdb_env_set_flags().
	 *	<li>#MDB_RDONLY
	 *		Open the environment in read-only mode. No write operations will be allowed.
	 * </ul>
Howard Chu's avatar
Howard Chu committed
301
302
	 * @param[in] mode The UNIX permissions to set on created files. This parameter
	 * is ignored on Windows.
303
304
305
306
307
308
309
310
311
312
313
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>#MDB_VERSION_MISMATCH - the version of the MDB library doesn't match the
	 *	version that created the database environment.
	 *	<li>EINVAL - the environment file headers are corrupted.
	 *	<li>ENOENT - the directory specified by the path parameter doesn't exist.
	 *	<li>EACCES - the user didn't have permission to access the environment files.
	 *	<li>EAGAIN - the environment was locked by another process.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
314
int  mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
315
316
317
318
319
320

	/** Return statistics about the MDB environment.
	 * @param[in] env An environment handle returned by #mdb_env_create()
	 * @param[out] stat The address of an #MDB_stat structure
	 * 	where the statistics will be copied
	 */
Howard Chu's avatar
Howard Chu committed
321
int  mdb_env_stat(MDB_env *env, MDB_stat *stat);
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338

	/** Flush the data buffers to disk.
	 * Data is always written to disk when #mdb_txn_commit() is called,
	 * but the operating system may keep it buffered. MDB always flushes
	 * the OS buffers upon commit as well, unless the environment was
	 * opened with #MDB_NOSYNC.
	 * @param[in] env An environment handle returned by #mdb_env_create()
	 * @param[in] force If non-zero, force the flush to occur. Otherwise
	 *  if the environment has the #MDB_NOSYNC flag set the flushes
	 *	will be omitted.
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EINVAL - an invalid parameter was specified.
	 *	<li>EIO - an error occurred during synchronization.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
339
int  mdb_env_sync(MDB_env *env, int force);
340
341
342
343
344
345
346
347

	/** Close the environment and release the memory map.
	 * Only a single thread may call this function. All transactions, databases,
	 * and cursors must already be closed before calling this function. Attempts to
	 * use any such handles after calling this function will cause a SIGSEGV.
	 * The environment handle will be freed and must not be used again after this call.
	 * @param[in] env An environment handle returned by #mdb_env_create()
	 */
Howard Chu's avatar
Howard Chu committed
348
void mdb_env_close(MDB_env *env);
349
350
351
352
353
354
355
356
357
358
359
360
361
362

	/** Set environment flags.
	 * This may be used to set some flags that weren't already set during
	 * #mdb_env_open(), or to unset these flags. Currently only the
	 * #MDB_NOSYNC flag setting may be changed with this function.
	 * @param[in] env An environment handle returned by #mdb_env_create()
	 * @param[in] flags The flags to change, bitwise OR'ed together
	 * @param[in] onoff A non-zero value sets the flags, zero clears them.
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EINVAL - an invalid parameter was specified.
	 * </ul>
	 */
363
int  mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
364
365
366
367
368
369
370
371
372
373

	/** Get environment flags.
	 * @param[in] env An environment handle returned by #mdb_env_create()
	 * @param[out] flags The address of an integer to store the flags
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EINVAL - an invalid parameter was specified.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
374
int  mdb_env_get_flags(MDB_env *env, unsigned int *flags);
375
376
377
378
379
380
381
382
383
384
385
386

	/** Return the path that was used in #mdb_env_open().
	 * @param[in] env An environment handle returned by #mdb_env_create()
	 * @param[out] path Address of a string pointer to contain the path. This
	 * is the actual string in the environment, not a copy. It should not be
	 * altered in any way.
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EINVAL - an invalid parameter was specified.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
387
int  mdb_env_get_path(MDB_env *env, const char **path);
388
389
390

	/** Set the size of the memory map to use for this environment.
	 * The size should be a multiple of the OS page size. The default is
Howard Chu's avatar
Howard Chu committed
391
392
	 * 10485760 bytes. The size of the memory map is also the maximum size
	 * of the database. The value should be chosen as large as possible,
393
394
395
396
397
398
399
400
401
402
	 * to accomodate future growth of the database.
	 * This function may only be called after #mdb_env_create() and before #mdb_env_open().
	 * @param[in] env An environment handle returned by #mdb_env_create()
	 * @param[in] size The size in bytes
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EINVAL - an invalid parameter was specified, or the environment is already open.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
403
int  mdb_env_set_mapsize(MDB_env *env, size_t size);
404
405
406
407
408
409
410
411
412
413
414
415
416

	/** Set the maximum number of threads for the environment.
	 * This defines the number of slots in the lock table that is used to track readers in the
	 * the environment. The default is 126.
	 * This function may only be called after #mdb_env_create() and before #mdb_env_open().
	 * @param[in] env An environment handle returned by #mdb_env_create()
	 * @param[in] readers The maximum number of threads
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EINVAL - an invalid parameter was specified, or the environment is already open.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
417
int  mdb_env_set_maxreaders(MDB_env *env, int readers);
418
419
420
421
422
423
424
425
426
427

	/** Get the maximum number of threads for the environment.
	 * @param[in] env An environment handle returned by #mdb_env_create()
	 * @param[out] readers Address of an integer to store the number of readers
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EINVAL - an invalid parameter was specified.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
428
int  mdb_env_get_maxreaders(MDB_env *env, int *readers);
429
430
431
432
433
434
435
436
437
438
439
440
441
442

	/** Set the maximum number of databases for the environment.
	 * This function is only needed if multiple databases will be used in the
	 * environment. Simpler applications that only use a single database can ignore
	 * this option.
	 * This function may only be called after #mdb_env_create() and before #mdb_env_open().
	 * @param[in] env An environment handle returned by #mdb_env_create()
	 * @param[in] dbs The maximum number of databases
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EINVAL - an invalid parameter was specified, or the environment is already open.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
443
int  mdb_env_set_maxdbs(MDB_env *env, int dbs);
Howard Chu's avatar
Howard Chu committed
444

445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
	/** Create a transaction for use with the environment.
	 * The transaction handle may be discarded using #mdb_txn_abort() or #mdb_txn_commit().
	 * @note Transactions may not span threads, a transaction must only be used by a
	 * single thread.
	 * @note Cursors may not span transactions; each cursor must be opened and closed
	 * within a single transaction.
	 * @param[in] env An environment handle returned by #mdb_env_create()
	 * @param[in] flags Special options for this transaction. This parameter
	 * must be set to 0 or by bitwise OR'ing together one or more of the
	 * values described here.
	 * <ul>
	 *	<li>#MDB_RDONLY
	 *		This transaction will not perform any write operations.
	 * </ul>
	 * @param[out] txn Address where the new #MDB_txn handle will be stored
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>#MDB_PANIC - a fatal error occurred earlier and the environment
	 *		must be shut down.
	 *	<li>ENOMEM - out of memory, or a read-only transaction was requested and
	 *		the reader lock table is full. See #mdb_env_set_maxreaders().
	 * </ul>
	 */
int  mdb_txn_begin(MDB_env *env, unsigned int flags, MDB_txn **txn);

	/** Commit all the operations of a transaction into the database.
	 * All cursors opened within the transaction must be closed before the transaction
	 * is committed.
	 * The transaction handle will be freed and must not be used again after this call.
	 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EINVAL - an invalid parameter was specified.
	 *	<li>ENOSPC - no more disk space.
	 *	<li>EIO - a low-level I/O error occurred while writing.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
484
int  mdb_txn_commit(MDB_txn *txn);
485
486
487
488
489
490
491

	/** Abandon all the operations of the transaction instead of saving them.
	 * All cursors opened within the transaction must be closed before the transaction
	 * is aborted.
	 * The transaction handle will be freed and must not be used again after this call.
	 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
	 */
Howard Chu's avatar
Howard Chu committed
492
493
void mdb_txn_abort(MDB_txn *txn);

494
495
496
	/** Reset a read-only transaction.
	 * This releases the current reader lock but doesn't free the
	 * transaction handle, allowing it to be used again later by #mdb_txn_renew().
497
	 * It otherwise has the same effect as #mdb_txn_abort() but saves some memory
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
	 * allocation/deallocation overhead if a thread is going to start a new
	 * read-only transaction again soon.
	 * All cursors opened within the transaction must be closed before the transaction
	 * is reset.
	 * Reader locks generally don't interfere with writers, but they keep old
	 * versions of database pages allocated. Thus they prevent the old pages
	 * from being reused when writers commit new data, and so under heavy load
	 * the database size may grow much more rapidly than otherwise.
	 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
	 */
void mdb_txn_reset(MDB_txn *txn);

	/** Renew a read-only transaction.
	 * This acquires a new reader lock for a transaction handle that had been
	 * released by #mdb_txn_reset(). It must be called before a reset transaction
	 * may be used again.
	 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>#MDB_PANIC - a fatal error occurred earlier and the environment
	 *		must be shut down.
	 *	<li>EINVAL - an invalid parameter was specified.
	 * </ul>
	 */
int  mdb_txn_renew(MDB_txn *txn);

	/** Open a database in the environment.
	 * The database handle may be discarded by calling #mdb_close().
	 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
	 * @param[in] name The name of the database to open. If only a single
	 * 	database is needed in the enviroment, this value may be NULL.
	 * @param[in] flags Special options for this database. This parameter
	 * must be set to 0 or by bitwise OR'ing together one or more of the
	 * values described here.
	 * <ul>
	 *	<li>#MDB_REVERSEKEY
	 *		Keys are strings to be compared in reverse order, from the end
	 *		of the strings to the beginning. By default, Keys are treated as strings and
	 *		compared from beginning to end.
	 *	<li>#MDB_DUPSORT
	 *		Duplicate keys may be used in the database. (Or, from another perspective,
	 *		keys may have multiple data items, stored in sorted order.) By default
	 *		keys must be unique and may have only a single data item.
	 *	<li>#MDB_INTEGERKEY
543
544
545
	 *		Keys are binary integers in native byte order. Setting this option
	 *		requires all keys to be the same size, typically sizeof(int)
	 *		or sizeof(long).
546
547
548
549
550
551
552
553
554
	 *	<li>#MDB_DUPFIXED
	 *		This flag may only be used in combination with #MDB_DUPSORT. This option
	 *		tells the library that the data items for this database are all the same
	 *		size, which allows further optimizations in storage and retrieval. When
	 *		all data items are the same size, the #MDB_GET_MULTIPLE and #MDB_NEXT_MULTIPLE
	 *		cursor operations may be used to retrieve multiple items at once.
	 *	<li>#MDB_INTEGERDUP
	 *		This option specifies that duplicate data items are also integers, and
	 *		should be sorted as such.
555
556
557
	 *	<li>#MDB_REVERSEDUP
	 *		This option specifies that duplicate data items should be compared as
	 *		strings in reverse order.
558
559
560
561
562
563
564
565
566
567
568
569
570
	 *	<li>#MDB_CREATE
	 *		Create the named database if it doesn't exist. This option is not
	 *		allowed in a read-only transaction or a read-only environment.
	 * </ul>
	 * @param[out] dbi Address where the new #MDB_dbi handle will be stored
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>#MDB_NOTFOUND - the specified database doesn't exist in the environment
	 *		and #MDB_CREATE was not specified.
	 *	<li>ENFILE - too many databases have been opened. See #mdb_env_set_maxdbs().
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
571
int  mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
572
573
574
575
576
577
578
579
580
581
582
583

	/** Retrieve statistics for a database.
	 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
	 * @param[in] dbi A database handle returned by #mdb_open()
	 * @param[out] stat The address of an #MDB_stat structure
	 * 	where the statistics will be copied
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EINVAL - an invalid parameter was specified.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
584
int  mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
585
586
587
588
589

	/** Close a database handle.
	 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
	 * @param[in] dbi A database handle returned by #mdb_open()
	 */
Howard Chu's avatar
Howard Chu committed
590
void mdb_close(MDB_txn *txn, MDB_dbi dbi);
Howard Chu's avatar
Howard Chu committed
591

592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
	/** Set a custom key comparison function for a database.
	 * The comparison function is called whenever it is necessary to compare a
	 * key specified by the application with a key currently stored in the database.
	 * If no comparison function is specified, and no special key flags were specified
	 * with #mdb_open(), the keys are compared lexically, with shorter keys collating
	 * before longer keys.
	 * @warning This function must be called before any data access functions are used,
	 * otherwise data corruption may occur. The same function must be used by every
	 * process accessing the database, every time the database is used.
	 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
	 * @param[in] dbi A database handle returned by #mdb_open()
	 * @param[in] cmp A #MDB_cmp_func function
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EINVAL - an invalid parameter was specified.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
610
int  mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631

	/** Set a custom data comparison function for a #MDB_DUPSORT database.
	 * This comparison function is called whenever it is necessary to compare a data
	 * item specified by the application with a data item currently stored in the database.
	 * This function only takes effect if the database was opened with the #MDB_DUPSORT
	 * flag.
	 * If no comparison function is specified, and no special key flags were specified
	 * with #mdb_open(), the data items are compared lexically, with shorter items collating
	 * before longer items.
	 * @warning This function must be called before any data access functions are used,
	 * otherwise data corruption may occur. The same function must be used by every
	 * process accessing the database, every time the database is used.
	 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
	 * @param[in] dbi A database handle returned by #mdb_open()
	 * @param[in] cmp A #MDB_cmp_func function
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EINVAL - an invalid parameter was specified.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
632
int  mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650

	/** Set a relocation function for a #MDB_FIXEDMAP database.
	 * @todo The relocation function is called whenever it is necessary to move the data
	 * of an item to a different position in the database (e.g. through tree
	 * balancing operations, shifts as a result of adds or deletes, etc.). It is
	 * intended to allow address/position-dependent data items to be stored in
	 * a database in an environment opened with the #MDB_FIXEDMAP option.
	 * Currently the relocation feature is unimplemented and setting
	 * this function has no effect.
	 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
	 * @param[in] dbi A database handle returned by #mdb_open()
	 * @param[in] rel A #MDB_rel_func function
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EINVAL - an invalid parameter was specified.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
651
652
int  mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);

653
654
655
656
657
658
659
	/** Get items from a database.
	 * This function retrieves key/data pairs from the database. The address
	 * and length of the data associated with the specified \b key are returned
	 * in the structure to which \b data refers.
	 * If the database supports duplicate keys (#MDB_DUPSORT) then the
	 * first data item for the key will be returned. Retrieval of other
	 * items requires the use of #mdb_cursor_get().
660
661
662
663
664
	 *
	 * @note The memory pointed to by the returned values is owned by the
	 * database. The caller need not dispose of the memory, and may not
	 * modify it in any way. For values returned in a read-only transaction
	 * any modification attempts will cause a SIGSEGV.
665
666
667
668
669
670
671
672
673
674
675
	 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
	 * @param[in] dbi A database handle returned by #mdb_open()
	 * @param[in] key The key to search for in the database
	 * @param[out] data The data corresponding to the key
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>#MDB_NOTFOUND - the key was not in the database.
	 *	<li>EINVAL - an invalid parameter was specified.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
676
int  mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
677
678
679
680
681
682
683
684
685

	/** Store items into a database.
	 * This function stores key/data pairs in the database. The default behavior
	 * is to enter the new key/data pair, replacing any previously existing key
	 * if duplicates are disallowed, or adding a duplicate data item if
	 * duplicates are allowed (#MDB_DUPSORT).
	 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
	 * @param[in] dbi A database handle returned by #mdb_open()
	 * @param[in] key The key to store in the database
686
	 * @param[in,out] data The data to store
687
688
689
690
691
692
693
694
695
696
697
698
	 * @param[in] flags Special options for this operation. This parameter
	 * must be set to 0 or by bitwise OR'ing together one or more of the
	 * values described here.
	 * <ul>
	 *	<li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
	 *		already appear in the database. This flag may only be specified
	 *		if the database was opened with #MDB_DUPSORT. The function will
	 *		return #MDB_KEYEXIST if the key/data pair already appears in the
	 *		database.
	 *	<li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
	 *		does not already appear in the database. The function will return
	 *		#MDB_KEYEXIST if the key already appears in the database, even if
699
700
	 *		the database supports duplicates (#MDB_DUPSORT). The \b data
	 *		parameter will be set to point to the existing item.
701
702
703
704
705
706
707
708
709
	 * </ul>
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EACCESS - an attempt was made to write in a read-only transaction.
	 *	<li>EINVAL - an invalid parameter was specified.
	 *	<li>ENOMEM - the database is full, see #mdb_env_set_mapsize().
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
710
int  mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
Howard Chu's avatar
Howard Chu committed
711
712
			    unsigned int flags);

713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
	/** Delete items from a database.
	 * This function removes key/data pairs from the database.
	 * If the database does not support sorted duplicate data items
	 * (#MDB_DUPSORT) the data parameter is ignored.
	 * If the database supports sorted duplicates and the data parameter
	 * is NULL, all of the duplicate data items for the key will be
	 * deleted. Otherwise, if the data parameter is non-NULL
	 * only the matching data item will be deleted.
	 * This function will return #MDB_NOTFOUND if the specified key/data
	 * pair is not in the database.
	 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
	 * @param[in] dbi A database handle returned by #mdb_open()
	 * @param[in] key The key to delete from the database
	 * @param[in] data The data to delete
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EACCESS - an attempt was made to write in a read-only transaction.
	 *	<li>EINVAL - an invalid parameter was specified.
	 * </ul>
	 */
int  mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);

	/** Create a cursor handle.
	 * Cursors are associated with a specific transaction and database and
	 * may not span threads.
	 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
	 * @param[in] dbi A database handle returned by #mdb_open()
	 * @param[out] cursor Address where the new #MDB_cursor handle will be stored
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EINVAL - an invalid parameter was specified.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
748
int  mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
749
750
751
752
753

	/** Close a cursor handle.
	 * The cursor handle will be freed and must not be used again after this call.
	 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
	 */
Howard Chu's avatar
Howard Chu committed
754
void mdb_cursor_close(MDB_cursor *cursor);
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775

	/** Retrieve by cursor.
	 * This function retrieves key/data pairs from the database. The address and length
	 * of the key are returned in the object to which \b key refers (except for the
	 * case of the #MDB_SET option, in which the \b key object is unchanged), and
	 * the address and length of the data are returned in the object to which \b data
	 * refers.
	 * @bug Cursors are not coordinated with write operations. If a cursor in a
	 * write transaction is performing a sequential scan while records are being
	 * inserted or deleted in the same transaction, the cursor will be corrupted.
	 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
	 * @param[in,out] key The key for a retrieved item
	 * @param[in,out] data The data of a retrieved item
	 * @param[in] op A cursor operation #MDB_cursor_op
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>#MDB_NOTFOUND - no matching key found.
	 *	<li>EINVAL - an invalid parameter was specified.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
776
777
int  mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
			    MDB_cursor_op op);
778

779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
	/** Store by cursor.
	 * This function stores key/data pairs into the database.
	 * If the function fails for any reason, the state of the cursor will be
	 * unchanged. If the function succeeds and an item is inserted into the
	 * database, the cursor is always positioned to refer to the newly inserted item.
	 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
	 * @param[in] key The key operated on.
	 * @param[in] data The data operated on.
	 * @param[in] flags Options for this operation. This parameter
	 * must be set to 0 or one of the values described here.
	 * <ul>
	 *	<li>#MDB_CURRENT - overwrite the data of the key/data pair to which
	 *		the cursor refers with the specified data item. The \b key
	 *		parameter is ignored.
	 *	<li>#MDB_NODUPDATA - enter the new key/data pair only if it does not
	 *		already appear in the database. This flag may only be specified
	 *		if the database was opened with #MDB_DUPSORT. The function will
	 *		return #MDB_KEYEXIST if the key/data pair already appears in the
	 *		database.
	 *	<li>#MDB_NOOVERWRITE - enter the new key/data pair only if the key
	 *		does not already appear in the database. The function will return
	 *		#MDB_KEYEXIST if the key already appears in the database, even if
	 *		the database supports duplicates (#MDB_DUPSORT).
	 * </ul>
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EACCES - an attempt was made to modify a read-only database.
	 *	<li>EINVAL - an invalid parameter was specified.
	 * </ul>
	 */
int  mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
				unsigned int flags);

	/**  Delete current key/data pair
	 * This function deletes the key/data pair to which the cursor refers.
	 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
	 * @param[in] flags Options for this operation. This parameter
	 * must be set to 0 or one of the values described here.
	 * <ul>
	 *	<li>#MDB_NODUPDATA - delete all of the data items for the current key.
	 *		This flag may only be specified if the database was opened with #MDB_DUPSORT.
	 * </ul>
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EACCES - an attempt was made to modify a read-only database.
	 *	<li>EINVAL - an invalid parameter was specified.
	 * </ul>
	 */
int  mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);

831
832
833
834
835
836
837
838
839
840
841
	/** Return count of duplicates for current key.
	 * This call is only valid on databases that support sorted duplicate
	 * data items #MDB_DUPSORT.
	 * @param[in] cursor A cursor handle returned by #mdb_cursor_open()
	 * @param[out] countp Address where the count will be stored
	 * @return A non-zero error value on failure and 0 on success. Some possible
	 * errors are:
	 * <ul>
	 *	<li>EINVAL - cursor is not initialized, or an invalid parameter was specified.
	 * </ul>
	 */
Howard Chu's avatar
Howard Chu committed
842
int  mdb_cursor_count(MDB_cursor *cursor, unsigned long *countp);
Howard Chu's avatar
Howard Chu committed
843

844
845
846
847
848
849
850
851
852
	/** Compare two data items according to a particular database.
	 * This returns a comparison as if the two data items were keys in the
	 * specified database.
	 * @param[in] txn A transaction handle returned by #mdb_txn_begin()
	 * @param[in] dbi A database handle returned by #mdb_open()
	 * @param[in] a The first item to compare
	 * @param[in] b The second item to compare
	 * @return < 0 if a < b, 0 if a == b, > 0 if a > b
	 */
Howard Chu's avatar
Howard Chu committed
853
int  mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
Howard Chu's avatar
Howard Chu committed
854

855
/**	@} */
Howard Chu's avatar
Howard Chu committed
856
#endif /* _MDB_H_ */