]> code.citadel.org Git - citadel.git/blob - citadel/database_sleepycat.c
* Build ok on Berkeley DB 4.1
[citadel.git] / citadel / database_sleepycat.c
1 /*
2  * $Id$
3  *
4  * Sleepycat (Berkeley) DB driver for Citadel/UX
5  *
6  */
7
8 /*****************************************************************************
9        Tunable configuration parameters for the Sleepycat DB back end
10  *****************************************************************************/
11
12 /* Citadel will checkpoint the db at the end of every session, but only if
13  * the specified number of kilobytes has been written, or if the specified
14  * number of minutes has passed, since the last checkpoint.
15  */
16 #define MAX_CHECKPOINT_KBYTES   256
17 #define MAX_CHECKPOINT_MINUTES  15
18
19 /*****************************************************************************/
20
21 #ifdef DLL_EXPORT
22 #define IN_LIBCIT
23 #endif
24
25 #include "sysdep.h"
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <dirent.h>
35
36 #ifdef HAVE_DB_H
37 #include <db.h>
38 #elif defined(HAVE_DB4_DB_H)
39 #include <db4/db.h>
40 #elif defined(HAVE_DB3_DB_H)
41 #include <db3/db.h>
42 #else
43 #error Neither <db.h> nor <db3/db.h> was found by configure. Install db3-devel.
44 #endif
45
46 #include <pthread.h>
47 #include "citadel.h"
48 #include "server.h"
49 #include "dynloader.h"
50 #include "citserver.h"
51 #include "database.h"
52 #include "msgbase.h"
53 #include "sysdep_decls.h"
54
55 static DB *dbp[MAXCDB];         /* One DB handle for each Citadel database */
56 static DB_ENV *dbenv;           /* The DB environment (global) */
57
58 struct cdbtsd {                 /* Thread-specific DB stuff */
59         DB_TXN *tid;            /* Transaction handle */
60         DBC *cursors[MAXCDB];   /* Cursors, for traversals... */
61 };
62
63 #ifdef HAVE_ZLIB
64 #include <zlib.h>
65 #endif
66
67 static pthread_key_t tsdkey;
68
69 #define MYCURSORS       (((struct cdbtsd*)pthread_getspecific(tsdkey))->cursors)
70 #define MYTID           (((struct cdbtsd*)pthread_getspecific(tsdkey))->tid)
71
72 /* just a little helper function */
73 static void txabort(DB_TXN *tid) {
74         int ret = txn_abort(tid);
75
76         if (ret) {
77                 lprintf(1, "cdb_*: txn_abort: %s\n", db_strerror(ret));
78                 abort();
79         }
80 }
81
82 /* this one is even more helpful than the last. */
83 static void txcommit(DB_TXN *tid) {
84         int ret = txn_commit(tid, 0);
85
86         if (ret) {
87                 lprintf(1, "cdb_*: txn_commit: %s\n", db_strerror(ret));
88                 abort();
89         }
90 }
91
92 /* are you sensing a pattern yet? */
93 static void txbegin(DB_TXN **tid) {
94         int ret = txn_begin(dbenv, NULL, tid, 0);
95
96         if (ret) {
97                 lprintf(1, "cdb_*: txn_begin: %s\n", db_strerror(ret));
98                 abort();
99         }
100 }
101
102 static void cclose(DBC *cursor) {
103         int ret;
104
105         if ((ret = cursor->c_close(cursor))) {
106                 lprintf(1, "cdb_*: c_close: %s\n", db_strerror(ret));
107                 abort();
108         }
109 }
110
111 static void bailIfCursor(DBC **cursors, const char *msg)
112 {
113   int i;
114
115   for (i = 0; i < MAXCDB; i++)
116     if (cursors[i] != NULL)
117       {
118         lprintf(1, "cdb_*: cursor still in progress on cdb %d: %s\n", i, msg);
119         abort();
120       }
121 }
122
123 static void check_handles(void *arg) {
124         if (arg != NULL) {
125                 struct cdbtsd *tsd = (struct cdbtsd *)arg;
126
127                 bailIfCursor(tsd->cursors, "in check_handles");
128
129                 if (tsd->tid != NULL) {
130                         lprintf(1, "cdb_*: transaction still in progress!");
131                         abort();
132                 }
133         }
134 }
135
136 static void dest_tsd(void *arg) {
137         if (arg != NULL) {
138                 check_handles(arg);
139                 phree(arg);
140         }
141 }
142
143 /*
144  * Ensure that we have a key for thread-specific data.  We don't
145  * put anything in here that Citadel cares about; this is just database
146  * related stuff like cursors and transactions.
147  *
148  * This should be called immediately after startup by any thread which wants
149  * to use database calls, except for whatever thread calls open_databases.
150  */
151 void cdb_allocate_tsd(void) {
152         struct cdbtsd *tsd;
153
154         if (pthread_getspecific(tsdkey) != NULL)
155                 return;
156
157         tsd = mallok(sizeof(struct cdbtsd));
158
159         tsd->tid = NULL;
160
161         memset(tsd->cursors, 0, sizeof tsd->cursors);
162         pthread_setspecific(tsdkey, tsd);
163 }
164
165 void cdb_free_tsd(void) {
166         dest_tsd(pthread_getspecific(tsdkey));
167         pthread_setspecific(tsdkey, NULL);
168 }
169
170 void cdb_check_handles(void) {
171         check_handles(pthread_getspecific(tsdkey));
172 }
173
174
175 /*
176  * Reclaim unused space in the databases.  We need to do each one of
177  * these discretely, rather than in a loop.
178  *
179  * This is a stub function in the Sleepycat DB backend, because there is no
180  * such API call available.
181  */
182 void defrag_databases(void)
183 {
184         /* do nothing */
185 }
186
187
188 /*
189  * Cull the database logs
190  */
191 static void cdb_cull_logs(void) {
192         u_int32_t flags;
193         int ret;
194         char **file, **list;
195         char errmsg[SIZ];
196
197         lprintf(5, "Database log file cull started.\n");
198
199         flags = DB_ARCH_ABS;
200
201         /* Get the list of names. */
202 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 3
203         if ((ret = log_archive(dbenv, &list, flags, NULL)) != 0) {
204 #elif DB_VERSION_MAJOR >= 4
205         if ((ret = dbenv->log_archive(dbenv, &list, flags)) != 0) {
206 #else
207         if ((ret = log_archive(dbenv, &list, flags)) != 0) {
208 #endif
209                 lprintf(1, "cdb_cull_logs: %s\n", db_strerror(ret));
210                 return;
211         }
212
213         /* Print the list of names. */
214         if (list != NULL) {
215                 for (file = list; *file != NULL; ++file) {
216                         lprintf(9, "Deleting log: %s\n", *file);
217                         ret = unlink(*file);
218                         if (ret != 0) {
219                                 snprintf(errmsg, sizeof(errmsg),
220                                         " ** ERROR **\n \n \n "
221                                         "Citadel was unable to delete the "
222                                         "database log file '%s' because of the "
223                                         "following error:\n \n %s\n \n"
224                                         " This log file is no longer in use "
225                                         "and may be safely deleted.\n",
226                                         *file,
227                                         strerror(errno));
228                                 aide_message(errmsg);
229                         }
230                 }
231                 free(list);
232         }
233
234         lprintf(5, "Database log file cull ended.\n");
235 }
236
237
238 /*
239  * Request a checkpoint of the database.
240  */
241 static void cdb_checkpoint(void) {
242         int ret;
243         static time_t last_cull = 0L;
244
245 #if DB_VERSION_MAJOR >= 4
246         ret = dbenv->txn_checkpoint(dbenv,
247 #else
248         ret = txn_checkpoint(dbenv,
249 #endif
250                                 MAX_CHECKPOINT_KBYTES,
251                                 MAX_CHECKPOINT_MINUTES,
252                                 0);
253
254 /* The DB_INCOMPLETE error is no longer possible (or even defined) as of
255  * Berkeley DB v4.1.  When we get to the point where v4.0 and earlier are no
256  * longer supported, we can remove this ifdef.
257  */
258 #ifdef DB_INCOMPLETE
259         if ( (ret != 0) && (ret != DB_INCOMPLETE) ) {
260                 lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n",
261                         db_strerror(ret));
262                 abort();
263         }
264         if (ret == DB_INCOMPLETE) {
265                 lprintf(3, "WARNING: txn_checkpoint: %s\n", db_strerror(ret));
266         }
267 #else /* DB_INCOMPLETE */
268         if (ret != 0) {
269                 lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n",
270                         db_strerror(ret));
271                 abort();
272         }
273 #endif /* DB_INCOMPLETE */
274
275         /* Cull the logs if we haven't done so for 24 hours */
276         if ((time(NULL) - last_cull) > 86400L) {
277                 last_cull = time(NULL);
278                 cdb_cull_logs();
279         }
280
281 }
282
283 /*
284  * Open the various databases we'll be using.  Any database which
285  * does not exist should be created.  Note that we don't need an S_DATABASE
286  * critical section here, because there aren't any active threads manipulating
287  * the database yet -- and besides, it causes problems on BSDI.
288  */
289 void open_databases(void)
290 {
291         int ret;
292         int i;
293         char dbfilename[SIZ];
294         u_int32_t flags = 0;
295
296         lprintf(9, "cdb_*: open_databases() starting\n");
297 #ifdef HAVE_ZLIB
298         lprintf(5, "zlib compression version %s\n", zlibVersion());
299 #endif
300
301         /*
302          * Silently try to create the database subdirectory.  If it's
303          * already there, no problem.
304          */
305         system("exec mkdir data 2>/dev/null");
306
307         lprintf(9, "cdb_*: Setting up DB environment\n");
308         db_env_set_func_yield(sched_yield);
309         ret = db_env_create(&dbenv, 0);
310         if (ret) {
311                 lprintf(1, "cdb_*: db_env_create: %s\n", db_strerror(ret));
312                 exit(ret);
313         }
314         dbenv->set_errpfx(dbenv, "citserver");
315
316         /*
317          * We want to specify the shared memory buffer pool cachesize,
318          * but everything else is the default.
319          */
320         ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
321         if (ret) {
322                 lprintf(1, "cdb_*: set_cachesize: %s\n", db_strerror(ret));
323                 dbenv->close(dbenv, 0);
324                 exit(ret);
325         }
326
327         if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
328                 lprintf(1, "cdb_*: set_lk_detect: %s\n", db_strerror(ret));
329                 dbenv->close(dbenv, 0);
330                 exit(ret);
331         }
332
333         flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_TXN|
334                 DB_INIT_LOCK|DB_THREAD;
335         ret = dbenv->open(dbenv, "./data", flags, 0);
336         if (ret) {
337                 lprintf(1, "cdb_*: dbenv->open: %s\n", db_strerror(ret));
338                 dbenv->close(dbenv, 0);
339                 exit(ret);
340         }
341
342         lprintf(7, "cdb_*: Starting up DB\n");
343
344         for (i = 0; i < MAXCDB; ++i) {
345
346                 /* Create a database handle */
347                 ret = db_create(&dbp[i], dbenv, 0);
348                 if (ret) {
349                         lprintf(1, "cdb_*: db_create: %s\n", db_strerror(ret));
350                         exit(ret);
351                 }
352
353
354                 /* Arbitrary names for our tables -- we reference them by
355                  * number, so we don't have string names for them.
356                  */
357                 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i);
358
359                 ret = dbp[i]->open(dbp[i],
360 #if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1
361                                 NULL,                   /* new parameter */
362 #endif
363                                 dbfilename,
364                                 NULL,
365                                 DB_BTREE,
366                                 DB_CREATE|DB_THREAD,
367                                 0600);
368                 if (ret) {
369                         lprintf(1, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
370                         exit(ret);
371                 }
372         }
373
374         if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
375                 lprintf(1, "cdb_*: pthread_key_create: %s\n", strerror(ret));
376                 exit(1);
377         }
378
379         cdb_allocate_tsd();
380         CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
381         lprintf(9, "cdb_*: open_databases() finished\n");
382 }
383
384
385 /*
386  * Close all of the db database files we've opened.  This can be done
387  * in a loop, since it's just a bunch of closes.
388  */
389 void close_databases(void)
390 {
391         int a;
392         int ret;
393
394         cdb_free_tsd();
395
396 #if DB_VERSION_MAJOR >= 4
397         if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
398 #else
399         if ((ret = txn_checkpoint(dbenv, 0, 0, 0))) {
400 #endif
401                 lprintf(1, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
402                 abort();
403         }
404
405         for (a = 0; a < MAXCDB; ++a) {
406                 lprintf(7, "cdb_*: Closing database %d\n", a);
407                 ret = dbp[a]->close(dbp[a], 0);
408                 if (ret) {
409                         lprintf(1, "cdb_*: db_close: %s\n", db_strerror(ret));
410                         abort();
411                 }
412                 
413         }
414
415         /* Close the handle. */
416         ret = dbenv->close(dbenv, 0);
417         if (ret) {
418                 lprintf(1, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
419                 abort();
420         }
421 }
422
423
424 /*
425  * Compression functions only used if we have zlib
426  */
427 #ifdef HAVE_ZLIB
428
429 void cdb_decompress_if_necessary(struct cdbdata *cdb) {
430         static int magic = COMPRESS_MAGIC;
431         struct CtdlCompressHeader zheader;
432         char *uncompressed_data;
433         char *compressed_data;
434         uLongf destLen, sourceLen;
435
436         if (cdb == NULL) return;
437         if (cdb->ptr == NULL) return;
438         if (memcmp(cdb->ptr, &magic, sizeof(magic))) return;
439
440         /* At this point we know we're looking at a compressed item. */
441         memcpy(&zheader, cdb->ptr, sizeof(struct CtdlCompressHeader));
442
443         compressed_data = cdb->ptr;
444         compressed_data += sizeof(struct CtdlCompressHeader);
445
446         sourceLen = (uLongf) zheader.compressed_len;
447         destLen = (uLongf) zheader.uncompressed_len;
448         uncompressed_data = mallok(zheader.uncompressed_len);
449
450         if (uncompress( (Bytef *) uncompressed_data,
451                         &destLen,
452                         compressed_data,
453                         sourceLen
454         ) != Z_OK) {
455                 lprintf(1, "uncompress() error\n");
456                 abort();
457         }
458
459         phree(cdb->ptr);
460         cdb->len = (size_t) destLen;
461         cdb->ptr = uncompressed_data;
462 }
463
464 #endif  /* HAVE_ZLIB */
465         
466
467 /*
468  * Store a piece of data.  Returns 0 if the operation was successful.  If a
469  * key already exists it should be overwritten.
470  */
471 int cdb_store(int cdb,
472               void *ckey, int ckeylen,
473               void *cdata, int cdatalen)
474 {
475
476   DBT dkey, ddata;
477   DB_TXN *tid;
478   int ret;
479
480 #ifdef HAVE_ZLIB
481         struct CtdlCompressHeader zheader;
482         char *compressed_data = NULL;
483         int compressing = 0;
484         size_t buffer_len;
485         uLongf destLen;
486 #endif
487   
488   memset(&dkey, 0, sizeof(DBT));
489   memset(&ddata, 0, sizeof(DBT));
490   dkey.size = ckeylen;
491   dkey.data = ckey;
492   ddata.size = cdatalen;
493   ddata.data = cdata;
494
495 #ifdef HAVE_ZLIB
496         /* Only compress Visit records.  Everything else is uncompressed. */
497         if (cdb == CDB_VISIT) {
498                 compressing = 1;
499                 zheader.magic = COMPRESS_MAGIC;
500                 zheader.uncompressed_len = cdatalen;
501                 buffer_len = ( (cdatalen * 101) / 100 ) + 100
502                                 + sizeof(struct CtdlCompressHeader) ;
503                 destLen = (uLongf) buffer_len;
504                 compressed_data = mallok(buffer_len);
505                 if (compress2(
506                         (Bytef *) (compressed_data +
507                                         sizeof(struct CtdlCompressHeader)),
508                         &destLen,
509                         (Bytef *) cdata,
510                         (uLongf) cdatalen,
511                         1
512                 ) != Z_OK) {
513                         lprintf(1, "compress2() error\n");
514                         abort();
515                 }
516                 zheader.compressed_len = (size_t) destLen;
517                 memcpy(compressed_data, &zheader,
518                         sizeof(struct CtdlCompressHeader));
519                 ddata.size = (size_t)  (sizeof(struct CtdlCompressHeader) +
520                                                 zheader.compressed_len);
521                 ddata.data = compressed_data;
522         }
523 #endif
524   
525   if (MYTID != NULL)
526     {
527       ret = dbp[cdb]->put(dbp[cdb],             /* db */
528                           MYTID,                /* transaction ID */
529                           &dkey,                /* key */
530                           &ddata,               /* data */
531                           0);           /* flags */
532       if (ret)
533         {
534           lprintf(1, "cdb_store(%d): %s\n", cdb,
535                   db_strerror(ret));
536           abort();
537         }
538 #ifdef HAVE_ZLIB
539       if (compressing) phree(compressed_data);
540 #endif
541       return ret;
542       
543     }
544   else
545     {
546       bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
547       
548     retry:
549       txbegin(&tid);
550       
551       if ((ret = dbp[cdb]->put(dbp[cdb],    /* db */
552                                tid,         /* transaction ID */
553                                &dkey,       /* key */
554                                &ddata,      /* data */
555                                0)))         /* flags */
556         {
557           if (ret == DB_LOCK_DEADLOCK)
558             {
559               txabort(tid);
560               goto retry;
561             }
562           else
563             {
564               lprintf(1, "cdb_store(%d): %s\n", cdb,
565                       db_strerror(ret));
566               abort();
567             }
568         }
569       else
570         {
571           txcommit(tid);
572 #ifdef HAVE_ZLIB
573           if (compressing) phree(compressed_data);
574 #endif
575           return ret;
576         }
577     }
578 }
579
580
581 /*
582  * Delete a piece of data.  Returns 0 if the operation was successful.
583  */
584 int cdb_delete(int cdb, void *key, int keylen)
585 {
586
587   DBT dkey;
588   DB_TXN *tid;
589   int ret;
590
591   memset(&dkey, 0, sizeof dkey);
592   dkey.size = keylen;
593   dkey.data = key;
594
595   if (MYTID != NULL)
596     {
597       ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
598       if (ret)
599         {
600           lprintf(1, "cdb_delete(%d): %s\n", cdb,
601                   db_strerror(ret));
602           if (ret != DB_NOTFOUND)
603             abort();
604         }
605     }
606   else
607     {
608       bailIfCursor(MYCURSORS, "attempt to delete during r/o cursor");
609     
610     retry:
611       txbegin(&tid);
612     
613       if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))
614           && ret != DB_NOTFOUND)
615         {
616           if (ret == DB_LOCK_DEADLOCK)
617             {
618               txabort(tid);
619               goto retry;
620             }
621           else
622             {
623               lprintf(1, "cdb_delete(%d): %s\n", cdb,
624                       db_strerror(ret));
625               abort();
626             }
627         }
628       else
629         {
630           txcommit(tid);
631         }
632     }
633   return ret;
634 }
635
636 static DBC *localcursor(int cdb)
637 {
638   int ret;
639   DBC *curs;
640
641   if (MYCURSORS[cdb] == NULL)
642     ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &curs, 0);
643   else
644     ret = MYCURSORS[cdb]->c_dup(MYCURSORS[cdb], &curs, DB_POSITION);
645
646   if (ret)
647     {
648       lprintf(1, "localcursor: %s\n", db_strerror(ret));
649       abort();
650     }
651
652   return curs;
653 }
654
655
656 /*
657  * Fetch a piece of data.  If not found, returns NULL.  Otherwise, it returns
658  * a struct cdbdata which it is the caller's responsibility to free later on
659  * using the cdb_free() routine.
660  */
661 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
662 {
663
664   struct cdbdata *tempcdb;
665   DBT dkey, dret;
666   int ret;
667
668   memset(&dkey, 0, sizeof(DBT));
669   dkey.size = keylen;
670   dkey.data = key;
671
672   if (MYTID != NULL)
673     {
674       memset(&dret, 0, sizeof(DBT));
675       dret.flags = DB_DBT_MALLOC;
676       ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
677     }
678   else
679     {
680       DBC *curs;
681
682       do
683         {
684           memset(&dret, 0, sizeof(DBT));
685           dret.flags = DB_DBT_MALLOC;
686
687           curs = localcursor(cdb);
688
689           ret = curs->c_get(curs, &dkey, &dret, DB_SET);
690           cclose(curs);
691         }
692       while (ret == DB_LOCK_DEADLOCK);
693
694     }
695
696   if ((ret != 0) && (ret != DB_NOTFOUND))
697     {
698       lprintf(1, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret));
699       abort();
700     }
701
702   if (ret != 0) return NULL;
703   tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
704
705   if (tempcdb == NULL)
706     {
707       lprintf(2, "cdb_fetch: Cannot allocate memory for tempcdb\n");
708       abort();
709     }
710
711   tempcdb->len = dret.size;
712   tempcdb->ptr = dret.data;
713 #ifdef HAVE_ZLIB
714   cdb_decompress_if_necessary(tempcdb);
715 #endif
716   return (tempcdb);
717 }
718
719
720 /*
721  * Free a cdbdata item (ok, this is really no big deal, but we might need to do
722  * more complex stuff with other database managers in the future).
723  */
724 void cdb_free(struct cdbdata *cdb)
725 {
726         phree(cdb->ptr);
727         phree(cdb);
728 }
729
730 void cdb_close_cursor(int cdb)
731 {
732         if (MYCURSORS[cdb] != NULL)
733                 cclose(MYCURSORS[cdb]);
734
735         MYCURSORS[cdb] = NULL;
736 }
737
738 /* 
739  * Prepare for a sequential search of an entire database.
740  * (There is guaranteed to be no more than one traversal in
741  * progress per thread at any given time.)
742  */
743 void cdb_rewind(int cdb)
744 {
745         int ret = 0;
746
747         if (MYCURSORS[cdb] != NULL)
748                 cclose(MYCURSORS[cdb]);
749
750         /*
751          * Now initialize the cursor
752          */
753         ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSORS[cdb], 0);
754         if (ret) {
755                 lprintf(1, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
756                 abort();
757         }
758 }
759
760
761 /*
762  * Fetch the next item in a sequential search.  Returns a pointer to a 
763  * cdbdata structure, or NULL if we've hit the end.
764  */
765 struct cdbdata *cdb_next_item(int cdb)
766 {
767         DBT key, data;
768         struct cdbdata *cdbret;
769         int ret = 0;
770
771         /* Initialize the key/data pair so the flags aren't set. */
772         memset(&key, 0, sizeof(key));
773         memset(&data, 0, sizeof(data));
774         data.flags = DB_DBT_MALLOC;
775
776         ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb],
777                 &key, &data, DB_NEXT);
778         
779         if (ret) {
780                 if (ret != DB_NOTFOUND) {
781                         lprintf(1, "cdb_next_item(%d): %s\n",
782                                 cdb, db_strerror(ret));
783                         abort();
784                 }
785                 cclose(MYCURSORS[cdb]);
786                 MYCURSORS[cdb] = NULL;
787                 return NULL;            /* presumably, end of file */
788         }
789
790         cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
791         cdbret->len = data.size;
792         cdbret->ptr = data.data;
793 #ifdef HAVE_ZLIB
794         cdb_decompress_if_necessary(cdbret);
795 #endif
796
797         return (cdbret);
798 }
799
800
801
802 /*
803  * Transaction-based stuff.  I'm writing this as I bake cookies...
804  */
805
806 void cdb_begin_transaction(void) {
807
808   bailIfCursor(MYCURSORS, "can't begin transaction during r/o cursor");
809
810   if (MYTID != NULL)
811     {
812       lprintf(1, "cdb_begin_transaction: ERROR: nested transaction\n");
813       abort();
814     }
815
816   txbegin(&MYTID);
817 }
818
819 void cdb_end_transaction(void) {
820   int i;
821
822   for (i = 0; i < MAXCDB; i++)
823     if (MYCURSORS[i] != NULL) {
824       lprintf(1, "cdb_end_transaction: WARNING: cursor %d still open at transaction end\n", i);
825       cclose(MYCURSORS[i]);
826       MYCURSORS[i] = NULL;
827     }
828
829   if (MYTID == NULL)
830     {
831       lprintf(1, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");
832       abort();
833     }
834   else
835     txcommit(MYTID);
836
837   MYTID = NULL;
838 }
839
840 /*
841  * Truncate (delete every record)
842  */
843 void cdb_trunc(int cdb)
844 {
845   DB_TXN *tid;
846   int ret;
847 #if DB_VERSION_MAJOR > 3 || DB_VERSION_MINOR > 2
848   u_int32_t count;
849 #endif
850   
851   if (MYTID != NULL)
852     {
853       lprintf(1, "cdb_trunc must not be called in a transaction.\n");
854       abort();
855     }
856   else
857     {
858       bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
859       
860 #if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 3
861       for (;;)
862         {
863           DBT key, data;
864
865           /* Initialize the key/data pair so the flags aren't set. */
866           memset(&key, 0, sizeof(key));
867           memset(&data, 0, sizeof(data));
868
869           txbegin(&tid);
870           
871           ret = dbp[cdb]->cursor(dbp[cdb], tid, &MYCURSORS[cdb], 0);
872           if (ret)
873             {
874               lprintf(1, "cdb_trunc: db_cursor: %s\n", db_strerror(ret));
875               abort();
876             }
877
878           ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb],
879                                       &key, &data, DB_NEXT);
880           if (ret)
881             {
882               cclose(MYCURSORS[cdb]);
883               txabort(tid);
884               if (ret == DB_LOCK_DEADLOCK)
885                 continue;
886
887               if (ret == DB_NOTFOUND)
888                 break;
889
890               lprintf(1, "cdb_trunc: c_get: %s\n", db_strerror(ret));
891               abort();
892             }
893
894           ret = MYCURSORS[cdb]->c_del(MYCURSORS[cdb], 0);
895           if (ret)
896             {
897               cclose(MYCURSORS[cdb]);
898               txabort(tid);
899               if (ret == DB_LOCK_DEADLOCK)
900                 continue;
901
902               lprintf(1, "cdb_trunc: c_del: %s\n", db_strerror(ret));
903               abort();
904             }
905
906           cclose(MYCURSORS[cdb]);
907           txcommit(tid);
908         }
909 #else
910     retry:
911       txbegin(&tid);
912       
913       if ((ret = dbp[cdb]->truncate(dbp[cdb],    /* db */
914                                     tid,         /* transaction ID */
915                                     &count,      /* #rows deleted */
916                                     0)))         /* flags */
917         {
918           if (ret == DB_LOCK_DEADLOCK)
919             {
920               txabort(tid);
921               goto retry;
922             }
923           else
924             {
925               lprintf(1, "cdb_truncate(%d): %s\n", cdb,
926                       db_strerror(ret));
927               abort();
928             }
929         }
930       else
931         {
932           txcommit(tid);
933         }
934 #endif
935     }
936 }