From 3e3b0e7d4417d16bd57f731f1192db2093e075ae Mon Sep 17 00:00:00 2001 From: shenmengju Date: Wed, 14 Aug 2024 15:29:58 +0800 Subject: [PATCH] modify list cmd with cache --- cmake/rediscache.cmake | 11 +- src/base_cmd.cc | 9 +- src/cache/config.h | 47 +++-- src/cache/list.cc | 273 ++++++++++++++++++++++++++ src/cache/redisCache.cc | 55 ++---- src/cache/redisCache.h | 105 +++++----- src/cache/string.cc | 100 ++++------ src/cache_define.h | 20 +- src/cmd_keys.cc | 110 +++++------ src/cmd_keys.h | 58 +++--- src/cmd_kv.cc | 187 +++++++++--------- src/cmd_kv.h | 106 +++++----- src/cmd_list.cc | 404 +++++++++++++++++++++++++++----------- src/cmd_list.h | 50 +++++ src/config.cc | 21 +- src/config.h | 4 +- src/db.cc | 8 +- src/db.h | 9 +- src/pcache.cc | 391 ++++++++++++++++++------------------ src/pcache.h | 230 +++++++++++----------- src/pcache_load_thread.cc | 127 ++++++------ src/pcache_load_thread.h | 26 ++- src/thread.h | 2 +- 23 files changed, 1372 insertions(+), 981 deletions(-) create mode 100644 src/cache/list.cc diff --git a/cmake/rediscache.cmake b/cmake/rediscache.cmake index 89d02ff16..cdf943c93 100644 --- a/cmake/rediscache.cmake +++ b/cmake/rediscache.cmake @@ -11,17 +11,18 @@ SET(REDISCACHE_LIBRARIES "${LIB_INSTALL_DIR}/librediscache.a" CACHE FILEPATH "re ExternalProject_Add( extern_rediscache ${EXTERNAL_PROJECT_LOG_ARGS} - URL https://github.com/pikiwidb/rediscache/archive/refs/tags/v1.0.7.tar.gz - URL_HASH MD5=02c8aadc018dd8d4d3803cc420d1d75b + #URL https://github.com/pikiwidb/rediscache/archive/refs/tags/v1.0.7.tar.gz + #URL_HASH MD5=02c8aadc018dd8d4d3803cc420d1d75b + #temp used + GIT_REPOSITORY git@github.com:hahahashen/rediscache.git + GIT_TAG feat/removeUseTcMallocMacroDefinition CMAKE_ARGS - -DCMAKE_BUILD_TYPE=${LIB_BUILD_TYPE} + -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_PREFIX} -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DPROJECT_BINARY_DIR=${LIB_INSTALL_PREFIX} -DCMAKE_FIND_LIBRARY_SUFFIXES=${LIB_INSTALL_PREFIX} -DSNAPPY_BUILD_TESTS=OFF - #-DWITH_JEMALLOC=${JEMALLOC_ON} - #rediscache 通过这个变量设置安装后,头文件的目录 设置这个变量后,deps-release/include路径下就有rediscache了,很神奇 -DCMAKE_INSTALL_INCLUDEDIR=${REDISCACHE_INCLUDE_DIR} -DCMAKE_POSITION_INDEPENDENT_CODE=ON BUILD_COMMAND make -j${CPU_CORE} diff --git a/src/base_cmd.cc b/src/base_cmd.cc index 1214c17f6..624ca458e 100644 --- a/src/base_cmd.cc +++ b/src/base_cmd.cc @@ -69,16 +69,15 @@ void BaseCmd::Execute(PClient* client) { return; } - if (IsNeedCacheDo() - && PCACHE_NONE != g_config.cache_mode.load() - && PSTORE.GetBackend(dbIndex)->GetCache()->CacheStatus() == PCACHE_STATUS_OK) { + if (IsNeedCacheDo() && PCACHE_NONE != g_config.cache_mode.load() && + PSTORE.GetBackend(dbIndex)->GetCache()->CacheStatus() == PCACHE_STATUS_OK) { if (IsNeedReadCache()) { ReadCache(client); } - if ( HasFlag(kCmdFlagsReadonly)&& client->CacheMiss()) { + if (HasFlag(kCmdFlagsReadonly) && client->CacheMiss()) { //@tobechecked 下面这行是pika实现中会用到的,pikiwidb中cmd层不用上key锁,因为storage层上了 //所以不需要加上这行,但是涉及锁所以再次确认比较好 - //pstd::lock::MultiScopeRecordLock record_lock(db_->LockMgr(), current_key()); + // pstd::lock::MultiScopeRecordLock record_lock(db_->LockMgr(), current_key()); DoThroughDB(client); if (IsNeedUpdateCache()) { DoUpdateCache(client); diff --git a/src/cache/config.h b/src/cache/config.h index 9d6bfafc0..b8d71e3c2 100644 --- a/src/cache/config.h +++ b/src/cache/config.h @@ -3,7 +3,6 @@ // 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. - #pragma once #include @@ -14,19 +13,19 @@ namespace cache { /* Redis maxmemory strategies */ enum RedisMaxmemoryPolicy { - CACHE_VOLATILE_LRU = 0, - CACHE_ALLKEYS_LRU = 1, - CACHE_VOLATILE_LFU = 2, - CACHE_ALLKEYS_LFU = 3, - CACHE_VOLATILE_RANDOM = 4, - CACHE_ALLKEYS_RANDOM = 5, - CACHE_VOLATILE_TTL = 6, - CACHE_NO_EVICTION = 7 + CACHE_VOLATILE_LRU = 0, + CACHE_ALLKEYS_LRU = 1, + CACHE_VOLATILE_LFU = 2, + CACHE_ALLKEYS_LFU = 3, + CACHE_VOLATILE_RANDOM = 4, + CACHE_ALLKEYS_RANDOM = 5, + CACHE_VOLATILE_TTL = 6, + CACHE_NO_EVICTION = 7 }; -#define CACHE_DEFAULT_MAXMEMORY CONFIG_DEFAULT_MAXMEMORY // 10G +#define CACHE_DEFAULT_MAXMEMORY CONFIG_DEFAULT_MAXMEMORY // 10G #define CACHE_DEFAULT_MAXMEMORY_SAMPLES CONFIG_DEFAULT_MAXMEMORY_SAMPLES -#define CACHE_DEFAULT_LFU_DECAY_TIME CONFIG_DEFAULT_LFU_DECAY_TIME +#define CACHE_DEFAULT_LFU_DECAY_TIME CONFIG_DEFAULT_LFU_DECAY_TIME /* * cache start pos @@ -39,20 +38,20 @@ constexpr int CACHE_START_FROM_END = -1; #define DEFAULT_CACHE_ITEMS_PER_KEY 512 struct CacheConfig { - uint64_t maxmemory; /* Can used max memory */ - int32_t maxmemory_policy; /* Policy for key eviction */ - int32_t maxmemory_samples; /* Precision of random sampling */ - int32_t lfu_decay_time; /* LFU counter decay factor. */ - int32_t zset_cache_start_direction; - int32_t zset_cache_field_num_per_key; + uint64_t maxmemory; /* Can used max memory */ + int32_t maxmemory_policy; /* Policy for key eviction */ + int32_t maxmemory_samples; /* Precision of random sampling */ + int32_t lfu_decay_time; /* LFU counter decay factor. */ + int32_t zset_cache_start_direction; + int32_t zset_cache_field_num_per_key; CacheConfig() - : maxmemory(CACHE_DEFAULT_MAXMEMORY) - , maxmemory_policy(CACHE_NO_EVICTION) - , maxmemory_samples(CACHE_DEFAULT_MAXMEMORY_SAMPLES) - , lfu_decay_time(CACHE_DEFAULT_LFU_DECAY_TIME) - , zset_cache_start_direction(CACHE_START_FROM_BEGIN) - , zset_cache_field_num_per_key(DEFAULT_CACHE_ITEMS_PER_KEY){} + : maxmemory(CACHE_DEFAULT_MAXMEMORY), + maxmemory_policy(CACHE_NO_EVICTION), + maxmemory_samples(CACHE_DEFAULT_MAXMEMORY_SAMPLES), + lfu_decay_time(CACHE_DEFAULT_LFU_DECAY_TIME), + zset_cache_start_direction(CACHE_START_FROM_BEGIN), + zset_cache_field_num_per_key(DEFAULT_CACHE_ITEMS_PER_KEY) {} CacheConfig& operator=(const CacheConfig& obj) { maxmemory = obj.maxmemory; @@ -64,4 +63,4 @@ struct CacheConfig { return *this; } }; -} // namespace cache +} // namespace cache diff --git a/src/cache/list.cc b/src/cache/list.cc new file mode 100644 index 000000000..fb148dbcf --- /dev/null +++ b/src/cache/list.cc @@ -0,0 +1,273 @@ +// Copyright (c) 2024-present, Qihoo, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// 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::LIndex(std::string &key, int64_t index, std::string *element) { + sds val; + robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); + DEFER { DecrObjectsRefCount(kobj); }; + int ret = RcLIndex(cache_, kobj, index, &val); + if (C_OK != ret) { + if (REDIS_KEY_NOT_EXIST == ret) { + return Status::NotFound("key not in cache"); + } else if (REDIS_ITEM_NOT_EXIST == ret) { + return Status::NotFound("index not exist"); + } + return Status::Corruption("RcLIndex failed"); + } + + element->clear(); + element->assign(val, sdslen(val)); + sdsfree(val); + + return Status::OK(); +} + +Status RedisCache::LInsert(std::string &key, storage::BeforeOrAfter &before_or_after, std::string &pivot, + std::string &value) { + int ret = RcFreeMemoryIfNeeded(cache_); + if (C_OK != ret) { + return Status::Corruption("[error] Free memory faild !"); + } + + int where = (before_or_after == storage::Before) ? REDIS_LIST_HEAD : REDIS_LIST_TAIL; + robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); + robj *pobj = createObject(OBJ_STRING, sdsnewlen(pivot.data(), pivot.size())); + robj *vobj = createObject(OBJ_STRING, sdsnewlen(value.data(), value.size())); + DEFER { DecrObjectsRefCount(kobj, pobj, vobj); }; + int res = RcLInsert(cache_, kobj, where, pobj, vobj); + if (C_OK != res) { + if (REDIS_KEY_NOT_EXIST == res) { + return Status::NotFound("key not in cache"); + } + return Status::Corruption("RcLInsert failed"); + } + + return Status::OK(); +} + +Status RedisCache::LLen(const std::string &key, uint64_t *len) { + robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); + DEFER { DecrObjectsRefCount(kobj); }; + int ret = RcLLen(cache_, kobj, reinterpret_cast(len)); + if (C_OK != ret) { + if (REDIS_KEY_NOT_EXIST == ret) { + return Status::NotFound("key not in cache"); + } + return Status::Corruption("RcLLen failed"); + } + + return Status::OK(); +} + +Status RedisCache::LPop(std::string &key, std::string *element) { + sds val; + robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); + DEFER { DecrObjectsRefCount(kobj); }; + int ret = RcLPop(cache_, kobj, &val); + if (C_OK != ret) { + if (REDIS_KEY_NOT_EXIST == ret) { + return Status::NotFound("key not in cache"); + } + return Status::Corruption("RcLPop failed"); + } + + element->clear(); + element->assign(val, sdslen(val)); + sdsfree(val); + + return Status::OK(); +} + +Status RedisCache::LPush(std::string &key, std::vector &values) { + int ret = RcFreeMemoryIfNeeded(cache_); + if (C_OK != ret) { + return Status::Corruption("[error] Free memory faild !"); + } + + robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); + robj **vals = (robj **)zcallocate(sizeof(robj *) * values.size()); + for (unsigned int i = 0; i < values.size(); ++i) { + vals[i] = createObject(OBJ_STRING, sdsnewlen(values[i].data(), values[i].size())); + } + DEFER { + FreeObjectList(vals, values.size()); + DecrObjectsRefCount(kobj); + }; + int res = RcLPush(cache_, kobj, vals, values.size()); + if (C_OK != res) { + return Status::Corruption("RcLPush failed"); + } + + return Status::OK(); +} + +Status RedisCache::LPushx(std::string &key, std::vector &values) { + int ret = RcFreeMemoryIfNeeded(cache_); + if (C_OK != ret) { + return Status::Corruption("[error] Free memory faild !"); + } + + robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); + robj **vals = (robj **)zcallocate(sizeof(robj *) * values.size()); + for (unsigned int i = 0; i < values.size(); ++i) { + vals[i] = createObject(OBJ_STRING, sdsnewlen(values[i].data(), values[i].size())); + } + DEFER { + FreeObjectList(vals, values.size()); + DecrObjectsRefCount(kobj); + }; + int res = RcLPushx(cache_, kobj, vals, values.size()); + if (C_OK != res) { + if (REDIS_KEY_NOT_EXIST == res) { + return Status::NotFound("key not in cache"); + } + return Status::Corruption("RcLPushx failed"); + } + + return Status::OK(); +} + +Status RedisCache::LRange(std::string &key, int64_t start, int64_t stop, std::vector *values) { + sds *vals = nullptr; + uint64_t vals_size = 0; + robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); + DEFER { DecrObjectsRefCount(kobj); }; + int ret = RcLRange(cache_, kobj, start, stop, &vals, reinterpret_cast(&vals_size)); + if (C_OK != ret) { + if (REDIS_KEY_NOT_EXIST == ret) { + return Status::NotFound("key not in cache"); + } + return Status::Corruption("RcLRange failed"); + } + + for (uint64_t i = 0; i < vals_size; ++i) { + values->push_back(std::string(vals[i], sdslen(vals[i]))); + } + + FreeSdsList(vals, vals_size); + return Status::OK(); +} + +Status RedisCache::LRem(std::string &key, int64_t count, std::string &value) { + robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); + robj *vobj = createObject(OBJ_STRING, sdsnewlen(value.data(), value.size())); + DEFER { DecrObjectsRefCount(kobj, vobj); }; + int ret = RcLRem(cache_, kobj, count, vobj); + if (C_OK != ret) { + if (REDIS_KEY_NOT_EXIST == ret) { + return Status::NotFound("key not in cache"); + } + return Status::Corruption("RcLRem failed"); + } + + return Status::OK(); +} + +Status RedisCache::LSet(std::string &key, int64_t index, std::string &value) { + robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); + robj *vobj = createObject(OBJ_STRING, sdsnewlen(value.data(), value.size())); + DEFER { DecrObjectsRefCount(kobj, vobj); }; + int ret = RcLSet(cache_, kobj, index, vobj); + if (C_OK != ret) { + if (REDIS_KEY_NOT_EXIST == ret) { + return Status::NotFound("key not in cache"); + } else if (REDIS_ITEM_NOT_EXIST == ret) { + return Status::NotFound("item not exist"); + } + return Status::Corruption("RcLSet failed"); + } + + return Status::OK(); +} + +Status RedisCache::LTrim(std::string &key, int64_t start, int64_t stop) { + robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); + DEFER { DecrObjectsRefCount(kobj); }; + int ret = RcLTrim(cache_, kobj, start, stop); + if (C_OK != ret) { + if (REDIS_KEY_NOT_EXIST == ret) { + return Status::NotFound("key not in cache"); + } else { + return Status::Corruption("RcLTrim failed"); + } + } + + return Status::OK(); +} + +Status RedisCache::RPop(std::string &key, std::string *element) { + sds val; + robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); + DEFER { DecrObjectsRefCount(kobj); }; + int ret = RcRPop(cache_, kobj, &val); + if (C_OK != ret) { + if (REDIS_KEY_NOT_EXIST == ret) { + return Status::NotFound("key not in cache"); + } + return Status::Corruption("RcRPop failed"); + } + + element->clear(); + element->assign(val, sdslen(val)); + sdsfree(val); + + return Status::OK(); +} + +Status RedisCache::RPush(std::string &key, std::vector &values) { + int res = RcFreeMemoryIfNeeded(cache_); + if (C_OK != res) { + return Status::Corruption("[error] Free memory faild !"); + } + + robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); + robj **vals = (robj **)zcallocate(sizeof(robj *) * values.size()); + for (unsigned int i = 0; i < values.size(); ++i) { + vals[i] = createObject(OBJ_STRING, sdsnewlen(values[i].data(), values[i].size())); + } + DEFER { + FreeObjectList(vals, values.size()); + DecrObjectsRefCount(kobj); + }; + int ret = RcRPush(cache_, kobj, vals, values.size()); + if (C_OK != ret) { + return Status::Corruption("RcRPush failed"); + } + + return Status::OK(); +} + +Status RedisCache::RPushx(std::string &key, std::vector &values) { + int res = RcFreeMemoryIfNeeded(cache_); + if (C_OK != res) { + return Status::Corruption("[error] Free memory faild !"); + } + + robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); + robj **vals = (robj **)zcallocate(sizeof(robj *) * values.size()); + for (unsigned int i = 0; i < values.size(); ++i) { + vals[i] = createObject(OBJ_STRING, sdsnewlen(values[i].data(), values[i].size())); + } + DEFER { + FreeObjectList(vals, values.size()); + DecrObjectsRefCount(kobj); + }; + int ret = RcRPushx(cache_, kobj, vals, values.size()); + if (C_OK != ret) { + if (REDIS_KEY_NOT_EXIST == ret) { + return Status::NotFound("key not in cache"); + } + return Status::Corruption("RcRPushx failed"); + } + + return Status::OK(); +} + +} // namespace cache \ No newline at end of file diff --git a/src/cache/redisCache.cc b/src/cache/redisCache.cc index f6e85a0be..8d80dd60f 100644 --- a/src/cache/redisCache.cc +++ b/src/cache/redisCache.cc @@ -3,10 +3,9 @@ // 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 "cache/redisCache.h" -#include "pstd/pstd_string.h" #include "pstd/pstd_defer.h" +#include "pstd/pstd_string.h" namespace cache { @@ -64,7 +63,9 @@ void RedisCache::SetConfig(CacheConfig *cfg) { uint64_t RedisCache::GetUsedMemory(void) { return RcGetUsedMemory(); } -void RedisCache::GetHitAndMissNum(int64_t *hits, int64_t *misses) { RcGetHitAndMissNum((long long int*)hits, (long long int*)misses); } +void RedisCache::GetHitAndMissNum(int64_t *hits, int64_t *misses) { + RcGetHitAndMissNum((long long int *)hits, (long long int *)misses); +} void RedisCache::ResetHitAndMissNum(void) { RcResetHitAndMissNum(); } @@ -82,11 +83,9 @@ int32_t RedisCache::ActiveExpireCycle(void) { return RcActiveExpireCycle(cache_) /*----------------------------------------------------------------------------- * Normal Commands *----------------------------------------------------------------------------*/ -bool RedisCache::Exists(std::string& key) { +bool RedisCache::Exists(std::string &key) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); - DEFER { - decrRefCount(kobj); - }; + DEFER { decrRefCount(kobj); }; bool is_exist = RcExists(cache_, kobj); return is_exist; @@ -94,17 +93,15 @@ bool RedisCache::Exists(std::string& key) { int64_t RedisCache::DbSize(void) { int64_t dbsize = 0; - RcCacheSize(cache_, (long long int*)&dbsize); + RcCacheSize(cache_, (long long int *)&dbsize); return dbsize; } void RedisCache::FlushCache(void) { RcFlushCache(cache_); } -Status RedisCache::Del(const std::string& key) { +Status RedisCache::Del(const std::string &key) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); - DEFER { - decrRefCount(kobj); - }; + DEFER { decrRefCount(kobj); }; int ret = RcDel(cache_, kobj); if (C_OK != ret) { if (REDIS_KEY_NOT_EXIST == ret) { @@ -117,12 +114,10 @@ Status RedisCache::Del(const std::string& key) { return Status::OK(); } -Status RedisCache::Expire(std::string& key, int64_t ttl) { +Status RedisCache::Expire(std::string &key, int64_t ttl) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); robj *tobj = createStringObjectFromLongLong(ttl); - DEFER { - DecrObjectsRefCount(kobj, tobj); - }; + DEFER { DecrObjectsRefCount(kobj, tobj); }; int ret = RcExpire(cache_, kobj, tobj); if (C_OK != ret) { if (REDIS_KEY_NOT_EXIST == ret) { @@ -135,12 +130,10 @@ Status RedisCache::Expire(std::string& key, int64_t ttl) { return Status::OK(); } -Status RedisCache::Expireat(std::string& key, int64_t ttl) { +Status RedisCache::Expireat(std::string &key, int64_t ttl) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); robj *tobj = createStringObjectFromLongLong(ttl); - DEFER { - DecrObjectsRefCount(kobj, tobj); - }; + DEFER { DecrObjectsRefCount(kobj, tobj); }; int ret = RcExpireat(cache_, kobj, tobj); if (C_OK != ret) { if (REDIS_KEY_NOT_EXIST == ret) { @@ -152,11 +145,9 @@ Status RedisCache::Expireat(std::string& key, int64_t ttl) { return Status::OK(); } -Status RedisCache::TTL(std::string& key, int64_t *ttl) { +Status RedisCache::TTL(std::string &key, int64_t *ttl) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); - DEFER { - DecrObjectsRefCount(kobj); - }; + DEFER { DecrObjectsRefCount(kobj); }; int ret = RcTTL(cache_, kobj, ttl); if (C_OK != ret) { if (REDIS_KEY_NOT_EXIST == ret) { @@ -168,11 +159,9 @@ Status RedisCache::TTL(std::string& key, int64_t *ttl) { return Status::OK(); } -Status RedisCache::Persist(std::string& key) { +Status RedisCache::Persist(std::string &key) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); - DEFER { - DecrObjectsRefCount(kobj); - }; + DEFER { DecrObjectsRefCount(kobj); }; int ret = RcPersist(cache_, kobj); if (C_OK != ret) { if (REDIS_KEY_NOT_EXIST == ret) { @@ -184,12 +173,10 @@ Status RedisCache::Persist(std::string& key) { return Status::OK(); } -Status RedisCache::Type(std::string& key, std::string *value) { +Status RedisCache::Type(std::string &key, std::string *value) { sds val; robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); - DEFER { - DecrObjectsRefCount(kobj); - }; + DEFER { DecrObjectsRefCount(kobj); }; int ret = RcType(cache_, kobj, &val); if (C_OK != ret) { if (REDIS_KEY_NOT_EXIST == ret) { @@ -267,6 +254,4 @@ void RedisCache::ConvertObjectToString(robj *obj, std::string *value) { } } -} // namespace cache - -/* EOF */ \ No newline at end of file +} // namespace cache \ No newline at end of file diff --git a/src/cache/redisCache.h b/src/cache/redisCache.h index c4a72cb03..7d4b22145 100644 --- a/src/cache/redisCache.h +++ b/src/cache/redisCache.h @@ -8,14 +8,14 @@ #include #include -#include -#include #include +#include #include +#include #include extern "C" { - #include "rediscache/redis.h" +#include "rediscache/redis.h" } //#include "rediscache/redis.h" @@ -28,7 +28,7 @@ namespace cache { using Status = rocksdb::Status; class RedisCache { -public: + public: RedisCache(); ~RedisCache(); @@ -41,35 +41,35 @@ class RedisCache { int32_t ActiveExpireCycle(void); // Normal Commands - bool Exists(std::string& key); + bool Exists(std::string &key); int64_t DbSize(void); - void FlushCache(void); // 清空cache - - Status Del(const std::string& key); // 删除某个key - Status Expire(std::string& key, int64_t ttl);// 设置键的TTL - Status Expireat(std::string& key, int64_t ttl); - Status TTL(std::string& key, int64_t *ttl); - Status Persist(std::string& key); - Status Type(std::string& key, std::string *value); + void FlushCache(void); // 清空cache + + Status Del(const std::string &key); // 删除某个key + Status Expire(std::string &key, int64_t ttl); // 设置键的TTL + Status Expireat(std::string &key, int64_t ttl); + Status TTL(std::string &key, int64_t *ttl); + Status Persist(std::string &key); + Status Type(std::string &key, std::string *value); Status RandomKey(std::string *key); // String Commands - Status Set(std::string& key, std::string &value, int64_t ttl); - Status SetWithoutTTL(std::string& key, std::string &value); - Status Setnx(std::string& key, std::string &value, int64_t ttl); - Status SetnxWithoutTTL(std::string& key, std::string &value); - Status Setxx(std::string& key, std::string &value, int64_t ttl); - Status SetxxWithoutTTL(std::string& key, std::string &value); - Status Get(const std::string& key, std::string *value); - Status Incr(std::string& key); - Status Decr(std::string& key); - Status IncrBy(std::string& key, int64_t incr); - Status DecrBy(std::string& key, int64_t incr); - Status Incrbyfloat(std::string& key, double incr); - Status Append(std::string& key, std::string &value); - Status GetRange(std::string& key, int64_t start, int64_t end, std::string *value); - Status SetRange(std::string& key, int64_t start, std::string &value); - Status Strlen(std::string& key, int32_t *len); + Status Set(std::string &key, std::string &value, int64_t ttl); + Status SetWithoutTTL(std::string &key, std::string &value); + Status Setnx(std::string &key, std::string &value, int64_t ttl); + Status SetnxWithoutTTL(std::string &key, std::string &value); + Status Setxx(std::string &key, std::string &value, int64_t ttl); + Status SetxxWithoutTTL(std::string &key, std::string &value); + Status Get(const std::string &key, std::string *value); + Status Incr(std::string &key); + Status Decr(std::string &key); + Status IncrBy(std::string &key, int64_t incr); + Status DecrBy(std::string &key, int64_t incr); + Status Incrbyfloat(std::string &key, double incr); + Status Append(std::string &key, std::string &value); + Status GetRange(std::string &key, int64_t start, int64_t end, std::string *value); + Status SetRange(std::string &key, int64_t start, std::string &value); + Status Strlen(std::string &key, int32_t *len); // Hash Commands // Status HDel(std::string& key, std::vector &fields); @@ -86,28 +86,27 @@ class RedisCache { // 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(std::string& key, uint64_t *len); + // 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); - // Status LInsert(std::string& key, storage::BeforeOrAfter &before_or_after, - // std::string &pivot, std::string &value); - // Status LLen(std::string& key, uint64_t *len); - // Status LPop(std::string& key, std::string *element); - // Status LPush(std::string& key, std::vector &values); - // Status LPushx(std::string& key, std::vector &values); - // Status LRange(std::string& key, int64_t start, int64_t stop, std::vector *values); - // Status LRem(std::string& key, int64_t count, std::string &value); - // Status LSet(std::string& key, int64_t index, std::string &value); - // Status LTrim(std::string& key, int64_t start, int64_t stop); - // Status RPop(std::string& key, std::string *element); - // Status RPush(std::string& key, std::vector &values); - // Status RPushx(std::string& key, std::vector &values); + // List Commands + Status LIndex(std::string &key, int64_t index, std::string *element); + Status LInsert(std::string &key, storage::BeforeOrAfter &before_or_after, std::string &pivot, std::string &value); + Status LLen(const std::string &key, uint64_t *len); + Status LPop(std::string &key, std::string *element); + Status LPush(std::string &key, std::vector &values); + Status LPushx(std::string &key, std::vector &values); + Status LRange(std::string &key, int64_t start, int64_t stop, std::vector *values); + Status LRem(std::string &key, int64_t count, std::string &value); + Status LSet(std::string &key, int64_t index, std::string &value); + Status LTrim(std::string &key, int64_t start, int64_t stop); + Status RPop(std::string &key, std::string *element); + Status RPush(std::string &key, std::vector &values); + Status RPushx(std::string &key, std::vector &values); // // Set Commands // Status SAdd(std::string& key, std::vector &members); - // Status SCard(std::string& key, uint64_t *len); + // Status SCard(const std::string& key, uint64_t *len); // Status SIsmember(std::string& key, std::string& member); // Status SMembers(std::string& key, std::vector *members); // Status SRem(std::string& key, std::vector &members); @@ -115,7 +114,7 @@ class RedisCache { // // Zset Commands // Status ZAdd(std::string& key, std::vector &score_members); - // Status ZCard(std::string& key, uint64_t *len); + // 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, @@ -155,7 +154,7 @@ class RedisCache { // Status BitPos(std::string& key, int64_t bit, int64_t start, int64_t *value); // Status BitPos(std::string& key, int64_t bit, int64_t start, int64_t end, int64_t *value); -protected: + protected: void DecrObjectsRefCount(robj *argv1, robj *argv2 = nullptr, robj *argv3 = nullptr); void FreeSdsList(sds *items, uint32_t size); void FreeObjectList(robj **items, uint32_t size); @@ -163,11 +162,11 @@ class RedisCache { void FreeZitemList(zitem *items, uint32_t size); void ConvertObjectToString(robj *obj, std::string *value); -private: - RedisCache(const RedisCache&); - RedisCache& operator=(const RedisCache&); + private: + RedisCache(const RedisCache &); + RedisCache &operator=(const RedisCache &); -private: + private: redisCache cache_; }; -} // namespace cache +} // namespace cache diff --git a/src/cache/string.cc b/src/cache/string.cc index 063c44c84..34337a9ef 100644 --- a/src/cache/string.cc +++ b/src/cache/string.cc @@ -5,12 +5,12 @@ #include -#include "redisCache.h" #include "pstd/pstd_defer.h" +#include "redisCache.h" namespace cache { -Status RedisCache::Set(std::string& key, std::string &value, int64_t ttl) { +Status RedisCache::Set(std::string &key, std::string &value, int64_t ttl) { int res = RcFreeMemoryIfNeeded(cache_); if (C_OK != res) { return Status::Corruption("[error] Free memory faild !"); @@ -19,9 +19,7 @@ Status RedisCache::Set(std::string& key, std::string &value, int64_t ttl) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); robj *vobj = createObject(OBJ_STRING, sdsnewlen(value.data(), value.size())); robj *tobj = createStringObjectFromLongLong(ttl); - DEFER { - DecrObjectsRefCount(kobj, vobj, tobj); - }; + DEFER { DecrObjectsRefCount(kobj, vobj, tobj); }; int ret = RcSet(cache_, kobj, vobj, tobj); if (C_OK != ret) { return Status::Corruption("RcSet failed"); @@ -30,7 +28,7 @@ Status RedisCache::Set(std::string& key, std::string &value, int64_t ttl) { return Status::OK(); } -Status RedisCache::SetWithoutTTL(std::string& key, std::string &value) { +Status RedisCache::SetWithoutTTL(std::string &key, std::string &value) { int ret = RcFreeMemoryIfNeeded(cache_); if (C_OK != ret) { return Status::Corruption("[error] Free memory faild !"); @@ -38,9 +36,7 @@ Status RedisCache::SetWithoutTTL(std::string& key, std::string &value) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); robj *vobj = createObject(OBJ_STRING, sdsnewlen(value.data(), value.size())); - DEFER { - DecrObjectsRefCount(kobj, vobj); - }; + DEFER { DecrObjectsRefCount(kobj, vobj); }; int res = RcSet(cache_, kobj, vobj, nullptr); if (C_OK != res) { return Status::Corruption("RcSetnx failed, key exists!"); @@ -49,7 +45,7 @@ Status RedisCache::SetWithoutTTL(std::string& key, std::string &value) { return Status::OK(); } -Status RedisCache::Setnx(std::string& key, std::string &value, int64_t ttl) { +Status RedisCache::Setnx(std::string &key, std::string &value, int64_t ttl) { int ret = RcFreeMemoryIfNeeded(cache_); if (C_OK != ret) { return Status::Corruption("[error] Free memory faild !"); @@ -58,9 +54,7 @@ Status RedisCache::Setnx(std::string& key, std::string &value, int64_t ttl) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); robj *vobj = createObject(OBJ_STRING, sdsnewlen(value.data(), value.size())); robj *tobj = createStringObjectFromLongLong(ttl); - DEFER { - DecrObjectsRefCount(kobj, vobj, tobj); - }; + DEFER { DecrObjectsRefCount(kobj, vobj, tobj); }; int res = RcSetnx(cache_, kobj, vobj, tobj); if (C_OK != res) { return Status::Corruption("RcSetnx failed, key exists!"); @@ -69,7 +63,7 @@ Status RedisCache::Setnx(std::string& key, std::string &value, int64_t ttl) { return Status::OK(); } -Status RedisCache::SetnxWithoutTTL(std::string& key, std::string &value) { +Status RedisCache::SetnxWithoutTTL(std::string &key, std::string &value) { int res = RcFreeMemoryIfNeeded(cache_); if (C_OK != res) { return Status::Corruption("[error] Free memory faild !"); @@ -77,9 +71,7 @@ Status RedisCache::SetnxWithoutTTL(std::string& key, std::string &value) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); robj *vobj = createObject(OBJ_STRING, sdsnewlen(value.data(), value.size())); - DEFER { - DecrObjectsRefCount(kobj, vobj); - }; + DEFER { DecrObjectsRefCount(kobj, vobj); }; int ret = RcSetnx(cache_, kobj, vobj, nullptr); if (C_OK != ret) { return Status::Corruption("RcSetnx failed, key exists!"); @@ -88,7 +80,7 @@ Status RedisCache::SetnxWithoutTTL(std::string& key, std::string &value) { return Status::OK(); } -Status RedisCache::Setxx(std::string& key, std::string &value, int64_t ttl) { +Status RedisCache::Setxx(std::string &key, std::string &value, int64_t ttl) { int ret = RcFreeMemoryIfNeeded(cache_); if (C_OK != ret) { return Status::Corruption("[error] Free memory faild !"); @@ -97,9 +89,7 @@ Status RedisCache::Setxx(std::string& key, std::string &value, int64_t ttl) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); robj *vobj = createObject(OBJ_STRING, sdsnewlen(value.data(), value.size())); robj *tobj = createStringObjectFromLongLong(ttl); - DEFER { - DecrObjectsRefCount(kobj, vobj, tobj); - }; + DEFER { DecrObjectsRefCount(kobj, vobj, tobj); }; int res = RcSetxx(cache_, kobj, vobj, tobj); if (C_OK != res) { return Status::Corruption("RcSetxx failed, key not exists!"); @@ -108,7 +98,7 @@ Status RedisCache::Setxx(std::string& key, std::string &value, int64_t ttl) { return Status::OK(); } -Status RedisCache::SetxxWithoutTTL(std::string& key, std::string &value) { +Status RedisCache::SetxxWithoutTTL(std::string &key, std::string &value) { int res = RcFreeMemoryIfNeeded(cache_); if (C_OK != res) { return Status::Corruption("[error] Free memory faild !"); @@ -116,9 +106,7 @@ Status RedisCache::SetxxWithoutTTL(std::string& key, std::string &value) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); robj *vobj = createObject(OBJ_STRING, sdsnewlen(value.data(), value.size())); - DEFER { - DecrObjectsRefCount(kobj, vobj); - }; + DEFER { DecrObjectsRefCount(kobj, vobj); }; int ret = RcSetxx(cache_, kobj, vobj, nullptr); if (C_OK != ret) { return Status::Corruption("RcSetxx failed, key not exists!"); @@ -127,12 +115,10 @@ Status RedisCache::SetxxWithoutTTL(std::string& key, std::string &value) { return Status::OK(); } -Status RedisCache::Get(const std::string& key, std::string *value) { +Status RedisCache::Get(const std::string &key, std::string *value) { robj *val; robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); - DEFER { - DecrObjectsRefCount(kobj); - }; + DEFER { DecrObjectsRefCount(kobj); }; int ret = RcGet(cache_, kobj, &val); if (C_OK != ret) { if (REDIS_KEY_NOT_EXIST == ret) { @@ -148,11 +134,9 @@ Status RedisCache::Get(const std::string& key, std::string *value) { return Status::OK(); } -Status RedisCache::Incr(std::string& key) { +Status RedisCache::Incr(std::string &key) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); - DEFER { - DecrObjectsRefCount(kobj); - }; + DEFER { DecrObjectsRefCount(kobj); }; long long int ret; int res = RcIncr(cache_, kobj, &ret); if (C_OK != res) { @@ -162,13 +146,11 @@ Status RedisCache::Incr(std::string& key) { return Status::OK(); } -Status RedisCache::Decr(std::string& key) { +Status RedisCache::Decr(std::string &key) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); - DEFER { - DecrObjectsRefCount(kobj); - }; + DEFER { DecrObjectsRefCount(kobj); }; long long int ret; - int res = RcDecr(cache_, kobj, &ret); + int res = RcDecr(cache_, kobj, &ret); if (C_OK != res) { return Status::Corruption("RcDecr failed!"); } @@ -176,11 +158,9 @@ Status RedisCache::Decr(std::string& key) { return Status::OK(); } -Status RedisCache::IncrBy(std::string& key, int64_t incr) { +Status RedisCache::IncrBy(std::string &key, int64_t incr) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); - DEFER { - DecrObjectsRefCount(kobj); - }; + DEFER { DecrObjectsRefCount(kobj); }; long long int ret; int res = RcIncrBy(cache_, kobj, incr, &ret); if (C_OK != res) { @@ -190,11 +170,9 @@ Status RedisCache::IncrBy(std::string& key, int64_t incr) { return Status::OK(); } -Status RedisCache::DecrBy(std::string& key, int64_t incr) { +Status RedisCache::DecrBy(std::string &key, int64_t incr) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); - DEFER { - DecrObjectsRefCount(kobj); - }; + DEFER { DecrObjectsRefCount(kobj); }; long long int ret; int res = RcDecrBy(cache_, kobj, incr, &ret); if (C_OK != res) { @@ -204,12 +182,10 @@ Status RedisCache::DecrBy(std::string& key, int64_t incr) { return Status::OK(); } -Status RedisCache::Incrbyfloat(std::string& key, double incr) { +Status RedisCache::Incrbyfloat(std::string &key, double incr) { long double ret = .0f; robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); - DEFER { - DecrObjectsRefCount(kobj); - }; + DEFER { DecrObjectsRefCount(kobj); }; int res = RcIncrByFloat(cache_, kobj, incr, &ret); if (C_OK != res) { return Status::Corruption("RcIncrByFloat failed!"); @@ -218,13 +194,11 @@ Status RedisCache::Incrbyfloat(std::string& key, double incr) { return Status::OK(); } -Status RedisCache::Append(std::string& key, std::string &value) { +Status RedisCache::Append(std::string &key, std::string &value) { uint64_t ret = 0; robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); robj *vobj = createObject(OBJ_STRING, sdsnewlen(value.data(), value.size())); - DEFER { - DecrObjectsRefCount(kobj, vobj); - }; + DEFER { DecrObjectsRefCount(kobj, vobj); }; int res = RcAppend(cache_, kobj, vobj, reinterpret_cast(&ret)); if (C_OK != res) { return Status::Corruption("RcAppend failed!"); @@ -233,12 +207,10 @@ Status RedisCache::Append(std::string& key, std::string &value) { return Status::OK(); } -Status RedisCache::GetRange(std::string& key, int64_t start, int64_t end, std::string *value) { +Status RedisCache::GetRange(std::string &key, int64_t start, int64_t end, std::string *value) { sds val; robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); - DEFER { - DecrObjectsRefCount(kobj); - }; + DEFER { DecrObjectsRefCount(kobj); }; int ret = RcGetRange(cache_, kobj, start, end, &val); if (C_OK != ret) { if (REDIS_KEY_NOT_EXIST == ret) { @@ -255,7 +227,7 @@ Status RedisCache::GetRange(std::string& key, int64_t start, int64_t end, std::s return Status::OK(); } -Status RedisCache::SetRange(std::string& key, int64_t start, std::string &value) { +Status RedisCache::SetRange(std::string &key, int64_t start, std::string &value) { if (C_OK != RcFreeMemoryIfNeeded(cache_)) { return Status::Corruption("[error] Free memory faild !"); } @@ -263,9 +235,7 @@ Status RedisCache::SetRange(std::string& key, int64_t start, std::string &value) uint64_t ret = 0; robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); robj *vobj = createObject(OBJ_STRING, sdsnewlen(value.data(), value.size())); - DEFER { - DecrObjectsRefCount(kobj, vobj); - }; + DEFER { DecrObjectsRefCount(kobj, vobj); }; int res = RcSetRange(cache_, kobj, start, vobj, reinterpret_cast(&ret)); if (C_OK != res) { return Status::Corruption("SetRange failed!"); @@ -274,11 +244,9 @@ Status RedisCache::SetRange(std::string& key, int64_t start, std::string &value) return Status::OK(); } -Status RedisCache::Strlen(std::string& key, int32_t *len) { +Status RedisCache::Strlen(std::string &key, int32_t *len) { robj *kobj = createObject(OBJ_STRING, sdsnewlen(key.data(), key.size())); - DEFER { - DecrObjectsRefCount(kobj); - }; + DEFER { DecrObjectsRefCount(kobj); }; int ret = RcStrlen(cache_, kobj, len); if (C_OK != ret) { if (REDIS_KEY_NOT_EXIST == ret) { diff --git a/src/cache_define.h b/src/cache_define.h index 780386f7a..e77365255 100644 --- a/src/cache_define.h +++ b/src/cache_define.h @@ -5,7 +5,7 @@ #pragma once -namespace pikiwidb{ +namespace pikiwidb { /* * cache mode */ @@ -24,17 +24,19 @@ const int PCACHE_STATUS_CLEAR = 5; const int PCACHE_START_FROM_BEGIN = 0; const int PCACHE_START_FROM_END = -1; -// prefix of pikiwidb cache -const std::string PCacheKeyPrefixK = "K"; -const std::string PCacheKeyPrefixH = "H"; -const std::string PCacheKeyPrefixS = "S"; -const std::string PCacheKeyPrefixZ = "Z"; -const std::string PCacheKeyPrefixL = "L"; +/* + * key type + */ +const char KEY_TYPE_KV = 'k'; +const char KEY_TYPE_HASH = 'h'; +const char KEY_TYPE_LIST = 'l'; +const char KEY_TYPE_SET = 's'; +const char KEY_TYPE_ZSET = 'z'; const int64_t CACHE_LOAD_QUEUE_MAX_SIZE = 2048; const int64_t CACHE_VALUE_ITEM_MAX_SIZE = 2048; const int64_t CACHE_LOAD_NUM_ONE_TIME = 256; // TTL option -const int PCache_TTL_NONE=-1; -} // namespace pikiwidb +const int PCache_TTL_NONE = -1; +} // namespace pikiwidb diff --git a/src/cmd_keys.cc b/src/cmd_keys.cc index b6b25f7ca..0d051392d 100644 --- a/src/cmd_keys.cc +++ b/src/cmd_keys.cc @@ -14,7 +14,8 @@ namespace pikiwidb { DelCmd::DelCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite| kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryKeyspace) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryKeyspace) {} bool DelCmd::DoInitial(PClient* client) { std::vector keys(client->argv_.begin() + 1, client->argv_.end()); client->SetKey(keys); @@ -32,9 +33,7 @@ void DelCmd::DoCmd(PClient* client) { } } -void DelCmd::DoThroughDB(PClient* client) { - DoCmd(client); -} +void DelCmd::DoThroughDB(PClient* client) { DoCmd(client); } void DelCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { @@ -44,7 +43,8 @@ void DelCmd::DoUpdateCache(PClient* client) { } ExistsCmd::ExistsCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsReadonly | kCmdFlagsDoThroughDB | kCmdFlagsReadCache, kAclCategoryRead | kAclCategoryKeyspace) {} + : BaseCmd(name, arity, kCmdFlagsReadonly | kCmdFlagsDoThroughDB | kCmdFlagsReadCache, + kAclCategoryRead | kAclCategoryKeyspace) {} bool ExistsCmd::DoInitial(PClient* client) { std::vector keys(client->argv_.begin() + 1, client->argv_.end()); @@ -67,14 +67,14 @@ void ExistsCmd::DoCmd(PClient* client) { } void ExistsCmd::ReadCache(PClient* client) { - auto keys=client->Keys(); + auto keys = client->Keys(); if (1 < keys.size()) { client->SetRes(CmdRes::kCacheMiss); return; } bool exist = PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Exists(keys[0]); - if (exist) { - client->AppendInteger(1); + if (exist) { + client->AppendInteger(1); } else { client->SetRes(CmdRes::kCacheMiss); } @@ -86,7 +86,8 @@ void ExistsCmd::DoThroughDB(PClient* client) { } TypeCmd::TypeCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsReadonly|kCmdFlagsDoThroughDB | kCmdFlagsReadCache, kAclCategoryRead | kAclCategoryKeyspace) {} + : BaseCmd(name, arity, kCmdFlagsReadonly | kCmdFlagsDoThroughDB | kCmdFlagsReadCache, + kAclCategoryRead | kAclCategoryKeyspace) {} bool TypeCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -105,8 +106,8 @@ void TypeCmd::DoCmd(PClient* client) { void TypeCmd::ReadCache(PClient* client) { std::string key_type; - auto key=client->Key(); - rocksdb::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Type(key, key_type); + auto key = client->Key(); + rocksdb::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Type(key, &key_type); if (s.ok()) { client->AppendContent(key_type); } else { @@ -120,7 +121,8 @@ void TypeCmd::DoThroughDB(PClient* client) { } ExpireCmd::ExpireCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryKeyspace) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryKeyspace) {} bool ExpireCmd::DoInitial(PClient* client) { if (pstd::String2int(client->argv_[2], &sec_) == 0) { @@ -142,19 +144,18 @@ void ExpireCmd::DoCmd(PClient* client) { } } -void ExpireCmd::DoThroughDB(PClient* client) { - DoCmd(client); -} +void ExpireCmd::DoThroughDB(PClient* client) { DoCmd(client); } void ExpireCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key=client->Key(); + auto key = client->Key(); PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Expire(key, sec_); } } TtlCmd::TtlCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsReadonly|kCmdFlagsDoThroughDB | kCmdFlagsReadCache, kAclCategoryRead | kAclCategoryKeyspace) {} + : BaseCmd(name, arity, kCmdFlagsReadonly | kCmdFlagsDoThroughDB | kCmdFlagsReadCache, + kAclCategoryRead | kAclCategoryKeyspace) {} bool TtlCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -172,16 +173,16 @@ void TtlCmd::DoCmd(PClient* client) { void TtlCmd::ReadCache(PClient* client) { rocksdb::Status s; - auto key=client->Key(); + auto key = client->Key(); auto timestamp = PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->TTL(key); if (timestamp == -3) { - client->SetRes(CmdRes::kErrOther, "ttl internal error"); - return; - } - if(timestamp!=-2){ + client->SetRes(CmdRes::kErrOther, "ttl internal error"); + return; + } + if (timestamp != -2) { client->AppendInteger(timestamp); - }else{ -// mean this key not exist + } else { + // mean this key not exist client->SetRes(CmdRes::kCacheMiss); } } @@ -192,7 +193,8 @@ void TtlCmd::DoThroughDB(PClient* client) { } PExpireCmd::PExpireCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryKeyspace) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryKeyspace) {} bool PExpireCmd::DoInitial(PClient* client) { if (pstd::String2int(client->argv_[2], &msec_) == 0) { @@ -214,19 +216,18 @@ void PExpireCmd::DoCmd(PClient* client) { } } -void PExpireCmd::DoThroughDB(PClient* client){ - DoCmd(client); -} +void PExpireCmd::DoThroughDB(PClient* client) { DoCmd(client); } void PExpireCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key=client->Key(); - PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Expire(key, msec_/1000); + auto key = client->Key(); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Expire(key, msec_ / 1000); } } ExpireatCmd::ExpireatCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryKeyspace) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryKeyspace) {} bool ExpireatCmd::DoInitial(PClient* client) { if (pstd::String2int(client->argv_[2], &time_stamp_) == 0) { @@ -244,23 +245,22 @@ void ExpireatCmd::DoCmd(PClient* client) { s_ = rocksdb::Status::OK(); } else { client->SetRes(CmdRes::kErrOther, "expireat internal error"); - s_ = rocksdb::Status::Corruption("expireat internal error"); + s_ = rocksdb::Status::Corruption("expireat internal error"); } } -void ExpireatCmd::DoThroughDB(PClient* client) { - DoCmd(client); -} +void ExpireatCmd::DoThroughDB(PClient* client) { DoCmd(client); } void ExpireatCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key=client->Key(); + auto key = client->Key(); PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Expireat(key, time_stamp_); } } PExpireatCmd::PExpireatCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite| kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryKeyspace) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryKeyspace) {} bool PExpireatCmd::DoInitial(PClient* client) { if (pstd::String2int(client->argv_[2], &time_stamp_ms_) == 0) { @@ -283,19 +283,18 @@ void PExpireatCmd::DoCmd(PClient* client) { } } -void PExpireatCmd::DoThroughDB(PClient* client) { - DoCmd(client); -} +void PExpireatCmd::DoThroughDB(PClient* client) { DoCmd(client); } void PExpireatCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key=client->Key(); - PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Expireat(key, time_stamp_ms_/1000); + auto key = client->Key(); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Expireat(key, time_stamp_ms_ / 1000); } } PersistCmd::PersistCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite|kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryKeyspace) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryKeyspace) {} bool PersistCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -313,13 +312,11 @@ void PersistCmd::DoCmd(PClient* client) { } } -void PersistCmd::DoThroughDB(PClient* client) { - DoCmd(client); -} +void PersistCmd::DoThroughDB(PClient* client) { DoCmd(client); } void PersistCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key=client->Key(); + auto key = client->Key(); PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Persist(key); } } @@ -346,7 +343,8 @@ void KeysCmd::DoCmd(PClient* client) { } PttlCmd::PttlCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsReadonly|kCmdFlagsDoThroughDB | kCmdFlagsReadCache, kAclCategoryRead | kAclCategoryKeyspace) {} + : BaseCmd(name, arity, kCmdFlagsReadonly | kCmdFlagsDoThroughDB | kCmdFlagsReadCache, + kAclCategoryRead | kAclCategoryKeyspace) {} bool PttlCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -366,16 +364,16 @@ void PttlCmd::DoCmd(PClient* client) { void PttlCmd::ReadCache(PClient* client) { rocksdb::Status s; - auto key=client->Key(); + auto key = client->Key(); auto timestamp = PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->TTL(key); if (timestamp == -3) { - client->SetRes(CmdRes::kErrOther, "ttl internal error"); - return; - } - if(timestamp!=-2){ - client->AppendInteger(timestamp*1000); - }else{ -// mean this key not exist + client->SetRes(CmdRes::kErrOther, "ttl internal error"); + return; + } + if (timestamp != -2) { + client->AppendInteger(timestamp * 1000); + } else { + // mean this key not exist client->SetRes(CmdRes::kCacheMiss); } } diff --git a/src/cmd_keys.h b/src/cmd_keys.h index 5df8dbca3..44ad32477 100644 --- a/src/cmd_keys.h +++ b/src/cmd_keys.h @@ -19,11 +19,11 @@ class DelCmd : public BaseCmd { bool DoInitial(PClient* client) override; private: - rocksdb::Status s_; - + rocksdb::Status s_; + void DoCmd(PClient* client) override; - void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; }; class ExistsCmd : public BaseCmd { @@ -35,8 +35,8 @@ class ExistsCmd : public BaseCmd { private: void DoCmd(PClient* client) override; - void DoThroughDB(PClient *client) override; - void ReadCache(PClient *client) override; + void DoThroughDB(PClient* client) override; + void ReadCache(PClient* client) override; }; class TypeCmd : public BaseCmd { @@ -48,8 +48,8 @@ class TypeCmd : public BaseCmd { private: void DoCmd(PClient* client) override; - void DoThroughDB(PClient *client) override; - void ReadCache(PClient *client) override; + void DoThroughDB(PClient* client) override; + void ReadCache(PClient* client) override; }; class ExpireCmd : public BaseCmd { @@ -61,10 +61,10 @@ class ExpireCmd : public BaseCmd { private: void DoCmd(PClient* client) override; - void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; - rocksdb::Status s_; - int64_t sec_ = 0; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; + rocksdb::Status s_; + int64_t sec_ = 0; }; class TtlCmd : public BaseCmd { @@ -76,8 +76,8 @@ class TtlCmd : public BaseCmd { private: void DoCmd(PClient* client) override; - void DoThroughDB(PClient *client) override; - void ReadCache(PClient *client) override; + void DoThroughDB(PClient* client) override; + void ReadCache(PClient* client) override; }; class PExpireCmd : public BaseCmd { @@ -89,8 +89,8 @@ class PExpireCmd : public BaseCmd { private: void DoCmd(PClient* client) override; - void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; int64_t msec_ = 0; rocksdb::Status s_; }; @@ -103,11 +103,11 @@ class ExpireatCmd : public BaseCmd { bool DoInitial(PClient* client) override; private: - rocksdb::Status s_; - int64_t time_stamp_ = 0; + rocksdb::Status s_; + int64_t time_stamp_ = 0; void DoCmd(PClient* client) override; - void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; }; class PExpireatCmd : public BaseCmd { @@ -118,11 +118,11 @@ class PExpireatCmd : public BaseCmd { bool DoInitial(PClient* client) override; private: - rocksdb::Status s_; - int64_t time_stamp_ms_ = 0; + rocksdb::Status s_; + int64_t time_stamp_ms_ = 0; void DoCmd(PClient* client) override; - void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; }; class PersistCmd : public BaseCmd { @@ -134,9 +134,9 @@ class PersistCmd : public BaseCmd { private: void DoCmd(PClient* client) override; - rocksdb::Status s_; - void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + rocksdb::Status s_; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; }; class KeysCmd : public BaseCmd { @@ -159,8 +159,8 @@ class PttlCmd : public BaseCmd { private: void DoCmd(PClient* client) override; - void DoThroughDB(PClient *client) override; - void ReadCache(PClient *client) override; + void DoThroughDB(PClient* client) override; + void ReadCache(PClient* client) override; }; class RenameCmd : public BaseCmd { diff --git a/src/cmd_kv.cc b/src/cmd_kv.cc index d72205454..9afc8f3e8 100644 --- a/src/cmd_kv.cc +++ b/src/cmd_kv.cc @@ -14,7 +14,9 @@ namespace pikiwidb { GetCmd::GetCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsReadonly | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache | kCmdFlagsReadCache, kAclCategoryRead | kAclCategoryString) {} + : BaseCmd(name, arity, + kCmdFlagsReadonly | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache | kCmdFlagsReadCache, + kAclCategoryRead | kAclCategoryString) {} bool GetCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -35,7 +37,7 @@ void GetCmd::DoCmd(PClient* client) { } void GetCmd::ReadCache(PClient* client) { - auto key_=client->Key(); + auto key_ = client->Key(); auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Get(key_, &value_); if (s.ok()) { client->AppendString(value_); @@ -51,13 +53,14 @@ void GetCmd::DoThroughDB(PClient* client) { void GetCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key_=client->Key(); + auto key_ = client->Key(); PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->WriteKVToCache(key_, value_, ttl_); } } SetCmd::SetCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryString) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryString) {} // SET key value [NX | XX] [EX seconds | PX milliseconds] bool SetCmd::DoInitial(PClient* client) { @@ -131,9 +134,7 @@ void SetCmd::DoCmd(PClient* client) { } } -void SetCmd::DoThroughDB(PClient* client) { - DoCmd(client); -} +void SetCmd::DoThroughDB(PClient* client) { DoCmd(client); } void SetCmd::DoUpdateCache(PClient* client) { if (SetCmd::kNX == condition_) { @@ -150,7 +151,8 @@ void SetCmd::DoUpdateCache(PClient* client) { } AppendCmd::AppendCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryString) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryString) {} bool AppendCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -159,7 +161,7 @@ bool AppendCmd::DoInitial(PClient* client) { void AppendCmd::DoCmd(PClient* client) { int32_t new_len = 0; - s_ =PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Append(client->Key(), client->argv_[2], &new_len); + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Append(client->Key(), client->argv_[2], &new_len); if (s_.ok() || s_.IsNotFound()) { client->AppendInteger(new_len); } else { @@ -167,19 +169,18 @@ void AppendCmd::DoCmd(PClient* client) { } } -void AppendCmd::DoThroughDB(PClient* client){ - DoCmd(client); -} +void AppendCmd::DoThroughDB(PClient* client) { DoCmd(client); } void AppendCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key_=client->Key(); + auto key_ = client->Key(); PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Appendxx(key_, client->argv_[2]); } } GetSetCmd::GetSetCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryString) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryString) {} bool GetSetCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -188,8 +189,7 @@ bool GetSetCmd::DoInitial(PClient* client) { void GetSetCmd::DoCmd(PClient* client) { std::string old_value; - s_ = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetSet(client->Key(), client->argv_[2], &old_value); + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetSet(client->Key(), client->argv_[2], &old_value); if (s_.ok()) { if (old_value.empty()) { client->AppendContent("$-1"); @@ -202,19 +202,19 @@ void GetSetCmd::DoCmd(PClient* client) { } } -void GetSetCmd::DoThroughDB(PClient* client) { - DoCmd(client); -} +void GetSetCmd::DoThroughDB(PClient* client) { DoCmd(client); } void GetSetCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key_=client->Key(); + auto key_ = client->Key(); PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->SetxxWithoutTTL(key_, client->argv_[2]); } } MGetCmd::MGetCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsReadonly | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache | kCmdFlagsReadCache, kAclCategoryRead | kAclCategoryString) {} + : BaseCmd(name, arity, + kCmdFlagsReadonly | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache | kCmdFlagsReadCache, + kAclCategoryRead | kAclCategoryString) {} bool MGetCmd::DoInitial(PClient* client) { std::vector keys(client->argv_.begin(), client->argv_.end()); @@ -224,8 +224,8 @@ bool MGetCmd::DoInitial(PClient* client) { } void MGetCmd::DoCmd(PClient* client) { - db_value_status_array_.clear(); - s_ =PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->MGet(client->Keys(), &db_value_status_array_); + db_value_status_array_.clear(); + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->MGet(client->Keys(), &db_value_status_array_); if (s_.ok()) { client->AppendArrayLen(db_value_status_array_.size()); for (const auto& vs : db_value_status_array_) { @@ -242,7 +242,7 @@ void MGetCmd::DoCmd(PClient* client) { } void MGetCmd::ReadCache(PClient* client) { - auto keys_=client->Keys(); + auto keys_ = client->Keys(); if (1 < keys_.size()) { client->SetRes(CmdRes::kCacheMiss); return; @@ -264,16 +264,19 @@ void MGetCmd::DoThroughDB(PClient* client) { } void MGetCmd::DoUpdateCache(PClient* client) { - auto keys_=client->Keys(); + auto keys_ = client->Keys(); for (size_t i = 0; i < keys_.size(); i++) { if (db_value_status_array_[i].status.ok()) { - PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->WriteKVToCache(keys_[i], db_value_status_array_[i].value, db_value_status_array_[i].ttl); + PSTORE.GetBackend(client->GetCurrentDB()) + ->GetCache() + ->WriteKVToCache(keys_[i], db_value_status_array_[i].value, db_value_status_array_[i].ttl); } } } MSetCmd::MSetCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryString) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryString) {} bool MSetCmd::DoInitial(PClient* client) { size_t argcSize = client->argv_.size(); @@ -297,13 +300,10 @@ void MSetCmd::DoCmd(PClient* client) { } } -void MSetCmd::DoThroughDB(PClient* client) { - DoCmd(client); -} +void MSetCmd::DoThroughDB(PClient* client) { DoCmd(client); } void MSetCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - std::string CachePrefixKeyK; for (auto key : kvs_) { PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->SetxxWithoutTTL(key.key, key.value); } @@ -352,7 +352,8 @@ void BitCountCmd::DoCmd(PClient* client) { } DecrCmd::DecrCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryRead | kAclCategoryString) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryRead | kAclCategoryString) {} bool DecrCmd::DoInitial(pikiwidb::PClient* client) { client->SetKey(client->argv_[1]); @@ -373,19 +374,18 @@ void DecrCmd::DoCmd(pikiwidb::PClient* client) { } } -void DecrCmd::DoThroughDB(PClient* client) { - DoCmd(client); -} +void DecrCmd::DoThroughDB(PClient* client) { DoCmd(client); } void DecrCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key_=client->Key(); + auto key_ = client->Key(); PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Decrxx(key_); } } IncrCmd::IncrCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryString) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryString) {} bool IncrCmd::DoInitial(pikiwidb::PClient* client) { client->SetKey(client->argv_[1]); @@ -394,7 +394,7 @@ bool IncrCmd::DoInitial(pikiwidb::PClient* client) { void IncrCmd::DoCmd(pikiwidb::PClient* client) { int64_t ret = 0; - s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Incrby(client->Key(), 1, &ret); + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Incrby(client->Key(), 1, &ret); if (s_.ok()) { client->AppendContent(":" + std::to_string(ret)); } else if (s_.IsCorruption() && s_.ToString() == "Corruption: Value is not a integer") { @@ -406,13 +406,11 @@ void IncrCmd::DoCmd(pikiwidb::PClient* client) { } } -void IncrCmd::DoThroughDB(PClient* client) { - DoCmd(client); -} +void IncrCmd::DoThroughDB(PClient* client) { DoCmd(client); } void IncrCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key_=client->Key(); + auto key_ = client->Key(); PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Incrxx(key_); } } @@ -478,7 +476,9 @@ void BitOpCmd::DoCmd(PClient* client) { } StrlenCmd::StrlenCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsReadonly | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache | kCmdFlagsReadCache, kAclCategoryRead | kAclCategoryString) {} + : BaseCmd(name, arity, + kCmdFlagsReadonly | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache | kCmdFlagsReadCache, + kAclCategoryRead | kAclCategoryString) {} bool StrlenCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -499,8 +499,8 @@ void StrlenCmd::DoCmd(PClient* client) { void StrlenCmd::ReadCache(PClient* client) { int32_t len = 0; - auto key=client->Key(); - auto s= PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Strlen(key, &len); + auto key = client->Key(); + auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Strlen(key, &len); if (s.ok()) { client->AppendInteger(len); } else { @@ -510,7 +510,7 @@ void StrlenCmd::ReadCache(PClient* client) { void StrlenCmd::DoThroughDB(PClient* client) { client->Clear(); - auto key=client->Key(); + auto key = client->Key(); s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetWithTTL(key, &value_, &sec_); if (s_.ok() || s_.IsNotFound()) { client->AppendInteger(value_.size()); @@ -521,13 +521,14 @@ void StrlenCmd::DoThroughDB(PClient* client) { void StrlenCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key=client->Key(); + auto key = client->Key(); PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->WriteKVToCache(key, value_, sec_); } } SetExCmd::SetExCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryString) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryString) {} bool SetExCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -539,7 +540,7 @@ bool SetExCmd::DoInitial(PClient* client) { } void SetExCmd::DoCmd(PClient* client) { - s_ =PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Setex(client->Key(), client->argv_[3], sec_); + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Setex(client->Key(), client->argv_[3], sec_); if (s_.ok()) { client->SetRes(CmdRes::kOK); } else if (s_.IsInvalidArgument()) { @@ -549,23 +550,22 @@ void SetExCmd::DoCmd(PClient* client) { } } -void SetExCmd::DoThroughDB(PClient* client) { - DoCmd(client); -} +void SetExCmd::DoThroughDB(PClient* client) { DoCmd(client); } void SetExCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key=client->Key(); + auto key = client->Key(); PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Setxx(key, client->argv_[3], sec_); } } PSetExCmd::PSetExCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryString) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryString) {} bool PSetExCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); - + if (pstd::String2int(client->argv_[2], &msec_) == 0) { client->SetRes(CmdRes::kInvalidInt); return false; @@ -575,8 +575,8 @@ bool PSetExCmd::DoInitial(PClient* client) { void PSetExCmd::DoCmd(PClient* client) { s_ = PSTORE.GetBackend(client->GetCurrentDB()) - ->GetStorage() - ->Setex(client->Key(), client->argv_[3], static_cast(msec_ / 1000)); + ->GetStorage() + ->Setex(client->Key(), client->argv_[3], static_cast(msec_ / 1000)); if (s_.ok()) { client->SetRes(CmdRes::kOK); } else if (s_.IsInvalidArgument()) { @@ -586,19 +586,18 @@ void PSetExCmd::DoCmd(PClient* client) { } } -void PSetExCmd::DoThroughDB(PClient* client) { - DoCmd(client); -} +void PSetExCmd::DoThroughDB(PClient* client) { DoCmd(client); } void PSetExCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key=client->Key(); - PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Setxx(key, client->argv_[3], msec_ / 1000); + auto key = client->Key(); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Setxx(key, client->argv_[3], msec_ / 1000); } } IncrbyCmd::IncrbyCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryString) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryString) {} bool IncrbyCmd::DoInitial(PClient* client) { int64_t by_ = 0; @@ -613,7 +612,7 @@ bool IncrbyCmd::DoInitial(PClient* client) { void IncrbyCmd::DoCmd(PClient* client) { int64_t ret = 0; pstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &by_); - s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Incrby(client->Key(), by_, &ret); + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Incrby(client->Key(), by_, &ret); if (s_.ok()) { client->AppendContent(":" + std::to_string(ret)); } else if (s_.IsCorruption() && s_.ToString() == "Corruption: Value is not a integer") { @@ -628,19 +627,18 @@ void IncrbyCmd::DoCmd(PClient* client) { }; } -void IncrbyCmd::DoThroughDB(PClient* client) { - DoCmd(client); -} +void IncrbyCmd::DoThroughDB(PClient* client) { DoCmd(client); } void IncrbyCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key_=client->Key(); + auto key_ = client->Key(); PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->IncrByxx(key_, by_); } } DecrbyCmd::DecrbyCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryString) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryString) {} bool DecrbyCmd::DoInitial(PClient* client) { int64_t by = 0; @@ -654,12 +652,12 @@ bool DecrbyCmd::DoInitial(PClient* client) { void DecrbyCmd::DoCmd(PClient* client) { int64_t ret = 0; - + if (pstd::String2int(client->argv_[2].data(), client->argv_[2].size(), &by_) == 0) { client->SetRes(CmdRes::kInvalidInt); return; } - s_= PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Decrby(client->Key(), by_, &ret); + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Decrby(client->Key(), by_, &ret); if (s_.ok()) { client->AppendContent(":" + std::to_string(ret)); } else if (s_.IsCorruption() && s_.ToString() == "Corruption: Value is not a integer") { @@ -674,19 +672,18 @@ void DecrbyCmd::DoCmd(PClient* client) { } } -void DecrbyCmd::DoThroughDB(PClient* client) { - DoCmd(client); -} +void DecrbyCmd::DoThroughDB(PClient* client) { DoCmd(client); } void DecrbyCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key_=client->Key(); + auto key_ = client->Key(); PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->DecrByxx(key_, by_); } } IncrbyFloatCmd::IncrbyFloatCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryString) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryString) {} bool IncrbyFloatCmd::DoInitial(PClient* client) { long double by_ = 0.00f; @@ -695,13 +692,13 @@ bool IncrbyFloatCmd::DoInitial(PClient* client) { return false; } client->SetKey(client->argv_[1]); - value_=client->argv_[2]; + value_ = client->argv_[2]; return true; } void IncrbyFloatCmd::DoCmd(PClient* client) { PString ret; - s_ =PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Incrbyfloat(client->Key(), value_, &ret); + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->Incrbyfloat(client->Key(), value_, &ret); if (s_.ok()) { client->AppendStringLen(ret.size()); client->AppendContent(ret); @@ -717,15 +714,13 @@ void IncrbyFloatCmd::DoCmd(PClient* client) { } } -void IncrbyFloatCmd::DoThroughDB(PClient* client) { - DoCmd(client); -} +void IncrbyFloatCmd::DoThroughDB(PClient* client) { DoCmd(client); } void IncrbyFloatCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { long double long_double_by; if (StrToLongDouble(value_.data(), value_.size(), &long_double_by) != -1) { - auto key_=client->Key(); + auto key_ = client->Key(); PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->Incrbyfloatxx(key_, long_double_by); } } @@ -778,7 +773,9 @@ void GetBitCmd::DoCmd(PClient* client) { } GetRangeCmd::GetRangeCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsReadonly | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache | kCmdFlagsReadCache, kAclCategoryRead | kAclCategoryString) {} + : BaseCmd(name, arity, + kCmdFlagsReadonly | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache | kCmdFlagsReadCache, + kAclCategoryRead | kAclCategoryString) {} bool GetRangeCmd::DoInitial(PClient* client) { // > range key start end @@ -815,7 +812,7 @@ void GetRangeCmd::DoCmd(PClient* client) { void GetRangeCmd::ReadCache(PClient* client) { std::string substr; - auto key=client->Key(); + auto key = client->Key(); auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->GetRange(key, start_, end_, &substr); if (s.ok()) { client->AppendStringLen(substr.size()); @@ -826,10 +823,12 @@ void GetRangeCmd::ReadCache(PClient* client) { } void GetRangeCmd::DoThroughDB(PClient* client) { - client->Clear(); + client->Clear(); std::string substr; - auto key=client->Key(); - s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetrangeWithValue(key, start_, end_, &substr, &value_, &sec_); + auto key = client->Key(); + s_ = PSTORE.GetBackend(client->GetCurrentDB()) + ->GetStorage() + ->GetrangeWithValue(key, start_, end_, &substr, &value_, &sec_); if (s_.ok()) { client->AppendStringLen(substr.size()); client->AppendContent(substr); @@ -843,8 +842,8 @@ void GetRangeCmd::DoThroughDB(PClient* client) { void GetRangeCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key=client->Key(); - PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->WriteKVToCache(key, value_, sec_); + auto key = client->Key(); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->WriteKVToCache(key, value_, sec_); } } @@ -893,7 +892,8 @@ void SetBitCmd::DoCmd(PClient* client) { } SetRangeCmd::SetRangeCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, kAclCategoryWrite | kAclCategoryString) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsKv | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryString) {} bool SetRangeCmd::DoInitial(PClient* client) { // setrange key offset value @@ -920,13 +920,11 @@ void SetRangeCmd::DoCmd(PClient* client) { client->AppendInteger(static_cast(ret)); } -void SetRangeCmd::DoThroughDB(PClient* client) { - DoCmd(client); -} +void SetRangeCmd::DoThroughDB(PClient* client) { DoCmd(client); } void SetRangeCmd::DoUpdateCache(PClient* client) { if (s_.ok()) { - auto key=client->Key(); + auto key = client->Key(); PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->SetRangexx(key, offset_, client->argv_[3]); } } @@ -963,5 +961,4 @@ void MSetnxCmd::DoCmd(PClient* client) { client->SetRes(CmdRes::kErrOther, s.ToString()); } } - } // namespace pikiwidb diff --git a/src/cmd_kv.h b/src/cmd_kv.h index a0fa5c62f..c7ad72fe3 100644 --- a/src/cmd_kv.h +++ b/src/cmd_kv.h @@ -19,16 +19,14 @@ class GetCmd : public BaseCmd { bool DoInitial(PClient *client) override; private: - std::string value_; - rocksdb::Status s_; - int64_t ttl_ = 0; + std::string value_; + rocksdb::Status s_; + int64_t ttl_ = 0; void DoCmd(PClient *client) override; - void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; - void ReadCache(PClient *client) override; - - + void DoThroughDB(PClient *client) override; + void DoUpdateCache(PClient *client) override; + void ReadCache(PClient *client) override; }; class SetCmd : public BaseCmd { @@ -38,12 +36,11 @@ class SetCmd : public BaseCmd { protected: bool DoInitial(PClient *client) override; - private: void DoCmd(PClient *client) override; void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + void DoUpdateCache(PClient *client) override; std::string value_; bool has_ttl_ = false; @@ -79,12 +76,12 @@ class StrlenCmd : public BaseCmd { private: void DoCmd(PClient *client) override; - void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; - void ReadCache(PClient *client) override; - rocksdb::Status s_; - int64_t sec_ = 0; - std::string value_; + void DoThroughDB(PClient *client) override; + void DoUpdateCache(PClient *client) override; + void ReadCache(PClient *client) override; + rocksdb::Status s_; + int64_t sec_ = 0; + std::string value_; }; class SetExCmd : public BaseCmd { @@ -95,11 +92,11 @@ class SetExCmd : public BaseCmd { bool DoInitial(PClient *client) override; private: - storage::Status s_; - int64_t sec_ = 0; + storage::Status s_; + int64_t sec_ = 0; void DoCmd(PClient *client) override; - void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + void DoThroughDB(PClient *client) override; + void DoUpdateCache(PClient *client) override; }; class PSetExCmd : public BaseCmd { @@ -110,11 +107,11 @@ class PSetExCmd : public BaseCmd { bool DoInitial(PClient *client) override; private: - int64_t msec_ = 0; - storage::Status s_; + int64_t msec_ = 0; + storage::Status s_; void DoCmd(PClient *client) override; void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + void DoUpdateCache(PClient *client) override; }; class SetNXCmd : public BaseCmd { @@ -136,10 +133,10 @@ class AppendCmd : public BaseCmd { bool DoInitial(PClient *client) override; private: - storage::Status s_; + storage::Status s_; void DoCmd(PClient *client) override; void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + void DoUpdateCache(PClient *client) override; }; class GetSetCmd : public BaseCmd { @@ -150,10 +147,10 @@ class GetSetCmd : public BaseCmd { bool DoInitial(PClient *client) override; private: - storage::Status s_; + storage::Status s_; void DoCmd(PClient *client) override; void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + void DoUpdateCache(PClient *client) override; }; class MGetCmd : public BaseCmd { @@ -164,12 +161,12 @@ class MGetCmd : public BaseCmd { bool DoInitial(PClient *client) override; private: - storage::Status s_; - std::vector db_value_status_array_; + storage::Status s_; + std::vector db_value_status_array_; void DoCmd(PClient *client) override; void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; - void ReadCache(PClient *client) override; + void DoUpdateCache(PClient *client) override; + void ReadCache(PClient *client) override; }; class MSetCmd : public BaseCmd { @@ -180,11 +177,11 @@ class MSetCmd : public BaseCmd { bool DoInitial(PClient *client) override; private: - std::vector kvs_; - storage::Status s_; + std::vector kvs_; + storage::Status s_; void DoCmd(PClient *client) override; void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + void DoUpdateCache(PClient *client) override; }; class BitCountCmd : public BaseCmd { @@ -206,10 +203,10 @@ class DecrCmd : public BaseCmd { bool DoInitial(PClient *client) override; private: - storage::Status s_; + storage::Status s_; void DoCmd(PClient *client) override; void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + void DoUpdateCache(PClient *client) override; }; class IncrCmd : public BaseCmd { @@ -220,10 +217,10 @@ class IncrCmd : public BaseCmd { bool DoInitial(PClient *client) override; private: - rocksdb::Status s_; + rocksdb::Status s_; void DoCmd(PClient *client) override; void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + void DoUpdateCache(PClient *client) override; }; class IncrbyCmd : public BaseCmd { @@ -232,14 +229,13 @@ class IncrbyCmd : public BaseCmd { protected: bool DoInitial(PClient *client) override; - private: - rocksdb::Status s_; - int64_t by_ = 0; + rocksdb::Status s_; + int64_t by_ = 0; void DoCmd(PClient *client) override; void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + void DoUpdateCache(PClient *client) override; }; class DecrbyCmd : public BaseCmd { public: @@ -249,11 +245,11 @@ class DecrbyCmd : public BaseCmd { bool DoInitial(PClient *client) override; private: - int64_t by_ = 0; - storage::Status s_; + int64_t by_ = 0; + storage::Status s_; void DoCmd(PClient *client) override; void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + void DoUpdateCache(PClient *client) override; }; class SetBitCmd : public BaseCmd { @@ -286,11 +282,11 @@ class IncrbyFloatCmd : public BaseCmd { bool DoInitial(PClient *client) override; private: - storage::Status s_; - std::string value_; + storage::Status s_; + std::string value_; void DoCmd(PClient *client) override; - void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + void DoThroughDB(PClient *client) override; + void DoUpdateCache(PClient *client) override; }; class GetRangeCmd : public BaseCmd { @@ -301,15 +297,15 @@ class GetRangeCmd : public BaseCmd { bool DoInitial(PClient *client) override; private: - int64_t start_ = 0; + int64_t start_ = 0; int64_t end_ = 0; storage::Status s_; std::string value_; int64_t sec_ = 0; void DoCmd(PClient *client) override; void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; - void ReadCache(PClient *client) override; + void DoUpdateCache(PClient *client) override; + void ReadCache(PClient *client) override; }; class SetRangeCmd : public BaseCmd { @@ -320,11 +316,11 @@ class SetRangeCmd : public BaseCmd { bool DoInitial(PClient *client) override; private: - storage::Status s_; - int64_t offset_ = 0; + storage::Status s_; + int64_t offset_ = 0; void DoCmd(PClient *client) override; void DoThroughDB(PClient *client) override; - void DoUpdateCache(PClient *client) override; + void DoUpdateCache(PClient *client) override; }; class MSetnxCmd : public BaseCmd { diff --git a/src/cmd_list.cc b/src/cmd_list.cc index c7c92abf3..d6d82fcf2 100644 --- a/src/cmd_list.cc +++ b/src/cmd_list.cc @@ -11,7 +11,8 @@ namespace pikiwidb { LPushCmd::LPushCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryList) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryList) {} bool LPushCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -21,19 +22,29 @@ bool LPushCmd::DoInitial(PClient* client) { void LPushCmd::DoCmd(PClient* client) { std::vector list_values(client->argv_.begin() + 2, client->argv_.end()); uint64_t reply_num = 0; - storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LPush(client->Key(), list_values, &reply_num); - if (s.ok()) { + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LPush(client->Key(), list_values, &reply_num); + if (s_.ok()) { client->AppendInteger(reply_num); - } else if (s.IsInvalidArgument()) { + } else if (s_.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); } else { client->SetRes(CmdRes::kSyntaxErr, "lpush cmd error"); } } +void LPushCmd::DoThroughDB(PClient* client) { DoCmd(client); } + +void LPushCmd::DoUpdateCache(PClient* client) { + if (s_.ok()) { + auto key = client->Key(); + std::vector list_values(client->argv_.begin() + 2, client->argv_.end()); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->LPushx(key, list_values); + } +} + LPushxCmd::LPushxCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryList) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsList | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryList) {} bool LPushxCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -43,14 +54,23 @@ bool LPushxCmd::DoInitial(PClient* client) { void LPushxCmd::DoCmd(PClient* client) { std::vector list_values(client->argv_.begin() + 2, client->argv_.end()); uint64_t reply_num = 0; - storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LPushx(client->Key(), list_values, &reply_num); - if (s.ok() || s.IsNotFound()) { + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LPushx(client->Key(), list_values, &reply_num); + if (s_.ok() || s_.IsNotFound()) { client->AppendInteger(reply_num); - } else if (s.IsInvalidArgument()) { + } else if (s_.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); } else { - client->SetRes(CmdRes::kErrOther, s.ToString()); + client->SetRes(CmdRes::kErrOther, s_.ToString()); + } +} + +void LPushxCmd::DoThroughDB(PClient* client) { DoCmd(client); } + +void LPushxCmd::DoUpdateCache(PClient* client) { + if (s_.ok()) { + auto key = client->Key(); + std::vector list_values(client->argv_.begin() + 2, client->argv_.end()); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->LPushx(key, list_values); } } @@ -82,7 +102,8 @@ void RPoplpushCmd::DoCmd(PClient* client) { } RPushCmd::RPushCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryList) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsList | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryList) {} bool RPushCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -92,19 +113,29 @@ bool RPushCmd::DoInitial(PClient* client) { void RPushCmd::DoCmd(PClient* client) { std::vector list_values(client->argv_.begin() + 2, client->argv_.end()); uint64_t reply_num = 0; - storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->RPush(client->Key(), list_values, &reply_num); - if (s.ok()) { + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->RPush(client->Key(), list_values, &reply_num); + if (s_.ok()) { client->AppendInteger(reply_num); - } else if (s.IsInvalidArgument()) { + } else if (s_.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); } else { client->SetRes(CmdRes::kSyntaxErr, "rpush cmd error"); } } +void RPushCmd::DoThroughDB(PClient* client) { DoCmd(client); } + +void RPushCmd::DoUpdateCache(PClient* client) { + if (s_.ok()) { + auto key = client->Key(); + std::vector list_values(client->argv_.begin() + 2, client->argv_.end()); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->RPushx(key, list_values); + } +} + RPushxCmd::RPushxCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryList) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsList | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryList) {} bool RPushxCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -114,19 +145,29 @@ bool RPushxCmd::DoInitial(PClient* client) { void RPushxCmd::DoCmd(PClient* client) { std::vector list_values(client->argv_.begin() + 2, client->argv_.end()); uint64_t reply_num = 0; - storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->RPushx(client->Key(), list_values, &reply_num); - if (s.ok() || s.IsNotFound()) { + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->RPushx(client->Key(), list_values, &reply_num); + if (s_.ok() || s_.IsNotFound()) { client->AppendInteger(reply_num); - } else if (s.IsInvalidArgument()) { + } else if (s_.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); } else { - client->SetRes(CmdRes::kErrOther, s.ToString()); + client->SetRes(CmdRes::kErrOther, s_.ToString()); + } +} + +void RPushxCmd::DoThroughDB(PClient* client) { DoCmd(client); } + +void RPushxCmd::DoUpdateCache(PClient* client) { + if (s_.ok()) { + auto key = client->Key(); + std::vector list_values(client->argv_.begin() + 2, client->argv_.end()); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->RPushx(key, list_values); } } LPopCmd::LPopCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryList) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsList | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryList) {} bool LPopCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -135,20 +176,31 @@ bool LPopCmd::DoInitial(PClient* client) { void LPopCmd::DoCmd(PClient* client) { std::vector elements; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LPop(client->Key(), 1, &elements); - if (s.ok()) { + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LPop(client->Key(), 1, &elements); + if (s_.ok()) { client->AppendString(elements[0]); - } else if (s.IsNotFound()) { + } else if (s_.IsNotFound()) { client->AppendStringLen(-1); - } else if (s.IsInvalidArgument()) { + } else if (s_.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); } else { - client->SetRes(CmdRes::kErrOther, s.ToString()); + client->SetRes(CmdRes::kErrOther, s_.ToString()); + } +} + +void LPopCmd::DoThroughDB(PClient* client) { DoCmd(client); } + +void LPopCmd::DoUpdateCache(PClient* client) { + if (s_.ok()) { + auto key = client->Key(); + std::string value; + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->LPop(key, &value); } } RPopCmd::RPopCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryList) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsList | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryList) {} bool RPopCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -157,38 +209,47 @@ bool RPopCmd::DoInitial(PClient* client) { void RPopCmd::DoCmd(PClient* client) { std::vector elements; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->RPop(client->Key(), 1, &elements); - if (s.ok()) { + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->RPop(client->Key(), 1, &elements); + if (s_.ok()) { client->AppendString(elements[0]); - } else if (s.IsNotFound()) { + } else if (s_.IsNotFound()) { client->AppendStringLen(-1); - } else if (s.IsInvalidArgument()) { + } else if (s_.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); } else { client->SetRes(CmdRes::kSyntaxErr, "rpop cmd error"); } } +void RPopCmd::DoThroughDB(PClient* client) { DoCmd(client); } + +void RPopCmd::DoUpdateCache(PClient* client) { + if (s_.ok()) { + std::string value; + auto key = client->Key(); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->RPop(key, &value); + } +} + LRangeCmd::LRangeCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsReadonly, kAclCategoryRead | kAclCategoryList) {} + : BaseCmd(name, arity, + kCmdFlagsReadonly | kCmdFlagsList | kCmdFlagsDoThroughDB | kCmdFlagsReadCache | kCmdFlagsUpdateCache, + kAclCategoryRead | kAclCategoryList) {} bool LRangeCmd::DoInitial(PClient* client) { + if (pstd::String2int(client->argv_[2], &start_index_) == 0 || pstd::String2int(client->argv_[3], &end_index_) == 0) { + client->SetRes(CmdRes::kInvalidInt); + return false; + } client->SetKey(client->argv_[1]); return true; } void LRangeCmd::DoCmd(PClient* client) { std::vector ret; - int64_t start_index = 0; - int64_t end_index = 0; - if (pstd::String2int(client->argv_[2], &start_index) == 0 || pstd::String2int(client->argv_[3], &end_index) == 0) { - client->SetRes(CmdRes::kInvalidInt); - return; - } - storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LRange(client->Key(), start_index, end_index, &ret); - if (!s.ok() && !s.IsNotFound()) { - if (s.IsInvalidArgument()) { + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LRange(client->Key(), start_index_, end_index_, &ret); + if (!s_.ok() && !s_.IsNotFound()) { + if (s_.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); } else { client->SetRes(CmdRes::kSyntaxErr, "lrange cmd error"); @@ -198,121 +259,170 @@ void LRangeCmd::DoCmd(PClient* client) { client->AppendStringVector(ret); } +void LRangeCmd::ReadCache(PClient* client) { + std::vector values; + auto key = client->Key(); + auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->LRange(key, start_index_, end_index_, &values); + if (s.ok()) { + client->AppendArrayLen(values.size()); + for (const auto& value : values) { + client->AppendString(value); + } + } else if (s.IsNotFound()) { + client->SetRes(CmdRes::kCacheMiss); + } else { + client->SetRes(CmdRes::kErrOther, s.ToString()); + } +} + +void LRangeCmd::DoThroughDB(PClient* client) { + client->Clear(); + DoCmd(client); +} + +void LRangeCmd::DoUpdateCache(PClient* client) { + if (s_.ok()) { + auto key = client->Key(); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->PushKeyToAsyncLoadQueue(KEY_TYPE_LIST, key, client); + } +} + LRemCmd::LRemCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryList) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsList | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryList) {} bool LRemCmd::DoInitial(PClient* client) { + if (pstd::String2int(client->argv_[2], &freq_) == 0) { + client->SetRes(CmdRes::kInvalidInt); + return false; + } client->SetKey(client->argv_[1]); return true; } void LRemCmd::DoCmd(PClient* client) { - int64_t freq_ = 0; - std::string count = client->argv_[2]; - if (pstd::String2int(count, &freq_) == 0) { - client->SetRes(CmdRes::kInvalidInt); - return; - } - uint64_t reply_num = 0; - storage::Status s = + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LRem(client->Key(), freq_, client->argv_[3], &reply_num); - if (s.ok() || s.IsNotFound()) { + if (s_.ok() || s_.IsNotFound()) { client->AppendInteger(reply_num); - } else if (s.IsInvalidArgument()) { + } else if (s_.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); } else { client->SetRes(CmdRes::kErrOther, "lrem cmd error"); } } +void LRemCmd::DoThroughDB(PClient* client) { DoCmd(client); } + +void LRemCmd::DoUpdateCache(PClient* client) { + if (s_.ok()) { + auto key = client->Key(); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->LRem(key, freq_, client->argv_[3]); + } +} + LTrimCmd::LTrimCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryList) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsList | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryList) {} bool LTrimCmd::DoInitial(PClient* client) { + if (pstd::String2int(client->argv_[2], &start_index_) == 0 || pstd::String2int(client->argv_[3], &end_index_) == 0) { + client->SetRes(CmdRes::kInvalidInt); + return false; + } client->SetKey(client->argv_[1]); return true; } void LTrimCmd::DoCmd(PClient* client) { - int64_t start_index = 0; - int64_t end_index = 0; - - if (pstd::String2int(client->argv_[2], &start_index) == 0 || pstd::String2int(client->argv_[3], &end_index) == 0) { - client->SetRes(CmdRes::kInvalidInt); - return; - } - storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LTrim(client->Key(), start_index, end_index); - if (s.ok() || s.IsNotFound()) { + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LTrim(client->Key(), start_index_, end_index_); + if (s_.ok() || s_.IsNotFound()) { client->SetRes(CmdRes::kOK); - } else if (s.IsInvalidArgument()) { + } else if (s_.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); } else { client->SetRes(CmdRes::kSyntaxErr, "ltrim cmd error"); } } +void LTrimCmd::DoThroughDB(PClient* client) { DoCmd(client); } + +void LTrimCmd::DoUpdateCache(PClient* client) { + if (s_.ok()) { + auto key = client->Key(); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->LTrim(key, start_index_, end_index_); + } +} + LSetCmd::LSetCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryList) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsList | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryList) {} bool LSetCmd::DoInitial(PClient* client) { + // isValidNumber ensures that the string is in decimal format, + // while strtol ensures that the string is within the range of long type + const std::string index_str = client->argv_[2]; + if (!pstd::IsValidNumber(index_str)) { + client->SetRes(CmdRes::kInvalidInt); + return false; + } + if (1 != pstd::String2int(index_str, &index_)) { + client->SetRes(CmdRes::kErrOther, "lset cmd error"); // this will not happend in normal case + return false; + } client->SetKey(client->argv_[1]); return true; } void LSetCmd::DoCmd(PClient* client) { - // isValidNumber ensures that the string is in decimal format, - // while strtol ensures that the string is within the range of long type - const std::string index_str = client->argv_[2]; - - if (pstd::IsValidNumber(index_str)) { - int64_t val = 0; - if (1 != pstd::String2int(index_str, &val)) { - client->SetRes(CmdRes::kErrOther, "lset cmd error"); // this will not happend in normal case - return; - } - storage::Status s = - PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LSet(client->Key(), val, client->argv_[3]); - if (s.ok()) { - client->SetRes(CmdRes::kOK); - } else if (s.IsNotFound()) { - client->SetRes(CmdRes::kNotFound); - } else if (s.IsCorruption()) { - client->SetRes(CmdRes::kOutOfRange); - } else if (s.IsInvalidArgument()) { - client->SetRes(CmdRes::kMultiKey); - } else { - client->SetRes(CmdRes::kSyntaxErr, "lset cmd error"); // just a safeguard - } + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LSet(client->Key(), index_, client->argv_[3]); + if (s_.ok()) { + client->SetRes(CmdRes::kOK); + } else if (s_.IsNotFound()) { + client->SetRes(CmdRes::kNotFound); + } else if (s_.IsCorruption()) { + client->SetRes(CmdRes::kOutOfRange); + } else if (s_.IsInvalidArgument()) { + client->SetRes(CmdRes::kMultiKey); } else { - client->SetRes(CmdRes::kInvalidInt); + client->SetRes(CmdRes::kSyntaxErr, "lset cmd error"); // just a safeguard + } +} + +void LSetCmd::DoThroughDB(PClient* client) { DoCmd(client); } + +void LSetCmd::DoUpdateCache(PClient* client) { + if (s_.ok()) { + auto key = client->Key(); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->LSet(key, index_, client->argv_[3]); } } LInsertCmd::LInsertCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryList) {} + : BaseCmd(name, arity, kCmdFlagsWrite | kCmdFlagsList | kCmdFlagsDoThroughDB | kCmdFlagsUpdateCache, + kAclCategoryWrite | kAclCategoryList) {} bool LInsertCmd::DoInitial(PClient* client) { if (!pstd::StringEqualCaseInsensitive(client->argv_[2], "BEFORE") && !pstd::StringEqualCaseInsensitive(client->argv_[2], "AFTER")) { return false; } + before_or_after_ = storage::Before; + if (pstd::StringEqualCaseInsensitive(client->argv_[2], "AFTER")) { + before_or_after_ = storage::After; + } client->SetKey(client->argv_[1]); return true; } void LInsertCmd::DoCmd(PClient* client) { int64_t ret = 0; - storage ::BeforeOrAfter before_or_after = storage::Before; - if (pstd::StringEqualCaseInsensitive(client->argv_[2], "AFTER")) { - before_or_after = storage::After; - } - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB()) - ->GetStorage() - ->LInsert(client->Key(), before_or_after, client->argv_[3], client->argv_[4], &ret); - if (!s.ok() && !s.IsNotFound()) { - if (s.IsInvalidArgument()) { + s_ = PSTORE.GetBackend(client->GetCurrentDB()) + ->GetStorage() + ->LInsert(client->Key(), before_or_after_, client->argv_[3], client->argv_[4], &ret); + if (!s_.ok() && !s_.IsNotFound()) { + if (s_.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); } else { client->SetRes(CmdRes::kSyntaxErr, "linsert cmd error"); @@ -322,37 +432,74 @@ void LInsertCmd::DoCmd(PClient* client) { client->AppendInteger(ret); } +void LInsertCmd::DoThroughDB(PClient* client) { DoCmd(client); } + +void LInsertCmd::DoUpdateCache(PClient* client) { + if (s_.ok()) { + auto key = client->Key(); + PSTORE.GetBackend(client->GetCurrentDB()) + ->GetCache() + ->LInsert(key, before_or_after_, client->argv_[3], client->argv_[4]); + } +} + LIndexCmd::LIndexCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsReadonly, kAclCategoryRead | kAclCategoryList) {} + : BaseCmd(name, arity, + kCmdFlagsReadonly | kCmdFlagsList | kCmdFlagsDoThroughDB | kCmdFlagsReadCache | kCmdFlagsUpdateCache, + kAclCategoryRead | kAclCategoryList) {} bool LIndexCmd::DoInitial(PClient* client) { + if (pstd::String2int(client->argv_[2], &index_) == 0) { + client->SetRes(CmdRes::kInvalidInt); + return false; + } client->SetKey(client->argv_[1]); return true; } void LIndexCmd::DoCmd(PClient* client) { - int64_t freq_ = 0; - std::string count = client->argv_[2]; - if (pstd::String2int(count, &freq_) == 0) { - client->SetRes(CmdRes::kInvalidInt); - return; + std::string value; + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LIndex(client->Key(), index_, &value); + if (s_.ok()) { + client->AppendString(value); + } else if (s_.IsNotFound()) { + client->AppendStringLen(-1); + } else if (s_.IsInvalidArgument()) { + client->SetRes(CmdRes::kMultiKey); + } else { + client->SetRes(CmdRes::kErrOther, s_.ToString()); } +} +void LIndexCmd::ReadCache(PClient* client) { std::string value; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LIndex(client->Key(), freq_, &value); + auto key = client->Key(); + auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->LIndex(key, index_, &value); if (s.ok()) { client->AppendString(value); } else if (s.IsNotFound()) { - client->AppendStringLen(-1); - } else if (s.IsInvalidArgument()) { - client->SetRes(CmdRes::kMultiKey); + client->SetRes(CmdRes::kCacheMiss); } else { client->SetRes(CmdRes::kErrOther, s.ToString()); } } +void LIndexCmd::DoUpdateCache(PClient* client) { + if (s_.ok()) { + auto key = client->Key(); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->PushKeyToAsyncLoadQueue(KEY_TYPE_LIST, key, client); + } +} + +void LIndexCmd::DoThroughDB(PClient* client) { + client->Clear(); + DoCmd(client); +} + LLenCmd::LLenCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, kCmdFlagsReadonly, kAclCategoryRead | kAclCategoryList) {} + : BaseCmd(name, arity, + kCmdFlagsReadonly | kCmdFlagsList | kCmdFlagsDoThroughDB | kCmdFlagsReadCache | kCmdFlagsUpdateCache, + kAclCategoryRead | kAclCategoryList) {} bool LLenCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -361,13 +508,38 @@ bool LLenCmd::DoInitial(PClient* client) { void LLenCmd::DoCmd(PClient* client) { uint64_t llen = 0; - storage::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LLen(client->Key(), &llen); - if (s.ok() || s.IsNotFound()) { + s_ = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LLen(client->Key(), &llen); + if (s_.ok() || s_.IsNotFound()) { client->AppendInteger(static_cast(llen)); - } else if (s.IsInvalidArgument()) { + } else if (s_.IsInvalidArgument()) { client->SetRes(CmdRes::kMultiKey); + } else { + client->SetRes(CmdRes::kErrOther, s_.ToString()); + } +} + +void LLenCmd::ReadCache(PClient* client) { + uint64_t llen = 0; + auto key = client->Key(); + auto s = PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->LLen(key, &llen); + if (s.ok()) { + client->AppendInteger(llen); + } else if (s.IsNotFound()) { + client->SetRes(CmdRes::kCacheMiss); } else { client->SetRes(CmdRes::kErrOther, s.ToString()); } } + +void LLenCmd::DoThroughDB(PClient* client) { + client->Clear(); + DoCmd(client); +} + +void LLenCmd::DoUpdateCache(PClient* client) { + if (s_.ok()) { + auto key = client->Key(); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->PushKeyToAsyncLoadQueue(KEY_TYPE_LIST, key, client); + } +} } // namespace pikiwidb diff --git a/src/cmd_list.h b/src/cmd_list.h index 0fa7c220b..5728685a7 100644 --- a/src/cmd_list.h +++ b/src/cmd_list.h @@ -18,6 +18,9 @@ class LPushCmd : public BaseCmd { private: void DoCmd(PClient* client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; + storage::Status s_; }; class RPushCmd : public BaseCmd { @@ -29,6 +32,9 @@ class RPushCmd : public BaseCmd { private: void DoCmd(PClient* client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; + storage::Status s_; }; class RPopCmd : public BaseCmd { @@ -40,6 +46,9 @@ class RPopCmd : public BaseCmd { private: void DoCmd(PClient* client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; + storage::Status s_; }; class LRangeCmd : public BaseCmd { public: @@ -50,6 +59,12 @@ class LRangeCmd : public BaseCmd { private: void DoCmd(PClient* client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; + void ReadCache(PClient* client) override; + int64_t start_index_ = 0; + int64_t end_index_ = 0; + storage::Status s_; }; class LRemCmd : public BaseCmd { @@ -61,6 +76,10 @@ class LRemCmd : public BaseCmd { private: void DoCmd(PClient* client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; + storage::Status s_; + int64_t freq_ = 0; }; class LTrimCmd : public BaseCmd { @@ -72,6 +91,11 @@ class LTrimCmd : public BaseCmd { private: void DoCmd(PClient* client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; + storage::Status s_; + int64_t start_index_ = 0; + int64_t end_index_ = 0; }; class LSetCmd : public BaseCmd { @@ -83,6 +107,10 @@ class LSetCmd : public BaseCmd { private: void DoCmd(PClient* client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; + storage::Status s_; + int64_t index_ = 0; }; class LInsertCmd : public BaseCmd { @@ -94,6 +122,10 @@ class LInsertCmd : public BaseCmd { private: void DoCmd(PClient* client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; + storage ::BeforeOrAfter before_or_after_; + storage::Status s_; }; class LPushxCmd : public BaseCmd { @@ -105,6 +137,9 @@ class LPushxCmd : public BaseCmd { private: void DoCmd(PClient* client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; + storage::Status s_; }; class RPushxCmd : public BaseCmd { @@ -116,6 +151,9 @@ class RPushxCmd : public BaseCmd { private: void DoCmd(PClient* client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; + storage::Status s_; }; class RPoplpushCmd : public BaseCmd { @@ -143,6 +181,9 @@ class LPopCmd : public BaseCmd { private: void DoCmd(PClient* client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; + storage::Status s_; }; class LIndexCmd : public BaseCmd { @@ -154,6 +195,11 @@ class LIndexCmd : public BaseCmd { private: void DoCmd(PClient* client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; + void ReadCache(PClient* client) override; + int64_t index_ = 0; + storage::Status s_; }; class LLenCmd : public BaseCmd { @@ -165,5 +211,9 @@ class LLenCmd : public BaseCmd { private: void DoCmd(PClient* client) override; + void DoThroughDB(PClient* client) override; + void DoUpdateCache(PClient* client) override; + void ReadCache(PClient* client) override; + storage::Status s_; }; } // namespace pikiwidb diff --git a/src/config.cc b/src/config.cc index bc8736bae..cf8d221f7 100644 --- a/src/config.cc +++ b/src/config.cc @@ -141,15 +141,14 @@ PConfig::PConfig() { // cache config //@tobechecked:rewritable - AddNumberWihLimit("cache-num",true,&cache_num,1,48); - AddNumberWihLimit("cache-mode",true,&cache_mode,0,1); - AddNumber("zset-cache-field-num-per-key",true,&zset_cache_field_num_per_key); - AddNumber("zset-cache-start-direction",true,&zset_cache_start_direction); - AddNumber("cache-maxmemory",true,&cache_maxmemory); - AddNumber("cache-maxmemory-policy",true,&cache_maxmemory_policy); - AddNumber("cache-maxmemory-samples",true,&cache_maxmemory_samples); - AddNumber("cache-lfu-decay-time",true,&cache_lfu_decay_time); - + AddNumberWihLimit("cache-num", true, &cache_num, 1, 48); + AddNumberWihLimit("cache-mode", true, &cache_mode, 0, 1); + AddNumber("zset-cache-field-num-per-key", true, &zset_cache_field_num_per_key); + AddNumber("zset-cache-start-direction", true, &zset_cache_start_direction); + AddNumber("cache-maxmemory", true, &cache_maxmemory); + AddNumber("cache-maxmemory-policy", true, &cache_maxmemory_policy); + AddNumber("cache-maxmemory-samples", true, &cache_maxmemory_samples); + AddNumber("cache-lfu-decay-time", true, &cache_lfu_decay_time); } bool PConfig::LoadFromFile(const std::string& file_name) { @@ -186,7 +185,7 @@ bool PConfig::LoadFromFile(const std::string& file_name) { } std::string all_cache_type_str; - all_cache_type_str=parser_.GetData("cache-type"); + all_cache_type_str = parser_.GetData("cache-type"); SetCacheType(all_cache_type_str); return true; @@ -197,7 +196,7 @@ void PConfig::SetCacheType(const std::string& value) { if (value == "") { return; } - + std::string lower_value = value; pstd::StringToLower(lower_value); lower_value.erase(remove_if(lower_value.begin(), lower_value.end(), isspace), lower_value.end()); diff --git a/src/config.h b/src/config.h index 33cc19461..832e076c9 100644 --- a/src/config.h +++ b/src/config.h @@ -174,10 +174,10 @@ class PConfig { std::atomic_uint64_t rocksdb_ttl_second = 604800; // default 86400 * 7 std::atomic_uint64_t rocksdb_periodic_second = 259200; // default 86400 * 3 - // cache + // cache std::vector cache_type_all; std::atomic_bool tmp_cache_disable_flag = false; - std::atomic_uint64_t cache_maxmemory= 10737418240; + std::atomic_uint64_t cache_maxmemory = 10737418240; std::atomic_int cache_num = 5; std::atomic_int cache_mode = 1; std::atomic_int cache_string = 0; diff --git a/src/db.cc b/src/db.cc index 6c79cc80a..faa2dfb1c 100644 --- a/src/db.cc +++ b/src/db.cc @@ -17,10 +17,7 @@ extern pikiwidb::PConfig g_config; namespace pikiwidb { DB::DB(int db_index, const std::string& db_path) - : db_index_(db_index), db_path_(db_path + std::to_string(db_index_) + '/') - { - - } + : db_index_(db_index), db_path_(db_path + std::to_string(db_index_) + '/') {} DB::~DB() { INFO("DB{} is closing...", db_index_); } @@ -65,7 +62,8 @@ rocksdb::Status DB::Open() { INFO("Open DB{} success!", db_index_); // Cache should not influence the project running states, so cache init code is put after varibale opened_ assignment. - cache_ = std::make_unique(g_config.zset_cache_start_direction.load(), g_config.zset_cache_field_num_per_key.load()); + cache_ = std::make_unique(g_config.zset_cache_start_direction.load(), + g_config.zset_cache_field_num_per_key.load()); // Create cache cache::CacheConfig cache_cfg; CacheConfigInit(cache_cfg); diff --git a/src/db.h b/src/db.h index 87be7ad9f..acf143b31 100644 --- a/src/db.h +++ b/src/db.h @@ -10,14 +10,14 @@ #include #include +#include "pcache.h" #include "pstd/log.h" #include "pstd/noncopyable.h" #include "storage/storage.h" -#include "pcache.h" namespace pikiwidb { -//class PCache; -//class PCacheLoadThread; +// class PCache; +// class PCacheLoadThread; class DB { public: @@ -42,7 +42,7 @@ class DB { int GetDbIndex() { return db_index_; } - std::unique_ptr& GetCache(){return cache_;} + std::unique_ptr& GetCache() { return cache_; } void CacheConfigInit(cache::CacheConfig& cache_cfg); @@ -60,7 +60,6 @@ class DB { bool opened_ = false; std::unique_ptr cache_; - }; } // namespace pikiwidb diff --git a/src/pcache.cc b/src/pcache.cc index f8c2356db..66677a1ca 100644 --- a/src/pcache.cc +++ b/src/pcache.cc @@ -3,24 +3,18 @@ // 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 #include -#include #include -#include +#include -#include "db.h" +#include "cache/config.h" +#include "cache/redisCache.h" #include "pcache.h" #include "pcache_load_thread.h" -// #include "include/pika_server.h" -// #include "include/pika_slot_command.h" -// #include "pstd/include/pika_codis_slot.h" -#include "cache/redisCache.h" -#include "cache/config.h" #include "pstd/log.h" -// extern PikaServer* g_pika_server; -namespace pikiwidb{ +namespace pikiwidb { #define EXTEND_CACHE_SIZE(N) (N * 12 / 10) using rocksdb::Status; @@ -30,10 +24,8 @@ PCache::PCache(int zset_cache_start_direction, int zset_cache_field_num_per_key) cache_num_(0), zset_cache_start_direction_(zset_cache_start_direction), zset_cache_field_num_per_key_(EXTEND_CACHE_SIZE(zset_cache_field_num_per_key)) { - - cache_load_thread_ = std::make_unique (zset_cache_start_direction_, zset_cache_field_num_per_key_); + cache_load_thread_ = std::make_unique(zset_cache_start_direction_, zset_cache_field_num_per_key_); cache_load_thread_->StartThread(); - } PCache::~PCache() { @@ -71,7 +63,8 @@ void PCache::ResetConfig(cache::CacheConfig *cache_cfg) { std::lock_guard l(rwlock_); zset_cache_start_direction_ = cache_cfg->zset_cache_start_direction; zset_cache_field_num_per_key_ = EXTEND_CACHE_SIZE(cache_cfg->zset_cache_field_num_per_key); - WARN("zset-cache-start-direction: {} , zset_cache_field_num_per_key: {} ",zset_cache_start_direction_, zset_cache_field_num_per_key_); + WARN("zset-cache-start-direction: {} , zset_cache_field_num_per_key: {} ", zset_cache_start_direction_, + zset_cache_field_num_per_key_); cache::RedisCache::SetConfig(cache_cfg); } @@ -93,8 +86,8 @@ void PCache::Info(CacheInfo &info) { info.status = cache_status_; info.cache_num = cache_num_; info.used_memory = cache::RedisCache::GetUsedMemory(); - //info.async_load_keys_num = cache_load_thread_->AsyncLoadKeysNum(); -// info.waitting_load_keys_num = cache_load_thread_->WaittingLoadKeysNum(); + // info.async_load_keys_num = cache_load_thread_->AsyncLoadKeysNum(); + // info.waitting_load_keys_num = cache_load_thread_->WaittingLoadKeysNum(); cache::RedisCache::GetHitAndMissNum(&info.hits, &info.misses); for (uint32_t i = 0; i < caches_.size(); ++i) { std::lock_guard lm(*cache_mutexs_[i]); @@ -102,7 +95,7 @@ void PCache::Info(CacheInfo &info) { } } -bool PCache::Exists(std::string& key) { +bool PCache::Exists(std::string &key) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); return caches_[cache_index]->Exists(key); @@ -126,19 +119,19 @@ Status PCache::Del(const std::vector &keys) { return s; } -Status PCache::Expire(std::string& key, int64_t ttl) { +Status PCache::Expire(std::string &key, int64_t ttl) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); return caches_[cache_index]->Expire(key, ttl); } -Status PCache::Expireat(std::string& key, int64_t ttl) { +Status PCache::Expireat(std::string &key, int64_t ttl) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); return caches_[cache_index]->Expireat(key, ttl); } -Status PCache::TTL(std::string& key, int64_t *ttl) { +Status PCache::TTL(std::string &key, int64_t *ttl) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); return caches_[cache_index]->TTL(key, ttl); @@ -163,7 +156,7 @@ Status PCache::Persist(std::string &key) { return caches_[cache_index]->Persist(key); } -Status PCache::Type(std::string& key, std::string *value) { +Status PCache::Type(std::string &key, std::string *value) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); return caches_[cache_index]->Type(key, value); @@ -188,37 +181,37 @@ Status PCache::RandomKey(std::string *key) { /*----------------------------------------------------------------------------- * String Commands *----------------------------------------------------------------------------*/ -Status PCache::Set(std::string& key, std::string &value, int64_t ttl) { +Status PCache::Set(std::string &key, std::string &value, int64_t ttl) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); return caches_[cache_index]->Set(key, value, ttl); } -Status PCache::Setnx(std::string& key, std::string &value, int64_t ttl) { +Status PCache::Setnx(std::string &key, std::string &value, int64_t ttl) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); return caches_[cache_index]->Setnx(key, value, ttl); } -Status PCache::SetnxWithoutTTL(std::string& key, std::string &value) { +Status PCache::SetnxWithoutTTL(std::string &key, std::string &value) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); return caches_[cache_index]->SetnxWithoutTTL(key, value); } -Status PCache::Setxx(std::string& key, std::string &value, int64_t ttl) { +Status PCache::Setxx(std::string &key, std::string &value, int64_t ttl) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); return caches_[cache_index]->Setxx(key, value, ttl); } -Status PCache::SetxxWithoutTTL(std::string& key, std::string &value) { +Status PCache::SetxxWithoutTTL(std::string &key, std::string &value) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); return caches_[cache_index]->SetxxWithoutTTL(key, value); } -Status PCache::Get(std::string& key, std::string *value) { +Status PCache::Get(std::string &key, std::string *value) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); return caches_[cache_index]->Get(key, value); @@ -249,7 +242,7 @@ Status PCache::MGet(const std::vector &keys, std::vectorExists(key)) { @@ -258,7 +251,7 @@ Status PCache::Incrxx(std::string& key) { return Status::NotFound("key not exist"); } -Status PCache::Decrxx(std::string& key) { +Status PCache::Decrxx(std::string &key) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); if (caches_[cache_index]->Exists(key)) { @@ -267,7 +260,7 @@ Status PCache::Decrxx(std::string& key) { return Status::NotFound("key not exist"); } -Status PCache::IncrByxx(std::string& key, uint64_t incr) { +Status PCache::IncrByxx(std::string &key, uint64_t incr) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); if (caches_[cache_index]->Exists(key)) { @@ -276,7 +269,7 @@ Status PCache::IncrByxx(std::string& key, uint64_t incr) { return Status::NotFound("key not exist"); } -Status PCache::DecrByxx(std::string& key, uint64_t incr) { +Status PCache::DecrByxx(std::string &key, uint64_t incr) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); if (caches_[cache_index]->Exists(key)) { @@ -285,7 +278,7 @@ Status PCache::DecrByxx(std::string& key, uint64_t incr) { return Status::NotFound("key not exist"); } -Status PCache::Incrbyfloatxx(std::string& key, long double incr) { +Status PCache::Incrbyfloatxx(std::string &key, long double incr) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); if (caches_[cache_index]->Exists(key)) { @@ -294,7 +287,7 @@ Status PCache::Incrbyfloatxx(std::string& key, long double incr) { return Status::NotFound("key not exist"); } -Status PCache::Appendxx(std::string& key, std::string &value) { +Status PCache::Appendxx(std::string &key, std::string &value) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); if (caches_[cache_index]->Exists(key)) { @@ -303,13 +296,13 @@ Status PCache::Appendxx(std::string& key, std::string &value) { return Status::NotFound("key not exist"); } -Status PCache::GetRange(std::string& key, int64_t start, int64_t end, std::string *value) { +Status PCache::GetRange(std::string &key, int64_t start, int64_t end, std::string *value) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); return caches_[cache_index]->GetRange(key, start, end, value); } -Status PCache::SetRangexx(std::string& key, int64_t start, std::string &value) { +Status PCache::SetRangexx(std::string &key, int64_t start, std::string &value) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); if (caches_[cache_index]->Exists(key)) { @@ -318,7 +311,7 @@ Status PCache::SetRangexx(std::string& key, int64_t start, std::string &value) { return Status::NotFound("key not exist"); } -Status PCache::Strlen(std::string& key, int32_t *len) { +Status PCache::Strlen(std::string &key, int32_t *len) { int cache_index = CacheIndex(key); std::lock_guard lm(*cache_mutexs_[cache_index]); return caches_[cache_index]->Strlen(key, len); @@ -463,110 +456,110 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // return caches_[cache_index]->HStrlen(key, field, len); // } -// /*----------------------------------------------------------------------------- -// * List Commands -// *----------------------------------------------------------------------------*/ -// Status PCache::LIndex(std::string& key, int64_t index, std::string *element) { -// int cache_index = CacheIndex(key); -// std::lock_guard lm(*cache_mutexs_[cache_index]); -// return caches_[cache_index]->LIndex(key, index, element); -// } +/*----------------------------------------------------------------------------- + * List Commands + *----------------------------------------------------------------------------*/ +Status PCache::LIndex(std::string &key, int64_t index, std::string *element) { + int cache_index = CacheIndex(key); + std::lock_guard lm(*cache_mutexs_[cache_index]); + return caches_[cache_index]->LIndex(key, index, element); +} -// Status PCache::LInsert(std::string& key, storage::BeforeOrAfter &before_or_after, std::string &pivot, -// std::string &value) { -// int cache_index = CacheIndex(key); -// std::lock_guard lm(*cache_mutexs_[cache_index]); -// return caches_[cache_index]->LInsert(key, before_or_after, pivot, value); -// } +Status PCache::LInsert(std::string &key, storage::BeforeOrAfter &before_or_after, std::string &pivot, + std::string &value) { + int cache_index = CacheIndex(key); + std::lock_guard lm(*cache_mutexs_[cache_index]); + return caches_[cache_index]->LInsert(key, before_or_after, pivot, value); +} -// Status PCache::LLen(std::string& key, uint64_t *len) { -// int cache_index = CacheIndex(key); -// std::lock_guard lm(*cache_mutexs_[cache_index]); -// return caches_[cache_index]->LLen(key, len); -// } +Status PCache::LLen(std::string &key, uint64_t *len) { + int cache_index = CacheIndex(key); + std::lock_guard lm(*cache_mutexs_[cache_index]); + return caches_[cache_index]->LLen(key, len); +} -// Status PCache::LPop(std::string& key, std::string *element) { -// int cache_index = CacheIndex(key); -// std::lock_guard lm(*cache_mutexs_[cache_index]); -// return caches_[cache_index]->LPop(key, element); -// } +Status PCache::LPop(std::string &key, std::string *element) { + int cache_index = CacheIndex(key); + std::lock_guard lm(*cache_mutexs_[cache_index]); + return caches_[cache_index]->LPop(key, element); +} -// Status PCache::LPush(std::string& key, std::vector &values) { -// int cache_index = CacheIndex(key); -// std::lock_guard lm(*cache_mutexs_[cache_index]); -// return caches_[cache_index]->LPush(key, values); -// } +Status PCache::LPush(std::string &key, std::vector &values) { + int cache_index = CacheIndex(key); + std::lock_guard lm(*cache_mutexs_[cache_index]); + return caches_[cache_index]->LPush(key, values); +} -// Status PCache::LPushx(std::string& key, std::vector &values) { -// int cache_index = CacheIndex(key); -// std::lock_guard lm(*cache_mutexs_[cache_index]); -// return caches_[cache_index]->LPushx(key, values); -// } +Status PCache::LPushx(std::string &key, std::vector &values) { + int cache_index = CacheIndex(key); + std::lock_guard lm(*cache_mutexs_[cache_index]); + return caches_[cache_index]->LPushx(key, values); +} -// Status PCache::LRange(std::string& key, int64_t start, int64_t stop, std::vector *values) { -// int cache_index = CacheIndex(key); -// std::lock_guard lm(*cache_mutexs_[cache_index]); -// return caches_[cache_index]->LRange(key, start, stop, values); -// } +Status PCache::LRange(std::string &key, int64_t start, int64_t stop, std::vector *values) { + int cache_index = CacheIndex(key); + std::lock_guard lm(*cache_mutexs_[cache_index]); + return caches_[cache_index]->LRange(key, start, stop, values); +} -// Status PCache::LRem(std::string& key, int64_t count, std::string &value) { -// int cache_index = CacheIndex(key); -// std::lock_guard lm(*cache_mutexs_[cache_index]); -// return caches_[cache_index]->LRem(key, count, value); -// } +Status PCache::LRem(std::string &key, int64_t count, std::string &value) { + int cache_index = CacheIndex(key); + std::lock_guard lm(*cache_mutexs_[cache_index]); + return caches_[cache_index]->LRem(key, count, value); +} -// Status PCache::LSet(std::string& key, int64_t index, std::string &value) { -// int cache_index = CacheIndex(key); -// std::lock_guard lm(*cache_mutexs_[cache_index]); -// return caches_[cache_index]->LSet(key, index, value); -// } +Status PCache::LSet(std::string &key, int64_t index, std::string &value) { + int cache_index = CacheIndex(key); + std::lock_guard lm(*cache_mutexs_[cache_index]); + return caches_[cache_index]->LSet(key, index, value); +} -// Status PCache::LTrim(std::string& key, int64_t start, int64_t stop) { -// int cache_index = CacheIndex(key); -// std::lock_guard lm(*cache_mutexs_[cache_index]); -// return caches_[cache_index]->LTrim(key, start, stop); -// } +Status PCache::LTrim(std::string &key, int64_t start, int64_t stop) { + int cache_index = CacheIndex(key); + std::lock_guard lm(*cache_mutexs_[cache_index]); + return caches_[cache_index]->LTrim(key, start, stop); +} -// Status PCache::RPop(std::string& key, std::string *element) { -// int cache_index = CacheIndex(key); -// std::lock_guard lm(*cache_mutexs_[cache_index]); -// return caches_[cache_index]->RPop(key, element); -// } +Status PCache::RPop(std::string &key, std::string *element) { + int cache_index = CacheIndex(key); + std::lock_guard lm(*cache_mutexs_[cache_index]); + return caches_[cache_index]->RPop(key, element); +} -// Status PCache::RPush(std::string& key, std::vector &values) { -// int cache_index = CacheIndex(key); -// std::lock_guard lm(*cache_mutexs_[cache_index]); -// return caches_[cache_index]->RPush(key, values); -// } +Status PCache::RPush(std::string &key, std::vector &values) { + int cache_index = CacheIndex(key); + std::lock_guard lm(*cache_mutexs_[cache_index]); + return caches_[cache_index]->RPush(key, values); +} -// Status PCache::RPushx(std::string& key, std::vector &values) { -// int cache_index = CacheIndex(key); -// std::lock_guard lm(*cache_mutexs_[cache_index]); -// return caches_[cache_index]->RPushx(key, values); -// } +Status PCache::RPushx(std::string &key, std::vector &values) { + int cache_index = CacheIndex(key); + std::lock_guard lm(*cache_mutexs_[cache_index]); + return caches_[cache_index]->RPushx(key, values); +} -// Status PCache::RPushnx(std::string& key, std::vector &values, int64_t ttl) { -// int cache_index = CacheIndex(key); -// std::lock_guard lm(*cache_mutexs_[cache_index]); -// if (!caches_[cache_index]->Exists(key)) { -// caches_[cache_index]->RPush(key, values); -// caches_[cache_index]->Expire(key, ttl); -// return Status::OK(); -// } else { -// return Status::NotFound("key exist"); -// } -// } +Status PCache::RPushnx(std::string &key, std::vector &values, int64_t ttl) { + int cache_index = CacheIndex(key); + std::lock_guard lm(*cache_mutexs_[cache_index]); + if (!caches_[cache_index]->Exists(key)) { + caches_[cache_index]->RPush(key, values); + caches_[cache_index]->Expire(key, ttl); + return Status::OK(); + } else { + return Status::NotFound("key exist"); + } +} -// Status PCache::RPushnxWithoutTTL(std::string& key, std::vector &values) { -// int cache_index = CacheIndex(key); -// std::lock_guard lm(*cache_mutexs_[cache_index]); -// if (!caches_[cache_index]->Exists(key)) { -// caches_[cache_index]->RPush(key, values); -// return Status::OK(); -// } else { -// return Status::NotFound("key exist"); -// } -// } +Status PCache::RPushnxWithoutTTL(std::string &key, std::vector &values) { + int cache_index = CacheIndex(key); + std::lock_guard lm(*cache_mutexs_[cache_index]); + if (!caches_[cache_index]->Exists(key)) { + caches_[cache_index]->RPush(key, values); + return Status::OK(); + } else { + return Status::NotFound("key exist"); + } +} // /*----------------------------------------------------------------------------- // * Set Commands @@ -667,7 +660,6 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // storage::ScoreMember &max_m) { // if (cache_obj) { // std::vector score_members; -// // 获取第一个成员 // auto s = cache_obj->ZRange(key, 0, 0, &score_members); // if (!s.ok() || score_members.empty()) { // return false; @@ -675,7 +667,6 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // min_m = score_members.front(); // score_members.clear(); -// // 获取最后一个成员 // s = cache_obj->ZRange(key, -1, -1, &score_members); // if (!s.ok() || score_members.empty()) { // return false; @@ -694,7 +685,6 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // if (cache_obj->Exists(key)) { // std::unordered_set unique; // std::list filtered_score_members; -// // 去除重复元素 // for (auto it = score_members.rbegin(); it != score_members.rend(); ++it) { // if (unique.find(it->member) == unique.end()) { // unique.insert(it->member); @@ -712,7 +702,6 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // storage::ScoreMember cache_min_sm; // storage::ScoreMember cache_max_sm; -// // 获取cache里面该集合的最大值和最小值 // if (!GetCacheMinMaxSM(cache_obj, key, cache_min_sm, cache_max_sm)) { // return Status::NotFound("key not exist"); // } @@ -901,18 +890,17 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // } // Status PCache::ZCount(std::string& key, std::string &min, std::string &max, uint64_t *len, ZCountCmd *cmd) { -// std::string CachePrefixKeyZ = PCacheKeyPrefixZ + key; -// int cache_index = CacheIndex(CachePrefixKeyZ); +// int cache_index = CacheIndex(key); // std::lock_guard lm(*cache_mutexs_[cache_index]); // auto cache_obj = caches_[cache_index]; // uint64_t cache_len = 0; -// cache_obj->ZCard(CachePrefixKeyZ, &cache_len); +// cache_obj->ZCard(key, &cache_len); // if (cache_len <= 0) { // return Status::NotFound("key not in cache"); // } else { // storage::ScoreMember cache_min_sm; // storage::ScoreMember cache_max_sm; -// if (!GetCacheMinMaxSM(cache_obj, CachePrefixKeyZ, cache_min_sm, cache_max_sm)) { +// if (!GetCacheMinMaxSM(cache_obj, key, cache_min_sm, cache_max_sm)) { // return Status::NotFound("key not exist"); // } // auto cache_min_score = cache_min_sm.score; @@ -920,7 +908,7 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // if (RangeStatus::RangeHit == CheckCacheRangeByScore(cache_len, cache_min_score, cache_max_score, cmd->MinScore(), // cmd->MaxScore(), cmd->LeftClose(), cmd->RightClose())) { -// auto s = cache_obj->ZCount(CachePrefixKeyZ, min, max, len); +// auto s = cache_obj->ZCount(key, min, max, len); // return s; // } else { // return Status::NotFound("key not in cache"); @@ -936,10 +924,9 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // bool PCache::ReloadCacheKeyIfNeeded(cache::RedisCache *cache_obj, std::string& key, int mem_len, int db_len, // const std::shared_ptr& db) { -// std::string CachePrefixKeyZ = PCacheKeyPrefixZ + key; // if (mem_len == -1) { // uint64_t cache_len = 0; -// cache_obj->ZCard(CachePrefixKeyZ, &cache_len); +// cache_obj->ZCard(key, &cache_len); // mem_len = cache_len; // } // if (db_len == -1) { @@ -951,7 +938,7 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // } // if (db_len < zset_cache_field_num_per_key_) { // if (mem_len * 2 < db_len) { -// cache_obj->Del(CachePrefixKeyZ); +// cache_obj->Del(key); // PushKeyToAsyncLoadQueue(PIKA_KEY_TYPE_ZSET, key, db); // return true; // } else { @@ -959,7 +946,7 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // } // } else { // if (zset_cache_field_num_per_key_ && mem_len * 2 < zset_cache_field_num_per_key_) { -// cache_obj->Del(CachePrefixKeyZ); +// cache_obj->Del(key); // PushKeyToAsyncLoadQueue(PIKA_KEY_TYPE_ZSET, key, db); // return true; // } else { @@ -968,7 +955,8 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // } // } -// Status PCache::ZIncrbyIfKeyExist(std::string& key, std::string& member, double increment, ZIncrbyCmd *cmd, const std::shared_ptr& db) { +// Status PCache::ZIncrbyIfKeyExist(std::string& key, std::string& member, double increment, ZIncrbyCmd *cmd, const +// std::shared_ptr& db) { // auto eps = std::numeric_limits::epsilon(); // if (-eps < increment && increment < eps) { // return Status::NotFound("icrement is 0, nothing to be done"); @@ -1035,7 +1023,8 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // return Status::NotFound("key not exist"); // } -// RangeStatus PCache::CheckCacheRange(int32_t cache_len, int32_t db_len, int64_t start, int64_t stop, int64_t &out_start, +// RangeStatus PCache::CheckCacheRange(int32_t cache_len, int32_t db_len, int64_t start, int64_t stop, int64_t +// &out_start, // int64_t &out_stop) { // out_start = start >= 0 ? start : db_len + start; // out_stop = stop >= 0 ? stop : db_len + stop; @@ -1064,7 +1053,8 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // } // } -// RangeStatus PCache::CheckCacheRevRange(int32_t cache_len, int32_t db_len, int64_t start, int64_t stop, int64_t &out_start, +// RangeStatus PCache::CheckCacheRevRange(int32_t cache_len, int32_t db_len, int64_t start, int64_t stop, int64_t +// &out_start, // int64_t &out_stop) { // int64_t start_index = stop >= 0 ? db_len - stop - 1 : -stop - 1; // int64_t stop_index = start >= 0 ? db_len - start - 1 : -start - 1; @@ -1099,25 +1089,25 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // } // } -// Status PCache::ZRange(std::string& key, int64_t start, int64_t stop, std::vector *score_members, +// Status PCache::ZRange(std::string& key, int64_t start, int64_t stop, std::vector +// *score_members, // const std::shared_ptr& db) { -// std::string CachePrefixKeyZ = PCacheKeyPrefixZ + key; -// int cache_index = CacheIndex(CachePrefixKeyZ); +// int cache_index = CacheIndex(key); // std::lock_guard lm(*cache_mutexs_[cache_index]); // auto cache_obj = caches_[cache_index]; // auto db_obj = db->storage(); // Status s; -// if (cache_obj->Exists(CachePrefixKeyZ)) { +// if (cache_obj->Exists(key)) { // uint64_t cache_len = 0; -// cache_obj->ZCard(CachePrefixKeyZ, &cache_len); +// cache_obj->ZCard(key, &cache_len); // int32_t db_len = 0; // db_obj->ZCard(key, &db_len); // int64_t out_start = 0; // int64_t out_stop = 0; // RangeStatus rs = CheckCacheRange(cache_len, db_len, start, stop, out_start, out_stop); // if (rs == RangeStatus::RangeHit) { -// return cache_obj->ZRange(CachePrefixKeyZ, out_start, out_stop, score_members); +// return cache_obj->ZRange(key, out_start, out_stop, score_members); // } else if (rs == RangeStatus::RangeMiss) { // ReloadCacheKeyIfNeeded(cache_obj, key, cache_len, db_len, db); // return Status::NotFound("key not in cache"); @@ -1133,26 +1123,25 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // Status PCache::ZRangebyscore(std::string& key, std::string &min, std::string &max, // std::vector *score_members, ZRangebyscoreCmd *cmd) { -// std::string CachePrefixKeyZ = PCacheKeyPrefixZ + key; -// int cache_index = CacheIndex(CachePrefixKeyZ); +// int cache_index = CacheIndex(key); // std::lock_guard lm(*cache_mutexs_[cache_index]); // auto cache_obj = caches_[cache_index]; // uint64_t cache_len = 0; -// cache_obj->ZCard(CachePrefixKeyZ, &cache_len); +// cache_obj->ZCard(key, &cache_len); // if (cache_len <= 0) { // return Status::NotFound("key not in cache"); // } else { // storage::ScoreMember cache_min_sm; // storage::ScoreMember cache_max_sm; -// if (!GetCacheMinMaxSM(cache_obj, CachePrefixKeyZ, cache_min_sm, cache_max_sm)) { +// if (!GetCacheMinMaxSM(cache_obj, key, cache_min_sm, cache_max_sm)) { // return Status::NotFound("key not exist"); // } // if (RangeStatus::RangeHit == CheckCacheRangeByScore(cache_len, cache_min_sm.score, cache_max_sm.score, // cmd->MinScore(), cmd->MaxScore(), cmd->LeftClose(), // cmd->RightClose())) { -// return cache_obj->ZRangebyscore(CachePrefixKeyZ, min, max, score_members, cmd->Offset(), cmd->Count()); +// return cache_obj->ZRangebyscore(key, min, max, score_members, cmd->Offset(), cmd->Count()); // } else { // return Status::NotFound("key not in cache"); // } @@ -1160,17 +1149,16 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // } // Status PCache::ZRank(std::string& key, std::string& member, int64_t *rank, const std::shared_ptr& db) { -// std::string CachePrefixKeyZ = PCacheKeyPrefixZ + key; -// int cache_index = CacheIndex(CachePrefixKeyZ); +// int cache_index = CacheIndex(key); // std::lock_guard lm(*cache_mutexs_[cache_index]); // auto cache_obj = caches_[cache_index]; // uint64_t cache_len = 0; -// cache_obj->ZCard(CachePrefixKeyZ, &cache_len); +// cache_obj->ZCard(key, &cache_len); // if (cache_len <= 0) { // return Status::NotFound("key not in cache"); // } else { -// auto s = cache_obj->ZRank(CachePrefixKeyZ, member, rank); +// auto s = cache_obj->ZRank(key, member, rank); // if (s.ok()) { // if (zset_cache_start_direction_ == cache::CACHE_START_FROM_END) { // int32_t db_len = 0; @@ -1259,25 +1247,25 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // return s; // } -// Status PCache::ZRevrange(std::string& key, int64_t start, int64_t stop, std::vector *score_members, +// Status PCache::ZRevrange(std::string& key, int64_t start, int64_t stop, std::vector +// *score_members, // const std::shared_ptr& db) { -// std::string CachePrefixKeyZ = PCacheKeyPrefixZ + key; -// int cache_index = CacheIndex(CachePrefixKeyZ); +// int cache_index = CacheIndex(key); // std::lock_guard lm(*cache_mutexs_[cache_index]); // auto cache_obj = caches_[cache_index]; // auto db_obj = db->storage(); // Status s; -// if (cache_obj->Exists(CachePrefixKeyZ)) { +// if (cache_obj->Exists(key)) { // uint64_t cache_len = 0; -// cache_obj->ZCard(CachePrefixKeyZ, &cache_len); +// cache_obj->ZCard(key, &cache_len); // int32_t db_len = 0; // db_obj->ZCard(key, &db_len); // int64_t out_start = 0; // int64_t out_stop = 0; // RangeStatus rs = CheckCacheRevRange(cache_len, db_len, start, stop, out_start, out_stop); // if (rs == RangeStatus::RangeHit) { -// return cache_obj->ZRevrange(CachePrefixKeyZ, out_start, out_stop, score_members); +// return cache_obj->ZRevrange(key, out_start, out_stop, score_members); // } else if (rs == RangeStatus::RangeMiss) { // ReloadCacheKeyIfNeeded(cache_obj, key, cache_len, db_len, db); // return Status::NotFound("key not in cache"); @@ -1294,19 +1282,18 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // Status PCache::ZRevrangebyscore(std::string& key, std::string &min, std::string &max, // std::vector *score_members, ZRevrangebyscoreCmd *cmd, // const std::shared_ptr& db) { -// std::string CachePrefixKeyZ = PCacheKeyPrefixZ + key; -// int cache_index = CacheIndex(CachePrefixKeyZ); +// int cache_index = CacheIndex(key); // std::lock_guard lm(*cache_mutexs_[cache_index]); // auto cache_obj = caches_[cache_index]; // uint64_t cache_len = 0; -// cache_obj->ZCard(CachePrefixKeyZ, &cache_len); +// cache_obj->ZCard(key, &cache_len); // if (cache_len <= 0) { // return Status::NotFound("key not in cache"); // } else { // storage::ScoreMember cache_min_sm; // storage::ScoreMember cache_max_sm; -// if (!GetCacheMinMaxSM(cache_obj, CachePrefixKeyZ, cache_min_sm, cache_max_sm)) { +// if (!GetCacheMinMaxSM(cache_obj, key, cache_min_sm, cache_max_sm)) { // return Status::NotFound("key not exist"); // } // auto cache_min_score = cache_min_sm.score; @@ -1315,7 +1302,7 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // auto rs = CheckCacheRangeByScore(cache_len, cache_min_score, cache_max_score, cmd->MinScore(), cmd->MaxScore(), // cmd->LeftClose(), cmd->RightClose()); // if (RangeStatus::RangeHit == rs) { -// return cache_obj->ZRevrangebyscore(CachePrefixKeyZ, min, max, score_members, cmd->Offset(), cmd->Count()); +// return cache_obj->ZRevrangebyscore(key, min, max, score_members, cmd->Offset(), cmd->Count()); // } else if (RangeStatus::RangeMiss == rs) { // ReloadCacheKeyIfNeeded(cache_obj, key, cache_len, -1, db); // return Status::NotFound("score range miss"); @@ -1330,37 +1317,34 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // db->storage()->ZCard(key, &db_len); // std::lock_guard l(rwlock_); -// std::string CachePrefixKeyZ = PCacheKeyPrefixZ + key; -// int cache_index = CacheIndex(CachePrefixKeyZ); +// int cache_index = CacheIndex(key); // std::lock_guard lm(*cache_mutexs_[cache_index]); // uint64_t cache_len = 0; -// caches_[cache_index]->ZCard(CachePrefixKeyZ, &cache_len); +// caches_[cache_index]->ZCard(key, &cache_len); // return (db_len == (int32_t)cache_len) && cache_len; // } // Status PCache::ZRevrangebylex(std::string& key, std::string &min, std::string &max, // std::vector *members, const std::shared_ptr& db) { // if (CacheSizeEqsDB(key, db)) { -// std::string CachePrefixKeyZ = PCacheKeyPrefixZ + key; -// int cache_index = CacheIndex(CachePrefixKeyZ); +// int cache_index = CacheIndex(key); // std::lock_guard lm(*cache_mutexs_[cache_index]); -// return caches_[cache_index]->ZRevrangebylex(CachePrefixKeyZ, min, max, members); +// return caches_[cache_index]->ZRevrangebylex(key, min, max, members); // } else { // return Status::NotFound("key not in cache"); // } // } // Status PCache::ZRevrank(std::string& key, std::string& member, int64_t *rank, const std::shared_ptr& db) { -// std::string CachePrefixKeyZ = PCacheKeyPrefixZ + key; -// int cache_index = CacheIndex(CachePrefixKeyZ); +// int cache_index = CacheIndex(key); // std::lock_guard lm(*cache_mutexs_[cache_index]); // auto cache_obj = caches_[cache_index]; // uint64_t cache_len = 0; -// cache_obj->ZCard(CachePrefixKeyZ, &cache_len); +// cache_obj->ZCard(key, &cache_len); // if (cache_len <= 0) { // return Status::NotFound("key not in cache"); // } else { -// auto s = cache_obj->ZRevrank(CachePrefixKeyZ, member, rank); +// auto s = cache_obj->ZRevrank(key, member, rank); // if (s.ok()) { // if (zset_cache_start_direction_ == cache::CACHE_START_FROM_BEGIN) { // int32_t db_len = 0; @@ -1386,10 +1370,9 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // Status PCache::ZRangebylex(std::string& key, std::string &min, std::string &max, std::vector *members, // const std::shared_ptr& db) { // if (CacheSizeEqsDB(key, db)) { -// std::string CachePrefixKeyZ = PCacheKeyPrefixZ + key; -// int cache_index = CacheIndex(CachePrefixKeyZ); +// int cache_index = CacheIndex(key); // std::lock_guard lm(*cache_mutexs_[cache_index]); -// return caches_[cache_index]->ZRangebylex(CachePrefixKeyZ, min, max, members); +// return caches_[cache_index]->ZRangebylex(key, min, max, members); // } else { // return Status::NotFound("key not in cache"); // } @@ -1398,11 +1381,10 @@ Status PCache::Strlen(std::string& key, int32_t *len) { // Status PCache::ZLexcount(std::string& key, std::string &min, std::string &max, uint64_t *len, // const std::shared_ptr& db) { // if (CacheSizeEqsDB(key, db)) { -// std::string CachePrefixKeyZ = PCacheKeyPrefixZ + key; -// int cache_index = CacheIndex(CachePrefixKeyZ); +// int cache_index = CacheIndex(key); // std::lock_guard lm(*cache_mutexs_[cache_index]); -// return caches_[cache_index]->ZLexcount(CachePrefixKeyZ, min, max, len); +// return caches_[cache_index]->ZLexcount(key, min, max, len); // } else { // return Status::NotFound("key not in cache"); // } @@ -1492,8 +1474,7 @@ Status PCache::InitWithoutLock(uint32_t cache_num, cache::CacheConfig *cache_cfg return Status::OK(); } -void PCache::DestroyWithoutLock(void) -{ +void PCache::DestroyWithoutLock(void) { cache_status_ = PCACHE_STATUS_DESTROY; for (auto iter = caches_.begin(); iter != caches_.end(); ++iter) { @@ -1503,12 +1484,12 @@ void PCache::DestroyWithoutLock(void) cache_mutexs_.clear(); } -int PCache::CacheIndex(const std::string& key) { - auto crc = crc32(0L, (const Bytef*)key.data(), (int)key.size()); +int PCache::CacheIndex(const std::string &key) { + auto crc = crc32(0L, (const Bytef *)key.data(), (int)key.size()); return (int)(crc % caches_.size()); } -Status PCache::WriteKVToCache(std::string& key, std::string &value, int64_t ttl) { +Status PCache::WriteKVToCache(std::string &key, std::string &value, int64_t ttl) { if (0 >= ttl) { if (PCache_TTL_NONE == ttl) { return SetnxWithoutTTL(key, value); @@ -1534,18 +1515,18 @@ Status PCache::WriteKVToCache(std::string& key, std::string &value, int64_t ttl) // return Status::OK(); // } -// Status PCache::WriteListToCache(std::string& key, std::vector &values, int64_t ttl) { -// if (0 >= ttl) { -// if (PIKA_TTL_NONE == ttl) { -// return RPushnxWithoutTTL(key, values); -// } else { -// return Del({key}); -// } -// } else { -// return RPushnx(key, values, ttl); -// } -// return Status::OK(); -// } +Status PCache::WriteListToCache(std::string &key, std::vector &values, int64_t ttl) { + if (0 >= ttl) { + if (PCache_TTL_NONE == ttl) { + return RPushnxWithoutTTL(key, values); + } else { + return Del({key}); + } + } else { + return RPushnx(key, values, ttl); + } + return Status::OK(); +} // Status PCache::WriteSetToCache(std::string& key, std::vector &members, int64_t ttl) { // if (0 >= ttl) { @@ -1573,12 +1554,12 @@ Status PCache::WriteKVToCache(std::string& key, std::string &value, int64_t ttl) // return Status::OK(); // } -void PCache::PushKeyToAsyncLoadQueue(const char key_type, std::string& key, const std::shared_ptr& db) { - cache_load_thread_->Push(key_type, key, db); +void PCache::PushKeyToAsyncLoadQueue(const char key_type, std::string &key, PClient *client) { + cache_load_thread_->Push(key_type, key, client); } void PCache::ClearHitRatio(void) { std::unique_lock l(rwlock_); cache::RedisCache::ResetHitAndMissNum(); } -} // namespace pikiwidb +} // namespace pikiwidb diff --git a/src/pcache.h b/src/pcache.h index 7bde90139..d3ac0c7d5 100644 --- a/src/pcache.h +++ b/src/pcache.h @@ -9,29 +9,16 @@ #include #include +#include "cache/redisCache.h" #include "cache_define.h" -//#include "include/pika_zset.h" -//#include "include/pika_command.h" +#include "client.h" #include "pstd/pstd_mutex.h" #include "pstd/pstd_status.h" -#include "cache/redisCache.h" -//#include "db.h" -//#include "pcache_load_thread.h" -// #include "storage/storage.h" - - -// class ZIncrbyCmd; -// class ZRangebyscoreCmd; -// class ZRevrangebyscoreCmd; -// class ZCountCmd; - -namespace pikiwidb{ +namespace pikiwidb { class PCacheLoadThread; -class DB; enum RangeStatus { RangeError = 1, RangeHit, RangeMiss }; - struct CacheInfo { int status = PCACHE_STATUS_NONE; uint32_t cache_num = 0; @@ -58,10 +45,10 @@ class PCache : public pstd::noncopyable, public std::enable_shared_from_this& fields); -// rocksdb::Status HSet(std::string& key, std::string& field, std::string& value); -// rocksdb::Status HSetIfKeyExist(std::string& key, std::string& field, std::string& value); -// rocksdb::Status HSetIfKeyExistAndFieldNotExist(std::string& key, std::string& field, std::string& value); -// rocksdb::Status HMSet(std::string& key, std::vector& fvs); -// rocksdb::Status HMSetnx(std::string& key, std::vector& fvs, int64_t ttl); -// rocksdb::Status HMSetnxWithoutTTL(std::string& key, std::vector& fvs); -// rocksdb::Status HMSetxx(std::string& key, std::vector& fvs); -// rocksdb::Status HGet(std::string& key, std::string& field, std::string* value); -// rocksdb::Status HMGet(std::string& key, std::vector& fields, std::vector* vss); -// rocksdb::Status HGetall(std::string& key, std::vector* fvs); -// rocksdb::Status HKeys(std::string& key, std::vector* fields); -// rocksdb::Status HVals(std::string& key, std::vector* values); -// rocksdb::Status HExists(std::string& key, std::string& field); -// rocksdb::Status HIncrbyxx(std::string& key, std::string& field, int64_t value); -// rocksdb::Status HIncrbyfloatxx(std::string& key, std::string& field, long double value); -// rocksdb::Status HLen(std::string& key, uint64_t* len); -// rocksdb::Status HStrlen(std::string& key, std::string& field, uint64_t* len); + // rocksdb::Status HDel(std::string& key, std::vector& fields); + // rocksdb::Status HSet(std::string& key, std::string& field, std::string& value); + // rocksdb::Status HSetIfKeyExist(std::string& key, std::string& field, std::string& value); + // rocksdb::Status HSetIfKeyExistAndFieldNotExist(std::string& key, std::string& field, std::string& value); + // rocksdb::Status HMSet(std::string& key, std::vector& fvs); + // rocksdb::Status HMSetnx(std::string& key, std::vector& fvs, int64_t ttl); + // rocksdb::Status HMSetnxWithoutTTL(std::string& key, std::vector& fvs); + // rocksdb::Status HMSetxx(std::string& key, std::vector& fvs); + // rocksdb::Status HGet(std::string& key, std::string& field, std::string* value); + // rocksdb::Status HMGet(std::string& key, std::vector& fields, std::vector* + // vss); rocksdb::Status HGetall(std::string& key, std::vector* fvs); rocksdb::Status + // HKeys(std::string& key, std::vector* fields); rocksdb::Status HVals(std::string& key, + // std::vector* values); rocksdb::Status HExists(std::string& key, std::string& field); rocksdb::Status + // HIncrbyxx(std::string& key, std::string& field, int64_t value); rocksdb::Status HIncrbyfloatxx(std::string& key, + // std::string& field, long double value); rocksdb::Status HLen(std::string& key, uint64_t* len); rocksdb::Status + // HStrlen(std::string& key, std::string& field, uint64_t* len); // List Commands -// rocksdb::Status LIndex(std::string& key, int64_t index, std::string* element); -// rocksdb::Status LInsert(std::string& key, storage::BeforeOrAfter& before_or_after, std::string& pivot, std::string& value); -// rocksdb::Status LLen(std::string& key, uint64_t* len); -// rocksdb::Status LPop(std::string& key, std::string* element); -// rocksdb::Status LPush(std::string& key, std::vector &values); -// rocksdb::Status LPushx(std::string& key, std::vector &values); -// rocksdb::Status LRange(std::string& key, int64_t start, int64_t stop, std::vector* values); -// rocksdb::Status LRem(std::string& key, int64_t count, std::string& value); -// rocksdb::Status LSet(std::string& key, int64_t index, std::string& value); -// rocksdb::Status LTrim(std::string& key, int64_t start, int64_t stop); -// rocksdb::Status RPop(std::string& key, std::string* element); -// rocksdb::Status RPush(std::string& key, std::vector &values); -// rocksdb::Status RPushx(std::string& key, std::vector &values); -// rocksdb::Status RPushnx(std::string& key, std::vector &values, int64_t ttl); -// rocksdb::Status RPushnxWithoutTTL(std::string& key, std::vector &values); + rocksdb::Status LIndex(std::string& key, int64_t index, std::string* element); + rocksdb::Status LInsert(std::string& key, storage::BeforeOrAfter& before_or_after, std::string& pivot, + std::string& value); + rocksdb::Status LLen(std::string& key, uint64_t* len); + rocksdb::Status LPop(std::string& key, std::string* element); + rocksdb::Status LPush(std::string& key, std::vector& values); + rocksdb::Status LPushx(std::string& key, std::vector& values); + rocksdb::Status LRange(std::string& key, int64_t start, int64_t stop, std::vector* values); + rocksdb::Status LRem(std::string& key, int64_t count, std::string& value); + rocksdb::Status LSet(std::string& key, int64_t index, std::string& value); + rocksdb::Status LTrim(std::string& key, int64_t start, int64_t stop); + rocksdb::Status RPop(std::string& key, std::string* element); + rocksdb::Status RPush(std::string& key, std::vector& values); + rocksdb::Status RPushx(std::string& key, std::vector& values); + rocksdb::Status RPushnx(std::string& key, std::vector& values, int64_t ttl); + rocksdb::Status RPushnxWithoutTTL(std::string& key, std::vector& values); // Set Commands -// rocksdb::Status SAdd(std::string& key, std::vector& members); -// rocksdb::Status SAddIfKeyExist(std::string& key, std::vector& members); -// rocksdb::Status SAddnx(std::string& key, std::vector& members, int64_t ttl); -// rocksdb::Status SAddnxWithoutTTL(std::string& key, std::vector& members); -// rocksdb::Status SCard(std::string& key, uint64_t* len); -// rocksdb::Status SIsmember(std::string& key, std::string& member); -// rocksdb::Status SMembers(std::string& key, std::vector* members); -// rocksdb::Status SRem(std::string& key, std::vector& members); -// rocksdb::Status SRandmember(std::string& key, int64_t count, std::vector* members); + // rocksdb::Status SAdd(std::string& key, std::vector& members); + // rocksdb::Status SAddIfKeyExist(std::string& key, std::vector& members); + // rocksdb::Status SAddnx(std::string& key, std::vector& members, int64_t ttl); + // rocksdb::Status SAddnxWithoutTTL(std::string& key, std::vector& members); + // rocksdb::Status SCard(std::string& key, uint64_t* len); + // rocksdb::Status SIsmember(std::string& key, std::string& member); + // rocksdb::Status SMembers(std::string& key, std::vector* members); + // rocksdb::Status SRem(std::string& key, std::vector& members); + // rocksdb::Status SRandmember(std::string& key, int64_t count, std::vector* members); // ZSet Commands -// rocksdb::Status ZAdd(std::string& key, std::vector& score_members); -// rocksdb::Status ZAddIfKeyExist(std::string& key, std::vector& score_members); -// rocksdb::Status ZAddnx(std::string& key, std::vector& score_members, int64_t ttl); -// rocksdb::Status ZAddnxWithoutTTL(std::string& key, std::vector& score_members); -// rocksdb::Status ZCard(std::string& key, uint32_t* len, const std::shared_ptr& db); -// rocksdb::Status ZCount(std::string& key, std::string& min, std::string& max, uint64_t* len, ZCountCmd* cmd); -// rocksdb::Status ZIncrby(std::string& key, std::string& member, double increment); -// rocksdb::Status ZIncrbyIfKeyExist(std::string& key, std::string& member, double increment, ZIncrbyCmd* cmd, const std::shared_ptr& db); -// rocksdb::Status ZRange(std::string& key, int64_t start, int64_t stop, std::vector* score_members, -// const std::shared_ptr& db); -// rocksdb::Status ZRangebyscore(std::string& key, std::string& min, std::string& max, -// std::vector* score_members, ZRangebyscoreCmd* cmd); -// rocksdb::Status ZRank(std::string& key, std::string& member, int64_t* rank, const std::shared_ptr& db); -// rocksdb::Status ZRem(std::string& key, std::vector& members, std::shared_ptr db); -// rocksdb::Status ZRemrangebyrank(std::string& key, std::string& min, std::string& max, int32_t ele_deleted = 0, -// const std::shared_ptr& db = nullptr); -// rocksdb::Status ZRemrangebyscore(std::string& key, std::string& min, std::string& max, const std::shared_ptr& db); -// rocksdb::Status ZRevrange(std::string& key, int64_t start, int64_t stop, std::vector* score_members, -// const std::shared_ptr& db); -// rocksdb::Status ZRevrangebyscore(std::string& key, std::string& min, std::string& max, -// std::vector* score_members, ZRevrangebyscoreCmd* cmd, -// const std::shared_ptr& db); -// rocksdb::Status ZRevrangebylex(std::string& key, std::string& min, std::string& max, std::vector* members, -// const std::shared_ptr& db); -// rocksdb::Status ZRevrank(std::string& key, std::string& member, int64_t *rank, const std::shared_ptr& db); -// rocksdb::Status ZScore(std::string& key, std::string& member, double* score, const std::shared_ptr& db); -// rocksdb::Status ZRangebylex(std::string& key, std::string& min, std::string& max, std::vector* members, const std::shared_ptr& db); -// rocksdb::Status ZLexcount(std::string& key, std::string& min, std::string& max, uint64_t* len, -// const std::shared_ptr& db); -// rocksdb::Status ZRemrangebylex(std::string& key, std::string& min, std::string& max, const std::shared_ptr& db); - -// // Bit Commands -// rocksdb::Status SetBit(std::string& key, size_t offset, int64_t value); -// rocksdb::Status SetBitIfKeyExist(std::string& key, size_t offset, int64_t value); -// rocksdb::Status GetBit(std::string& key, size_t offset, int64_t* value); -// rocksdb::Status BitCount(std::string& key, int64_t start, int64_t end, int64_t* value, bool have_offset); -// rocksdb::Status BitPos(std::string& key, int64_t bit, int64_t* value); -// rocksdb::Status BitPos(std::string& key, int64_t bit, int64_t start, int64_t* value); -// rocksdb::Status BitPos(std::string& key, int64_t bit, int64_t start, int64_t end, int64_t* value); + // rocksdb::Status ZAdd(std::string& key, std::vector& score_members); + // rocksdb::Status ZAddIfKeyExist(std::string& key, std::vector& score_members); + // rocksdb::Status ZAddnx(std::string& key, std::vector& score_members, int64_t ttl); + // rocksdb::Status ZAddnxWithoutTTL(std::string& key, std::vector& score_members); + // rocksdb::Status ZCard(std::string& key, uint32_t* len, const std::shared_ptr& db); + // rocksdb::Status ZCount(std::string& key, std::string& min, std::string& max, uint64_t* len, ZCountCmd* cmd); + // rocksdb::Status ZIncrby(std::string& key, std::string& member, double increment); + // rocksdb::Status ZIncrbyIfKeyExist(std::string& key, std::string& member, double increment, ZIncrbyCmd* cmd, const + // std::shared_ptr& db); rocksdb::Status ZRange(std::string& key, int64_t start, int64_t stop, + // std::vector* score_members, + // const std::shared_ptr& db); + // rocksdb::Status ZRangebyscore(std::string& key, std::string& min, std::string& max, + // std::vector* score_members, ZRangebyscoreCmd* cmd); + // rocksdb::Status ZRank(std::string& key, std::string& member, int64_t* rank, const std::shared_ptr& db); + // rocksdb::Status ZRem(std::string& key, std::vector& members, std::shared_ptr db); + // rocksdb::Status ZRemrangebyrank(std::string& key, std::string& min, std::string& max, int32_t ele_deleted = 0, + // const std::shared_ptr& db = nullptr); + // rocksdb::Status ZRemrangebyscore(std::string& key, std::string& min, std::string& max, const std::shared_ptr& + // db); rocksdb::Status ZRevrange(std::string& key, int64_t start, int64_t stop, std::vector* + // score_members, + // const std::shared_ptr& db); + // rocksdb::Status ZRevrangebyscore(std::string& key, std::string& min, std::string& max, + // std::vector* score_members, ZRevrangebyscoreCmd* cmd, + // const std::shared_ptr& db); + // rocksdb::Status ZRevrangebylex(std::string& key, std::string& min, std::string& max, std::vector* + // members, + // const std::shared_ptr& db); + // rocksdb::Status ZRevrank(std::string& key, std::string& member, int64_t *rank, const std::shared_ptr& db); + // rocksdb::Status ZScore(std::string& key, std::string& member, double* score, const std::shared_ptr& db); + // rocksdb::Status ZRangebylex(std::string& key, std::string& min, std::string& max, std::vector* + // members, const std::shared_ptr& db); rocksdb::Status ZLexcount(std::string& key, std::string& min, + // std::string& max, uint64_t* len, + // const std::shared_ptr& db); + // rocksdb::Status ZRemrangebylex(std::string& key, std::string& min, std::string& max, const std::shared_ptr& + // db); + + // // Bit Commands + // rocksdb::Status SetBit(std::string& key, size_t offset, int64_t value); + // rocksdb::Status SetBitIfKeyExist(std::string& key, size_t offset, int64_t value); + // rocksdb::Status GetBit(std::string& key, size_t offset, int64_t* value); + // rocksdb::Status BitCount(std::string& key, int64_t start, int64_t end, int64_t* value, bool have_offset); + // rocksdb::Status BitPos(std::string& key, int64_t bit, int64_t* value); + // rocksdb::Status BitPos(std::string& key, int64_t bit, int64_t start, int64_t* value); + // rocksdb::Status BitPos(std::string& key, int64_t bit, int64_t start, int64_t end, int64_t* value); // Cache rocksdb::Status WriteKVToCache(std::string& key, std::string& value, int64_t ttl); rocksdb::Status WriteHashToCache(std::string& key, std::vector& fvs, int64_t ttl); - rocksdb::Status WriteListToCache(std::string& key, std::vector &values, int64_t ttl); + rocksdb::Status WriteListToCache(std::string& key, std::vector& values, int64_t ttl); rocksdb::Status WriteSetToCache(std::string& key, std::vector& members, int64_t ttl); rocksdb::Status WriteZSetToCache(std::string& key, std::vector& score_members, int64_t ttl); - void PushKeyToAsyncLoadQueue(const char key_type, std::string& key, const std::shared_ptr& db); + void PushKeyToAsyncLoadQueue(const char key_type, std::string& key, PClient* client); rocksdb::Status CacheZCard(std::string& key, uint64_t* len); - - private: + private: rocksdb::Status InitWithoutLock(uint32_t cache_num, cache::CacheConfig* cache_cfg); void DestroyWithoutLock(void); int CacheIndex(const std::string& key); - RangeStatus CheckCacheRange(int32_t cache_len, int32_t db_len, int64_t start, int64_t stop, int64_t& out_start, - int64_t& out_stop); - RangeStatus CheckCacheRevRange(int32_t cache_len, int32_t db_len, int64_t start, int64_t stop, int64_t& out_start, - int64_t& out_stop); - RangeStatus CheckCacheRangeByScore(uint64_t cache_len, double cache_min, double cache_max, double min, - double max, bool left_close, bool right_close); - bool CacheSizeEqsDB(std::string& key, const std::shared_ptr& db); - void GetMinMaxScore(std::vector& score_members, double &min, double &max); - bool GetCacheMinMaxSM(cache::RedisCache* cache_obj, std::string& key, storage::ScoreMember &min_m, - storage::ScoreMember &max_m); - bool ReloadCacheKeyIfNeeded(cache::RedisCache* cache_obj, std::string& key, int mem_len = -1, int db_len = -1, - const std::shared_ptr& db = nullptr); - rocksdb::Status CleanCacheKeyIfNeeded(cache::RedisCache* cache_obj, std::string& key); + // RangeStatus CheckCacheRange(int32_t cache_len, int32_t db_len, int64_t start, int64_t stop, int64_t& out_start, + // int64_t& out_stop); + // RangeStatus CheckCacheRevRange(int32_t cache_len, int32_t db_len, int64_t start, int64_t stop, int64_t& out_start, + // int64_t& out_stop); + // RangeStatus CheckCacheRangeByScore(uint64_t cache_len, double cache_min, double cache_max, double min, + // double max, bool left_close, bool right_close); + // bool CacheSizeEqsDB(std::string& key, const std::shared_ptr& db); + // void GetMinMaxScore(std::vector& score_members, double &min, double &max); + // bool GetCacheMinMaxSM(cache::RedisCache* cache_obj, std::string& key, storage::ScoreMember &min_m, + // storage::ScoreMember &max_m); + // bool ReloadCacheKeyIfNeeded(cache::RedisCache* cache_obj, std::string& key, int mem_len = -1, int db_len = -1, + // const std::shared_ptr& db = nullptr); + // rocksdb::Status CleanCacheKeyIfNeeded(cache::RedisCache* cache_obj, std::string& key); private: std::atomic cache_status_; @@ -225,8 +215,8 @@ class PCache : public pstd::noncopyable, public std::enable_shared_from_this cache_load_thread_; + std::unique_ptr cache_load_thread_; std::vector caches_; std::vector> cache_mutexs_; }; -} // namespace pikiwidb +} // namespace pikiwidb diff --git a/src/pcache_load_thread.cc b/src/pcache_load_thread.cc index 10d7812b6..8cece45c9 100644 --- a/src/pcache_load_thread.cc +++ b/src/pcache_load_thread.cc @@ -4,25 +4,22 @@ * 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 #include "pcache_load_thread.h" -// #include "include/pika_server.h" #include "pcache.h" #include "pstd/log.h" #include "pstd/scope_record_lock.h" +#include "store.h" -// extern PikaServer* g_pika_server; -namespace pikiwidb{ +namespace pikiwidb { PCacheLoadThread::PCacheLoadThread(int zset_cache_start_direction, int zset_cache_field_num_per_key) - : should_exit_(false) - , loadkeys_cond_() - , async_load_keys_num_(0) - , waitting_load_keys_num_(0) - , zset_cache_start_direction_(zset_cache_start_direction) - , zset_cache_field_num_per_key_(zset_cache_field_num_per_key) -{ + : should_exit_(false), + loadkeys_cond_(), + async_load_keys_num_(0), + waitting_load_keys_num_(0), + zset_cache_start_direction_(zset_cache_start_direction), + zset_cache_field_num_per_key_(zset_cache_field_num_per_key) { set_thread_name("PCacheLoadThread"); } @@ -36,45 +33,43 @@ PCacheLoadThread::~PCacheLoadThread() { StopThread(); } -void PCacheLoadThread::Push(const char key_type, std::string& key, const std::shared_ptr& db) { +void PCacheLoadThread::Push(const char key_type, std::string& key, PClient* client) { std::unique_lock lq(loadkeys_mutex_); std::unique_lock lm(loadkeys_map_mutex_); if (CACHE_LOAD_QUEUE_MAX_SIZE < loadkeys_queue_.size()) { // 5s to print logs once static uint64_t last_log_time_us = 0; if (pstd::NowMicros() - last_log_time_us > 5000000) { - WARN("PCacheLoadThread::Push waiting... "); + WARN("PCacheLoadThread::Push waiting... "); last_log_time_us = pstd::NowMicros(); } return; } if (loadkeys_map_.find(key) == loadkeys_map_.end()) { - std::tuple> ktuple = std::make_tuple(key_type, key, db); + std::tuple ktuple = std::make_tuple(key_type, key, client); loadkeys_queue_.push_back(ktuple); loadkeys_map_[key] = std::string(""); loadkeys_cond_.notify_all(); } } -bool PCacheLoadThread::LoadKV(std::string& key, const std::shared_ptr& db) { +bool PCacheLoadThread::LoadKV(std::string& key, PClient* client) { std::string value; int64_t ttl = -1; - rocksdb::Status s = db->GetStorage()->GetWithTTL(key, &value, &ttl); + rocksdb::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetWithTTL(key, &value, &ttl); if (!s.ok()) { - WARN("load kv failed, key={}",key); + WARN("load kv failed, key={}", key); return false; } - std::string CachePrefixKeyK = PCacheKeyPrefixK + key; - db->GetCache()->WriteKVToCache(CachePrefixKeyK, value, ttl); + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->WriteKVToCache(key, value, ttl); return true; } -// bool PCacheLoadThread::LoadHash(std::string& key, const std::shared_ptr& db) { +// bool PCacheLoadThread::LoadHash(std::string& key, PClient* client) { // int32_t len = 0; // db->storage()->HLen(key, &len); // if (0 >= len || CACHE_VALUE_ITEM_MAX_SIZE < len) { -// WARN("can not load key, because item size:{} beyond max item size:",len,CACHE_VALUE_ITEM_MAX_SIZE); // return false; // } @@ -82,37 +77,33 @@ bool PCacheLoadThread::LoadKV(std::string& key, const std::shared_ptr& db) { // int64_t ttl = -1; // rocksdb::Status s = db->storage()->HGetallWithTTL(key, &fvs, &ttl); // if (!s.ok()) { -// WARN("load hash failed, key={}",key); - +// LOG(WARNING) << "load hash failed, key=" << key; // return false; // } -// std::string CachePrefixKeyH = PCacheKeyPrefixH + key; -// db->cache()->WriteHashToCache(CachePrefixKeyH, fvs, ttl); +// db->cache()->WriteHashToCache(key, fvs, ttl); // return true; // } -// bool PCacheLoadThread::LoadList(std::string& key, const std::shared_ptr& db) { -// uint64_t len = 0; -// db->storage()->LLen(key, &len); -// if (len <= 0 || CACHE_VALUE_ITEM_MAX_SIZE < len) { -// LOG(WARNING) << "can not load key, because item size:" << len -// << " beyond max item size:" << CACHE_VALUE_ITEM_MAX_SIZE; -// return false; -// } +bool PCacheLoadThread::LoadList(std::string& key, PClient* client) { + uint64_t len = 0; + PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LLen(key, &len); + if (len <= 0 || CACHE_VALUE_ITEM_MAX_SIZE < len) { + WARN("can not load key, because item size: {} , beyond max item size: {} ", len, CACHE_VALUE_ITEM_MAX_SIZE); + return false; + } -// std::vector values; -// int64_t ttl = -1; -// rocksdb::Status s = db->storage()->LRangeWithTTL(key, 0, -1, &values, &ttl); -// if (!s.ok()) { -// LOG(WARNING) << "load list failed, key=" << key; -// return false; -// } -// std::string CachePrefixKeyL = PCacheKeyPrefixL + key; -// db->cache()->WriteListToCache(CachePrefixKeyL, values, ttl); -// return true; -// } + std::vector values; + int64_t ttl = -1; + rocksdb::Status s = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->LRangeWithTTL(key, 0, -1, &values, &ttl); + if (!s.ok()) { + WARN("load list failed, key= {}", key); + return false; + } + PSTORE.GetBackend(client->GetCurrentDB())->GetCache()->WriteListToCache(key, values, ttl); + return true; +} -// bool PCacheLoadThread::LoadSet(std::string& key, const std::shared_ptr& db) { +// bool PCacheLoadThread::LoadSet(std::string& key,PClient* client) { // int32_t len = 0; // db->storage()->SCard(key, &len); // if (0 >= len || CACHE_VALUE_ITEM_MAX_SIZE < len) { @@ -128,12 +119,11 @@ bool PCacheLoadThread::LoadKV(std::string& key, const std::shared_ptr& db) { // LOG(WARNING) << "load set failed, key=" << key; // return false; // } -// std::string CachePrefixKeyS = PCacheKeyPrefixS + key; -// db->cache()->WriteSetToCache(CachePrefixKeyS, values, ttl); +// db->cache()->WriteSetToCache(key, values, ttl); // return true; // } -// bool PCacheLoadThread::LoadZset(std::string& key, const std::shared_ptr& db) { +// bool PCacheLoadThread::LoadZset(std::string& key, PClient* client) { // int32_t len = 0; // int start_index = 0; // int stop_index = -1; @@ -143,8 +133,7 @@ bool PCacheLoadThread::LoadKV(std::string& key, const std::shared_ptr& db) { // } // uint64_t cache_len = 0; -// std::string CachePrefixKeyZ = PCacheKeyPrefixZ + key; -// db->cache()->CacheZCard(CachePrefixKeyZ, &cache_len); +// db->cache()->CacheZCard(key, &cache_len); // if (cache_len != 0) { // return true; // } @@ -165,35 +154,36 @@ bool PCacheLoadThread::LoadKV(std::string& key, const std::shared_ptr& db) { // LOG(WARNING) << "load zset failed, key=" << key; // return false; // } -// db->cache()->WriteZSetToCache(CachePrefixKeyZ, score_members, ttl); +// db->cache()->WriteZSetToCache(key, score_members, ttl); // return true; // } -bool PCacheLoadThread::LoadKey(const char key_type, std::string& key, const std::shared_ptr& db) { - // @tobeChecked 下面这行代码是pika实现中,分析pikiwidb中不再需要对DB上key锁,由Storage层来进行上锁(该两行留存,待确认无误后删除) +bool PCacheLoadThread::LoadKey(const char key_type, std::string& key, PClient* client) { + // @tobeChecked + // 下面这行代码是pika实现中,分析pikiwidb中不再需要对DB上key锁,由Storage层来进行上锁(该两行留存,待确认无误后删除) // pstd::lock::ScopeRecordLock record_lock(db->LockMgr(), key); switch (key_type) { case 'k': - return LoadKV(key, db); + return LoadKV(key, client); // case 'h': - // return LoadHash(key, db); - // case 'l': - // return LoadList(key, db); + // return LoadHash(key, client); + case 'l': + return LoadList(key, client); // case 's': - // return LoadSet(key, db); + // return LoadSet(key, client); // case 'z': - // return LoadZset(key, db); + // return LoadZset(key, client); default: - WARN("PCacheLoadThread::LoadKey invalid key type : {}",key_type); + WARN("PCacheLoadThread::LoadKey invalid key type : {}", key_type); return false; } } -void *PCacheLoadThread::ThreadMain() { - INFO("PCacheLoadThread::ThreadMain Start"); +void* PCacheLoadThread::ThreadMain() { + INFO("PCacheLoadThread::ThreadMain Start"); while (!should_exit_) { - std::deque>> load_keys; + std::deque> load_keys; { std::unique_lock lq(loadkeys_mutex_); waitting_load_keys_num_ = loadkeys_queue_.size(); @@ -212,17 +202,16 @@ void *PCacheLoadThread::ThreadMain() { } } } - for (auto iter = load_keys.begin(); iter != load_keys.end(); ++iter) { - if (LoadKey(std::get<0>(*iter), std::get<1>(*iter), std::get<2>(*iter))) { + for (auto& load_key : load_keys) { + if (LoadKey(std::get<0>(load_key), std::get<1>(load_key), std::get<2>(load_key))) { ++async_load_keys_num_; - } else { - WARN("PCacheLoadThread::ThreadMain LoadKey: {} failed!!!",std::get<1>(*iter)); } + std::unique_lock lm(loadkeys_map_mutex_); - loadkeys_map_.erase(std::get<1>(*iter)); + loadkeys_map_.erase(std::get<1>(load_key)); } } return nullptr; } -} // namespace cache +} // namespace pikiwidb diff --git a/src/pcache_load_thread.h b/src/pcache_load_thread.h index 4f8617546..49b494f41 100644 --- a/src/pcache_load_thread.h +++ b/src/pcache_load_thread.h @@ -5,7 +5,6 @@ * of patent rights can be found in the PATENTS file in the same directory. */ - #pragma once #include @@ -13,13 +12,11 @@ #include #include +#include "client.h" #include "pcache.h" -// #include "include/pika_define.h" #include "thread.h" -#include "db.h" -// #include "storage/storage.h" -namespace pikiwidb{ +namespace pikiwidb { class PCacheLoadThread : public Thread { public: @@ -28,20 +25,20 @@ class PCacheLoadThread : public Thread { uint64_t AsyncLoadKeysNum(void) { return async_load_keys_num_; } uint32_t WaittingLoadKeysNum(void) { return waitting_load_keys_num_; } - void Push(const char key_type, std::string& key, const std::shared_ptr& db); + void Push(const char key_type, std::string& key, PClient* client); private: - bool LoadKV(std::string& key, const std::shared_ptr& db); - bool LoadHash(std::string& key, const std::shared_ptr& db); - bool LoadList(std::string& key, const std::shared_ptr& db); - bool LoadSet(std::string& key, const std::shared_ptr& db); - bool LoadZset(std::string& key, const std::shared_ptr& db); - bool LoadKey(const char key_type, std::string& key, const std::shared_ptr& db); + bool LoadKV(std::string& key, PClient* client); + // bool LoadHash(std::string& key, PClient* client); + bool LoadList(std::string& key, PClient* client); + // bool LoadSet(std::string& key, PClient* client); + // bool LoadZset(std::string& key, PClient* client); + bool LoadKey(const char key_type, std::string& key, PClient* client); virtual void* ThreadMain() override; private: std::atomic_bool should_exit_; - std::deque>> loadkeys_queue_; + std::deque> loadkeys_queue_; pstd::CondVar loadkeys_cond_; pstd::Mutex loadkeys_mutex_; @@ -54,6 +51,5 @@ class PCacheLoadThread : public Thread { // currently only take effects to zset int zset_cache_start_direction_; int zset_cache_field_num_per_key_; - }; -} // namespace cache +} // namespace pikiwidb diff --git a/src/thread.h b/src/thread.h index 6b9126d2c..474ab55cc 100644 --- a/src/thread.h +++ b/src/thread.h @@ -11,8 +11,8 @@ #include #include -#include "pstd/pstd_mutex.h" #include "pstd/noncopyable.h" +#include "pstd/pstd_mutex.h" namespace pikiwidb {