summaryrefslogtreecommitdiffstats
path: root/src/kvstorage_bdb.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/kvstorage_bdb.c')
-rw-r--r--src/kvstorage_bdb.c121
1 files changed, 52 insertions, 69 deletions
diff --git a/src/kvstorage_bdb.c b/src/kvstorage_bdb.c
index 67143c3b4..7970291f6 100644
--- a/src/kvstorage_bdb.c
+++ b/src/kvstorage_bdb.c
@@ -25,8 +25,8 @@
#include "config.h"
#include "kvstorage.h"
#include "kvstorage_bdb.h"
-#include "main.h"
#include "util.h"
+#include "main.h"
#include <db.h>
struct bdb_op {
@@ -40,15 +40,15 @@ struct bdb_op {
/* Main bdb structure */
struct rspamd_bdb_backend {
- backend_init init_func; /*< this callback is called on kv storage initialization */
- backend_insert insert_func; /*< this callback is called when element is inserted */
- backend_replace replace_func; /*< this callback is called when element is replaced */
- backend_lookup lookup_func; /*< this callback is used for lookup of element */
- backend_delete delete_func; /*< this callback is called when an element is deleted */
- backend_sync sync_func; /*< this callback is called when backend need to be synced */
- backend_destroy destroy_func; /*< this callback is used for destroying all elements inside backend */
- DB_ENV *envp; /*< db environment */
- DB *dbp; /*< db pointer */
+ backend_init init_func; /*< this callback is called on kv storage initialization */
+ backend_insert insert_func; /*< this callback is called when element is inserted */
+ backend_replace replace_func; /*< this callback is called when element is replaced */
+ backend_lookup lookup_func; /*< this callback is used for lookup of element */
+ backend_delete delete_func; /*< this callback is called when an element is deleted */
+ backend_sync sync_func; /*< this callback is called when backend need to be synced */
+ backend_destroy destroy_func; /*< this callback is used for destroying all elements inside backend */
+ DB_ENV *envp; /*< db environment */
+ DB *dbp; /*< db pointer */
gchar *filename;
gchar *dirname;
guint sync_ops;
@@ -59,19 +59,16 @@ struct rspamd_bdb_backend {
/* Process single bdb operation */
static gboolean
-bdb_process_single_op (struct rspamd_bdb_backend *db,
- DB_TXN *txn,
- struct bdb_op *op)
+bdb_process_single_op (struct rspamd_bdb_backend *db, DB_TXN *txn, struct bdb_op *op)
{
- DBT db_key, db_data;
+ DBT db_key, db_data;
- memset (&db_key, 0, sizeof(DBT));
+ memset (&db_key, 0, sizeof(DBT));
memset (&db_data, 0, sizeof(DBT));
db_key.size = op->elt->keylen;
db_key.data = ELT_KEY (op->elt);
- db_data.size = op->elt->size + sizeof (struct rspamd_kv_element) +
- op->elt->keylen + 1;
+ db_data.size = op->elt->size + sizeof (struct rspamd_kv_element) + op->elt->keylen + 1;
db_data.data = op->elt;
switch (op->op) {
@@ -99,14 +96,14 @@ bdb_process_single_op (struct rspamd_bdb_backend *db,
static gboolean
bdb_process_queue (struct rspamd_kv_backend *backend)
{
- struct rspamd_bdb_backend *db = (struct rspamd_bdb_backend *)backend;
- struct bdb_op *op;
- GList *cur;
+ struct rspamd_bdb_backend *db = (struct rspamd_bdb_backend *)backend;
+ struct bdb_op *op;
+ GList *cur;
cur = db->ops_queue->head;
while (cur) {
op = cur->data;
- if (!bdb_process_single_op (db, NULL, op)) {
+ if (! bdb_process_single_op (db, NULL, op)) {
return FALSE;
}
cur = g_list_next (cur);
@@ -116,8 +113,7 @@ bdb_process_queue (struct rspamd_kv_backend *backend)
cur = db->ops_queue->head;
while (cur) {
op = cur->data;
- if (op->op == BDB_OP_DELETE || (op->elt->flags & KV_ELT_NEED_FREE) !=
- 0) {
+ if (op->op == BDB_OP_DELETE || (op->elt->flags & KV_ELT_NEED_FREE) != 0) {
/* Also clean memory */
g_slice_free1 (ELT_SIZE (op->elt), op->elt);
}
@@ -136,9 +132,9 @@ bdb_process_queue (struct rspamd_kv_backend *backend)
static void
rspamd_bdb_init (struct rspamd_kv_backend *backend)
{
- struct rspamd_bdb_backend *db = (struct rspamd_bdb_backend *)backend;
- guint32 flags;
- gint ret;
+ struct rspamd_bdb_backend *db = (struct rspamd_bdb_backend *)backend;
+ guint32 flags;
+ gint ret;
if ((ret = db_env_create (&db->envp, 0)) != 0) {
/* Cannot create environment */
@@ -146,9 +142,9 @@ rspamd_bdb_init (struct rspamd_kv_backend *backend)
}
flags = DB_INIT_MPOOL |
- DB_CREATE | /* Create the environment if it does not already exist. */
- DB_INIT_LOCK | /* Initialize locking. */
- DB_THREAD; /* Use threads */
+ DB_CREATE | /* Create the environment if it does not already exist. */
+ DB_INIT_LOCK | /* Initialize locking. */
+ DB_THREAD; /* Use threads */
if ((ret = db->envp->open (db->envp, db->dirname, flags, 0)) != 0) {
/* Cannot open environment */
@@ -172,9 +168,7 @@ rspamd_bdb_init (struct rspamd_kv_backend *backend)
goto err;
}
- if ((ret =
- db->dbp->open (db->dbp, NULL, db->filename, NULL, DB_HASH, flags,
- 0)) != 0) {
+ if ((ret = db->dbp->open (db->dbp, NULL, db->filename, NULL, DB_HASH, flags, 0)) != 0) {
goto err;
}
@@ -193,13 +187,10 @@ err:
}
static gboolean
-rspamd_bdb_insert (struct rspamd_kv_backend *backend,
- gpointer key,
- guint keylen,
- struct rspamd_kv_element *elt)
+rspamd_bdb_insert (struct rspamd_kv_backend *backend, gpointer key, guint keylen, struct rspamd_kv_element *elt)
{
- struct rspamd_bdb_backend *db = (struct rspamd_bdb_backend *)backend;
- struct bdb_op *op;
+ struct rspamd_bdb_backend *db = (struct rspamd_bdb_backend *)backend;
+ struct bdb_op *op;
if (!db->initialized) {
return FALSE;
@@ -213,8 +204,7 @@ rspamd_bdb_insert (struct rspamd_kv_backend *backend,
g_queue_push_head (db->ops_queue, op);
g_hash_table_insert (db->ops_hash, elt, op);
- if (db->sync_ops > 0 && g_queue_get_length (db->ops_queue) >=
- db->sync_ops) {
+ if (db->sync_ops > 0 && g_queue_get_length (db->ops_queue) >= db->sync_ops) {
return bdb_process_queue (backend);
}
@@ -222,13 +212,10 @@ rspamd_bdb_insert (struct rspamd_kv_backend *backend,
}
static gboolean
-rspamd_bdb_replace (struct rspamd_kv_backend *backend,
- gpointer key,
- guint keylen,
- struct rspamd_kv_element *elt)
+rspamd_bdb_replace (struct rspamd_kv_backend *backend, gpointer key, guint keylen, struct rspamd_kv_element *elt)
{
- struct rspamd_bdb_backend *db = (struct rspamd_bdb_backend *)backend;
- struct bdb_op *op;
+ struct rspamd_bdb_backend *db = (struct rspamd_bdb_backend *)backend;
+ struct bdb_op *op;
if (!db->initialized) {
return FALSE;
@@ -242,23 +229,21 @@ rspamd_bdb_replace (struct rspamd_kv_backend *backend,
g_queue_push_head (db->ops_queue, op);
g_hash_table_insert (db->ops_hash, elt, op);
- if (db->sync_ops > 0 && g_queue_get_length (db->ops_queue) >=
- db->sync_ops) {
+ if (db->sync_ops > 0 && g_queue_get_length (db->ops_queue) >= db->sync_ops) {
return bdb_process_queue (backend);
}
return TRUE;
}
-static struct rspamd_kv_element *
-rspamd_bdb_lookup (struct rspamd_kv_backend *backend, gpointer key,
- guint keylen)
+static struct rspamd_kv_element*
+rspamd_bdb_lookup (struct rspamd_kv_backend *backend, gpointer key, guint keylen)
{
- struct rspamd_bdb_backend *db = (struct rspamd_bdb_backend *)backend;
- struct bdb_op *op;
- DBT db_key, db_data;
- struct rspamd_kv_element *elt = NULL;
- struct rspamd_kv_element search_elt;
+ struct rspamd_bdb_backend *db = (struct rspamd_bdb_backend *)backend;
+ struct bdb_op *op;
+ DBT db_key, db_data;
+ struct rspamd_kv_element *elt = NULL;
+ struct rspamd_kv_element search_elt;
search_elt.keylen = keylen;
search_elt.p = key;
@@ -275,7 +260,7 @@ rspamd_bdb_lookup (struct rspamd_kv_backend *backend, gpointer key,
return op->elt;
}
- memset (&db_key, 0, sizeof(DBT));
+ memset (&db_key, 0, sizeof(DBT));
memset (&db_data, 0, sizeof(DBT));
db_key.size = keylen;
db_key.data = key;
@@ -290,13 +275,12 @@ rspamd_bdb_lookup (struct rspamd_kv_backend *backend, gpointer key,
}
static void
-rspamd_bdb_delete (struct rspamd_kv_backend *backend, gpointer key,
- guint keylen)
+rspamd_bdb_delete (struct rspamd_kv_backend *backend, gpointer key, guint keylen)
{
- struct rspamd_bdb_backend *db = (struct rspamd_bdb_backend *)backend;
- struct bdb_op *op;
- struct rspamd_kv_element *elt;
- struct rspamd_kv_element search_elt;
+ struct rspamd_bdb_backend *db = (struct rspamd_bdb_backend *)backend;
+ struct bdb_op *op;
+ struct rspamd_kv_element *elt;
+ struct rspamd_kv_element search_elt;
search_elt.keylen = keylen;
search_elt.p = key;
@@ -322,8 +306,7 @@ rspamd_bdb_delete (struct rspamd_kv_backend *backend, gpointer key,
g_queue_push_head (db->ops_queue, op);
g_hash_table_insert (db->ops_hash, elt, op);
- if (db->sync_ops > 0 && g_queue_get_length (db->ops_queue) >=
- db->sync_ops) {
+ if (db->sync_ops > 0 && g_queue_get_length (db->ops_queue) >= db->sync_ops) {
bdb_process_queue (backend);
}
@@ -333,7 +316,7 @@ rspamd_bdb_delete (struct rspamd_kv_backend *backend, gpointer key,
static void
rspamd_bdb_destroy (struct rspamd_kv_backend *backend)
{
- struct rspamd_bdb_backend *db = (struct rspamd_bdb_backend *)backend;
+ struct rspamd_bdb_backend *db = (struct rspamd_bdb_backend *)backend;
if (db->initialized) {
bdb_process_queue (backend);
@@ -355,9 +338,9 @@ rspamd_bdb_destroy (struct rspamd_kv_backend *backend)
struct rspamd_kv_backend *
rspamd_kv_bdb_new (const gchar *filename, guint sync_ops)
{
- struct rspamd_bdb_backend *new;
- struct stat st;
- gchar *dirname;
+ struct rspamd_bdb_backend *new;
+ struct stat st;
+ gchar *dirname;
if (filename == NULL) {
return NULL;