Tweaks
authorPeter Geoghegan <[email protected]>
Tue, 11 Mar 2014 07:53:44 +0000 (00:53 -0700)
committerPeter Geoghegan <[email protected]>
Tue, 11 Mar 2014 07:53:44 +0000 (00:53 -0700)
JsonbIteratorGet() name and position altered.

Consistently use the term "scalar" in preference to the RFC term
"primitive" internally.

doc/src/sgml/json.sgml
src/backend/utils/adt/jsonb.c
src/backend/utils/adt/jsonb_gin.c
src/backend/utils/adt/jsonb_gist.c
src/backend/utils/adt/jsonb_op.c
src/backend/utils/adt/jsonb_support.c
src/backend/utils/adt/jsonfuncs.c
src/include/utils/jsonb.h

index f2f108211b86f04cffb7291f200510e9bee8c242..d55517fd5de2f2f52d24c105f008d260fb79425e 100644 (file)
   <para>
    The following are all valid <type>jsonb</> expressions:
   <programlisting>
--- Simple scalar value (explicitly required by RFC-7159)
+-- Simple scalar/primitive value (explicitly required by RFC-7159)
 SELECT '5'::jsonb;
 
 -- Array of heterogeneous, primitive-typed elements
@@ -180,8 +180,8 @@ SELECT '{"bar": "baz", "balance": 7.77, "active":false}'::jsonb;
   </programlisting>
   </para>
   <para>
-   Note the distinction between scalar values (primitive types) as
-   elements, keys and values.
+   Note the distinction between scalar/primitive values as elements,
+   keys and values.
   </para>
  </sect2>
  <sect2 id="json-indexing">
index efa0c6c27fe152a617e84c42dca4e285d9acda3a..3852e8dc74ecb26003c284a1627e71120ea1d914 100644 (file)
@@ -137,8 +137,8 @@ jsonb_typeof(PG_FUNCTION_ARGS)
         * A root scalar is stored as an array of one element, so we get the
         * array and then its first (and only) member.
         */
