From: Vsevolod Stakhov Date: Wed, 18 Feb 2015 12:56:43 +0000 (+0000) Subject: Allow sqlite3 cache customization. X-Git-Tag: 0.9.0~673 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fbf727498eefbe38d6d439b1b315f290e58aa051;p=thirdparty%2Frspamd.git Allow sqlite3 cache customization. --- diff --git a/src/libstat/learn_cache/learn_cache.h b/src/libstat/learn_cache/learn_cache.h index 51b2a9dbd0..273e906f50 100644 --- a/src/libstat/learn_cache/learn_cache.h +++ b/src/libstat/learn_cache/learn_cache.h @@ -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_ */ diff --git a/src/libstat/learn_cache/sqlite3_cache.c b/src/libstat/learn_cache/sqlite3_cache.c index 8d89225427..d8c904d904 100644 --- a/src/libstat/learn_cache/sqlite3_cache.c +++ b/src/libstat/learn_cache/sqlite3_cache.c @@ -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); + } + +} diff --git a/src/libstat/stat_config.c b/src/libstat/stat_config.c index 574d25fd39..b8ad6ec30c 100644 --- a/src/libstat/stat_config.c +++ b/src/libstat/stat_config.c @@ -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 } };