summaryrefslogtreecommitdiff
path: root/lib/ntdb/doc/TDB_porting.txt
blob: 8b0ca2fec81fdf0df456b17b3f6a87cfd92f4751 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
Interface differences between TDB and NTDB.

- ntdb shares 'struct TDB_DATA' with tdb, but TDB defines the TDB_DATA
  typedef, whereas ntdb defines NTDB_DATA (ie. both are compatible).
  If you include both ntdb.h and tdb.h, #include tdb.h first,
  otherwise you'll get a compile error when tdb.h re-defined struct
  TDB_DATA.

  Example:
	#include <tdb.h>
	#include <ntdb.h>

- ntdb functions return NTDB_SUCCESS (ie 0) on success, and a negative
  error on failure, whereas tdb functions returned 0 on success, and
  -1 on failure.  tdb then used tdb_error() to determine the error;
  this API is nasty if we ever want to support threads, so is not supported.

  Example:
	#include <tdb.h>
	#include <ntdb.h>

	void tdb_example(struct tdb_context *tdb, TDB_DATA key, TDB_DATA d)
	{
		if (tdb_store(tdb, key, d) == -1) {
			printf("store failed: %s\n", tdb_errorstr(tdb));
		}
	}

	void ntdb_example(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA d)
	{
		enum NTDB_ERROR e;

		e = ntdb_store(ntdb, key, d);
		if (e) {
			printf("store failed: %s\n", ntdb_errorstr(e));
		}
	}

- ntdb's ntdb_fetch() returns an error, tdb's returned the data directly
  (or tdb_null, and you were supposed to check tdb_error() to find out why).

  Example:
	#include <tdb.h>
	#include <ntdb.h>

	void tdb_example(struct tdb_context *tdb, TDB_DATA key)
	{
		TDB_DATA data;

		data = tdb_fetch(tdb, key);
		if (!data.dptr) {
			printf("fetch failed: %s\n", tdb_errorstr(tdb));
		}
	}

	void ntdb_example(struct ntdb_context *ntdb, NTDB_DATA key)
	{
		NTDB_DATA data;
		enum NTDB_ERROR e;

		e = ntdb_fetch(ntdb, key, &data);
		if (e) {
			printf("fetch failed: %s\n", ntdb_errorstr(e));
		}
	}

- ntdb's ntdb_nextkey() frees the old key's dptr, in tdb you needed to do
  this manually.

  Example:
	#include <tdb.h>
	#include <ntdb.h>

	void tdb_example(struct tdb_context *tdb)
	{
		TDB_DATA key, next, data;

		for (key = tdb_firstkey(tdb); key.dptr; key = next) {
			printf("Got key!\n");
			next = tdb_nextkey(tdb, key);
			free(key.dptr);
		}
	}


	void ntdb_example(struct ntdb_context *ntdb)
	{
		NTDB_DATA k, data;
		enum NTDB_ERROR e;

		for (e = ntdb_firstkey(ntdb,&k); !e; e = ntdb_nextkey(ntdb,&k))
			printf("Got key!\n");
	}

- Unlike tdb_open/tdb_open_ex, ntdb_open does not allow NULL names,
  even for NTDB_INTERNAL dbs, and thus ntdb_name() never returns NULL.

  Example:
	#include <tdb.h>
	#include <ntdb.h>

	struct tdb_context *tdb_example(void)
	{
		return tdb_open(NULL, 0, TDB_INTERNAL, O_RDWR, 0);
	}

	struct ntdb_context *ntdb_example(void)
	{
		return ntdb_open("example", NTDB_INTERNAL, O_RDWR, 0);
	}

- ntdb uses a linked list of attribute structures to implement logging and
  alternate hashes.  tdb used tdb_open_ex, which was not extensible.

  Example:
	#include <tdb.h>
	#include <ntdb.h>

	/* Custom hash function */
	static unsigned int my_tdb_hash_func(TDB_DATA *key)
	{
		return key->dsize;
	}

	struct tdb_context *tdb_example(void)
	{
		return tdb_open_ex("example.tdb", 0, TDB_DEFAULT,
		                   O_CREAT|O_RDWR, 0600, NULL, my_hash_func);
	}

	/* Custom hash function */
	static unsigned int my_ntdb_hash_func(const void *key, size_t len,
					      uint32_t seed, void *data)
	{
		return len;
	}

	struct ntdb_context *ntdb_example(void)
	{
		union ntdb_attribute hash;

		hash.base.attr = NTDB_ATTRIBUTE_HASH;
		hash.base.next = NULL;
		hash.hash.fn = my_ntdb_hash_func;
		return ntdb_open("example.ntdb", NTDB_DEFAULT,
		                   O_CREAT|O_RDWR, 0600, &hash);
	}

