From 31c7e0667153650f870e9251784798594ee10568 Mon Sep 17 00:00:00 2001 From: Peter Geoghegan Date: Sat, 15 Mar 2014 13:44:23 -0700 Subject: [PATCH] On second thought, have macros live in jsonb.h --- src/backend/utils/adt/jsonb.c | 2 +- src/backend/utils/adt/jsonb_util.c | 23 +----------- src/include/utils/jsonb.h | 60 +++++++++++++++++------------- 3 files changed, 37 insertions(+), 48 deletions(-) diff --git a/src/backend/utils/adt/jsonb.c b/src/backend/utils/adt/jsonb.c index 2856ed2f9f..9944110f69 100644 --- a/src/backend/utils/adt/jsonb.c +++ b/src/backend/utils/adt/jsonb.c @@ -196,7 +196,7 @@ cstring_with_len_to_jsonb(char *json, int len) static size_t checkStringLen(size_t len) { - if (len > JSONB_MAX_STRING_LEN) + if (len > JENTRY_POSMASK) ereport(ERROR, (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION), errmsg("string too long for jsonb string"))); diff --git a/src/backend/utils/adt/jsonb_util.c b/src/backend/utils/adt/jsonb_util.c index e1b7c07b1e..704c715292 100644 --- a/src/backend/utils/adt/jsonb_util.c +++ b/src/backend/utils/adt/jsonb_util.c @@ -19,30 +19,10 @@ #include "utils/memutils.h" #include "utils/jsonb.h" -#define JENTRY_ISSTRING (0x00000000) -#define JENTRY_ISNUMERIC (0x10000000) -#define JENTRY_ISNEST (0x20000000) -#define JENTRY_ISNULL (0x40000000) -#define JENTRY_ISBOOL (0x10000000 | 0x20000000) -#define JENTRY_ISFALSE JENTRY_ISBOOL -#define JENTRY_ISTRUE (0x10000000 | 0x20000000 | 0x40000000) - -/* Note possible multiple evaluations, also access to prior array element */ -#define JBE_ISFIRST(he_) (((he_).header & JENTRY_ISFIRST) != 0) -#define JBE_ISSTRING(he_) (((he_).header & JENTRY_TYPEMASK) == JENTRY_ISSTRING) -#define JBE_ISNUMERIC(he_) (((he_).header & JENTRY_TYPEMASK) == JENTRY_ISNUMERIC) -#define JBE_ISNEST(he_) (((he_).header & JENTRY_TYPEMASK) == JENTRY_ISNEST) -#define JBE_ISNULL(he_) (((he_).header & JENTRY_TYPEMASK) == JENTRY_ISNULL) -#define JBE_ISBOOL(he_) (((he_).header & JENTRY_TYPEMASK & JENTRY_ISBOOL) == JENTRY_ISBOOL) -#define JBE_ISBOOL_TRUE(he_) (((he_).header & JENTRY_TYPEMASK) == JENTRY_ISTRUE) -#define JBE_ISBOOL_FALSE(he_) (JBE_ISBOOL(he_) && !JBE_ISBOOL_TRUE(he_)) - /* * State used while converting an arbitrary JsonbValue into a Jsonb value * (4-byte varlena uncompressed representation of a Jsonb) - */ - -/* + * * ConvertLevel: Bookkeeping around current level when converting. */ typedef struct convertLevel @@ -497,7 +477,6 @@ getIthJsonbValueFromSuperHeader(JsonbSuperHeader sheader, uint32 flags, { r->type = jbvNumeric; r->numeric = (Numeric) (data + INTALIGN(JBE_OFF(*e))); - r->size = 2 * sizeof(JEntry) + VARSIZE_ANY(r->numeric); } else if (JBE_ISNULL(*e)) diff --git a/src/include/utils/jsonb.h b/src/include/utils/jsonb.h index 568c78de09..44d8eaf483 100644 --- a/src/include/utils/jsonb.h +++ b/src/include/utils/jsonb.h @@ -16,19 +16,10 @@ #include "utils/array.h" #include "utils/numeric.h" -#define JENTRY_POSMASK 0x0FFFFFFF -#define JENTRY_ISFIRST 0x80000000 -#define JENTRY_TYPEMASK (~(JENTRY_POSMASK | JENTRY_ISFIRST)) - -/* - * determined by the size of "endpos" (ie JENTRY_POSMASK) - */ -#define JSONB_MAX_STRING_LEN JENTRY_POSMASK - /* - * It's not possible to get more than 2^28 items into an Jsonb. + * JB_CMASK is used to extract count of items * - * JB_CMASK is count of items mask + * It's not possible to get more than 2^28 items into an Jsonb. */ #define JB_CMASK 0x0FFFFFFF @@ -47,6 +38,34 @@ #define JB_ROOT_IS_ARRAY(jbp_) (JB_ISEMPTY(jbp_) ? \ 0: ( *(uint32*) VARDATA(jbp_) & JB_FARRAY)) +/* Jentry macros */ +#define JENTRY_POSMASK 0x0FFFFFFF +#define JENTRY_ISFIRST 0x80000000 +#define JENTRY_TYPEMASK (~(JENTRY_POSMASK | JENTRY_ISFIRST)) +#define JENTRY_ISSTRING (0x00000000) +#define JENTRY_ISNUMERIC (0x10000000) +#define JENTRY_ISNEST (0x20000000) +#define JENTRY_ISNULL (0x40000000) +#define JENTRY_ISBOOL (0x10000000 | 0x20000000) +#define JENTRY_ISFALSE JENTRY_ISBOOL +#define JENTRY_ISTRUE (0x10000000 | 0x20000000 | 0x40000000) +/* Note possible multiple evaluations, also access to prior array element */ +#define JBE_ISFIRST(je_) (((je_).header & JENTRY_ISFIRST) != 0) +#define JBE_ISSTRING(je_) (((je_).header & JENTRY_TYPEMASK) == JENTRY_ISSTRING) +#define JBE_ISNUMERIC(je_) (((je_).header & JENTRY_TYPEMASK) == JENTRY_ISNUMERIC) +#define JBE_ISNEST(je_) (((je_).header & JENTRY_TYPEMASK) == JENTRY_ISNEST) +#define JBE_ISNULL(je_) (((je_).header & JENTRY_TYPEMASK) == JENTRY_ISNULL) +#define JBE_ISBOOL(je_) (((je_).header & JENTRY_TYPEMASK & JENTRY_ISBOOL) == JENTRY_ISBOOL) +#define JBE_ISBOOL_TRUE(je_) (((je_).header & JENTRY_TYPEMASK) == JENTRY_ISTRUE) +#define JBE_ISBOOL_FALSE(je_) (JBE_ISBOOL(je_) && !JBE_ISBOOL_TRUE(je_)) + +/* Get offset for Jentry */ +#define JBE_ENDPOS(je_) ((je_).header & JENTRY_POSMASK) +#define JBE_OFF(je_) (JBE_ISFIRST(je_) ? 0 : JBE_ENDPOS((&(je_))[-1])) +#define JBE_LEN(je_) (JBE_ISFIRST(je_) ? \ + JBE_ENDPOS(je_) \ + : JBE_ENDPOS(je_) - JBE_ENDPOS((&(je_))[-1])) + /* Flags indicating a stage of sequential Jsonb processing */ #define WJB_DONE 0x000 #define WJB_KEY 0x001 @@ -57,13 +76,6 @@ #define WJB_BEGIN_OBJECT 0x020 #define WJB_END_OBJECT 0x040 -/* Get offset for Jentry */ -#define JBE_ENDPOS(he_) ((he_).header & JENTRY_POSMASK) -#define JBE_OFF(he_) (JBE_ISFIRST(he_) ? 0 : JBE_ENDPOS((&(he_))[-1])) -#define JBE_LEN(he_) (JBE_ISFIRST(he_) ? \ - JBE_ENDPOS(he_) \ - : JBE_ENDPOS(he_) - JBE_ENDPOS((&(he_))[-1])) - /* * When using a GIN index for jsonb, we choose to index both keys and values. * The storage format is "text" values, with K, V, or N prepended to the string @@ -93,7 +105,6 @@ typedef struct JsonbPair JsonbPair; typedef struct JsonbValue JsonbValue; typedef char* JsonbSuperHeader; - /* * Jsonbs are varlena objects, so must meet the varlena convention that the * first int32 of the object contains the total object size in bytes. Be sure @@ -138,7 +149,7 @@ struct JsonbValue { enum { - /* Scalar types (influences sort order) */ + /* Scalar types */ jbvNull = 0x0, jbvString, jbvNumeric, @@ -148,9 +159,9 @@ struct JsonbValue jbvObject, /* Binary form of jbvArray/jbvObject/scalar */ jbvBinary - } type; + } type; /* Influences sort order */ - Size size; /* Estimation size of node (including + Size size; /* Estimated size of node (including * subnodes) */ union @@ -159,7 +170,7 @@ struct JsonbValue bool boolean; struct { - uint32 len; + Size len; char *val; /* Not necessarily null-terminated */ } string; /* String primitive type */ @@ -179,7 +190,7 @@ struct JsonbValue struct { - uint32 len; + Size len; char *data; } binary; }; @@ -230,7 +241,6 @@ typedef struct JsonbIterator /* Current item in buffer ("nElems-wise" count) */ int i; - /* * Data proper. Note that this points just past end of meta array. We use * "meta" metadata (Jentrys) with JBE_OFF() macro to find appropriate -- 2.39.5