aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-09-01 16:31:45 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-09-01 16:31:45 +0100
commit8df2f0ecd577e8dab3b94561110ca5ebaa551f40 (patch)
tree3fc192bb931d2384ae6a8a589c8a1b9959acdb9a
parentb5e56ad3167066dcedba677e715c7e4cf7b535ff (diff)
downloadrspamd-8df2f0ecd577e8dab3b94561110ca5ebaa551f40.tar.gz
rspamd-8df2f0ecd577e8dab3b94561110ca5ebaa551f40.zip
[Minor] Add sqlite helpers to the abstract API
-rw-r--r--src/libserver/fuzzy_backend.c128
-rw-r--r--src/libserver/fuzzy_backend.h2
-rw-r--r--src/libserver/fuzzy_backend_sqlite.c10
3 files changed, 131 insertions, 9 deletions
diff --git a/src/libserver/fuzzy_backend.c b/src/libserver/fuzzy_backend.c
index 514b36068..7254f2aa2 100644
--- a/src/libserver/fuzzy_backend.c
+++ b/src/libserver/fuzzy_backend.c
@@ -37,7 +37,7 @@ void rspamd_fuzzy_backend_count_sqlite (struct rspamd_fuzzy_backend *bk,
rspamd_fuzzy_count_cb cb, void *ud,
void *subr_ud);
void rspamd_fuzzy_backend_version_sqlite (struct rspamd_fuzzy_backend *bk,
- const gchar *version,
+ const gchar *src,
rspamd_fuzzy_version_cb cb, void *ud,
void *subr_ud);
@@ -56,7 +56,7 @@ struct rspamd_fuzzy_backend_subr {
rspamd_fuzzy_count_cb cb, void *ud,
void *subr_ud);
void (*version) (struct rspamd_fuzzy_backend *bk,
- const gchar *version,
+ const gchar *src,
rspamd_fuzzy_version_cb cb, void *ud,
void *subr_ud);
};
@@ -85,6 +85,122 @@ rspamd_fuzzy_backend_quark (void)
return g_quark_from_static_string ("fuzzy-backend");
}
+void*
+rspamd_fuzzy_backend_init_sqlite (struct rspamd_fuzzy_backend *bk,
+ const ucl_object_t *obj, GError **err)
+{
+ const ucl_object_t *elt;
+
+ elt = ucl_object_lookup_any (obj, "hashfile", "hash_file", "file",
+ "database", NULL);
+
+ if (elt == NULL || ucl_object_type (elt) != UCL_STRING) {
+ g_set_error (err, rspamd_fuzzy_backend_quark (),
+ EINVAL, "missing sqlite3 path");
+ return NULL;
+ }
+
+ return rspamd_fuzzy_backend_sqlite_open (ucl_object_tostring (elt),
+ FALSE, err);
+}
+
+void
+rspamd_fuzzy_backend_check_sqlite (struct rspamd_fuzzy_backend *bk,
+ const struct rspamd_fuzzy_cmd *cmd,
+ rspamd_fuzzy_check_cb cb, void *ud,
+ void *subr_ud)
+{
+ struct rspamd_fuzzy_backend_sqlite *sq = subr_ud;
+ struct rspamd_fuzzy_reply rep;
+
+ rep = rspamd_fuzzy_backend_sqlite_check (sq, cmd, bk->expire);
+
+ if (cb) {
+ cb (&rep, ud);
+ }
+}
+
+void rspamd_fuzzy_backend_update_sqlite (struct rspamd_fuzzy_backend *bk,
+ GQueue *updates, const gchar *src,
+ rspamd_fuzzy_update_cb cb, void *ud,
+ void *subr_ud)
+{
+ struct rspamd_fuzzy_backend_sqlite *sq = subr_ud;
+ gboolean success = FALSE;
+ GList *cur;
+ struct fuzzy_peer_cmd *io_cmd;
+ struct rspamd_fuzzy_cmd *cmd;
+ gpointer ptr;
+ guint nupdates = 0;
+
+ if (rspamd_fuzzy_backend_sqlite_prepare_update (sq, src)) {
+ cur = updates->head;
+
+ while (cur) {
+ io_cmd = cur->data;
+
+ if (io_cmd->is_shingle) {
+ cmd = &io_cmd->cmd.shingle.basic;
+ ptr = &io_cmd->cmd.shingle;
+ }
+ else {
+ cmd = &io_cmd->cmd.normal;
+ ptr = &io_cmd->cmd.normal;
+ }
+
+ if (cmd->cmd == FUZZY_WRITE) {
+ rspamd_fuzzy_backend_sqlite_add (sq, ptr);
+ }
+ else {
+ rspamd_fuzzy_backend_sqlite_del (sq, ptr);
+ }
+
+ nupdates ++;
+ cur = g_list_next (cur);
+ }
+
+ if (rspamd_fuzzy_backend_sqlite_finish_update (sq, src,
+ nupdates > 0)) {
+ success = TRUE;
+ }
+ }
+
+ if (cb) {
+ cb (success, ud);
+ }
+}
+
+void
+rspamd_fuzzy_backend_count_sqlite (struct rspamd_fuzzy_backend *bk,
+ rspamd_fuzzy_count_cb cb, void *ud,
+ void *subr_ud)
+{
+ struct rspamd_fuzzy_backend_sqlite *sq = subr_ud;
+ guint64 nhashes;
+
+ nhashes = rspamd_fuzzy_backend_sqlite_count (sq);
+
+ if (cb) {
+ cb (nhashes, ud);
+ }
+}
+
+void
+rspamd_fuzzy_backend_version_sqlite (struct rspamd_fuzzy_backend *bk,
+ const gchar *src,
+ rspamd_fuzzy_version_cb cb, void *ud,
+ void *subr_ud)
+{
+ struct rspamd_fuzzy_backend_sqlite *sq = subr_ud;
+ guint64 rev;
+
+ rev = rspamd_fuzzy_backend_sqlite_version (sq, src);
+
+ if (cb) {
+ cb (rev, ud);
+ }
+}
+
struct rspamd_fuzzy_backend *
rspamd_fuzzy_backend_create (struct event_base *ev_base,
@@ -147,8 +263,14 @@ rspamd_fuzzy_backend_process_updates (struct rspamd_fuzzy_backend *bk,
void *ud)
{
g_assert (bk != NULL);
+ g_assert (updates != NULL);
- bk->subr->update (bk, updates, src, cb, ud, bk->subr_ud);
+ if (g_queue_get_length (updates) > 0) {
+ bk->subr->update (bk, updates, src, cb, ud, bk->subr_ud);
+ }
+ else if (cb) {
+ cb (TRUE, ud);
+ }
}
diff --git a/src/libserver/fuzzy_backend.h b/src/libserver/fuzzy_backend.h
index b51a71c42..5fceaba12 100644
--- a/src/libserver/fuzzy_backend.h
+++ b/src/libserver/fuzzy_backend.h
@@ -26,7 +26,7 @@ struct rspamd_fuzzy_backend;
* Callbacks for fuzzy methods
*/
typedef void (*rspamd_fuzzy_check_cb) (struct rspamd_fuzzy_reply *rep, void *ud);
-typedef void (*rspamd_fuzzy_update_cb) (void *ud);
+typedef void (*rspamd_fuzzy_update_cb) (gboolean success, void *ud);
typedef void (*rspamd_fuzzy_version_cb) (guint64 rev, void *ud);
typedef void (*rspamd_fuzzy_count_cb) (guint64 count, void *ud);
diff --git a/src/libserver/fuzzy_backend_sqlite.c b/src/libserver/fuzzy_backend_sqlite.c
index e45c0c401..834d274c8 100644
--- a/src/libserver/fuzzy_backend_sqlite.c
+++ b/src/libserver/fuzzy_backend_sqlite.c
@@ -244,9 +244,9 @@ static struct rspamd_fuzzy_stmts {
};
static GQuark
-rspamd_fuzzy_backend_quark(void)
+rspamd_fuzzy_backend_sqlite_quark (void)
{
- return g_quark_from_static_string ("fuzzy-storage-backend");
+ return g_quark_from_static_string ("fuzzy-backend-sqlite");
}
static gboolean
@@ -261,7 +261,7 @@ rspamd_fuzzy_backend_sqlite_prepare_stmts (struct rspamd_fuzzy_backend_sqlite *b
}
if (sqlite3_prepare_v2 (bk->db, prepared_stmts[i].sql, -1,
&prepared_stmts[i].stmt, NULL) != SQLITE_OK) {
- g_set_error (err, rspamd_fuzzy_backend_quark (),
+ g_set_error (err, rspamd_fuzzy_backend_sqlite_quark (),
-1, "Cannot initialize prepared sql `%s`: %s",
prepared_stmts[i].sql, sqlite3_errmsg (bk->db));
@@ -408,7 +408,7 @@ rspamd_fuzzy_backend_sqlite_run_sql (const gchar *sql, struct rspamd_fuzzy_backe
nanosleep (&ts, NULL) == 0);
if (ret != SQLITE_OK) {
- g_set_error (err, rspamd_fuzzy_backend_quark (),
+ g_set_error (err, rspamd_fuzzy_backend_sqlite_quark (),
-1, "Cannot execute raw sql `%s`: %s",
sql, sqlite3_errmsg (bk->db));
return FALSE;
@@ -463,7 +463,7 @@ rspamd_fuzzy_backend_sqlite_open (const gchar *path,
struct rspamd_fuzzy_backend_sqlite *backend;
if (path == NULL) {
- g_set_error (err, rspamd_fuzzy_backend_quark (),
+ g_set_error (err, rspamd_fuzzy_backend_sqlite_quark (),
ENOENT, "Path has not been specified");
return NULL;
}