aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstat/backends
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-07-27 16:58:08 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-07-27 16:58:08 +0100
commitc2d505b2e459acf34fb4df1f9ed9f331571afa35 (patch)
treea2904ead306a61ac0d301085a37c5870c5105f04 /src/libstat/backends
parent7190316de556bff8a0cf0a905d43af5a748d65ba (diff)
downloadrspamd-c2d505b2e459acf34fb4df1f9ed9f331571afa35.tar.gz
rspamd-c2d505b2e459acf34fb4df1f9ed9f331571afa35.zip
Fix tokenizers configuration in sqlite3.
Diffstat (limited to 'src/libstat/backends')
-rw-r--r--src/libstat/backends/sqlite3_backend.c72
1 files changed, 67 insertions, 5 deletions
diff --git a/src/libstat/backends/sqlite3_backend.c b/src/libstat/backends/sqlite3_backend.c
index ca88a3477..617dd99bb 100644
--- a/src/libstat/backends/sqlite3_backend.c
+++ b/src/libstat/backends/sqlite3_backend.c
@@ -55,6 +55,7 @@ struct rspamd_stat_sqlite3_rt {
static const char *create_tables_sql =
"BEGIN IMMEDIATE;"
+ "CREATE TABLE tokenizer(data BLOB);"
"CREATE TABLE users("
"id INTEGER PRIMARY KEY,"
"name TEXT,"
@@ -95,6 +96,8 @@ enum rspamd_stat_sqlite3_stmt_idx {
RSPAMD_STAT_BACKEND_GET_USER,
RSPAMD_STAT_BACKEND_INSERT_LANGUAGE,
RSPAMD_STAT_BACKEND_INSERT_USER,
+ RSPAMD_STAT_BACKEND_SAVE_TOKENIZER,
+ RSPAMD_STAT_BACKEND_LOAD_TOKENIZER,
RSPAMD_STAT_BACKEND_MAX
};
@@ -210,6 +213,22 @@ static struct rspamd_sqlite3_prstmt prepared_stmts[RSPAMD_STAT_BACKEND_MAX] =
.args = "T",
.result = SQLITE_ROW,
.ret = "L"
+ },
+ {
+ .idx = RSPAMD_STAT_BACKEND_SAVE_TOKENIZER,
+ .sql = "INSERT INTO tokenizer(data) VALUES (?1);",
+ .stmt = NULL,
+ .args = "B",
+ .result = SQLITE_ROW,
+ .ret = ""
+ },
+ {
+ .idx = RSPAMD_STAT_BACKEND_LOAD_TOKENIZER,
+ .sql = "SELECT data FROM tokenizer;",
+ .stmt = NULL,
+ .args = "",
+ .result = SQLITE_ROW,
+ .ret = "B"
}
};
@@ -311,10 +330,15 @@ rspamd_sqlite3_get_language (struct rspamd_stat_sqlite3_db *db,
}
static struct rspamd_stat_sqlite3_db *
-rspamd_sqlite3_opendb (const gchar *path, const ucl_object_t *opts,
+rspamd_sqlite3_opendb (rspamd_mempool_t *pool,
+ struct rspamd_statfile_config *stcf,
+ const gchar *path, const ucl_object_t *opts,
gboolean create, GError **err)
{
struct rspamd_stat_sqlite3_db *bk;
+ struct rspamd_stat_tokenizer *tokenizer;
+ gpointer tk_conf;
+ guint64 sz;
bk = g_slice_alloc0 (sizeof (*bk));
bk->sqlite = rspamd_sqlite3_open_or_create (path, create_tables_sql, err);
@@ -335,6 +359,26 @@ rspamd_sqlite3_opendb (const gchar *path, const ucl_object_t *opts,
return NULL;
}
+ /* Check tokenizer configuration */
+ if (rspamd_sqlite3_run_prstmt (bk->sqlite, bk->prstmt,
+ RSPAMD_STAT_BACKEND_LOAD_TOKENIZER, &sz, &tk_conf) != SQLITE_OK) {
+ g_assert (stcf->clcf->tokenizer != NULL);
+ tokenizer = rspamd_stat_get_tokenizer (stcf->clcf->tokenizer->name);
+ g_assert (tokenizer != NULL);
+ tk_conf = tokenizer->get_config (pool, stcf->clcf->tokenizer, (gsize *)&sz);
+
+ if (!rspamd_sqlite3_run_prstmt (bk->sqlite, bk->prstmt,
+ RSPAMD_STAT_BACKEND_SAVE_TOKENIZER, sz, tk_conf) != SQLITE_OK) {
+ sqlite3_close (bk->sqlite);
+ g_slice_free1 (sizeof (*bk), bk);
+
+ return NULL;
+ }
+ }
+ else {
+ g_free (tk_conf);
+ }
+
return bk;
}
@@ -379,8 +423,8 @@ rspamd_sqlite3_init (struct rspamd_stat_ctx *ctx,
filename = ucl_object_tostring (filenameo);
- if ((bk = rspamd_sqlite3_opendb (filename, stf->opts, TRUE,
- &err)) == NULL) {
+ if ((bk = rspamd_sqlite3_opendb (cfg->cfg_pool, stf, filename,
+ stf->opts, TRUE, &err)) == NULL) {
msg_err ("cannot open sqlite3 db: %e", err);
}
@@ -694,6 +738,24 @@ gpointer
rspamd_sqlite3_load_tokenizer_config (gpointer runtime,
gsize *len)
{
- /* TODO: unbreak */
- return NULL;
+ gpointer tk_conf, copied_conf;
+ guint64 sz;
+ struct rspamd_stat_sqlite3_rt *rt = runtime;
+ struct rspamd_stat_sqlite3_db *bk;
+
+ g_assert (rt != NULL);
+ bk = rt->db;
+
+ g_assert (rspamd_sqlite3_run_prstmt (bk->sqlite, bk->prstmt,
+ RSPAMD_STAT_BACKEND_LOAD_TOKENIZER, &sz, &tk_conf) == SQLITE_OK);
+ g_assert (sz > 0);
+ copied_conf = rspamd_mempool_alloc (rt->task->task_pool, sz);
+ memcpy (copied_conf, tk_conf, sz);
+ g_free (tk_conf);
+
+ if (*len) {
+ *len = sz;
+ }
+
+ return copied_conf;
}