Skip to content

Commit

Permalink
filterx: add skeleton for native FilterXDict
Browse files Browse the repository at this point in the history
Signed-off-by: László Várady <laszlo.varady@anno.io>
  • Loading branch information
MrAnno committed Nov 19, 2024
1 parent 3b42dc0 commit c3b970f
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/filterx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ set(FILTERX_HEADERS
filterx/func-vars.h
filterx/object-datetime.h
filterx/object-dict-interface.h
filterx/object-dict.h
filterx/object-extractor.h
filterx/object-json-internal.h
filterx/object-json.h
Expand Down Expand Up @@ -110,6 +111,7 @@ set(FILTERX_SOURCES
filterx/func-vars.c
filterx/object-datetime.c
filterx/object-dict-interface.c
filterx/object-dict.c
filterx/object-extractor.c
filterx/object-json-array.c
filterx/object-json-object.c
Expand Down
2 changes: 2 additions & 0 deletions lib/filterx/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ filterxinclude_HEADERS = \
lib/filterx/func-vars.h \
lib/filterx/object-datetime.h \
lib/filterx/object-dict-interface.h \
lib/filterx/object-dict.h \
lib/filterx/object-extractor.h \
lib/filterx/object-json-internal.h \
lib/filterx/object-json.h \
Expand Down Expand Up @@ -112,6 +113,7 @@ filterx_sources = \
lib/filterx/func-vars.c \
lib/filterx/object-datetime.c \
lib/filterx/object-dict-interface.c \
lib/filterx/object-dict.c \
lib/filterx/object-extractor.c \
lib/filterx/object-json-array.c \
lib/filterx/object-json-object.c \
Expand Down
3 changes: 3 additions & 0 deletions lib/filterx/filterx-globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "filterx/object-null.h"
#include "filterx/object-string.h"
#include "filterx/object-json.h"
#include "filterx/object-dict.h"
#include "filterx/object-datetime.h"
#include "filterx/object-message-value.h"
#include "filterx/object-list-interface.h"
Expand Down Expand Up @@ -88,6 +89,7 @@ static void
_simple_init(void)
{
filterx_builtin_simple_functions_init_private(&filterx_builtin_simple_functions);
g_assert(filterx_builtin_simple_function_register("dict", filterx_dict_new_from_args));
g_assert(filterx_builtin_simple_function_register("json", filterx_json_new_from_args));
g_assert(filterx_builtin_simple_function_register("json_array", filterx_json_array_new_from_args));
g_assert(filterx_builtin_simple_function_register("datetime", filterx_typecast_datetime));
Expand Down Expand Up @@ -237,6 +239,7 @@ filterx_global_init(void)

filterx_type_init(&FILTERX_TYPE_NAME(list));
filterx_type_init(&FILTERX_TYPE_NAME(dict));
filterx_type_init(&FILTERX_TYPE_NAME(dictobj));

filterx_type_init(&FILTERX_TYPE_NAME(null));
filterx_type_init(&FILTERX_TYPE_NAME(integer));
Expand Down
224 changes: 224 additions & 0 deletions lib/filterx/object-dict.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/*
* Copyright (c) 2024 Axoflow
* Copyright (c) 2024 László Várady
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/

#include "filterx/object-dict.h"
#include "filterx/object-dict-interface.h"
#include "filterx/object-extractor.h"
#include "filterx/object-json.h"
#include "filterx/filterx-object.h"
#include "filterx/filterx-object-istype.h"
#include "filterx/filterx-ref.h"
#include "filterx/filterx-eval.h"

#include "syslog-ng.h"
#include "str-utils.h"
#include "adt/iord_map.h"

typedef struct _FilterXDictKey
{
FilterXObject *key_obj;
IOrdMapNode n;
guint8 in_cache:1;
} FilterXDictKey;

/*
* FilterXDictValue is either a FilterXRef or an immutable FilterXObject.
* The union and the cache are used to avoid unnecessary allocations.
*
* - FilterXDictValue can be casted to FilterXRef (check _filterx_dict_value_is_ref()) and vice versa.
* - A FilterXRef is always owned and never cached.
* - An immutable FilterXObject is never owned and may be cached.
*
* These are important because certain preconditions must be met due to the intrusive IOrdMapNode and the cache.
*/
typedef union _FilterXDictValue
{
FilterXRef ref;
struct
{
FilterXObject __null_pad;
FilterXObject *val;
IOrdMapNode n;
guint8 in_cache:1;
} immutable;
} FilterXDictValue;

G_STATIC_ASSERT(offsetof(FilterXRef, n) == offsetof(FilterXDictValue, immutable.n));
G_STATIC_ASSERT(offsetof(FilterXDictValue, ref) == offsetof(FilterXDictValue, immutable.__null_pad));
G_STATIC_ASSERT(offsetof(FilterXDictValue, ref) == 0);

#define CACHE_SIZE 32

struct _FilterXDictObj
{
FilterXDict super;
IOrdMap map;

FilterXDictKey key_cache[CACHE_SIZE];
FilterXDictValue value_cache[CACHE_SIZE];
guint8 key_cache_len;
guint8 value_cache_len;
};

static gboolean
_filterx_dict_truthy(FilterXObject *s)
{
return TRUE;
}

static gboolean
_filterx_dict_marshal(FilterXObject *s, GString *repr, LogMessageValueType *t)
{
return TRUE;
}

static gboolean
_filterx_dict_repr(FilterXObject *s, GString *repr)
{
return TRUE;
}

static FilterXObject *
_filterx_dict_clone(FilterXObject *s)
{
return NULL;
}

static FilterXObject *
_filterx_dict_get_subscript(FilterXDict *s, FilterXObject *key)
{
return NULL;
}

static gboolean
_filterx_dict_set_subscript(FilterXDict *s, FilterXObject *key, FilterXObject **new_value)
{
return TRUE;
}

static gboolean
_filterx_dict_unset_key(FilterXDict *s, FilterXObject *key)
{
return TRUE;
}

static guint64
_filterx_dict_size(FilterXDict *s)
{
return 0;
}

static gboolean
_filterx_dict_foreach(FilterXDict *s, FilterXDictIterFunc func, gpointer user_data)
{
return TRUE;
}

static FilterXObject *
_filterx_dict_factory(FilterXObject *self)
{
return filterx_dict_new();
}

static void
_filterx_dict_free(FilterXObject *s)
{
filterx_object_free_method(s);
}

FilterXObject *
filterx_dict_new(void)
{
FilterXDictObj *self = g_new0(FilterXDictObj, 1);
filterx_dict_init_instance(&self->super, &FILTERX_TYPE_NAME(dictobj));

self->super.get_subscript = _filterx_dict_get_subscript;
self->super.set_subscript = _filterx_dict_set_subscript;
self->super.unset_key = _filterx_dict_unset_key;
self->super.len = _filterx_dict_size;
self->super.iter = _filterx_dict_foreach;

return &self->super.super;
}

FilterXObject *
filterx_dict_new_from_args(FilterXExpr *s, GPtrArray *args)
{
if (!args || args->len == 0)
return filterx_dict_new();

if (args->len != 1)
{
filterx_eval_push_error("Too many arguments", s, NULL);
return NULL;
}

FilterXObject *arg = (FilterXObject *) g_ptr_array_index(args, 0);

FilterXObject *arg_unwrapped = filterx_ref_unwrap_ro(arg);
if (filterx_object_is_type(arg_unwrapped, &FILTERX_TYPE_NAME(dictobj)))
return filterx_object_ref(arg);

if (filterx_object_is_type(arg_unwrapped, &FILTERX_TYPE_NAME(dict)))
{
FilterXObject *self = filterx_dict_new();
if (!filterx_dict_merge(self, arg_unwrapped))
{
filterx_object_unref(self);
return NULL;
}
return self;
}

const gchar *repr;
gsize repr_len;
if (filterx_object_extract_string_ref(arg, &repr, &repr_len))
{
FilterXObject *json = filterx_json_object_new_from_repr(repr, repr_len);

FilterXObject *self = filterx_dict_new();
if (!filterx_dict_merge(self, json))
{
filterx_object_unref(self);
filterx_object_unref(json);
return NULL;
}
filterx_object_unref(json);
return self;
}

filterx_eval_push_error_info("Argument must be a dict or a string", s,
g_strdup_printf("got \"%s\" instead", arg_unwrapped->type->name), TRUE);
return NULL;
}

FILTERX_DEFINE_TYPE(dictobj, FILTERX_TYPE_NAME(dict),
.is_mutable = TRUE,
.truthy = _filterx_dict_truthy,
.free_fn = _filterx_dict_free,
.marshal = _filterx_dict_marshal,
.repr = _filterx_dict_repr,
.clone = _filterx_dict_clone,
.dict_factory = _filterx_dict_factory,
);
37 changes: 37 additions & 0 deletions lib/filterx/object-dict.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 Axoflow
* Copyright (c) 2024 László Várady
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/

#ifndef FILTERX_OBJECT_DICT_H
#define FILTERX_OBJECT_DICT_H

#include "filterx/filterx-object.h"

typedef struct _FilterXDictObj FilterXDictObj;

FILTERX_DECLARE_TYPE(dictobj);

FilterXObject *filterx_dict_new(void);
FilterXObject *filterx_dict_new_from_args(FilterXExpr *s, GPtrArray *args);

#endif

0 comments on commit c3b970f

Please sign in to comment.