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 */
80 MDB_SET, /* position at key, or fail */
81 MDB_SET_RANGE /* position at given key */
85 /* BerkeleyDB uses -30800 to -30999, we'll go under them */
87 #define MDB_KEYEXIST (-30799) /* key/data pair already exists */
88 #define MDB_NOTFOUND (-30798) /* key/data pair not found (EOF) */
89 #define MDB_PAGE_NOTFOUND (-30797) /* Requested page not found */
90 #define MDB_CORRUPTED (-30796) /* Located page was wrong type */
91 #define MDB_PANIC (-30795) /* Update of meta page failed, probably I/O error */
92 #define MDB_VERSION_MISMATCH (-30794) /* Environment version mismatch */
95 #define MDB_REVERSEKEY 0x02 /* use reverse string keys */
96 #define MDB_DUPSORT 0x04 /* use sorted duplicates */
97 #define MDB_INTEGERKEY 0x08 /* numeric keys in native byte order */
99 /* environment flags */
100 #define MDB_FIXEDMAP 0x01 /* mmap at a fixed address */
101 #define MDB_NOSYNC 0x10000 /* don't fsync after commit */
102 #define MDB_RDONLY 0x20000 /* read only */
104 /* DB or env flags */
105 #define MDB_CREATE 0x40000 /* create if not present */
107 typedef struct MDB_stat {
108 unsigned int ms_psize;
109 unsigned int ms_depth;
110 unsigned long ms_branch_pages;
111 unsigned long ms_leaf_pages;
112 unsigned long ms_overflow_pages;
113 unsigned long ms_entries;
116 char *mdb_version(int *major, int *minor, int *patch);
117 char *mdb_strerror(int err);
118 int mdb_env_create(MDB_env **env);
119 int mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
120 int mdb_env_stat(MDB_env *env, MDB_stat *stat);
121 int mdb_env_sync(MDB_env *env, int force);
122 void mdb_env_close(MDB_env *env);
123 int mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
124 int mdb_env_get_flags(MDB_env *env, unsigned int *flags);
125 int mdb_env_get_path(MDB_env *env, const char **path);
126 int mdb_env_set_mapsize(MDB_env *env, size_t size);
127 int mdb_env_set_maxreaders(MDB_env *env, int readers);
128 int mdb_env_get_maxreaders(MDB_env *env, int *readers);
129 int mdb_env_set_maxdbs(MDB_env *env, int dbs);
131 int mdb_txn_begin(MDB_env *env, int rdonly, MDB_txn **txn);
132 int mdb_txn_commit(MDB_txn *txn);
133 void mdb_txn_abort(MDB_txn *txn);
135 int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
136 int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
137 void mdb_close(MDB_txn *txn, MDB_dbi dbi);
139 int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
140 int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
141 int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
143 int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
144 int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
146 int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
149 int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
150 void mdb_cursor_close(MDB_cursor *cursor);
151 int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
153 int mdb_cursor_count(MDB_cursor *cursor, unsigned long *countp);
155 int mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);