summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2015-02-27 15:22:49 +0100
committerantirez <antirez@gmail.com>2015-02-27 15:22:49 +0100
commitd8f8b0575f489bba28cd2b03380bdbbbc48b6f66 (patch)
tree6b2c7ec5c9340cd0af8014d01d8c1a356b559a64
parentc95507881acb0d8cdaf7e0a29f445ee2fdaa2c80 (diff)
downloadredis-d8f8b0575f489bba28cd2b03380bdbbbc48b6f66.tar.gz
Hash: API to get value string len by field name.
-rw-r--r--src/t_hash.c24
1 files changed, 23 insertions, 1 deletions
diff --git a/src/t_hash.c b/src/t_hash.c
index 7f33bba0c..c228765db 100644
--- a/src/t_hash.c
+++ b/src/t_hash.c
@@ -130,7 +130,6 @@ robj *hashTypeGetObject(robj *o, robj *field) {
value = createStringObjectFromLongLong(vll);
}
}
-
} else if (o->encoding == REDIS_ENCODING_HT) {
robj *aux;
@@ -144,6 +143,29 @@ robj *hashTypeGetObject(robj *o, robj *field) {
return value;
}
+/* Higher level function using hashTypeGet*() to return the length of the
+ * object associated with the requested field, or 0 if the field does not
+ * exist. */
+size_t hashTypeGetValueLength(robj *o, robj *field) {
+ size_t len = 0;
+ if (o->encoding == REDIS_ENCODING_ZIPLIST) {
+ unsigned char *vstr = NULL;
+ unsigned int vlen = UINT_MAX;
+ long long vll = LLONG_MAX;
+
+ if (hashTypeGetFromZiplist(o, field, &vstr, &vlen, &vll) == 0)
+ len = vstr ? vlen : sdigits10(vll);
+ } else if (o->encoding == REDIS_ENCODING_HT) {
+ robj *aux;
+
+ if (hashTypeGetFromHashTable(o, field, &aux) == 0)
+ len = sdslen(aux->ptr);
+ } else {
+ redisPanic("Unknown hash encoding");
+ }
+ return len;
+}
+
/* Test if the specified field exists in the given hash. Returns 1 if the field
* exists, and 0 when it doesn't. */
int hashTypeExists(robj *o, robj *field) {