]> git.ozlabs.org Git - ccan/blob - ccan/tdb/tdb.h
tdb: implement tdb_summary.
[ccan] / ccan / tdb / tdb.h
1 #ifndef __TDB_H__
2 #define __TDB_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 #endif
41
42 /* flags to tdb_store() */
43 #define TDB_REPLACE 1           /* Unused */
44 #define TDB_INSERT 2            /* Don't overwrite an existing entry */
45 #define TDB_MODIFY 3            /* Don't create an existing entry    */
46
47 /* flags for tdb_open() */
48 #define TDB_DEFAULT 0 /* just a readability place holder */
49 #define TDB_CLEAR_IF_FIRST 1
50 #define TDB_INTERNAL 2 /* don't store on disk */
51 #define TDB_NOLOCK   4 /* don't do any locking */
52 #define TDB_NOMMAP   8 /* don't use mmap */
53 #define TDB_CONVERT 16 /* convert endian (internal use) */
54 #define TDB_BIGENDIAN 32 /* header is big-endian (internal use) */
55 #define TDB_NOSYNC   64 /* don't use synchronous transactions */
56 #define TDB_SEQNUM   128 /* maintain a sequence number */
57 #define TDB_VOLATILE   256 /* Activate the per-hashchain freelist, default 5 */
58 #define TDB_ALLOW_NESTING 512 /* Allow transactions to nest */
59 #define TDB_DISALLOW_NESTING 1024 /* Disallow transactions to nest */
60 #define TDB_INCOMPATIBLE_HASH 2048 /* Better hashing: can't be opened by older tdb versions. */
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_NOLOCK, TDB_ERR_LOCK_TIMEOUT,
65                 TDB_ERR_NOEXIST, TDB_ERR_EINVAL, TDB_ERR_RDONLY,
66                 TDB_ERR_NESTING};
67
68 /* flags for tdb_summary. Logical or to combine. */
69 enum tdb_summary_flags { TDB_SUMMARY_HISTOGRAMS = 1 };
70
71 /* debugging uses one of the following levels */
72 enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR, 
73                       TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
74
75 typedef struct TDB_DATA {
76         unsigned char *dptr;
77         size_t dsize;
78 } TDB_DATA;
79
80 #ifndef PRINTF_ATTRIBUTE
81 #if (__GNUC__ >= 3)
82 /** Use gcc attribute to check printf fns.  a1 is the 1-based index of
83  * the parameter containing the format, and a2 the index of the first
84  * argument. Note that some gcc 2.x versions don't handle this
85  * properly **/
86 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
87 #else
88 #define PRINTF_ATTRIBUTE(a1, a2)
89 #endif
90 #endif
91
92 /* this is the context structure that is returned from a db open */
93 typedef struct tdb_context TDB_CONTEXT;
94
95 typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
96 typedef void (*tdb_log_func)(struct tdb_context *, enum tdb_debug_level, const char *, ...) PRINTF_ATTRIBUTE(3, 4);
97 typedef unsigned int (*tdb_hash_func)(TDB_DATA *key);
98
99 struct tdb_logging_context {
100         tdb_log_func log_fn;
101         void *log_private;
102 };
103
104 struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
105                       int open_flags, mode_t mode);
106 struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
107                          int open_flags, mode_t mode,
108                          const struct tdb_logging_context *log_ctx,
109                          tdb_hash_func hash_fn);
110 void tdb_set_max_dead(struct tdb_context *tdb, int max_dead);
111
112 int tdb_reopen(struct tdb_context *tdb);
113 int tdb_reopen_all(int parent_longlived);
114 void tdb_set_logging_function(struct tdb_context *tdb, const struct tdb_logging_context *log_ctx);
115 enum TDB_ERROR tdb_error(struct tdb_context *tdb);
116 const char *tdb_errorstr(struct tdb_context *tdb);
117 TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
118 int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
119                      int (*parser)(TDB_DATA key, TDB_DATA data,
120                                    void *private_data),
121                      void *private_data);
122 int tdb_delete(struct tdb_context *tdb, TDB_DATA key);
123 int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
124 int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf);
125 int tdb_close(struct tdb_context *tdb);
126 TDB_DATA tdb_firstkey(struct tdb_context *tdb);
127 TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key);
128 int tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *);
129 int tdb_traverse_read(struct tdb_context *tdb, tdb_traverse_func fn, void *);
130 int tdb_exists(struct tdb_context *tdb, TDB_DATA key);
131 int tdb_lockall(struct tdb_context *tdb);
132 int tdb_lockall_nonblock(struct tdb_context *tdb);
133 int tdb_unlockall(struct tdb_context *tdb);
134 int tdb_lockall_read(struct tdb_context *tdb);
135 int tdb_lockall_read_nonblock(struct tdb_context *tdb);
136 int tdb_unlockall_read(struct tdb_context *tdb);
137 int tdb_lockall_mark(struct tdb_context *tdb);
138 int tdb_lockall_unmark(struct tdb_context *tdb);
139 const char *tdb_name(struct tdb_context *tdb);
140 int tdb_fd(struct tdb_context *tdb);
141 tdb_log_func tdb_log_fn(struct tdb_context *tdb);
142 void *tdb_get_logging_private(struct tdb_context *tdb);
143 int tdb_transaction_start(struct tdb_context *tdb);
144 int tdb_transaction_prepare_commit(struct tdb_context *tdb);
145 int tdb_transaction_commit(struct tdb_context *tdb);
146 int tdb_transaction_cancel(struct tdb_context *tdb);
147 int tdb_transaction_recover(struct tdb_context *tdb);
148 int tdb_get_seqnum(struct tdb_context *tdb);
149 int tdb_hash_size(struct tdb_context *tdb);
150 size_t tdb_map_size(struct tdb_context *tdb);
151 int tdb_get_flags(struct tdb_context *tdb);
152 void tdb_add_flags(struct tdb_context *tdb, unsigned flag);
153 void tdb_remove_flags(struct tdb_context *tdb, unsigned flag);
154 void tdb_enable_seqnum(struct tdb_context *tdb);
155 void tdb_increment_seqnum_nonblock(struct tdb_context *tdb);
156 unsigned int tdb_jenkins_hash(TDB_DATA *key);
157 int tdb_check(struct tdb_context *tdb,
158               int (*check)(TDB_DATA key, TDB_DATA data, void *private),
159               void *private);
160
161 /* Low level locking functions: use with care */
162 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
163 int tdb_chainlock_nonblock(struct tdb_context *tdb, TDB_DATA key);
164 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
165 int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
166 int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
167 int tdb_chainlock_mark(struct tdb_context *tdb, TDB_DATA key);
168 int tdb_chainlock_unmark(struct tdb_context *tdb, TDB_DATA key);
169
170 void tdb_setalarm_sigptr(struct tdb_context *tdb, volatile sig_atomic_t *sigptr);
171
172 /* wipe and repack */
173 int tdb_wipe_all(struct tdb_context *tdb);
174 int tdb_repack(struct tdb_context *tdb);
175
176 /* Debug functions. Not used in production. */
177 void tdb_dump_all(struct tdb_context *tdb);
178 int tdb_printfreelist(struct tdb_context *tdb);
179 int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries);
180 int tdb_freelist_size(struct tdb_context *tdb);
181 char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags);
182
183 extern TDB_DATA tdb_null;
184
185 #ifdef  __cplusplus
186 }
187 #endif
188
189 #endif /* tdb.h */