xref: /openbsd/usr.sbin/ldapd/btree.h (revision 5cea9f8f)
1 /*	$OpenBSD: btree.h,v 1.6 2010/07/02 01:43:00 martinh Exp $ */
2 
3 /*
4  * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef _btree_h_
20 #define _btree_h_
21 
22 #include <openssl/sha.h>
23 
24 struct mpage;
25 struct cursor;
26 struct btree_txn;
27 
28 struct btval {
29 	void		*data;
30 	size_t		 size;
31 	int		 free_data;		/* true if data malloc'd */
32 	struct mpage	*mp;			/* ref'd memory page */
33 };
34 
35 typedef int		 (*bt_cmp_func)(const struct btval *a,
36 					const struct btval *b);
37 typedef void		 (*bt_prefix_func)(const struct btval *a,
38 					   const struct btval *b,
39 					   struct btval *sep);
40 
41 #define BT_NOOVERWRITE	 1
42 
43 enum cursor_op {				/* cursor operations */
44 	BT_CURSOR,				/* position at given key */
45 	BT_CURSOR_EXACT,			/* position at key, or fail */
46 	BT_FIRST,
47 	BT_NEXT,
48 	BT_LAST,				/* not implemented */
49 	BT_PREV					/* not implemented */
50 };
51 
52 /* return codes */
53 #define BT_FAIL		-1
54 #define BT_SUCCESS	 0
55 
56 /* btree flags */
57 #define BT_NOSYNC		 0x02		/* don't fsync after commit */
58 #define BT_RDONLY		 0x04		/* read only */
59 #define BT_REVERSEKEY		 0x08		/* use reverse string keys */
60 
61 struct btree_stat {
62 	unsigned long long int	 hits;		/* cache hits */
63 	unsigned long long int	 reads;		/* page reads */
64 	unsigned int		 max_cache;	/* max cached pages */
65 	unsigned int		 cache_size;	/* current cache size */
66 	unsigned int		 branch_pages;
67 	unsigned int		 leaf_pages;
68 	unsigned int		 overflow_pages;
69 	unsigned int		 revisions;
70 	unsigned int		 depth;
71 	unsigned long long int	 entries;
72 	unsigned int		 psize;
73 	time_t			 created_at;
74 };
75 
76 struct btree		*btree_open_fd(int fd, unsigned int flags);
77 struct btree		*btree_open(const char *path, unsigned int flags,
78 			    mode_t mode);
79 void			 btree_close(struct btree *bt);
80 const struct btree_stat	*btree_stat(struct btree *bt);
81 
82 struct btree_txn	*btree_txn_begin(struct btree *bt, int rdonly);
83 int			 btree_txn_commit(struct btree_txn *txn);
84 void			 btree_txn_abort(struct btree_txn *txn);
85 
86 int			 btree_txn_get(struct btree *bt, struct btree_txn *txn,
87 			    struct btval *key, struct btval *data);
88 int			 btree_txn_put(struct btree *bt, struct btree_txn *txn,
89 			    struct btval *key, struct btval *data,
90 			    unsigned int flags);
91 int			 btree_txn_del(struct btree *bt, struct btree_txn *txn,
92 			    struct btval *key, struct btval *data);
93 
94 #define btree_get(bt, key, data)	 \
95 			 btree_txn_get(bt, NULL, key, data)
96 #define btree_put(bt, key, data, flags)	 \
97 			 btree_txn_put(bt, NULL, key, data, flags)
98 #define btree_del(bt, key, data)	 \
99 			 btree_txn_del(bt, NULL, key, data)
100 
101 void			 btree_set_cache_size(struct btree *bt,
102 			    unsigned int cache_size);
103 unsigned int		 btree_get_flags(struct btree *bt);
104 const char		*btree_get_path(struct btree *bt);
105 
106 #define btree_cursor_open(bt)	 \
107 			 btree_txn_cursor_open(bt, NULL)
108 struct cursor		*btree_txn_cursor_open(struct btree *bt,
109 			    struct btree_txn *txn);
110 void			 btree_cursor_close(struct cursor *cursor);
111 int			 btree_cursor_get(struct cursor *cursor,
112 			    struct btval *key, struct btval *data,
113 			    enum cursor_op op);
114 
115 int			 btree_sync(struct btree *bt);
116 int			 btree_compact(struct btree *bt);
117 int			 btree_revert(struct btree *bt);
118 
119 int			 btree_cmp(struct btree *bt, const struct btval *a,
120 			     const struct btval *b);
121 void			 btval_reset(struct btval *btv);
122 
123 #endif
124 
125