Refactor
authorPeter Geoghegan <[email protected]>
Thu, 6 Mar 2014 07:46:30 +0000 (23:46 -0800)
committerPeter Geoghegan <[email protected]>
Thu, 6 Mar 2014 07:46:30 +0000 (23:46 -0800)
Remove dead code. Shape up jsonb.h.

src/backend/utils/adt/jsonb_gist.c
src/backend/utils/adt/jsonb_op.c
src/include/utils/jsonb.h

index 1fed81f3ec2ce1276c93ab62a9986e9a44979ef3..24b676806e0f6a0564f1b9f1bf6d8db3db16ed46 100644 (file)
 #include "utils/jsonb.h"
 #include "utils/pg_crc.h"
 
-/*
- * When using a GIN/GiST 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 to indicate key, value, or null values.  (As of 9.1 it might be
- * better to store null values as nulls, but we'll keep it this way for on-disk
- * compatibility.)
- */
-#define ELEMFLAG    'E'
-#define KEYFLAG     'K'
-#define VALFLAG     'V'
-#define NULLFLAG    'N'
-
 /* bigint defines */
 #define BITBYTE 8
 #define SIGLENINT  4           /* >122 => key will toast, so very slow!!! */
index 438588c4c621595e680f065e8f096ccc599a8342..1eb394046bb2c14b1ee4dd4572d4ce985e2ef8e5 100644 (file)
@@ -95,130 +95,6 @@ arrayToJsonbSortedArray(ArrayType *a)
    return v;
 }
 
-static bool
-j_atoi(char *c, int l, int *acc)
-{
-   bool    negative = false;
-   char    *p = c;
-
-   *acc = 0;
-
-   while(isspace(*p) && p - c < l)
-       p++;
-
-   if (p - c >= l)
-       return false;
-
-   if (*p == '-')
-   {
-       negative = true;
-       p++;
-   }
-   else if (*p == '+')
-   {
-       p++;
-   }
-
-   if (p - c >= l)
-       return false;
-
-
-   while(p - c < l)
-   {
-       if (!isdigit(*p))
-           return false;
-
-       *acc *= 10;
-       *acc += (*p - '0');
-       p++;
-   }
-
-   if (negative)
-       *acc = - *acc;
-
-   return true;
-}
-
-static JsonbValue*
-JsonbDeepFetch(Jsonb *in, ArrayType *path)
-{
-   JsonbValue          *v = NULL;
-   static JsonbValue   init /* could be returned */;
-   Datum               *path_elems;
-   bool                *path_nulls;
-   int                 path_len, i;
-
-   Assert(ARR_ELEMTYPE(path) == TEXTOID);
-
-   if (ARR_NDIM(path) > 1)
-       ereport(ERROR,
-               (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
-                errmsg("wrong number of array subscripts")));
-
-   if (JB_ROOT_COUNT(in) == 0)
-       return NULL;
-
-   deconstruct_array(path, TEXTOID, -1, false, 'i',
-                     &path_elems, &path_nulls, &path_len);
-
-   init.type = jbvBinary;
-   init.size = VARSIZE(in);
-   init.binary.data = VARDATA(in);
-   init.binary.len = VARSIZE_ANY_EXHDR(in);
-
-   v = &init;
-
-   if (path_len == 0)
-       return v;
-
-   for(i=0; v != NULL && i<path_len; i++)
-   {
-       uint32  header;
-
-       if (v->type != jbvBinary || path_nulls[i])
-           return NULL;
-
-       header = *(uint32*)v->binary.data;
-
-       if (header & JB_FLAG_OBJECT)
-       {
-           v = findUncompressedJsonbValue(v->binary.data, JB_FLAG_OBJECT,
-                                           NULL,
-                                           VARDATA_ANY(path_elems[i]),
-                                           VARSIZE_ANY_EXHDR(path_elems[i]));
-       }
-       else if (header & JB_FLAG_ARRAY)
-       {
-           int ith;
-
-           if (j_atoi(VARDATA_ANY(path_elems[i]),
-                      VARSIZE_ANY_EXHDR(path_elems[i]), &ith) == false)
-               return NULL;
-
-           if (ith < 0)
-           {
-               if (-ith > (int)(header & JB_COUNT_MASK))
-                   return NULL;
-               else
-                   ith = ((int)(header & JB_COUNT_MASK)) + ith;
-           }
-           else
-           {
-               if (ith >= (int)(header & JB_COUNT_MASK))
-                   return NULL;
-           }
-
-           v = getJsonbValue(v->binary.data, JB_FLAG_ARRAY, ith);
-       }
-       else
-       {
-           elog(ERROR,"wrong jsonb container type");
-       }
-   }
-
-   return v;
-}
-
 Datum
 jsonb_exists(PG_FUNCTION_ARGS)
 {
@@ -531,7 +407,7 @@ jsonb_contained(PG_FUNCTION_ARGS)
 }
 
 /*
- * B-Tree operator class functions.
+ * B-Tree operator class procedures
  */
 Datum
 jsonb_cmp(PG_FUNCTION_ARGS)
index 679e4549b0c2c9e6619445a8bfd4e673fd7226c7..83c6ccc2922607da5c80de390569237dd0a5c16f 100644 (file)
 #include "utils/array.h"
 #include "utils/numeric.h"
 
-/*
- * JEntry: there is one of these for each key _and_ value in an jsonb
- *
- * the position offset points to the _end_ so that we can get the length
- * by subtraction from the previous entry. the ISFIRST flag lets us tell
- * whether there is a previous entry.
- */
-typedef struct
-{
-   uint32      entry;
-}  JEntry;
-
-#define JENTRY_ISFIRST     0x80000000
-#define JENTRY_ISSTRING        (0x00000000)        /* keep binary compatibility */
+#define JENTRY_ISFIRST     (0x80000000)
+#define JENTRY_ISSTRING        (0x00000000)
 #define JENTRY_ISNUMERIC   (0x10000000)
 #define JENTRY_ISNEST      (0x20000000)
-#define JENTRY_ISNULL      (0x40000000)        /* keep binary compatibility */
+#define JENTRY_ISNULL      (0x40000000)
 #define JENTRY_ISBOOL      (0x10000000 | 0x20000000)
 #define JENTRY_ISFALSE     JENTRY_ISBOOL
 #define JENTRY_ISTRUE      (0x10000000 | 0x20000000 | 0x40000000)
@@ -62,13 +50,6 @@ typedef struct
  */
 #define JSONB_MAX_STRING_LEN       JENTRY_POSMASK
 
-typedef struct
-{
-   int32       vl_len_;        /* varlena header (do not touch directly!) */
-   /* header of hash or array jsonb type */
-   /* array of JEntry follows */
-} Jsonb;
-
 /*
  * it's not possible to get more than 2^28 items into an jsonb.
  */
@@ -93,6 +74,25 @@ typedef struct
 typedef struct JsonbPair JsonbPair;
 typedef struct JsonbValue JsonbValue;
 
+/*
+ * JEntry: there is one of these for each key _and_ value in a jsonb
+ *
+ * The position offset points to the _end_ so that we can get the length
+ * by subtraction from the previous entry. the ISFIRST flag lets us tell
+ * whether there is a previous entry.
+ */
+typedef struct
+{
+   uint32      entry;
+}  JEntry;
+
+typedef struct
+{
+   int32       vl_len_;        /* varlena header (do not touch directly!) */
+   /* header of hash or array jsonb type */
+   /* array of JEntry follows */
+} Jsonb;
+
 struct JsonbValue
 {
    enum
@@ -103,10 +103,10 @@ struct JsonbValue
        jbvBool,
        jbvArray,
        jbvHash,
-       jbvBinary               /* binary form of jbvArray/jbvHash */
+       jbvBinary               /* Binary form of jbvArray/jbvHash */
    }           type;
 
-   uint32      size;           /* estimation size of node (including
+   uint32      size;           /* Estimation size of node (including
                                 * subnodes) */
 
    union
