Skip to content

Commit 048b449

Browse files
author
Nikita Glukhov
committed
Add JsonValueInitBinary()
1 parent 75f4598 commit 048b449

File tree

6 files changed

+24
-47
lines changed

6 files changed

+24
-47
lines changed

src/backend/utils/adt/json_generic.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,10 @@ JsonValueUnwrap(const JsonValue *val, JsonValue *valbuf)
153153
JsonValue *
154154
JsonValueWrapInBinary(const JsonValue *val, JsonValue *bin)
155155
{
156-
JsonContainer *jc = JsonValueToContainer(val);
157-
158156
if (!bin)
159157
bin = (JsonValue *) palloc(sizeof(JsonValue));
160158

161-
bin->type = jbvBinary;
162-
bin->val.binary.data = jc;
163-
bin->val.binary.uniquified = JsonValueIsUniquified(val);
164-
165-
return bin;
159+
return JsonValueInitBinary(bin, JsonValueToContainer(val));
166160
}
167161

168162
static inline JsonValue *
@@ -716,11 +710,7 @@ JsonToJsonValue(Json *json, JsonValue *jv)
716710
if (!jv)
717711
jv = palloc(sizeof(JsonValue));
718712

719-
jv->type = jbvBinary;
720-
jv->val.binary.data = &json->root;
721-
jv->val.binary.uniquified = json->root.ops != &jsontContainerOps;
722-
723-
return jv;
713+
return JsonValueInitBinary(jv, &json->root);
724714
}
725715

726716
#ifdef JSON_FLATTEN_INTO_JSONEXT

