]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Rework] Further steps
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 23 Apr 2022 13:35:39 +0000 (14:35 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 23 Apr 2022 13:35:39 +0000 (14:35 +0100)
src/libserver/symcache/symcache_c.cxx
src/libserver/symcache/symcache_internal.hxx
src/libserver/symcache/symcache_item.cxx
src/libserver/symcache/symcache_item.hxx
src/lua/lua_config.c

index df75bf2c84875f9296a66debe5ee9b5557421082..abedba23f6d82e9e8c33f92d181f0d3537a27bda 100644 (file)
@@ -221,19 +221,50 @@ rspamd_symcache_item_name (struct rspamd_symcache_item *item)
 }
 
 gint
-rspamd_symcache_item_flags (struct rspamd_symcache_item *item)
+rspamd_symcache_item_flags(struct rspamd_symcache_item *item)
 {
        auto *real_item = C_API_SYMCACHE_ITEM(item);
        return real_item->get_flags();
 }
 
+guint
+rspamd_symcache_get_symbol_flags(struct rspamd_symcache *cache,
+                                                                const gchar *symbol)
+{
+       auto *real_cache = C_API_SYMCACHE(cache);
+
+       auto *sym = real_cache->get_item_by_name(symbol, false);
+
+       if (sym) {
+               return sym->get_flags();
+       }
+
+       return 0;
+}
+
 const struct rspamd_symcache_item_stat *
-rspamd_symcache_item_stat (struct rspamd_symcache_item *item)
+rspamd_symcache_item_stat(struct rspamd_symcache_item *item)
 {
        auto *real_item = C_API_SYMCACHE_ITEM(item);
        return real_item->st;
 }
 
+void
+rspamd_symcache_get_symbol_details(struct rspamd_symcache *cache,
+                                                                  const gchar *symbol,
+                                                                  ucl_object_t *this_sym_ucl)
+{
+       auto *real_cache = C_API_SYMCACHE(cache);
+
+       auto *sym = real_cache->get_item_by_name(symbol, false);
+
+       if (sym) {
+               ucl_object_insert_key (this_sym_ucl,
+                               ucl_object_fromstring(sym->get_type_str()),
+                               "type", strlen("type"), false);
+       }
+}
+
 void
 rspamd_symcache_foreach(struct rspamd_symcache *cache,
                                                          void (*func) (struct rspamd_symcache_item *item, gpointer /* userdata */),
@@ -246,9 +277,10 @@ rspamd_symcache_foreach(struct rspamd_symcache *cache,
        });
 }
 
