Skip to content

Commit

Permalink
[Rework] Rework expression API
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed May 17, 2019
1 parent 29a3b94 commit 957e212
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 101 deletions.
6 changes: 3 additions & 3 deletions src/libmime/mime_expressions.c
Expand Up @@ -87,7 +87,7 @@ static gboolean rspamd_has_flag_expr (struct rspamd_task *task,

static rspamd_expression_atom_t * rspamd_mime_expr_parse (const gchar *line, gsize len,
rspamd_mempool_t *pool, gpointer ud, GError **err);
static gdouble rspamd_mime_expr_process (struct rspamd_expr_process_data *process_data, rspamd_expression_atom_t *atom);
static gdouble rspamd_mime_expr_process (void *ud, rspamd_expression_atom_t *atom);
static gint rspamd_mime_expr_priority (rspamd_expression_atom_t *atom);
static void rspamd_mime_expr_destroy (rspamd_expression_atom_t *atom);

Expand Down Expand Up @@ -1070,9 +1070,9 @@ rspamd_mime_expr_process_function (struct rspamd_function_atom * func,
}

static gdouble
rspamd_mime_expr_process (struct rspamd_expr_process_data *process_data, rspamd_expression_atom_t *atom)
rspamd_mime_expr_process (void *ud, rspamd_expression_atom_t *atom)
{
struct rspamd_task *task = process_data->task;
struct rspamd_task *task = (struct rspamd_task *)ud;
struct rspamd_mime_atom *mime_atom;
lua_State *L;
gdouble ret = 0;
Expand Down
15 changes: 5 additions & 10 deletions src/libserver/composites.c
Expand Up @@ -68,7 +68,7 @@ struct symbol_remove_data {

static rspamd_expression_atom_t * rspamd_composite_expr_parse (const gchar *line, gsize len,
rspamd_mempool_t *pool, gpointer ud, GError **err);
static gdouble rspamd_composite_expr_process (struct rspamd_expr_process_data *process_data, rspamd_expression_atom_t *atom);
static gdouble rspamd_composite_expr_process (void *ud, rspamd_expression_atom_t *atom);
static gint rspamd_composite_expr_priority (rspamd_expression_atom_t *atom);
static void rspamd_composite_expr_destroy (rspamd_expression_atom_t *atom);
static void composites_foreach_callback (gpointer key, gpointer value, void *data);
Expand Down Expand Up @@ -253,10 +253,10 @@ rspamd_composite_process_symbol_removal (rspamd_expression_atom_t *atom,
}

static gdouble
rspamd_composite_expr_process (struct rspamd_expr_process_data *process_data,
rspamd_composite_expr_process (void *ud,
rspamd_expression_atom_t *atom)
{
struct composites_data *cd = process_data->cd;
struct composites_data *cd = (struct composites_data *)ud;
const gchar *beg = atom->data, *sym = NULL;

struct rspamd_symbol_result *ms = NULL;
Expand Down Expand Up @@ -442,13 +442,8 @@ composites_foreach_callback (gpointer key, gpointer value, void *data)
return;
}

struct rspamd_expr_process_data process_data;
memset (&process_data, 0, sizeof process_data);

process_data.flags = RSPAMD_EXPRESSION_FLAG_NOOPT;
process_data.cd = cd;

rc = rspamd_process_expression (comp->expr, &process_data);
rc = rspamd_process_expression (comp->expr, RSPAMD_EXPRESSION_FLAG_NOOPT,
cd);

/* Checked bit */
setbit (cd->checked, comp->id * 2);
Expand Down
38 changes: 30 additions & 8 deletions src/libutil/expression.c
Expand Up @@ -57,6 +57,14 @@ struct rspamd_expression {
guint evals;
};

struct rspamd_expr_process_data {
gpointer *ud;
gint flags;
/* != NULL if trace is collected */
GPtrArray *trace;
rspamd_expression_process_cb process_closure;
};

static GQuark
rspamd_expr_quark (void)
{
Expand Down Expand Up @@ -1021,7 +1029,7 @@ rspamd_ast_process_node (struct rspamd_expression *expr, GNode *node,
t1 = rspamd_get_ticks (TRUE);
}

elt->value = process_data->process_closure (process_data, elt->p.atom);
elt->value = process_data->process_closure (process_data->ud, elt->p.atom);

if (fabs (elt->value) > 1e-9) {
elt->p.atom->hits ++;
Expand Down Expand Up @@ -1103,8 +1111,11 @@ rspamd_ast_cleanup_traverse (GNode *n, gpointer d)
gdouble
rspamd_process_expression_closure (struct rspamd_expression *expr,
rspamd_expression_process_cb cb,
struct rspamd_expr_process_data *process_data)
gint flags,
gpointer runtime_ud,
GPtrArray **track)
{
struct rspamd_expr_process_data pd;
gdouble ret = 0;

g_assert (expr != NULL);
Expand All @@ -1113,8 +1124,16 @@ rspamd_process_expression_closure (struct rspamd_expression *expr,

expr->evals ++;

process_data->process_closure = cb;
ret = rspamd_ast_process_node (expr, expr->ast, process_data);
pd.process_closure = cb;
pd.flags = flags;
pd.ud = runtime_ud;

if (track) {
pd.trace = g_ptr_array_sized_new (32);
*track = pd.trace;
}

ret = rspamd_ast_process_node (expr, expr->ast, &pd);

/* Cleanup */
g_node_traverse (expr->ast, G_IN_ORDER, G_TRAVERSE_ALL, -1,
Expand All @@ -1138,18 +1157,21 @@ rspamd_process_expression_closure (struct rspamd_expression *expr,

gdouble
rspamd_process_expression_track (struct rspamd_expression *expr,
struct rspamd_expr_process_data *process_data)
gint flags,
gpointer runtime_ud,
GPtrArray **track)
{
return rspamd_process_expression_closure (expr,
expr->subr->process, process_data);
expr->subr->process, flags, runtime_ud, track);
}

gdouble
rspamd_process_expression (struct rspamd_expression *expr,
struct rspamd_expr_process_data *process_data)
gint flags,
gpointer runtime_ud)
{
return rspamd_process_expression_closure (expr,
expr->subr->process, process_data);
expr->subr->process, flags, runtime_ud, NULL);
}

static gboolean
Expand Down
28 changes: 9 additions & 19 deletions src/libutil/expression.h
Expand Up @@ -56,24 +56,9 @@ typedef struct rspamd_expression_atom_s {
gint priority;
} rspamd_expression_atom_t;

struct rspamd_expr_process_data;

typedef gdouble (*rspamd_expression_process_cb)(struct rspamd_expr_process_data *process_data,
typedef gdouble (*rspamd_expression_process_cb)(gpointer runtime_data,
rspamd_expression_atom_t *atom);

struct rspamd_expr_process_data {
/* Current Lua state to run atom processing */
struct lua_State *L;
/* Parameter of lua-function processing atom*/
gint stack_item;
gint flags;
/* != NULL if trace is collected */
GPtrArray *trace;
struct composites_data *cd;
struct rspamd_task *task;
rspamd_expression_process_cb process_closure;
};

struct rspamd_atom_subr {
/* Parses atom from string and returns atom structure */
rspamd_expression_atom_t * (*parse)(const gchar *line, gsize len,
Expand Down Expand Up @@ -111,7 +96,8 @@ gboolean rspamd_parse_expression (const gchar *line, gsize len,
* @return the value of expression
*/
gdouble rspamd_process_expression (struct rspamd_expression *expr,
struct rspamd_expr_process_data *process_data);
gint flags,
gpointer runtime_ud);

/**
* Process the expression and return its value using atom 'process' functions with the specified data pointer.
Expand All @@ -122,7 +108,9 @@ gdouble rspamd_process_expression (struct rspamd_expression *expr,
* @return the value of expression
*/
gdouble rspamd_process_expression_track (struct rspamd_expression *expr,
struct rspamd_expr_process_data *process_data);
gint flags,
gpointer runtime_ud,
GPtrArray **track);

/**
* Process the expression with the custom processor
Expand All @@ -133,7 +121,9 @@ gdouble rspamd_process_expression_track (struct rspamd_expression *expr,
*/
gdouble rspamd_process_expression_closure (struct rspamd_expression *expr,
rspamd_expression_process_cb cb,
struct rspamd_expr_process_data *process_data);
gint flags,
gpointer runtime_ud,
GPtrArray **track);
/**
* Shows string representation of an expression
* @param expr expression to show
Expand Down

0 comments on commit 957e212

Please sign in to comment.