src/backend/utils/adt/jsonb_util.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,7 @@ JsonContainerFlatten(JsonContainer *jc, JsonValueEncoder encoder,
136136
if (binary)
137137
Assert(binary->type == jbvBinary);
138138
else
139-
{
140-
jbv.type = jbvBinary;
141-
jbv.val.binary.data = jc;
142-
jbv.val.binary.uniquified = JsonContainerIsUniquified(jc);
143-
144-
binary = &jbv;
145-
}
139+
binary = JsonValueInitBinary(&jbv, jc);
146140

147141
if (!binary->val.binary.uniquified)
148142
binary = JsonValueUniquify(&uniquified, binary);
@@ -654,15 +648,14 @@ fillJsonbValue(const JsonbContainer *container, int index,
654648
}
655649
else
656650
{
651+
JsonContainerData *cont = JsonContainerAlloc();
657652
Assert(JBE_ISCONTAINER(entry));
658-
result->type = jbvBinary;
659-
result->val.binary.data = JsonContainerAlloc();
660-
jsonbInitContainer((JsonContainerData *) result->val.binary.data,
653+
jsonbInitContainer(cont,
661654
/* Remove alignment padding from data pointer and length */
662655
(JsonbContainer *)(base_addr + INTALIGN(offset)),
663656
getJsonbLength(container, index) -
664657
(INTALIGN(offset) - offset));
665-
result->val.binary.uniquified = true;
658+
JsonValueInitBinary(result, cont);
666659
}
667660
}
668661

src/backend/utils/adt/jsonfuncs.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3538,9 +3538,7 @@ populate_record_worker(FunctionCallInfo fcinfo, const char *funcname,
35383538
jsv.val.jsonb = &jbv;
35393539

35403540
/* fill binary jsonb value pointing to jb */
3541-
jbv.type = jbvBinary;
3542-
jbv.val.binary.data = &jb->root;
3543-
jbv.val.binary.uniquified = true;
3541+
JsonValueInitBinary(&jbv, JsonRoot(jb));
35443542
}
35453543

35463544
rettuple = populate_composite(&cache->c.io.composite, cache->argtype,

src/backend/utils/adt/jsonpath_exec.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ static void JsonValueListInitIterator(const JsonValueList *jvl,
247247
static JsonbValue *JsonValueListNext(const JsonValueList *jvl,
248248
JsonValueListIterator *it);
249249
static int JsonbType(JsonbValue *jb);
250-
static JsonbValue *JsonbInitBinary(JsonbValue *jbv, Jsonb *jb);
251250
static int JsonbType(JsonbValue *jb);
252251
static JsonbValue *getScalar(JsonbValue *scalar, enum jbvType type);
253252
static JsonbValue *wrapItemsInArray(const JsonValueList *items);
@@ -646,7 +645,7 @@ executeJsonPath(JsonPath *path, Jsonb *vars, Jsonb *json, bool throwErrors,
646645
jspInit(&jsp, path);
647646

648647
if (!JsonbExtractScalar(&json->root, &jbv))
649-
JsonbInitBinary(&jbv, json);
648+
JsonValueInitBinary(&jbv, JsonRoot(json));
650649

651650
if (vars && !JsonContainerIsObject(&vars->root))
652651
{
@@ -2119,7 +2118,7 @@ executeKeyValueMethod(JsonPathExecContext *cxt, JsonPathItem *jsp,
21192118

21202119
jsonb = JsonbValueToJsonb(keyval);
21212120

2122-
JsonbInitBinary(&obj, jsonb);
2121+
JsonValueInitBinary(&obj, JsonRoot(jsonb));
21232122

21242123
baseObject = setBaseObject(cxt, &obj, cxt->lastGeneratedObjectId++);
21252124

@@ -2238,7 +2237,7 @@ getJsonPathVariable(JsonPathExecContext *cxt, JsonPathItem *variable,
22382237
pnstrdup(varName, varNameLength))));
22392238
}
22402239

2241-
JsonbInitBinary(&tmp, vars);
2240+
JsonValueInitBinary(&tmp, JsonRoot(vars));
22422241
setBaseObject(cxt, &tmp, 1);
22432242
}
22442243

@@ -2617,18 +2616,6 @@ JsonValueListNext(const JsonValueList *jvl, JsonValueListIterator *it)
26172616
return result;
26182617
}
26192618

2620-
/*
2621-
* Initialize a binary JsonbValue with the given jsonb container.
2622-
*/
2623-
static JsonbValue *
2624-
JsonbInitBinary(JsonbValue *jbv, Jsonb *jb)
2625-
{
2626-
jbv->type = jbvBinary;
2627-
jbv->val.binary.data = &jb->root;
2628-
2629-
return jbv;
2630-
}
2631-
26322619
/*
26332620
* Returns jbv* type of JsonbValue. Note, it never returns jbvBinary as is.
26342621
*/

src/common/jsonapi.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,13 +1329,10 @@ jsontFillValue(JsonIterator **pit, JsonValue *res, bool skipNested,
13291329
case JSON_TOKEN_OBJECT_START:
13301330
case JSON_TOKEN_ARRAY_START:
13311331
{
1332+
JsonContainerData *cont = JsonContainerAlloc();
13321333
char *token_start = lex->token_start;
13331334
int len;
13341335

1335-
res->type = jbvBinary;
1336-
res->val.binary.data = JsonContainerAlloc();
1337-
res->val.binary.uniquified = false;
1338-
13391336
if (skipNested)
13401337
{
13411338
if (tok == JSON_TOKEN_OBJECT_START)
@@ -1348,12 +1345,14 @@ jsontFillValue(JsonIterator **pit, JsonValue *res, bool skipNested,
13481345
else
13491346
len = lex->input_length - (lex->token_start - lex->input);
13501347

1351-
jsontInitContainer((JsonContainerData *) res->val.binary.data,
1348+
jsontInitContainer(cont,
13521349
token_start, len,
13531350
tok == JSON_TOKEN_OBJECT_START ? jbvObject
13541351
: jbvArray,
13551352
-1);
13561353

1354+
JsonValueInitBinary(res, cont);
1355+
13571356
if (skipNested)
13581357
return false;
13591358

src/include/utils/json_generic.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,16 @@ JsonValueInitArray(JsonValue *val, int nElems, int nElemsAllocated,
369369
return val;
370370
}
371371

372+
static inline JsonValue *
373+
JsonValueInitBinary(JsonValue *val, JsonContainer *cont)
374+
{
375+
val->type = jbvBinary;
376+
val->val.binary.data = cont;
377+
val->val.binary.uniquified = JsonContainerIsUniquified(cont);
378+
379+
return val;
380+
}
381+
372382
extern Json *JsonValueToJson(JsonValue *val);
373383
extern JsonValue *JsonToJsonValue(Json *json, JsonValue *jv);
374384
extern JsonValue *JsonValueUnpackBinary(const JsonValue *jbv);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy