More comments on default GIN opclass
authorPeter Geoghegan <pg@heroku.com>
Sat, 15 Mar 2014 08:07:28 +0000 (01:07 -0700)
committerPeter Geoghegan <pg@heroku.com>
Sat, 15 Mar 2014 08:07:28 +0000 (01:07 -0700)
src/backend/utils/adt/jsonb_gin.c
src/backend/utils/adt/jsonb_support.c
src/include/utils/jsonb.h

index 29a7a4abe7a6ff3b96bb69a0e8cf10f26457e708..664ce5a5a21cea46a19402586fdfb3419069335c 100644 (file)
@@ -91,14 +91,18 @@ gin_extract_jsonb(PG_FUNCTION_ARGS)
            entries = (Datum *) repalloc(entries, sizeof(Datum) * total);
        }
 
+       /*
+        * Serialize keys and elements as one.  Array elements must be indexed
+        * as keys for the benefit of JsonbContainsStrategyNumber.
+        *
+        * Even though we bunch together keys and elements here, the resultant
+        * serialized text datum sufficiently reflects the original Jsonb
+        * structure, because Keys must have values, and elements cannot have
+        * keys.
+        */
        switch (r)
        {
            case WJB_KEY:
-               /*
-                * Serialize keys and elements as one.  This is necessary
-                * because array elements must be indexed as keys for the
-                * benefit of JsonbContainsStrategyNumber.
-                */
            case WJB_ELEM:
                entries[i++] = PointerGetDatum(make_scalar_text_key(&v, KEYELEMFLAG));
                break;
@@ -142,7 +146,7 @@ gin_extract_jsonb_query(PG_FUNCTION_ARGS)
        *nentries = 1;
        entries = (Datum *) palloc(sizeof(Datum));
        item = make_text_key(VARDATA_ANY(query), VARSIZE_ANY_EXHDR(query),
-                       KEYELEMFLAG);
+                            KEYELEMFLAG);
        entries[0] = PointerGetDatum(item);
    }
    else if (strategy == JsonbExistsAnyStrategyNumber ||
@@ -168,8 +172,8 @@ gin_extract_jsonb_query(PG_FUNCTION_ARGS)
            if (key_nulls[i])
                continue;
            item = make_text_key(VARDATA(key_datums[i]),
-                           VARSIZE(key_datums[i]) - VARHDRSZ,
-                           KEYELEMFLAG);
+                                VARSIZE(key_datums[i]) - VARHDRSZ,
+                                KEYELEMFLAG);
            entries[j++] = PointerGetDatum(item);
        }
 
@@ -456,7 +460,7 @@ make_scalar_text_key(const JsonbValue * v, char flag)
             * It isn't ideal that numerics are stored in a relatively bulky
             * textual format.  However, it's a notationally convenient way of
             * storing a "union" type in the GIN B-Tree, and indexing Jsonb
-            * strings take precedence.
+            * strings takes precedence.
             */
            cstr = numeric_normalize(v->numeric);
            item = make_text_key(cstr, strlen(cstr), flag);
index 9f4bbd593aa2037ff0942ace7aa74b79b6b37437..5c0b671d947e8a75137ee900ce730711dc68372c 100644 (file)
@@ -772,7 +772,7 @@ JsonbIteratorNext(JsonbIterator ** it, JsonbValue * v, bool skipNested)
  *
  * Containment means that all of the values in the rhs "mContained" iterator's
  * datum exist within the lhs "val" datum at the same nesting level (and, by
- * corollary, all sub-nesting levels).
+ * corollary, all sub-nesting levels on the rhs).
  */
 bool
 deepContains(JsonbIterator ** val, JsonbIterator ** mContained)
index 5575557a8814730f4319938e53766e2b2be2b378..ef606cef500721df73708e34f1289721f07e7a63 100644 (file)
@@ -161,7 +161,7 @@ struct JsonbValue
        {
            uint32      len;
            char       *val;    /* Not necessarily null-terminated */
-       } string;
+       } string;       /* String primitive type */
 
        struct
        {
@@ -169,13 +169,13 @@ struct JsonbValue
            JsonbValue *elems;
            bool        scalar; /* Scalar actually shares representation with
                                 * array */
-       } array;
+       } array;        /* Array container type */
 
        struct
        {
            int         nPairs;
            JsonbPair  *pairs;
-       } object;       /* Associative data structure */
+       } object;       /* Associative container type */
 
        struct
        {
@@ -219,9 +219,6 @@ typedef struct JsonbIterator
    /* Jsonb varlena buffer (may or may not be root) */
    char       *buffer;
 
-   /* Current item in buffer */
-   int         i;
-
    /* Current value */
    uint32      containerType; /* Never of value JB_FSCALAR, since
                                * scalars will appear in pseudo-arrays */
@@ -230,6 +227,10 @@ typedef struct JsonbIterator
    bool        isScalar;      /* Pseudo-array scalar value? */
    JEntry     *meta;
 
+   /* 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
@@ -289,7 +290,8 @@ extern JsonbValue *getIthJsonbValueFromSuperHeader(JsonbSuperHeader sheader,
                                                   uint32 flags, uint32 i);
 extern JsonbValue *pushJsonbValue(ToJsonbState ** state, int r, JsonbValue *v);
 extern JsonbIterator *JsonbIteratorInit(JsonbSuperHeader buffer);
-extern int JsonbIteratorNext(JsonbIterator **it, JsonbValue *v, bool skipNested);
+extern int JsonbIteratorNext(JsonbIterator **it, JsonbValue *v,
+                            bool skipNested);
 extern Jsonb *JsonbValueToJsonb(JsonbValue *v);
 extern bool deepContains(JsonbIterator ** val, JsonbIterator ** mContained);
 extern JsonbValue *arrayToJsonbSortedArray(ArrayType *a);