diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-06-19 15:25:10 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-06-19 15:25:10 +0100 |
commit | 45c538da92ef6482a6e276be8064321c0636aff4 (patch) | |
tree | 34f0fb94e604da018dd8fcfb68d119e91c59a881 /src/libstat/backends | |
parent | 37117c9d9884e2293373aea028e33eaaedf4b6a5 (diff) | |
download | rspamd-45c538da92ef6482a6e276be8064321c0636aff4.tar.gz rspamd-45c538da92ef6482a6e276be8064321c0636aff4.zip |
Implement statistics closing.
Diffstat (limited to 'src/libstat/backends')
-rw-r--r-- | src/libstat/backends/backends.h | 4 | ||||
-rw-r--r-- | src/libstat/backends/mmaped_file.c | 43 | ||||
-rw-r--r-- | src/libstat/backends/sqlite3_backend.c | 31 |
3 files changed, 57 insertions, 21 deletions
diff --git a/src/libstat/backends/backends.h b/src/libstat/backends/backends.h index 8180e1baf..18f4fce26 100644 --- a/src/libstat/backends/backends.h +++ b/src/libstat/backends/backends.h @@ -54,6 +54,7 @@ struct rspamd_stat_backend { gulong (*inc_learns)(struct rspamd_statfile_runtime *runtime, gpointer ctx); gulong (*dec_learns)(struct rspamd_statfile_runtime *runtime, gpointer ctx); ucl_object_t* (*get_stat)(struct rspamd_statfile_runtime *runtime, gpointer ctx); + void (*close)(gpointer ctx); gpointer ctx; }; @@ -79,7 +80,8 @@ struct rspamd_stat_backend { gulong rspamd_##name##_learns (struct rspamd_statfile_runtime *runtime, \ gpointer ctx); \ ucl_object_t * rspamd_##name##_get_stat (struct rspamd_statfile_runtime *runtime, \ - gpointer ctx) + gpointer ctx); \ + void rspamd_##name##_close (gpointer ctx) RSPAMD_STAT_BACKEND_DEF(mmaped_file); RSPAMD_STAT_BACKEND_DEF(redis); diff --git a/src/libstat/backends/mmaped_file.c b/src/libstat/backends/mmaped_file.c index 70452378f..b4d60ad0d 100644 --- a/src/libstat/backends/mmaped_file.c +++ b/src/libstat/backends/mmaped_file.c @@ -638,7 +638,7 @@ rspamd_mmaped_file_open (rspamd_mmaped_file_ctx * pool, } gint -rspamd_mmaped_file_close (rspamd_mmaped_file_ctx * pool, +rspamd_mmaped_file_close_file (rspamd_mmaped_file_ctx * pool, rspamd_mmaped_file_t * file) { rspamd_mmaped_file_t *pos; @@ -797,23 +797,6 @@ rspamd_mmaped_file_create (rspamd_mmaped_file_ctx * pool, const gchar *filename, return 0; } -void -rspamd_mmaped_file_destroy (rspamd_mmaped_file_ctx * pool) -{ - GHashTableIter it; - gpointer k, v; - rspamd_mmaped_file_t *f; - - g_hash_table_iter_init (&it, pool->files); - while (g_hash_table_iter_next (&it, &k, &v)) { - f = (rspamd_mmaped_file_t *)v; - rspamd_mmaped_file_close (pool, f); - } - - g_hash_table_destroy (pool->files); - rspamd_mempool_delete (pool->pool); -} - gpointer rspamd_mmaped_file_init (struct rspamd_stat_ctx *ctx, struct rspamd_config *cfg) { @@ -883,6 +866,30 @@ rspamd_mmaped_file_init (struct rspamd_stat_ctx *ctx, struct rspamd_config *cfg) return (gpointer)new; } +void +rspamd_mmaped_file_close (gpointer p) +{ + rspamd_mmaped_file_ctx *ctx = (rspamd_mmaped_file_ctx *)p; + GHashTableIter it; + gpointer k, v; + rspamd_mmaped_file_t *mf; + + g_assert (ctx != NULL); + + rspamd_mempool_lock_mutex (ctx->lock); + g_hash_table_iter_init (&it, ctx->files); + + while (g_hash_table_iter_next (&it, &k, &v)) { + mf = v; + rspamd_mmaped_file_close_file (ctx, mf); + } + + g_hash_table_unref (ctx->files); + rspamd_mempool_unlock_mutex (ctx->lock); + + rspamd_mempool_delete (ctx->pool); +} + gpointer rspamd_mmaped_file_runtime (struct rspamd_task *task, struct rspamd_statfile_config *stcf, diff --git a/src/libstat/backends/sqlite3_backend.c b/src/libstat/backends/sqlite3_backend.c index 43c52483f..e5471b436 100644 --- a/src/libstat/backends/sqlite3_backend.c +++ b/src/libstat/backends/sqlite3_backend.c @@ -63,7 +63,7 @@ static const char *create_tables_sql = ");" "CREATE UNIQUE INDEX IF NOT EXISTS un ON users(name);" "CREATE UNIQUE INDEX IF NOT EXISTS ln ON languages(name);" - "PRAGMA user_version=" SQLITE3_SCHEMA_VERSION + "PRAGMA user_version=" SQLITE3_SCHEMA_VERSION ";" "INSERT INTO users(id, name, learns) VALUES(0, '" SQLITE3_DEFAULT "',0);" "INSERT INTO languages(id, name, learns) VALUES(0, '" SQLITE3_DEFAULT "',0);" "COMMIT;"; @@ -302,7 +302,7 @@ rspamd_sqlite3_init (struct rspamd_stat_ctx *ctx, while (curst) { stf = curst->data; - if (strcmp (stf->backend, SQLITE3_BACKEND_TYPE) == 0) { + if (stf->backend && strcmp (stf->backend, SQLITE3_BACKEND_TYPE) == 0) { /* * Check configuration sanity */ @@ -349,6 +349,33 @@ rspamd_sqlite3_init (struct rspamd_stat_ctx *ctx, return (gpointer)new; } +void +rspamd_sqlite3_close (gpointer p) +{ + struct rspamd_stat_sqlite3_ctx *ctx = p; + struct rspamd_stat_sqlite3_db *bk; + GHashTableIter it; + gpointer k, v; + + g_hash_table_iter_init (&it, ctx->files); + + while (g_hash_table_iter_next (&it, &k, &v)) { + bk = v; + + if (bk->sqlite) { + if (bk->in_transaction) { + rspamd_sqlite3_run_prstmt (bk, RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT); + } + + sqlite3_close (bk->sqlite); + rspamd_sqlite3_close_prstmt (bk->prstmt); + g_slice_free1 (sizeof (*bk), bk); + } + } + + g_hash_table_destroy (ctx->files); +} + gpointer rspamd_sqlite3_runtime (struct rspamd_task *task, struct rspamd_statfile_config *stcf, gboolean learn, gpointer p) |