]> source.dussan.org Git - rspamd.git/commitdiff
[Rework] Rework options hash structure
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 29 Jan 2020 15:03:08 +0000 (15:03 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 29 Jan 2020 15:03:08 +0000 (15:03 +0000)
src/libmime/scan_result.c
src/libmime/scan_result.h
src/libmime/scan_result_private.h

index 7a8451f9c907006c5a0d50db7af436c3bedfc9ee..671e99a9b0ca2815df5ef00e80849c1958d1303b 100644 (file)
@@ -293,7 +293,7 @@ insert_metric_result (struct rspamd_task *task,
                s->nshots ++;
 
                if (opt) {
-                       rspamd_task_add_result_option (task, s, opt);
+                       rspamd_task_add_result_option (task, s, opt, strlen (opt));
                }
 
                /* Adjust diff */
@@ -459,7 +459,9 @@ insert_metric_result (struct rspamd_task *task,
                        s->score = 0;
                }
 
-               rspamd_task_add_result_option (task, s, opt);
+               if (opt) {
+                       rspamd_task_add_result_option (task, s, opt, strlen (opt));
+               }
        }
 
        msg_debug_metric ("final insertion for symbol %s, score %.2f, factor: %f",
@@ -503,12 +505,13 @@ rspamd_task_insert_result_full (struct rspamd_task *task,
 
 gboolean
 rspamd_task_add_result_option (struct rspamd_task *task,
-               struct rspamd_symbol_result *s, const gchar *val)
+                                                          struct rspamd_symbol_result *s,
+                                                          const gchar *val,
+                                                          gsize vlen)
 {
-       struct rspamd_symbol_option *opt;
+       struct rspamd_symbol_option *opt, srch;
        gboolean ret = FALSE;
        gchar *opt_cpy = NULL;
-       gsize vlen;
        khiter_t k;
        gint r;
 
@@ -524,8 +527,6 @@ rspamd_task_add_result_option (struct rspamd_task *task,
                        s->options = kh_init (rspamd_options_hash);
                }
 
-               vlen = strlen (val);
-
                if (vlen + s->opts_len > task->cfg->max_opts_len) {
                        /* Add truncated option */
                        msg_info_task ("cannot add more options to symbol %s when adding option %s",
@@ -544,7 +545,9 @@ rspamd_task_add_result_option (struct rspamd_task *task,
                if (!(s->sym && (s->sym->flags & RSPAMD_SYMBOL_FLAG_ONEPARAM)) &&
                                kh_size (s->options) < task->cfg->default_max_shots) {
                        /* Append new options */
-                       k = kh_get (rspamd_options_hash, s->options, val);
+                       srch.option = (gchar *)val;
+                       srch.optlen = vlen;
+                       k = kh_get (rspamd_options_hash, s->options, &srch);
 
                        if (k == kh_end (s->options)) {
                                opt = rspamd_mempool_alloc0 (task->task_pool, sizeof (*opt));
@@ -553,9 +556,10 @@ rspamd_task_add_result_option (struct rspamd_task *task,
                                        opt_cpy = rspamd_mempool_strdup (task->task_pool, val);
                                }
 
-                               k = kh_put (rspamd_options_hash, s->options, opt_cpy, &r);
+                               opt->optlen = vlen;
+                               opt->option = opt_cpy;
 
-                               kh_value (s->options, k) = opt;
+                               k = kh_put (rspamd_options_hash, s->options, opt, &r);
                                opt->option = opt_cpy;
                                DL_APPEND (s->opts_head, opt);
 
index 3b222fffbb4bab6480570650312073e0e76846cc..74d0d81b902ee9cc2ba7586bd79958e6972ee81d 100644 (file)
@@ -20,6 +20,7 @@ struct rspamd_classifier_config;
 
 struct rspamd_symbol_option {
        gchar *option;
+       gsize optlen;
        struct rspamd_symbol_option *prev, *next;
 };
 
@@ -142,7 +143,9 @@ struct rspamd_symbol_result *rspamd_task_insert_result_full (struct rspamd_task
  * @param opt
  */
 gboolean rspamd_task_add_result_option (struct rspamd_task *task,
-                                                                               struct rspamd_symbol_result *s, const gchar *opt);
+                                                                               struct rspamd_symbol_result *s,
+                                                                               const gchar *opt,
+                                                                               gsize vlen);
 
 /**
  * Finds symbol result
index aa90430e1e09e5a38d11cd133d03860eb8c5182b..cb4ff4cdaa56e57cad411d965ca41326df977243 100644 (file)
 extern "C" {
 #endif
 
-KHASH_MAP_INIT_STR (rspamd_options_hash, struct rspamd_symbol_option *);
+#define RSPAMD_OPTS_SEED 0x9f1f608628a4fefbULL
+#define rspamd_symopt_hash(opt) (rspamd_cryptobox_fast_hash ( \
+               ((struct rspamd_symbol_option *)opt)->option, \
+               ((struct rspamd_symbol_option *)opt)->optlen, RSPAMD_OPTS_SEED))
+static inline bool
+rspamd_symopt_equal (const struct rspamd_symbol_option *o1,
+               const struct rspamd_symbol_option *o2)
+{
+       if (o1->optlen == o2->optlen) {
+               return (memcmp (o1->option, o2->option, o1->optlen) == 0);
+       }
+
+       return false;
+}
+
+KHASH_INIT (rspamd_options_hash, struct rspamd_symbol_option *, char,
+               0, rspamd_symopt_hash, rspamd_symopt_equal);
 /**
  * Result of metric processing
  */