Changeset: c403696ad873 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/c403696ad873
Modified Files:
        monetdb5/modules/atoms/json.c
        monetdb5/modules/atoms/pg_jsonpath/jsonpath.h
        monetdb5/modules/atoms/pg_jsonpath/jsonpath_exec.c
        monetdb5/modules/atoms/pg_jsonpath/jsonpath_scan.l
        monetdb5/modules/atoms/pg_jsonpath/postgres_defines.h
        monetdb5/modules/atoms/pg_jsonpath/postgres_defines_internal.h
Branch: json-extend
Log Message:

share main allocator between yyjson and pg_jsonpath


diffs (236 lines):

diff --git a/monetdb5/modules/atoms/json.c b/monetdb5/modules/atoms/json.c
--- a/monetdb5/modules/atoms/json.c
+++ b/monetdb5/modules/atoms/json.c
@@ -1644,6 +1644,18 @@ JSONfilterArrayDefault_hge(json *ret, co
 
 #include "jsonpath.h"
 
+static
+void* JSONalloc(void* ctx, size_t size) {
+       allocator* sa = ctx;
+       return sa_alloc(sa, size);
+}
+
+static
+void* JSONrealloc(void* ctx, void *ptr, size_t old_size, size_t size){
+       allocator* sa = ctx;
+       return sa_realloc(sa, ptr, size, old_size);
+}
+
 static str
 JSONfilter(json *ret, const json *js, const char *const *expr)
 {
@@ -1654,17 +1666,22 @@ JSONfilter(json *ret, const json *js, co
        }
 
        char errmsg[1024] = {0};
-       struct Node* escontext = init_escontext(errmsg); // TODO: can be a 
implementation detail of lex and parse. Pass on allocator as parameter
-       if (!escontext)
+       allocator* sa = sa_create(NULL);
+       if (!sa)
                throw(MAL, "json.filter", SQLSTATE(HY013) MAL_MALLOC_FAIL);
-
-       JsonPathParseResult* path = parsejsonpath(*expr, strlen(*expr), 
escontext);
+       JsonPathParseResult* path = parsejsonpath(*expr, strlen(*expr), sa, 
errmsg);
        if (errmsg[0]) {
+               sa_destroy(sa);
                return createException(MAL, SQLSTATE(HY013), "JsonPathQuery 
iternal error: %s", errmsg);
        }
        assert(path);
 
-       yyjson_alc* alc = yyjson_alc_dyn_new(); // TODO initialize this with 
gdk memory functions.
+       yyjson_alc _alc = {0};
+       yyjson_alc* alc = &_alc;
+       alc->malloc = JSONalloc;
+       alc->realloc = JSONrealloc;
+       alc->ctx = sa;
+
        yyjson_read_err* read_error = NULL;
        yyjson_doc *doc = yyjson_read_opts(*js, strlen(*js), 0, alc, 
read_error);
        yyjson_val *root = yyjson_doc_get_root(doc);
@@ -1673,20 +1690,23 @@ JSONfilter(json *ret, const json *js, co
        List* vars = NULL;
        const char *column_name = NULL;
        yyjson_val *res = JsonPathQuery((Datum) root, path, JSW_UNCONDITIONAL, 
&empty, &error, vars, column_name, alc, errmsg);
-       if (!res && errmsg[0])
+       if (!res && errmsg[0]) {
+               sa_destroy(sa);
                return createException(MAL, SQLSTATE(HY013), "JsonPathQuery 
iternal error: %s", errmsg);
+       }
 
-       if (!res)
+       if (!res) {
+               sa_destroy(sa);
                throw(MAL, "json.filter", SQLSTATE(HY013) "JsonPathQuery 
error");
+       }
 
        size_t* len = NULL;
        yyjson_write_err* write_error = NULL;
        char* tmp_res = yyjson_val_write_opts(res, 0, alc, len, write_error); 
// TODO use different allocator for result
 
        *ret = GDKstrdup(tmp_res);
-       yyjson_alc_dyn_free(alc);
+       sa_destroy(sa);
        return MAL_SUCCEED;
-       // return JSONfilterInternal(ret, js, expr, 0);
 }
 
 // glue all values together with an optional separator
diff --git a/monetdb5/modules/atoms/pg_jsonpath/jsonpath.h 
b/monetdb5/modules/atoms/pg_jsonpath/jsonpath.h
--- a/monetdb5/modules/atoms/pg_jsonpath/jsonpath.h
+++ b/monetdb5/modules/atoms/pg_jsonpath/jsonpath.h
@@ -177,7 +177,7 @@ extern const char *jspOperationName(Json
 extern struct Node * init_escontext(char* errmsg);
 
 extern JsonPathParseResult *parsejsonpath(const char *str, int len,
-                                                                               
  struct Node *escontext);
+                                                                               
        allocator* sa, char* errmsg);
 /*
 extern bool jspConvertRegexFlags(uint32 xflags, int *result,
                                                                 struct Node 
*escontext);
diff --git a/monetdb5/modules/atoms/pg_jsonpath/jsonpath_exec.c 
b/monetdb5/modules/atoms/pg_jsonpath/jsonpath_exec.c
--- a/monetdb5/modules/atoms/pg_jsonpath/jsonpath_exec.c
+++ b/monetdb5/modules/atoms/pg_jsonpath/jsonpath_exec.c
@@ -88,6 +88,7 @@ typedef struct JsonPathExecContext
        JsonBaseObjectInfo baseObject;  /* "base object" for .keyvalue()
                                                                         * 
evaluation */
        yyjson_alc              *alc;
+       allocator               *sa;
        yyjson_mut_doc  *mutable_doc;
        int                     lastGeneratedObjectId;  /* "id" counter for 
.keyvalue()
                                                                                
 * evaluation */
@@ -245,7 +246,7 @@ static JsonPathExecResult getArrayIndex(
 static JsonBaseObjectInfo setBaseObject(JsonPathExecContext *cxt,
                                                                                
JsonbValue *jbv, int32 id);
 // REPLACED static void JsonValueListClear(JsonValueList *jvl);
-static void JsonValueListAppend(JsonValueList *jvl, JsonbValue *jbv);
+static void JsonValueListAppend(JsonPathExecContext *cxt, JsonValueList *jvl, 
JsonbValue *jbv);
 static int     JsonValueListLength(const JsonValueList *jvl);
 static bool JsonValueListIsEmpty(JsonValueList *jvl);
 static JsonbValue *JsonValueListHead(JsonValueList *jvl);
@@ -1127,7 +1128,7 @@ executeNextItem(JsonPathExecContext *cxt
                return executeItem(cxt, next, v, found);
 
        if (found)
-               JsonValueListAppend(found, v);
+               JsonValueListAppend(cxt, found, v);
 
        return jperOk;
 }
@@ -1159,7 +1160,7 @@ executeItemOptUnwrapResult(JsonPathExecC
                        if (JsonbType(item) == jbvArray)
                                executeItemUnwrapTargetArray(cxt, NULL, item, 
found, false);
                        else
-                               JsonValueListAppend(found, item);
+                               JsonValueListAppend(cxt, found, item);
                }
 
                return jperOk;
@@ -1409,7 +1410,7 @@ executeAnyItem(JsonPathExecContext *cxt,
                                        break;
                        }
                        else if (found)
-                               JsonValueListAppend(found, val);
+                               JsonValueListAppend(cxt, found, val);
                        else
                                return jperOk;
                }
@@ -2146,10 +2147,11 @@ setBaseObject(JsonPathExecContext *cxt, 
 
 
 static void
-JsonValueListAppend(JsonValueList *jvl, JsonbValue *jbv)
+JsonValueListAppend(JsonPathExecContext *cxt, JsonValueList *jvl, JsonbValue 
*jbv)
 {
        if (jvl->singleton)
        {
+               #define escontext cxt /*trick to have the macro work*/
                jvl->list = list_make2(jvl->singleton, jbv);
                jvl->singleton = NULL;
        }
@@ -2288,13 +2290,15 @@ JsonPathExists(Datum jb, JsonPath *jp, b
 {
        JsonPathExecResult res;
 
-       JsonPathExecContext cxt = {0};
-       cxt.alc = alc;
-       cxt._errmsg = errmsg;
+       JsonPathExecContext _cxt = {0};
+       _cxt.alc = alc;
+       _cxt.sa = _cxt.alc->ctx;
+       _cxt._errmsg = errmsg;
+       JsonPathExecContext *cxt = &_cxt;
 
        res = executeJsonPath(jp, vars,
                                                  GetJsonPathVar, 
CountJsonPathVars,
-                                                 DatumGetJsonbP(jb), !error, 
NULL, true, &cxt);
+                                                 DatumGetJsonbP(jb), !error, 
NULL, true, cxt);
        if (!jperIsError(res) || errmsg[0])
                return false; // throw exception
 
@@ -2325,6 +2329,7 @@ JsonPathQuery(Datum jb, JsonPath *jp, Js
 
        JsonPathExecContext _cxt = {0};
        _cxt.alc = alc;
+       _cxt.sa = _cxt.alc->ctx;
        _cxt._errmsg = errmsg;
        JsonPathExecContext* cxt = &_cxt;
 
@@ -2427,6 +2432,7 @@ JsonPathValue(Datum jb, JsonPath *jp, bo
 
        JsonPathExecContext _cxt = {0};
        _cxt.alc = alc;
+       _cxt.sa = _cxt.alc->ctx;
        _cxt._errmsg = errmsg;
        JsonPathExecContext* cxt = &_cxt;
 
diff --git a/monetdb5/modules/atoms/pg_jsonpath/jsonpath_scan.l 
b/monetdb5/modules/atoms/pg_jsonpath/jsonpath_scan.l
--- a/monetdb5/modules/atoms/pg_jsonpath/jsonpath_scan.l
+++ b/monetdb5/modules/atoms/pg_jsonpath/jsonpath_scan.l
@@ -564,14 +564,17 @@ init_escontext(char* errmsg)
 
 /* Interface to jsonpath parser */
 JsonPathParseResult *
-parsejsonpath(const char *str, int len, struct Node *escontext)
+parsejsonpath(const char *str, int len, allocator* sa, char* errmsg)
 {
+       struct Node escontext;
+       escontext.sa = sa;
+       escontext._errmsg = errmsg;
        JsonPathParseResult     *parseresult;
 
        jsonpath_scanner_init(str, len);
 
-       if (jsonpath_yyparse((void *) &parseresult, escontext) != 0)
-               jsonpath_yyerror(NULL, escontext, "invalid input"); /* 
shouldn't happen */
+       if (jsonpath_yyparse((void *) &parseresult, &escontext) != 0)
+               jsonpath_yyerror(NULL, &escontext, "invalid input"); /* 
shouldn't happen */
 
        jsonpath_scanner_finish();
 
diff --git a/monetdb5/modules/atoms/pg_jsonpath/postgres_defines.h 
b/monetdb5/modules/atoms/pg_jsonpath/postgres_defines.h
--- a/monetdb5/modules/atoms/pg_jsonpath/postgres_defines.h
+++ b/monetdb5/modules/atoms/pg_jsonpath/postgres_defines.h
@@ -47,8 +47,6 @@ typedef uintptr_t Datum;
 // postgres_ext.h
 typedef unsigned int Oid;
 
-struct Node;
-
 #if 0
 // numeric.c
 typedef int16 NumericDigit;
diff --git a/monetdb5/modules/atoms/pg_jsonpath/postgres_defines_internal.h 
b/monetdb5/modules/atoms/pg_jsonpath/postgres_defines_internal.h
--- a/monetdb5/modules/atoms/pg_jsonpath/postgres_defines_internal.h
+++ b/monetdb5/modules/atoms/pg_jsonpath/postgres_defines_internal.h
@@ -34,8 +34,8 @@ typedef struct Node
 #define Max(A,B) MAX(A, B)
 #define Min(A,B) MIN(A, B)
 
-#define list_make1(X) list_add(NULL, X)
-#define list_make2(X, Y) list_append(list_add(NULL, X), Y)
+#define list_make1(X) list_add(sa_list(escontext->sa), X)
+#define list_make2(X, Y) list_append(list_add(sa_list(escontext->sa), X), Y)
 #define lappend(X, Y) list_append(X, Y)
 
 #define linitial(L) (L->h ? L->h->data : NULL)
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to