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 typedef enum MDB_cursor_op { /* cursor operations */
56 MDB_GET_BOTH, /* position at key/data */
57 MDB_GET_BOTH_RANGE, /* position at key, nearest data */
67 MDB_SET, /* position at key, or fail */
68 MDB_SET_RANGE /* position at given key */
74 #define MDB_KEYEXIST -2
75 #define MDB_NOTFOUND -3
76 #define MDB_VERSION_MISMATCH -4
79 #define MDB_REVERSEKEY 0x02 /* use reverse string keys */
80 #define MDB_DUPSORT 0x04 /* use sorted duplicates */
82 /* environment flags */
83 #define MDB_FIXEDMAP 0x01 /* mmap at a fixed address */
84 #define MDB_NOSYNC 0x10000 /* don't fsync after commit */
85 #define MDB_RDONLY 0x20000 /* read only */
88 #define MDB_CREATE 0x40000 /* create if not present */
90 typedef struct MDB_stat {
91 unsigned int ms_psize;
92 unsigned int ms_depth;
93 unsigned long ms_branch_pages;
94 unsigned long ms_leaf_pages;
95 unsigned long ms_overflow_pages;
96 unsigned long ms_entries;
99 int mdbenv_create(MDB_env **env);
100 int mdbenv_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
101 int mdbenv_stat(MDB_env *env, MDB_stat *stat);
102 void mdbenv_close(MDB_env *env);
103 int mdbenv_get_flags(MDB_env *env, unsigned int *flags);
104 int mdbenv_get_path(MDB_env *env, const char **path);
105 int mdbenv_set_mapsize(MDB_env *env, size_t size);
106 int mdbenv_set_maxreaders(MDB_env *env, int readers);
107 int mdbenv_get_maxreaders(MDB_env *env, int *readers);
108 int mdbenv_set_maxdbs(MDB_env *env, int dbs);
109 int mdbenv_sync(MDB_env *env);
111 int mdb_txn_begin(MDB_env *env, int rdonly, MDB_txn **txn);
112 int mdb_txn_commit(MDB_txn *txn);
113 void mdb_txn_abort(MDB_txn *txn);
115 int mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
116 int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
117 void mdb_close(MDB_txn *txn, MDB_dbi dbi);
119 int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
120 int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
121 int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
123 int mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
124 int mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
126 int mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
128 int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
129 void mdb_cursor_close(MDB_cursor *cursor);
130 int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
132 int mdb_cursor_put(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
134 int mdb_cursor_del(MDB_cursor *cursor, unsigned int flags);
136 int mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);