Skip to content

Commit 1d85bfb

Browse files
author
Nikita Glukhov
committed
Add JsonbRoot(), JsonbGetSize() macros
1 parent 25aae19 commit 1d85bfb

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
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: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ jsonb_object_keys(PG_FUNCTION_ARGS)
570570
state->sent_count = 0;
571571
state->result = palloc(state->result_size * sizeof(char *));
572572

573-
it = JsonbIteratorInit(&jb->root);
573+
it = JsonbIteratorInit(JsonbRoot(jb));
574574

575575
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
576576
{
@@ -824,7 +824,7 @@ jsonb_object_field(PG_FUNCTION_ARGS)
824824
if (!JB_ROOT_IS_OBJECT(jb))
825825
PG_RETURN_NULL();
826826

827-
v = getKeyJsonValueFromContainer(&jb->root,
827+
v = getKeyJsonValueFromContainer(JsonbRoot(jb),
828828
VARDATA_ANY(key),
829829
VARSIZE_ANY_EXHDR(key),
830830
&vbuf);
@@ -862,7 +862,7 @@ jsonb_object_field_text(PG_FUNCTION_ARGS)
862862
if (!JB_ROOT_IS_OBJECT(jb))
863863
PG_RETURN_NULL();
864864

865-
v = getKeyJsonValueFromContainer(&jb->root,
865+
v = getKeyJsonValueFromContainer(JsonbRoot(jb),
866866
VARDATA_ANY(key),
867867
VARSIZE_ANY_EXHDR(key),
868868
&vbuf);
@@ -909,7 +909,7 @@ jsonb_array_element(PG_FUNCTION_ARGS)
909909
element += nelements;
910910
}
911911

912-
v = getIthJsonbValueFromContainer(&jb->root, element);
912+
v = getIthJsonbValueFromContainer(JsonbRoot(jb), element);
913913
if (v != NULL)
914914
PG_RETURN_JSONB_P(JsonbValueToJsonb(v));
915915

@@ -952,7 +952,7 @@ jsonb_array_element_text(PG_FUNCTION_ARGS)
952952
element += nelements;
953953
}
954954

955-
v = getIthJsonbValueFromContainer(&jb->root, element);
955+
v = getIthJsonbValueFromContainer(JsonbRoot(jb), element);
956956

957957
if (v != NULL && v->type != jbvNull)
958958
PG_RETURN_TEXT_P(JsonbValueAsText(v));
@@ -1468,7 +1468,7 @@ get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text)
14681468
Datum
14691469
jsonb_get_element(Jsonb *jb, Datum *path, int npath, bool *isnull, bool as_text)
14701470
{
1471-
JsonbContainer *container = &jb->root;
1471+
JsonbContainer *container = JsonbRoot(jb);
14721472
JsonbValue *jbvp = NULL;
14731473
int i;
14741474
bool have_object = false,
@@ -1503,7 +1503,7 @@ jsonb_get_element(Jsonb *jb, Datum *path, int npath, bool *isnull, bool as_text)
15031503
{
15041504
return PointerGetDatum(cstring_to_text(JsonbToCString(NULL,
15051505
container,
1506-
VARSIZE(jb))));
1506+
JsonbGetSize(jb))));
15071507
}
15081508
else
15091509
{
@@ -1623,7 +1623,7 @@ jsonb_set_element(Jsonb *jb, Datum *path, int path_len,
16231623
if (newval->type == jbvArray && newval->val.array.rawScalar)
16241624
*newval = newval->val.array.elems[0];
16251625

1626-
it = JsonbIteratorInit(&jb->root);
1626+
it = JsonbIteratorInit(JsonbRoot(jb));
16271627

16281628
res = setPath(&it, path, path_nulls, path_len, &state, 0, newval,
16291629
JB_PATH_CREATE | JB_PATH_FILL_GAPS |
@@ -1955,7 +1955,7 @@ each_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
19551955
"jsonb_each temporary cxt",
19561956
ALLOCSET_DEFAULT_SIZES);
19571957

1958-
it = JsonbIteratorInit(&jb->root);
1958+
it = JsonbIteratorInit(JsonbRoot(jb));
19591959

19601960
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
19611961
{
@@ -2252,7 +2252,7 @@ elements_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname,
22522252
"jsonb_array_elements temporary cxt",
22532253
ALLOCSET_DEFAULT_SIZES);
22542254

2255-
it = JsonbIteratorInit(&jb->root);
2255+
it = JsonbIteratorInit(JsonbRoot(jb));
22562256

22572257
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
22582258
{
@@ -3041,7 +3041,7 @@ populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv)
30413041
*/
30423042
Jsonb *jsonb = JsonbValueToJsonb(jbv);
30433043

3044-
str = JsonbToCString(NULL, &jsonb->root, VARSIZE(jsonb));
3044+
str = JsonbToCString(NULL, JsonbRoot(jsonb), JsonbGetSize(jsonb));
30453045
}
30463046
else if (jbv->type == jbvString) /* quotes are stripped */
30473047
str = pnstrdup(jbv->val.string.val, jbv->val.string.len);
@@ -3555,7 +3555,7 @@ populate_record_worker(FunctionCallInfo fcinfo, const char *funcname,
35553555

35563556
/* fill binary jsonb value pointing to jb */
35573557
jbv.type = jbvBinary;
3558-
jbv.val.binary.data = &jb->root;
3558+
jbv.val.binary.data = JsonbRoot(jb);
35593559
jbv.val.binary.len = VARSIZE(jb) - VARHDRSZ;
35603560
}
35613561

@@ -3915,7 +3915,7 @@ populate_recordset_worker(FunctionCallInfo fcinfo, const char *funcname,
39153915
errmsg("cannot call %s on a non-array",
39163916
funcname)));
39173917