@@ -116,14 +116,14 @@ struct JsonbValue
        struct
        {
            uint32      len;
-           char       *val;    /* could be not null-terminated */
+           char       *val;    /* Not necessarily null-terminated */
        }           string;
 
        struct
        {
            int         nelems;
            JsonbValue *elems;
-           bool        scalar; /* scalar actually shares representation with
+           bool        scalar; /* Scalar actually shares representation with
                                 * array */
        }           array;
 
@@ -146,13 +146,19 @@ struct JsonbPair
 {
    JsonbValue  key;
    JsonbValue  value;
-   uint32      order;          /* to keep order of pairs with equal key */
+   uint32      order;          /* To preserve order of pairs with equal keys */
 };
 
+typedef struct ToJsonbState
+{
+   JsonbValue  v;
+   uint32      size;
+   struct ToJsonbState *next;
+} ToJsonbState;
+
 /*
- * jsonb support functios
+ * jsonb support functions
  */
-
 #define WJB_KEY                (0x001)
 #define WJB_VALUE          (0x002)
 #define WJB_ELEM           (0x004)
@@ -164,31 +170,17 @@ struct JsonbPair
 typedef void (*walk_jsonb_cb) (void * /* arg */ , JsonbValue * /* value */ ,
                                   uint32 /* flags */ , uint32 /* level */ );
 extern void walkUncompressedJsonb(JsonbValue *v, walk_jsonb_cb cb, void *cb_arg);
-
 extern int compareJsonbStringValue(const void *a, const void *b, void *arg);
 extern int compareJsonbPair(const void *a, const void *b, void *arg);
-
 extern int compareJsonbBinaryValue(char *a, char *b);
 extern int compareJsonbValue(JsonbValue *a, JsonbValue *b);
-
 extern JsonbValue *findUncompressedJsonbValueByValue(char *buffer, uint32 flags,
                                  uint32 *lowbound, JsonbValue *key);
 extern JsonbValue *findUncompressedJsonbValue(char *buffer, uint32 flags,
                           uint32 *lowbound, char *key, uint32 keylen);
-
 extern JsonbValue *getJsonbValue(char *buffer, uint32 flags, int32 i);
-
-typedef struct ToJsonbState
-{
-   JsonbValue  v;
-   uint32      size;
-   struct ToJsonbState *next;
-}  ToJsonbState;
-
 extern JsonbValue *pushJsonbValue(ToJsonbState ** state, int r /* WJB_* */ , JsonbValue *v);
-
 extern void uniqueJsonbValue(JsonbValue *v);
-
 extern uint32 compressJsonb(JsonbValue *v, char *buffer);
 
 typedef struct JsonbIterator
@@ -218,17 +210,17 @@ typedef struct JsonbIterator
 } JsonbIterator;
 
 extern JsonbIterator *JsonbIteratorInit(char *buffer);
-extern int /* WJB_* */ JsonbIteratorGet(JsonbIterator **it, JsonbValue *v, bool skipNested);
+extern int JsonbIteratorGet(JsonbIterator **it, JsonbValue *v, bool skipNested);
+extern char *JsonbToCString(StringInfo out, char *in, int estimated_len);
+extern Jsonb *JsonbValueToJsonb(JsonbValue *v);
+extern void JsonbPutEscapedValue(StringInfo out, JsonbValue *v);
 
+/* I/O routines */
 extern Datum jsonb_in(PG_FUNCTION_ARGS);
 extern Datum jsonb_out(PG_FUNCTION_ARGS);
 extern Datum jsonb_recv(PG_FUNCTION_ARGS);
 extern Datum jsonb_send(PG_FUNCTION_ARGS);
-
 extern Datum jsonb_typeof(PG_FUNCTION_ARGS);
-extern char *JsonbToCString(StringInfo out, char *in, int estimated_len);
-extern Jsonb *JsonbValueToJsonb(JsonbValue *v);
-extern void JsonbPutEscapedValue(StringInfo out, JsonbValue *v);
 
 /* Indexing-related ops */
 extern Datum jsonb_exists(PG_FUNCTION_ARGS);