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