diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-07-26 10:49:23 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-07-26 10:49:23 +0100 |
commit | 537a7180a0d5132c11636c4fd8b1450cd99d352c (patch) | |
tree | fb9f8c84955a411bdffbd6371ea32f2716fb3687 /src/libstat/learn_cache/sqlite3_cache.c | |
parent | 5fd7a90fdaa33f52c59bdb0ca84451e5c1e22365 (diff) | |
download | rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.tar.gz rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.zip |
[Rework] Use clang-format to unify formatting in all sources
No meaningful changes.
Diffstat (limited to 'src/libstat/learn_cache/sqlite3_cache.c')
-rw-r--r-- | src/libstat/learn_cache/sqlite3_cache.c | 251 |
1 files changed, 116 insertions, 135 deletions
diff --git a/src/libstat/learn_cache/sqlite3_cache.c b/src/libstat/learn_cache/sqlite3_cache.c index 755114367..d8ad20ad2 100644 --- a/src/libstat/learn_cache/sqlite3_cache.c +++ b/src/libstat/learn_cache/sqlite3_cache.c @@ -25,13 +25,13 @@ #include "libutil/sqlite_utils.h" static const char *create_tables_sql = - "" - "CREATE TABLE IF NOT EXISTS learns(" - "id INTEGER PRIMARY KEY," - "flag INTEGER NOT NULL," - "digest TEXT NOT NULL);" - "CREATE UNIQUE INDEX IF NOT EXISTS d ON learns(digest);" - ""; + "" + "CREATE TABLE IF NOT EXISTS learns(" + "id INTEGER PRIMARY KEY," + "flag INTEGER NOT NULL," + "digest TEXT NOT NULL);" + "CREATE UNIQUE INDEX IF NOT EXISTS d ON learns(digest);" + ""; #define SQLITE_CACHE_PATH RSPAMD_DBDIR "/learn_cache.sqlite" @@ -47,64 +47,49 @@ enum rspamd_stat_sqlite3_stmt_idx { }; static struct rspamd_sqlite3_prstmt prepared_stmts[RSPAMD_STAT_CACHE_MAX] = -{ - { - .idx = RSPAMD_STAT_CACHE_TRANSACTION_START_IM, - .sql = "BEGIN IMMEDIATE TRANSACTION;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - }, - { - .idx = RSPAMD_STAT_CACHE_TRANSACTION_START_DEF, - .sql = "BEGIN DEFERRED TRANSACTION;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - }, - { - .idx = RSPAMD_STAT_CACHE_TRANSACTION_COMMIT, - .sql = "COMMIT;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - }, - { - .idx = RSPAMD_STAT_CACHE_TRANSACTION_ROLLBACK, - .sql = "ROLLBACK;", - .args = "", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - }, { - .idx = RSPAMD_STAT_CACHE_GET_LEARN, - .sql = "SELECT flag FROM learns WHERE digest=?1", - .args = "V", - .stmt = NULL, - .result = SQLITE_ROW, - .ret = "I" - }, - { - .idx = RSPAMD_STAT_CACHE_ADD_LEARN, - .sql = "INSERT INTO learns(digest, flag) VALUES (?1, ?2);", - .args = "VI", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - }, - { - .idx = RSPAMD_STAT_CACHE_UPDATE_LEARN, - .sql = "UPDATE learns SET flag=?1 WHERE digest=?2;", - .args = "IV", - .stmt = NULL, - .result = SQLITE_DONE, - .ret = "" - } -}; + {.idx = RSPAMD_STAT_CACHE_TRANSACTION_START_IM, + .sql = "BEGIN IMMEDIATE TRANSACTION;", + .args = "", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}, + {.idx = RSPAMD_STAT_CACHE_TRANSACTION_START_DEF, + .sql = "BEGIN DEFERRED TRANSACTION;", + .args = "", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}, + {.idx = RSPAMD_STAT_CACHE_TRANSACTION_COMMIT, + .sql = "COMMIT;", + .args = "", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}, + {.idx = RSPAMD_STAT_CACHE_TRANSACTION_ROLLBACK, + .sql = "ROLLBACK;", + .args = "", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}, + {.idx = RSPAMD_STAT_CACHE_GET_LEARN, + .sql = "SELECT flag FROM learns WHERE digest=?1", + .args = "V", + .stmt = NULL, + .result = SQLITE_ROW, + .ret = "I"}, + {.idx = RSPAMD_STAT_CACHE_ADD_LEARN, + .sql = "INSERT INTO learns(digest, flag) VALUES (?1, ?2);", + .args = "VI", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}, + {.idx = RSPAMD_STAT_CACHE_UPDATE_LEARN, + .sql = "UPDATE learns SET flag=?1 WHERE digest=?2;", + .args = "IV", + .stmt = NULL, + .result = SQLITE_DONE, + .ret = ""}}; struct rspamd_stat_sqlite3_ctx { sqlite3 *db; @@ -112,10 +97,10 @@ struct rspamd_stat_sqlite3_ctx { }; gpointer -rspamd_stat_cache_sqlite3_init (struct rspamd_stat_ctx *ctx, - struct rspamd_config *cfg, - struct rspamd_statfile *st, - const ucl_object_t *cf) +rspamd_stat_cache_sqlite3_init(struct rspamd_stat_ctx *ctx, + struct rspamd_config *cfg, + struct rspamd_statfile *st, + const ucl_object_t *cf) { struct rspamd_stat_sqlite3_ctx *new = NULL; const ucl_object_t *elt; @@ -125,35 +110,35 @@ rspamd_stat_cache_sqlite3_init (struct rspamd_stat_ctx *ctx, GError *err = NULL; if (cf) { - elt = ucl_object_lookup_any (cf, "path", "file", NULL); + elt = ucl_object_lookup_any(cf, "path", "file", NULL); if (elt != NULL) { - path = ucl_object_tostring (elt); + path = ucl_object_tostring(elt); } } - rspamd_snprintf (dbpath, sizeof (dbpath), "%s", path); + rspamd_snprintf(dbpath, sizeof(dbpath), "%s", path); - sqlite = rspamd_sqlite3_open_or_create (cfg->cfg_pool, - dbpath, create_tables_sql, 0, &err); + sqlite = rspamd_sqlite3_open_or_create(cfg->cfg_pool, + dbpath, create_tables_sql, 0, &err); if (sqlite == NULL) { - msg_err ("cannot open sqlite3 cache: %e", err); - g_error_free (err); + msg_err("cannot open sqlite3 cache: %e", err); + g_error_free(err); err = NULL; } else { - new = g_malloc0 (sizeof (*new)); + new = g_malloc0(sizeof(*new)); new->db = sqlite; - new->prstmt = rspamd_sqlite3_init_prstmt (sqlite, prepared_stmts, - RSPAMD_STAT_CACHE_MAX, &err); + new->prstmt = rspamd_sqlite3_init_prstmt(sqlite, prepared_stmts, + RSPAMD_STAT_CACHE_MAX, &err); if (new->prstmt == NULL) { - msg_err ("cannot open sqlite3 cache: %e", err); - g_error_free (err); + msg_err("cannot open sqlite3 cache: %e", err); + g_error_free(err); err = NULL; - sqlite3_close (sqlite); - g_free (new); + sqlite3_close(sqlite); + g_free(new); new = NULL; } } @@ -162,17 +147,16 @@ rspamd_stat_cache_sqlite3_init (struct rspamd_stat_ctx *ctx, } gpointer -rspamd_stat_cache_sqlite3_runtime (struct rspamd_task *task, - gpointer ctx, gboolean learn) +rspamd_stat_cache_sqlite3_runtime(struct rspamd_task *task, + gpointer ctx, gboolean learn) { /* No need of runtime for this type of classifier */ return ctx; } -gint -rspamd_stat_cache_sqlite3_check (struct rspamd_task *task, - gboolean is_spam, - gpointer runtime) +gint rspamd_stat_cache_sqlite3_check(struct rspamd_task *task, + gboolean is_spam, + gpointer runtime) { struct rspamd_stat_sqlite3_ctx *ctx = runtime; rspamd_cryptobox_hash_state_t st; @@ -188,41 +172,41 @@ rspamd_stat_cache_sqlite3_check (struct rspamd_task *task, } if (ctx != NULL && ctx->db != NULL) { - out = rspamd_mempool_alloc (task->task_pool, rspamd_cryptobox_HASHBYTES); + out = rspamd_mempool_alloc(task->task_pool, rspamd_cryptobox_HASHBYTES); - rspamd_cryptobox_hash_init (&st, NULL, 0); + rspamd_cryptobox_hash_init(&st, NULL, 0); - user = rspamd_mempool_get_variable (task->task_pool, "stat_user"); + user = rspamd_mempool_get_variable(task->task_pool, "stat_user"); /* Use dedicated hash space for per users cache */ if (user != NULL) { - rspamd_cryptobox_hash_update (&st, user, strlen (user)); + rspamd_cryptobox_hash_update(&st, user, strlen(user)); } - for (i = 0; i < task->tokens->len; i ++) { - tok = g_ptr_array_index (task->tokens, i); - rspamd_cryptobox_hash_update (&st, (guchar *)&tok->data, - sizeof (tok->data)); + for (i = 0; i < task->tokens->len; i++) { + tok = g_ptr_array_index(task->tokens, i); + rspamd_cryptobox_hash_update(&st, (guchar *) &tok->data, + sizeof(tok->data)); } - rspamd_cryptobox_hash_final (&st, out); + rspamd_cryptobox_hash_final(&st, out); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_START_DEF); - rc = rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_GET_LEARN, (gint64)rspamd_cryptobox_HASHBYTES, - out, &flag); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_START_DEF); + rc = rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_GET_LEARN, (gint64) rspamd_cryptobox_HASHBYTES, + out, &flag); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); /* Save hash into variables */ - rspamd_mempool_set_variable (task->task_pool, "words_hash", out, NULL); + rspamd_mempool_set_variable(task->task_pool, "words_hash", out, NULL); if (rc == SQLITE_OK) { /* We have some existing record in the table */ if (!!flag == !!is_spam) { /* Already learned */ - msg_warn_task ("already seen stat hash: %*bs", - rspamd_cryptobox_HASHBYTES, out); + msg_warn_task("already seen stat hash: %*bs", + rspamd_cryptobox_HASHBYTES, out); return RSPAMD_LEARN_IGNORE; } else { @@ -235,17 +219,16 @@ rspamd_stat_cache_sqlite3_check (struct rspamd_task *task, return RSPAMD_LEARN_OK; } -gint -rspamd_stat_cache_sqlite3_learn (struct rspamd_task *task, - gboolean is_spam, - gpointer runtime) +gint rspamd_stat_cache_sqlite3_learn(struct rspamd_task *task, + gboolean is_spam, + gpointer runtime) { struct rspamd_stat_sqlite3_ctx *ctx = runtime; gboolean unlearn = !!(task->flags & RSPAMD_TASK_FLAG_UNLEARN); guchar *h; gint64 flag; - h = rspamd_mempool_get_variable (task->task_pool, "words_hash"); + h = rspamd_mempool_get_variable(task->task_pool, "words_hash"); if (h == NULL) { return RSPAMD_LEARN_IGNORE; @@ -255,39 +238,37 @@ rspamd_stat_cache_sqlite3_learn (struct rspamd_task *task, if (!unlearn) { /* Insert result new id */ - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_START_IM); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_ADD_LEARN, - (gint64)rspamd_cryptobox_HASHBYTES, h, flag); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_START_IM); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_ADD_LEARN, + (gint64) rspamd_cryptobox_HASHBYTES, h, flag); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); } else { - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_START_IM); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_UPDATE_LEARN, - flag, - (gint64)rspamd_cryptobox_HASHBYTES, h); - rspamd_sqlite3_run_prstmt (task->task_pool, ctx->db, ctx->prstmt, - RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_START_IM); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_UPDATE_LEARN, + flag, + (gint64) rspamd_cryptobox_HASHBYTES, h); + rspamd_sqlite3_run_prstmt(task->task_pool, ctx->db, ctx->prstmt, + RSPAMD_STAT_CACHE_TRANSACTION_COMMIT); } - rspamd_sqlite3_sync (ctx->db, NULL, NULL); + rspamd_sqlite3_sync(ctx->db, NULL, NULL); return RSPAMD_LEARN_OK; } -void -rspamd_stat_cache_sqlite3_close (gpointer c) +void rspamd_stat_cache_sqlite3_close(gpointer c) { - struct rspamd_stat_sqlite3_ctx *ctx = (struct rspamd_stat_sqlite3_ctx *)c; + struct rspamd_stat_sqlite3_ctx *ctx = (struct rspamd_stat_sqlite3_ctx *) c; if (ctx != NULL) { - rspamd_sqlite3_close_prstmt (ctx->db, ctx->prstmt); - sqlite3_close (ctx->db); - g_free (ctx); + rspamd_sqlite3_close_prstmt(ctx->db, ctx->prstmt); + sqlite3_close(ctx->db); + g_free(ctx); } - } |