Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4f28ccd

Browse files
author
Nikita Glukhov
committed
Add JsonValueInitBinary()
1 parent 5085a24 commit 4f28ccd

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);
@@ -653,15 +647,14 @@ fillJsonbValue(const JsonbContainer *container, int index,
653647
}
654648
else
655649
{
650+
JsonContainerData *cont = JsonContainerAlloc();
656651
Assert(JBE_ISCONTAINER(entry));
657-
result->type = jbvBinary;
658-
result->val.binary.data = JsonContainerAlloc();
659-
jsonbInitContainer((JsonContainerData *) result->val.binary.data,
652+
jsonbInitContainer(cont,
660653
/* Remove alignment padding from data pointer and length */
661654
(JsonbContainer *)(base_addr + INTALIGN(offset)),
662655
getJsonbLength(container, index) -
663656
(INTALIGN(offset) - offset));
664-
result->val.binary.uniquified = true;
657+
JsonValueInitBinary(result, cont);
665658
}
666659
}
667660

src/backend/utils/adt/jsonfuncs.c

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

36823682
/* fill binary jsonb value pointing to jb */
3683-
jbv.type = jbvBinary;
3684-
jbv.val.binary.data = &jb->root;
3685-
jbv.val.binary.uniquified = true;
3683+
JsonValueInitBinary(&jbv, JsonRoot(jb));
36863684
}
36873685

36883686
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
@@ -1331,13 +1331,10 @@ jsontFillValue(JsonIterator **pit, JsonValue *res, bool skipNested,
13311331
case JSON_TOKEN_OBJECT_START:
13321332
case JSON_TOKEN_ARRAY_START:
13331333
{
1334+
JsonContainerData *cont = JsonContainerAlloc();
13341335
char *token_start = lex->token_start;
13351336
int len;
13361337

1337-
res->type = jbvBinary;
1338-
res->val.binary.data = JsonContainerAlloc();
1339-
res->val.binary.uniquified = false;
1340-
13411338
if (skipNested)
13421339
{
13431340
if (tok == JSON_TOKEN_OBJECT_START)
@@ -1350,12 +1347,14 @@ jsontFillValue(JsonIterator **pit, JsonValue *res, bool skipNested,
13501347
else
13511348
len = lex->input_length - (lex->token_start - lex->input);
13521349

1353-
jsontInitContainer((JsonContainerData *) res->val.binary.data,
1350+
jsontInitContainer(cont,
13541351
token_start, len,
13551352
tok == JSON_TOKEN_OBJECT_START ? jbvObject
13561353
: jbvArray,
13571354
-1);
13581355

1356+
JsonValueInitBinary(res, cont);
1357+
13591358
if (skipNested)
13601359
return false;
13611360

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)