xref: /openbsd-src/usr.sbin/ldapd/btree.h (revision 5cea9f8f9c8627248bdeb9514fc48852bf848089)
1*5cea9f8fSmartinh /*	$OpenBSD: btree.h,v 1.6 2010/07/02 01:43:00 martinh Exp $ */
25d465952Smartinh 
35d465952Smartinh /*
45d465952Smartinh  * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
55d465952Smartinh  *
65d465952Smartinh  * Permission to use, copy, modify, and distribute this software for any
75d465952Smartinh  * purpose with or without fee is hereby granted, provided that the above
85d465952Smartinh  * copyright notice and this permission notice appear in all copies.
95d465952Smartinh  *
105d465952Smartinh  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
115d465952Smartinh  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
125d465952Smartinh  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
135d465952Smartinh  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
145d465952Smartinh  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
155d465952Smartinh  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
165d465952Smartinh  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
175d465952Smartinh  */
185d465952Smartinh 
195d465952Smartinh #ifndef _btree_h_
205d465952Smartinh #define _btree_h_
215d465952Smartinh 
225d465952Smartinh #include <openssl/sha.h>
235d465952Smartinh 
245d465952Smartinh struct mpage;
255d465952Smartinh struct cursor;
265d465952Smartinh struct btree_txn;
275d465952Smartinh 
285d465952Smartinh struct btval {
295d465952Smartinh 	void		*data;
305d465952Smartinh 	size_t		 size;
315d465952Smartinh 	int		 free_data;		/* true if data malloc'd */
325d465952Smartinh 	struct mpage	*mp;			/* ref'd memory page */
335d465952Smartinh };
345d465952Smartinh 
355d465952Smartinh typedef int		 (*bt_cmp_func)(const struct btval *a,
365d465952Smartinh 					const struct btval *b);
375d465952Smartinh typedef void		 (*bt_prefix_func)(const struct btval *a,
385d465952Smartinh 					   const struct btval *b,
395d465952Smartinh 					   struct btval *sep);
405d465952Smartinh 
415d465952Smartinh #define BT_NOOVERWRITE	 1
425d465952Smartinh 
435d465952Smartinh enum cursor_op {				/* cursor operations */
445d465952Smartinh 	BT_CURSOR,				/* position at given key */
45*5cea9f8fSmartinh 	BT_CURSOR_EXACT,			/* position at key, or fail */
465d465952Smartinh 	BT_FIRST,
475d465952Smartinh 	BT_NEXT,
485d465952Smartinh 	BT_LAST,				/* not implemented */
495d465952Smartinh 	BT_PREV					/* not implemented */
505d465952Smartinh };
515d465952Smartinh 
525d465952Smartinh /* return codes */
535d465952Smartinh #define BT_FAIL		-1
545d465952Smartinh #define BT_SUCCESS	 0
555d465952Smartinh 
565d465952Smartinh /* btree flags */
57ad613b4dSmartinh #define BT_NOSYNC		 0x02		/* don't fsync after commit */
58ad613b4dSmartinh #define BT_RDONLY		 0x04		/* read only */
59ad613b4dSmartinh #define BT_REVERSEKEY		 0x08		/* use reverse string keys */
605d465952Smartinh 
615d465952Smartinh struct btree_stat {
625d465952Smartinh 	unsigned long long int	 hits;		/* cache hits */
635d465952Smartinh 	unsigned long long int	 reads;		/* page reads */
645d465952Smartinh 	unsigned int		 max_cache;	/* max cached pages */
655d465952Smartinh 	unsigned int		 cache_size;	/* current cache size */
665d465952Smartinh 	unsigned int		 branch_pages;
675d465952Smartinh 	unsigned int		 leaf_pages;
685d465952Smartinh 	unsigned int		 overflow_pages;
695d465952Smartinh 	unsigned int		 revisions;
705d465952Smartinh 	unsigned int		 depth;
715d465952Smartinh 	unsigned long long int	 entries;
725d465952Smartinh 	unsigned int		 psize;
735d465952Smartinh 	time_t			 created_at;
745d465952Smartinh };
755d465952Smartinh 
76176c8cbcSmartinh struct btree		*btree_open_fd(int fd, unsigned int flags);
77176c8cbcSmartinh struct btree		*btree_open(const char *path, unsigned int flags,
785d465952Smartinh 			    mode_t mode);
795d465952Smartinh void			 btree_close(struct btree *bt);
805d465952Smartinh const struct btree_stat	*btree_stat(struct btree *bt);
815d465952Smartinh 
825d465952Smartinh struct btree_txn	*btree_txn_begin(struct btree *bt, int rdonly);
835d465952Smartinh int			 btree_txn_commit(struct btree_txn *txn);
845d465952Smartinh void			 btree_txn_abort(struct btree_txn *txn);
855d465952Smartinh 
865d465952Smartinh int			 btree_txn_get(struct btree *bt, struct btree_txn *txn,
875d465952Smartinh 			    struct btval *key, struct btval *data);
885d465952Smartinh int			 btree_txn_put(struct btree *bt, struct btree_txn *txn,
895d465952Smartinh 			    struct btval *key, struct btval *data,
905d465952Smartinh 			    unsigned int flags);
915d465952Smartinh int			 btree_txn_del(struct btree *bt, struct btree_txn *txn,
925d465952Smartinh 			    struct btval *key, struct btval *data);
935d465952Smartinh 
945d465952Smartinh #define btree_get(bt, key, data)	 \
955d465952Smartinh 			 btree_txn_get(bt, NULL, key, data)
965d465952Smartinh #define btree_put(bt, key, data, flags)	 \
975d465952Smartinh 			 btree_txn_put(bt, NULL, key, data, flags)
985d465952Smartinh #define btree_del(bt, key, data)	 \
995d465952Smartinh 			 btree_txn_del(bt, NULL, key, data)
1005d465952Smartinh 
1015d465952Smartinh void			 btree_set_cache_size(struct btree *bt,
1025d465952Smartinh 			    unsigned int cache_size);
1035d465952Smartinh unsigned int		 btree_get_flags(struct btree *bt);
1045d465952Smartinh const char		*btree_get_path(struct btree *bt);
1055d465952Smartinh 
1065d465952Smartinh #define btree_cursor_open(bt)	 \
1075d465952Smartinh 			 btree_txn_cursor_open(bt, NULL)
1085d465952Smartinh struct cursor		*btree_txn_cursor_open(struct btree *bt,
1095d465952Smartinh 			    struct btree_txn *txn);
1105d465952Smartinh void			 btree_cursor_close(struct cursor *cursor);
1115d465952Smartinh int			 btree_cursor_get(struct cursor *cursor,
1125d465952Smartinh 			    struct btval *key, struct btval *data,
1135d465952Smartinh 			    enum cursor_op op);
1145d465952Smartinh 
1155d465952Smartinh int			 btree_sync(struct btree *bt);
1165d465952Smartinh int			 btree_compact(struct btree *bt);
1175d465952Smartinh int			 btree_revert(struct btree *bt);
1185d465952Smartinh 
1195d465952Smartinh int			 btree_cmp(struct btree *bt, const struct btval *a,
1205d465952Smartinh 			     const struct btval *b);
1215d465952Smartinh void			 btval_reset(struct btval *btv);
1225d465952Smartinh 
1235d465952Smartinh #endif
1245d465952Smartinh 
125