Consistently use new 'object' nomenclature for associative structure
authorPeter Geoghegan <[email protected]>
Fri, 7 Mar 2014 08:51:48 +0000 (00:51 -0800)
committerPeter Geoghegan <[email protected]>
Fri, 7 Mar 2014 08:51:48 +0000 (00:51 -0800)
src/backend/utils/adt/jsonb.c
src/backend/utils/adt/jsonb_gin.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 3a937d9058373ace36771668af30f1f6db31b65b..c2f9a68d09ea89c4228e7712119ff7e2ca6b951b 100644 (file)
@@ -210,7 +210,7 @@ jsonb_in_scalar(void *state, char *token, JsonTokenType tokentype)
            case jbvArray:
                _state->res = pushJsonbValue(&_state->state, WJB_ELEM, &v);
                break;
-           case jbvHash:
+           case jbvObject:
                _state->res = pushJsonbValue(&_state->state, WJB_VALUE, &v);
                break;
            default:
index 3f92cf060e5fa52b7a27a265ed2cfb18c0d88508..08662636ec8de18f005ce5cfbbcb1406ac75e07d 100644 (file)
@@ -318,7 +318,7 @@ hash_value(JsonbValue *v, PathHashStack *stack)
            COMP_CRC32(stack->hash_state, v->string.val, v->string.len);
            break;
        default:
-           elog(ERROR, "wrong jsonb scalar type");
+           elog(ERROR, "invalid jsonb scalar type");
            break;
    }
 }
index 50b9f42244b80e091132f788e9aee5b7f26e2b9e..50fc834d14ea5163181307686e86ec4d0b64edc8 100644 (file)
@@ -56,7 +56,7 @@ arrayToJsonbSortedArray(ArrayType *a)
    v = palloc(sizeof(*v));
    v->type = jbvArray;
    v->array.scalar = false;