- tdb's tdb_open/tdb_open_ex took an explicit hash size, defaulting to
  131.  ntdb's uses an attribute for this, defaulting to 8192.

  Example:
	#include <tdb.h>
	#include <ntdb.h>

	struct tdb_context *tdb_example(void)
	{
		return tdb_open("example.tdb", 10007, TDB_DEFAULT,
		                O_CREAT|O_RDWR, 0600);
	}

	struct ntdb_context *ntdb_example(void)
	{
		union ntdb_attribute hashsize;

		hashsize.base.attr = NTDB_ATTRIBUTE_HASHSIZE;
		hashsize.base.next = NULL;
		hashsize.hashsize.size = 16384;
		return ntdb_open("example.ntdb", NTDB_DEFAULT,
		                   O_CREAT|O_RDWR, 0600, &hashsize);
	}

- ntdb does locking on read-only databases (ie. O_RDONLY passed to ntdb_open).
  tdb did not: use the NTDB_NOLOCK flag if you want to suppress locking.

  Example:
	#include <tdb.h>
	#include <ntdb.h>

	struct tdb_context *tdb_example(void)
	{
		return tdb_open("example.tdb", 0, TDB_DEFAULT, O_RDONLY, 0);
	}

	struct ntdb_context *ntdb_example(void)
	{
		return ntdb_open("example.ntdb", NTDB_NOLOCK, O_RDONLY, NULL);
	}

- ntdb's log function is simpler than tdb's log function.  The string
  is already formatted, is not terminated by a '\n', and it takes an
  enum ntdb_log_level not a tdb_debug_level, and which has only three
  values: NTDB_LOG_ERROR, NTDB_LOG_USE_ERROR and NTDB_LOG_WARNING.

	#include <tdb.h>
	#include <ntdb.h>

	static void tdb_log(struct tdb_context *tdb,
	                    enum tdb_debug_level level, const char *fmt, ...)
	{
		va_list ap;
		const char *name;

		switch (level) {
		case TDB_DEBUG_FATAL:
			fprintf(stderr, "FATAL: ");
			break;
		case TDB_DEBUG_ERROR:
			fprintf(stderr, "ERROR: ");
			break;
		case TDB_DEBUG_WARNING:
			fprintf(stderr, "WARNING: ");
			break;
		case TDB_DEBUG_TRACE:
			/* Don't print out tracing. */
			return;
		}

		name = tdb_name(tdb);
		if (!name) {
			name = "unnamed";
		}

		fprintf(stderr, "tdb(%s):", name);

		va_start(ap, fmt);
		vfprintf(stderr, fmt, ap);
		va_end(ap);
	}

	struct tdb_context *tdb_example(void)
	{
		struct tdb_logging_context lctx;

		lctx.log_fn = tdb_log;
		return tdb_open_ex("example.tdb", 0, TDB_DEFAULT,
		                   O_CREAT|O_RDWR, 0600, &lctx, NULL);
	}

	static void ntdb_log(struct ntdb_context *ntdb,
			     enum ntdb_log_level level,
			     enum NTDB_ERROR ecode,
			     const char *message,
			     void *data)
	{
		switch (level) {
		case NTDB_LOG_ERROR:
			fprintf(stderr, "ERROR: ");
			break;
		case NTDB_LOG_USE_ERROR:
			/* We made a mistake, so abort. */
			abort();
			break;
		case NTDB_LOG_WARNING:
			fprintf(stderr, "WARNING: ");
			break;
		}

		fprintf(stderr, "ntdb(%s):%s:%s\n",
			ntdb_name(ntdb), ntdb_errorstr(ecode), message);
	}

	struct ntdb_context *ntdb_example(void)
	{
		union ntdb_attribute log;

		log.base.attr = NTDB_ATTRIBUTE_LOG;
		log.base.next = NULL;
		log.log.fn = ntdb_log;
		return ntdb_open("example.ntdb", NTDB_DEFAULT,
		                 O_CREAT|O_RDWR, 0600, &log);
	}

- ntdb provides ntdb_deq() for comparing two NTDB_DATA, and ntdb_mkdata() for
  creating an NTDB_DATA.

	#include <tdb.h>
	#include <ntdb.h>

	void tdb_example(struct tdb_context *tdb)
	{
		TDB_DATA data, key;

		key.dsize = strlen("hello");
		key.dptr = "hello";
		data = tdb_fetch(tdb, key);
		if (data.dsize == key.dsize
		    && !memcmp(data.dptr, key.dptr, key.dsize))
			printf("key is same as data\n");
		}
		free(data.dptr);
	}

	void ntdb_example(struct ntdb_context *ntdb)
	{
		NTDB_DATA data, key;

		key = ntdb_mkdata("hello", strlen("hello"));
		if (ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS) {
			if (ntdb_deq(key, data)) {
				printf("key is same as data\n");
			}
			free(data.dptr);
		}
	}

