Browse Source

Add method to register symbols conditions

tags/1.0.0
Vsevolod Stakhov 8 years ago
parent
commit
641af77bc5
2 changed files with 63 additions and 0 deletions
  1. 44
    0
      src/libserver/symbols_cache.c
  2. 19
    0
      src/libserver/symbols_cache.h

+ 44
- 0
src/libserver/symbols_cache.c View File

@@ -481,6 +481,7 @@ rspamd_symbols_cache_add_symbol (struct symbols_cache *cache,

item = rspamd_mempool_alloc0_shared (cache->static_pool,
sizeof (struct cache_item));
item->condition_cb = -1;
/*
* We do not share cd to skip locking, instead we'll just calculate it on
* save or accumulate
@@ -523,6 +524,29 @@ rspamd_symbols_cache_add_symbol (struct symbols_cache *cache,
return item->id;
}

gboolean
rspamd_symbols_cache_add_condition (struct symbols_cache *cache, gint id, lua_State *L, gint cbref)
{
struct cache_item *item;

g_assert (cache != NULL);

if (id < 0 || id >= cache->items_by_id->len) {
return FALSE;
}

item = g_ptr_array_index (cache->items_by_id, id);

if (item->condition_cb != -1) {
/* We already have a condition, so we need to remove old cbref first */
msg_warn ("rewriting condition for symbol %s", item->symbol);
luaL_unref (L, LUA_REGISTRYINDEX, item->condition_cb);
}

item->condition_cb = cbref;

return TRUE;
}

void
rspamd_symbols_cache_destroy (struct symbols_cache *cache)
@@ -1262,3 +1286,23 @@ rspamd_symbols_cache_add_delayed_dependency (struct symbols_cache *cache,

cache->delayed_deps = g_list_prepend (cache->delayed_deps, ddep);
}

gint
rspamd_symbols_cache_find_symbol (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) {
return item->id;
}

return -1;
}

+ 19
- 0
src/libserver/symbols_cache.h View File

@@ -27,6 +27,7 @@

#include "config.h"
#include "ucl.h"
#include "lua/lua_common.h"

#define MAX_SYMBOL 128

@@ -83,6 +84,24 @@ gint rspamd_symbols_cache_add_symbol (struct symbols_cache *cache,
enum rspamd_symbol_type type,
gint parent);

/**
* Add condition to the specific symbol in cache
* @param cache
* @param id id of symbol
* @param L lua state pointer
* @param cbref callback reference (returned by luaL_ref)
* @return TRUE if condition has been added
*/
gboolean rspamd_symbols_cache_add_condition (struct symbols_cache *cache, gint id, lua_State *L, gint cbref);

/**
* Find symbol in cache by id and returns its id
* @param 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);

/**
* Call function for cached symbol using saved callback
* @param task task object

Loading…
Cancel
Save