tdb2.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. /* error codes */
  50. enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
  51. TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOEXIST,
  52. TDB_ERR_EINVAL, TDB_ERR_RDONLY };
  53. /* flags for tdb_summary. Logical or to combine. */
  54. enum tdb_summary_flags { TDB_SUMMARY_HISTOGRAMS = 1 };
  55. /* logging uses one of the following levels */
  56. enum tdb_log_level {TDB_LOG_ERROR = 0, TDB_LOG_USE_ERROR, TDB_LOG_WARNING};
  57. typedef struct tdb_data {
  58. unsigned char *dptr;
  59. size_t dsize;
  60. } TDB_DATA;
  61. struct tdb_context;
  62. /* FIXME: Make typesafe */
  63. typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
  64. enum tdb_attribute_type {
  65. TDB_ATTRIBUTE_LOG = 0,
  66. TDB_ATTRIBUTE_HASH = 1,
  67. TDB_ATTRIBUTE_SEED = 2,
  68. TDB_ATTRIBUTE_STATS = 3
  69. };
  70. struct tdb_attribute_base {
  71. enum tdb_attribute_type attr;
  72. union tdb_attribute *next;
  73. };
  74. struct tdb_attribute_log {
  75. struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_LOG */
  76. void (*log_fn)(struct tdb_context *tdb,
  77. enum tdb_log_level level,
  78. void *log_private,
  79. const char *message);
  80. void *log_private;
  81. };
  82. struct tdb_attribute_hash {
  83. struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_HASH */
  84. uint64_t (*hash_fn)(const void *key, size_t len, uint64_t seed,
  85. void *priv);
  86. void *hash_private;
  87. };
  88. struct tdb_attribute_seed {
  89. struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_SEED */
  90. uint64_t seed;
  91. };
  92. struct tdb_attribute_stats {
  93. struct tdb_attribute_base base; /* .attr = TDB_ATTRIBUTE_STATS */
  94. size_t size; /* = sizeof(struct tdb_attribute_stats) */
  95. uint64_t allocs;
  96. uint64_t alloc_subhash;
  97. uint64_t alloc_chain;
  98. uint64_t alloc_bucket_exact;
  99. uint64_t alloc_bucket_max;
  100. uint64_t alloc_leftover;
  101. uint64_t alloc_coalesce_tried;
  102. uint64_t alloc_coalesce_lockfail;
  103. uint64_t alloc_coalesce_race;
  104. uint64_t alloc_coalesce_succeeded;
  105. uint64_t alloc_coalesce_num_merged;
  106. uint64_t compares;
  107. uint64_t compare_wrong_bucket;
  108. uint64_t compare_wrong_offsetbits;
  109. uint64_t compare_wrong_keylen;
  110. uint64_t compare_wrong_rechash;
  111. uint64_t compare_wrong_keycmp;
  112. uint64_t expands;
  113. uint64_t frees;
  114. uint64_t locks;
  115. uint64_t lock_lowlevel;
  116. uint64_t lock_nonblock;
  117. };
  118. union tdb_attribute {
  119. struct tdb_attribute_base base;
  120. struct tdb_attribute_log log;
  121. struct tdb_attribute_hash hash;
  122. struct tdb_attribute_seed seed;
  123. struct tdb_attribute_stats stats;
  124. };
  125. struct tdb_context *tdb_open(const char *name, int tdb_flags,
  126. int open_flags, mode_t mode,
  127. union tdb_attribute *attributes);
  128. struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key);
  129. int tdb_delete(struct tdb_context *tdb, struct tdb_data key);
  130. int tdb_store(struct tdb_context *tdb, struct tdb_data key, struct tdb_data dbuf, int flag);
  131. int tdb_append(struct tdb_context *tdb, struct tdb_data key, struct tdb_data dbuf);
  132. int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
  133. int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
  134. int64_t tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *p);
  135. int64_t tdb_traverse_read(struct tdb_context *tdb,
  136. tdb_traverse_func fn, void *p);
  137. TDB_DATA tdb_firstkey(struct tdb_context *tdb);
  138. TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key);
  139. int tdb_close(struct tdb_context *tdb);
  140. int tdb_check(struct tdb_context *tdb,
  141. int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
  142. void *private_data);
  143. enum TDB_ERROR tdb_error(const struct tdb_context *tdb);
  144. const char *tdb_errorstr(const struct tdb_context *tdb);
  145. int tdb_transaction_start(struct tdb_context *tdb);
  146. void tdb_transaction_cancel(struct tdb_context *tdb);
  147. int tdb_transaction_prepare_commit(struct tdb_context *tdb);
  148. int tdb_transaction_commit(struct tdb_context *tdb);
  149. char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags);
  150. extern struct tdb_data tdb_null;
  151. #ifdef __cplusplus
  152. }
  153. #endif
  154. #endif /* tdb2.h */