]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
Allow sqlite3 cache customization.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 18 Feb 2015 12:56:43 +0000 (12:56 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 18 Feb 2015 12:56:43 +0000 (12:56 +0000)
src/libstat/learn_cache/learn_cache.h
src/libstat/learn_cache/sqlite3_cache.c
src/libstat/stat_config.c

index 51b2a9dbd0cc4a1dbd39efe4c780a66036135f8f..273e906f5001a0232dfabea75831522942a1f004 100644 (file)
@@ -40,6 +40,7 @@ struct rspamd_stat_cache {
        gint (*process)(struct rspamd_task *task,
                        gboolean is_spam,
                        gpointer ctx);
+       void (*close) (gpointer ctx);
        gpointer ctx;
 };
 
@@ -48,5 +49,6 @@ gpointer rspamd_stat_cache_sqlite3_init(struct rspamd_stat_ctx *ctx,
 gint rspamd_stat_cache_sqlite3_process (
                struct rspamd_task *task,
                gboolean is_spam, gpointer c);
+void rspamd_stat_cache_sqlite3_close (gpointer c);
 
 #endif /* LEARN_CACHE_H_ */
index 8d892254276652b55df19065c574436e9fce418c..d8c904d904dbc24c912720c793ee0ba78d8f51b5 100644 (file)
@@ -53,12 +53,14 @@ rspamd_stat_cache_sqlite3_init(struct rspamd_stat_ctx *ctx,
 {
        struct rspamd_stat_sqlite3_ctx *new = NULL;
        struct rspamd_classifier_config *clf;
-       const ucl_object_t *obj;
+       const ucl_object_t *obj, *elt;
        GList *cur;
+       gchar dbpath[PATH_MAX];
        sqlite3 *sqlite;
        gboolean has_sqlite_cache = FALSE;
        gint rc;
 
+       rspamd_snprintf (dbpath, sizeof (dbpath), SQLITE_CACHE_PATH);
        cur = cfg->classifiers;
 
        while (cur) {
@@ -67,17 +69,30 @@ rspamd_stat_cache_sqlite3_init(struct rspamd_stat_ctx *ctx,
                obj = ucl_object_find_key (clf->opts, "cache");
 
                /* Sqlite3 cache is the default learn cache method */
-               if (obj == NULL || g_ascii_strcasecmp (ucl_object_tostring (obj),
-                               "sqlite3") == 0) {
+               if (obj == NULL) {
                        has_sqlite_cache = TRUE;
                        break;
                }
+               else if (ucl_object_type (obj) == UCL_OBJECT) {
+                       elt = ucl_object_find_key (obj, "name");
+
+                       if (ucl_object_type (elt) == UCL_STRING &&
+                               g_ascii_strcasecmp (ucl_object_tostring (elt), "sqlite3") == 0) {
+
+                               has_sqlite_cache = TRUE;
+                               elt = ucl_object_find_key (obj, "path");
+                               if (elt != NULL && ucl_object_type (elt) == UCL_STRING) {
+                                       rspamd_snprintf (dbpath, sizeof (dbpath), "%s",
+                                                       ucl_object_tostring (elt));
+                               }
+                       }
+               }
 
                cur = g_list_next (cur);
        }
 
        if (has_sqlite_cache) {
-               if ((rc = sqlite3_open_v2 (SQLITE_CACHE_PATH, &sqlite,
+               if ((rc = sqlite3_open_v2 (dbpath, &sqlite,
                                SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_NOMUTEX, NULL))
                                != SQLITE_OK) {
                        msg_err ("Cannot open sqlite db %s: %s", SQLITE_CACHE_PATH,
@@ -202,3 +217,15 @@ rspamd_stat_cache_sqlite3_process (struct rspamd_task *task,
 
        return RSPAMD_LEARN_OK;
 }
+
+void
+rspamd_stat_cache_sqlite3_close (gpointer c)
+{
+       struct rspamd_stat_sqlite3_ctx *ctx = (struct rspamd_stat_sqlite3_ctx *)c;
+
+       if (ctx != NULL) {
+               sqlite3_close (ctx->db);
+               g_slice_free1 (sizeof (*ctx), ctx);
+       }
+
+}
index 574d25fd390b1e8891da22cc2df9305bbf88200c..b8ad6ec30ca9f0059efee47c71edd569dadf594b 100644 (file)
@@ -61,7 +61,8 @@ static struct rspamd_stat_cache stat_caches[] = {
        {
                .name = RSPAMD_DEFAULT_CACHE,
                .init = rspamd_stat_cache_sqlite3_init,
-               .process = rspamd_stat_cache_sqlite3_process
+               .process = rspamd_stat_cache_sqlite3_process,
+               .close = rspamd_stat_cache_sqlite3_close
        }
 };