1 /*	$NetBSD: slmdb.h,v 1.2 2017/02/14 01:16:49 christos Exp $	*/
2 
3 #ifndef _SLMDB_H_INCLUDED_
4 #define _SLMDB_H_INCLUDED_
5 
6 /*++
7 /* NAME
8 /*	slmdb 3h
9 /* SUMMARY
10 /*	LMDB API wrapper
11 /* SYNOPSIS
12 /*	#include <slmdb.h>
13 /* DESCRIPTION
14 /* .nf
15 
16  /*
17   * System library.
18   */
19 #include <setjmp.h>
20 
21 #ifdef PATH_LMDB_H
22 #include PATH_LMDB_H
23 #else
24 #include <lmdb.h>
25 #endif
26 
27  /*
28   * Utility library.
29   */
30 #include <check_arg.h>
31 
32  /*
33   * External interface.
34   */
35 #ifdef NO_SIGSETJMP
36 #define SLMDB_JMP_BUF jmp_buf
37 #else
38 #define SLMDB_JMP_BUF sigjmp_buf
39 #endif
40 
41  /*
42   * All data structure members are private.
43   */
44 typedef struct {
45     size_t  curr_limit;			/* database soft size limit */
46     int     size_incr;			/* database expansion factor */
47     size_t  hard_limit;			/* database hard size limit */
48     int     open_flags;			/* open() flags */
49     int     lmdb_flags;			/* LMDB-specific flags */
50     int     slmdb_flags;		/* bulk-mode flag */
51     MDB_env *env;			/* database environment */
52     MDB_dbi dbi;			/* database instance */
53     MDB_txn *txn;			/* bulk transaction */
54     int     db_fd;			/* database file handle */
55     MDB_cursor *cursor;			/* iterator */
56     MDB_val saved_key;			/* saved cursor key buffer */
57     size_t  saved_key_size;		/* saved cursor key buffer size */
58     void    (*longjmp_fn) (void *, int);/* exception handling */
59     void    (*notify_fn) (void *, int,...);	/* workaround notification */
60     void    (*assert_fn) (void *, const char *);	/* assert notification */
61     void   *cb_context;			/* call-back context */
62     int     api_retry_count;		/* slmdb(3) API call retry count */
63     int     bulk_retry_count;		/* bulk_mode retry count */
64     int     api_retry_limit;		/* slmdb(3) API call retry limit */
65     int     bulk_retry_limit;		/* bulk_mode retry limit */
66 } SLMDB;
67 
68 #define SLMDB_FLAG_BULK		(1 << 0)
69 
70 extern int slmdb_init(SLMDB *, size_t, int, size_t);
71 extern int slmdb_open(SLMDB *, const char *, int, int, int);
72 extern int slmdb_get(SLMDB *, MDB_val *, MDB_val *);
73 extern int slmdb_put(SLMDB *, MDB_val *, MDB_val *, int);
74 extern int slmdb_del(SLMDB *, MDB_val *);
75 extern int slmdb_cursor_get(SLMDB *, MDB_val *, MDB_val *, MDB_cursor_op);
76 extern int slmdb_control(SLMDB *, int,...);
77 extern int slmdb_close(SLMDB *);
78 
79 #define slmdb_fd(slmdb)			((slmdb)->db_fd)
80 #define slmdb_curr_limit(slmdb)		((slmdb)->curr_limit)
81 
82 /* Legacy API: type-unchecked arguments, internal use. */
83 #define SLMDB_CTL_END			0
84 #define SLMDB_CTL_LONGJMP_FN		1	/* exception handling */
85 #define SLMDB_CTL_NOTIFY_FN		2	/* debug logging function */
86 #define SLMDB_CTL_CB_CONTEXT		3	/* call-back context */
87 #define SLMDB_CTL_API_RETRY_LIMIT	5	/* per slmdb(3) API call */
88 #define SLMDB_CTL_BULK_RETRY_LIMIT	6	/* per bulk update */
89 #define SLMDB_CTL_ASSERT_FN		7	/* report assertion failure */
90 
91 /* Safer API: type-checked arguments, external use. */
92 #define CA_SLMDB_CTL_END		SLMDB_CTL_END
93 #define CA_SLMDB_CTL_LONGJMP_FN(v)	SLMDB_CTL_LONGJMP_FN, CHECK_VAL(SLMDB_CTL, SLMDB_LONGJMP_FN, (v))
94 #define CA_SLMDB_CTL_NOTIFY_FN(v)	SLMDB_CTL_NOTIFY_FN, CHECK_VAL(SLMDB_CTL, SLMDB_NOTIFY_FN, (v))
95 #define CA_SLMDB_CTL_CB_CONTEXT(v)	SLMDB_CTL_CB_CONTEXT, CHECK_PTR(SLMDB_CTL, void, (v))
96 #define CA_SLMDB_CTL_API_RETRY_LIMIT(v)	SLMDB_CTL_API_RETRY_LIMIT, CHECK_VAL(SLMDB_CTL, int, (v))
97 #define CA_SLMDB_CTL_BULK_RETRY_LIMIT(v) SLMDB_CTL_BULK_RETRY_LIMIT, CHECK_VAL(SLMDB_CTL, int, (v))
98 #define CA_SLMDB_CTL_ASSERT_FN(v)	SLMDB_CTL_ASSERT_FN, CHECK_VAL(SLMDB_CTL, SLMDB_ASSERT_FN, (v))
99 
100 typedef void (*SLMDB_NOTIFY_FN) (void *, int,...);
101 typedef void (*SLMDB_LONGJMP_FN) (void *, int);
102 typedef void (*SLMDB_ASSERT_FN) (void *, const char *);
103 
104 CHECK_VAL_HELPER_DCL(SLMDB_CTL, int);
105 CHECK_VAL_HELPER_DCL(SLMDB_CTL, SLMDB_NOTIFY_FN);
106 CHECK_VAL_HELPER_DCL(SLMDB_CTL, SLMDB_LONGJMP_FN);
107 CHECK_VAL_HELPER_DCL(SLMDB_CTL, SLMDB_ASSERT_FN);
108 CHECK_PTR_HELPER_DCL(SLMDB_CTL, void);
109 
110 /* LICENSE
111 /* .ad
112 /* .fi
113 /*	The Secure Mailer license must be distributed with this software.
114 /* AUTHOR(S)
115 /*	Howard Chu
116 /*	Symas Corporation
117 /*--*/
118 
119 #endif
120