]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Add new auxiliary methods for symbols cache
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 4 Apr 2016 17:21:11 +0000 (18:21 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 4 Apr 2016 17:21:11 +0000 (18:21 +0100)
src/libserver/symbols_cache.c
src/libserver/symbols_cache.h

index 0b0d8b0f00d8b12ad8cf8fc51a93dad876ed3697..862bd0f11c487db353444d1d7c6d157245f74a3e 100644 (file)
@@ -42,6 +42,9 @@
 
 static const guchar rspamd_symbols_cache_magic[8] = {'r', 's', 'c', 1, 0, 0, 0, 0 };
 
+static gint rspamd_symbols_cache_find_symbol_parent (struct symbols_cache *cache,
+               const gchar *name);
+
 struct rspamd_symbols_cache_header {
        guchar magic[8];
        guint nitems;
@@ -307,7 +310,7 @@ rspamd_symbols_cache_post_init (struct symbols_cache *cache)
        while (cur) {
                ddep = cur->data;
 
-               id = rspamd_symbols_cache_find_symbol (cache, ddep->from);
+               id = rspamd_symbols_cache_find_symbol_parent (cache, ddep->from);
                if (id != -1) {
                        it = g_ptr_array_index (cache->items_by_id, id);
                }
@@ -330,7 +333,7 @@ rspamd_symbols_cache_post_init (struct symbols_cache *cache)
        while (cur) {
                dcond = cur->data;
 
-               id = rspamd_symbols_cache_find_symbol (cache, dcond->sym);
+               id = rspamd_symbols_cache_find_symbol_parent (cache, dcond->sym);
                if (id != -1) {
                        it = g_ptr_array_index (cache->items_by_id, id);
                }
@@ -694,7 +697,7 @@ rspamd_symbols_cache_add_condition_delayed (struct symbols_cache *cache,
        g_assert (cache != NULL);
        g_assert (sym != NULL);
 
-       id = rspamd_symbols_cache_find_symbol (cache, sym);
+       id = rspamd_symbols_cache_find_symbol_parent (cache, sym);
 
        if (id != -1) {
                /* We already know id, so just register a direct condition */
@@ -1657,6 +1660,27 @@ rspamd_symbols_cache_find_symbol (struct symbols_cache *cache, const gchar *name
 
        item = g_hash_table_lookup (cache->items_by_symbol, name);
 
+       if (item != NULL) {
+               return item->id;
+       }
+
+       return -1;
+}
+
+static gint
+rspamd_symbols_cache_find_symbol_parent (struct symbols_cache *cache,
+               const gchar *name)
+{
+       struct cache_item *item;
+
+       g_assert (cache != NULL);
+
+       if (name == NULL) {
+               return -1;
+       }
+
+       item = g_hash_table_lookup (cache->items_by_symbol, name);
+
        if (item != NULL) {
 
                while (item != NULL && item->parent != -1) {
@@ -1669,6 +1693,23 @@ rspamd_symbols_cache_find_symbol (struct symbols_cache *cache, const gchar *name
        return -1;
 }
 
+const gchar *
+rspamd_symbols_cache_symbol_by_id (struct symbols_cache *cache,
+               gint id)
+{
+       struct cache_item *item;
+
+       g_assert (cache != NULL);
+
+       if (id < 0 || id >= (gint)cache->items_by_id->len) {
+               return NULL;
+       }
+
+       item = g_ptr_array_index (cache->items_by_id, id);
+
+       return item->symbol;
+}
+
 void
 rspamd_symbols_cache_disable_symbol (struct rspamd_task *task,
                struct symbols_cache *cache, const gchar *symbol)
index 6dd70f04ca5105e90c63ead201880020da66f0a2..e199741298e69f685f8da091aa73c01e2b457919 100644 (file)
@@ -105,7 +105,17 @@ gboolean rspamd_symbols_cache_add_condition_delayed (struct symbols_cache *cache
  * @param name
  * @return id of symbol or (-1) if a symbol has not been found
  */
-gint rspamd_symbols_cache_find_symbol (struct symbols_cache *cache, const gchar *name);
+gint rspamd_symbols_cache_find_symbol (struct symbols_cache *cache,
+               const gchar *name);
+
+/**
+ * Find symbol in cache by its id
+ * @param cache
+ * @param id
+ * @return symbol's name or NULL
+ */
+const gchar * rspamd_symbols_cache_symbol_by_id (struct symbols_cache *cache,
+               gint id);
 
 /**
  * Call function for cached symbol using saved callback