+static void zrankCommand(redisClient *c) {
+ zrankGenericCommand(c, 0);
+}
+
+static void zrevrankCommand(redisClient *c) {
+ zrankGenericCommand(c, 1);
+}
+
+/* =================================== Hashes =============================== */
+static void hsetCommand(redisClient *c) {
+ int update = 0;
+ robj *o = lookupKeyWrite(c->db,c->argv[1]);
+
+ if (o == NULL) {
+ o = createHashObject();
+ dictAdd(c->db->dict,c->argv[1],o);
+ incrRefCount(c->argv[1]);
+ } else {
+ if (o->type != REDIS_HASH) {
+ addReply(c,shared.wrongtypeerr);
+ return;
+ }
+ }
+ /* We want to convert the zipmap into an hash table right now if the
+ * entry to be added is too big. Note that we check if the object
+ * is integer encoded before to try fetching the length in the test below.
+ * This is because integers are small, but currently stringObjectLen()
+ * performs a slow conversion: not worth it. */
+ if (o->encoding == REDIS_ENCODING_ZIPMAP &&
+ ((c->argv[2]->encoding == REDIS_ENCODING_RAW &&
+ sdslen(c->argv[2]->ptr) > server.hash_max_zipmap_value) ||
+ (c->argv[3]->encoding == REDIS_ENCODING_RAW &&
+ sdslen(c->argv[3]->ptr) > server.hash_max_zipmap_value)))
+ {
+ convertToRealHash(o);
+ }
+
+ if (o->encoding == REDIS_ENCODING_ZIPMAP) {
+ unsigned char *zm = o->ptr;
+ robj *valobj = getDecodedObject(c->argv[3]);
+
+ zm = zipmapSet(zm,c->argv[2]->ptr,sdslen(c->argv[2]->ptr),
+ valobj->ptr,sdslen(valobj->ptr),&update);
+ decrRefCount(valobj);
+ o->ptr = zm;
+
+ /* And here there is the second check for hash conversion...
+ * we want to do it only if the operation was not just an update as
+ * zipmapLen() is O(N). */
+ if (!update && zipmapLen(zm) > server.hash_max_zipmap_entries)
+ convertToRealHash(o);
+ } else {
+ tryObjectEncoding(c->argv[2]);
+ /* note that c->argv[3] is already encoded, as the latest arg
+ * of a bulk command is always integer encoded if possible. */
+ if (dictAdd(o->ptr,c->argv[2],c->argv[3]) == DICT_OK) {
+ incrRefCount(c->argv[2]);
+ } else {
+ update = 1;
+ }
+ incrRefCount(c->argv[3]);
+ }
+ server.dirty++;
+ addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",update == 0));
+}
+
+static void hgetCommand(redisClient *c) {
+ robj *o = lookupKeyRead(c->db,c->argv[1]);
+
+ if (o == NULL) {
+ addReply(c,shared.nullbulk);
+ return;
+ } else {
+ if (o->type != REDIS_HASH) {
+ addReply(c,shared.wrongtypeerr);
+ return;
+ }
+
+ if (o->encoding == REDIS_ENCODING_ZIPMAP) {
+ unsigned char *zm = o->ptr;
+ unsigned char *val;
+ unsigned int vlen;
+
+ if (zipmapGet(zm,c->argv[2]->ptr,sdslen(c->argv[2]->ptr), &val,&vlen)) {
+ addReplySds(c,sdscatprintf(sdsempty(),"$%u\r\n", vlen));
+ addReplySds(c,sdsnewlen(val,vlen));
+ addReply(c,shared.crlf);
+ return;
+ } else {
+ addReply(c,shared.nullbulk);
+ return;
+ }
+ } else {
+ struct dictEntry *de;
+
+ de = dictFind(o->ptr,c->argv[2]);
+ if (de == NULL) {
+ addReply(c,shared.nullbulk);
+ } else {
+ robj *e = dictGetEntryVal(de);
+
+ addReplyBulkLen(c,e);
+ addReply(c,e);
+ addReply(c,shared.crlf);
+ }
+ }
+ }
+}
+
+static void hdelCommand(redisClient *c) {
+ robj *o = lookupKeyRead(c->db,c->argv[1]);
+
+ if (o == NULL) {
+ addReply(c,shared.czero);
+ return;
+ } else {
+ int deleted = 0;
+
+ if (o->type != REDIS_HASH) {
+ addReply(c,shared.wrongtypeerr);
+ return;
+ }
+
+ if (o->encoding == REDIS_ENCODING_ZIPMAP) {
+ o->ptr = zipmapDel((unsigned char*) o->ptr,
+ (unsigned char*) c->argv[2]->ptr,
+ sdslen(c->argv[2]->ptr), &deleted);
+ } else {
+ deleted = dictDelete((dict*)o->ptr,c->argv[2]) == DICT_OK;
+ }
+ addReply(c,deleted ? shared.cone : shared.czero);
+ }
+}
+
+static void convertToRealHash(robj *o) {
+ unsigned char *key, *val, *p, *zm = o->ptr;
+ unsigned int klen, vlen;
+ dict *dict = dictCreate(&hashDictType,NULL);
+
+ assert(o->type == REDIS_HASH && o->encoding != REDIS_ENCODING_HT);
+ p = zipmapRewind(zm);
+ while((p = zipmapNext(p,&key,&klen,&val,&vlen)) != NULL) {
+ robj *keyobj, *valobj;
+
+ keyobj = createStringObject((char*)key,klen);
+ valobj = createStringObject((char*)val,vlen);
+ tryObjectEncoding(keyobj);
+ tryObjectEncoding(valobj);
+ dictAdd(dict,keyobj,valobj);
+ }
+ o->encoding = REDIS_ENCODING_HT;
+ o->ptr = dict;
+ zfree(zm);
+}
+