]> code.citadel.org Git - citadel.git/blobdiff - citadel/database_sleepycat.c
* Compress VISIT records using zlib if available. This reduces the object
[citadel.git] / citadel / database_sleepycat.c
index 58a6224bb458d30de809179cc1307fc791b88bac..ae9c5551b52b9e55533335618820a98fdae3eac0 100644 (file)
  * the specified number of kilobytes has been written, or if the specified
  * number of minutes has passed, since the last checkpoint.
  */
-#define MAX_CHECKPOINT_KBYTES  0
+#define MAX_CHECKPOINT_KBYTES  256
 #define MAX_CHECKPOINT_MINUTES 15
 
 /*****************************************************************************/
 
+#ifdef DLL_EXPORT
+#define IN_LIBCIT
+#endif
+
 #include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
 #include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>
+
+#ifdef HAVE_DB_H
 #include <db.h>
+#elif defined(HAVE_DB4_DB_H)
+#include <db4/db.h>
+#elif defined(HAVE_DB3_DB_H)
+#include <db3/db.h>
+#else
+#error Neither <db.h> nor <db3/db.h> was found by configure. Install db3-devel.
+#endif
+
 #include <pthread.h>
 #include "citadel.h"
 #include "server.h"
+#include "dynloader.h"
 #include "citserver.h"
 #include "database.h"
+#include "msgbase.h"
 #include "sysdep_decls.h"
-#include "dynloader.h"
 
-DB *dbp[MAXCDB];               /* One DB handle for each Citadel database */
-DB_ENV *dbenv;                 /* The DB environment (global) */
+static DB *dbp[MAXCDB];                /* One DB handle for each Citadel database */
+static DB_ENV *dbenv;          /* The DB environment (global) */
 
 struct cdbtsd {                        /* Thread-specific DB stuff */
        DB_TXN *tid;            /* Transaction handle */
-       DBC *cursor;            /* Cursor, for traversals... */
+       DBC *cursors[MAXCDB];   /* Cursors, for traversals... */
 };
 
-int num_ssd = 0;
+#ifdef HAVE_ZLIB
+#include <zlib.h>
+#endif
 
 static pthread_key_t tsdkey;
 
-#define MYCURSOR       (((struct cdbtsd*)pthread_getspecific(tsdkey))->cursor)
+#define MYCURSORS      (((struct cdbtsd*)pthread_getspecific(tsdkey))->cursors)
 #define MYTID          (((struct cdbtsd*)pthread_getspecific(tsdkey))->tid)
 
 /* just a little helper function */
