* Silently refuse to add directory entries for Internet addresses already
[citadel.git] / citadel / database_sleepycat.c
index 491c0d2d51dea6b34815b72df9f07b54d2b443d9..62c3deb9f4f6994a1e8216adb2aabac3fe04da47 100644 (file)
        Tunable configuration parameters for the Sleepycat DB back end
  *****************************************************************************/
 
-/* Set to 1 for transaction-based database logging.  This is recommended for
- * safe recovery in the event of system or application failure.
- */
-#define TRANSACTION_BASED      1
-
 /* Citadel will checkpoint the db at the end of every session, but only if
  * 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 <stdio.h>
-#include <time.h>
 #include <ctype.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_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 "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 cdbssd {                        /* Session-specific DB stuff */
-       DBC *cursor;            /* Cursor, for traversals... */
-       DB_TXN *tid;            /* Transaction ID */
+struct cdbtsd {                        /* Thread-specific DB stuff */
+       DB_TXN *tid;            /* Transaction handle */
+       DBC *cursors[MAXCDB];   /* Cursors, for traversals... */
 };
 
-struct cdbssd *ssd_arr = NULL;
-int num_ssd = 0;
-#define MYCURSOR       ssd_arr[CC->cs_pid].cursor
-#define MYTID          ssd_arr[CC->cs_pid].tid
+static pthread_key_t tsdkey;
+
+#define MYCURSORS      (((struct cdbtsd*)pthread_getspecific(tsdkey))->cursors)
+#define MYTID          (((struct cdbtsd*)pthread_getspecific(tsdkey))->tid)
+
+/* just a little helper function */
+static void txabort(DB_TXN *tid) {
+        int ret = txn_abort(tid);
+
+        if (ret) {
+                lprintf(1, "cdb_*: txn_abort: %s\n", db_strerror(ret));
+               abort();
+       }
+}
+
+/* this one is even more helpful than the last. */
+static void txcommit(DB_TXN *tid) {
+        int ret = txn_commit(tid, 0);
+
+        if (ret) {
+                lprintf(1, "cdb_*: txn_commit: %s\n", db_strerror(ret));
+               abort();
+       }
+}
+
+/* are you sensing a pattern yet? */
+static void txbegin(DB_TXN **tid) {
+        int ret = txn_begin(dbenv, NULL, tid, 0);
+
+        if (ret) {
+                lprintf(1, "cdb_*: txn_begin: %s\n", db_strerror(ret));
+               abort();
+       }
+}
+
+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 check_handles(void *arg) {
+       if (arg != NULL) {
+               struct cdbtsd *tsd = (struct cdbtsd *)arg;
+
+               bailIfCursor(tsd->cursors, "in check_handles");
+
+               if (tsd->tid != NULL) {
+                       lprintf(1, "cdb_*: transaction still in progress!");
+                       abort();
+               }
+       }
+}
+
+static void dest_tsd(void *arg) {
+       if (arg != NULL) {
+               check_handles(arg);
+               phree(arg);
+       }
+}
 
 /*
- * Ensure that we have enough space for session-specific data.  We don't
+ * Ensure that we have a key for thread-specific data.  We don't
  * put anything in here that Citadel cares about; this is just database
  * related stuff like cursors and transactions.
+ *
+ * This should be called immediately after startup by any thread which wants
+ * to use database calls, except for whatever thread calls open_databases.
  */
-void cdb_allocate_ssd(void) {
-       /*
-        * Make sure we have a cursor allocated for this session
-        */
+void cdb_allocate_tsd(void) {
+       struct cdbtsd *tsd;
 
-       lprintf(9, "num_ssd before realloc = %d\n", num_ssd);
-       if (num_ssd <= CC->cs_pid) {
-               num_ssd = CC->cs_pid + 1;
-               if (ssd_arr == NULL) {
-                       ssd_arr = (struct cdbssd *)
-                           mallok((sizeof(struct cdbssd) * num_ssd));
-               } else {
-                       ssd_arr = (struct cdbssd *)
-                           reallok(ssd_arr, (sizeof(struct cdbssd) * num_ssd));
-               }
-       }
-       lprintf(9, "num_ssd  after realloc = %d\n", num_ssd);
+       if (pthread_getspecific(tsdkey) != NULL)
+               return;
+
+       tsd = mallok(sizeof *tsd);
+
+       tsd->tid = NULL;
+
+       memset(tsd->cursors, 0, sizeof tsd->cursors);
+       pthread_setspecific(tsdkey, tsd);
+}
+
+void cdb_free_tsd(void) {
+       dest_tsd(pthread_getspecific(tsdkey));
+       pthread_setspecific(tsdkey, NULL);
+}
+
+void cdb_check_handles(void) {
+       check_handles(pthread_getspecific(tsdkey));
 }
 
 
@@ -90,23 +178,74 @@ void defrag_databases(void)
 }
 
 
