1 /* mdb.h - memory-mapped database library header file */
3 * Copyright 2011 Howard Chu, Symas Corp.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted only as authorized by the OpenLDAP
10 * A copy of this license is available in the file LICENSE in the
11 * top-level directory of the distribution or, alternatively, at
12 * <http://www.OpenLDAP.org/license.html>.
14 * This code is derived from btree.c written by Martin Hedenfalk.
16 * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
18 * Permission to use, copy, modify, and distribute this software for any
19 * purpose with or without fee is hereby granted, provided that the above
20 * copyright notice and this permission notice appear in all copies.
22 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
23 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
25 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
26 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
27 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
28 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33 #include <sys/types.h>
35 #define MDB_VERSION_MAJOR 0
36 #define MDB_VERSION_MINOR 8
37 #define MDB_VERSION_PATCH 0
38 #define MDB_VERINT(a,b,c) (((a) << 24) | ((b) << 16) | (c))
39 #define MDB_VERSION_FULL \
40 MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH)
41 #define MDB_VERSION_DATE "August 11, 2011"
42 #define MDB_VERSTR(a,b,c,d) "MDB " #a "." #b "." #c ": (" #d ")"
43 #define MDB_VERFOO(a,b,c,d) MDB_VERSTR(a,b,c,d)
44 #define MDB_VERSION_STRING \
45 MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE)
51 typedef struct MDB_cursor MDB_cursor;
52 typedef struct MDB_txn MDB_txn;
53 typedef struct MDB_env MDB_env;
55 typedef unsigned int MDB_dbi;
57 typedef struct MDB_val {
62 typedef int (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
63 typedef void (MDB_rel_func)(void *ptr, void *oldptr);
65 #define MDB_NOOVERWRITE 0x10
66 #define MDB_NODUPDATA 0x20
67 #define MDB_DEL_DUP 0x40
69 typedef enum MDB_cursor_op { /* cursor operations */
71 MDB_GET_BOTH, /* position at key/data */
72 MDB_GET_BOTH_RANGE, /* position at key, nearest data */
73 MDB_GET_MULTIPLE, /* only for MDB_DUPFIXED */
77 MDB_NEXT_MULTIPLE, /* only for MDB_DUPFIXED */
82 MDB_SET, /* position at key, or fail */
83 MDB_SET_RANGE /* position at given key */
87 /* BerkeleyDB uses -30800 to -30999, we'll go under them */
89 #define MDB_KEYEXIST (-30799) /* key/data pair already exists */
90 #define MDB_NOTFOUND (-30798) /* key/data pair not found (EOF) */
91 #define MDB_PAGE_NOTFOUND (-30797) /* Requested page not found */
92 #define MDB_CORRUPTED (-30796) /* Located page was wrong type */
93 #define MDB_PANIC (-30795) /* Update of meta page failed, probably I/O error */
94 #define MDB_VERSION_MISMATCH (-30794) /* Environment version mismatch */
97 #define MDB_REVERSEKEY 0x02 /* use reverse string keys */
98 #define MDB_DUPSORT 0x04 /* use sorted duplicates */
99 #define MDB_INTEGERKEY 0x08 /* numeric keys in native byte order */
100 #define MDB_DUPFIXED 0x10 /* sorted dup items have fixed size */
101 #define MDB_INTEGERDUP 0x20 /* numeric dups in native byte order */
103 /* environment flags */
104 #define MDB_FIXEDMAP 0x01 /* mmap at a fixed address */
105 #define MDB_NOSYNC 0x10000 /* don't fsync after commit */
106 #define MDB_RDONLY 0x20000 /* read only */
108 /* DB or env flags */
109 #define MDB_CREATE 0x40000 /* create if not present */
111 typedef struct MDB_stat {
112 unsigned int ms_psize;
113 unsigned int ms_depth;
114 unsigned long ms_branch_pages;
115 unsigned long ms_leaf_pages;
116 unsigned long ms_overflow_pages;
117 unsigned long ms_entries;
120 char *mdb_version(int *major, int *minor, int *patch);
121 char *mdb_strerror(int err);
122 int mdb_env_create(MDB_env **env);
123 int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
124 int mdb_env_stat(MDB_env *env, MDB_stat *stat);
125 int mdb_env_sync(MDB_env *env, int force);
126 void mdb_env_close(MDB_env *env);
127 int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
128 int mdb_env_get_flags(MDB_env *env, unsigned int *flags);
129 int mdb_env_get_path(MDB_env *env, const char **path);
130 int mdb_env_set_mapsize(MDB_env *env, size_t size);
131 int mdb_env_set_maxreaders(MDB_env *env, int readers);
132 int mdb_env_get_maxreaders(MDB_env *env, int *readers);
133 int mdb_env_set_maxdbs(MDB_env *env, int dbs);
135 int mdb_txn_begin(MDB_env *env, int rdonly, MDB_txn **txn);
136 int mdb_txn_commit(MDB_txn *txn);
137 void mdb_txn_abort(MDB_txn *txn);
139 int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
140 int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
141 void mdb_close(MDB_txn *txn, MDB_dbi dbi);
143 int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
144 int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
145 int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
147 int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
148 int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
150 int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
153 int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
154 void mdb_cursor_close(MDB_cursor *cursor);
155 int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
157 int mdb_cursor_count(MDB_cursor *cursor, unsigned long *countp);
159 int mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);