Skip to content

Commit e324ad7

Browse files
author
Nikita Glukhov
committed
Add JsonbRoot(), JsonbGetSize() macros
1 parent 3fd88de commit e324ad7

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
@@ -111,7 +111,7 @@ jsonb_out(PG_FUNCTION_ARGS)
111111
Jsonb *jb = PG_GETARG_JSONB_P(0);
112112
char *out;
113113

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

116116
PG_RETURN_CSTRING(out);
117117
}
@@ -129,7 +129,7 @@ jsonb_send(PG_FUNCTION_ARGS)
129129
StringInfo jtext = makeStringInfo();
130130
int version = 1;
131131

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

134134
pq_begintypsend(&buf);
135135
pq_sendint8(&buf, version);

src/backend/utils/adt/jsonfuncs.c

Lines changed: 26 additions & 26 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
{
@@ -826,7 +826,7 @@ jsonb_object_field(PG_FUNCTION_ARGS)
826826
if (!JB_ROOT_IS_OBJECT(jb))
827827
PG_RETURN_NULL();
828828

829-
v = getKeyJsonValueFromContainer(&jb->root,
829+
v = getKeyJsonValueFromContainer(JsonbRoot(jb),
830830
VARDATA_ANY(key),
831831
VARSIZE_ANY_EXHDR(key),
832832
&vbuf);
@@ -864,7 +864,7 @@ jsonb_object_field_text(PG_FUNCTION_ARGS)
864864
if (!JB_ROOT_IS_OBJECT(jb))
865865
PG_RETURN_NULL();
866866

867-
v = getKeyJsonValueFromContainer(&jb->root,
867+
v = getKeyJsonValueFromContainer(JsonbRoot(jb),
868868
VARDATA_ANY(key),
869869
VARSIZE_ANY_EXHDR(key),
870870
&vbuf);
@@ -911,7 +911,7 @@ jsonb_array_element(PG_FUNCTION_ARGS)
911911
element += nelements;
912912
}
913913

914-
v = getIthJsonbValueFromContainer(&jb->root, element);
914+
v = getIthJsonbValueFromContainer(JsonbRoot(jb), element);
915915
if (v != NULL)
916916
PG_RETURN_JSONB_P(JsonbValueToJsonb(v));
917917

@@ -954,7 +954,7 @@ jsonb_array_element_text(PG_FUNCTION_ARGS)
954954
element += nelements;
955955
}
956956

957-
v = getIthJsonbValueFromContainer(&jb->root, element);
957+
v = getIthJsonbValueFromContainer(JsonbRoot(jb), element);
958958

959959
if (v != NULL && v->type != jbvNull)
960960
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 |
@@ -1927,7 +1927,7 @@ each_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname, bool as_text)
19271927
"jsonb_each temporary cxt",
19281928
ALLOCSET_DEFAULT_SIZES);
19291929

1930-
it = JsonbIteratorInit(&jb->root);
1930+
it = JsonbIteratorInit(JsonbRoot(jb));
19311931

19321932
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
19331933
{
@@ -2171,7 +2171,7 @@ elements_worker_jsonb(FunctionCallInfo fcinfo, const char *funcname,
21712171
"jsonb_array_elements temporary cxt",
21722172
ALLOCSET_DEFAULT_SIZES);
21732173

2174-
it = JsonbIteratorInit(&jb->root);
2174+
it = JsonbIteratorInit(JsonbRoot(jb));
21752175

21762176
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
21772177
{
@@ -2928,7 +2928,7 @@ populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv)
29282928
*/
29292929
Jsonb *jsonb = JsonbValueToJsonb(jbv);
29302930

2931-
str = JsonbToCString(NULL, &jsonb->root, VARSIZE(jsonb));
2931+
str = JsonbToCString(NULL, JsonbRoot(jsonb), JsonbGetSize(jsonb));
29322932
}
29332933
else if (jbv->type == jbvString) /* quotes are stripped */
29342934
str = pnstrdup(jbv->val.string.val, jbv->val.string.len);
@@ -3487,7 +3487,7 @@ populate_record_worker(FunctionCallInfo fcinfo, const char *funcname,
34873487

34883488
/* fill binary jsonb value pointing to jb */
34893489
jbv.type = jbvBinary;
3490-
jbv.val.binary.data = &jb->root;
3490+
jbv.val.binary.data = JsonbRoot(jb);
34913491
jbv.val.binary.len = VARSIZE(jb) - VARHDRSZ;
34923492
}
34933493

@@ -3850,7 +3850,7 @@ populate_recordset_worker(FunctionCallInfo fcinfo, const char *funcname,
38503850
errmsg("cannot call %s on a non-array",
38513851
funcname)));
38523852

3853-
it = JsonbIteratorInit(&jb->root);
3853+
it = JsonbIteratorInit(JsonbRoot(jb));
38543854

