[BACK]Return to btree.h CVS log [TXT][DIR] Up to [local] / src / usr.sbin / ldapd

File: [local] / src / usr.sbin / ldapd / btree.h (download)

Revision 1.6, Fri Jul 2 01:43:00 2010 UTC (13 years, 11 months ago) by martinh
Branch: MAIN
CVS Tags: OPENBSD_7_5_BASE, OPENBSD_7_5, OPENBSD_7_4_BASE, OPENBSD_7_4, OPENBSD_7_3_BASE, OPENBSD_7_3, OPENBSD_7_2_BASE, OPENBSD_7_2, OPENBSD_7_1_BASE, OPENBSD_7_1, OPENBSD_7_0_BASE, OPENBSD_7_0, OPENBSD_6_9_BASE, OPENBSD_6_9, OPENBSD_6_8_BASE, OPENBSD_6_8, OPENBSD_6_7_BASE, OPENBSD_6_7, OPENBSD_6_6_BASE, OPENBSD_6_6, OPENBSD_6_5_BASE, OPENBSD_6_5, OPENBSD_6_4_BASE, OPENBSD_6_4, OPENBSD_6_3_BASE, OPENBSD_6_3, OPENBSD_6_2_BASE, OPENBSD_6_2, OPENBSD_6_1_BASE, OPENBSD_6_1, OPENBSD_6_0_BASE, OPENBSD_6_0, OPENBSD_5_9_BASE, OPENBSD_5_9, OPENBSD_5_8_BASE, OPENBSD_5_8, OPENBSD_5_7_BASE, OPENBSD_5_7, OPENBSD_5_6_BASE, OPENBSD_5_6, OPENBSD_5_5_BASE, OPENBSD_5_5, OPENBSD_5_4_BASE, OPENBSD_5_4, OPENBSD_5_3_BASE, OPENBSD_5_3, OPENBSD_5_2_BASE, OPENBSD_5_2, OPENBSD_5_1_BASE, OPENBSD_5_1, OPENBSD_5_0_BASE, OPENBSD_5_0, OPENBSD_4_9_BASE, OPENBSD_4_9, OPENBSD_4_8_BASE, OPENBSD_4_8, HEAD
Changes since 1.5: +2 -1 lines

Add a BT_CURSOR_EXACT operation to btree_cursor_get. It behaves like
BT_CURSOR, but fails if the key is not found.

/*	$OpenBSD: btree.h,v 1.6 2010/07/02 01:43:00 martinh Exp $ */

/*
 * 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.
 */

#ifndef _btree_h_
#define _btree_h_

#include <openssl/sha.h>

struct mpage;
struct cursor;
struct btree_txn;

struct btval {
	void		*data;
	size_t		 size;
	int		 free_data;		/* true if data malloc'd */
	struct mpage	*mp;			/* ref'd memory page */
};

typedef int		 (*bt_cmp_func)(const struct btval *a,
					const struct btval *b);
typedef void		 (*bt_prefix_func)(const struct btval *a,
					   const struct btval *b,
					   struct btval *sep);

#define BT_NOOVERWRITE	 1

enum cursor_op {				/* cursor operations */
	BT_CURSOR,				/* position at given key */
	BT_CURSOR_EXACT,			/* position at key, or fail */
	BT_FIRST,
	BT_NEXT,
	BT_LAST,				/* not implemented */
	BT_PREV					/* not implemented */
};

/* return codes */
#define BT_FAIL		-1
#define BT_SUCCESS	 0

/* btree flags */
#define BT_NOSYNC		 0x02		/* don't fsync after commit */
#define BT_RDONLY		 0x04		/* read only */
#define BT_REVERSEKEY		 0x08		/* use reverse string keys */

struct btree_stat {
	unsigned long long int	 hits;		/* cache hits */
	unsigned long long int	 reads;		/* page reads */
	unsigned int		 max_cache;	/* max cached pages */
	unsigned int		 cache_size;	/* current cache size */
	unsigned int		 branch_pages;
	unsigned int		 leaf_pages;
	unsigned int		 overflow_pages;
	unsigned int		 revisions;
	unsigned int		 depth;
	unsigned long long int	 entries;
	unsigned int		 psize;
	time_t			 created_at;
};

struct btree		*btree_open_fd(int fd, unsigned int flags);
struct btree		*btree_open(const char *path, unsigned int flags,
			    mode_t mode);
void			 btree_close(struct btree *bt);
const struct btree_stat	*btree_stat(struct btree *bt);

struct btree_txn	*btree_txn_begin(struct btree *bt, int rdonly);
int			 btree_txn_commit(struct btree_txn *txn);
void			 btree_txn_abort(struct btree_txn *txn);

int			 btree_txn_get(struct btree *bt, struct btree_txn *txn,
			    struct btval *key, struct btval *data);
int			 btree_txn_put(struct btree *bt, struct btree_txn *txn,
			    struct btval *key, struct btval *data,
			    unsigned int flags);
int			 btree_txn_del(struct btree *bt, struct btree_txn *txn,
			    struct btval *key, struct btval *data);

#define btree_get(bt, key, data)	 \
			 btree_txn_get(bt, NULL, key, data)
#define btree_put(bt, key, data, flags)	 \
			 btree_txn_put(bt, NULL, key, data, flags)
#define btree_del(bt, key, data)	 \
			 btree_txn_del(bt, NULL, key, data)

void			 btree_set_cache_size(struct btree *bt,
			    unsigned int cache_size);
unsigned int		 btree_get_flags(struct btree *bt);
const char		*btree_get_path(struct btree *bt);

#define btree_cursor_open(bt)	 \
			 btree_txn_cursor_open(bt, NULL)
struct cursor		*btree_txn_cursor_open(struct btree *bt,
			    struct btree_txn *txn);
void			 btree_cursor_close(struct cursor *cursor);
int			 btree_cursor_get(struct cursor *cursor,
			    struct btval *key, struct btval *data,
			    enum cursor_op op);

int			 btree_sync(struct btree *bt);
int			 btree_compact(struct btree *bt);
int			 btree_revert(struct btree *bt);

int			 btree_cmp(struct btree *bt, const struct btval *a,
			     const struct btval *b);
void			 btval_reset(struct btval *btv);

#endif