mdb.h 5.68 KB
Newer Older
Howard Chu's avatar
Howard Chu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* mdb.h - memory-mapped database library header file */
/*
 * Copyright 2011 Howard Chu, Symas Corp.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 *
 * This code is derived from btree.c written by Martin Hedenfalk.
 *
 * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
Howard Chu's avatar
Howard Chu committed
30
31
32
33
34
#ifndef _MDB_H_
#define _MDB_H_

#include <sys/types.h>

35
36
37
#define MDB_VERSION_MAJOR	0
#define MDB_VERSION_MINOR	8
#define MDB_VERSION_PATCH	0
38
#define MDB_VERINT(a,b,c)	(((a) << 24) | ((b) << 16) | (c))
Howard Chu's avatar
Howard Chu committed
39
40
41
42
43
44
45
#define MDB_VERSION_FULL	\
	MDB_VERINT(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH)
#define MDB_VERSION_DATE	"August 11, 2011"
#define MDB_VERSTR(a,b,c,d)	"MDB " #a "." #b "." #c ": (" #d ")"
#define MDB_VERFOO(a,b,c,d)	MDB_VERSTR(a,b,c,d)
#define	MDB_VERSION_STRING	\
	MDB_VERFOO(MDB_VERSION_MAJOR,MDB_VERSION_MINOR,MDB_VERSION_PATCH,MDB_VERSION_DATE)
46

Howard Chu's avatar
Howard Chu committed
47
48
49
50
51
52
53
54
struct MDB_cursor;
struct MDB_txn;
struct MDB_env;

typedef struct MDB_cursor MDB_cursor;
typedef struct MDB_txn MDB_txn;
typedef struct MDB_env MDB_env;

Howard Chu's avatar
Howard Chu committed
55
56
typedef unsigned int	MDB_dbi;

Howard Chu's avatar
Howard Chu committed
57
58
typedef struct MDB_val {
	size_t		 mv_size;
59
	void		*mv_data;
Howard Chu's avatar
Howard Chu committed
60
61
62
63
64
} MDB_val;

typedef int  (MDB_cmp_func)(const MDB_val *a, const MDB_val *b);
typedef void (MDB_rel_func)(void *ptr, void *oldptr);

Howard Chu's avatar
Howard Chu committed
65
66
67
#define MDB_NOOVERWRITE	0x10
#define MDB_NODUPDATA	0x20
#define MDB_DEL_DUP		0x40
68

Howard Chu's avatar
Howard Chu committed
69
70
typedef enum MDB_cursor_op {		/* cursor operations */
	MDB_FIRST,
Howard Chu's avatar
Howard Chu committed
71
72
	MDB_GET_BOTH,			/* position at key/data */
	MDB_GET_BOTH_RANGE,		/* position at key, nearest data */
Howard Chu's avatar
Howard Chu committed
73
	MDB_LAST,
Howard Chu's avatar
Howard Chu committed
74
75
76
	MDB_NEXT,
	MDB_NEXT_DUP,
	MDB_NEXT_NODUP,
Howard Chu's avatar
Howard Chu committed
77
	MDB_PREV,
Howard Chu's avatar
Howard Chu committed
78
79
80
81
	MDB_PREV_DUP,
	MDB_PREV_NODUP,
	MDB_SET,				/* position at key, or fail */
	MDB_SET_RANGE			/* position at given key */
Howard Chu's avatar
Howard Chu committed
82
83
84
} MDB_cursor_op;

/* return codes */
Howard Chu's avatar
Howard Chu committed
85
/* BerkeleyDB uses -30800 to -30999, we'll go under them */
Howard Chu's avatar
Howard Chu committed
86
#define MDB_SUCCESS	 0
Howard Chu's avatar
Howard Chu committed
87
88
89
90
91
92
#define MDB_KEYEXIST	(-30799)		/* key/data pair already exists */
#define MDB_NOTFOUND	(-30798)		/* key/data pair not found (EOF) */
#define MDB_PAGE_NOTFOUND	(-30797)	/* Requested page not found */
#define MDB_CORRUPTED	(-30796)		/* Located page was wrong type */
#define MDB_PANIC		(-30795)		/* Update of meta page failed, probably I/O error */
#define MDB_VERSION_MISMATCH	(-30794)	/* Environment version mismatch */
Howard Chu's avatar
Howard Chu committed
93
94
95