- Failure inside a transaction (such as a lock function failing) does
  not implicitly cancel the transaction; you still need to call
  ntdb_transaction_cancel().

	#include <tdb.h>
	#include <ntdb.h>

	void tdb_example(struct tdb_context *tdb, TDB_DATA key, TDB_DATA d)
	{
		if (tdb_transaction_start(tdb) == -1) {
			printf("transaction failed: %s\n", tdb_errorstr(tdb));
			return;
		}

		if (tdb_store(tdb, key, d) == -1) {
			printf("store failed: %s\n", tdb_errorstr(tdb));
			return;
		}
		if (tdb_transaction_commit(tdb) == -1) {
			printf("commit failed: %s\n", tdb_errorstr(tdb));
		}
	}

	void ntdb_example(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA d)
	{
		enum NTDB_ERROR e;

		e = ntdb_transaction_start(ntdb);
		if (e) {
			printf("transaction failed: %s\n", ntdb_errorstr(e));
			return;
		}

		e = ntdb_store(ntdb, key, d);
		if (e) {
			printf("store failed: %s\n", ntdb_errorstr(e));
			ntdb_transaction_cancel(ntdb);
		}

		e = ntdb_transaction_commit(ntdb);
		if (e) {
			printf("commit failed: %s\n", ntdb_errorstr(e));
		}
	}

- There is no NTDB_CLEAR_IF_FIRST flag; it has severe scalability and
  API problems.  If necessary, you can emulate this by using the open
  hook and placing a 1-byte lock at offset 4.  If your program forks
  and exits, you will need to place this lock again in the child before
  the parent exits.

  Example:

	#include <tdb.h>
	#include <ntdb.h>

	struct tdb_context *tdb_example(void)
	{
		return tdb_open("example.tdb", 0, TDB_CLEAR_IF_FIRST,
		                   O_CREAT|O_RDWR, 0600);
	}

	static enum NTDB_ERROR clear_if_first(int fd, void *unused)
	{
		/* We hold a lock offset 4 always, so we can tell if
		 * anyone else is. */
		struct flock fl;

		fl.l_type = F_WRLCK;
		fl.l_whence = SEEK_SET;
		fl.l_start = 4; /* ACTIVE_LOCK */
		fl.l_len = 1;

		if (fcntl(fd, F_SETLK, &fl) == 0) {
			/* We must be first ones to open it!  Clear it. */
			if (ftruncate(fd, 0) != 0) {
				return NTDB_ERR_IO;
			}
		}
		fl.l_type = F_RDLCK;
		if (fcntl(fd, F_SETLKW, &fl) != 0) {
			return NTDB_ERR_IO;
		}
		return NTDB_SUCCESS;
	}

	struct ntdb_context *ntdb_example(void)
	{
		union ntdb_attribute open_attr;

		open_attr.openhook.base.attr = NTDB_ATTRIBUTE_OPENHOOK;
		open_attr.openhook.base.next = NULL;
		open_attr.openhook.fn = clear_if_first;

		return ntdb_open("example.ntdb", NTDB_DEFAULT,
		                 O_CREAT|O_RDWR, 0600, &open_attr);
	}

- ntdb traversals are not reliable if the database is changed during
  the traversal, ie your traversal may not cover all elements, or may
  cover elements multiple times.  As a special exception, deleting the
  current record within ntdb_traverse() is reliable.

- There is no ntdb_traverse_read, since ntdb_traverse does not hold
  a lock across the entire traversal anyway.  If you want to make sure
  that your traversal function does not write to the database, you can
  set and clear the NTDB_RDONLY flag around the traversal.

- ntdb does not need tdb_reopen() or tdb_reopen_all().  If you call
  fork() after during certain operations the child should close the
  ntdb, or complete the operations before continuing to use the tdb:

	ntdb_transaction_start(): child must ntdb_transaction_cancel()
	ntdb_lockall(): child must call ntdb_unlockall()
	ntdb_lockall_read(): child must call ntdb_unlockall_read()
	ntdb_chainlock(): child must call ntdb_chainunlock()
	ntdb_parse() callback: child must return from ntdb_parse()

- ntdb will not open a non-ntdb file, even if O_CREAT is specified.  tdb
  will overwrite an unknown file in that case.