+/*
+ * Cull the database logs
+ */
+static void cdb_cull_logs(void) {
+       u_int32_t flags;
+       int ret;
+       char **file, **list;
+
+       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);
+                       unlink(*file);
+               }
+               free(list);
+       }
+
+       lprintf(5, "Database log file cull ended.\n");
+}
+
 
 /*
  * Request a checkpoint of the database.
  */
-void cdb_checkpoint(void) {
+static void cdb_checkpoint(void) {
        int ret;
+       static time_t last_cull = 0L;
 
-       lprintf(7, "DB checkpoint\n");
+#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) {
-               lprintf(1, "txn_checkpoint: %s\n", db_strerror(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();
+       }
+
+}
 
 /*
  * Open the various databases we'll be using.  Any database which
@@ -118,19 +257,21 @@ void open_databases(void)
 {
        int ret;
        int i;
-       char dbfilename[256];
+       char dbfilename[SIZ];
        u_int32_t flags = 0;
 
+       lprintf(9, "cdb_*: open_databases() starting\n");
         /*
          * Silently try to create the database subdirectory.  If it's
          * already there, no problem.
          */
         system("exec mkdir data 2>/dev/null");
 
-       lprintf(9, "Setting up DB environment\n");
+       lprintf(9, "cdb_*: Setting up DB environment\n");
+       db_env_set_func_yield(sched_yield);
        ret = db_env_create(&dbenv, 0);
        if (ret) {
-               lprintf(1, "db_env_create: %s\n", db_strerror(ret));
+               lprintf(1, "cdb_*: db_env_create: %s\n", db_strerror(ret));
                exit(ret);
        }
        dbenv->set_errpfx(dbenv, "citserver");
@@ -141,38 +282,34 @@ void open_databases(void)
          */
         ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
        if (ret) {
-               lprintf(1, "set_cachesize: %s\n", db_strerror(ret));
+               lprintf(1, "cdb_*: set_cachesize: %s\n", db_strerror(ret));
                 dbenv->close(dbenv, 0);
                 exit(ret);
         }
 
-        /*
-        * We specify DB_PRIVATE but not DB_INIT_LOCK or DB_THREAD, even
-        * though this is a multithreaded application.  Since Citadel does all
-        * database access in S_DATABASE critical sections, access to the db
-        * is serialized already, so don't bother the database manager with
-        * it.  Besides, it locks up when we do it that way.
-         */
-#ifdef TRANSACTION_BASED
-        flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_TXN;
-#else
-        flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE;
-#endif
+       if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
+               lprintf(1, "cdb_*: set_lk_detect: %s\n", db_strerror(ret));
+               dbenv->close(dbenv, 0);
+               exit(ret);
+       }
+
+        flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_TXN|
+               DB_INIT_LOCK|DB_THREAD;
         ret = dbenv->open(dbenv, "./data", flags, 0);
        if (ret) {
-               lprintf(1, "dbenv->open: %s\n", db_strerror(ret));
+               lprintf(1, "cdb_*: dbenv->open: %s\n", db_strerror(ret));
                 dbenv->close(dbenv, 0);
                 exit(ret);
         }
 
-       lprintf(7, "Starting up DB\n");
+       lprintf(7, "cdb_*: Starting up DB\n");
 
        for (i = 0; i < MAXCDB; ++i) {
 
                /* Create a database handle */
                ret = db_create(&dbp[i], dbenv, 0);
                if (ret) {
-                       lprintf(1, "db_create: %s\n", db_strerror(ret));
+                       lprintf(1, "cdb_*: db_create: %s\n", db_strerror(ret));
                        exit(ret);
                }
 
@@ -186,24 +323,27 @@ void open_databases(void)
                                dbfilename,
                                NULL,
                                DB_BTREE,
-                               DB_CREATE,
+                               DB_CREATE|DB_THREAD,
                                0600);
                if (ret) {
-                       lprintf(1, "db_open[%d]: %s\n", i, db_strerror(ret));
+                       lprintf(1, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
                        exit(ret);
                }
        }
 
-       cdb_allocate_ssd();
-       CtdlRegisterSessionHook(cdb_allocate_ssd, EVT_START);
-#ifdef TRANSACTION_BASED
-       CtdlRegisterSessionHook(cdb_checkpoint, EVT_STOP);
-#endif
+       if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
+               lprintf(1, "cdb_*: pthread_key_create: %s\n", strerror(ret));
+               exit(1);
+       }
+
+       cdb_allocate_tsd();
+       CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
+       lprintf(9, "cdb_*: open_databases() finished\n");
 }
 
 
 /*
- * Close all of the gdbm database files we've opened.  This can be done
+ * Close all of the db database files we've opened.  This can be done
  * in a loop, since it's just a bunch of closes.
  */
 void close_databases(void)
@@ -211,30 +351,35 @@ void close_databases(void)
        int a;
        int ret;
 
-       begin_critical_section(S_DATABASE);
+       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) {
-               lprintf(7, "Closing database %d\n", a);
+               lprintf(7, "cdb_*: Closing database %d\n", a);
                ret = dbp[a]->close(dbp[a], 0);
                if (ret) {
-                       lprintf(1, "db_close: %s\n", db_strerror(ret));
+                       lprintf(1, "cdb_*: db_close: %s\n", db_strerror(ret));
+                       abort();
                }
                
        }
 