-   v->array.elems = palloc(sizeof(*v->hash.pairs) * key_count);
+   v->array.elems = palloc(sizeof(*v->object.pairs) * key_count);
 
    for (i = 0, j = 0; i < key_count; i++)
    {
@@ -124,7 +124,7 @@ jsonb_exists_any(PG_FUNCTION_ARGS)
    uint32          *plowbound = NULL, lowbound = 0;
    bool            res = false;
 
-   if (JB_ISEMPTY(jb) || v == NULL || v->hash.npairs == 0)
+   if (JB_ISEMPTY(jb) || v == NULL || v->object.npairs == 0)
        PG_RETURN_BOOL(false);
 
    if (JB_ROOT_IS_OBJECT(jb))
@@ -158,8 +158,13 @@ jsonb_exists_all(PG_FUNCTION_ARGS)
    uint32          *plowbound = NULL, lowbound = 0;
    bool            res = false;
 
-   if (JB_ISEMPTY(js) || v == NULL || v->hash.npairs == 0)
-       PG_RETURN_BOOL(false);
+   if (JB_ISEMPTY(js) || v == NULL || v->array.nelems == 0)
+   {
+       if (v == NULL || v->array.nelems == 0)
+           PG_RETURN_BOOL(true); /* compatibility */
+       else
+           PG_RETURN_BOOL(false);
+   }
 
    if (JB_ROOT_IS_OBJECT(js))
        plowbound = &lowbound;
@@ -172,8 +177,10 @@ jsonb_exists_all(PG_FUNCTION_ARGS)
     */
    for (i = 0; i < v->array.nelems; i++)
    {
-       if (findUncompressedJsonbValueByValue(VARDATA(js), JB_FLAG_OBJECT | JB_FLAG_ARRAY, plowbound,
-                                              v->array.elems + i) != NULL)
+       if (findUncompressedJsonbValueByValue(VARDATA(js),
+                                             JB_FLAG_OBJECT | JB_FLAG_ARRAY,
+                                             plowbound,
+                                             v->array.elems + i) != NULL)
        {
            res = true;
            break;
@@ -483,7 +490,7 @@ jsonb_hash(PG_FUNCTION_ARGS)
                break;
            case WJB_BEGIN_OBJECT:
                COMP_CRC32(crc, "hb", 3);
-               COMP_CRC32(crc, &v.hash.npairs, sizeof(v.hash.npairs));
+               COMP_CRC32(crc, &v.object.npairs, sizeof(v.object.npairs));
                break;
            case WJB_KEY:
                COMP_CRC32(crc, "k", 2);
index 0ac42bde5c6c34da504b035ed3a24cc46b6568f3..f55c4da64bbd596bc36e4aa49321f0034ce19813 100644 (file)
@@ -82,7 +82,7 @@ JsonbValueToJsonb(JsonbValue *v)
        Assert(sz <= res->size);
        SET_VARSIZE(out, sz + VARHDRSZ);
    }
-   else if (v->type == jbvHash || v->type == jbvArray)
+   else if (v->type == jbvObject || v->type == jbvArray)
    {
        uint32      sz;
 
@@ -203,27 +203,27 @@ compareJsonbValue(JsonbValue *a, JsonbValue *b)
                }
 
                return (a->array.nelems > b->array.nelems) ? 1 : -1;
-           case jbvHash:
-               if (a->hash.npairs == b->hash.npairs)
+           case jbvObject:
+               if (a->object.npairs == b->object.npairs)
                {
                    int         i,
                                r;
 
-                   for (i = 0; i < a->hash.npairs; i++)
+                   for (i = 0; i < a->object.npairs; i++)
                    {
-                       if ((r = compareJsonbStringValue(&a->hash.pairs[i].key,
-                                                      &b->hash.pairs[i].key,
+                       if ((r = compareJsonbStringValue(&a->object.pairs[i].key,
+                                                      &b->object.pairs[i].key,
                                                         NULL)) != 0)
                            return r;
-                       if ((r = compareJsonbValue(&a->hash.pairs[i].value,
-                                             &b->hash.pairs[i].value)) != 0)
+                       if ((r = compareJsonbValue(&a->object.pairs[i].value,
+                                             &b->object.pairs[i].value)) != 0)
                            return r;
                    }
 
                    return 0;
                }
 
-               return (a->hash.npairs > b->hash.npairs) ? 1 : -1;
+               return (a->object.npairs > b->object.npairs) ? 1 : -1;
            case jbvBinary:
                return compareJsonbBinaryValue(a->binary.data, b->binary.data);
            default:
@@ -284,9 +284,9 @@ compareJsonbBinaryValue(char *a, char *b)
                        if (v1.array.nelems != v2.array.nelems)
                            res = (v1.array.nelems > v2.array.nelems) ? 1 : -1;
                        break;
-                   case jbvHash:
-                       if (v1.hash.npairs != v2.hash.npairs)
-                           res = (v1.hash.npairs > v2.hash.npairs) ? 1 : -1;
+                   case jbvObject:
+                       if (v1.object.npairs != v2.object.npairs)
+                           res = (v1.object.npairs > v2.object.npairs) ? 1 : -1;
                        break;
                    default:
                        break;
@@ -307,7 +307,7 @@ compareJsonbBinaryValue(char *a, char *b)
 }
 
 /****************************************************************************
- *         find string key in hash or element by value in array            *
+ * find string key in object or element by value in array          *
  ****************************************************************************/
 JsonbValue *
 findUncompressedJsonbValueByValue(char *buffer, uint32 flags,
@@ -495,9 +495,8 @@ findUncompressedJsonbValue(char *buffer, uint32 flags, uint32 *lowbound,
 }
 
 /*
- * Get i-th value of array or hash. if i < 0 then it counts from
- * the end of array/hash. Note: returns pointer to statically
- * allocated JsonbValue.
+ * Get i-th value of array or object.  If i < 0, then it counts from the end of
+ * array/object. Note: returns pointer to statically allocated JsonbValue.
  */
 JsonbValue *
 getJsonbValue(char *buffer, uint32 flags, int32 i)
@@ -603,21 +602,21 @@ walkUncompressedJsonbDo(JsonbValue *v, walk_jsonb_cb cb, void *cb_arg, uint32 le
            }
            cb(cb_arg, v, WJB_END_ARRAY, level);
            break;
-       case jbvHash:
+       case jbvObject:
            cb(cb_arg, v, WJB_BEGIN_OBJECT, level);
 
-           for (i = 0; i < v->hash.npairs; i++)
+           for (i = 0; i < v->object.npairs; i++)
            {
-               cb(cb_arg, &v->hash.pairs[i].key, WJB_KEY, level);
-
-               if (v->hash.pairs[i].value.type == jbvNull ||
-                   v->hash.pairs[i].value.type == jbvString ||
-                   v->hash.pairs[i].value.type == jbvBool ||
-                   v->hash.pairs[i].value.type == jbvNumeric ||
-                   v->hash.pairs[i].value.type == jbvBinary)
-                   cb(cb_arg, &v->hash.pairs[i].value, WJB_VALUE, level);
+               cb(cb_arg, &v->object.pairs[i].key, WJB_KEY, level);
+
+               if (v->object.pairs[i].value.type == jbvNull ||
+                   v->object.pairs[i].value.type == jbvString ||
+                   v->object.pairs[i].value.type == jbvBool ||
+                   v->object.pairs[i].value.type == jbvNumeric ||
+                   v->object.pairs[i].value.type == jbvBinary)
+                   cb(cb_arg, &v->object.pairs[i].value, WJB_VALUE, level);
                else
-                   walkUncompressedJsonbDo(&v->hash.pairs[i].value, cb, cb_arg,
+                   walkUncompressedJsonbDo(&v->object.pairs[i].value, cb, cb_arg,
                                            level + 1);
            }
 
@@ -789,8 +788,8 @@ JsonbIteratorGet(JsonbIterator **it, JsonbValue *v, bool skipNested)
        case JB_FLAG_OBJECT | jbi_start:
            (*it)->state = jbi_key;
            (*it)->i = 0;
-           v->type = jbvHash;
-           v->hash.npairs = (*it)->nelems;
+           v->type = jbvObject;
+           v->object.npairs = (*it)->nelems;
            res = WJB_BEGIN_OBJECT;
            break;
        case JB_FLAG_OBJECT | jbi_key:
@@ -971,7 +970,7 @@ compressCallback(void *arg, JsonbValue *value, uint32 flags, uint32 level)
    if (flags & (WJB_BEGIN_ARRAY | WJB_BEGIN_OBJECT))
    {
        Assert(((flags & WJB_BEGIN_ARRAY) && value->type == jbvArray) ||
-              ((flags & WJB_BEGIN_OBJECT) && value->type == jbvHash));
+              ((flags & WJB_BEGIN_OBJECT) && value->type == jbvObject));
 
        curLevelState->begin = state->ptr;
 
@@ -1012,8 +1011,8 @@ compressCallback(void *arg, JsonbValue *value, uint32 flags, uint32 level)
        }
        else
        {
-           *curLevelState->header = value->hash.npairs | JB_FLAG_OBJECT;
-           state->ptr += sizeof(JEntry) * value->hash.npairs * 2;
+           *curLevelState->header = value->object.npairs | JB_FLAG_OBJECT;
+           state->ptr += sizeof(JEntry) * value->object.npairs * 2;
        }
    }
    else if (flags & WJB_ELEM)
@@ -1038,7 +1037,7 @@ compressCallback(void *arg, JsonbValue *value, uint32 flags, uint32 level)
                    i;
 
        Assert(((flags & WJB_END_ARRAY) && value->type == jbvArray) ||
-              ((flags & WJB_END_OBJECT) && value->type == jbvHash));
+              ((flags & WJB_END_OBJECT) && value->type == jbvObject));
        if (level == 0)
            return;
 
@@ -1138,17 +1137,17 @@ appendKey(ToJsonbState * state, JsonbValue *v)
 {
    JsonbValue *h = &state->v;
 
-   Assert(h->type == jbvHash);
+   Assert(h->type == jbvObject);
 
-   if (h->hash.npairs >= state->size)
+   if (h->object.npairs >= state->size)
    {
        state->size *= 2;
-       h->hash.pairs = repalloc(h->hash.pairs,
-                                sizeof(*h->hash.pairs) * state->size);
+       h->object.pairs = repalloc(h->object.pairs,
+                                sizeof(*h->object.pairs) * state->size);
    }
 
-   h->hash.pairs[h->hash.npairs].key = *v;
-   h->hash.pairs[h->hash.npairs].order = h->hash.npairs;
+   h->object.pairs[h->object.npairs].key = *v;
+   h->object.pairs[h->object.npairs].order = h->object.npairs;
 
    h->size += v->size;
 }
@@ -1158,33 +1157,33 @@ appendValue(ToJsonbState * state, JsonbValue *v)
 {
    JsonbValue *h = &state->v;
 
-   Assert(h->type == jbvHash);
+   Assert(h->type == jbvObject);
 
-   h->hash.pairs[h->hash.npairs++].value = *v;
+   h->object.pairs[h->object.npairs++].value = *v;
 
    h->size += v->size;
 }
 
 /*
- * Sort and unique pairs in hash-like JsonbValue
+ * Sort and unique pairs in JsonbValue (associative data structure)
  */
 static void
 uniqueJsonbValue(JsonbValue *v)
 {
    bool        hasNonUniq = false;
 
-   Assert(v->type == jbvHash);
+   Assert(v->type == jbvObject);
 
-   if (v->hash.npairs > 1)
-       qsort_arg(v->hash.pairs, v->hash.npairs, sizeof(*v->hash.pairs),
+   if (v->object.npairs > 1)
+       qsort_arg(v->object.pairs, v->object.npairs, sizeof(*v->object.pairs),
                  compareJsonbPair, &hasNonUniq);
 
    if (hasNonUniq)
    {
-       JsonbPair  *ptr = v->hash.pairs + 1,
-                  *res = v->hash.pairs;
+       JsonbPair  *ptr = v->object.pairs + 1,
+                  *res = v->object.pairs;
 
-       while (ptr - v->hash.pairs < v->hash.npairs)
+       while (ptr - v->object.pairs < v->object.npairs)
        {
            if (ptr->key.string.len == res->key.string.len &&
                memcmp(ptr->key.string.val, res->key.string.val,
@@ -1201,20 +1200,21 @@ uniqueJsonbValue(JsonbValue *v)
            ptr++;
        }
 
-       v->hash.npairs = res + 1 - v->hash.pairs;
+       v->object.npairs = res + 1 - v->object.pairs;
    }
 }
 
 /*
- * Pushes the value into state. With r = WJB_END_OBJECT and v = NULL
- * it will order and unique hash's keys otherwise we believe that
- * pushed keys was ordered and unique.
+ * Pushes the value into state.  With r = WJB_END_OBJECT and v = NULL it will
+ * order and unique hash's keys otherwise we believe that pushed keys was
+ * ordered and unique.
+ *
  * Initial state of ToJsonbState is NULL.
  */
 JsonbValue *
-pushJsonbValue(ToJsonbState ** state, int r /* WJB_* */ , JsonbValue *v)
+pushJsonbValue(ToJsonbState **state, int r /* WJB_* */, JsonbValue *v)
 {
-   JsonbValue *h = NULL;
+   JsonbValue      *h = NULL;
 
    switch (r)
    {
@@ -1233,12 +1233,12 @@ pushJsonbValue(ToJsonbState ** state, int r /* WJB_* */ , JsonbValue *v)
        case WJB_BEGIN_OBJECT:
            *state = pushState(state);
            h = &(*state)->v;
-           (*state)->v.type = jbvHash;
+           (*state)->v.type = jbvObject;
            (*state)->v.size = 3 * sizeof(JEntry);
-           (*state)->v.hash.npairs = 0;
-           (*state)->size = (v && v->type == jbvHash && v->hash.npairs > 0) ?
-               v->hash.npairs : 4;
-           (*state)->v.hash.pairs = palloc(sizeof(*(*state)->v.hash.pairs) *
+           (*state)->v.object.npairs = 0;
+           (*state)->size = (v && v->type == jbvObject && v->object.npairs > 0) ?
+               v->object.npairs : 4;
+           (*state)->v.object.pairs = palloc(sizeof(*(*state)->v.object.pairs) *
                                            (*state)->size);
            break;
        case WJB_ELEM:
@@ -1264,15 +1264,15 @@ pushJsonbValue(ToJsonbState ** state, int r /* WJB_* */ , JsonbValue *v)
                uniqueJsonbValue(h);
 
            /*
-            * no break here - end of hash requres some extra work but rest is
-            * the same as for array
+            * No break here - end of "object" associative data structure
+            * requires some extra work but rest is the same as for array
             */
        case WJB_END_ARRAY:
            h = &(*state)->v;
 
            /*
-            * pop stack and push current array/hash as value in parent
-            * array/hash
+            * Pop stack and push current array/"object" as value in parent
+            * array/"object"
             */
            *state = (*state)->next;
            if (*state)
@@ -1282,16 +1282,16 @@ pushJsonbValue(ToJsonbState ** state, int r /* WJB_* */ , JsonbValue *v)
                    case jbvArray:
                        appendArray(*state, h);
                        break;
-                   case jbvHash:
+                   case jbvObject:
                        appendValue(*state, h);
                        break;
                    default:
-                       elog(ERROR, "unknown type of jsonb container");
+                       elog(ERROR, "invalid jsonb container type");
                }
            }
            break;
        default:
-           elog(ERROR, "wrong type of jsonb value");
+           elog(ERROR, "invalid jsonb container type");
    }
 
    return h;
index b62dda9cd16899d069bbbc3fb4fbb535fb5cd17e..74d20db1815cccbdc10b4095e91446be362d4b7f 100644 (file)
@@ -1230,7 +1230,7 @@ get_jsonb_path_all(FunctionCallInfo fcinfo, bool as_text)
        }
        else
        {
-           have_object = jbvp->type == jbvHash;
+           have_object = jbvp->type == jbvObject;
            have_array = jbvp->type == jbvArray;
        }
    }
index 4880b1e3f377b61ad5ae330e1bbc62e173efad07..f4a50890e02fac29a7cc627928663a07f3f1bc47 100644 (file)
@@ -101,8 +101,8 @@ struct JsonbValue
        jbvNumeric,
        jbvBool,
        jbvArray,
-       jbvHash,
-       jbvBinary               /* Binary form of jbvArray/jbvHash */
+       jbvObject,
+       jbvBinary               /* Binary form of jbvArray/jbvObject */
    }           type;
 
    uint32      size;           /* Estimation size of node (including
@@ -130,7 +130,7 @@ struct JsonbValue
        {
            int         npairs;
            JsonbPair  *pairs;
-       }           hash;
+       }           object;     /* Associative data structure */
 
        struct
        {