On second thought, have macros live in jsonb.h
authorPeter Geoghegan <pg@heroku.com>
Sat, 15 Mar 2014 20:44:23 +0000 (13:44 -0700)
committerPeter Geoghegan <pg@heroku.com>
Sat, 15 Mar 2014 20:44:23 +0000 (13:44 -0700)
src/backend/utils/adt/jsonb.c
src/backend/utils/adt/jsonb_util.c
src/include/utils/jsonb.h

index 2856ed2f9fe167d7e7d1497c17ffe456b7e9d76d..9944110f69dff60133a04093f0695637772d4b99 100644 (file)
@@ -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")));
index e1b7c07b1e420e4141a8324c245b512ea5f755c1..704c715292f40bf2364ad3a7e98ca84a63944795 100644 (file)
 #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))
index 568c78de0972fbb1ecf1a86fbd79f7c220b90bd6..44d8eaf483a1d1601f33ed3537de88de1b762ccc 100644 (file)
 #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
 
 #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
 #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