Skip to content

Commit

Permalink
filterx/filterx-variable: introduce "variable_type" at the variable l…
Browse files Browse the repository at this point in the history
…ayer

Previously the type enum was only used during the initialization of
FilterXExprVariable which was then translated to a set of booleans
at the variable layer. Also clean up naming a bit.

Signed-off-by: Balazs Scheidler <[email protected]>
  • Loading branch information
bazsi committed Jan 7, 2025
1 parent 24aed20 commit ae41725
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 57 deletions.
27 changes: 14 additions & 13 deletions lib/filterx/expr-variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@
typedef struct _FilterXVariableExpr
{
FilterXExpr super;
FilterXObject *variable_name;
FilterXVariableType variable_type;
NVHandle handle;
guint32 declared:1, handle_is_macro:1;
FilterXObject *variable_name;
guint32 handle_is_macro:1;
} FilterXVariableExpr;

static FilterXObject *
Expand All @@ -61,7 +62,7 @@ _pull_variable_from_message(FilterXVariableExpr *self, FilterXEvalContext *conte
static void
_whiteout_variable(FilterXVariableExpr *self, FilterXEvalContext *context)
{
filterx_scope_register_variable(context->scope, self->handle, NULL);
filterx_scope_register_variable(context->scope, FX_VAR_MESSAGE_TIED, self->handle, NULL);
}

static FilterXObject *
Expand All @@ -87,7 +88,7 @@ _eval(FilterXExpr *s)
FilterXObject *msg_ref = _pull_variable_from_message(self, context, context->msg);
if(!msg_ref)
return NULL;
filterx_scope_register_variable(context->scope, self->handle, msg_ref);
filterx_scope_register_variable(context->scope, FX_VAR_MESSAGE_TIED, self->handle, msg_ref);
return msg_ref;
}

Expand Down Expand Up @@ -124,10 +125,7 @@ _assign(FilterXExpr *s, FilterXObject *new_value)
/* NOTE: we pass NULL as initial_value to make sure the new variable
* is considered changed due to the assignment */

if (self->declared)
variable = filterx_scope_register_declared_variable(scope, self->handle, NULL);
else
variable = filterx_scope_register_variable(scope, self->handle, NULL);
variable = filterx_scope_register_variable(scope, self->variable_type, self->handle, NULL);
}

/* this only clones mutable objects */
Expand Down Expand Up @@ -217,7 +215,7 @@ _deinit(FilterXExpr *s, GlobalConfig *cfg)
}

static FilterXExpr *
filterx_variable_expr_new(FilterXString *name, FilterXVariableType type)
filterx_variable_expr_new(FilterXString *name, FilterXVariableType variable_type)
{
FilterXVariableExpr *self = g_new0(FilterXVariableExpr, 1);

Expand All @@ -231,9 +229,10 @@ filterx_variable_expr_new(FilterXString *name, FilterXVariableType type)
self->super.is_set = _isset;
self->super.unset = _unset;

self->variable_type = variable_type;
self->variable_name = (FilterXObject *) name;
self->handle = filterx_map_varname_to_handle(filterx_string_get_value_ref(self->variable_name, NULL), type);
if (type == FX_VAR_MESSAGE)
self->handle = filterx_map_varname_to_handle(filterx_string_get_value_ref(self->variable_name, NULL), variable_type);
if (variable_type == FX_VAR_MESSAGE_TIED)
self->handle_is_macro = log_msg_is_handle_macro(filterx_variable_handle_to_nv_handle(self->handle));

return &self->super;
Expand All @@ -242,7 +241,7 @@ filterx_variable_expr_new(FilterXString *name, FilterXVariableType type)
FilterXExpr *
filterx_msg_variable_expr_new(FilterXString *name)
{
return filterx_variable_expr_new(name, FX_VAR_MESSAGE);
return filterx_variable_expr_new(name, FX_VAR_MESSAGE_TIED);
}

FilterXExpr *
Expand All @@ -257,5 +256,7 @@ filterx_variable_expr_declare(FilterXExpr *s)
FilterXVariableExpr *self = (FilterXVariableExpr *) s;

g_assert(s->eval == _eval);
self->declared = TRUE;
/* we can only declare a floating variable */
g_assert(self->variable_type == FX_VAR_FLOATING);
self->variable_type = FX_VAR_DECLARED_FLOATING;
}
21 changes: 4 additions & 17 deletions lib/filterx/filterx-scope.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ filterx_scope_lookup_variable(FilterXScope *self, FilterXVariableHandle handle)