-static int txabort(DB_TXN *tid) {
+static void txabort(DB_TXN *tid) {
         int ret = txn_abort(tid);
 
-        if (ret)
+        if (ret) {
                 lprintf(1, "cdb_*: txn_abort: %s\n", db_strerror(ret));
-
-        return ret;
+               abort();
+       }
 }
 
 /* this one is even more helpful than the last. */
-static int txcommit(DB_TXN *tid) {
+static void txcommit(DB_TXN *tid) {
         int ret = txn_commit(tid, 0);
 
-        if (ret)
+        if (ret) {
                 lprintf(1, "cdb_*: txn_commit: %s\n", db_strerror(ret));
-
-        return ret;
+               abort();
+       }
 }
 
 /* are you sensing a pattern yet? */
-static int txbegin(DB_TXN **tid) {
+static void txbegin(DB_TXN **tid) {
         int ret = txn_begin(dbenv, NULL, tid, 0);
 
-        if (ret)
+        if (ret) {
                 lprintf(1, "cdb_*: txn_begin: %s\n", db_strerror(ret));
+               abort();
+       }
+}
 
-        return ret;
+static void cclose(DBC *cursor) {
+       int ret;
+
+       if ((ret = cursor->c_close(cursor))) {
+               lprintf(1, "cdb_*: c_close: %s\n", db_strerror(ret));
+               abort();
+       }
+}
+
+static void bailIfCursor(DBC **cursors, const char *msg)
+{
+  int i;
+
+  for (i = 0; i < MAXCDB; i++)
+    if (cursors[i] != NULL)
+      {
+       lprintf(1, "cdb_*: cursor still in progress on cdb %d: %s\n", i, msg);
+       abort();
+      }
 }
 
-static void release_handles(void *arg) {
+static void check_handles(void *arg) {
        if (arg != NULL) {
                struct cdbtsd *tsd = (struct cdbtsd *)arg;
 
-               if (tsd->cursor != NULL) {
-                       lprintf(1, "cdb_*: WARNING: cursor still in progress; "
-                               "closing!\n");
-                       tsd->cursor->c_close(tsd->cursor);
-               }
+               bailIfCursor(tsd->cursors, "in check_handles");
 
                if (tsd->tid != NULL) {
-                       lprintf(1, "cdb_*: ERROR: transaction still in progress; "
-                               "aborting!\n");
-                       txabort(tsd->tid);
+                       lprintf(1, "cdb_*: transaction still in progress!");
+                       abort();
                }
        }
 }
 
 static void dest_tsd(void *arg) {
        if (arg != NULL) {
-               release_handles(arg);
+               check_handles(arg);
                phree(arg);
        }
 }
@@ -114,10 +149,16 @@ static void dest_tsd(void *arg) {
  * to use database calls, except for whatever thread calls open_databases.
  */
 void cdb_allocate_tsd(void) {
-       struct cdbtsd *tsd = mallok(sizeof *tsd);
+       struct cdbtsd *tsd;
+
+       if (pthread_getspecific(tsdkey) != NULL)
+               return;
+
+       tsd = mallok(sizeof *tsd);
 
        tsd->tid = NULL;
-       tsd->cursor = NULL;
+
+       memset(tsd->cursors, 0, sizeof tsd->cursors);
        pthread_setspecific(tsdkey, tsd);
 }
 
@@ -126,8 +167,8 @@ void cdb_free_tsd(void) {
        pthread_setspecific(tsdkey, NULL);
 }
 
-void cdb_release_handles(void) {
-       release_handles(pthread_getspecific(tsdkey));
+void cdb_check_handles(void) {
+       check_handles(pthread_getspecific(tsdkey));
 }
 
 
@@ -144,20 +185,86 @@ void defrag_databases(void)
 }
 
 
+/*
+ * Cull the database logs
+ */
+static void cdb_cull_logs(void) {
+       u_int32_t flags;
+       int ret;
+       char **file, **list;
+       char errmsg[SIZ];
+
+       lprintf(5, "Database log file cull started.\n");
+
+       flags = DB_ARCH_ABS;
+
+       /* Get the list of names. */
+#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 3
+       if ((ret = log_archive(dbenv, &list, flags, NULL)) != 0) {
+#elif DB_VERSION_MAJOR >= 4
+       if ((ret = dbenv->log_archive(dbenv, &list, flags)) != 0) {
+#else
+       if ((ret = log_archive(dbenv, &list, flags)) != 0) {
+#endif
+               lprintf(1, "cdb_cull_logs: %s\n", db_strerror(ret));
+               return;
+       }
+
+       /* Print the list of names. */
+       if (list != NULL) {
+               for (file = list; *file != NULL; ++file) {
+                       lprintf(9, "Deleting log: %s\n", *file);
+                       ret = unlink(*file);
+                       if (ret != 0) {
+                               snprintf(errmsg, sizeof(errmsg),
+                                       " ** ERROR **\n \n \n "
+                                       "Citadel was unable to delete the "
+                                       "database log file '%s' because of the "
+                                       "following error:\n \n %s\n \n"
+                                       " This log file is no longer in use "
+                                       "and may be safely deleted.\n",
+                                       *file,
+                                       strerror(errno));
+                               aide_message(errmsg);
+                       }
+               }
+               free(list);
+       }
+
+       lprintf(5, "Database log file cull ended.\n");
+}
+
 
 /*
  * Request a checkpoint of the database.
  */
 static void cdb_checkpoint(void) {
        int ret;
+       static time_t last_cull = 0L;
 
+#if DB_VERSION_MAJOR >= 4
+       ret = dbenv->txn_checkpoint(dbenv,
+#else
        ret = txn_checkpoint(dbenv,
+#endif
                                MAX_CHECKPOINT_KBYTES,
                                MAX_CHECKPOINT_MINUTES,
                                0);
-       if (ret) {
+       if ( (ret != 0) && (ret != DB_INCOMPLETE) ) {
                lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n", db_strerror(ret));
+               abort();
+       }
+
+       if (ret == DB_INCOMPLETE) {
+               lprintf(3, "WARNING: txn_checkpoint: %s\n", db_strerror(ret));
        }
+
+       /* Cull the logs if we haven't done so for 24 hours */
+       if ((time(NULL) - last_cull) > 86400L) {
+               last_cull = time(NULL);
+               cdb_cull_logs();
+       }
+
 }
 
 /*
@@ -174,6 +281,10 @@ void open_databases(void)
        u_int32_t flags = 0;
 
        lprintf(9, "cdb_*: open_databases() starting\n");
+#ifdef HAVE_ZLIB
+       lprintf(5, "zlib compression version %s\n", zlibVersion());
+#endif
+
         /*
          * Silently try to create the database subdirectory.  If it's
          * already there, no problem.
@@ -230,13 +341,13 @@ void open_databases(void)
                /* Arbitrary names for our tables -- we reference them by
                 * number, so we don't have string names for them.
                 */
-               sprintf(dbfilename, "cdb.%02x", i);
+               snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i);
 
                ret = dbp[i]->open(dbp[i],
                                dbfilename,
                                NULL,
                                DB_BTREE,
-                               DB_CREATE,
+                               DB_CREATE|DB_THREAD,
                                0600);
                if (ret) {
                        lprintf(1, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
@@ -266,8 +377,13 @@ void close_databases(void)
 
        cdb_free_tsd();
 
+#if DB_VERSION_MAJOR >= 4
+       if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
+#else
        if ((ret = txn_checkpoint(dbenv, 0, 0, 0))) {
+#endif
                lprintf(1, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
+               abort();
        }
 
        for (a = 0; a < MAXCDB; ++a) {
@@ -275,6 +391,7 @@ void close_databases(void)
                ret = dbp[a]->close(dbp[a], 0);
                if (ret) {
                        lprintf(1, "cdb_*: db_close: %s\n", db_strerror(ret));
+                       abort();
                }
                
        }
@@ -283,9 +400,54 @@ void close_databases(void)
         ret = dbenv->close(dbenv, 0);
        if (ret) {
                 lprintf(1, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
+               abort();
         }
 }
 
+
+/*
+ * Compression functions only used if we have zlib
+ */
+#ifdef HAVE_ZLIB
+
+void cdb_decompress_if_necessary(struct cdbdata *cdb) {
+       static int magic = COMPRESS_MAGIC;
+       struct CtdlCompressHeader zheader;
+       char *uncompressed_data;
+       char *compressed_data;
+       uLongf destLen, sourceLen;
+
+       if (cdb == NULL) return;
+       if (cdb->ptr == NULL) return;
+       if (memcmp(cdb->ptr, &magic, sizeof(magic))) return;
+
+       /* At this point we know we're looking at a compressed item. */
+       memcpy(&zheader, cdb->ptr, sizeof(struct CtdlCompressHeader));
+
+       compressed_data = cdb->ptr;
+       compressed_data += sizeof(struct CtdlCompressHeader);
+
+       sourceLen = (uLongf) zheader.compressed_len;
+       destLen = (uLongf) zheader.uncompressed_len;
+       uncompressed_data = mallok(zheader.uncompressed_len);
+
+       if (uncompress( (Bytef *) uncompressed_data,
+                       &destLen,
+                       compressed_data,
+                       sourceLen
+       ) != Z_OK) {
+               lprintf(1, "uncompress() error\n");
+               abort();
+       }
+
+       phree(cdb->ptr);
+       cdb->len = (size_t) destLen;
+       cdb->ptr = uncompressed_data;
+}
+
+#endif /* HAVE_ZLIB */
+       
+
 /*
  * Store a piece of data.  Returns 0 if the operation was successful.  If a
  * key already exists it should be overwritten.
@@ -295,53 +457,108 @@ int cdb_store(int cdb,
              void *cdata, int cdatalen)
 {
 
-       DBT dkey, ddata;
-       DB_TXN *tid;
-       int ret;
-
-       memset(&dkey, 0, sizeof(DBT));
-       memset(&ddata, 0, sizeof(DBT));
-       dkey.size = ckeylen;
-       dkey.data = ckey;
-       ddata.size = cdatalen;
-       ddata.data = cdata;
-
-       if (MYTID != NULL) {
-               ret = dbp[cdb]->put(dbp[cdb],           /* db */
-                                       MYTID,          /* transaction ID */
-                                       &dkey,          /* key */
-                                       &ddata,         /* data */
-                                       0);             /* flags */
-               if (ret) {
-                       lprintf(1, "cdb_store(%d): %s\n", cdb,
-                               db_strerror(ret));
-               }
-               return ret;
-       } else {
-           retry:
-               if (txbegin(&tid))
-                       return -1;
-
-               if ((ret = dbp[cdb]->put(dbp[cdb],      /* db */
-                                        tid,           /* transaction ID */
-                                        &dkey,         /* key */
-                                        &ddata,        /* data */
-                                        0))) {         /* flags */
-                       if (ret == DB_LOCK_DEADLOCK) {
-                               if (txabort(tid))
-                                       return ret;
-                               else
-                                       goto retry;
-                       } else {
-                               lprintf(1, "cdb_store(%d): %s\n", cdb,
-                                       db_strerror(ret));
-                               txabort(tid);
-                               return ret;
-                       }
-               } else {
-                       return txcommit(tid);
+  DBT dkey, ddata;
+  DB_TXN *tid;
+  int ret;
+
+#ifdef HAVE_ZLIB
+       struct CtdlCompressHeader zheader;
+       char *compressed_data = NULL;
+       int compressing = 0;
+       size_t buffer_len;
+       uLongf destLen;
+#endif
+  
+  memset(&dkey, 0, sizeof(DBT));
+  memset(&ddata, 0, sizeof(DBT));
+  dkey.size = ckeylen;
+  dkey.data = ckey;
+  ddata.size = cdatalen;
+  ddata.data = cdata;
+
+#ifdef HAVE_ZLIB
+       /* Only compress Visit records.  Everything else is uncompressed. */
+       if (cdb == CDB_VISIT) {
+               compressing = 1;
+               zheader.magic = COMPRESS_MAGIC;
+               zheader.uncompressed_len = cdatalen;
+               buffer_len = ( (cdatalen * 101) / 100 ) + 100
+                               + sizeof(struct CtdlCompressHeader) ;
+               destLen = (uLongf) buffer_len;
+               compressed_data = mallok(buffer_len);
+               if (compress2(
+                       (Bytef *) (compressed_data +
+                                       sizeof(struct CtdlCompressHeader)),
+                       &destLen,
+                       (Bytef *) cdata,
+                       (uLongf) cdatalen,
+                       1
+               ) != Z_OK) {
+                       lprintf(1, "compress2() error\n");
+                       abort();
                }
+               zheader.compressed_len = (size_t) destLen;
+               memcpy(compressed_data, &zheader,
+                       sizeof(struct CtdlCompressHeader));
+               ddata.size = (size_t)  (sizeof(struct CtdlCompressHeader) +
+                                               zheader.compressed_len);
+               ddata.data = compressed_data;
+       }
+#endif
+  
+  if (MYTID != NULL)
+    {
+      ret = dbp[cdb]->put(dbp[cdb],            /* db */
+                         MYTID,                /* transaction ID */
+                         &dkey,                /* key */
+                         &ddata,               /* data */
+                         0);           /* flags */
+      if (ret)
+       {
+         lprintf(1, "cdb_store(%d): %s\n", cdb,
+                 db_strerror(ret));
+         abort();
        }
+#ifdef HAVE_ZLIB
+      if (compressing) phree(compressed_data);
+#endif
+      return ret;
+      
+    }
+  else
+    {
+      bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
+      
+    retry:
+      txbegin(&tid);
+      
+      if ((ret = dbp[cdb]->put(dbp[cdb],    /* db */
+                              tid,         /* transaction ID */
+                              &dkey,       /* key */
+                              &ddata,      /* data */
+                              0)))         /* flags */
+       {
+         if (ret == DB_LOCK_DEADLOCK)
+           {
+             txabort(tid);
+             goto retry;
+           }
+         else
+           {
+             lprintf(1, "cdb_store(%d): %s\n", cdb,
+                     db_strerror(ret));
+             abort();
+           }
+       }
+      else
+       {
+         txcommit(tid);
+#ifdef HAVE_ZLIB
+         if (compressing) phree(compressed_data);
+#endif
+         return ret;
+       }
+    }
 }
 
 
@@ -351,41 +568,73 @@ int cdb_store(int cdb,
 int cdb_delete(int cdb, void *key, int keylen)
 {
 
-       DBT dkey;
-       DB_TXN *tid;
-       int ret;
-
-       memset(&dkey, 0, sizeof dkey);
-       dkey.size = keylen;
-       dkey.data = key;
-
-       if (MYTID != NULL) {
-               ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
-               return (ret);
-       } else {
-           retry:
-               if (txbegin(&tid))
-                       return -1;
-
-               if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))) {
-                       if (ret == DB_LOCK_DEADLOCK) {
-                                       if (txabort(tid))
-                                               return ret;
-                                       else
-                                               goto retry;
-                       } else {
-                               lprintf(1, "cdb_delete(%d): %s\n", cdb,
-                                       db_strerror(ret));
-                               txabort(tid);
-                               return ret;
-                       }
-               } else {
-                       return txcommit(tid);
-               }
+  DBT dkey;
+  DB_TXN *tid;
+  int ret;
+
+  memset(&dkey, 0, sizeof dkey);
+  dkey.size = keylen;
+  dkey.data = key;
+
+  if (MYTID != NULL)
+    {
+      ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
+      if (ret)
+       {
+         lprintf(1, "cdb_delete(%d): %s\n", cdb,
+                 db_strerror(ret));
+         if (ret != DB_NOTFOUND)
+           abort();
+       }
+    }
+  else
+    {
+      bailIfCursor(MYCURSORS, "attempt to delete during r/o cursor");
+    
+    retry:
+      txbegin(&tid);
+    
+      if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))
+         && ret != DB_NOTFOUND)
+       {
+         if (ret == DB_LOCK_DEADLOCK)
+           {
+             txabort(tid);
+             goto retry;
+           }
+         else
+           {
+             lprintf(1, "cdb_delete(%d): %s\n", cdb,
+                     db_strerror(ret));
+             abort();
+           }
        }
+      else
+       {
+         txcommit(tid);
+       }
+    }
+  return ret;
 }
 
+static DBC *localcursor(int cdb)
+{
+  int ret;
+  DBC *curs;
 
+  if (MYCURSORS[cdb] == NULL)
+    ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &curs, 0);
+  else
+    ret = MYCURSORS[cdb]->c_dup(MYCURSORS[cdb], &curs, DB_POSITION);
+
+  if (ret)
+    {
+      lprintf(1, "localcursor: %s\n", db_strerror(ret));
+      abort();
+    }
+
+  return curs;
+}
 
 
 /*
@@ -396,46 +645,57 @@ int cdb_delete(int cdb, void *key, int keylen)
 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
 {
 
-       struct cdbdata *tempcdb;
-       DBT dkey, dret;
-       DB_TXN *tid;
-       int ret;
-
-       memset(&dkey, 0, sizeof(DBT));
-       memset(&dret, 0, sizeof(DBT));
-       dkey.size = keylen;
-       dkey.data = key;
-       dret.flags = DB_DBT_MALLOC;
-
-       if (MYTID != NULL) {
-               ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
-       } else {
-           retry:
-               if (txbegin(&tid))
-                       return NULL;
-
-               ret = dbp[cdb]->get(dbp[cdb], tid, &dkey, &dret, 0);
-
-               if (ret == DB_LOCK_DEADLOCK) {
-                       if (txabort(tid))
-                               return NULL;
-                       else
-                               goto retry;
-               } else if (txcommit(tid))
-                       return NULL;
+  struct cdbdata *tempcdb;
+  DBT dkey, dret;
+  int ret;
+
+  memset(&dkey, 0, sizeof(DBT));
+  dkey.size = keylen;
+  dkey.data = key;
+
+  if (MYTID != NULL)
+    {
+      memset(&dret, 0, sizeof(DBT));
+      dret.flags = DB_DBT_MALLOC;
+      ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
+    }
+  else
+    {
+      DBC *curs;
+
+      do
+       {
+         memset(&dret, 0, sizeof(DBT));
+         dret.flags = DB_DBT_MALLOC;
+
+         curs = localcursor(cdb);
+
+         ret = curs->c_get(curs, &dkey, &dret, DB_SET);
+         cclose(curs);
        }
+      while (ret == DB_LOCK_DEADLOCK);
 
-       if ((ret != 0) && (ret != DB_NOTFOUND)) {
-               lprintf(1, "cdb_fetch: %s\n", db_strerror(ret));
-       }
-       if (ret != 0) return NULL;
-       tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
-       if (tempcdb == NULL) {
-               lprintf(2, "cdb_fetch: Cannot allocate memory!\n");
-       }
-       tempcdb->len = dret.size;
-       tempcdb->ptr = dret.data;
-       return (tempcdb);
+    }
+
+  if ((ret != 0) && (ret != DB_NOTFOUND))
+    {
+      lprintf(1, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret));
+      abort();
+    }
+
+  if (ret != 0) return NULL;
+  tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
+
+  if (tempcdb == NULL)
+    {
+      lprintf(2, "cdb_fetch: Cannot allocate memory for tempcdb\n");
+      abort();
+    }
+
+  tempcdb->len = dret.size;
+  tempcdb->ptr = dret.data;
+  cdb_decompress_if_necessary(tempcdb);
+  return (tempcdb);
 }
 
 
@@ -449,6 +709,13 @@ void cdb_free(struct cdbdata *cdb)
        phree(cdb);
 }
 
+void cdb_close_cursor(int cdb)
+{
+       if (MYCURSORS[cdb] != NULL)
+               cclose(MYCURSORS[cdb]);
+
+       MYCURSORS[cdb] = NULL;
+}
 
 /* 
  * Prepare for a sequential search of an entire database.
@@ -459,20 +726,16 @@ void cdb_rewind(int cdb)
 {
        int ret = 0;
 
-       if (MYCURSOR != NULL)
-               MYCURSOR->c_close(MYCURSOR);
-
-       if (MYTID == NULL) {
-               lprintf(1, "cdb_rewind: ERROR: cursor use outside transaction\n");
-               abort();
-       }
+       if (MYCURSORS[cdb] != NULL)
+               cclose(MYCURSORS[cdb]);
 
        /*
         * Now initialize the cursor
         */
-       ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSOR, 0);
+       ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSORS[cdb], 0);
        if (ret) {
                lprintf(1, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
+               abort();
        }
 }
 
@@ -492,46 +755,162 @@ struct cdbdata *cdb_next_item(int cdb)
         memset(&data, 0, sizeof(data));
        data.flags = DB_DBT_MALLOC;
 
-       ret = MYCURSOR->c_get(MYCURSOR,
+       ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb],
                &key, &data, DB_NEXT);
        
        if (ret) {
-               MYCURSOR->c_close(MYCURSOR);
-               MYCURSOR = NULL;
+               if (ret != DB_NOTFOUND) {
+                       lprintf(1, "cdb_next_item(%d): %s\n",
+                               cdb, db_strerror(ret));
+                       abort();
+               }
+               cclose(MYCURSORS[cdb]);
+               MYCURSORS[cdb] = NULL;
                return NULL;            /* presumably, end of file */
        }
 
        cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
        cdbret->len = data.size;
        cdbret->ptr = data.data;
+       cdb_decompress_if_necessary(cdbret);
 
        return (cdbret);
 }
 
 
+
 /*
  * Transaction-based stuff.  I'm writing this as I bake cookies...
  */
 
 void cdb_begin_transaction(void) {
 
-       if (MYTID != NULL) {    /* FIXME this slows it down, take it out */
-               lprintf(1, "cdb_begin_transaction: ERROR: opening a new transaction with one already open!\n");
-               abort();
-       }
-       else {
-               txbegin(&MYTID);
-       }
+  bailIfCursor(MYCURSORS, "can't begin transaction during r/o cursor");
+
+  if (MYTID != NULL)
+    {
+      lprintf(1, "cdb_begin_transaction: ERROR: nested transaction\n");
+      abort();
+    }
+
+  txbegin(&MYTID);
 }
 
 void cdb_end_transaction(void) {
-       if (MYCURSOR != NULL) {
-               lprintf(1, "cdb_end_transaction: WARNING: cursor still open at transaction end\n");
-               MYCURSOR->c_close(MYCURSOR);
-               MYCURSOR = NULL;
-       }
-       if (MYTID == NULL) lprintf(1, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");
-       else txcommit(MYTID);
-       MYTID = NULL;
+  int i;
+
+  for (i = 0; i < MAXCDB; i++)
+    if (MYCURSORS[i] != NULL) {
+      lprintf(1, "cdb_end_transaction: WARNING: cursor %d still open at transaction end\n", i);
+      cclose(MYCURSORS[i]);
+      MYCURSORS[i] = NULL;
+    }
+
+  if (MYTID == NULL)
+    {
+      lprintf(1, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");
+      abort();
+    }
+  else
+    txcommit(MYTID);
+
+  MYTID = NULL;
 }
 
+/*
+ * Truncate (delete every record)
+ */
+void cdb_trunc(int cdb)
+{
+  DB_TXN *tid;
+  int ret;
+#if DB_VERSION_MAJOR > 3 || DB_VERSION_MINOR > 2
+  u_int32_t count;
+#endif
+  
+  if (MYTID != NULL)
+    {
+      lprintf(1, "cdb_trunc must not be called in a transaction.\n");
+      abort();
+    }
+  else
+    {
+      bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
+      
+#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 3
+      for (;;)
+       {
+         DBT key, data;
+
+         /* Initialize the key/data pair so the flags aren't set. */
+         memset(&key, 0, sizeof(key));
+         memset(&data, 0, sizeof(data));
+
+         txbegin(&tid);
+         
+         ret = dbp[cdb]->cursor(dbp[cdb], tid, &MYCURSORS[cdb], 0);
+         if (ret)
+           {
+             lprintf(1, "cdb_trunc: db_cursor: %s\n", db_strerror(ret));
+             abort();
+           }
+
+         ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb],
+                                     &key, &data, DB_NEXT);
+         if (ret)
+           {
+             cclose(MYCURSORS[cdb]);
+             txabort(tid);
+             if (ret == DB_LOCK_DEADLOCK)
+               continue;
+
+             if (ret == DB_NOTFOUND)
+               break;
+
+             lprintf(1, "cdb_trunc: c_get: %s\n", db_strerror(ret));
+             abort();
+           }
+
+         ret = MYCURSORS[cdb]->c_del(MYCURSORS[cdb], 0);
+         if (ret)
+           {
+             cclose(MYCURSORS[cdb]);
+             txabort(tid);
+             if (ret == DB_LOCK_DEADLOCK)
+               continue;
+
+             lprintf(1, "cdb_trunc: c_del: %s\n", db_strerror(ret));
+             abort();
+           }
+
+         cclose(MYCURSORS[cdb]);
+         txcommit(tid);
+       }
+#else
+    retry:
+      txbegin(&tid);
+      
+      if ((ret = dbp[cdb]->truncate(dbp[cdb],    /* db */
+                                   tid,         /* transaction ID */
+                                   &count,      /* #rows deleted */
+                                   0)))         /* flags */
+       {
+         if (ret == DB_LOCK_DEADLOCK)
+           {
+             txabort(tid);
+             goto retry;
+           }
+         else
+           {
+             lprintf(1, "cdb_truncate(%d): %s\n", cdb,
+                     db_strerror(ret));
+             abort();
+           }
+       }
+      else
+       {
+         txcommit(tid);
+       }
+#endif
+    }
+}