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"
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 */
4543struct 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 );
8886static int compareJsonbScalarValue (const JsonbValue * a , const JsonbValue * b );
@@ -107,7 +105,7 @@ static void uniqueifyJsonbObject(JsonbValue *object, bool unique_keys,
107105static 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
112110JsonValue *
113111JsonValueUnpackBinary (const JsonValue * jbv )
@@ -183,7 +181,7 @@ JsonValueFlatten(const JsonValue *val, JsonValueEncoder encoder,
183181 * by index within the container's JEntry array.
184182 */
185183static 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 */
210208static 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
380378typedef 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
389387static 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)
431429static JsonbValue *
432430jsonbFindValueInArray (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 *
510508jsonbFindKeyInObject (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 */
605603static 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 */
12011199static JsonIterator *
12021200jsonbIteratorInit (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
22072205static 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
22192217static void
22202218jsonbInit (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
0 commit comments