Skip to content

Commit 118dd93

Browse files
author
Nikita Glukhov
committed
Add JsonbRoot(), JsonbGetSize() macros
1 parent 7f35dab commit 118dd93

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

src/backend/utils/adt/jsonb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ jsonb_out(PG_FUNCTION_ARGS)
140140
Jsonb *jb = PG_GETARG_JSONB_P(0);
141141
char *out;
142142

143-
out = JsonbToCString(NULL, &jb->root, VARSIZE(jb));
143+
out = JsonbToCString(NULL, JsonbRoot(jb), JsonbGetSize(jb));
144144

145145
PG_RETURN_CSTRING(out);
146146
}
@@ -158,7 +158,7 @@ jsonb_send(PG_FUNCTION_ARGS)
158158
StringInfo jtext = makeStringInfo();
159159
int version = 1;
160160

161-
(void) JsonbToCString(jtext, &jb->root, VARSIZE(jb));
161+
(void) JsonbToCString(jtext, JsonbRoot(jb), JsonbGetSize(jb));
162162

163163
pq_begintypsend(&buf);
164164
pq_sendint8(&buf, version);

src/backend/utils/adt/jsonfuncs.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ jsonb_object_keys(PG_FUNCTION_ARGS)
565565
state->sent_count = 0;
566566
state->result = palloc(state->result_size * sizeof(char *));
567567

568-
it = JsonbIteratorInit(&jb->root);
568+
it = JsonbIteratorInit(JsonbRoot(jb));
569569

570570
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
571571
{
@@ -830,7 +830,7 @@ jsonb_object_field(PG_FUNCTION_ARGS)
830830
if (!JB_ROOT_IS_OBJECT(jb))
831831
PG_RETURN_NULL();
832832

833-
v = getKeyJsonValueFromContainer(&jb->root,
833+
v = getKeyJsonValueFromContainer(JsonbRoot(jb),
834834
VARDATA_ANY(key),
835835
VARSIZE_ANY_EXHDR(key),
836836
&vbuf);
@@ -868,7 +868,7 @@ jsonb_object_field_text(PG_FUNCTION_ARGS)
868868
if (!JB_ROOT_IS_OBJECT(jb))
869869
PG_RETURN_NULL();
870870

871-
v = getKeyJsonValueFromContainer(&jb->root,
871+
v = getKeyJsonValueFromContainer(JsonbRoot(jb),
872872
VARDATA_ANY(key),
873873
VARSIZE_ANY_EXHDR(key),
874874
&vbuf);
@@ -915,7 +915,7 @@ jsonb_array_element(PG_FUNCTION_ARGS)
915915
element += nelements;
916916
}
917917

918-
v = getIthJsonbValueFromContainer(&jb->root, element);
918+
v = getIthJsonbValueFromContainer(JsonbRoot(jb), element);
919919
if (v != NULL)
920920
PG_RETURN_JSONB_P(JsonbValueToJsonb(v));
921921

@@ -958,7 +958,7 @@ jsonb_array_element_text(PG_FUNCTION_ARGS)
958958
element += nelements;
959959
}
960960

961-
v = getIthJsonbValueFromContainer(&jb->root, element);
961+
v = getIthJsonbValueFromContainer(JsonbRoot(jb), element);
962962

963963
if (v != NULL && v->type != jbvNull)
964964
PG_RETURN_TEXT_P(JsonbValueAsText(v));
@@ -1468,7 +1468,7 @@ get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text)
14681468
&pathtext, &pathnulls, &npath);
14691469

14701470
/* Identify whether we have object, array, or scalar at top-level */
1471-
container = &jb->root;
1471+
container = JsonbRoot(jb);
14721472

14731473
if (JB_ROOT_IS_OBJECT(jb))
14741474
have_object = true;
@@ -1496,7 +1496,7 @@ get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text)
14961496
{
14971497
PG_RETURN_TEXT_P(cstring_to_text(JsonbToCString(NULL,
14981498
container,
1499-
VARSIZE(jb))));
1499+
JsonbGetSize(jb))));
15001500
}
15011501
else
15021502
{
@@ -1813,7 +1813,7 @@ each_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
18131813
"jsonb_each temporary cxt",
18141814
ALLOCSET_DEFAULT_SIZES);
18151815

1816-
it = JsonbIteratorInit(&jb->root);
1816+
it = JsonbIteratorInit(JsonbRoot(jb));
18171817

