Skip to content

Commit fda6461

Browse files
author
Nikita Glukhov
committed
Refactor jsonb.h, remove redefines
1 parent e3fa4c0 commit fda6461

File tree

12 files changed

+196
-187
lines changed

12 files changed

+196
-187
lines changed

src/backend/utils/adt/json_generic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include "miscadmin.h"
1414
#include "utils/builtins.h"
15-
#include "utils/json_generic.h"
15+
#include "utils/jsonb.h"
1616
#include "utils/memutils.h"
1717

1818
static Json *JsonExpand(Json *tmp, Datum value, bool freeValue,

src/backend/utils/adt/jsonb.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "utils/datetime.h"
2626
#include "utils/json.h"
2727
#include "utils/jsonb.h"
28-
#include "utils/json_generic.h"
2928
#include "utils/jsonfuncs.h"
3029
#include "utils/lsyscache.h"
3130
#include "utils/syscache.h"

src/backend/utils/adt/jsonb_gin.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
#include "utils/builtins.h"
6969
#include "utils/jsonb.h"
7070
#include "utils/jsonpath.h"
71-
#include "utils/json_generic.h"
7271
#include "utils/varlena.h"
7372

7473
typedef struct PathHashStack

src/backend/utils/adt/jsonb_op.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "miscadmin.h"
1818
#include "utils/builtins.h"
1919
#include "utils/jsonb.h"
20-
#include "utils/json_generic.h"
2120

2221
Datum
2322
jsonb_exists(PG_FUNCTION_ARGS)

src/backend/utils/adt/jsonb_util.c

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
*/
1414
#include "postgres.h"
1515

16-
#define JSONB_UTIL_C
17-
1816
#include "catalog/pg_collation.h"
1917
#include "catalog/pg_type.h"
2018
#include "common/hashfn.h"
@@ -25,21 +23,21 @@
2523
#include "utils/datetime.h"
2624
#include "utils/json.h"
2725
#include "utils/jsonb.h"
28-
#include "utils/json_generic.h"
26+
#include "utils/jsonb_internals.h"
2927
#include "utils/memutils.h"
3028
#include "utils/varlena.h"
3129

3230
/*
3331
* Maximum number of elements in an array (or key/value pairs in an object).
3432
* This is limited by two things: the size of the JEntry array must fit
3533
* in MaxAllocSize, and the number of elements (or pairs) must fit in the bits
36-
* reserved for that in the JsonbContainer.header field.
34+
* reserved for that in the JsonbContainerHeader.header field.
3735
*
3836
* (The total size of an array's or object's elements is also limited by
3937
* JENTRY_OFFLENMASK, but we're not concerned about that here.)
4038
*/
41-
#define JSONB_MAX_ELEMS (Min(MaxAllocSize / sizeof(JsonbValue), JB_CMASK))
42-
#define JSONB_MAX_PAIRS (Min(MaxAllocSize / sizeof(JsonbPair), JB_CMASK))
39+
#define JSONB_MAX_ELEMS (Min(MaxAllocSize / sizeof(JsonbValue), JBC_CMASK))
40+
#define JSONB_MAX_PAIRS (Min(MaxAllocSize / sizeof(JsonbPair), JBC_CMASK))
4341

4442
/* Conversion state used when parsing Jsonb from text, or for type coercion */
4543
struct JsonbParseState
@@ -56,7 +54,7 @@ typedef struct jsonbIterator
5654
JsonIterator ji;
5755

5856
/* Container being iterated */
59-
const JsonbContainer *container;
57+
const JsonbContainerHeader *container;
6058

6159
uint32 nElems; /* Number of elements in children array (will
6260
* be nPairs for objects) */
@@ -82,7 +80,7 @@ typedef struct jsonbIterator
8280
JsonbIterState state;
8381
} jsonbIterator;
8482

