tdb2.h 5.9 KB

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