static FilterXVariable *
_register_variable(FilterXScope *self,
FilterXVariableType variable_type,
FilterXVariableHandle handle,
FilterXObject *initial_value)
{
Expand Down Expand Up @@ -142,19 +143,19 @@ _register_variable(FilterXScope *self,


FilterXVariable v;
filterx_variable_init_instance(&v, handle, initial_value, self->generation);
filterx_variable_init_instance(&v, variable_type, handle, initial_value, self->generation);
g_array_insert_val(self->variables, v_index, v);

return &g_array_index(self->variables, FilterXVariable, v_index);
}

FilterXVariable *
filterx_scope_register_variable(FilterXScope *self,
FilterXVariableType variable_type,
FilterXVariableHandle handle,
FilterXObject *initial_value)
{
FilterXVariable *v = _register_variable(self, handle, initial_value);
filterx_variable_set_declared(v, FALSE);
FilterXVariable *v = _register_variable(self, variable_type, handle, initial_value);

/* the scope needs to be synced with the message if it holds a
* message-tied variable (e.g. $MSG) */
Expand All @@ -163,20 +164,6 @@ filterx_scope_register_variable(FilterXScope *self,
return v;
}

FilterXVariable *
filterx_scope_register_declared_variable(FilterXScope *self,
FilterXVariableHandle handle,
FilterXObject *initial_value)

{
g_assert(filterx_variable_handle_is_floating(handle));

FilterXVariable *v = _register_variable(self, handle, initial_value);
filterx_variable_set_declared(v, TRUE);

return v;
}

gboolean
filterx_scope_foreach_variable(FilterXScope *self, FilterXScopeForeachFunc func, gpointer user_data)
{
Expand Down
4 changes: 1 addition & 3 deletions lib/filterx/filterx-scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ void filterx_scope_sync(FilterXScope *self, LogMessage *msg);

FilterXVariable *filterx_scope_lookup_variable(FilterXScope *self, FilterXVariableHandle handle);
FilterXVariable *filterx_scope_register_variable(FilterXScope *self,
FilterXVariableType variable_type,
FilterXVariableHandle handle,
FilterXObject *initial_value);
FilterXVariable *filterx_scope_register_declared_variable(FilterXScope *self,
FilterXVariableHandle handle,
FilterXObject *initial_value);
gboolean filterx_scope_foreach_variable(FilterXScope *self, FilterXScopeForeachFunc func, gpointer user_data);
void filterx_scope_invalidate_log_msg_cache(FilterXScope *self);

Expand Down
13 changes: 8 additions & 5 deletions lib/filterx/filterx-variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
FilterXVariableHandle
filterx_map_varname_to_handle(const gchar *name, FilterXVariableType type)
{
if (type == FX_VAR_MESSAGE)
if (type == FX_VAR_MESSAGE_TIED)
name++;

NVHandle nv_handle = log_msg_get_value_handle(name);

if (type == FX_VAR_MESSAGE)
if (type == FX_VAR_MESSAGE_TIED)
return (FilterXVariableHandle) nv_handle;
return (FilterXVariableHandle) nv_handle | FILTERX_HANDLE_FLOATING_BIT;
}
Expand All @@ -45,12 +45,15 @@ filterx_variable_clear(FilterXVariable *v)
}

void
filterx_variable_init_instance(FilterXVariable *v, FilterXVariableHandle handle,
FilterXObject *initial_value, guint32 generation)
filterx_variable_init_instance(FilterXVariable *v,
FilterXVariableType variable_type,
FilterXVariableHandle handle,
FilterXObject *initial_value,
guint32 generation)
{
v->handle = handle;
v->variable_type = variable_type;
v->assigned = FALSE;
v->declared = FALSE;
v->generation = generation;
v->value = filterx_object_ref(initial_value);
}
21 changes: 9 additions & 12 deletions lib/filterx/filterx-variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

typedef enum
{
FX_VAR_MESSAGE,
FX_VAR_MESSAGE_TIED,
FX_VAR_FLOATING,
FX_VAR_DECLARED,
FX_VAR_DECLARED_FLOATING,
} FilterXVariableType;

#define FILTERX_SCOPE_MAX_GENERATION ((1UL << 20) - 1)
Expand Down Expand Up @@ -72,13 +72,16 @@ typedef struct _FilterXVariable
* declared -- this variable is declared (e.g. retained for the entire input pipeline)
*/
guint32 assigned:1,
declared:1,
variable_type:2,
generation:20;
FilterXObject *value;
} FilterXVariable;

void filterx_variable_init_instance(FilterXVariable *v, FilterXVariableHandle handle,
FilterXObject *initial_value, guint32 generation);
void filterx_variable_init_instance(FilterXVariable *v,
FilterXVariableType variable_type,
FilterXVariableHandle handle,
FilterXObject *initial_value,
guint32 generation);
void filterx_variable_clear(FilterXVariable *v);

static inline gboolean
Expand Down Expand Up @@ -134,7 +137,7 @@ filterx_variable_is_set(FilterXVariable *v)
static inline gboolean
filterx_variable_is_declared(FilterXVariable *v)
{
return v->declared;
return v->variable_type == FX_VAR_DECLARED_FLOATING;
}

static inline gboolean
Expand All @@ -161,10 +164,4 @@ filterx_variable_unassign(FilterXVariable *v)
v->assigned = FALSE;
}

static inline void
filterx_variable_set_declared(FilterXVariable *v, gboolean declared)
{
v->declared = declared;
}

#endif
11 changes: 4 additions & 7 deletions lib/filterx/func-vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,11 @@ _load_from_dict(FilterXObject *key, FilterXObject *value, gpointer user_data)
return FALSE;
}

gboolean is_floating = key_str[0] != '$';
FilterXVariableHandle handle = filterx_map_varname_to_handle(key_str, is_floating ? FX_VAR_FLOATING : FX_VAR_MESSAGE);
FilterXVariableType variable_type = (key_str[0] == '$') ? FX_VAR_MESSAGE_TIED : FX_VAR_DECLARED_FLOATING;
FilterXVariableHandle handle = filterx_map_varname_to_handle(key_str, variable_type);

FilterXVariable *variable = NULL;
if (is_floating)
variable = filterx_scope_register_declared_variable(scope, handle, NULL);
else
variable = filterx_scope_register_variable(scope, handle, NULL);
variable = filterx_scope_register_variable(scope, variable_type, handle, NULL);

FilterXObject *cloned_value = filterx_object_clone(value);
filterx_variable_set_value(variable, cloned_value);
Expand All @@ -143,7 +140,7 @@ _load_from_dict(FilterXObject *key, FilterXObject *value, gpointer user_data)
evt_tag_str("key", key_str),
evt_tag_str("value", repr->str),
evt_tag_str("type", value->type->name),
evt_tag_str("variable_type", is_floating ? "declared" : "message"));
evt_tag_int("variable_type", variable_type));
}

return TRUE;
Expand Down

0 comments on commit ae41725

Please sign in to comment.