5 TDB version 2: trivial database library
7 Copyright (C) Andrew Tridgell 1999-2004
8 Copyright (C) Rusty Russell 2010-2011
10 ** NOTE! The following LGPL license applies to the tdb
11 ** library. This does NOT imply that all of Samba is released
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 3 of the License, or (at your option) any later version.
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, see <http://www.gnu.org/licenses/>.
34 #include <sys/types.h>
37 /* For sig_atomic_t. */
42 #include <ccan/compiler/compiler.h>
48 * tdb_open - open a database file
49 * @name: the file name (can be NULL if flags contains TDB_INTERNAL)
50 * @tdb_flags: options for this database
51 * @open_flags: flags argument for tdb's open() call.
52 * @mode: mode argument for tdb's open() call.
53 * @attributes: linked list of extra attributes for this tdb.
55 * This call opens (and potentially creates) a database file.
56 * Multiple processes can have the TDB file open at once.
58 * On failure it will return NULL, and set errno: it may also call
59 * any log attribute found in @attributes.
64 struct tdb_context *tdb_open(const char *name, int tdb_flags,
65 int open_flags, mode_t mode,
66 union tdb_attribute *attributes);
69 /* flags for tdb_open() */
70 #define TDB_DEFAULT 0 /* just a readability place holder */
71 #define TDB_INTERNAL 2 /* don't store on disk */
72 #define TDB_NOLOCK 4 /* don't do any locking */
73 #define TDB_NOMMAP 8 /* don't use mmap */
74 #define TDB_CONVERT 16 /* convert endian */
75 #define TDB_NOSYNC 64 /* don't use synchronous transactions */
78 * tdb_close - close and free a tdb.
79 * @tdb: the tdb context returned from tdb_open()
81 * This always succeeds, in that @tdb is unusable after this call. But if
82 * some unexpected error occurred while closing, it will return non-zero
83 * (the only clue as to cause will be via the log attribute).
85 int tdb_close(struct tdb_context *tdb);
88 * struct tdb_data - representation of keys or values.
89 * @dptr: the data pointer
90 * @dsize: the size of the data pointed to by dptr.
92 * This is the "blob" representation of keys and data used by TDB.
94 typedef struct tdb_data {
100 * enum TDB_ERROR - error returns for TDB
106 TDB_SUCCESS = 0, /* No error. */
107 TDB_ERR_CORRUPT = -1, /* We read the db, and it was bogus. */
108 TDB_ERR_IO = -2, /* We couldn't read/write the db. */
109 TDB_ERR_LOCK = -3, /* Locking failed. */
110 TDB_ERR_OOM = -4, /* Out of Memory. */
111 TDB_ERR_EXISTS = -5, /* The key already exists. */
112 TDB_ERR_NOEXIST = -6, /* The key does not exist. */
113 TDB_ERR_EINVAL = -7, /* You're using it wrong. */
114 TDB_ERR_RDONLY = -8, /* The database is read-only. */
115 TDB_ERR_LAST = TDB_ERR_RDONLY
119 * tdb_store - store a key/value pair in a tdb.
120 * @tdb: the tdb context returned from tdb_open()
122 * @dbuf: the data to associate with the key.
123 * @flag: TDB_REPLACE, TDB_INSERT or TDB_MODIFY.
125 * This inserts (or overwrites) a key/value pair in the TDB. If flag
126 * is TDB_REPLACE, it doesn't matter whether the key exists or not;
127 * TDB_INSERT means it must not exist (TDB_ERR_EXISTS otherwise),
128 * and TDB_MODIFY means it must exist (TDB_ERR_NOEXIST otherwise).
130 * On success, this returns 0, on failure -1, and sets tdb_error().
133 * tdb_fetch, tdb_transaction_start, tdb_append, tdb_delete.
135 int tdb_store(struct tdb_context *tdb,
137 struct tdb_data dbuf,
140 /* flags to tdb_store() */
141 #define TDB_REPLACE 1 /* A readability place holder */
142 #define TDB_INSERT 2 /* Don't overwrite an existing entry */
143 #define TDB_MODIFY 3 /* Don't create an existing entry */
146 * tdb_fetch - fetch a value from a tdb.
147 * @tdb: the tdb context returned from tdb_open()
150 * This looks up a key in the database and returns it, or returns tdb_null
151 * and sets tdb_error() if there's a problem (usually, TDB_ERR_NOEXIST).
153 * It is your responsibility to call free() on the returned structure's
156 struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key);
159 * enum TDB_ERROR - error codes for TDB
162 * tdb_error(), tdb_errorstr()
166 * tdb_error - fetch the last error value from the tdb.
167 * @tdb: the tdb context returned from tdb_open()
169 * This returns the last error, or TDB_SUCCESS. It always returns TDB_SUCCESS
170 * immediately after tdb_open() returns the (non-NULL) tdb context.
175 enum TDB_ERROR tdb_error(const struct tdb_context *tdb);
178 * tdb_errorstr - map the tdb error onto a constant readable string
179 * @tdb: the tdb context returned from tdb_open()
181 * This is more useful for displaying errors to users than tdb_error.
186 const char *tdb_errorstr(const struct tdb_context *tdb);
189 * tdb_append - append a value to a key/value pair in a tdb.
190 * @tdb: the tdb context returned from tdb_open()
192 * @dbuf: the data to append.
194 * This is equivalent to fetching a record, reallocating .dptr to add the
195 * data, and writing it back, only it's much more efficient. If the key
196 * doesn't exist, it's equivalent to tdb_store (with an additional hint that
197 * you expect to expand the record in future).
199 * Returns 0 on success, -1 on failure (and sets tdb_error()).
202 * tdb_fetch(), tdb_store()
204 int tdb_append(struct tdb_context *tdb,
206 struct tdb_data dbuf);
209 * tdb_delete - delete a key from a tdb.
210 * @tdb: the tdb context returned from tdb_open()
211 * @key: the key to delete.
213 * Returns 0 on success, or -1 on error (usually tdb_error() would be
214 * TDB_ERR_NOEXIST in that case).
217 * tdb_fetch(), tdb_store()
219 int tdb_delete(struct tdb_context *tdb, struct tdb_data key);
222 * tdb_transaction_start - start a transaction
223 * @tdb: the tdb context returned from tdb_open()
225 * This begins a series of atomic operations. Other processes will be able
226 * to read the tdb, but not alter it (they will block), nor will they see
227 * any changes until tdb_transaction_commit() is called.
229 * On failure, returns -1 and sets tdb_error().
232 * tdb_transaction_cancel, tdb_transaction_commit.
234 int tdb_transaction_start(struct tdb_context *tdb);
237 * tdb_transaction_cancel - abandon a transaction
238 * @tdb: the tdb context returned from tdb_open()
240 * This aborts a transaction, discarding any changes which were made.
241 * tdb_close() does this implicitly.
243 void tdb_transaction_cancel(struct tdb_context *tdb);
246 * tdb_transaction_commit - commit a transaction
247 * @tdb: the tdb context returned from tdb_open()
249 * This completes a transaction, writing any changes which were made.
251 * fsync() is used to commit the transaction (unless TDB_NOSYNC is set),
252 * making it robust against machine crashes, but very slow compared to
253 * other TDB operations.
255 * Returns 0 on success, or -1 on failure: this can only be caused by
256 * unexpected errors (eg. I/O or memory); this is no point looping on
257 * transaction failure.
260 * tdb_transaction_prepare_commit()
262 int tdb_transaction_commit(struct tdb_context *tdb);
265 * tdb_transaction_prepare_commit - prepare to commit a transaction
266 * @tdb: the tdb context returned from tdb_open()
268 * This ensures we have the resources to commit a transaction (using
269 * tdb_transaction_commit): if this succeeds then a transaction will only
270 * fail if the write() or fsync() calls fail.
272 * Returns 0 on success, or -1 on failure.
275 * tdb_transaction_commit()
277 int tdb_transaction_prepare_commit(struct tdb_context *tdb);
279 /* FIXME: Make typesafe */
280 typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
283 * tdb_traverse - traverse a TDB
284 * @tdb: the tdb context returned from tdb_open()
285 * @fn: the function to call for every key/value pair (or NULL)
286 * @p: the pointer to hand to @f
288 * This walks the TDB until all they keys have been traversed, or @fn
289 * returns non-zero. If the traverse function or other processes are
290 * changing data or adding or deleting keys, the traverse may be
291 * unreliable: keys may be skipped or (rarely) visited twice.
293 * There is one specific exception: the special case of deleting the
294 * current key does not undermine the reliability of the traversal.
296 * On success, returns the number of keys iterated. On error returns
297 * -1 and sets tdb_error().
299 int64_t tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *p);
302 * tdb_firstkey - get the "first" key in a TDB
303 * @tdb: the tdb context returned from tdb_open()
305 * This returns an arbitrary key in the database; with tdb_nextkey() it allows
306 * open-coded traversal of the database.
308 * On failure, returns tdb_null and sets tdb_error(). On success, returns
309 * a key, or tdb_null and set tdb_error() to TDB_SUCCESS for an empty database.
311 TDB_DATA tdb_firstkey(struct tdb_context *tdb);
314 * tdb_nextkey - get the "next" key in a TDB
315 * @tdb: the tdb context returned from tdb_open()
316 * @key: a key returned by tdb_firstkey() or tdb_nextkey().
318 * This returns another key in the database. On failure or the last key
319 * it returns tdb_null: tdb_error() will be TDB_SUCCESS if it was the last key.
321 TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key);
324 * tdb_chainlock - lock a record in the TDB
325 * @tdb: the tdb context returned from tdb_open()
326 * @key: the key to lock.
328 * This prevents any changes from occurring to a group of keys including @key,
329 * even if @key does not exist. This allows primitive atomic updates of
330 * records without using transactions.
332 * You cannot begin a transaction while holding a tdb_chainlock(), nor can
333 * you do any operations on any other keys in the database. This also means
334 * that you cannot hold more than one tdb_chainlock() at a time.
339 int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
342 * tdb_chainunlock - unlock a record in the TDB
343 * @tdb: the tdb context returned from tdb_open()
344 * @key: the key to unlock.
346 int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
349 * tdb_check - check a TDB for consistency
350 * @tdb: the tdb context returned from tdb_open()
351 * @check: function to check each key/data pair (or NULL)
352 * @private_data: pointer for @check
354 * This performs a consistency check of the open database, optionally calling
355 * a check() function on each record so you can do your own data consistency
356 * checks as well. If check() returns non-zero, it is considered a failure.
358 * Returns 0 on success, or -1 on failure and sets tdb_error().
360 int tdb_check(struct tdb_context *tdb,
361 int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
365 * enum tdb_summary_flags - flags for tdb_summary.
367 enum tdb_summary_flags {
368 TDB_SUMMARY_HISTOGRAMS = 1 /* Draw graphs in the summary. */
372 * tdb_summary - return a string describing the TDB state
373 * @tdb: the tdb context returned from tdb_open()
374 * @flags: flags to control the summary output.
376 * This returns a developer-readable string describing the overall
377 * state of the tdb, such as the percentage used and sizes of records.
378 * It is designed to provide information about the tdb at a glance
379 * without displaying any keys or data in the database.
381 * On success, returns a nul-terminated multi-line string. On failure,
382 * returns NULL and sets tdb_error().
384 char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags);
389 * enum tdb_attribute_type - descriminator for union tdb_attribute.
391 enum tdb_attribute_type {
392 TDB_ATTRIBUTE_LOG = 0,
393 TDB_ATTRIBUTE_HASH = 1,
394 TDB_ATTRIBUTE_SEED = 2,
395 TDB_ATTRIBUTE_STATS = 3
399 * struct tdb_attribute_base - common fields for all tdb attributes.
401 struct tdb_attribute_base {
402 enum tdb_attribute_type attr;
403 union tdb_attribute *next;
407 * enum tdb_log_level - log levels for tdb_attribute_log
408 * @TDB_LOG_ERROR: used to log unrecoverable errors such as I/O errors
409 * or internal consistency failures.
410 * @TDB_LOG_USE_ERROR: used to log usage errors such as invalid parameters
411 * or writing to a read-only database.
412 * @TDB_LOG_WARNING: used for informational messages on issues which
413 * are unusual but handled by TDB internally, such
414 * as a failure to mmap or failure to open /dev/urandom.
423 * struct tdb_attribute_log - log function attribute
425 * This attribute provides a hook for you to log errors.
427 struct tdb_attribute_log {
428 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_LOG */
429 void (*log_fn)(struct tdb_context *tdb,
430 enum tdb_log_level level,
432 const char *message);
437 * struct tdb_attribute_hash - hash function attribute
439 * This attribute allows you to provide an alternative hash function.
440 * This hash function will be handed keys from the database; it will also
441 * be handed the 8-byte TDB_HASH_MAGIC value for checking the header (the
442 * tdb_open() will fail if the hash value doesn't match the header).
444 * Note that if your hash function gives different results on
445 * different machine endians, your tdb will no longer work across
446 * different architectures!
448 struct tdb_attribute_hash {
449 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_HASH */
450 uint64_t (*hash_fn)(const void *key, size_t len, uint64_t seed,
456 * struct tdb_attribute_seed - hash function seed attribute
458 * The hash function seed is normally taken from /dev/urandom (or equivalent)
459 * but can be set manually here. This is mainly for testing purposes.
461 struct tdb_attribute_seed {
462 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_SEED */
467 * struct tdb_attribute_stats - tdb operational statistics
469 * This attribute records statistics of various low-level TDB operations.
470 * This can be used to assist performance evaluation.
472 * New fields will be added at the end, hence the "size" argument which
473 * indicates how large your structure is. If your size is larger than
474 * that known about by this version of tdb, the size will be reduced to
475 * the known structure size. Thus you can detect older versions, and
476 * thus know that newer stats will not be updated.
478 struct tdb_attribute_stats {
479 struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_STATS */
480 size_t size; /* = sizeof(struct tdb_attribute_stats) */
482 uint64_t alloc_subhash;
483 uint64_t alloc_chain;
484 uint64_t alloc_bucket_exact;
485 uint64_t alloc_bucket_max;
486 uint64_t alloc_leftover;
487 uint64_t alloc_coalesce_tried;
488 uint64_t alloc_coalesce_lockfail;
489 uint64_t alloc_coalesce_race;
490 uint64_t alloc_coalesce_succeeded;
491 uint64_t alloc_coalesce_num_merged;
493 uint64_t compare_wrong_bucket;
494 uint64_t compare_wrong_offsetbits;
495 uint64_t compare_wrong_keylen;
496 uint64_t compare_wrong_rechash;
497 uint64_t compare_wrong_keycmp;
501 uint64_t lock_lowlevel;
502 uint64_t lock_nonblock;
506 * union tdb_attribute - tdb attributes.
508 * This represents all the known attributes.
511 * struct tdb_attribute_log, struct tdb_attribute_hash,
512 * struct tdb_attribute_seed, struct tdb_attribute_stats.
514 union tdb_attribute {
515 struct tdb_attribute_base base;
516 struct tdb_attribute_log log;
517 struct tdb_attribute_hash hash;
518 struct tdb_attribute_seed seed;
519 struct tdb_attribute_stats stats;
523 * tdb_null - a convenient value for errors.
525 extern struct tdb_data tdb_null;