bdb_decompress_if_necessary() now operates on DBT instead of cdbdata
authorArt Cancro <ajc@citadel.org>
Mon, 21 Aug 2023 19:09:45 +0000 (10:09 -0900)
committerArt Cancro <ajc@citadel.org>
Mon, 21 Aug 2023 19:09:45 +0000 (10:09 -0900)
This allows us to maintain the DB_DBT_REALLOC heuristics.

citadel/server/backends/berkeley_db/berkeley_db.c
citadel/server/server.h

index 529d4c60ade74a491687e7489a765a7751b2adf2..60063099e47cd3453a053b1fab5935ec563f0e03 100644 (file)
@@ -327,12 +327,11 @@ void bdb_close_databases(void) {
 }
 
 
-// Decompress a database item if it was compressed on disk
-void bdb_decompress_if_necessary(struct cdbdata *cdb) {
+// Decompress a DBT data block if it was compressed on disk.
+void bdb_decompress_if_necessary(DBT *d) {
        static int magic = COMPRESS_MAGIC;
 
-       if ((cdb == NULL) || (cdb->ptr == NULL) || (cdb->len < sizeof(magic)) || (memcmp(cdb->ptr, &magic, sizeof(magic)))) {
-               cdb->cdbfree_must_free_ptr = 0;
+       if ((d == NULL) || (d->data == NULL) || (d->size < sizeof(magic)) || (memcmp(d->data, &magic, sizeof(magic)))) {
                return;
        }
 
@@ -346,12 +345,12 @@ void bdb_decompress_if_necessary(struct cdbdata *cdb) {
 
        memset(&zheader, 0, sizeof(struct CtdlCompressHeader));
        cplen = sizeof(struct CtdlCompressHeader);
-       if (sizeof(struct CtdlCompressHeader) > cdb->len) {
-               cplen = cdb->len;
+       if (sizeof(struct CtdlCompressHeader) > d->size) {
+               cplen = d->size;
        }
-       memcpy(&zheader, cdb->ptr, cplen);
+       memcpy(&zheader, d->data, cplen);
 
-       compressed_data = cdb->ptr;
+       compressed_data = d->data;
        compressed_data += sizeof(struct CtdlCompressHeader);
 
        sourceLen = (uLongf) zheader.compressed_len;
@@ -364,10 +363,9 @@ void bdb_decompress_if_necessary(struct cdbdata *cdb) {
                bdb_abort();
        }
 
-       //free(cdb->ptr);
-       cdb->len = (size_t) destLen;
-       cdb->ptr = uncompressed_data;
-       cdb->cdbfree_must_free_ptr = 1;
+       free(d->data);
+       d->size = (size_t) destLen;
+       d->data = uncompressed_data;
 }
 
 
@@ -562,6 +560,8 @@ struct cdbdata *bdb_fetch(int cdb, const void *key, int keylen) {
                return NULL;
        }
 
+       bdb_decompress_if_necessary(&TSD->dbdata[cdb]);
+
        tempcdb = (struct cdbdata *) malloc(sizeof(struct cdbdata));
        if (tempcdb == NULL) {
                syslog(LOG_ERR, "bdb: bdb_fetch() cannot allocate memory for tempcdb: %m");
@@ -570,7 +570,6 @@ struct cdbdata *bdb_fetch(int cdb, const void *key, int keylen) {
        else {
                tempcdb->len = TSD->dbdata[cdb].size;
                tempcdb->ptr = TSD->dbdata[cdb].data;
-               bdb_decompress_if_necessary(tempcdb);
                return (tempcdb);
        }
 }
@@ -578,9 +577,6 @@ struct cdbdata *bdb_fetch(int cdb, const void *key, int keylen) {
 
 // Free a cdbdata item.
 void bdb_free(struct cdbdata *cdb) {
-       if (cdb->cdbfree_must_free_ptr) {
-               free(cdb->ptr);
-       }
        free(cdb);
 }
 
@@ -635,10 +631,11 @@ struct cdbdata *bdb_next_item(int cdb) {
                return NULL;    // presumably, we are at the end
        }
 
+       bdb_decompress_if_necessary(&TSD->dbdata[cdb]);
+
        cdbret = (struct cdbdata *) malloc(sizeof(struct cdbdata));
        cdbret->len = TSD->dbdata[cdb].size;
        cdbret->ptr = TSD->dbdata[cdb].data;
-       bdb_decompress_if_necessary(cdbret);
 
        return (cdbret);
 }
index b16289bf6baaf6d49d39cd428e7fa836b2a3ef80..c628df2a18e09e49cf28a8685c6e4278a847ede5 100644 (file)
@@ -70,7 +70,6 @@ struct ExpressMessage {
 struct cdbdata {
        size_t len;                     // size of datum pointed to by ptr
        char *ptr;                      // datum
-       int cdbfree_must_free_ptr;      // nonzero if cdb_free() is expected to free(ptr)
 };