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