Skip to content

Commit

Permalink
modify zset part cmd with cache and fix count_ bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hahahashen committed Sep 20, 2024
1 parent 0263cd3 commit 33cd365
Show file tree
Hide file tree
Showing 18 changed files with 854 additions and 811 deletions.
6 changes: 3 additions & 3 deletions src/base_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ bool BaseCmd::IsNeedCacheDo(PClient* client) const {
}
} else if (HasFlag(kCmdFlagsZset)) {
int32_t db_len = 0;
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZCard(client->Key(), &db_len);
auto zset_cache_field_num_per_key=g_config.zset_cache_field_num_per_key.load();
if (!g_config.cache_zset.load() || db_len>zset_cache_field_num_per_key) {
PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->ZCard(client->Key(), &db_len);
auto zset_cache_field_num_per_key = g_config.zset_cache_field_num_per_key.load();
if (!g_config.cache_zset.load() || db_len > zset_cache_field_num_per_key) {
return false;
}
} else if (HasFlag(kCmdFlagsHash)) {
Expand Down
75 changes: 26 additions & 49 deletions src/cache/hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.


#include "pstd_defer.h"
#include "redisCache.h"

namespace cache {

Status RedisCache::HDel(std::string& key, std::vector<std::string> &fields) {
Status RedisCache::HDel(std::string &key, std::vector<std::string> &fields) {
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
robj **fields_obj = (robj **)zcallocate(sizeof(robj *) * fields.size());
for (unsigned int i = 0; i < fields.size(); ++i) {
Expand All @@ -31,7 +30,7 @@ Status RedisCache::HDel(std::string& key, std::vector<std::string> &fields) {
return Status::OK();
}

Status RedisCache::HSet(std::string& key, std::string &field, std::string &value) {
Status RedisCache::HSet(std::string &key, std::string &field, std::string &value) {
int res = RcFreeMemoryIfNeeded(cache_);
if (C_OK != res) {
return Status::Corruption("[error] Free memory faild !");
Expand All @@ -40,9 +39,7 @@ Status RedisCache::HSet(std::string& key, std::string &field, std::string &value
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
robj *fobj = createObject(OBJ_STRING, sdsnewlen(field.data(), field.size()));
robj *vobj = createObject(OBJ_STRING, sdsnewlen(value.data(), value.size()));
DEFER {
DecrObjectsRefCount(kobj, fobj, vobj);
};
DEFER { DecrObjectsRefCount(kobj, fobj, vobj); };
int ret = RcHSet(cache_, kobj, fobj, vobj);
if (C_OK != ret) {
return Status::Corruption("RcHSet failed");
Expand All @@ -51,25 +48,23 @@ Status RedisCache::HSet(std::string& key, std::string &field, std::string &value
return Status::OK();
}

Status RedisCache::HSetnx(std::string& key, std::string &field, std::string &value) {
Status RedisCache::HSetnx(std::string &key, std::string &field, std::string &value) {
if (C_OK != RcFreeMemoryIfNeeded(cache_)) {
return Status::Corruption("[error] Free memory faild !");
}

robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
robj *fobj = createObject(OBJ_STRING, sdsnewlen(field.data(), field.size()));
robj *vobj = createObject(OBJ_STRING, sdsnewlen(value.data(), value.size()));
DEFER {
DecrObjectsRefCount(kobj, fobj, vobj);
};
DEFER { DecrObjectsRefCount(kobj, fobj, vobj); };
if (C_OK != RcHSetnx(cache_, kobj, fobj, vobj)) {
return Status::Corruption("RcHSetnx failed");
}

return Status::OK();
}

Status RedisCache::HMSet(std::string& key, std::vector<storage::FieldValue> &fvs) {
Status RedisCache::HMSet(std::string &key, std::vector<storage::FieldValue> &fvs) {
int res = RcFreeMemoryIfNeeded(cache_);
if (C_OK != res) {
return Status::Corruption("[error] Free memory faild !");
Expand All @@ -93,13 +88,11 @@ Status RedisCache::HMSet(std::string& key, std::vector<storage::FieldValue> &fvs
return Status::OK();
}

Status RedisCache::HGet(std::string& key, std::string &field, std::string *value) {
Status RedisCache::HGet(std::string &key, std::string &field, std::string *value) {
sds val;
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
robj *fobj = createObject(OBJ_STRING, sdsnewlen(field.data(), field.size()));
DEFER {
DecrObjectsRefCount(kobj, fobj);
};
DEFER { DecrObjectsRefCount(kobj, fobj); };
int ret = RcHGet(cache_, kobj, fobj, &val);
if (C_OK != ret) {
if (REDIS_KEY_NOT_EXIST == ret) {
Expand All @@ -117,7 +110,7 @@ Status RedisCache::HGet(std::string& key, std::string &field, std::string *value
return Status::OK();
}

Status RedisCache::HMGet(std::string& key, std::vector<std::string> &fields, std::vector<storage::ValueStatus> *vss) {
Status RedisCache::HMGet(std::string &key, std::vector<std::string> &fields, std::vector<storage::ValueStatus> *vss) {
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
hitem *items = (hitem *)zcallocate(sizeof(hitem) * fields.size());
for (unsigned int i = 0; i < fields.size(); ++i) {
Expand Down Expand Up @@ -148,13 +141,11 @@ Status RedisCache::HMGet(std::string& key, std::vector<std::string> &fields, std
return Status::OK();
}

Status RedisCache::HGetall(std::string& key, std::vector<storage::FieldValue> *fvs) {
Status RedisCache::HGetall(std::string &key, std::vector<storage::FieldValue> *fvs) {
hitem *items = nullptr;
unsigned long items_size = 0;
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
DEFER {
DecrObjectsRefCount(kobj);
};
DEFER { DecrObjectsRefCount(kobj); };
int ret = RcHGetAll(cache_, kobj, &items, &items_size);
if (C_OK != ret) {
if (REDIS_KEY_NOT_EXIST == ret) {
Expand All @@ -174,13 +165,11 @@ Status RedisCache::HGetall(std::string& key, std::vector<storage::FieldValue> *f
return Status::OK();
}

Status RedisCache::HKeys(std::string& key, std::vector<std::string> *fields) {
Status RedisCache::HKeys(std::string &key, std::vector<std::string> *fields) {
hitem *items = nullptr;
unsigned long items_size = 0;
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
DEFER {
DecrObjectsRefCount(kobj);
};
DEFER { DecrObjectsRefCount(kobj); };
int ret = RcHKeys(cache_, kobj, &items, &items_size);
if (C_OK != ret) {
if (REDIS_KEY_NOT_EXIST == ret) {
Expand All @@ -197,13 +186,11 @@ Status RedisCache::HKeys(std::string& key, std::vector<std::string> *fields) {
return Status::OK();
}

Status RedisCache::HVals(std::string& key, std::vector<std::string> *values) {
Status RedisCache::HVals(std::string &key, std::vector<std::string> *values) {
hitem *items = nullptr;
unsigned long items_size = 0;
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
DEFER {
DecrObjectsRefCount(kobj);
};
DEFER { DecrObjectsRefCount(kobj); };
int ret = RcHVals(cache_, kobj, &items, &items_size);
if (C_OK != ret) {
if (REDIS_KEY_NOT_EXIST == ret) {
Expand All @@ -220,13 +207,11 @@ Status RedisCache::HVals(std::string& key, std::vector<std::string> *values) {
return Status::OK();
}

Status RedisCache::HExists(std::string& key, std::string &field) {
Status RedisCache::HExists(std::string &key, std::string &field) {
int is_exist = 0;
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
robj *fobj = createObject(OBJ_STRING, sdsnewlen(field.data(), field.size()));
DEFER {
DecrObjectsRefCount(kobj, fobj);
};
DEFER { DecrObjectsRefCount(kobj, fobj); };
int ret = RcHExists(cache_, kobj, fobj, &is_exist);
if (C_OK != ret) {
if (REDIS_KEY_NOT_EXIST == ret) {
Expand All @@ -238,14 +223,12 @@ Status RedisCache::HExists(std::string& key, std::string &field) {
return is_exist ? Status::OK() : Status::NotFound("field not exist");
}

Status RedisCache::HIncrby(std::string& key, std::string &field, int64_t value) {
Status RedisCache::HIncrby(std::string &key, std::string &field, int64_t value) {
int64_t result = 0;
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
robj *fobj = createObject(OBJ_STRING, sdsnewlen(field.data(), field.size()));
DEFER {
DecrObjectsRefCount(kobj, fobj);
};
int ret = RcHIncrby(cache_, kobj, fobj, value, (long long int*)&result);
DEFER { DecrObjectsRefCount(kobj, fobj); };
int ret = RcHIncrby(cache_, kobj, fobj, value, (long long int *)&result);
if (C_OK != ret) {
if (REDIS_KEY_NOT_EXIST == ret) {
return Status::NotFound("key not in cache");
Expand All @@ -256,13 +239,11 @@ Status RedisCache::HIncrby(std::string& key, std::string &field, int64_t value)
return Status::OK();
}

Status RedisCache::HIncrbyfloat(std::string& key, std::string &field, double value) {
Status RedisCache::HIncrbyfloat(std::string &key, std::string &field, double value) {
long double result = .0f;
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
robj *fobj = createObject(OBJ_STRING, sdsnewlen(field.data(), field.size()));
DEFER {
DecrObjectsRefCount(kobj, fobj);
};
DEFER { DecrObjectsRefCount(kobj, fobj); };
int ret = RcHIncrbyfloat(cache_, kobj, fobj, value, &result);
if (C_OK != ret) {
if (REDIS_KEY_NOT_EXIST == ret) {
Expand All @@ -274,11 +255,9 @@ Status RedisCache::HIncrbyfloat(std::string& key, std::string &field, double val
return Status::OK();
}

Status RedisCache::HLen(const std::string& key, uint64_t *len) {
Status RedisCache::HLen(const std::string &key, uint64_t *len) {
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
DEFER {
DecrObjectsRefCount(kobj);
};
DEFER { DecrObjectsRefCount(kobj); };
int ret = RcHlen(cache_, kobj, reinterpret_cast<unsigned long *>(len));
if (C_OK != ret) {
if (REDIS_KEY_NOT_EXIST == ret) {
Expand All @@ -290,12 +269,10 @@ Status RedisCache::HLen(const std::string& key, uint64_t *len) {
return Status::OK();
}

Status RedisCache::HStrlen(std::string& key, std::string &field, uint64_t *len) {
Status RedisCache::HStrlen(std::string &key, std::string &field, uint64_t *len) {
robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size()));
robj *fobj = createObject(OBJ_STRING, sdsnewlen(field.data(), field.size()));
DEFER {
DecrObjectsRefCount(kobj, fobj);
};
DEFER { DecrObjectsRefCount(kobj, fobj); };
int ret = RcHStrlen(cache_, kobj, fobj, reinterpret_cast<unsigned long *>(len));
if (C_OK != ret) {
if (REDIS_KEY_NOT_EXIST == ret) {
Expand Down
94 changes: 40 additions & 54 deletions src/cache/redisCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,20 @@ class RedisCache {
Status Strlen(std::string &key, int32_t *len);

// Hash Commands
Status HDel(std::string& key, std::vector<std::string> &fields);
Status HSet(std::string& key, std::string &field, std::string &value);
Status HSetnx(std::string& key, std::string &field, std::string &value);
Status HMSet(std::string& key, std::vector<storage::FieldValue> &fvs);
Status HGet(std::string& key, std::string &field, std::string *value);
Status HMGet(std::string& key,
std::vector<std::string> &fields,
std::vector<storage::ValueStatus>* vss);
Status HGetall(std::string& key, std::vector<storage::FieldValue> *fvs);
Status HKeys(std::string& key, std::vector<std::string> *fields);
Status HVals(std::string& key, std::vector<std::string> *values);
Status HExists(std::string& key, std::string &field);
Status HIncrby(std::string& key, std::string &field, int64_t value);
Status HIncrbyfloat(std::string& key, std::string &field, double value);
Status HLen(const std::string& key, uint64_t *len);
Status HStrlen(std::string& key, std::string &field, uint64_t *len);
Status HDel(std::string &key, std::vector<std::string> &fields);
Status HSet(std::string &key, std::string &field, std::string &value);
Status HSetnx(std::string &key, std::string &field, std::string &value);
Status HMSet(std::string &key, std::vector<storage::FieldValue> &fvs);
Status HGet(std::string &key, std::string &field, std::string *value);
Status HMGet(std::string &key, std::vector<std::string> &fields, std::vector<storage::ValueStatus> *vss);
Status HGetall(std::string &key, std::vector<storage::FieldValue> *fvs);
Status HKeys(std::string &key, std::vector<std::string> *fields);
Status HVals(std::string &key, std::vector<std::string> *values);
Status HExists(std::string &key, std::string &field);
Status HIncrby(std::string &key, std::string &field, int64_t value);
Status HIncrbyfloat(std::string &key, std::string &field, double value);
Status HLen(const std::string &key, uint64_t *len);
Status HStrlen(std::string &key, std::string &field, uint64_t *len);

// List Commands
Status LIndex(std::string &key, int64_t index, std::string *element);
Expand All @@ -104,46 +102,34 @@ class RedisCache {
Status RPushx(std::string &key, std::vector<std::string> &values);

// // Set Commands
Status SAdd(std::string& key, std::vector<std::string> &members);
Status SCard(const std::string& key, uint64_t *len);
Status SIsmember(std::string& key, std::string& member);
Status SMembers(std::string& key, std::vector<std::string> *members);
Status SRem(std::string& key, std::vector<std::string> &members);
Status SRandmember(std::string& key, int64_t count, std::vector<std::string> *members);
Status SAdd(std::string &key, std::vector<std::string> &members);
Status SCard(const std::string &key, uint64_t *len);
Status SIsmember(std::string &key, std::string &member);
Status SMembers(std::string &key, std::vector<std::string> *members);
Status SRem(std::string &key, std::vector<std::string> &members);
Status SRandmember(std::string &key, int64_t count, std::vector<std::string> *members);

// Zset Commands
Status ZAdd(std::string& key, std::vector<storage::ScoreMember> &score_members);
Status ZCard(const std::string& key, uint64_t *len);
Status ZCount(std::string& key, std::string &min, std::string &max, uint64_t *len);
Status ZIncrby(std::string& key, std::string& member, double increment);
Status ZRange(std::string& key,
int64_t start, int64_t stop,
std::vector<storage::ScoreMember> *score_members);
Status ZRangebyscore(std::string& key,
std::string &min, std::string &max,
std::vector<storage::ScoreMember> *score_members,
int64_t offset = 0, int64_t count = -1);
Status ZRank(std::string& key, std::string& member, int64_t *rank);
Status ZRem(std::string& key, std::vector<std::string> &members);
Status ZRemrangebyrank(std::string& key, std::string &min, std::string &max);
Status ZRemrangebyscore(std::string& key, std::string &min, std::string &max);
Status ZRevrange(std::string& key,
int64_t start, int64_t stop,
std::vector<storage::ScoreMember> *score_members);
Status ZRevrangebyscore(std::string& key,
std::string &min, std::string &max,
std::vector<storage::ScoreMember> *score_members,
int64_t offset = 0, int64_t count = -1);
Status ZRevrangebylex(std::string& key,
std::string &min, std::string &max,
std::vector<std::string> *members);
Status ZRevrank(std::string& key, std::string& member, int64_t *rank);
Status ZScore(std::string& key, std::string& member, double *score);
Status ZRangebylex(std::string& key,
std::string &min, std::string &max,
std::vector<std::string> *members);
Status ZLexcount(std::string& key, std::string &min, std::string &max, uint64_t *len);
Status ZRemrangebylex(std::string& key, std::string &min, std::string &max);
Status ZAdd(std::string &key, std::vector<storage::ScoreMember> &score_members);
Status ZCard(const std::string &key, uint64_t *len);
Status ZCount(std::string &key, std::string &min, std::string &max, uint64_t *len);
Status ZIncrby(std::string &key, std::string &member, double increment);
Status ZRange(std::string &key, int64_t start, int64_t stop, std::vector<storage::ScoreMember> *score_members);
Status ZRangebyscore(std::string &key, std::string &min, std::string &max,
std::vector<storage::ScoreMember> *score_members, int64_t offset = 0, int64_t count = -1);
Status ZRank(std::string &key, std::string &member, int64_t *rank);
Status ZRem(std::string &key, std::vector<std::string> &members);
Status ZRemrangebyrank(std::string &key, std::string &min, std::string &max);
Status ZRemrangebyscore(std::string &key, std::string &min, std::string &max);
Status ZRevrange(std::string &key, int64_t start, int64_t stop, std::vector<storage::ScoreMember> *score_members);
Status ZRevrangebyscore(std::string &key, std::string &min, std::string &max,
std::vector<storage::ScoreMember> *score_members, int64_t offset = 0, int64_t count = -1);
Status ZRevrangebylex(std::string &key, std::string &min, std::string &max, std::vector<std::string> *members);
Status ZRevrank(std::string &key, std::string &member, int64_t *rank);
Status ZScore(std::string &key, std::string &member, double *score);
Status ZRangebylex(std::string &key, std::string &min, std::string &max, std::vector<std::string> *members);
Status ZLexcount(std::string &key, std::string &min, std::string &max, uint64_t *len);
Status ZRemrangebylex(std::string &key, std::string &min, std::string &max);

// // Bit Commands
// Status SetBit(std::string& key, size_t offset, int64_t value);
Expand Down
Loading

0 comments on commit 33cd365

Please sign in to comment.