Skip to content

Commit 0a71567

Browse files
committed
json: deep freeze objects in cache_json_file() filterx fn
Signed-off-by: Attila Szakacs <attila.szakacs@axoflow.com>
1 parent c89b0dd commit 0a71567

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

lib/filterx/object-json.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ filterx_json_convert_json_to_object_cached(FilterXObject *self, FilterXWeakRef *
134134
return filterx_object_ref(filterx_obj);
135135

136136
filterx_obj = filterx_json_convert_json_to_object(self, root_container, jso);
137-
if (!filterx_object_is_frozen(self))
138-
filterx_json_associate_cached_object(jso, filterx_obj);
137+
filterx_json_associate_cached_object(jso, filterx_obj);
139138
return filterx_obj;
140139
}
141140

modules/json/filterx-cache-json-file.c

+53-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "filterx-cache-json-file.h"
2424
#include "filterx/object-json.h"
2525
#include "filterx/object-string.h"
26+
#include "filterx/object-list-interface.h"
27+
#include "filterx/object-dict-interface.h"
2628
#include "filterx/expr-literal.h"
2729
#include "filterx/filterx-eval.h"
2830
#include "scratch-buffers.h"
@@ -54,6 +56,7 @@ typedef struct FilterXFunctionCacheJsonFile_
5456
FilterXFunction super;
5557
gchar *filepath;
5658
FilterXObject *cached_json;
59+
GPtrArray *frozen_objects;
5760
} FilterXFuntionCacheJsonFile;
5861

5962
static gchar *
@@ -143,11 +146,56 @@ _free(FilterXExpr *s)
143146
FilterXFuntionCacheJsonFile *self = (FilterXFuntionCacheJsonFile *) s;
144147

145148
g_free(self->filepath);
146-
if (self->cached_json)
147-
filterx_object_unfreeze_and_free(self->cached_json);
149+
g_ptr_array_unref(self->frozen_objects);
148150
filterx_function_free_method(&self->super);
149151
}
150152

153+
static void _deep_freeze(FilterXFuntionCacheJsonFile *self, FilterXObject *object);
154+
155+
static void
156+
_deep_freeze_dict(FilterXFuntionCacheJsonFile *self, FilterXObject *object)
157+
{
158+
struct json_object_iter itr;
159+
json_object_object_foreachC(filterx_json_object_get_value(object), itr)
160+
{
161+
struct json_object *elem_jso = itr.val;
162+
FilterXObject *elem_object = filterx_json_convert_json_to_object(object, NULL, elem_jso);
163+
_deep_freeze(self, elem_object);
164+
filterx_json_associate_cached_object(elem_jso, elem_object);
165+
}
166+
}
167+
168+
static void
169+
_deep_freeze_list(FilterXFuntionCacheJsonFile *self, FilterXObject *object)
170+
{
171+
struct json_object *jso = filterx_json_object_get_value(object);
172+
guint64 len = json_object_array_length(jso);
173+
174+
for (guint64 i = 0; i < len; i++)
175+
{
176+
struct json_object *elem_jso = json_object_array_get_idx(jso, i);
177+
FilterXObject *elem_object = filterx_json_convert_json_to_object(object, NULL, elem_jso);
178+
_deep_freeze(self, elem_object);
179+
filterx_json_associate_cached_object(elem_jso, elem_object);
180+
}
181+
}
182+
183+
static void
184+
_deep_freeze(FilterXFuntionCacheJsonFile *self, FilterXObject *object)
185+
{
186+
if (!object)
187+
return;
188+
189+
if (filterx_object_freeze(object))
190+
g_ptr_array_add(self->frozen_objects, object);
191+
192+
if (filterx_object_is_type(object, &FILTERX_TYPE_NAME(json_object)))
193+
_deep_freeze_dict(self, object);
194+
195+
if (filterx_object_is_type(object, &FILTERX_TYPE_NAME(json_array)))
196+
_deep_freeze_list(self, object);
197+
}
198+
151199
FilterXFunction *
152200
filterx_function_cache_json_file_new(const gchar *function_name, FilterXFunctionArgs *args, GError **error)
153201
{
@@ -165,7 +213,9 @@ filterx_function_cache_json_file_new(const gchar *function_name, FilterXFunction
165213
if (!self->cached_json)
166214
goto error;
167215

168-
filterx_object_freeze(self->cached_json);
216+
self->frozen_objects = g_ptr_array_new_with_free_func((GDestroyNotify) filterx_object_unfreeze_and_free);
217+
_deep_freeze(self, self->cached_json);
218+
169219
filterx_function_args_free(args);
170220
return &self->super;
171221

0 commit comments

Comments
 (0)