-void rspamd_symcache_disable_all_symbols (struct rspamd_task *task,
-                                                                                 struct rspamd_symcache *_cache,
-                                                                                 guint skip_mask)
+void
+rspamd_symcache_disable_all_symbols(struct rspamd_task *task,
+                                                                       struct rspamd_symcache *_cache,
+                                                                       guint skip_mask)
 {
        auto *cache_runtime = C_API_SYMCACHE_RUNTIME(task->symcache_runtime);
 
index 2c3bfe739414b54fb1ef14116c247706a1341477..8a7e44e6246fc75a8bae0f39f2505b9e6ccab2ff 100644 (file)
@@ -80,12 +80,23 @@ struct symcache_header {
 struct cache_item;
 using cache_item_ptr = std::shared_ptr<cache_item>;
 
+/**
+ * This structure is intended to keep the current ordering for all symbols
+ * It is designed to be shared among all tasks and keep references to the real
+ * symbols.
+ * If some symbol has been added or removed to the symbol cache, it will not affect
+ * the current order, and it will only be regenerated for the subsequent tasks.
+ * This allows safe and no copy sharing and keeping track of all symbols in the
+ * cache runtime.
+ */
 struct order_generation {
+       /* All items ordered */
        std::vector<cache_item_ptr> d;
        /* Mapping from symbol name to the position in the order array */
        robin_hood::unordered_flat_map<std::string_view, unsigned int> by_symbol;
        /* Mapping from symbol id to the position in the order array */
        robin_hood::unordered_flat_map<unsigned int, unsigned int> by_cache_id;
+       /* It matches cache->generation_id; if not, a fresh ordering is required */
        unsigned int generation_id;
 
        explicit order_generation(std::size_t nelts, unsigned id) : generation_id(id) {
@@ -100,7 +111,6 @@ struct order_generation {
 using order_generation_ptr = std::shared_ptr<order_generation>;
 
 
-
 struct delayed_cache_dependency {
        std::string from;
        std::string to;
index b25fc991ff6ccfa1d53d40a02cf133c5a3848054..0ca080ac0da417ad0c2fdb926e9654b3bca66334 100644 (file)
@@ -203,6 +203,30 @@ auto cache_item::update_counters_check_peak(lua_State *L,
        return ret;
 }
 
+auto cache_item::get_type_str() const -> const char *
+{
+       switch(type) {
+       case symcache_item_type::CONNFILTER:
+               return "connfilter";
+       case symcache_item_type::FILTER:
+               return "filter";
+       case symcache_item_type::IDEMPOTENT:
+               return "idempotent";
+       case symcache_item_type::PREFILTER:
+               return "prefilter";
+       case symcache_item_type::POSTFILTER:
+               return "postfilter";
+       case symcache_item_type::COMPOSITE:
+               return "composite";
+       case symcache_item_type::CLASSIFIER:
+               return "classifier";
+       case symcache_item_type::VIRTUAL:
+               return "virtual";
+       }
+
+       RSPAMD_UNREACHABLE;
+}
+
 auto virtual_item::get_parent(const symcache &cache) const -> const cache_item *
 {
        if (parent) {
index 23347a8ecfceb4516b377a20ceebd1406a910ee0..18a46d3176e3e5d25c7223accd9572f97c7fac31 100644 (file)
@@ -260,6 +260,8 @@ public:
                return type;
        }
 
+       auto get_type_str() const -> const char*;
+
        auto get_name() const -> const std::string &
        {
                return symbol;
index 8c19671f1d1aa222c7fcb4785e0d5cdeaf22a805..8f7854347121984b5905eba133f636a83c4cc1d4 100644 (file)
@@ -869,7 +869,6 @@ static const struct luaL_reg configlib_m[] = {
        LUA_INTERFACE_DEF (config, register_dependency),
        LUA_INTERFACE_DEF (config, register_settings_id),
        LUA_INTERFACE_DEF (config, get_symbol_flags),
-       LUA_INTERFACE_DEF (config, add_symbol_flags),
        LUA_INTERFACE_DEF (config, set_metric_symbol),
        {"set_symbol", lua_config_set_metric_symbol},
        LUA_INTERFACE_DEF (config, set_metric_action),
@@ -1985,38 +1984,6 @@ lua_config_get_symbol_flags (lua_State *L)
        return 1;
 }
 
-static gint
-lua_config_add_symbol_flags (lua_State *L)
-{
-       struct rspamd_config *cfg = lua_check_config (L, 1);
-       const gchar *name = luaL_checkstring (L, 2);
-       guint flags, new_flags = 0;
-
-       if (cfg && name && lua_istable (L, 3)) {
-
-               for (lua_pushnil (L); lua_next (L, 3); lua_pop (L, 1)) {
-                       new_flags |= lua_parse_symbol_flags (lua_tostring (L, -1));
-               }
-
-               flags = rspamd_symcache_get_symbol_flags (cfg->cache,
-                               name);
-
-               if (flags != 0) {
-                       rspamd_symcache_add_symbol_flags (cfg->cache, name, new_flags);
-                       /* Push old flags */
-                       lua_push_symbol_flags (L, flags, LUA_SYMOPT_FLAG_CREATE_ARRAY);
-               }
-               else {
-                       lua_pushnil (L);
-               }
-       }
-       else {
-               return luaL_error (L, "invalid arguments");
-       }
-
-       return 1;
-}
-
 static gint
 lua_config_register_symbol (lua_State * L)
 {