aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-05-13 15:58:01 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-05-13 15:58:01 +0100
commit25b777b360662a391454f64346589dbec792831f (patch)
tree2179566b59a65ae27500c2e4e9dcae7149d60486 /src
parentbbbb8d029cf889fd1c770341094417d834cab9e0 (diff)
downloadrspamd-25b777b360662a391454f64346589dbec792831f.tar.gz
rspamd-25b777b360662a391454f64346589dbec792831f.zip
[Feature] Add versions to fuzzy storage
Diffstat (limited to 'src')
-rw-r--r--src/fuzzy_storage.c6
-rw-r--r--src/libserver/fuzzy_backend.c46
-rw-r--r--src/libserver/fuzzy_backend.h4
3 files changed, 50 insertions, 6 deletions
diff --git a/src/fuzzy_storage.c b/src/fuzzy_storage.c
index f43cb3f44..7803307b0 100644
--- a/src/fuzzy_storage.c
+++ b/src/fuzzy_storage.c
@@ -209,6 +209,7 @@ rspamd_fuzzy_process_updates_queue (struct rspamd_fuzzy_storage_ctx *ctx)
struct rspamd_fuzzy_cmd *cmd;
gpointer ptr;
guint nupdates = 0;
+ time_t now = time (NULL);
if (ctx->updates_pending &&
g_queue_get_length (ctx->updates_pending) > 0 &&
@@ -227,7 +228,7 @@ rspamd_fuzzy_process_updates_queue (struct rspamd_fuzzy_storage_ctx *ctx)
}
if (cmd->cmd == FUZZY_WRITE) {
- rspamd_fuzzy_backend_add (ctx->backend, ptr);
+ rspamd_fuzzy_backend_add (ctx->backend, ptr, now);
}
else {
rspamd_fuzzy_backend_del (ctx->backend, ptr);
@@ -248,7 +249,8 @@ rspamd_fuzzy_process_updates_queue (struct rspamd_fuzzy_storage_ctx *ctx)
}
g_queue_clear (ctx->updates_pending);
- msg_info ("updated fuzzy storage: %ud updates processed", nupdates);
+ msg_info ("updated fuzzy storage: %ud updates processed, version: %d",
+ nupdates, rspamd_fuzzy_backend_version (ctx->backend));
}
else {
msg_err ("cannot commit update transaction to fuzzy backend, "
diff --git a/src/libserver/fuzzy_backend.c b/src/libserver/fuzzy_backend.c
index a13d27f09..26e595e5f 100644
--- a/src/libserver/fuzzy_backend.c
+++ b/src/libserver/fuzzy_backend.c
@@ -93,6 +93,7 @@ enum rspamd_fuzzy_statement_idx {
RSPAMD_FUZZY_BACKEND_EXPIRE,
RSPAMD_FUZZY_BACKEND_VACUUM,
RSPAMD_FUZZY_BACKEND_DELETE_ORPHANED,
+ RSPAMD_FUZZY_BACKEND_VERSION,
RSPAMD_FUZZY_BACKEND_MAX
};
static struct rspamd_fuzzy_stmts {
@@ -212,6 +213,13 @@ static struct rspamd_fuzzy_stmts {
.stmt = NULL,
.result = SQLITE_DONE
},
+ {
+ .idx = RSPAMD_FUZZY_BACKEND_VERSION,
+ .sql = "PRAGMA user_version;",
+ .args = "",
+ .stmt = NULL,
+ .result = SQLITE_ROW
+ },
};
static GQuark
@@ -624,7 +632,8 @@ rspamd_fuzzy_backend_prepare_update (struct rspamd_fuzzy_backend *backend)
gboolean
rspamd_fuzzy_backend_add (struct rspamd_fuzzy_backend *backend,
- const struct rspamd_fuzzy_cmd *cmd)
+ const struct rspamd_fuzzy_cmd *cmd,
+ time_t timestamp)
{
int rc, i;
gint64 id, flag;
@@ -680,7 +689,7 @@ rspamd_fuzzy_backend_add (struct rspamd_fuzzy_backend *backend,
(gint) cmd->flag,
cmd->digest,
(gint64) cmd->value,
- (gint64) time (NULL));
+ (gint64) timestamp);
if (rc == SQLITE_OK) {
if (cmd->shingles_count > 0) {
@@ -722,7 +731,9 @@ rspamd_fuzzy_backend_add (struct rspamd_fuzzy_backend *backend,
gboolean
rspamd_fuzzy_backend_finish_update (struct rspamd_fuzzy_backend *backend)
{
- gint rc, wal_frames, wal_checkpointed;
+ gint rc, wal_frames, wal_checkpointed, ver;
+ gint64 version = 0;
+ gchar version_buf[128];
rc = rspamd_fuzzy_backend_run_stmt (backend, TRUE,
RSPAMD_FUZZY_BACKEND_TRANSACTION_COMMIT);
@@ -745,6 +756,17 @@ rspamd_fuzzy_backend_finish_update (struct rspamd_fuzzy_backend *backend)
}
}
+ /* Get and update version */
+ ver = rspamd_fuzzy_backend_version (backend);
+ ++ver;
+ rspamd_snprintf (version_buf, sizeof (version_buf), "PRAGMA user_version=%d;",
+ ver);
+
+ if (sqlite3_exec (backend->db, version_buf, NULL, NULL, NULL) != SQLITE_OK) {
+ msg_err_fuzzy_backend ("cannot set database version to %L: %s",
+ version, sqlite3_errmsg (backend->db));
+ }
+
return TRUE;
}
@@ -953,6 +975,24 @@ rspamd_fuzzy_backend_count (struct rspamd_fuzzy_backend *backend)
return 0;
}
+gint
+rspamd_fuzzy_backend_version (struct rspamd_fuzzy_backend *backend)
+{
+ gint ret = 0;
+
+ if (backend) {
+ if (rspamd_fuzzy_backend_run_stmt (backend, FALSE,
+ RSPAMD_FUZZY_BACKEND_VERSION) == SQLITE_OK) {
+ ret = sqlite3_column_int64 (
+ prepared_stmts[RSPAMD_FUZZY_BACKEND_VERSION].stmt, 0);
+ }
+
+ rspamd_fuzzy_backend_cleanup_stmt (backend, RSPAMD_FUZZY_BACKEND_VERSION);
+ }
+
+ return ret;
+}
+
gsize
rspamd_fuzzy_backend_expired (struct rspamd_fuzzy_backend *backend)
{
diff --git a/src/libserver/fuzzy_backend.h b/src/libserver/fuzzy_backend.h
index 04da9bbba..bcd199d1a 100644
--- a/src/libserver/fuzzy_backend.h
+++ b/src/libserver/fuzzy_backend.h
@@ -56,7 +56,8 @@ gboolean rspamd_fuzzy_backend_prepare_update (struct rspamd_fuzzy_backend *backe
*/
gboolean rspamd_fuzzy_backend_add (
struct rspamd_fuzzy_backend *backend,
- const struct rspamd_fuzzy_cmd *cmd);
+ const struct rspamd_fuzzy_cmd *cmd,
+ time_t timestamp);
/**
* Delete digest from the database
@@ -89,6 +90,7 @@ gboolean rspamd_fuzzy_backend_sync (struct rspamd_fuzzy_backend *backend,
void rspamd_fuzzy_backend_close (struct rspamd_fuzzy_backend *backend);
gsize rspamd_fuzzy_backend_count (struct rspamd_fuzzy_backend *backend);
+gint rspamd_fuzzy_backend_version (struct rspamd_fuzzy_backend *backend);
gsize rspamd_fuzzy_backend_expired (struct rspamd_fuzzy_backend *backend);
const gchar * rspamd_fuzzy_backend_id (struct rspamd_fuzzy_backend *backend);