85-
static void fillJsonbValue(const JsonbContainer *container, int index,
83+
static void fillJsonbValue(const JsonbContainerHeader *container, int index,
8684
char *base_addr, uint32 offset,
8785
JsonbValue *result);
8886
static int compareJsonbScalarValue(const JsonbValue *a, const JsonbValue *b);
@@ -107,7 +105,7 @@ static void uniqueifyJsonbObject(JsonbValue *object, bool unique_keys,
107105
static JsonbValue *pushSingleScalarJsonbValue(JsonbParseState **pstate,
108106
const JsonbValue *jbval,
109107
bool unpackBinary);
110-
static void jsonbInitContainer(JsonContainerData *jc, JsonbContainer *jbc, int len);
108+
static void jsonbInitContainer(JsonContainerData *jc, JsonbContainerHeader *jbc, int len);
111109

112110
JsonValue *
113111
JsonValueUnpackBinary(const JsonValue *jbv)
@@ -183,7 +181,7 @@ JsonValueFlatten(const JsonValue *val, JsonValueEncoder encoder,
183181
* by index within the container's JEntry array.
184182
*/
185183
static uint32
186-
getJsonbOffset(const JsonbContainer *jc, int index)
184+
getJsonbOffset(const JsonbContainerHeader *jc, int index)
187185
{
188186
uint32 offset = 0;
189187
int i;
@@ -208,7 +206,7 @@ getJsonbOffset(const JsonbContainer *jc, int index)
208206
* The node is identified by index within the container's JEntry array.
209207
*/
210208
static uint32
211-
getJsonbLength(const JsonbContainer *jc, int index)
209+
getJsonbLength(const JsonbContainerHeader *jc, int index)
212210
{
213211
uint32 off;
214212
uint32 len;
@@ -379,19 +377,19 @@ compareJsonbContainers(JsonContainer *a, JsonContainer *b)
379377

380378
typedef struct JsonbArrayIterator
381379
{
382-
const JsonbContainer *container;
380+
const JsonbContainerHeader *container;
383381
char *base_addr;
384382
int index;
385383
int count;
386384
uint32 offset;
387385
} JsonbArrayIterator;
388386

389387
static void
390-
JsonbArrayIteratorInit(JsonbArrayIterator *it, const JsonbContainer *container)
388+
JsonbArrayIteratorInit(JsonbArrayIterator *it, const JsonbContainerHeader *container)
391389
{
392390
it->container = container;
393391
it->index = 0;
394-
it->count = (container->header & JB_CMASK);
392+
it->count = (container->header & JBC_CMASK);
395393
it->offset = 0;
396394
it->base_addr = (char *) (container->children + it->count);
397395
}
@@ -431,7 +429,7 @@ JsonbArrayIteratorGetIth(JsonbArrayIterator *it, uint32 i)
431429
static JsonbValue *
432430
jsonbFindValueInArray(JsonContainer *jsc, const JsonbValue *key)
433431
{
434-
const JsonbContainer *container = jsc->data;
432+
const JsonbContainerHeader *container = jsc->data;
435433
JsonbArrayIterator it;
436434
JsonbValue *result = palloc(sizeof(JsonbValue));
437435

@@ -510,7 +508,7 @@ static JsonbValue *
510508
jsonbFindKeyInObject(JsonContainer *jsc, const char *keyVal, int keyLen,
511509
JsonValue *res)
512510
{
513-
const JsonbContainer *container = jsc->data;
511+
const JsonbContainerHeader *container = jsc->data;
514512
const JEntry *children = container->children;
515513
int count = JsonContainerSize(jsc);
516514
char *baseAddr;
@@ -603,7 +601,7 @@ jsonbGetArrayElement(JsonContainer *jsc, uint32 i)
603601
* expanded.
604602
*/
605603
static void
606-
fillJsonbValue(const JsonbContainer *container, int index,
604+
fillJsonbValue(const JsonbContainerHeader *container, int index,
607605
char *base_addr, uint32 offset,
608606
JsonbValue *result)
609607
{
@@ -642,7 +640,7 @@ fillJsonbValue(const JsonbContainer *container, int index,
642640
result->val.binary.data = JsonContainerAlloc();
643641
jsonbInitContainer((JsonContainerData *) result->val.binary.data,
644642
/* Remove alignment padding from data pointer and length */
645-
(JsonbContainer *)(base_addr + INTALIGN(offset)),
643+
(JsonbContainerHeader *)(base_addr + INTALIGN(offset)),
646644
getJsonbLength(container, index) -
647645
(INTALIGN(offset) - offset));
648646
}
@@ -1193,40 +1191,40 @@ iteratorFromContainer(JsonContainer *container, jsonbIterator *parent)
11931191
}
11941192

11951193
/*
1196-
* Given a JsonbContainer, expand to jsonbIterator to iterate over items
1194+
* Given a JsonContainer, expand to jsonbIterator to iterate over items
11971195
* fully expanded to in-memory representation for manipulation.
11981196
*
11991197
* See jsonbIteratorNext() for notes on memory management.
12001198
*/
12011199
static JsonIterator *
12021200
jsonbIteratorInit(JsonContainer *cont)
12031201
{
1204-
const JsonbContainer *container = cont->data;
1202+
const JsonbContainerHeader *container = cont->data;
12051203
jsonbIterator *it;
12061204

12071205
it = palloc0(sizeof(jsonbIterator));
12081206
it->ji.container = cont;
12091207
it->ji.parent = NULL;
12101208
it->ji.next = jsonbIteratorNext;
12111209
it->container = container;
1212-
it->nElems = container->header & JB_CMASK;
1210+
it->nElems = container->header & JBC_CMASK;
12131211

12141212
/* Array starts just after header */
12151213
it->children = container->children;
12161214

1217-
switch (container->header & (JB_FARRAY | JB_FOBJECT))
1215+
switch (container->header & (JBC_FARRAY | JBC_FOBJECT))
12181216
{
1219-
case JB_FARRAY:
1217+
case JBC_FARRAY:
12201218
it->dataProper =
12211219
(char *) it->children + it->nElems * sizeof(JEntry);
1222-
it->isScalar = (container->header & JB_FSCALAR) != 0;
1220+
it->isScalar = (container->header & JBC_FSCALAR) != 0;
12231221
/* This is either a "raw scalar", or an array */
12241222
Assert(!it->isScalar || it->nElems == 1);
12251223

12261224
it->state = JBI_ARRAY_START;
12271225
break;
12281226

1229-
case JB_FOBJECT:
1227+
case JBC_FOBJECT:
12301228
it->dataProper =
12311229
(char *) it->children + it->nElems * sizeof(JEntry) * 2;
12321230
it->state = JBI_OBJECT_START;
@@ -1816,12 +1814,12 @@ convertJsonbArray(StringInfo buffer, JEntry *pheader, const JsonbValue *val, int
18161814
* Construct the header Jentry and store it in the beginning of the
18171815
* variable-length payload.
18181816
*/
1819-
header = nElems | JB_FARRAY;
1817+
header = nElems | JBC_FARRAY;
18201818
if (val->val.array.rawScalar)
18211819
{
18221820
Assert(nElems == 1);
18231821
Assert(level == 0);
1824-
header |= JB_FSCALAR;
1822+
header |= JBC_FSCALAR;
18251823
}
18261824

18271825
appendToBuffer(buffer, (char *) &header, sizeof(uint32));
@@ -1902,7 +1900,7 @@ convertJsonbObject(StringInfo buffer, JEntry *pheader, const JsonbValue *val, in
19021900
* Construct the header Jentry and store it in the beginning of the
19031901
* variable-length payload.
19041902
*/
1905-
header = nPairs | JB_FOBJECT;
1903+
header = nPairs | JBC_FOBJECT;
19061904
appendToBuffer(buffer, (char *) &header, sizeof(uint32));
19071905

19081906
/* Reserve space for the JEntries of the keys and values. */
@@ -2205,21 +2203,21 @@ uniqueifyJsonbObject(JsonbValue *object, bool unique_keys, bool skip_nulls)
22052203

22062204

22072205
static void
2208-
jsonbInitContainer(JsonContainerData *jc, JsonbContainer *jbc, int len)
2206+
jsonbInitContainer(JsonContainerData *jc, JsonbContainerHeader *jbc, int len)
22092207
{
22102208
jc->ops = &jsonbContainerOps;
22112209
jc->data = jbc;
22122210
jc->len = len;
2213-
jc->size = jbc->header & JB_CMASK;
2214-
jc->type = jbc->header & JB_FOBJECT ? jbvObject :
2215-
jbc->header & JB_FSCALAR ? jbvArray | jbvScalar :
2216-
jbvArray;
2211+
jc->size = jbc->header & JBC_CMASK;
2212+
jc->type = jbc->header & JBC_FOBJECT ? jbvObject :
2213+
jbc->header & JBC_FSCALAR ? jbvArray | jbvScalar :
2214+
jbvArray;
22172215
}
22182216

22192217
static void
22202218
jsonbInit(JsonContainerData *jc, Datum value)
22212219
{
2222-
Jsonb *jb = (Jsonb *) DatumGetPointer(value);
2220+
JsonbDatum *jb = (JsonbDatum *) DatumGetPointer(value);
22232221
jsonbInitContainer(jc, &jb->root, VARSIZE_ANY_EXHDR(jb));
22242222
}
22252223

src/backend/utils/adt/jsonbsubs.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "parser/parse_coerce.h"
2222
#include "parser/parse_expr.h"
2323
#include "utils/jsonb.h"
24-
#include "utils/json_generic.h"
2524
#include "utils/jsonfuncs.h"
2625
#include "utils/builtins.h"
2726
#include "utils/lsyscache.h"

src/backend/utils/adt/jsonfuncs.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include "utils/hsearch.h"
3232
#include "utils/json.h"
3333
#include "utils/jsonb.h"
34-
#include "utils/json_generic.h"
3534
#include "utils/jsonfuncs.h"
3635
#include "utils/lsyscache.h"
3736
#include "utils/memutils.h"
@@ -3171,7 +3170,6 @@ json_populate_type(Datum json_val, Oid json_type, Oid typid, int32 typmod,
31713170
/* fill binary jsonb value pointing to jb */
31723171
jbv.type = jbvBinary;
31733172
jbv.val.binary.data = &jsonb->root;
3174-
jbv.val.binary.len = VARSIZE(jsonb) - VARHDRSZ;
31753173
}
31763174

31773175
if (!*cache)

src/include/utils/json_generic.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
#ifndef UTILS_JSON_GENERIC_H
1414
#define UTILS_JSON_GENERIC_H
1515

16-
#define JSON_GENERIC
17-
1816
#include "postgres.h"
1917
#include "lib/stringinfo.h"
2018
#include "utils/jsonb.h"
@@ -79,6 +77,10 @@ typedef struct Json
7977
bool is_json; /* json or jsonb */
8078
} Json;
8179

80+
typedef Json Jsonb;
81+
typedef JsonContainer JsonbContainer;
82+
83+
8284
#define JsonIsTemporary(json) ((json)->obj.isTemporary)
8385

8486
#define JsonFlattenToJsonbDatum(json) \
@@ -117,11 +119,6 @@ typedef struct Json
117119

118120
#define JsonbValueToJsonb JsonValueToJson
119121

120-
#ifndef JSONB_UTIL_C
121-
#define Jsonb Json
122-
#define JsonbContainer JsonContainer
123-
#endif
124-
125122
#define JB_ROOT_COUNT(json) JsonContainerSize(JsonRoot(json))
126123
#define JB_ROOT_IS_SCALAR(json) JsonContainerIsScalar(JsonRoot(json))
127124
#define JB_ROOT_IS_OBJECT(json) JsonContainerIsObject(JsonRoot(json))

0 commit comments

Comments
 (0)