18181818
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
18191819
{
@@ -2110,7 +2110,7 @@ elements_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname,
21102110
"jsonb_array_elements temporary cxt",
21112111
ALLOCSET_DEFAULT_SIZES);
21122112

2113-
it = JsonbIteratorInit(&jb->root);
2113+
it = JsonbIteratorInit(JsonbRoot(jb));
21142114

21152115
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
21162116
{
@@ -2899,7 +2899,7 @@ populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv)
28992899
*/
29002900
Jsonb *jsonb = JsonbValueToJsonb(jbv);
29012901

2902-
str = JsonbToCString(NULL, &jsonb->root, VARSIZE(jsonb));
2902+
str = JsonbToCString(NULL, JSonbRoot(jsonb), JsonbGetSize(jsonb));
29032903
}
29042904
else if (jbv->type == jbvString) /* quotes are stripped */
29052905
str = pnstrdup(jbv->val.string.val, jbv->val.string.len);
@@ -3774,7 +3774,7 @@ populate_recordset_worker(FunctionCallInfo fcinfo, const char *funcname,
37743774
errmsg("cannot call %s on a non-array",
37753775
funcname)));
37763776

3777-
it = JsonbIteratorInit(&jb->root);
3777+
it = JsonbIteratorInit(JsonbRoot(jb));
37783778

37793779
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
37803780
{
@@ -4113,7 +4113,7 @@ jsonb_strip_nulls(PG_FUNCTION_ARGS)
41134113
if (JB_ROOT_IS_SCALAR(jb))
41144114
PG_RETURN_JSONB_P(jb);
41154115

4116-
it = JsonbIteratorInit(&jb->root);
4116+
it = JsonbIteratorInit(JsonbRoot(jb));
41174117

41184118
while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
41194119
{
@@ -4167,7 +4167,7 @@ addJsonbToParseState(JsonbParseState **jbps, Jsonb *jb)
41674167
JsonbValue v;
41684168
JsonbIteratorToken type;
41694169

4170-
it = JsonbIteratorInit(&jb->root);
4170+
it = JsonbIteratorInit(JsonbRoot(jb));
41714171

41724172
Assert(*jbps);
41734173

@@ -4203,7 +4203,7 @@ jsonb_pretty(PG_FUNCTION_ARGS)
42034203
Jsonb *jb = PG_GETARG_JSONB_P(0);
42044204
StringInfo str = makeStringInfo();
42054205

4206-
JsonbToCStringIndent(str, &jb->root, VARSIZE(jb));
4206+
JsonbToCStringIndent(str, JsonbRoot(jb), JsonbGetSize(jb));
42074207

42084208
PG_RETURN_TEXT_P(cstring_to_text_with_len(str->data, str->len));
42094209
}
@@ -4214,7 +4214,7 @@ jsonb_canonical(PG_FUNCTION_ARGS)
42144214
Jsonb *jb = PG_GETARG_JSONB_P(0);
42154215
StringInfo str = makeStringInfo();
42164216

4217-
JsonbToCStringCanonical(str, &jb->root, VARSIZE(jb));
4217+
JsonbToCStringCanonical(str, JsonbRoot(jb), JsonbGetSize(jb));
42184218

42194219
PG_RETURN_TEXT_P(cstring_to_text_with_len(str->data, str->len));
42204220
}
@@ -4248,8 +4248,8 @@ jsonb_concat(PG_FUNCTION_ARGS)
42484248
PG_RETURN_JSONB_P(jb1);
42494249
}
42504250

4251-
it1 = JsonbIteratorInit(&jb1->root);
4252-
it2 = JsonbIteratorInit(&jb2->root);
4251+
it1 = JsonbIteratorInit(JsonbRoot(jb1));
4252+
it2 = JsonbIteratorInit(JsonbRoot(jb2));
42534253

42544254
res = IteratorConcat(&it1, &it2, &state);
42554255

@@ -4287,7 +4287,7 @@ jsonb_delete(PG_FUNCTION_ARGS)
42874287
if (JB_ROOT_COUNT(in) == 0)
42884288
PG_RETURN_JSONB_P(in);
42894289

4290-
it = JsonbIteratorInit(&in->root);
4290+
it = JsonbIteratorInit(JsonbRoot(in));
42914291

