]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb2.h
3577cc3500702f21891df64095bbc883a1648f17
[ccan] / ccan / tdb2 / tdb2.h
1 #ifndef CCAN_TDB2_H
2 #define CCAN_TDB2_H
3
4 /*
5    Unix SMB/CIFS implementation.
6
7    trivial database library
8
9    Copyright (C) Andrew Tridgell 1999-2004
10
11      ** NOTE! The following LGPL license applies to the tdb
12      ** library. This does NOT imply that all of Samba is released
13      ** under the LGPL
14
15    This library is free software; you can redistribute it and/or
16    modify it under the terms of the GNU Lesser General Public
17    License as published by the Free Software Foundation; either
18    version 3 of the License, or (at your option) any later version.
19
20    This library is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    Lesser General Public License for more details.
24
25    You should have received a copy of the GNU Lesser General Public
26    License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 */
28
29 #ifdef  __cplusplus
30 extern "C" {
31 #endif
32
33 #ifndef _SAMBA_BUILD_
34 /* For mode_t */
35 #include <sys/types.h>
36 /* For O_* flags. */
37 #include <sys/stat.h>
38 /* For sig_atomic_t. */
39 #include <signal.h>
40 /* For uint64_t */
41 #include <stdint.h>
42 #endif
43 #include <ccan/compiler/compiler.h>
44
45 /* flags to tdb_store() */
46 #define TDB_REPLACE 1           /* Unused */
47 #define TDB_INSERT 2            /* Don't overwrite an existing entry */
48 #define TDB_MODIFY 3            /* Don't create an existing entry    */
49
50 /* flags for tdb_open() */
51 #define TDB_DEFAULT 0 /* just a readability place holder */
52 #define TDB_CLEAR_IF_FIRST 1
53 #define TDB_INTERNAL 2 /* don't store on disk */
54 #define TDB_NOLOCK   4 /* don't do any locking */
55 #define TDB_NOMMAP   8 /* don't use mmap */
56 #define TDB_CONVERT 16 /* convert endian */
57 #define TDB_NOSYNC   64 /* don't use synchronous transactions */
58 #define TDB_SEQNUM   128 /* maintain a sequence number */
59 #define TDB_VOLATILE   256 /* Activate the per-hashchain freelist, default 5 */
60 #define TDB_ALLOW_NESTING 512 /* Allow transactions to nest */
61
62 /* error codes */
63 enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
64                 TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOEXIST,
65                 TDB_ERR_EINVAL, TDB_ERR_RDONLY, TDB_ERR_NESTING };
66
67 /* flags for tdb_summary. Logical or to combine. */
68 enum tdb_summary_flags { TDB_SUMMARY_HISTOGRAMS = 1 };
69
70 /* logging uses one of the following levels */
71 enum tdb_log_level {TDB_LOG_ERROR = 0, TDB_LOG_USE_ERROR, TDB_LOG_WARNING};
72
73 typedef struct tdb_data {
74         unsigned char *dptr;
75         size_t dsize;
76 } TDB_DATA;
77
78 struct tdb_context;
79
80 /* FIXME: Make typesafe */
81 typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
82 typedef uint64_t (*tdb_hashfn_t)(const void *key, size_t len, uint64_t seed,
83                                  void *priv);
84
85 enum tdb_attribute_type {
86         TDB_ATTRIBUTE_LOG = 0,
87         TDB_ATTRIBUTE_HASH = 1,
88         TDB_ATTRIBUTE_SEED = 2,
89         TDB_ATTRIBUTE_STATS = 3
90 };
91
92 struct tdb_attribute_base {
93         enum tdb_attribute_type attr;
94         union tdb_attribute *next;
95 };
96
97 struct tdb_attribute_log {
98         struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_LOG */
99         void (*log_fn)(struct tdb_context *tdb,
100                        enum tdb_log_level level,
101                        void *log_private,
102                        const char *message);
103         void *log_private;
104 };
105
106 struct tdb_attribute_hash {
107         struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_HASH */
108         tdb_hashfn_t hash_fn;
109         void *hash_private;
110 };
111
112 struct tdb_attribute_seed {
113         struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_SEED */
114         uint64_t seed;
115 };
116
117 struct tdb_attribute_stats {
118         struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_STATS */
119         size_t size; /* = sizeof(struct tdb_attribute_stats) */
120         uint64_t allocs;
121         uint64_t   alloc_subhash;
122         uint64_t   alloc_chain;
123         uint64_t   alloc_bucket_exact;
124         uint64_t   alloc_bucket_max;
125         uint64_t   alloc_leftover;
126         uint64_t   alloc_coalesce_tried;
127         uint64_t     alloc_coalesce_lockfail;
128         uint64_t     alloc_coalesce_race;
129         uint64_t     alloc_coalesce_succeeded;
130         uint64_t        alloc_coalesce_num_merged;
131         uint64_t compares;
132         uint64_t   compare_wrong_bucket;
133         uint64_t   compare_wrong_offsetbits;
134         uint64_t   compare_wrong_keylen;
135         uint64_t   compare_wrong_rechash;
136         uint64_t   compare_wrong_keycmp;
137         uint64_t expands;
138         uint64_t frees;
139         uint64_t locks;
140         uint64_t    lock_lowlevel;
141         uint64_t    lock_nonblock;
142 };
143
144 union tdb_attribute {
145         struct tdb_attribute_base base;
146         struct tdb_attribute_log log;
147         struct tdb_attribute_hash hash;
148         struct tdb_attribute_seed seed;
149         struct tdb_attribute_stats stats;
150 };
151                 
152 struct tdb_context *tdb_open(const char *name, int tdb_flags,
153                              int open_flags, mode_t mode,
154                              union tdb_attribute *attributes);
155
156 struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key);
157 int tdb_delete(struct tdb_context *tdb, struct tdb_data key);
158 int tdb_store(struct tdb_context *tdb, struct tdb_data key, struct tdb_data dbuf, int flag);
159 int tdb_append(struct tdb_context *tdb, struct tdb_data key, struct tdb_data dbuf);
160 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
161 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
162 int64_t tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *p);
163 int64_t tdb_traverse_read(struct tdb_context *tdb,
164                           tdb_traverse_func fn, void *p);
165 TDB_DATA tdb_firstkey(struct tdb_context *tdb);
166 TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key);
167 int tdb_close(struct tdb_context *tdb);
168 int tdb_check(struct tdb_context *tdb,
169               int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
170               void *private_data);
171
172 enum TDB_ERROR tdb_error(const struct tdb_context *tdb);
173 const char *tdb_errorstr(const struct tdb_context *tdb);
174
175 int tdb_transaction_start(struct tdb_context *tdb);
176 void tdb_transaction_cancel(struct tdb_context *tdb);
177 int tdb_transaction_prepare_commit(struct tdb_context *tdb);
178 int tdb_transaction_commit(struct tdb_context *tdb);
179
180 char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags);
181
182 extern struct tdb_data tdb_null;
183
184 #ifdef  __cplusplus
185 }
186 #endif
187
188 #endif /* tdb2.h */