/* DB flags */
#define MDB_REVERSEKEY	0x02		/* use reverse string keys */
Howard Chu's avatar
Howard Chu committed
96
#define MDB_DUPSORT		0x04		/* use sorted duplicates */
Howard Chu's avatar
Howard Chu committed
97
#define MDB_INTEGERKEY	0x08		/* numeric keys in native byte order */
Howard Chu's avatar
Howard Chu committed
98
99
100

/* environment flags */
#define MDB_FIXEDMAP	0x01		/* mmap at a fixed address */
Howard Chu's avatar
Howard Chu committed
101
102
103
104
105
#define MDB_NOSYNC		0x10000		/* don't fsync after commit */
#define MDB_RDONLY		0x20000		/* read only */

/* DB or env flags */
#define MDB_CREATE		0x40000		/* create if not present */
Howard Chu's avatar
Howard Chu committed
106
107
108

typedef struct MDB_stat {
	unsigned int	ms_psize;
Howard Chu's avatar
Howard Chu committed
109
	unsigned int	ms_depth;
Howard Chu's avatar
Howard Chu committed
110
111
112
113
114
115
	unsigned long	ms_branch_pages;
	unsigned long	ms_leaf_pages;
	unsigned long	ms_overflow_pages;
	unsigned long	ms_entries;
} MDB_stat;

Howard Chu's avatar
Howard Chu committed
116
char *mdb_version(int *major, int *minor, int *patch);
117
char *mdb_strerror(int err);
Howard Chu's avatar
Howard Chu committed
118
119
120
int  mdb_env_create(MDB_env **env);
int  mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode);
int  mdb_env_stat(MDB_env *env, MDB_stat *stat);
Howard Chu's avatar
Howard Chu committed
121
int  mdb_env_sync(MDB_env *env, int force);
Howard Chu's avatar
Howard Chu committed
122
void mdb_env_close(MDB_env *env);
123
int  mdb_env_set_flags(MDB_env *env, unsigned int flags, int onoff);
Howard Chu's avatar
Howard Chu committed
124
125
126
127
128
129
int  mdb_env_get_flags(MDB_env *env, unsigned int *flags);
int  mdb_env_get_path(MDB_env *env, const char **path);
int  mdb_env_set_mapsize(MDB_env *env, size_t size);
int  mdb_env_set_maxreaders(MDB_env *env, int readers);
int  mdb_env_get_maxreaders(MDB_env *env, int *readers);
int  mdb_env_set_maxdbs(MDB_env *env, int dbs);
Howard Chu's avatar
Howard Chu committed
130
131
132
133
134

int  mdb_txn_begin(MDB_env *env, int rdonly, MDB_txn **txn);
int  mdb_txn_commit(MDB_txn *txn);
void mdb_txn_abort(MDB_txn *txn);

Howard Chu's avatar
Howard Chu committed
135
136
int  mdb_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi);
int  mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *stat);
Howard Chu's avatar
Howard Chu committed
137
void mdb_close(MDB_txn *txn, MDB_dbi dbi);
Howard Chu's avatar
Howard Chu committed
138

Howard Chu's avatar
Howard Chu committed
139
140
141
142
int  mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
int  mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp);
int  mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel);

Howard Chu's avatar
Howard Chu committed
143
144
int  mdb_get(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data);
int  mdb_put(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
Howard Chu's avatar
Howard Chu committed
145
			    unsigned int flags);
146
147
int  mdb_del(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data,
			    unsigned int flags);
Howard Chu's avatar
Howard Chu committed
148

Howard Chu's avatar
Howard Chu committed
149
int  mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor);
Howard Chu's avatar
Howard Chu committed
150
151
152
void mdb_cursor_close(MDB_cursor *cursor);
int  mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data,
			    MDB_cursor_op op);
Howard Chu's avatar
Howard Chu committed
153
int  mdb_cursor_count(MDB_cursor *cursor, unsigned long *countp);
Howard Chu's avatar
Howard Chu committed
154

Howard Chu's avatar
Howard Chu committed
155
int  mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b);
Howard Chu's avatar
Howard Chu committed
156
157

#endif /* _MDB_H_ */