3918-
it = JsonbIteratorInit(&jb->root);
3918+
it = JsonbIteratorInit(JsonbRoot(jb));
39193919

39203920
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
39213921
{
@@ -4253,7 +4253,7 @@ jsonb_strip_nulls(PG_FUNCTION_ARGS)
42534253
if (JB_ROOT_IS_SCALAR(jb))
42544254
PG_RETURN_JSONB_P(jb);
42554255

4256-
it = JsonbIteratorInit(&jb->root);
4256+
it = JsonbIteratorInit(JsonbRoot(jb));
42574257

42584258
while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
42594259
{
@@ -4302,7 +4302,7 @@ jsonb_pretty(PG_FUNCTION_ARGS)
43024302
Jsonb *jb = PG_GETARG_JSONB_P(0);
43034303
StringInfo str = makeStringInfo();
43044304

4305-
JsonbToCStringIndent(str, &jb->root, VARSIZE(jb));
4305+
JsonbToCStringIndent(str, JsonbRoot(jb), JsonbGetSize(jb));
43064306

43074307
PG_RETURN_TEXT_P(cstring_to_text_with_len(str->data, str->len));
43084308
}
@@ -4313,7 +4313,7 @@ jsonb_canonical(PG_FUNCTION_ARGS)
43134313
Jsonb *jb = PG_GETARG_JSONB_P(0);
43144314
StringInfo str = makeStringInfo();
43154315

4316-
JsonbToCStringCanonical(str, &jb->root, VARSIZE(jb));
4316+
JsonbToCStringCanonical(str, JsonbRoot(jb), JsonbGetSize(jb));
43174317

43184318
PG_RETURN_TEXT_P(cstring_to_text_with_len(str->data, str->len));
43194319
}
@@ -4347,8 +4347,8 @@ jsonb_concat(PG_FUNCTION_ARGS)
43474347
PG_RETURN_JSONB_P(jb1);
43484348
}
43494349

4350-
it1 = JsonbIteratorInit(&jb1->root);
4351-
it2 = JsonbIteratorInit(&jb2->root);
4350+
it1 = JsonbIteratorInit(JsonbRoot(jb1));
4351+
it2 = JsonbIteratorInit(JsonbRoot(jb2));
43524352

43534353
res = IteratorConcat(&it1, &it2, &state);
43544354

