TDB_porting.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. Interface differences between TDB and NTDB.
  2. - ntdb shares 'struct TDB_DATA' with tdb, but TDB defines the TDB_DATA
  3. typedef, whereas ntdb defines NTDB_DATA (ie. both are compatible).
  4. If you include both ntdb.h and tdb.h, #include tdb.h first,
  5. otherwise you'll get a compile error when tdb.h re-defined struct
  6. TDB_DATA.
  7. Example:
  8. #include <tdb.h>
  9. #include <ntdb.h>
  10. - ntdb functions return NTDB_SUCCESS (ie 0) on success, and a negative
  11. error on failure, whereas tdb functions returned 0 on success, and
  12. -1 on failure. tdb then used tdb_error() to determine the error;
  13. this API is nasty if we ever want to support threads, so is not supported.
  14. Example:
  15. #include <tdb.h>
  16. #include <ntdb.h>
  17. void tdb_example(struct tdb_context *tdb, TDB_DATA key, TDB_DATA d)
  18. {
  19. if (tdb_store(tdb, key, d) == -1) {
  20. printf("store failed: %s\n", tdb_errorstr(tdb));
  21. }
  22. }
  23. void ntdb_example(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA d)
  24. {
  25. enum NTDB_ERROR e;
  26. e = ntdb_store(ntdb, key, d);
  27. if (e) {
  28. printf("store failed: %s\n", ntdb_errorstr(e));
  29. }
  30. }
  31. - ntdb's ntdb_fetch() returns an error, tdb's returned the data directly
  32. (or tdb_null, and you were supposed to check tdb_error() to find out why).
  33. Example:
  34. #include <tdb.h>
  35. #include <ntdb.h>
  36. void tdb_example(struct tdb_context *tdb, TDB_DATA key)
  37. {
  38. TDB_DATA data;
  39. data = tdb_fetch(tdb, key);
  40. if (!data.dptr) {
  41. printf("fetch failed: %s\n", tdb_errorstr(tdb));
  42. }
  43. }
  44. void ntdb_example(struct ntdb_context *ntdb, NTDB_DATA key)
  45. {
  46. NTDB_DATA data;
  47. enum NTDB_ERROR e;
  48. e = ntdb_fetch(ntdb, key, &data);
  49. if (e) {
  50. printf("fetch failed: %s\n", ntdb_errorstr(e));
  51. }
  52. }
  53. - ntdb's ntdb_nextkey() frees the old key's dptr, in tdb you needed to do
  54. this manually.
  55. Example:
  56. #include <tdb.h>
  57. #include <ntdb.h>
  58. void tdb_example(struct tdb_context *tdb)
  59. {
  60. TDB_DATA key, next, data;
  61. for (key = tdb_firstkey(tdb); key.dptr; key = next) {
  62. printf("Got key!\n");
  63. next = tdb_nextkey(tdb, key);
  64. free(key.dptr);
  65. }
  66. }
  67. void ntdb_example(struct ntdb_context *ntdb)
  68. {
  69. NTDB_DATA k, data;
  70. enum NTDB_ERROR e;
  71. for (e = ntdb_firstkey(ntdb,&k); !e; e = ntdb_nextkey(ntdb,&k))
  72. printf("Got key!\n");
  73. }
  74. - Unlike tdb_open/tdb_open_ex, ntdb_open does not allow NULL names,
  75. even for NTDB_INTERNAL dbs, and thus ntdb_name() never returns NULL.
  76. Example:
  77. #include <tdb.h>
  78. #include <ntdb.h>
  79. struct tdb_context *tdb_example(void)
  80. {
  81. return tdb_open(NULL, 0, TDB_INTERNAL, O_RDWR, 0);
  82. }
  83. struct ntdb_context *ntdb_example(void)
  84. {
  85. return ntdb_open("example", NTDB_INTERNAL, O_RDWR, 0);
  86. }
  87. - ntdb uses a linked list of attribute structures to implement logging and
  88. alternate hashes. tdb used tdb_open_ex, which was not extensible.
  89. Example:
  90. #include <tdb.h>
  91. #include <ntdb.h>
  92. /* Custom hash function */
  93. static unsigned int my_tdb_hash_func(TDB_DATA *key)
  94. {
  95. return key->dsize;
  96. }
  97. struct tdb_context *tdb_example(void)
  98. {
  99. return tdb_open_ex("example.tdb", 0, TDB_DEFAULT,
  100. O_CREAT|O_RDWR, 0600, NULL, my_hash_func);
  101. }
  102. /* Custom hash function */
  103. static unsigned int my_ntdb_hash_func(const void *key, size_t len,
  104. uint32_t seed, void *data)
  105. {
  106. return len;
  107. }
  108. struct ntdb_context *ntdb_example(void)
  109. {
  110. union ntdb_attribute hash;
  111. hash.base.attr = NTDB_ATTRIBUTE_HASH;
  112. hash.base.next = NULL;
  113. hash.hash.fn = my_ntdb_hash_func;
  114. return ntdb_open("example.ntdb", NTDB_DEFAULT,
  115. O_CREAT|O_RDWR, 0600, &hash);
  116. }
  117. - tdb's tdb_open/tdb_open_ex took an explicit hash size, defaulting to
  118. 131. ntdb's uses an attribute for this, defaulting to 8192.
  119. Example:
  120. #include <tdb.h>
  121. #include <ntdb.h>
  122. struct tdb_context *tdb_example(void)
  123. {
  124. return tdb_open("example.tdb", 10007, TDB_DEFAULT,
  125. O_CREAT|O_RDWR, 0600);
  126. }
  127. struct ntdb_context *ntdb_example(void)
  128. {
  129. union ntdb_attribute hashsize;
  130. hashsize.base.attr = NTDB_ATTRIBUTE_HASHSIZE;
  131. hashsize.base.next = NULL;
  132. hashsize.hashsize.size = 16384;
  133. return ntdb_open("example.ntdb", NTDB_DEFAULT,
  134. O_CREAT|O_RDWR, 0600, &hashsize);
  135. }
  136. - ntdb's log function is simpler than tdb's log function. The string
  137. is already formatted, is not terminated by a '\n', and it takes an
  138. enum ntdb_log_level not a tdb_debug_level, and which has only three
  139. values: NTDB_LOG_ERROR, NTDB_LOG_USE_ERROR and NTDB_LOG_WARNING.
  140. #include <tdb.h>
  141. #include <ntdb.h>
  142. static void tdb_log(struct tdb_context *tdb,
  143. enum tdb_debug_level level, const char *fmt, ...)
  144. {
  145. va_list ap;
  146. const char *name;
  147. switch (level) {
  148. case TDB_DEBUG_FATAL:
  149. fprintf(stderr, "FATAL: ");
  150. break;
  151. case TDB_DEBUG_ERROR:
  152. fprintf(stderr, "ERROR: ");
  153. break;
  154. case TDB_DEBUG_WARNING:
  155. fprintf(stderr, "WARNING: ");
  156. break;
  157. case TDB_DEBUG_TRACE:
  158. /* Don't print out tracing. */
  159. return;
  160. }
  161. name = tdb_name(tdb);
  162. if (!name) {
  163. name = "unnamed";
  164. }
  165. fprintf(stderr, "tdb(%s):", name);
  166. va_start(ap, fmt);
  167. vfprintf(stderr, fmt, ap);
  168. va_end(ap);
  169. }
  170. struct tdb_context *tdb_example(void)
  171. {
  172. struct tdb_logging_context lctx;
  173. lctx.log_fn = tdb_log;
  174. return tdb_open_ex("example.tdb", 0, TDB_DEFAULT,
  175. O_CREAT|O_RDWR, 0600, &lctx, NULL);
  176. }
  177. static void ntdb_log(struct ntdb_context *ntdb,
  178. enum ntdb_log_level level,
  179. enum NTDB_ERROR ecode,
  180. const char *message,
  181. void *data)
  182. {
  183. switch (level) {
  184. case NTDB_LOG_ERROR:
  185. fprintf(stderr, "ERROR: ");
  186. break;
  187. case NTDB_LOG_USE_ERROR:
  188. /* We made a mistake, so abort. */
  189. abort();
  190. break;
  191. case NTDB_LOG_WARNING:
  192. fprintf(stderr, "WARNING: ");
  193. break;
  194. }
  195. fprintf(stderr, "ntdb(%s):%s:%s\n",
  196. ntdb_name(ntdb), ntdb_errorstr(ecode), message);
  197. }
  198. struct ntdb_context *ntdb_example(void)
  199. {
  200. union ntdb_attribute log;
  201. log.base.attr = NTDB_ATTRIBUTE_LOG;
  202. log.base.next = NULL;
  203. log.log.fn = ntdb_log;
  204. return ntdb_open("example.ntdb", NTDB_DEFAULT,
  205. O_CREAT|O_RDWR, 0600, &log);
  206. }
  207. - ntdb provides ntdb_deq() for comparing two NTDB_DATA, and ntdb_mkdata() for
  208. creating an NTDB_DATA.
  209. #include <tdb.h>
  210. #include <ntdb.h>
  211. void tdb_example(struct tdb_context *tdb)
  212. {
  213. TDB_DATA data, key;
  214. key.dsize = strlen("hello");
  215. key.dptr = "hello";
  216. data = tdb_fetch(tdb, key);
  217. if (data.dsize == key.dsize
  218. && !memcmp(data.dptr, key.dptr, key.dsize))
  219. printf("key is same as data\n");
  220. }
  221. free(data.dptr);
  222. }
  223. void ntdb_example(struct ntdb_context *ntdb)
  224. {
  225. NTDB_DATA data, key;
  226. key = ntdb_mkdata("hello", strlen("hello"));
  227. if (ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS) {
  228. if (ntdb_deq(key, data)) {
  229. printf("key is same as data\n");
  230. }
  231. free(data.dptr);
  232. }
  233. }
  234. - ntdb's ntdb_parse_record() takes a type-checked callback data
  235. pointer, not a void * (though a void * pointer still works). The
  236. callback function is allowed to do read operations on the database,
  237. or write operations if you first call ntdb_lockall(). TDB's
  238. tdb_parse_record() did not allow any database access within the
  239. callback, could crash if you tried.
  240. Example:
  241. #include <tdb.h>
  242. #include <ntdb.h>
  243. static int tdb_parser(TDB_DATA key, TDB_DATA data, void *private_data)
  244. {
  245. TDB_DATA *expect = private_data;
  246. return data.dsize == expect->dsize
  247. && !memcmp(data.dptr, expect->dptr, data.dsize);
  248. }
  249. void tdb_example(struct tdb_context *tdb, TDB_DATA key, NTDB_DATA d)
  250. {
  251. switch (tdb_parse_record(tdb, key, tdb_parser, &d)) {
  252. case -1:
  253. printf("parse failed: %s\n", tdb_errorstr(tdb));
  254. break;
  255. case 0:
  256. printf("data was different!\n");
  257. break;
  258. case 1:
  259. printf("data was same!\n");
  260. break;
  261. }
  262. }
  263. static int ntdb_parser(TDB_DATA key, TDB_DATA data, TDB_DATA *expect)
  264. {
  265. return ntdb_deq(data, *expect);
  266. }
  267. void ntdb_example(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA d)
  268. {
  269. enum NTDB_ERROR e;
  270. e = tdb_parse_record(tdb, key, tdb_parser, &d);
  271. switch (e) {
  272. case 0:
  273. printf("data was different!\n");
  274. break;
  275. case 1:
  276. printf("data was same!\n");
  277. break;
  278. default:
  279. printf("parse failed: %s\n", ntdb_errorstr(e));
  280. break;
  281. }
  282. }
  283. - ntdb does locking on read-only databases (ie. O_RDONLY passed to ntdb_open).
  284. tdb did not: use the NTDB_NOLOCK flag if you want to suppress locking.
  285. Example:
  286. #include <tdb.h>
  287. #include <ntdb.h>
  288. struct tdb_context *tdb_example(void)
  289. {
  290. return tdb_open("example.tdb", 0, TDB_DEFAULT, O_RDONLY, 0);
  291. }
  292. struct ntdb_context *ntdb_example(void)
  293. {
  294. return ntdb_open("example.ntdb", NTDB_NOLOCK, O_RDONLY, NULL);
  295. }
  296. - Failure inside a transaction (such as a lock function failing) does
  297. not implicitly cancel the transaction; you still need to call
  298. ntdb_transaction_cancel().
  299. #include <tdb.h>
  300. #include <ntdb.h>
  301. void tdb_example(struct tdb_context *tdb, TDB_DATA key, TDB_DATA d)
  302. {
  303. if (tdb_transaction_start(tdb) == -1) {
  304. printf("transaction failed: %s\n", tdb_errorstr(tdb));
  305. return;
  306. }
  307. if (tdb_store(tdb, key, d) == -1) {
  308. printf("store failed: %s\n", tdb_errorstr(tdb));
  309. return;
  310. }
  311. if (tdb_transaction_commit(tdb) == -1) {
  312. printf("commit failed: %s\n", tdb_errorstr(tdb));
  313. }
  314. }
  315. void ntdb_example(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA d)
  316. {
  317. enum NTDB_ERROR e;
  318. e = ntdb_transaction_start(ntdb);
  319. if (e) {
  320. printf("transaction failed: %s\n", ntdb_errorstr(e));
  321. return;
  322. }
  323. e = ntdb_store(ntdb, key, d);
  324. if (e) {
  325. printf("store failed: %s\n", ntdb_errorstr(e));
  326. ntdb_transaction_cancel(ntdb);
  327. }
  328. e = ntdb_transaction_commit(ntdb);
  329. if (e) {
  330. printf("commit failed: %s\n", ntdb_errorstr(e));
  331. }
  332. }
  333. - There is no NTDB_CLEAR_IF_FIRST flag; it has severe scalability and
  334. API problems. If necessary, you can emulate this by using the open
  335. hook and placing a 1-byte lock at offset 4. If your program forks
  336. and exits, you will need to place this lock again in the child before
  337. the parent exits.
  338. Example:
  339. #include <tdb.h>
  340. #include <ntdb.h>
  341. struct tdb_context *tdb_example(void)
  342. {
  343. return tdb_open("example.tdb", 0, TDB_CLEAR_IF_FIRST,
  344. O_CREAT|O_RDWR, 0600);
  345. }
  346. static enum NTDB_ERROR clear_if_first(int fd, void *unused)
  347. {
  348. /* We hold a lock offset 4 always, so we can tell if
  349. * anyone else is. */
  350. struct flock fl;
  351. fl.l_type = F_WRLCK;
  352. fl.l_whence = SEEK_SET;
  353. fl.l_start = 4; /* ACTIVE_LOCK */
  354. fl.l_len = 1;
  355. if (fcntl(fd, F_SETLK, &fl) == 0) {
  356. /* We must be first ones to open it! Clear it. */
  357. if (ftruncate(fd, 0) != 0) {
  358. return NTDB_ERR_IO;
  359. }
  360. }
  361. fl.l_type = F_RDLCK;
  362. if (fcntl(fd, F_SETLKW, &fl) != 0) {
  363. return NTDB_ERR_IO;
  364. }
  365. return NTDB_SUCCESS;
  366. }
  367. struct ntdb_context *ntdb_example(void)
  368. {
  369. union ntdb_attribute open_attr;
  370. open_attr.openhook.base.attr = NTDB_ATTRIBUTE_OPENHOOK;
  371. open_attr.openhook.base.next = NULL;
  372. open_attr.openhook.fn = clear_if_first;
  373. return ntdb_open("example.ntdb", NTDB_DEFAULT,
  374. O_CREAT|O_RDWR, 0600, &open_attr);
  375. }
  376. - ntdb traversals are not reliable if the database is changed during
  377. the traversal, ie your traversal may not cover all elements, or may
  378. cover elements multiple times. As a special exception, deleting the
  379. current record within ntdb_traverse() is reliable.
  380. - There is no ntdb_traverse_read, since ntdb_traverse does not hold
  381. a lock across the entire traversal anyway. If you want to make sure
  382. that your traversal function does not write to the database, you can
  383. set and clear the NTDB_RDONLY flag around the traversal.
  384. - ntdb does not need tdb_reopen() or tdb_reopen_all(). If you call
  385. fork() after during certain operations the child should close the
  386. ntdb, or complete the operations before continuing to use the tdb:
  387. ntdb_transaction_start(): child must ntdb_transaction_cancel()
  388. ntdb_lockall(): child must call ntdb_unlockall()
  389. ntdb_lockall_read(): child must call ntdb_unlockall_read()
  390. ntdb_chainlock(): child must call ntdb_chainunlock()
  391. ntdb_parse() callback: child must return from ntdb_parse()
  392. - ntdb will not open a non-ntdb file, even if O_CREAT is specified. tdb
  393. will overwrite an unknown file in that case.