From 29bbf526f4e7d6fe31618901a753ee350fbe03d5 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 5 Apr 2016 17:26:28 +0100 Subject: [PATCH] [Feature] Add method to get number of symbols in the cache --- src/libserver/symbols_cache.c | 8 ++++++++ src/libserver/symbols_cache.h | 7 +++++++ src/lua/lua_config.c | 26 ++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/src/libserver/symbols_cache.c b/src/libserver/symbols_cache.c index 862bd0f11..eea7f84b2 100644 --- a/src/libserver/symbols_cache.c +++ b/src/libserver/symbols_cache.c @@ -1710,6 +1710,14 @@ rspamd_symbols_cache_symbol_by_id (struct symbols_cache *cache, return item->symbol; } +guint +rspamd_symbols_cache_symbols_count (struct symbols_cache *cache) +{ + g_assert (cache != NULL); + + return cache->items_by_id->len; +} + void rspamd_symbols_cache_disable_symbol (struct rspamd_task *task, struct symbols_cache *cache, const gchar *symbol) diff --git a/src/libserver/symbols_cache.h b/src/libserver/symbols_cache.h index e19974129..0874ac475 100644 --- a/src/libserver/symbols_cache.h +++ b/src/libserver/symbols_cache.h @@ -117,6 +117,13 @@ gint rspamd_symbols_cache_find_symbol (struct symbols_cache *cache, const gchar * rspamd_symbols_cache_symbol_by_id (struct symbols_cache *cache, gint id); +/** + * Returns number of symbols registered in symbols cache + * @param cache + * @return number of symbols in the cache + */ +guint rspamd_symbols_cache_symbols_count (struct symbols_cache *cache); + /** * Call function for cached symbol using saved callback * @param task task object diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index cbf68dd07..aa4327b27 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -404,6 +404,13 @@ LUA_FUNCTION_DEF (config, register_worker_script); */ LUA_FUNCTION_DEF (config, add_on_load); +/*** + * @method rspamd_config:get_symbols_count() + * Returns number of symbols registered in rspamd configuration + * @return {number} number of symbols registered in the configuration + */ +LUA_FUNCTION_DEF (config, get_symbols_count); + static const struct luaL_reg configlib_m[] = { LUA_INTERFACE_DEF (config, get_module_opt), LUA_INTERFACE_DEF (config, get_mempool), @@ -433,6 +440,7 @@ static const struct luaL_reg configlib_m[] = { LUA_INTERFACE_DEF (config, replace_regexp), LUA_INTERFACE_DEF (config, register_worker_script), LUA_INTERFACE_DEF (config, add_on_load), + LUA_INTERFACE_DEF (config, get_symbols_count), {"__tostring", rspamd_lua_class_tostring}, {"__newindex", lua_config_newindex}, {NULL, NULL} @@ -1702,6 +1710,24 @@ lua_config_add_on_load (lua_State *L) return 0; } +static gint +lua_config_get_symbols_count (lua_State *L) +{ + struct rspamd_config *cfg = lua_check_config (L, 1); + guint res = 0; + + if (cfg != NULL) { + res = rspamd_symbols_cache_symbols_count (cfg->cache); + } + else { + return luaL_error (L, "invalid arguments"); + } + + lua_pushnumber (L, res); + + return 1; +} + void luaopen_config (lua_State * L) { -- 2.39.5