@@ -4386,7 +4386,7 @@ jsonb_delete(PG_FUNCTION_ARGS)
43864386
if (JB_ROOT_COUNT(in) == 0)
43874387
PG_RETURN_JSONB_P(in);
43884388

4389-
it = JsonbIteratorInit(&in->root);
4389+
it = JsonbIteratorInit(JsonbRoot(in));
43904390

43914391
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
43924392
{
@@ -4451,7 +4451,7 @@ jsonb_delete_array(PG_FUNCTION_ARGS)
44514451
if (keys_len == 0)
44524452
PG_RETURN_JSONB_P(in);
44534453

4454-
it = JsonbIteratorInit(&in->root);
4454+
it = JsonbIteratorInit(JsonbRoot(in));
44554455

44564456
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
44574457
{
@@ -4530,7 +4530,7 @@ jsonb_delete_idx(PG_FUNCTION_ARGS)
45304530
if (JB_ROOT_COUNT(in) == 0)
45314531
PG_RETURN_JSONB_P(in);
45324532

4533-
it = JsonbIteratorInit(&in->root);
4533+
it = JsonbIteratorInit(JsonbRoot(in));
45344534

45354535
r = JsonbIteratorNext(&it, &v, false);
45364536
Assert(r == WJB_BEGIN_ARRAY);
@@ -4604,7 +4604,7 @@ jsonb_set(PG_FUNCTION_ARGS)
46044604
if (path_len == 0)
46054605
PG_RETURN_JSONB_P(in);
46064606

4607-
it = JsonbIteratorInit(&in->root);
4607+
it = JsonbIteratorInit(JsonbRoot(in));
46084608

46094609
res = setPath(&it, path_elems, path_nulls, path_len, &st,
46104610
0, &newval, create ? JB_PATH_CREATE : JB_PATH_REPLACE);
@@ -4716,7 +4716,7 @@ jsonb_delete_path(PG_FUNCTION_ARGS)
47164716
if (path_len == 0)
47174717
PG_RETURN_JSONB_P(in);
47184718

4719-
it = JsonbIteratorInit(&in->root);
4719+
it = JsonbIteratorInit(JsonbRoot(in));
47204720

47214721
res = setPath(&it, path_elems, path_nulls, path_len, &st,
47224722
0, NULL, JB_PATH_DELETE);
@@ -4762,7 +4762,7 @@ jsonb_insert(PG_FUNCTION_ARGS)
47624762
if (path_len == 0)
47634763
PG_RETURN_JSONB_P(in);
47644764

4765-
it = JsonbIteratorInit(&in->root);
4765+
it = JsonbIteratorInit(JsonbRoot(in));
47664766

47674767
res = setPath(&it, path_elems, path_nulls, path_len, &st, 0, &newval,
47684768
after ? JB_PATH_INSERT_AFTER : JB_PATH_INSERT_BEFORE);
@@ -5249,7 +5249,7 @@ parse_jsonb_index_flags(Jsonb *jb)
52495249
JsonbIteratorToken type;
52505250
uint32 flags = 0;
52515251

5252-
it = JsonbIteratorInit(&jb->root);
5252+
it = JsonbIteratorInit(JsonbRoot(jb));
52535253

52545254
type = JsonbIteratorNext(&it, &v, false);
52555255

@@ -5317,7 +5317,7 @@ iterate_jsonb_values(Jsonb *jb, uint32 flags, void *state,
53175317
JsonbValue v;
53185318
JsonbIteratorToken type;
53195319

5320-
it = JsonbIteratorInit(&jb->root);
5320+
it = JsonbIteratorInit(JsonbRoot(jb));
53215321

53225322
/*
53235323
* Just recursively iterating over jsonb and call callback on all
@@ -5457,7 +5457,7 @@ transform_jsonb_string_values(Jsonb *jsonb, void *action_state,
54575457
JsonbParseState *st = NULL;
54585458
text *out;
54595459

5460-
it = JsonbIteratorInit(&jsonb->root);
5460+
it = JsonbIteratorInit(JsonbRoot(jsonb));
54615461

54625462
while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
54635463
{

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)