]> code.citadel.org Git - citadel.git/blob - citadel/database_sleepycat.c
* During startup, display the version string from the Berkeley DB library.
[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         lprintf(5, "%s\n", db_version(NULL, NULL, NULL));
283 #ifdef HAVE_ZLIB
284         lprintf(5, "zlib compression version %s\n", zlibVersion());
285 #endif
286
287         /*
288          * Silently try to create the database subdirectory.  If it's
289          * already there, no problem.
290          */
291         system("exec mkdir data 2>/dev/null");
292
293         lprintf(9, "cdb_*: Setting up DB environment\n");
294         db_env_set_func_yield(sched_yield);
295         ret = db_env_create(&dbenv, 0);
296         if (ret) {
297                 lprintf(1, "cdb_*: db_env_create: %s\n", db_strerror(ret));
298                 exit(ret);
299         }
300         dbenv->set_errpfx(dbenv, "citserver");
301
302         /*
303          * We want to specify the shared memory buffer pool cachesize,
304          * but everything else is the default.
305          */
306         ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
307         if (ret) {
308                 lprintf(1, "cdb_*: set_cachesize: %s\n", db_strerror(ret));
309                 dbenv->close(dbenv, 0);
310                 exit(ret);
311         }
312
313         if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
314                 lprintf(1, "cdb_*: set_lk_detect: %s\n", db_strerror(ret));
315                 dbenv->close(dbenv, 0);
316                 exit(ret);
317         }
318
319         flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_TXN|
320                 DB_INIT_LOCK|DB_THREAD;
321         ret = dbenv->open(dbenv, "./data", flags, 0);
322         if (ret) {
323                 lprintf(1, "cdb_*: dbenv->open: %s\n", db_strerror(ret));
324                 dbenv->close(dbenv, 0);
325                 exit(ret);
326         }
327
328         lprintf(7, "cdb_*: Starting up DB\n");
329
330         for (i = 0; i < MAXCDB; ++i) {
331
332                 /* Create a database handle */
333                 ret = db_create(&dbp[i], dbenv, 0);
334                 if (ret) {
335                         lprintf(1, "cdb_*: db_create: %s\n", db_strerror(ret));
336                         exit(ret);
337                 }
338
339
340                 /* Arbitrary names for our tables -- we reference them by
341                  * number, so we don't have string names for them.
342                  */
343                 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i);
344
345                 ret = dbp[i]->open(dbp[i],
346                                 NULL,
347                                 dbfilename,
348                                 NULL,
349                                 DB_BTREE,
350                                 DB_CREATE|DB_THREAD
351                                 |DB_AUTO_COMMIT
352                                 ,
353                                 0600);
354                 if (ret) {
355                         lprintf(1, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
356                         exit(ret);
357                 }
358         }
359
360         if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
361                 lprintf(1, "cdb_*: pthread_key_create: %s\n", strerror(ret));
362                 exit(1);
363         }
364
365         cdb_allocate_tsd();
366         CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
367         lprintf(9, "cdb_*: open_databases() finished\n");
368 }
369
370
371 /*
372  * Close all of the db database files we've opened.  This can be done
373  * in a loop, since it's just a bunch of closes.
374  */
375 void close_databases(void)
376 {
377         int a;
378         int ret;
379
380         cdb_free_tsd();
381
382         if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
383                 lprintf(1, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
384                 abort();
385         }
386
387         for (a = 0; a < MAXCDB; ++a) {
388                 lprintf(7, "cdb_*: Closing database %d\n", a);
389                 ret = dbp[a]->close(dbp[a], 0);
390                 if (ret) {
391                         lprintf(1, "cdb_*: db_close: %s\n", db_strerror(ret));
392                         abort();
393                 }
394                 
395         }
396
397         /* Close the handle. */
398         ret = dbenv->close(dbenv, 0);
399         if (ret) {
400                 lprintf(1, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
401                 abort();
402         }
403 }
404
405
406 /*
407  * Compression functions only used if we have zlib
408  */
409 #ifdef HAVE_ZLIB
410
411 void cdb_decompress_if_necessary(struct cdbdata *cdb) {
412         static int magic = COMPRESS_MAGIC;
413         struct CtdlCompressHeader zheader;
414         char *uncompressed_data;
415         char *compressed_data;
416         uLongf destLen, sourceLen;
417
418         if (cdb == NULL) return;
419         if (cdb->ptr == NULL) return;
420         if (memcmp(cdb->ptr, &magic, sizeof(magic))) return;
421
422         /* At this point we know we're looking at a compressed item. */
423         memcpy(&zheader, cdb->ptr, sizeof(struct CtdlCompressHeader));
424
425         compressed_data = cdb->ptr;
426         compressed_data += sizeof(struct CtdlCompressHeader);
427
428         sourceLen = (uLongf) zheader.compressed_len;
429         destLen = (uLongf) zheader.uncompressed_len;
430         uncompressed_data = mallok(zheader.uncompressed_len);
431
432         if (uncompress( (Bytef *) uncompressed_data,
433                         &destLen,
434                         compressed_data,
435                         sourceLen
436         ) != Z_OK) {
437                 lprintf(1, "uncompress() error\n");
438                 abort();
439         }
440
441         phree(cdb->ptr);
442         cdb->len = (size_t) destLen;
443         cdb->ptr = uncompressed_data;
444 }
445
446 #endif  /* HAVE_ZLIB */
447         
448
449 /*
450  * Store a piece of data.  Returns 0 if the operation was successful.  If a
451  * key already exists it should be overwritten.
452  */
453 int cdb_store(int cdb,
454               void *ckey, int ckeylen,
455               void *cdata, int cdatalen)
456 {
457
458   DBT dkey, ddata;
459   DB_TXN *tid;
460   int ret;
461
462 #ifdef HAVE_ZLIB
463         struct CtdlCompressHeader zheader;
464         char *compressed_data = NULL;
465         int compressing = 0;
466         size_t buffer_len;
467         uLongf destLen;
468 #endif
469   
470   memset(&dkey, 0, sizeof(DBT));
471   memset(&ddata, 0, sizeof(DBT));
472   dkey.size = ckeylen;
473   dkey.data = ckey;
474   ddata.size = cdatalen;
475   ddata.data = cdata;
476
477 #ifdef HAVE_ZLIB
478         /* Only compress Visit records.  Everything else is uncompressed. */
479         if (cdb == CDB_VISIT) {
480                 compressing = 1;
481                 zheader.magic = COMPRESS_MAGIC;
482                 zheader.uncompressed_len = cdatalen;
483                 buffer_len = ( (cdatalen * 101) / 100 ) + 100
484                                 + sizeof(struct CtdlCompressHeader) ;
485                 destLen = (uLongf) buffer_len;
486                 compressed_data = mallok(buffer_len);
487                 if (compress2(
488                         (Bytef *) (compressed_data +
489                                         sizeof(struct CtdlCompressHeader)),
490                         &destLen,
491                         (Bytef *) cdata,
492                         (uLongf) cdatalen,
493                         1
494                 ) != Z_OK) {
495                         lprintf(1, "compress2() error\n");
496                         abort();
497                 }
498                 zheader.compressed_len = (size_t) destLen;
499                 memcpy(compressed_data, &zheader,
500                         sizeof(struct CtdlCompressHeader));
501                 ddata.size = (size_t)  (sizeof(struct CtdlCompressHeader) +
502                                                 zheader.compressed_len);
503                 ddata.data = compressed_data;
504         }
505 #endif
506   
507   if (MYTID != NULL)
508     {
509       ret = dbp[cdb]->put(dbp[cdb],             /* db */
510                           MYTID,                /* transaction ID */
511                           &dkey,                /* key */
512                           &ddata,               /* data */
513                           0);           /* flags */
514       if (ret)
515         {
516           lprintf(1, "cdb_store(%d): %s\n", cdb,
517                   db_strerror(ret));
518           abort();
519         }
520 #ifdef HAVE_ZLIB
521       if (compressing) phree(compressed_data);
522 #endif
523       return ret;
524       
525     }
526   else
527     {
528       bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
529       
530     retry:
531       txbegin(&tid);
532       
533       if ((ret = dbp[cdb]->put(dbp[cdb],    /* db */
534                                tid,         /* transaction ID */
535                                &dkey,       /* key */
536                                &ddata,      /* data */
537                                0)))         /* flags */
538         {
539           if (ret == DB_LOCK_DEADLOCK)
540             {
541               txabort(tid);
542               goto retry;
543             }
544           else
545             {
546               lprintf(1, "cdb_store(%d): %s\n", cdb,
547                       db_strerror(ret));
548               abort();
549             }
550         }
551       else
552         {
553           txcommit(tid);
554 #ifdef HAVE_ZLIB
555           if (compressing) phree(compressed_data);
556 #endif
557           return ret;
558         }
559     }
560 }
561
562
563 /*
564  * Delete a piece of data.  Returns 0 if the operation was successful.
565  */
566 int cdb_delete(int cdb, void *key, int keylen)
567 {
568
569   DBT dkey;
570   DB_TXN *tid;
571   int ret;
572
573   memset(&dkey, 0, sizeof dkey);
574   dkey.size = keylen;
575   dkey.data = key;
576
577   if (MYTID != NULL)
578     {
579       ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
580       if (ret)
581         {
582           lprintf(1, "cdb_delete(%d): %s\n", cdb,
583                   db_strerror(ret));
584           if (ret != DB_NOTFOUND)
585             abort();
586         }
587     }
588   else
589     {
590       bailIfCursor(MYCURSORS, "attempt to delete during r/o cursor");
591     
592     retry:
593       txbegin(&tid);
594     
595       if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))
596           && ret != DB_NOTFOUND)
597         {
598           if (ret == DB_LOCK_DEADLOCK)
599             {
600               txabort(tid);
601               goto retry;
602             }
603           else
604             {
605               lprintf(1, "cdb_delete(%d): %s\n", cdb,
606                       db_strerror(ret));
607               abort();
608             }
609         }
610       else
611         {
612           txcommit(tid);
613         }
614     }
615   return ret;
616 }
617
618 static DBC *localcursor(int cdb)
619 {
620   int ret;
621   DBC *curs;
622
623   if (MYCURSORS[cdb] == NULL)
624     ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &curs, 0);
625   else
626     ret = MYCURSORS[cdb]->c_dup(MYCURSORS[cdb], &curs, DB_POSITION);
627
628   if (ret)
629     {
630       lprintf(1, "localcursor: %s\n", db_strerror(ret));
631       abort();
632     }
633
634   return curs;
635 }
636
637
638 /*
639  * Fetch a piece of data.  If not found, returns NULL.  Otherwise, it returns
640  * a struct cdbdata which it is the caller's responsibility to free later on
641  * using the cdb_free() routine.
642  */
643 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
644 {
645
646   struct cdbdata *tempcdb;
647   DBT dkey, dret;
648   int ret;
649
650   memset(&dkey, 0, sizeof(DBT));
651   dkey.size = keylen;
652   dkey.data = key;
653
654   if (MYTID != NULL)
655     {
656       memset(&dret, 0, sizeof(DBT));
657       dret.flags = DB_DBT_MALLOC;
658       ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
659     }
660   else
661     {
662       DBC *curs;
663
664       do
665         {
666           memset(&dret, 0, sizeof(DBT));
667           dret.flags = DB_DBT_MALLOC;
668
669           curs = localcursor(cdb);
670
671           ret = curs->c_get(curs, &dkey, &dret, DB_SET);
672           cclose(curs);
673         }
674       while (ret == DB_LOCK_DEADLOCK);
675
676     }
677
678   if ((ret != 0) && (ret != DB_NOTFOUND))
679     {
680       lprintf(1, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret));
681       abort();
682     }
683
684   if (ret != 0) return NULL;
685   tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
686
687   if (tempcdb == NULL)
688     {
689       lprintf(2, "cdb_fetch: Cannot allocate memory for tempcdb\n");
690       abort();
691     }
692
693   tempcdb->len = dret.size;
694   tempcdb->ptr = dret.data;
695 #ifdef HAVE_ZLIB
696   cdb_decompress_if_necessary(tempcdb);
697 #endif
698   return (tempcdb);
699 }
700
701
702 /*
703  * Free a cdbdata item (ok, this is really no big deal, but we might need to do
704  * more complex stuff with other database managers in the future).
705  */
706 void cdb_free(struct cdbdata *cdb)
707 {
708         phree(cdb->ptr);
709         phree(cdb);
710 }
711
712 void cdb_close_cursor(int cdb)
713 {
714         if (MYCURSORS[cdb] != NULL)
715                 cclose(MYCURSORS[cdb]);
716
717         MYCURSORS[cdb] = NULL;
718 }
719
720 /* 
721  * Prepare for a sequential search of an entire database.
722  * (There is guaranteed to be no more than one traversal in
723  * progress per thread at any given time.)
724  */
725 void cdb_rewind(int cdb)
726 {
727         int ret = 0;
728
729         if (MYCURSORS[cdb] != NULL)
730                 cclose(MYCURSORS[cdb]);
731
732         /*
733          * Now initialize the cursor
734          */
735         ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSORS[cdb], 0);
736         if (ret) {
737                 lprintf(1, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
738                 abort();
739         }
740 }
741
742
743 /*
744  * Fetch the next item in a sequential search.  Returns a pointer to a 
745  * cdbdata structure, or NULL if we've hit the end.
746  */
747 struct cdbdata *cdb_next_item(int cdb)
748 {
749         DBT key, data;
750         struct cdbdata *cdbret;
751         int ret = 0;
752
753         /* Initialize the key/data pair so the flags aren't set. */
754         memset(&key, 0, sizeof(key));
755         memset(&data, 0, sizeof(data));
756         data.flags = DB_DBT_MALLOC;
757
758         ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb],
759                 &key, &data, DB_NEXT);
760         
761         if (ret) {
762                 if (ret != DB_NOTFOUND) {
763                         lprintf(1, "cdb_next_item(%d): %s\n",
764                                 cdb, db_strerror(ret));
765                         abort();
766                 }
767                 cclose(MYCURSORS[cdb]);
768                 MYCURSORS[cdb] = NULL;
769                 return NULL;            /* presumably, end of file */
770         }
771
772         cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
773         cdbret->len = data.size;
774         cdbret->ptr = data.data;
775 #ifdef HAVE_ZLIB
776         cdb_decompress_if_necessary(cdbret);
777 #endif
778
779         return (cdbret);
780 }
781
782
783
784 /*
785  * Transaction-based stuff.  I'm writing this as I bake cookies...
786  */
787
788 void cdb_begin_transaction(void) {
789
790   bailIfCursor(MYCURSORS, "can't begin transaction during r/o cursor");
791
792   if (MYTID != NULL)
793     {
794       lprintf(1, "cdb_begin_transaction: ERROR: nested transaction\n");
795       abort();
796     }
797
798   txbegin(&MYTID);
799 }
800
801 void cdb_end_transaction(void) {
802   int i;
803
804   for (i = 0; i < MAXCDB; i++)
805     if (MYCURSORS[i] != NULL) {
806       lprintf(1, "cdb_end_transaction: WARNING: cursor %d still open at transaction end\n", i);
807       cclose(MYCURSORS[i]);
808       MYCURSORS[i] = NULL;
809     }
810
811   if (MYTID == NULL)
812     {
813       lprintf(1, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");
814       abort();
815     }
816   else
817     txcommit(MYTID);
818
819   MYTID = NULL;
820 }
821
822 /*
823  * Truncate (delete every record)
824  */
825 void cdb_trunc(int cdb)
826 {
827   DB_TXN *tid;
828   int ret;
829   u_int32_t count;
830   
831   if (MYTID != NULL)
832     {
833       lprintf(1, "cdb_trunc must not be called in a transaction.\n");
834       abort();
835     }
836   else
837     {
838       bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
839       
840     retry:
841       txbegin(&tid);
842       
843       if ((ret = dbp[cdb]->truncate(dbp[cdb],    /* db */
844                                     tid,         /* transaction ID */
845                                     &count,      /* #rows deleted */
846                                     0)))         /* flags */
847         {
848           if (ret == DB_LOCK_DEADLOCK)
849             {
850               txabort(tid);
851               goto retry;
852             }
853           else
854             {
855               lprintf(1, "cdb_truncate(%d): %s\n", cdb,
856                       db_strerror(ret));
857               abort();
858             }
859         }
860       else
861         {
862           txcommit(tid);
863         }
864     }
865 }