]> 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 45cff4dc2e30c46c9a679b5a1ebac9e6ad67b50e..ae9c5551b52b9e55533335618820a98fdae3eac0 100644 (file)
@@ -35,6 +35,8 @@
 
 #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
@@ -47,6 +49,7 @@
 #include "dynloader.h"
 #include "citserver.h"
 #include "database.h"
+#include "msgbase.h"
 #include "sysdep_decls.h"
 
 static DB *dbp[MAXCDB];                /* One DB handle for each Citadel database */
@@ -57,6 +60,10 @@ struct cdbtsd {                      /* Thread-specific DB stuff */
        DBC *cursors[MAXCDB];   /* Cursors, for traversals... */
 };
 
+#ifdef HAVE_ZLIB
+#include <zlib.h>
+#endif
+
 static pthread_key_t tsdkey;
 
 #define MYCURSORS      (((struct cdbtsd*)pthread_getspecific(tsdkey))->cursors)
@@ -185,6 +192,7 @@ 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");
 
@@ -206,7 +214,19 @@ static void cdb_cull_logs(void) {
        if (list != NULL) {
                for (file = list; *file != NULL; ++file) {
                        lprintf(9, "Deleting log: %s\n", *file);
-                       unlink(*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);
        }
@@ -261,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.
@@ -317,7 +341,7 @@ 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,
@@ -380,6 +404,50 @@ void close_databases(void)
         }
 }
 
+
+/*
+ * 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.
@@ -392,6 +460,14 @@ int cdb_store(int cdb,
   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));
@@ -399,6 +475,36 @@ int cdb_store(int cdb,
   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)
     {
@@ -413,6 +519,9 @@ int cdb_store(int cdb,
                  db_strerror(ret));
          abort();
        }
+#ifdef HAVE_ZLIB
+      if (compressing) phree(compressed_data);
+#endif
       return ret;
       
     }
@@ -444,6 +553,9 @@ int cdb_store(int cdb,
       else
        {
          txcommit(tid);
+#ifdef HAVE_ZLIB
+         if (compressing) phree(compressed_data);
+#endif
          return ret;
        }
     }
@@ -582,6 +694,7 @@ struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
 
   tempcdb->len = dret.size;
   tempcdb->ptr = dret.data;
+  cdb_decompress_if_necessary(tempcdb);
   return (tempcdb);
 }
 
@@ -659,11 +772,13 @@ struct cdbdata *cdb_next_item(int cdb)
        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...
  */
@@ -702,3 +817,100 @@ void cdb_end_transaction(void) {
   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
+    }
+}