]> git.sur5r.net Git - openldap/blob - libraries/libmdb/mdb.h
30d4e927ea0760cc65858650596fefc25b758e6d
[openldap] / libraries / libmdb / mdb.h
1 /* mdb.h - memory-mapped database library header file */
2 /*
3  * Copyright 2011 Howard Chu, Symas Corp.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted only as authorized by the OpenLDAP
8  * Public License.
9  *
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>.
13  *
14  * This code is derived from btree.c written by Martin Hedenfalk.
15  *
16  * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
17  *
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.
21  *
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.
29  */
30 #ifndef _MDB_H_
31 #define _MDB_H_
32
33 #include <sys/types.h>
34
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)
46
47 struct MDB_cursor;
48 struct MDB_txn;
49 struct MDB_env;
50
51 typedef struct MDB_cursor MDB_cursor;
52 typedef struct MDB_txn MDB_txn;
53 typedef struct MDB_env MDB_env;
54
55 typedef unsigned int    MDB_dbi;
56
57 typedef struct MDB_val {
58         size_t           mv_size;
59         void            *mv_data;
60 } MDB_val;
61
62 typedef int  (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
63 typedef void (MDB_rel_func)(void *ptr, void *oldptr);
64
65 #define MDB_NOOVERWRITE 0x10
66 #define MDB_NODUPDATA   0x20
67 #define MDB_DEL_DUP             0x40
68
69 typedef enum MDB_cursor_op {            /* cursor operations */
70         MDB_FIRST,
71         MDB_GET_BOTH,                   /* position at key/data */
72         MDB_GET_BOTH_RANGE,             /* position at key, nearest data */
73         MDB_LAST,
74         MDB_NEXT,
75         MDB_NEXT_DUP,
76         MDB_NEXT_NODUP,
77         MDB_PREV,
78         MDB_PREV_DUP,
79         MDB_PREV_NODUP,
80         MDB_SET,                                /* position at key, or fail */
81         MDB_SET_RANGE                   /* position at given key */
82 } MDB_cursor_op;
83
84 /* return codes */
85 /* BerkeleyDB uses -30800 to -30999, we'll go under them */
86 #define MDB_SUCCESS      0
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 */
93
94 /* DB flags */
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 */
98
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 */
103
104 /* DB or env flags */
105 #define MDB_CREATE              0x40000         /* create if not present */
106
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;
114 } MDB_stat;
115
116 char *mdb_version(int *major, int *minor, int *patch);
117 int  mdb_env_create(MDB_env **env);
118 int  mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
119 int  mdb_env_stat(MDB_env *env, MDB_stat *stat);
120 int  mdb_env_sync(MDB_env *env, int force);
121 void mdb_env_close(MDB_env *env);
122 int  mdb_env_get_flags(MDB_env *env, unsigned int *flags);
123 int  mdb_env_get_path(MDB_env *env, const char **path);
124 int  mdb_env_set_mapsize(MDB_env *env, size_t size);
125 int  mdb_env_set_maxreaders(MDB_env *env, int readers);
126 int  mdb_env_get_maxreaders(MDB_env *env, int *readers);
127 int  mdb_env_set_maxdbs(MDB_env *env, int dbs);
128
129 int  mdb_txn_begin(MDB_env *env, int rdonly, MDB_txn **txn);
130 int  mdb_txn_commit(MDB_txn *txn);
131 void mdb_txn_abort(MDB_txn *txn);
132
133 int  mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
134 int  mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
135 void mdb_close(MDB_txn *txn, MDB_dbi dbi);
136
137 int  mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
138 int  mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
139 int  mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);
140
141 int  mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
142 int  mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
143                             unsigned int flags);
144 int  mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
145                             unsigned int flags);
146
147 int  mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
148 void mdb_cursor_close(MDB_cursor *cursor);
149 int  mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
150                             MDB_cursor_op op);
151 int  mdb_cursor_count(MDB_cursor *cursor, unsigned long *countp);
152
153 int  mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
154
155 #endif /* _MDB_H_ */