diff --git a/src/db.c b/src/db.c index bb858d4fce2..ad6cd107041 100644 --- a/src/db.c +++ b/src/db.c @@ -89,36 +89,6 @@ void updateKeysizesHist(redisDb *db, int didx, uint32_t type, uint64_t oldLen, u } } -/* Remove key statistic from keysizes histogram */ -void removeKeysizesHist(redisDb *db, int didx, robj *val) { - uint64_t len; - switch (val->type) - { - case OBJ_STRING: len = stringObjectLen(val); break; - case OBJ_LIST: len = listTypeLength(val); break; - case OBJ_HASH: len = hashTypeLength(val, 0); break; - case OBJ_SET: len = setTypeSize(val); break; - case OBJ_ZSET: len = zsetLength(val); break; - default: return; - } - updateKeysizesHist(db, didx, val->type, len, 0); -} - -/* Add key statistic to keysizes histogram */ -void addKeysizesHist(redisDb *db, int didx, robj *val) { - uint64_t len; - switch (val->type) - { - case OBJ_STRING: len = stringObjectLen(val); break; - case OBJ_LIST: len = listTypeLength(val); break; - case OBJ_HASH: len = hashTypeLength(val, 0); break; - case OBJ_SET: len = setTypeSize(val); break; - case OBJ_ZSET: len = zsetLength(val); break; - default: return; - } - updateKeysizesHist(db, didx, val->type, 0, len); -} - /* Lookup a key for read or write operations, or return NULL if the key is not * found in the specified DB. This function implements the functionality of * lookupKeyRead(), lookupKeyWrite() and their ...WithFlags() variants. @@ -278,7 +248,7 @@ static dictEntry *dbAddInternal(redisDb *db, robj *key, robj *val, int update_if kvstoreDictSetVal(db->keys, slot, de, val); signalKeyAsReady(db, key, val->type); notifyKeyspaceEvent(NOTIFY_NEW,"new",key,db->id); - addKeysizesHist(db, slot, val); + updateKeysizesHist(db, slot, val->type, 0, getObjectLength(val)); /* add hist */ return de; } @@ -324,7 +294,7 @@ int dbAddRDBLoad(redisDb *db, sds key, robj *val) { int slot = getKeySlot(key); dictEntry *de = kvstoreDictAddRaw(db->keys, slot, key, NULL); if (de == NULL) return 0; - addKeysizesHist(db, slot, val); + updateKeysizesHist(db, slot, val->type, 0, getObjectLength(val)); /* add hist */ initObjectLRUOrLFU(val); kvstoreDictSetVal(db->keys, slot, de, val); return 1; @@ -349,7 +319,7 @@ static void dbSetValue(redisDb *db, robj *key, robj *val, int overwrite, dictEnt robj *old = dictGetVal(de); /* Remove old key from keysizes histogram */ - removeKeysizesHist(db, slot, old); + updateKeysizesHist(db, slot, old->type, getObjectLength(old), 0); /* remove hist */ val->lru = old->lru; @@ -370,7 +340,7 @@ static void dbSetValue(redisDb *db, robj *key, robj *val, int overwrite, dictEnt kvstoreDictSetVal(db->keys, slot, de, val); /* Add new key to keysizes histogram */ - addKeysizesHist(db, slot, val); + updateKeysizesHist(db, slot, val->type, 0, getObjectLength(val)); /* if hash with HFEs, take care to remove from global HFE DS */ if (old->type == OBJ_HASH) @@ -486,7 +456,8 @@ int dbGenericDelete(redisDb *db, robj *key, int async, int flags) { if (de) { robj *val = dictGetVal(de); - removeKeysizesHist(db, slot, val); + /* remove key from histogram */ + updateKeysizesHist(db, slot, val->type, getObjectLength(val), 0); /* If hash object with expiry on fields, remove it from HFE DS of DB */ if (val->type == OBJ_HASH) diff --git a/src/module.c b/src/module.c index b686df13c20..e58f59b2193 100644 --- a/src/module.c +++ b/src/module.c @@ -4171,15 +4171,7 @@ int RM_KeyType(RedisModuleKey *key) { * If the key pointer is NULL or the key is empty, zero is returned. */ size_t RM_ValueLength(RedisModuleKey *key) { if (key == NULL || key->value == NULL) return 0; - switch(key->value->type) { - case OBJ_STRING: return stringObjectLen(key->value); - case OBJ_LIST: return listTypeLength(key->value); - case OBJ_SET: return setTypeSize(key->value); - case OBJ_ZSET: return zsetLength(key->value); - case OBJ_HASH: return hashTypeLength(key->value, 0); /* OPEN: To subtract expired fields? */ - case OBJ_STREAM: return streamLength(key->value); - default: return 0; - } + return getObjectLength(key->value); } /* If the key is open for writing, remove it, and setup the key to diff --git a/src/object.c b/src/object.c index 2b42e7b3e63..d065359fa7f 100644 --- a/src/object.c +++ b/src/object.c @@ -680,6 +680,18 @@ robj *tryObjectEncoding(robj *o) { return tryObjectEncodingEx(o, 1); } +size_t getObjectLength(robj *o) { + switch(o->type) { + case OBJ_STRING: return stringObjectLen(o); + case OBJ_LIST: return listTypeLength(o); + case OBJ_SET: return setTypeSize(o); + case OBJ_ZSET: return zsetLength(o); + case OBJ_HASH: return hashTypeLength(o, 0); + case OBJ_STREAM: return streamLength(o); + default: return 0; + } +} + /* Get a decoded version of an encoded object (returned as a new object). * If the object is already raw-encoded just increment the ref count. */ robj *getDecodedObject(robj *o) { diff --git a/src/server.h b/src/server.h index e0c852dae2a..1b32dc65f10 100644 --- a/src/server.h +++ b/src/server.h @@ -2796,6 +2796,7 @@ int isSdsRepresentableAsLongLong(sds s, long long *llval); int isObjectRepresentableAsLongLong(robj *o, long long *llongval); robj *tryObjectEncoding(robj *o); robj *tryObjectEncodingEx(robj *o, int try_trim); +size_t getObjectLength(robj *o); robj *getDecodedObject(robj *o); size_t stringObjectLen(robj *o); robj *createStringObjectFromLongLong(long long value); @@ -3361,8 +3362,6 @@ int setModuleNumericConfig(ModuleConfig *config, long long val, const char **err /* db.c -- Keyspace access API */ void updateKeysizesHist(redisDb *db, int didx, uint32_t type, uint64_t oldLen, uint64_t newLen); -void removeKeysizesHist(redisDb *db, int didx, robj *val); -void addKeysizesHist(redisDb *db, int didx, robj *val); int removeExpire(redisDb *db, robj *key); void deleteExpiredKeyAndPropagate(redisDb *db, robj *keyobj); void propagateDeletion(redisDb *db, robj *key, int lazy);