]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Add basic traversal helpers
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 25 Apr 2018 13:07:31 +0000 (14:07 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 25 Apr 2018 13:07:31 +0000 (14:07 +0100)
src/libutil/map.c
src/libutil/map.h
src/libutil/map_helpers.c

index 0a27365eb1290633b0676a2aa03c94b28e28c350..698ccf3fd8f34a1b00159f8ea9fc81b59de680b4 100644 (file)
@@ -2231,4 +2231,13 @@ rspamd_map_get_traverse_function (struct rspamd_map *map)
        }
 
        return NULL;
-}
\ No newline at end of file
+}
+
+void
+rspamd_map_traverse (struct rspamd_map *map, rspamd_map_traverse_cb cb,
+               gpointer cbdata, gboolean reset_hits)
+{
+       if (*map->user_data && map->traverse_function) {
+               map->traverse_function (*map->user_data, cb, cbdata, reset_hits);
+       }
+}
index 75c8788cad6d194f52eee1255380ebc3d866d0a3..d10ee331a5acaa95e38fea3ceb6ad8add030fee4 100644 (file)
@@ -25,7 +25,8 @@ typedef void (*map_fin_cb_t)(struct map_cb_data *data);
 
 typedef gboolean (*rspamd_map_traverse_cb)(gconstpointer key,
                gconstpointer value, gsize hits, gpointer ud);
-typedef void (*rspamd_map_traverse_function)(rspamd_map_traverse_cb cb,
+typedef void (*rspamd_map_traverse_function)(void *data,
+               rspamd_map_traverse_cb cb,
                gpointer cbdata, gboolean reset_hits);
 
 /**
@@ -89,4 +90,15 @@ void rspamd_map_remove_all (struct rspamd_config *cfg);
  */
 rspamd_map_traverse_function rspamd_map_get_traverse_function (struct rspamd_map *map);
 
+/**
+ * Perform map traverse
+ * @param map
+ * @param cb
+ * @param cbdata
+ * @param reset_hits
+ * @return
+ */
+void rspamd_map_traverse (struct rspamd_map *map, rspamd_map_traverse_cb cb,
+               gpointer cbdata, gboolean reset_hits);
+
 #endif
index 7bb442a9792636e7ee0fa89b6a12268165632410..b7d6acb490f3fb1749c8392376b34c7a3e4cb1b4 100644 (file)
@@ -568,6 +568,26 @@ rspamd_map_helper_insert_re (gpointer st, gconstpointer key, gconstpointer value
        g_ptr_array_add (re_map->values, val);
 }
 
+static void
+rspamd_map_helper_traverse_regexp (void *data,
+               rspamd_map_traverse_cb cb,
+               gpointer cbdata,
+               gboolean reset_hits)
+{
+       gconstpointer k;
+       struct rspamd_map_helper_value *val;
+       struct rspamd_regexp_map_helper *re_map = data;
+
+       kh_foreach (re_map->htb, k, val, {
+               if (!cb (k, val->value, val->hits, cbdata)) {
+                       break;
+               }
+
+               if (reset_hits) {
+                       val->hits = 0;
+               }
+       });
+}
 
 struct rspamd_hash_map_helper *
 rspamd_map_helper_new_hash (struct rspamd_map *map)
@@ -602,6 +622,27 @@ rspamd_map_helper_destroy_hash (struct rspamd_hash_map_helper *r)
        rspamd_mempool_delete (r->pool);
 }
 
+static void
+rspamd_map_helper_traverse_hash (void *data,
+               rspamd_map_traverse_cb cb,
+               gpointer cbdata,
+               gboolean reset_hits)
+{
+       gconstpointer k;
+       struct rspamd_map_helper_value *val;
+       struct rspamd_hash_map_helper *ht = data;
+
+       kh_foreach (ht->htb, k, val, {
+               if (!cb (k, val->value, val->hits, cbdata)) {
+                       break;
+               }
+
+               if (reset_hits) {
+                       val->hits = 0;
+               }
+       });
+}
+
 struct rspamd_radix_map_helper *
 rspamd_map_helper_new_radix (struct rspamd_map *map)
 {
@@ -636,6 +677,27 @@ rspamd_map_helper_destroy_radix (struct rspamd_radix_map_helper *r)
        rspamd_mempool_delete (r->pool);
 }
 
+static void
+rspamd_map_helper_traverse_radix (void *data,
+               rspamd_map_traverse_cb cb,
+               gpointer cbdata,
+               gboolean reset_hits)
+{
+       gconstpointer k;
+       struct rspamd_map_helper_value *val;
+       struct rspamd_radix_map_helper *r = data;
+
+       kh_foreach (r->htb, k, val, {
+               if (!cb (k, val->value, val->hits, cbdata)) {
+                       break;
+               }
+
+               if (reset_hits) {
+                       val->hits = 0;
+               }
+       });
+}
+
 struct rspamd_regexp_map_helper *
 rspamd_map_helper_new_regexp (struct rspamd_map *map,
                enum rspamd_regexp_map_flags flags)
@@ -732,6 +794,7 @@ rspamd_kv_list_fin (struct map_cb_data *data)
        if (data->cur_data) {
                htb = (struct rspamd_hash_map_helper *)data->cur_data;
                msg_info_map ("read hash of %d elements", kh_size (htb->htb));
+               data->map->traverse_function = rspamd_map_helper_traverse_hash;
        }
 }
 
@@ -774,6 +837,7 @@ rspamd_radix_fin (struct map_cb_data *data)
                r = (struct rspamd_radix_map_helper *)data->cur_data;
                msg_info_map ("read radix trie of %z elements: %s",
                                radix_get_size (r->trie), radix_get_info (r->trie));
+               data->map->traverse_function = rspamd_map_helper_traverse_radix;
        }
 }
 
@@ -951,6 +1015,7 @@ rspamd_regexp_list_fin (struct map_cb_data *data)
                rspamd_re_map_finalize (re_map);
                msg_info_map ("read regexp list of %ud elements",
                                re_map->regexps->len);
+               data->map->traverse_function = rspamd_map_helper_traverse_regexp;
        }
 }