42924292
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
42934293
{
@@ -4352,7 +4352,7 @@ jsonb_delete_array(PG_FUNCTION_ARGS)
43524352
if (keys_len == 0)
43534353
PG_RETURN_JSONB_P(in);
43544354

4355-
it = JsonbIteratorInit(&in->root);
4355+
it = JsonbIteratorInit(JsonbRoot(in));
43564356

43574357
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
43584358
{
@@ -4431,7 +4431,7 @@ jsonb_delete_idx(PG_FUNCTION_ARGS)
44314431
if (JB_ROOT_COUNT(in) == 0)
44324432
PG_RETURN_JSONB_P(in);
44334433

4434-
it = JsonbIteratorInit(&in->root);
4434+
it = JsonbIteratorInit(JsonbRoot(in));
44354435

44364436
r = JsonbIteratorNext(&it, &v, false);
44374437
Assert(r == WJB_BEGIN_ARRAY);
@@ -4502,7 +4502,7 @@ jsonb_set(PG_FUNCTION_ARGS)
45024502
if (path_len == 0)
45034503
PG_RETURN_JSONB_P(in);
45044504

4505-
it = JsonbIteratorInit(&in->root);
4505+
it = JsonbIteratorInit(JsonbRoot(in));
45064506

45074507
res = setPath(&it, path_elems, path_nulls, path_len, &st,
45084508
0, newval, create ? JB_PATH_CREATE : JB_PATH_REPLACE);
@@ -4614,7 +4614,7 @@ jsonb_delete_path(PG_FUNCTION_ARGS)
46144614
if (path_len == 0)
46154615
PG_RETURN_JSONB_P(in);
46164616

4617-
it = JsonbIteratorInit(&in->root);
4617+
it = JsonbIteratorInit(JsonbRoot(in));
46184618

46194619
res = setPath(&it, path_elems, path_nulls, path_len, &st,
46204620
0, NULL, JB_PATH_DELETE);
@@ -4657,7 +4657,7 @@ jsonb_insert(PG_FUNCTION_ARGS)
46574657
if (path_len == 0)
46584658
PG_RETURN_JSONB_P(in);
46594659

4660-
it = JsonbIteratorInit(&in->root);
4660+
it = JsonbIteratorInit(JsonbRoot(in));
46614661

46624662
res = setPath(&it, path_elems, path_nulls, path_len, &st, 0, newval,
46634663
after ? JB_PATH_INSERT_AFTER : JB_PATH_INSERT_BEFORE);
@@ -5033,7 +5033,7 @@ parse_jsonb_index_flags(Jsonb *jb)
50335033
JsonbIteratorToken type;
50345034
uint32 flags = 0;
50355035

5036-
it = JsonbIteratorInit(&jb->root);
5036+
it = JsonbIteratorInit(JsonbRoot(jb));
50375037

50385038
type = JsonbIteratorNext(&it, &v, false);
50395039

@@ -5101,7 +5101,7 @@ iterate_jsonb_values(Jsonb *jb, uint32 flags, void *state,
51015101
JsonbValue v;
51025102
JsonbIteratorToken type;
51035103

5104-
it = JsonbIteratorInit(&jb->root);
5104+
it = JsonbIteratorInit(JsonbRoot(jb));
51055105

51065106
/*
51075107
* Just recursively iterating over jsonb and call callback on all
@@ -5241,7 +5241,7 @@ transform_jsonb_string_values(Jsonb *jsonb, void *action_state,
52415241
JsonbParseState *st = NULL;
52425242
text *out;
52435243

5244-
it = JsonbIteratorInit(&jsonb->root);
5244+
it = JsonbIteratorInit(JsonbRoot(jsonb));
52455245

52465246
while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
52475247
{

src/include/utils/jsonb.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ typedef enum
7575
#define PG_GETARG_JSONB_P_COPY(x) DatumGetJsonbPCopy(PG_GETARG_DATUM(x))
7676
#define PG_RETURN_JSONB_P(x) PG_RETURN_DATUM(JsonbPGetDatum(x))
7777

78+
#define JsonbRoot(jsonb) (&(jsonb)->root)
79+
#define JsonbGetSize(jsonb) VARSIZE(jsonb)
80+
7881
typedef struct JsonbPair JsonbPair;
7982
typedef struct JsonbValue JsonbValue;
8083

0 commit comments

Comments
 (0)