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>
39 typedef struct MDB_cursor MDB_cursor;
40 typedef struct MDB_txn MDB_txn;
41 typedef struct MDB_env MDB_env;
43 typedef unsigned int MDB_dbi;
45 typedef struct MDB_val {
50 typedef int (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
51 typedef void (MDB_rel_func)(void *ptr, void *oldptr);
53 #define MDB_NOOVERWRITE 0x10
54 #define MDB_NODUPDATA 0x20
55 #define MDB_DEL_DUP 0x40
57 typedef enum MDB_cursor_op { /* cursor operations */
59 MDB_GET_BOTH, /* position at key/data */
60 MDB_GET_BOTH_RANGE, /* position at key, nearest data */
68 MDB_SET, /* position at key, or fail */
69 MDB_SET_RANGE /* position at given key */
75 #define MDB_KEYEXIST -2
76 #define MDB_NOTFOUND -3
77 #define MDB_VERSION_MISMATCH -4
80 #define MDB_REVERSEKEY 0x02 /* use reverse string keys */
81 #define MDB_DUPSORT 0x04 /* use sorted duplicates */
83 /* environment flags */
84 #define MDB_FIXEDMAP 0x01 /* mmap at a fixed address */
85 #define MDB_NOSYNC 0x10000 /* don't fsync after commit */
86 #define MDB_RDONLY 0x20000 /* read only */
89 #define MDB_CREATE 0x40000 /* create if not present */
91 typedef struct MDB_stat {
92 unsigned int ms_psize;
93 unsigned int ms_depth;
94 unsigned long ms_branch_pages;
95 unsigned long ms_leaf_pages;
96 unsigned long ms_overflow_pages;
97 unsigned long ms_entries;
100 int mdbenv_create(MDB_env **env);
101 int mdbenv_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
102 int mdbenv_stat(MDB_env *env, MDB_stat *stat);
103 void mdbenv_close(MDB_env *env);
104 int mdbenv_get_flags(MDB_env *env, unsigned int *flags);
105 int mdbenv_get_path(MDB_env *env, const char **path);
106 int mdbenv_set_mapsize(MDB_env *env, size_t size);
107 int mdbenv_set_maxreaders(MDB_env *env, int readers);
108 int mdbenv_get_maxreaders(MDB_env *env, int *readers);
109 int mdbenv_set_maxdbs(MDB_env *env, int dbs);
110 int mdbenv_sync(MDB_env *env);
112 int mdb_txn_begin(MDB_env *env, int rdonly, MDB_txn **txn);
113 int mdb_txn_commit(MDB_txn *txn);
114 void mdb_txn_abort(MDB_txn *txn);
116 int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
117 int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
118 void mdb_close(MDB_txn *txn, MDB_dbi dbi);
120 int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
121 int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
122 int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
124 int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
125 int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
127 int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
130 int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
131 void mdb_cursor_close(MDB_cursor *cursor);
132 int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
134 int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
136 int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
138 int mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);