-
-
         /* Close the handle. */
         ret = dbenv->close(dbenv, 0);
        if (ret) {
-                lprintf(1, "DBENV->close: %s\n", db_strerror(ret));
+                lprintf(1, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
+               abort();
         }
-
-
-       end_critical_section(S_DATABASE);
-
 }
 
-
 /*
  * Store a piece of data.  Returns 0 if the operation was successful.  If a
  * key already exists it should be overwritten.
@@ -244,28 +389,64 @@ int cdb_store(int cdb,
              void *cdata, int cdatalen)
 {
 
-       DBT dkey, ddata;
-       int ret;
-
-       memset(&dkey, 0, sizeof(DBT));
-       memset(&ddata, 0, sizeof(DBT));
-       dkey.size = ckeylen;
-       dkey.data = ckey;
-       ddata.size = cdatalen;
-       ddata.data = cdata;
-
-       begin_critical_section(S_DATABASE);
-       ret = dbp[cdb]->put(dbp[cdb],           /* db */
-                               MYTID,          /* transaction ID */
-                               &dkey,          /* key */
-                               &ddata,         /* data */
-                               0);             /* flags */
-       end_critical_section(S_DATABASE);
-       if (ret) {
-               lprintf(1, "cdb_store: %s\n", db_strerror(ret));
-               return (-1);
+  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));
+         abort();
        }
-       return (0);
+      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);
+         return ret;
+       }
+    }
 }
 
 
@@ -275,20 +456,73 @@ int cdb_store(int cdb,
 int cdb_delete(int cdb, void *key, int keylen)
 {
 
-       DBT dkey;
-       int ret;
-
-       dkey.size = keylen;
-       dkey.data = key;
+  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;
+}
 
-       begin_critical_section(S_DATABASE);
-       ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
-       end_critical_section(S_DATABASE);
-       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;
+}
 
 
 /*
@@ -299,30 +533,56 @@ 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;
-       int ret;
-
-       memset(&dkey, 0, sizeof(DBT));
-       memset(&dret, 0, sizeof(DBT));
-       dkey.size = keylen;
-       dkey.data = key;
-       dret.flags = DB_DBT_MALLOC;
-
-       begin_critical_section(S_DATABASE);
-       ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
-       end_critical_section(S_DATABASE);
-       if ((ret != 0) && (ret != DB_NOTFOUND)) {
-               lprintf(1, "cdb_fetch: %s\n", db_strerror(ret));
+  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);
        }
-       if (ret != 0) return NULL;
-       tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
-       if (tempcdb == NULL) {
-               lprintf(2, "Cannot allocate memory!\n");
-       }
-       tempcdb->len = dret.size;
-       tempcdb->ptr = dret.data;
-       return (tempcdb);
+      while (ret == DB_LOCK_DEADLOCK);
+
+    }
+
+  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;
+  return (tempcdb);
 }
 
 
@@ -336,25 +596,34 @@ 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.
  * (There is guaranteed to be no more than one traversal in
- * progress per session at any given time.)
+ * progress per thread at any given time.)
  */
 void cdb_rewind(int cdb)
 {
        int ret = 0;
 
+       if (MYCURSORS[cdb] != NULL)
+               cclose(MYCURSORS[cdb]);
+
        /*
         * Now initialize the cursor
         */
-       begin_critical_section(S_DATABASE);
-       ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSOR, 0);
+       ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSORS[cdb], 0);
        if (ret) {
-               lprintf(1, "db_cursor: %s\n", db_strerror(ret));
+               lprintf(1, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
+               abort();
        }
-       end_critical_section(S_DATABASE);
 }
 
 
@@ -373,12 +642,19 @@ struct cdbdata *cdb_next_item(int cdb)
         memset(&data, 0, sizeof(data));
        data.flags = DB_DBT_MALLOC;
 
-       begin_critical_section(S_DATABASE);
-       ret = MYCURSOR->c_get(MYCURSOR,
+       ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb],
                &key, &data, DB_NEXT);
-       end_critical_section(S_DATABASE);
        
-       if (ret) return NULL;           /* presumably, end of file */
+       if (ret) {
+               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;
@@ -388,21 +664,50 @@ struct cdbdata *cdb_next_item(int cdb)
 }
 
 
+
+/*
+ * Truncate (delete every record)
+ */
+void cdb_trunc(int cdb) {
+       /* FIXME this needs to be implemented */
+}
+
+
 /*
  * Transaction-based stuff.  I'm writing this as I bake cookies...
  */
 
 void cdb_begin_transaction(void) {
 
-#ifdef TRANSACTION_BASED
-       txn_begin(dbenv, NULL, &MYTID, 0);
-#else
-       MYTID = NULL;
-#endif
+  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) {
-#ifdef TRANSACTION_BASED
-       txn_commit(MYTID, 0);
-#endif
+  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;
 }
+