38553855
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
38563856
{
@@ -4187,7 +4187,7 @@ jsonb_strip_nulls(PG_FUNCTION_ARGS)
41874187
if (JB_ROOT_IS_SCALAR(jb))
41884188
PG_RETURN_JSONB_P(jb);
41894189

4190-
it = JsonbIteratorInit(&jb->root);
4190+
it = JsonbIteratorInit(JsonbRoot(jb));
41914191

41924192
while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
41934193
{
@@ -4236,7 +4236,7 @@ jsonb_pretty(PG_FUNCTION_ARGS)
42364236
Jsonb *jb = PG_GETARG_JSONB_P(0);
42374237
StringInfo str = makeStringInfo();
42384238

4239-
JsonbToCStringIndent(str, &jb->root, VARSIZE(jb));
4239+
JsonbToCStringIndent(str, JsonbRoot(jb), JsonbGetSize(jb));
42404240

42414241
PG_RETURN_TEXT_P(cstring_to_text_with_len(str->data, str->len));
42424242
}
@@ -4270,8 +4270,8 @@ jsonb_concat(PG_FUNCTION_ARGS)
42704270
PG_RETURN_JSONB_P(jb1);
42714271
}
42724272

4273-
it1 = JsonbIteratorInit(&jb1->root);
4274-
it2 = JsonbIteratorInit(&jb2->root);
4273+
it1 = JsonbIteratorInit(JsonbRoot(jb1));
4274+
it2 = JsonbIteratorInit(JsonbRoot(jb2));
42754275

42764276
res = IteratorConcat(&it1, &it2, &state);
42774277

@@ -4309,7 +4309,7 @@ jsonb_delete(PG_FUNCTION_ARGS)
43094309
if (JB_ROOT_COUNT(in) == 0)
43104310
PG_RETURN_JSONB_P(in);
43114311

4312-
it = JsonbIteratorInit(&in->root);
4312+
it = JsonbIteratorInit(JsonbRoot(in));
43134313

43144314
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
43154315
{
@@ -4373,7 +4373,7 @@ jsonb_delete_array(PG_FUNCTION_ARGS)
43734373
if (keys_len == 0)
43744374
PG_RETURN_JSONB_P(in);
43754375

4376-
it = JsonbIteratorInit(&in->root);
4376+
it = JsonbIteratorInit(JsonbRoot(in));
43774377

43784378
while ((r = JsonbIteratorNext(&it, &v, skipNested)) != WJB_DONE)
43794379
{
@@ -4452,7 +4452,7 @@ jsonb_delete_idx(PG_FUNCTION_ARGS)
44524452
if (JB_ROOT_COUNT(in) == 0)
44534453
PG_RETURN_JSONB_P(in);
44544454

4455-
it = JsonbIteratorInit(&in->root);
4455+
it = JsonbIteratorInit(JsonbRoot(in));
44564456

44574457
r = JsonbIteratorNext(&it, &v, false);
44584458
Assert(r == WJB_BEGIN_ARRAY);
@@ -4525,7 +4525,7 @@ jsonb_set(PG_FUNCTION_ARGS)
45254525
if (path_len == 0)
45264526
PG_RETURN_JSONB_P(in);
45274527

4528-
it = JsonbIteratorInit(&in->root);
4528+
it = JsonbIteratorInit(JsonbRoot(in));
45294529

45304530
res = setPath(&it, path_elems, path_nulls, path_len, &st,
45314531
0, &newval, create ? JB_PATH_CREATE : JB_PATH_REPLACE);
@@ -4636,7 +4636,7 @@ jsonb_delete_path(PG_FUNCTION_ARGS)
46364636
if (path_len == 0)
46374637
PG_RETURN_JSONB_P(in);
46384638

4639-
it = JsonbIteratorInit(&in->root);
4639+
it = JsonbIteratorInit(JsonbRoot(in));
46404640

46414641
res = setPath(&it, path_elems, path_nulls, path_len, &st,
46424642
0, NULL, JB_PATH_DELETE);
@@ -4681,7 +4681,7 @@ jsonb_insert(PG_FUNCTION_ARGS)
46814681
if (path_len == 0)
46824682
PG_RETURN_JSONB_P(in);
46834683

4684-
it = JsonbIteratorInit(&in->root);
4684+
it = JsonbIteratorInit(JsonbRoot(in));
46854685

46864686
res = setPath(&it, path_elems, path_nulls, path_len, &st, 0, &newval,
46874687
after ? JB_PATH_INSERT_AFTER : JB_PATH_INSERT_BEFORE);
@@ -5168,7 +5168,7 @@ parse_jsonb_index_flags(Jsonb *jb)
51685168
JsonbIteratorToken type;
51695169
uint32 flags = 0;
51705170

5171-
it = JsonbIteratorInit(&jb->root);
5171+
it = JsonbIteratorInit(JsonbRoot(jb));
51725172

51735173
type = JsonbIteratorNext(&it, &v, false);
51745174

@@ -5236,7 +5236,7 @@ iterate_jsonb_values(Jsonb *jb, uint32 flags, void *state,
52365236
JsonbValue v;
52375237
JsonbIteratorToken type;
52385238

5239-
it = JsonbIteratorInit(&jb->root);
5239+
it = JsonbIteratorInit(JsonbRoot(jb));
52405240

52415241
/*
52425242
* Just recursively iterating over jsonb and call callback on all
@@ -5376,7 +5376,7 @@ transform_jsonb_string_values(Jsonb *jsonb, void *action_state,
53765376
JsonbParseState *st = NULL;
53775377
text *out;
53785378

5379-
it = JsonbIteratorInit(&jsonb->root);
5379+
it = JsonbIteratorInit(JsonbRoot(jsonb));
53805380

53815381
while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
53825382
{

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)
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