13
13
*/
14
14
#include "postgres.h"
15
15
16
- #define JSONB_UTIL_C
17
-
18
16
#include "catalog/pg_collation.h"
19
17
#include "catalog/pg_type.h"
20
18
#include "common/hashfn.h"
25
23
#include "utils/datetime.h"
26
24
#include "utils/json.h"
27
25
#include "utils/jsonb.h"
28
- #include "utils/json_generic .h"
26
+ #include "utils/jsonb_internals .h"
29
27
#include "utils/memutils.h"
30
28
#include "utils/varlena.h"
31
29
32
30
/*
33
31
* Maximum number of elements in an array (or key/value pairs in an object).
34
32
* This is limited by two things: the size of the JEntry array must fit
35
33
* 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.
37
35
*
38
36
* (The total size of an array's or object's elements is also limited by
39
37
* JENTRY_OFFLENMASK, but we're not concerned about that here.)
40
38
*/
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 ))
43
41
44
42
/* Conversion state used when parsing Jsonb from text, or for type coercion */
45
43
struct JsonbParseState
@@ -56,7 +54,7 @@ typedef struct jsonbIterator
56
54
JsonIterator ji ;
57
55
58
56
/* Container being iterated */
59
- const JsonbContainer * container ;
57
+ const JsonbContainerHeader * container ;
60
58
61
59
uint32 nElems ; /* Number of elements in children array (will
62
60
* be nPairs for objects) */
@@ -82,7 +80,7 @@ typedef struct jsonbIterator
82
80
JsonbIterState state ;
83
81
} jsonbIterator ;
84
82
85
- static void fillJsonbValue (const JsonbContainer * container , int index ,
83
+ static void fillJsonbValue (const JsonbContainerHeader * container , int index ,
86
84
char * base_addr , uint32 offset ,
87
85
JsonbValue * result );
88
86
static int compareJsonbScalarValue (const JsonbValue * a , const JsonbValue * b );
@@ -107,7 +105,7 @@ static void uniqueifyJsonbObject(JsonbValue *object, bool unique_keys,
107
105
static JsonbValue * pushSingleScalarJsonbValue (JsonbParseState * * pstate ,
108
106
const JsonbValue * jbval ,
109
107
bool unpackBinary );
110
- static void jsonbInitContainer (JsonContainerData * jc , JsonbContainer * jbc , int len );
108
+ static void jsonbInitContainer (JsonContainerData * jc , JsonbContainerHeader * jbc , int len );
111
109
112
110
JsonValue *
113
111
JsonValueUnpackBinary (const JsonValue * jbv )
@@ -183,7 +181,7 @@ JsonValueFlatten(const JsonValue *val, JsonValueEncoder encoder,
183
181
* by index within the container's JEntry array.
184
182
*/
185
183
static uint32
186
- getJsonbOffset (const JsonbContainer * jc , int index )
184
+ getJsonbOffset (const JsonbContainerHeader * jc , int index )
187
185
{
188
186
uint32 offset = 0 ;
189
187
int i ;
@@ -208,7 +206,7 @@ getJsonbOffset(const JsonbContainer *jc, int index)
208
206
* The node is identified by index within the container's JEntry array.
209
207
*/
210
208
static uint32
211
- getJsonbLength (const JsonbContainer * jc , int index )
209
+ getJsonbLength (const JsonbContainerHeader * jc , int index )
212
210
{
213
211
uint32 off ;
214
212
uint32 len ;
@@ -379,19 +377,19 @@ compareJsonbContainers(JsonContainer *a, JsonContainer *b)
379
377
380
378
typedef struct JsonbArrayIterator
381
379
{
382
- const JsonbContainer * container ;
380
+ const JsonbContainerHeader * container ;
383
381
char * base_addr ;
384
382
int index ;
385
383
int count ;
386
384
uint32 offset ;
387
385
} JsonbArrayIterator ;
388
386
389
387
static void
390
- JsonbArrayIteratorInit (JsonbArrayIterator * it , const JsonbContainer * container )
388
+ JsonbArrayIteratorInit (JsonbArrayIterator * it , const JsonbContainerHeader * container )
391
389
{
392
390
it -> container = container ;
393
391
it -> index = 0 ;
394
- it -> count = (container -> header & JB_CMASK );
392
+ it -> count = (container -> header & JBC_CMASK );
395
393
it -> offset = 0 ;
396
394
it -> base_addr = (char * ) (container -> children + it -> count );
397
395
}
@@ -431,7 +429,7 @@ JsonbArrayIteratorGetIth(JsonbArrayIterator *it, uint32 i)
431
429
static JsonbValue *
432
430
jsonbFindValueInArray (JsonContainer * jsc , const JsonbValue * key )
433
431
{
434
- const JsonbContainer * container = jsc -> data ;
432
+ const JsonbContainerHeader * container = jsc -> data ;
435
433
JsonbArrayIterator it ;
436
434
JsonbValue * result = palloc (sizeof (JsonbValue ));
437
435
@@ -510,7 +508,7 @@ static JsonbValue *
510
508
jsonbFindKeyInObject (JsonContainer * jsc , const char * keyVal , int keyLen ,
511
509
JsonValue * res )
512
510
{
513
- const JsonbContainer * container = jsc -> data ;
511
+ const JsonbContainerHeader * container = jsc -> data ;
514
512
const JEntry * children = container -> children ;
515
513
int count = JsonContainerSize (jsc );
516
514
char * baseAddr ;
@@ -603,7 +601,7 @@ jsonbGetArrayElement(JsonContainer *jsc, uint32 i)
603
601
* expanded.
604
602
*/
605
603
static void
606
- fillJsonbValue (const JsonbContainer * container , int index ,
604
+ fillJsonbValue (const JsonbContainerHeader * container , int index ,
607
605
char * base_addr , uint32 offset ,
608
606
JsonbValue * result )
609
607
{
@@ -642,7 +640,7 @@ fillJsonbValue(const JsonbContainer *container, int index,
642
640
result -> val .binary .data = JsonContainerAlloc ();
643
641
jsonbInitContainer ((JsonContainerData * ) result -> val .binary .data ,
644
642
/* Remove alignment padding from data pointer and length */
645
- (JsonbContainer * )(base_addr + INTALIGN (offset )),
643
+ (JsonbContainerHeader * )(base_addr + INTALIGN (offset )),
646
644
getJsonbLength (container , index ) -
647
645
(INTALIGN (offset ) - offset ));
648
646
}
@@ -1193,40 +1191,40 @@ iteratorFromContainer(JsonContainer *container, jsonbIterator *parent)
1193
1191
}
1194
1192
1195
1193
/*
1196
- * Given a JsonbContainer , expand to jsonbIterator to iterate over items
1194
+ * Given a JsonContainer , expand to jsonbIterator to iterate over items
1197
1195
* fully expanded to in-memory representation for manipulation.
1198
1196
*
1199
1197
* See jsonbIteratorNext() for notes on memory management.
1200
1198
*/
1201
1199
static JsonIterator *
1202
1200
jsonbIteratorInit (JsonContainer * cont )
1203
1201
{
1204
- const JsonbContainer * container = cont -> data ;
1202
+ const JsonbContainerHeader * container = cont -> data ;
1205
1203
jsonbIterator * it ;
1206
1204
1207
1205
it = palloc0 (sizeof (jsonbIterator ));
1208
1206
it -> ji .container = cont ;
1209
1207
it -> ji .parent = NULL ;
1210
1208
it -> ji .next = jsonbIteratorNext ;
1211
1209
it -> container = container ;
1212
- it -> nElems = container -> header & JB_CMASK ;
1210
+ it -> nElems = container -> header & JBC_CMASK ;
1213
1211
1214
1212
/* Array starts just after header */
1215
1213
it -> children = container -> children ;
1216
1214
1217
- switch (container -> header & (JB_FARRAY | JB_FOBJECT ))
1215
+ switch (container -> header & (JBC_FARRAY | JBC_FOBJECT ))
1218
1216
{
1219
- case JB_FARRAY :
1217
+ case JBC_FARRAY :
1220
1218
it -> dataProper =
1221
1219
(char * ) it -> children + it -> nElems * sizeof (JEntry );
1222
- it -> isScalar = (container -> header & JB_FSCALAR ) != 0 ;
1220
+ it -> isScalar = (container -> header & JBC_FSCALAR ) != 0 ;
1223
1221
/* This is either a "raw scalar", or an array */
1224
1222
Assert (!it -> isScalar || it -> nElems == 1 );
1225
1223
1226
1224
it -> state = JBI_ARRAY_START ;
1227
1225
break ;
1228
1226
1229
- case JB_FOBJECT :
1227
+ case JBC_FOBJECT :
1230
1228
it -> dataProper =
1231
1229
(char * ) it -> children + it -> nElems * sizeof (JEntry ) * 2 ;
1232
1230
it -> state = JBI_OBJECT_START ;
@@ -1816,12 +1814,12 @@ convertJsonbArray(StringInfo buffer, JEntry *pheader, const JsonbValue *val, int
1816
1814
* Construct the header Jentry and store it in the beginning of the
1817
1815
* variable-length payload.
1818
1816
*/
1819
- header = nElems | JB_FARRAY ;
1817
+ header = nElems | JBC_FARRAY ;
1820
1818
if (val -> val .array .rawScalar )
1821
1819
{
1822
1820
Assert (nElems == 1 );
1823
1821
Assert (level == 0 );
1824
- header |= JB_FSCALAR ;
1822
+ header |= JBC_FSCALAR ;
1825
1823
}
1826
1824
1827
1825
appendToBuffer (buffer , (char * ) & header , sizeof (uint32 ));
@@ -1902,7 +1900,7 @@ convertJsonbObject(StringInfo buffer, JEntry *pheader, const JsonbValue *val, in
1902
1900
* Construct the header Jentry and store it in the beginning of the
1903
1901
* variable-length payload.
1904
1902
*/
1905
- header = nPairs | JB_FOBJECT ;
1903
+ header = nPairs | JBC_FOBJECT ;
1906
1904
appendToBuffer (buffer , (char * ) & header , sizeof (uint32 ));
1907
1905
1908
1906
/* Reserve space for the JEntries of the keys and values. */
@@ -2205,21 +2203,21 @@ uniqueifyJsonbObject(JsonbValue *object, bool unique_keys, bool skip_nulls)
2205
2203
2206
2204
2207
2205
static void
2208
- jsonbInitContainer (JsonContainerData * jc , JsonbContainer * jbc , int len )
2206
+ jsonbInitContainer (JsonContainerData * jc , JsonbContainerHeader * jbc , int len )
2209
2207
{
2210
2208
jc -> ops = & jsonbContainerOps ;
2211
2209
jc -> data = jbc ;
2212
2210
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 ;
2217
2215
}
2218
2216
2219
2217
static void
2220
2218
jsonbInit (JsonContainerData * jc , Datum value )
2221
2219
{
2222
- Jsonb * jb = (Jsonb * ) DatumGetPointer (value );
2220
+ JsonbDatum * jb = (JsonbDatum * ) DatumGetPointer (value );
2223
2221
jsonbInitContainer (jc , & jb -> root , VARSIZE_ANY_EXHDR (jb ));
2224
2222
}
2225
2223
0 commit comments