-       (void) JsonbIteratorGet(&it, &v, true);
-       (void) JsonbIteratorGet(&it, &v, true);
+       (void) JsonbIteratorNext(&it, &v, true);
+       (void) JsonbIteratorNext(&it, &v, true);
        switch (v.type)
        {
            case jbvNull:
@@ -382,7 +382,7 @@ JsonbToCString(StringInfo out, char *in, int estimated_len)
 
    it = JsonbIteratorInit(in);
 
-   while (redo_switch || ((type = JsonbIteratorGet(&it, &v, false)) != 0))
+   while (redo_switch || ((type = JsonbIteratorNext(&it, &v, false)) != 0))
    {
        redo_switch = false;
        switch (type)
@@ -413,7 +413,7 @@ JsonbToCString(StringInfo out, char *in, int estimated_len)
                jsonb_put_escaped_value(out, &v);
                appendBinaryStringInfo(out, ": ", 2);
 
-               type = JsonbIteratorGet(&it, &v, false);
+               type = JsonbIteratorNext(&it, &v, false);
                if (type == WJB_VALUE)
                {
                    first = false;
index 293930d79a3b26e02dd035f6ffb7fa06008e98bd..788d21ee8e1edf4820952b0e126d319172aa1aa5 100644 (file)
@@ -28,8 +28,8 @@ typedef struct PathHashStack
 }  PathHashStack;
 
 static text *make_text_key(const char *str, int len, char flag);
-static text *make_primitive_text_key(const JsonbValue * v, char flag);
-static void hash_primitive_value(const JsonbValue * v, PathHashStack * stack);
+static text *make_scalar_text_key(const JsonbValue * v, char flag);
+static void hash_scalar_value(const JsonbValue * v, PathHashStack * stack);
 
 /*
  *
@@ -58,7 +58,7 @@ gin_extract_jsonb(PG_FUNCTION_ARGS)
 
    it = JsonbIteratorInit(VARDATA(jb));
 
-   while ((r = JsonbIteratorGet(&it, &v, false)) != 0)
+   while ((r = JsonbIteratorNext(&it, &v, false)) != 0)
    {
        if (i >= total)
        {
@@ -75,10 +75,10 @@ gin_extract_jsonb(PG_FUNCTION_ARGS)
                 * benefit of JsonbContainsStrategyNumber.
                 */
            case WJB_ELEM:
-               entries[i++] = PointerGetDatum(make_primitive_text_key(&v, KEYELEMFLAG));
+               entries[i++] = PointerGetDatum(make_scalar_text_key(&v, KEYELEMFLAG));
                break;
            case WJB_VALUE:
-               entries[i++] = PointerGetDatum(make_primitive_text_key(&v, VALFLAG));
+               entries[i++] = PointerGetDatum(make_scalar_text_key(&v, VALFLAG));
                break;
            default:
                break;
@@ -302,7 +302,7 @@ gin_extract_jsonb_hash(PG_FUNCTION_ARGS)
     * path.  For faster calculation of hashes, use a stack of precalculated
     * hashes of prefixes.
     */
-   while ((r = JsonbIteratorGet(&it, &v, false)) != 0)
+   while ((r = JsonbIteratorNext(&it, &v, false)) != 0)
    {
        pg_crc32        path_crc32;
        PathHashStack  *tmp;
@@ -333,14 +333,14 @@ gin_extract_jsonb_hash(PG_FUNCTION_ARGS)
            case WJB_KEY:
                /* Calc hash of key and separated into preserved stack item */
                stack->hash_state = stack->next->hash_state;
-               hash_primitive_value(&v, stack);
+               hash_scalar_value(&v, stack);
                COMP_CRC32(stack->hash_state, PATH_SEPARATOR, 1);
                break;
            case WJB_VALUE:
            case WJB_ELEM:
                stack->hash_state = stack->next->hash_state;
                COMP_CRC32(stack->hash_state, PATH_SEPARATOR, 1);
-               hash_primitive_value(&v, stack);
+               hash_scalar_value(&v, stack);
                path_crc32 = stack->hash_state;
                FIN_CRC32(path_crc32);
                entries[i++] = path_crc32;
@@ -413,7 +413,7 @@ make_text_key(const char *str, int len, char flag)
  * Create a textual representation of a jsonbValue for GIN storage.
  */
 static text *
-make_primitive_text_key(const JsonbValue * v, char flag)
+make_scalar_text_key(const JsonbValue * v, char flag)
 {
    text       *item;
    char       *cstr;
@@ -439,17 +439,17 @@ make_primitive_text_key(const JsonbValue * v, char flag)
            item = make_text_key(v->string.val, v->string.len, flag);
            break;
        default:
-           elog(ERROR, "invalid jsonb primitive type: %d", v->type);
+           elog(ERROR, "invalid jsonb scalar type: %d", v->type);
    }
 
    return item;
 }
 
 /*
- * Hash a JsonbValue primitive value, and push it on to hashing stack
+ * Hash a JsonbValue scalar value, and push it on to hashing stack
  */
 static void
-hash_primitive_value(const JsonbValue * v, PathHashStack * stack)
+hash_scalar_value(const JsonbValue * v, PathHashStack * stack)
 {
    switch (v->type)
    {
@@ -470,7 +470,7 @@ hash_primitive_value(const JsonbValue * v, PathHashStack * stack)
            COMP_CRC32(stack->hash_state, v->string.val, v->string.len);
            break;
        default:
-           elog(ERROR, "invalid jsonb primitive type");
+           elog(ERROR, "invalid jsonb scalar type");
            break;
    }
 }
index 125b34ad0d1831f5ac61293a799fd788a497085a..8d3f0eaf257f494988cc34d26b54370ae91d65f7 100644 (file)
@@ -138,7 +138,7 @@ gjsonb_consistent(PG_FUNCTION_ARGS)
                JsonbIterator *it = JsonbIteratorInit(VARDATA(query));
                JsonbValue  v;
 
-               while ((r = JsonbIteratorGet(&it, &v, false)) != 0)
+               while ((r = JsonbIteratorNext(&it, &v, false)) != 0)
                {
                    if ((r == WJB_ELEM || r == WJB_KEY || r == WJB_VALUE) && v.type != jbvNull)
                    {
@@ -298,7 +298,7 @@ gjsonb_compress(PG_FUNCTION_ARGS)
            JsonbValue  v;
            int         r;
 
-           while ((r = JsonbIteratorGet(&it, &v, false)) != 0)
+           while ((r = JsonbIteratorNext(&it, &v, false)) != 0)
            {
                if ((r == WJB_ELEM || r == WJB_KEY || r == WJB_VALUE) &&
                    v.type != jbvNull)
index 2f87d45ba2db885448a3d23f389e984c941bd21b..c56a78de23583ae73ea2131482fcd4c802b2c5f9 100644 (file)
@@ -275,7 +275,7 @@ jsonb_hash(PG_FUNCTION_ARGS)
    it = JsonbIteratorInit(VARDATA(jb));
    INIT_CRC32(crc);
 
-   while ((r = JsonbIteratorGet(&it, &v, false)) != 0)
+   while ((r = JsonbIteratorNext(&it, &v, false)) != 0)
    {
        switch (r)
        {
@@ -337,8 +337,8 @@ deepContains(JsonbIterator ** it1, JsonbIterator ** it2)
                v2;
    bool        res = true;
 
-   r1 = JsonbIteratorGet(it1, &v1, false);
-   r2 = JsonbIteratorGet(it2, &v2, false);
+   r1 = JsonbIteratorNext(it1, &v1, false);
+   r2 = JsonbIteratorNext(it2, &v2, false);
 
    if (r1 != r2)
    {
@@ -351,7 +351,7 @@ deepContains(JsonbIterator ** it1, JsonbIterator ** it2)
 
        for (;;)
        {
-           r2 = JsonbIteratorGet(it2, &v2, false);
+           r2 = JsonbIteratorNext(it2, &v2, false);
            if (r2 == WJB_END_OBJECT)
                break;
 
@@ -367,7 +367,7 @@ deepContains(JsonbIterator ** it1, JsonbIterator ** it2)
                break;
            }
 
-           r2 = JsonbIteratorGet(it2, &v2, true);
+           r2 = JsonbIteratorNext(it2, &v2, true);
            Assert(r2 == WJB_VALUE);
 
            if (v->type != v2.type)
@@ -408,7 +408,7 @@ deepContains(JsonbIterator ** it1, JsonbIterator ** it2)
 
        for (;;)
        {
-           r2 = JsonbIteratorGet(it2, &v2, true);
+           r2 = JsonbIteratorNext(it2, &v2, true);
            if (r2 == WJB_END_ARRAY)
                break;
 
@@ -438,7 +438,7 @@ deepContains(JsonbIterator ** it1, JsonbIterator ** it2)
 
                    for (i = 0; i < nelems; i++)
                    {
-                       r2 = JsonbIteratorGet(it1, &v1, true);
+                       r2 = JsonbIteratorNext(it1, &v1, true);
                        Assert(r2 == WJB_ELEM);
 
                        if (v1.type == jbvBinary)
index cd12c70a74639e1e8d2509888a2fd54afd03ba3d..2491bddfa57e25dc25c50f9fe92802d8a521d1c1 100644 (file)
@@ -285,8 +285,8 @@ compareJsonbBinaryValue(char *a, char *b)
        int         r1,
                    r2;
 
-       r1 = JsonbIteratorGet(&it1, &v1, false);
-       r2 = JsonbIteratorGet(&it2, &v2, false);
+       r1 = JsonbIteratorNext(&it1, &v1, false);
+       r2 = JsonbIteratorNext(&it2, &v2, false);
 
        if (r1 == r2)
        {
@@ -707,8 +707,23 @@ pushJsonbValue(ToJsonbState ** state, int r, JsonbValue * v)
    return h;
 }
 
+/*
+ * Given an unparsed varlena buffer, expand to JsonbIterator.
+ */
+JsonbIterator *
+JsonbIteratorInit(char *buffer)
+{
+   JsonbIterator *it = palloc(sizeof(*it));
+
+   parseBuffer(it, buffer);
+   it->next = NULL;
+
+   return it;
+}
+
+/* Get next JsonbValue while iterating */
 int
-JsonbIteratorGet(JsonbIterator ** it, JsonbValue * v, bool skipNested)
+JsonbIteratorNext(JsonbIterator ** it, JsonbValue * v, bool skipNested)
 {
    int         res;
 
@@ -740,7 +755,7 @@ JsonbIteratorGet(JsonbIterator ** it, JsonbValue * v, bool skipNested)
            }
            else if (formAnswer(it, v, &(*it)->array[(*it)->i++], skipNested))
            {
-               res = JsonbIteratorGet(it, v, skipNested);
+               res = JsonbIteratorNext(it, v, skipNested);
            }
            else
            {
@@ -770,7 +785,7 @@ JsonbIteratorGet(JsonbIterator ** it, JsonbValue * v, bool skipNested)
        case JB_FLAG_OBJECT | jbi_value:
            (*it)->state = jbi_key;
            if (formAnswer(it, v, &(*it)->array[((*it)->i++) * 2 + 1], skipNested))
-               res = JsonbIteratorGet(it, v, skipNested);
+               res = JsonbIteratorNext(it, v, skipNested);
            else
                res = WJB_VALUE;
            break;
@@ -781,17 +796,6 @@ JsonbIteratorGet(JsonbIterator ** it, JsonbValue * v, bool skipNested)
    return res;
 }
 
-JsonbIterator *
-JsonbIteratorInit(char *buffer)
-{
-   JsonbIterator *it = palloc(sizeof(*it));
-
-   parseBuffer(it, buffer);
-   it->next = NULL;
-
-   return it;
-}
-
 /****************************************************************************
  * Walk the tree representation of jsonb                   *
  ****************************************************************************/
index dd4230d6344c179e0f8e6cf4dc19a33bde089d78..ac38ad2c633d4b2d6e44609d1a7de54dd7d4e6f9 100644 (file)
@@ -282,7 +282,7 @@ jsonb_object_keys(PG_FUNCTION_ARGS)
 
        it = JsonbIteratorInit(VARDATA_ANY(jb));
 
-       while ((r = JsonbIteratorGet(&it, &v, skipNested)) != 0)
+       while ((r = JsonbIteratorNext(&it, &v, skipNested)) != 0)
        {
            skipNested = true;
 
@@ -480,7 +480,7 @@ jsonb_object_field(PG_FUNCTION_ARGS)
 
    it = JsonbIteratorInit(VARDATA_ANY(jb));
 
-   while ((r = JsonbIteratorGet(&it, &v, skipNested)) != 0)
+   while ((r = JsonbIteratorNext(&it, &v, skipNested)) != 0)
    {
        skipNested = true;
 
@@ -492,7 +492,7 @@ jsonb_object_field(PG_FUNCTION_ARGS)
                 * The next thing the iterator fetches should be the value, no
                 * matter what shape it is.
                 */
-               r = JsonbIteratorGet(&it, &v, skipNested);
+               r = JsonbIteratorNext(&it, &v, skipNested);
                PG_RETURN_JSONB(JsonbValueToJsonb(&v));
            }
        }
@@ -541,7 +541,7 @@ jsonb_object_field_text(PG_FUNCTION_ARGS)
 
    it = JsonbIteratorInit(VARDATA_ANY(jb));
 
-   while ((r = JsonbIteratorGet(&it, &v, skipNested)) != 0)
+   while ((r = JsonbIteratorNext(&it, &v, skipNested)) != 0)
    {
        skipNested = true;
 
@@ -555,7 +555,7 @@ jsonb_object_field_text(PG_FUNCTION_ARGS)
                 * The next thing the iterator fetches should be the value, no
                 * matter what shape it is.
                 */
-               r = JsonbIteratorGet(&it, &v, skipNested);
+               r = JsonbIteratorNext(&it, &v, skipNested);
 
                /*
                 * if it's a scalar string it needs to be de-escaped,
@@ -624,7 +624,7 @@ jsonb_array_element(PG_FUNCTION_ARGS)
 
    it = JsonbIteratorInit(VARDATA_ANY(jb));
 
-   while ((r = JsonbIteratorGet(&it, &v, skipNested)) != 0)
+   while ((r = JsonbIteratorNext(&it, &v, skipNested)) != 0)
    {
        skipNested = true;
 
@@ -678,7 +678,7 @@ jsonb_array_element_text(PG_FUNCTION_ARGS)
 
    it = JsonbIteratorInit(VARDATA_ANY(jb));
 
-   while ((r = JsonbIteratorGet(&it, &v, skipNested)) != 0)
+   while ((r = JsonbIteratorNext(&it, &v, skipNested)) != 0)
    {
        skipNested = true;
 
@@ -1223,7 +1223,7 @@ get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text)
            JsonbIterator *it = JsonbIteratorInit(jbvp->binary.data);
            int         r;
 
-           r = JsonbIteratorGet(&it, &tv, true);
+           r = JsonbIteratorNext(&it, &tv, true);
            jbvp = (JsonbValue *) jbvp->binary.data;
            have_object = r == WJB_BEGIN_OBJECT;
            have_array = r == WJB_BEGIN_ARRAY;
@@ -1438,7 +1438,7 @@ each_worker_jsonb(FunctionCallInfo fcinfo, bool as_text)
 
    it = JsonbIteratorInit(VARDATA_ANY(jb));
 
-   while ((r = JsonbIteratorGet(&it, &v, skipNested)) != 0)
+   while ((r = JsonbIteratorNext(&it, &v, skipNested)) != 0)
    {
        skipNested = true;
 
@@ -1458,7 +1458,7 @@ each_worker_jsonb(FunctionCallInfo fcinfo, bool as_text)
             * The next thing the iterator fetches should be the value, no
             * matter what shape it is.
             */
-           r = JsonbIteratorGet(&it, &v, skipNested);
+           r = JsonbIteratorNext(&it, &v, skipNested);
 
            values[0] = PointerGetDatum(key);
 
@@ -1763,7 +1763,7 @@ elements_worker_jsonb(FunctionCallInfo fcinfo, bool as_text)
 
    it = JsonbIteratorInit(VARDATA_ANY(jb));
 
-   while ((r = JsonbIteratorGet(&it, &v, skipNested)) != 0)
+   while ((r = JsonbIteratorNext(&it, &v, skipNested)) != 0)
    {
        skipNested = true;
 
@@ -2767,7 +2767,7 @@ populate_recordset_worker(FunctionCallInfo fcinfo, bool have_record_arg)
 
        it = JsonbIteratorInit(VARDATA_ANY(jb));
 
-       while ((r = JsonbIteratorGet(&it, &v, skipNested)) != 0)
+       while ((r = JsonbIteratorNext(&it, &v, skipNested)) != 0)
        {
            skipNested = true;
 
index 70ce22afdc0cb6d8a847118e43192bc009a99379..f45ab30f0279eebc103e81ef59bb597a2c8d8d92 100644 (file)
@@ -98,13 +98,16 @@ struct JsonbValue
 {
    enum
    {
+       /* Scalar types */
        jbvNull,
        jbvString,
        jbvNumeric,
        jbvBool,
+       /* Composite types */
        jbvArray,
        jbvObject,
-       jbvBinary               /* Binary form of jbvArray/jbvObject */
+       /* Binary form of jbvArray/jbvObject */
+       jbvBinary
    }           type;
 
    uint32      size;           /* Estimation size of node (including
@@ -157,6 +160,10 @@ typedef struct ToJsonbState
    struct ToJsonbState *next;
 } ToJsonbState;
 
+/*
+ * JsonbIterator holds details of the type for each iteration. It also stores
+ * an unoriginal unparsed varlena buffer.
+ */
 typedef struct JsonbIterator
 {
    uint32      type;
@@ -236,7 +243,7 @@ extern JsonbValue *findUncompressedJsonbValue(char *buffer, uint32 flags,
 extern JsonbValue *getJsonbValue(char *buffer, uint32 flags, int32 i);
 extern JsonbValue *pushJsonbValue(ToJsonbState ** state, int r, JsonbValue *v);
 extern JsonbIterator *JsonbIteratorInit(char *buffer);
-extern int JsonbIteratorGet(JsonbIterator **it, JsonbValue *v, bool skipNested);
+extern int JsonbIteratorNext(JsonbIterator **it, JsonbValue *v, bool skipNested);
 extern Jsonb *JsonbValueToJsonb(JsonbValue *v);
 
 /* jsonb.c support function */