Skip to content

Commit f7c6246

Browse files
committed
Introduce convenience macros to hide JsonbContainer header accesses better.
This improves readability a bit and may make future improvements easier. In passing, make sure that the JB_ROOT_IS_XXX macros deliver boolean (0/1) results; the previous coding was a bug hazard, though no actual bugs are known. Nikita Glukhov, extended a bit by me Discussion: https://postgr.es/m/9e21a39c-c1d7-b9b5-44a0-c5345a5029f6@postgrespro.ru
1 parent 049ac80 commit f7c6246

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

src/backend/utils/adt/jsonb_util.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ findJsonbValueFromContainer(JsonbContainer *container, uint32 flags,
328328
JsonbValue *key)
329329
{
330330
JEntry *children = container->children;
331-
int count = (container->header & JB_CMASK);
331+
int count = JsonContainerSize(container);
332332
JsonbValue *result;
333333

334334
Assert((flags & ~(JB_FARRAY | JB_FOBJECT)) == 0);
@@ -339,7 +339,7 @@ findJsonbValueFromContainer(JsonbContainer *container, uint32 flags,
339339

340340
result = palloc(sizeof(JsonbValue));
341341

342-
if (flags & JB_FARRAY & container->header)
342+
if ((flags & JB_FARRAY) && JsonContainerIsArray(container))
343343
{
344344
char *base_addr = (char *) (children + count);
345345
uint32 offset = 0;
@@ -358,7 +358,7 @@ findJsonbValueFromContainer(JsonbContainer *container, uint32 flags,
358358
JBE_ADVANCE_OFFSET(offset, children[i]);
359359
}
360360
}
361-
else if (flags & JB_FOBJECT & container->header)
361+
else if ((flags & JB_FOBJECT) && JsonContainerIsObject(container))
362362
{
363363
/* Since this is an object, account for *Pairs* of Jentrys */
364364
char *base_addr = (char *) (children + count * 2);
@@ -422,10 +422,10 @@ getIthJsonbValueFromContainer(JsonbContainer *container, uint32 i)
422422
char *base_addr;
423423
uint32 nelements;
424424

425-
if ((container->header & JB_FARRAY) == 0)
425+
if (!JsonContainerIsArray(container))
426426
elog(ERROR, "not a jsonb array");
427427

428-
nelements = container->header & JB_CMASK;
428+
nelements = JsonContainerSize(container);
429429
base_addr = (char *) &container->children[nelements];
430430

431431
if (i >= nelements)
@@ -904,7 +904,7 @@ iteratorFromContainer(JsonbContainer *container, JsonbIterator *parent)
904904
it = palloc(sizeof(JsonbIterator));
905905
it->container = container;
906906
it->parent = parent;
907-
it->nElems = container->header & JB_CMASK;
907+
it->nElems = JsonContainerSize(container);
908908

909909
/* Array starts just after header */
910910
it->children = container->children;
@@ -914,7 +914,7 @@ iteratorFromContainer(JsonbContainer *container, JsonbIterator *parent)
914914
case JB_FARRAY:
915915
it->dataProper =
916916
(char *) it->children + it->nElems * sizeof(JEntry);
917-
it->isScalar = (container->header & JB_FSCALAR) != 0;
917+
it->isScalar = JsonContainerIsScalar(container);
918918
/* This is either a "raw scalar", or an array */
919919
Assert(!it->isScalar || it->nElems == 1);
920920

src/backend/utils/adt/jsonfuncs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,10 +1266,10 @@ get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text)
12661266
uint32 nelements;
12671267

12681268
/* Container must be array, but make sure */
1269-
if ((container->header & JB_FARRAY) == 0)
1269+
if (!JsonContainerIsArray(container))
12701270
elog(ERROR, "not a jsonb array");
12711271

1272-
nelements = container->header & JB_CMASK;
1272+
nelements = JsonContainerSize(container);
12731273

12741274
if (-lindex > nelements)
12751275
PG_RETURN_NULL();

src/include/utils/jsonb.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ typedef struct JsonbContainer
205205
#define JB_FOBJECT 0x20000000
206206
#define JB_FARRAY 0x40000000
207207

208+
/* convenience macros for accessing a JsonbContainer struct */
209+
#define JsonContainerSize(jc) ((jc)->header & JB_CMASK)
210+
#define JsonContainerIsScalar(jc) (((jc)->header & JB_FSCALAR) != 0)
211+
#define JsonContainerIsObject(jc) (((jc)->header & JB_FOBJECT) != 0)
212+
#define JsonContainerIsArray(jc) (((jc)->header & JB_FARRAY) != 0)
213+
208214
/* The top-level on-disk format for a jsonb datum. */
209215
typedef struct
210216
{
@@ -213,10 +219,10 @@ typedef struct
213219
} Jsonb;
214220

215221
/* convenience macros for accessing the root container in a Jsonb datum */
216-
#define JB_ROOT_COUNT(jbp_) ( *(uint32*) VARDATA(jbp_) & JB_CMASK)
217-
#define JB_ROOT_IS_SCALAR(jbp_) ( *(uint32*) VARDATA(jbp_) & JB_FSCALAR)
218-
#define JB_ROOT_IS_OBJECT(jbp_) ( *(uint32*) VARDATA(jbp_) & JB_FOBJECT)
219-
#define JB_ROOT_IS_ARRAY(jbp_) ( *(uint32*) VARDATA(jbp_) & JB_FARRAY)
222+
#define JB_ROOT_COUNT(jbp_) (*(uint32 *) VARDATA(jbp_) & JB_CMASK)
223+
#define JB_ROOT_IS_SCALAR(jbp_) ((*(uint32 *) VARDATA(jbp_) & JB_FSCALAR) != 0)
224+
#define JB_ROOT_IS_OBJECT(jbp_) ((*(uint32 *) VARDATA(jbp_) & JB_FOBJECT) != 0)
225+
#define JB_ROOT_IS_ARRAY(jbp_) ((*(uint32 *) VARDATA(jbp_) & JB_FARRAY) != 0)
220226

221227

222228
enum jbvType
@@ -241,7 +247,7 @@ enum jbvType
241247
*/
242248
struct JsonbValue
243249
{
244-
enum jbvType type; /* Influences sort order */
250+
enum jbvType type; /* Influences sort order */
245251

246252
union
247253
{

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