Skip to content

Commit

Permalink
json: deep freeze objects in cache_json_file() filterx fn
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
  • Loading branch information
alltilla committed Jun 1, 2024
1 parent c89b0dd commit 1f04874
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
3 changes: 1 addition & 2 deletions lib/filterx/object-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ filterx_json_convert_json_to_object_cached(FilterXObject *self, FilterXWeakRef *
return filterx_object_ref(filterx_obj);

filterx_obj = filterx_json_convert_json_to_object(self, root_container, jso);
if (!filterx_object_is_frozen(self))
filterx_json_associate_cached_object(jso, filterx_obj);
filterx_json_associate_cached_object(jso, filterx_obj);
return filterx_obj;
}

Expand Down
57 changes: 54 additions & 3 deletions modules/json/filterx-cache-json-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "filterx-cache-json-file.h"
#include "filterx/object-json.h"
#include "filterx/object-string.h"
#include "filterx/object-list-interface.h"
#include "filterx/object-dict-interface.h"
#include "filterx/expr-literal.h"
#include "filterx/filterx-eval.h"
#include "scratch-buffers.h"
Expand Down Expand Up @@ -54,6 +56,7 @@ typedef struct FilterXFunctionCacheJsonFile_
FilterXFunction super;
gchar *filepath;
FilterXObject *cached_json;
GPtrArray *frozen_objects;
} FilterXFuntionCacheJsonFile;

static gchar *
Expand Down Expand Up @@ -143,11 +146,56 @@ _free(FilterXExpr *s)
FilterXFuntionCacheJsonFile *self = (FilterXFuntionCacheJsonFile *) s;

g_free(self->filepath);
if (self->cached_json)
filterx_object_unfreeze_and_free(self->cached_json);
g_ptr_array_unref(self->frozen_objects);
filterx_function_free_method(&self->super);
}

static void _deep_freeze(FilterXFuntionCacheJsonFile *self, FilterXObject *object);

static void
_deep_freeze_dict(FilterXFuntionCacheJsonFile *self, FilterXObject *object)
{
struct json_object_iter itr;
json_object_object_foreachC(filterx_json_object_get_value(object), itr)
{
struct json_object *elem_jso = itr.val;
FilterXObject *elem_object = filterx_json_convert_json_to_object(self->cached_json, NULL, elem_jso);
_deep_freeze(self, elem_object);
filterx_json_associate_cached_object(elem_jso, elem_object);
}
}

static void
_deep_freeze_list(FilterXFuntionCacheJsonFile *self, FilterXObject *object)
{
struct json_object *jso = filterx_json_object_get_value(object);
guint64 len = json_object_array_length(jso);

for (guint64 i = 0; i < len; i++)
{
struct json_object *elem_jso = json_object_array_get_idx(jso, i);
FilterXObject *elem_object = filterx_json_convert_json_to_object(self->cached_json, NULL, elem_jso);
_deep_freeze(self, elem_object);
filterx_json_associate_cached_object(elem_jso, elem_object);
}
}

static void
_deep_freeze(FilterXFuntionCacheJsonFile *self, FilterXObject *object)
{
if (!object)
return;

if (filterx_object_freeze(object))
g_ptr_array_add(self->frozen_objects, object);

if (filterx_object_is_type(object, &FILTERX_TYPE_NAME(json_object)))
_deep_freeze_dict(self, object);

if (filterx_object_is_type(object, &FILTERX_TYPE_NAME(json_array)))
_deep_freeze_list(self, object);
}

FilterXFunction *
filterx_function_cache_json_file_new(const gchar *function_name, FilterXFunctionArgs *args, GError **error)
{
Expand All @@ -157,6 +205,8 @@ filterx_function_cache_json_file_new(const gchar *function_name, FilterXFunction
self->super.super.eval = _eval;
self->super.super.free_fn = _free;

self->frozen_objects = g_ptr_array_new_with_free_func((GDestroyNotify) filterx_object_unfreeze_and_free);

self->filepath = _extract_filepath(args, error);
if (!self->filepath)
goto error;
Expand All @@ -165,7 +215,8 @@ filterx_function_cache_json_file_new(const gchar *function_name, FilterXFunction
if (!self->cached_json)
goto error;

filterx_object_freeze(self->cached_json);
_deep_freeze(self, self->cached_json);

filterx_function_args_free(args);
return &self->super;

Expand Down

0 comments on commit 1f04874

Please sign in to comment.