From 4764f0bea8a50d4bf209776503929334c2b1ab19 Mon Sep 17 00:00:00 2001 From: Tao_ta <745103130@qq.com> Date: Tue, 5 Dec 2023 08:46:25 +0800 Subject: [PATCH 1/4] feat: string cmd setbit --- src/base_cmd.h | 1 + src/cmd_kv.cc | 57 ++++++++++++++++++++++++++++++++++++++++ src/cmd_kv.h | 11 ++++++++ src/cmd_table_manager.cc | 2 ++ 4 files changed, 71 insertions(+) diff --git a/src/base_cmd.h b/src/base_cmd.h index 65c5352a9..6d42b020a 100644 --- a/src/base_cmd.h +++ b/src/base_cmd.h @@ -34,6 +34,7 @@ const std::string kCmdNameStrlen = "strlen"; const std::string kCmdNameSetex = "setex"; const std::string kCmdNamePsetex = "psetex"; const std::string kCmdNameSetnx = "setnx"; +const std::string kCmdNameSetBit = "setbit"; // multi const std::string kCmdNameMulti = "multi"; diff --git a/src/cmd_kv.cc b/src/cmd_kv.cc index 724e55525..b1f7499b2 100644 --- a/src/cmd_kv.cc +++ b/src/cmd_kv.cc @@ -456,4 +456,61 @@ void SetnxCmd::DoCmd(PClient* client) { } } +SetBitCmd::SetBitCmd(const std::string& name, int16_t arity) + : BaseCmd(name, arity, CmdFlagsWrite, AclCategoryWrite | AclCategoryString) {} + +bool SetBitCmd::DoInitial(PClient* client) { + client->SetKey(client->argv_[1]); + return true; +} + +void SetBitCmd::DoCmd(PClient* client) { + PObject* value = nullptr; + PError err = PSTORE.GetValueByType(client->Key(), value, PType_string); + if (err == PError_notExist) { + value = PSTORE.SetValue(client->Key(), PObject::CreateString("")); + err = PError_ok; + } + + if (err != PError_ok) { + client->AppendInteger(0); + return; + } + + long offset = 0; + long on = 0; + if (!Strtol(client->argv_[2].c_str(), client->argv_[2].size(), &offset) || + !Strtol(client->argv_[3].c_str(), client->argv_[3].size(), &on)) { + client->SetRes(CmdRes::kInvalidInt); + return; + } + + if (offset < 0 || offset > kStringMaxBytes) { + client->AppendInteger(0); + return; + } + + PString newVal(*GetDecodedString(value)); + + size_t bytes = offset / 8; + size_t bits = offset % 8; + + if (bytes + 1 > newVal.size()) { + newVal.resize(bytes + 1, '\0'); + } + + const char oldByte = newVal[bytes]; + char& byte = newVal[bytes]; + if (on) { + byte |= (0x1 << bits); + } else { + byte &= ~(0x1 << bits); + } + + value->Reset(new PString(newVal)); + value->encoding = PEncode_raw; + client->AppendInteger((oldByte & (0x1 << bits)) ? 1 : 0); + return; +} + } // namespace pikiwidb \ No newline at end of file diff --git a/src/cmd_kv.h b/src/cmd_kv.h index b28ebbcff..4d7a4c368 100644 --- a/src/cmd_kv.h +++ b/src/cmd_kv.h @@ -160,4 +160,15 @@ class IncrbyCmd : public BaseCmd { void DoCmd(PClient *client) override; }; +class SetBitCmd : public BaseCmd { + public: + SetBitCmd(const std::string &name, int16_t arity); + + protected: + bool DoInitial(PClient *client) override; + + private: + void DoCmd(PClient *client) override; +}; + } // namespace pikiwidb diff --git a/src/cmd_table_manager.cc b/src/cmd_table_manager.cc index 32277a9b2..dd9875dab 100644 --- a/src/cmd_table_manager.cc +++ b/src/cmd_table_manager.cc @@ -62,6 +62,8 @@ void CmdTableManager::InitCmdTable() { cmds_->insert(std::make_pair(kCmdNamePsetex, std::move(psetexPtr))); std::unique_ptr setnxPtr = std::make_unique(kCmdNameSetnx, 3); cmds_->insert(std::make_pair(kCmdNameSetnx, std::move(setnxPtr))); + std::unique_ptr setbitPtr = std::make_unique(kCmdNameSetBit, 4); + cmds_->insert(std::make_pair(kCmdNameSetBit, std::move(setbitPtr))); } std::pair CmdTableManager::GetCommand(const std::string& cmdName, PClient* client) { From 8c58ddbde9daa4bd9e1e8d251241cf6d7f9b8204 Mon Sep 17 00:00:00 2001 From: Tao_ta <745103130@qq.com> Date: Tue, 12 Dec 2023 08:39:34 +0800 Subject: [PATCH 2/4] solving format error --- src/cmd_kv.cc | 2 +- src/pstd/env.cc | 42 +++--- src/pstd/env.h | 12 +- src/pstd/lock_mgr.cc | 2 +- src/pstd/lock_mgr.h | 9 +- src/pstd/mutex.h | 3 +- src/pstd/mutex_impl.cc | 2 +- src/pstd/mutex_impl.h | 4 +- src/pstd/noncopyable.h | 6 +- src/pstd/pstd_defer.h | 2 +- src/pstd/pstd_mutex.h | 19 +-- src/pstd/scope_record_lock.cc | 5 +- src/pstd/scope_record_lock.h | 14 +- src/storage/include/storage/storage.h | 12 +- src/storage/include/storage/util.h | 1 - src/storage/src/base_data_key_format.h | 3 +- src/storage/src/base_filter.h | 4 +- src/storage/src/base_value_format.h | 7 +- src/storage/src/custom_comparator.h | 9 +- src/storage/src/debug.h | 6 +- src/storage/src/lists_data_key_format.h | 2 +- src/storage/src/lists_filter.h | 9 +- src/storage/src/lists_meta_value_format.h | 9 +- src/storage/src/lru_cache.h | 2 +- src/storage/src/redis.cc | 155 +++++++++++----------- src/storage/src/redis.h | 2 +- src/storage/src/redis_hashes.cc | 16 +-- src/storage/src/redis_lists.cc | 15 ++- src/storage/src/redis_sets.cc | 81 ++++++----- src/storage/src/redis_sets.h | 9 +- src/storage/src/redis_strings.cc | 20 +-- src/storage/src/redis_strings.h | 3 +- src/storage/src/redis_zsets.cc | 36 ++--- src/storage/src/storage.cc | 25 ++-- src/storage/src/strings_value_format.h | 3 +- src/storage/src/util.cc | 11 +- src/storage/src/zsets_data_key_format.h | 2 +- 37 files changed, 285 insertions(+), 279 deletions(-) mode change 100755 => 100644 src/pstd/pstd_defer.h diff --git a/src/cmd_kv.cc b/src/cmd_kv.cc index 0b271f5e0..3063cac05 100644 --- a/src/cmd_kv.cc +++ b/src/cmd_kv.cc @@ -597,4 +597,4 @@ void SetBitCmd::DoCmd(PClient* client) { return; } -} +} // namespace pikiwidb diff --git a/src/pstd/env.cc b/src/pstd/env.cc index 6ac5f323c..5dc40ae05 100644 --- a/src/pstd/env.cc +++ b/src/pstd/env.cc @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. + * Copyright (c) 2023-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. @@ -18,15 +18,15 @@ #include #include #include -#include #include +#include #include #if __has_include() -#include +# include namespace filesystem = std::filesystem; #elif __has_include() -#include +# include namespace filesystem = std::experimental::filesystem; #endif @@ -188,9 +188,7 @@ int DeleteDir(const std::string& path) { return -1; } -bool DeleteDirIfExist(const std::string& path) { - return !(IsDir(path) == 0 && DeleteDir(path) != 0); -} +bool DeleteDirIfExist(const std::string& path) { return !(IsDir(path) == 0 && DeleteDir(path) != 0); } uint64_t Du(const std::string& path) { uint64_t sum = 0; @@ -238,7 +236,7 @@ class PosixSequentialFile : public SequentialFile { public: virtual void setUnBuffer() { setbuf(file_, nullptr); } - PosixSequentialFile(std::string fname, FILE* f) : filename_(std::move(fname)), file_(f) { setbuf(file_, nullptr); } + PosixSequentialFile(std::string fname, FILE* f) : filename_(std::move(fname)), file_(f) { setbuf(file_, nullptr); } ~PosixSequentialFile() override { if (file_) { @@ -292,14 +290,14 @@ class PosixMmapFile : public WritableFile { private: std::string filename_; int fd_ = -1; - size_t page_size_ = 0; - size_t map_size_ = 0; // How much extra memory to map at a time - char* base_ = nullptr; // The mapped region - char* limit_ = nullptr; // Limit of the mapped region - char* dst_ = nullptr; // Where to write next (in range [base_,limit_]) - char* last_sync_ = nullptr; // Where have we synced up to - uint64_t file_offset_ = 0; // Offset of base_ in file - uint64_t write_len_ = 0; // The data that written in the file + size_t page_size_ = 0; + size_t map_size_ = 0; // How much extra memory to map at a time + char* base_ = nullptr; // The mapped region + char* limit_ = nullptr; // Limit of the mapped region + char* dst_ = nullptr; // Where to write next (in range [base_,limit_]) + char* last_sync_ = nullptr; // Where have we synced up to + uint64_t file_offset_ = 0; // Offset of base_ in file + uint64_t write_len_ = 0; // The data that written in the file // Have we done an munmap of unsynced data? bool pending_sync_ = false; @@ -362,14 +360,13 @@ class PosixMmapFile : public WritableFile { } public: - PosixMmapFile(std::string fname, int fd, size_t page_size, uint64_t write_len = 0) + PosixMmapFile(std::string fname, int fd, size_t page_size, uint64_t write_len = 0) : filename_(std::move(fname)), fd_(fd), page_size_(page_size), map_size_(Roundup(kMmapBoundSize, page_size)), - write_len_(write_len) - { + write_len_(write_len) { if (write_len_ != 0) { while (map_size_ < write_len_) { map_size_ += (1024 * 1024); @@ -480,7 +477,7 @@ RWFile::~RWFile() = default; class MmapRWFile : public RWFile { public: - MmapRWFile(std::string fname, int fd, size_t page_size) + MmapRWFile(std::string fname, int fd, size_t page_size) : filename_(std::move(fname)), fd_(fd), page_size_(page_size), map_size_(Roundup(65536, page_size)) { DoMapRegion(); } @@ -514,7 +511,7 @@ class MmapRWFile : public RWFile { static size_t Roundup(size_t x, size_t y) { return ((x + y - 1) / y) * y; } std::string filename_; int fd_ = -1; - size_t page_size_[[maybe_unused]] = 0; + size_t page_size_ [[maybe_unused]] = 0; size_t map_size_ = 0; char* base_ = nullptr; }; @@ -528,8 +525,7 @@ class PosixRandomRWFile : public RandomRWFile { // bool fallocate_with_keep_size_; public: - PosixRandomRWFile(std::string fname, int fd) - : filename_(std::move(fname)), fd_(fd) { + PosixRandomRWFile(std::string fname, int fd) : filename_(std::move(fname)), fd_(fd) { // fallocate_with_keep_size_ = options.fallocate_with_keep_size; } diff --git a/src/pstd/env.h b/src/pstd/env.h index 6451c94f7..12a94021b 100644 --- a/src/pstd/env.h +++ b/src/pstd/env.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. + * Copyright (c) 2023-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. @@ -8,12 +8,12 @@ #pragma once #include +#include #include #include -#include -#include "pstd_status.h" #include "noncopyable.h" +#include "pstd_status.h" namespace pstd { @@ -57,7 +57,8 @@ int RenameFile(const std::string& oldname, const std::string& newname); class FileLock : public pstd::noncopyable { public: FileLock() = default; - virtual ~FileLock()= default;; + virtual ~FileLock() = default; + ; int fd_ = -1; std::string name_; @@ -100,7 +101,8 @@ class WritableFile : public pstd::noncopyable { // A abstract for the sequential readable file class SequentialFile { public: - SequentialFile()= default;; + SequentialFile() = default; + ; virtual ~SequentialFile(); // virtual Status Read(size_t n, char *&result, char *scratch) = 0; virtual Status Read(size_t n, Slice* result, char* scratch) = 0; diff --git a/src/pstd/lock_mgr.cc b/src/pstd/lock_mgr.cc index 930c6af54..8b5816ecd 100644 --- a/src/pstd/lock_mgr.cc +++ b/src/pstd/lock_mgr.cc @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. + * Copyright (c) 2023-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. diff --git a/src/pstd/lock_mgr.h b/src/pstd/lock_mgr.h index 5d172b81e..c84fa0715 100644 --- a/src/pstd/lock_mgr.h +++ b/src/pstd/lock_mgr.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. + * Copyright (c) 2023-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. @@ -20,7 +20,7 @@ struct LockMap; struct LockMapStripe; class LockMgr : public pstd::noncopyable { -public: + public: LockMgr(size_t default_num_stripes, int64_t max_num_locks, const std::shared_ptr& factory); ~LockMgr(); @@ -32,9 +32,9 @@ class LockMgr : public pstd::noncopyable { // Unlock a key locked by TryLock(). void UnLock(const std::string& key); -private: + private: // Default number of lock map stripes - const size_t default_num_stripes_[[maybe_unused]]; + const size_t default_num_stripes_ [[maybe_unused]]; // Limit on number of keys locked per column family const int64_t max_num_locks_; @@ -50,7 +50,6 @@ class LockMgr : public pstd::noncopyable { Status AcquireLocked(const std::shared_ptr& stripe, const std::string& key); void UnLockKey(const std::string& key, const std::shared_ptr& stripe); - }; } // namespace lock diff --git a/src/pstd/mutex.h b/src/pstd/mutex.h index b768d2b2a..5e5af3168 100644 --- a/src/pstd/mutex.h +++ b/src/pstd/mutex.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. + * Copyright (c) 2023-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. @@ -83,4 +83,3 @@ class MutexFactory { }; } // namespace pstd::lock - diff --git a/src/pstd/mutex_impl.cc b/src/pstd/mutex_impl.cc index 80568b88c..ae6388d83 100644 --- a/src/pstd/mutex_impl.cc +++ b/src/pstd/mutex_impl.cc @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. + * Copyright (c) 2023-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. diff --git a/src/pstd/mutex_impl.h b/src/pstd/mutex_impl.h index 5165933f1..b2008903c 100644 --- a/src/pstd/mutex_impl.h +++ b/src/pstd/mutex_impl.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. + * Copyright (c) 2023-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. @@ -15,7 +15,7 @@ namespace pstd { namespace lock { // Default implementation of MutexFactory. class MutexFactoryImpl : public MutexFactory { -public: + public: std::shared_ptr AllocateMutex() override; std::shared_ptr AllocateCondVar() override; }; diff --git a/src/pstd/noncopyable.h b/src/pstd/noncopyable.h index 039c8d39c..786bacec0 100644 --- a/src/pstd/noncopyable.h +++ b/src/pstd/noncopyable.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. + * Copyright (c) 2023-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. @@ -10,11 +10,11 @@ namespace pstd { class noncopyable { -protected: + protected: noncopyable() = default; ~noncopyable() = default; -private: + private: noncopyable(const noncopyable&) = delete; void operator=(const noncopyable&) = delete; }; diff --git a/src/pstd/pstd_defer.h b/src/pstd/pstd_defer.h old mode 100755 new mode 100644 index 4929c9323..5876fc1a5 --- a/src/pstd/pstd_defer.h +++ b/src/pstd/pstd_defer.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. + * Copyright (c) 2023-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. diff --git a/src/pstd/pstd_mutex.h b/src/pstd/pstd_mutex.h index 2c0a5f928..b93fc4261 100644 --- a/src/pstd/pstd_mutex.h +++ b/src/pstd/pstd_mutex.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. + * Copyright (c) 2023-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. @@ -32,7 +32,7 @@ void InitOnce(OnceType& once, F&& f, Args&&... args) { } class RefMutex : public pstd::noncopyable { -public: + public: RefMutex() = default; ~RefMutex() = default; @@ -45,14 +45,15 @@ class RefMutex : public pstd::noncopyable { void Unref(); bool IsLastRef() { return refs_ == 1; } -private: + private: std::mutex mu_; int refs_ = 0; }; class RecordMutex : public pstd::noncopyable { -public: - RecordMutex()= default;; + public: + RecordMutex() = default; + ; ~RecordMutex(); void MultiLock(const std::vector& keys); @@ -60,18 +61,18 @@ class RecordMutex : public pstd::noncopyable { void MultiUnlock(const std::vector& keys); void Unlock(const std::string& key); -private: + private: Mutex mutex_; std::unordered_map records_; }; class RecordLock : public pstd::noncopyable { -public: - RecordLock(RecordMutex* mu, std::string key) : mu_(mu), key_(std::move(key)) { mu_->Lock(key_); } + public: + RecordLock(RecordMutex* mu, std::string key) : mu_(mu), key_(std::move(key)) { mu_->Lock(key_); } ~RecordLock() { mu_->Unlock(key_); } -private: + private: RecordMutex* const mu_; std::string key_; }; diff --git a/src/pstd/scope_record_lock.cc b/src/pstd/scope_record_lock.cc index 6ef391bd3..13d541cc8 100644 --- a/src/pstd/scope_record_lock.cc +++ b/src/pstd/scope_record_lock.cc @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. + * Copyright (c) 2023-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. @@ -11,7 +11,8 @@ namespace pstd::lock { -MultiScopeRecordLock::MultiScopeRecordLock(const std::shared_ptr& lock_mgr, const std::vector& keys) +MultiScopeRecordLock::MultiScopeRecordLock(const std::shared_ptr& lock_mgr, + const std::vector& keys) : lock_mgr_(lock_mgr), keys_(keys) { std::string pre_key; std::sort(keys_.begin(), keys_.end()); diff --git a/src/pstd/scope_record_lock.h b/src/pstd/scope_record_lock.h index a17e16062..49dbb87d2 100644 --- a/src/pstd/scope_record_lock.h +++ b/src/pstd/scope_record_lock.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. + * Copyright (c) 2023-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. @@ -21,36 +21,36 @@ namespace pstd::lock { using Slice = rocksdb::Slice; class ScopeRecordLock final : public pstd::noncopyable { -public: + public: ScopeRecordLock(const std::shared_ptr& lock_mgr, const Slice& key) : lock_mgr_(lock_mgr), key_(key) { lock_mgr_->TryLock(key_.ToString()); } ~ScopeRecordLock() { lock_mgr_->UnLock(key_.ToString()); } -private: + private: std::shared_ptr const lock_mgr_; Slice key_; }; class MultiScopeRecordLock final : public pstd::noncopyable { -public: + public: MultiScopeRecordLock(const std::shared_ptr& lock_mgr, const std::vector& keys); ~MultiScopeRecordLock(); -private: + private: std::shared_ptr const lock_mgr_; std::vector keys_; }; class MultiRecordLock : public noncopyable { -public: + public: explicit MultiRecordLock(const std::shared_ptr& lock_mgr) : lock_mgr_(lock_mgr) {} ~MultiRecordLock() = default; void Lock(const std::vector& keys); void Unlock(const std::vector& keys); -private: + private: std::shared_ptr const lock_mgr_; }; diff --git a/src/storage/include/storage/storage.h b/src/storage/include/storage/storage.h index 0ea59b7b5..9161279de 100644 --- a/src/storage/include/storage/storage.h +++ b/src/storage/include/storage/storage.h @@ -219,7 +219,8 @@ class Storage { // Perform a bitwise operation between multiple keys // and store the result in the destination key - Status BitOp(BitOpType op, const std::string& dest_key, const std::vector& src_keys, std::string &value_to_dest, int64_t* ret); + Status BitOp(BitOpType op, const std::string& dest_key, const std::vector& src_keys, + std::string& value_to_dest, int64_t* ret); // Return the position of the first bit set to 1 or 0 in a string // BitPos key 0 @@ -383,7 +384,8 @@ class Storage { // key3 = {a, c, e} // SDIFFSTORE destination key1 key2 key3 // destination = {b, d} - Status SDiffstore(const Slice& destination, const std::vector& keys, std::vector& value_to_dest, int32_t* ret); + Status SDiffstore(const Slice& destination, const std::vector& keys, + std::vector& value_to_dest, int32_t* ret); // Returns the members of the set resulting from the intersection of all the // given sets. @@ -406,7 +408,8 @@ class Storage { // key3 = {a, c, e} // SINTERSTORE destination key1 key2 key3 // destination = {a, c} - Status SInterstore(const Slice& destination, const std::vector& keys, std::vector& value_to_dest, int32_t* ret); + Status SInterstore(const Slice& destination, const std::vector& keys, + std::vector& value_to_dest, int32_t* ret); // Returns if member is a member of the set stored at key. Status SIsmember(const Slice& key, const Slice& member, int32_t* ret); @@ -463,7 +466,8 @@ class Storage { // key3 = {c, d, e} // SUNIONSTORE destination key1 key2 key3 // destination = {a, b, c, d, e} - Status SUnionstore(const Slice& destination, const std::vector& keys, std::vector& value_to_dest, int32_t* ret); + Status SUnionstore(const Slice& destination, const std::vector& keys, + std::vector& value_to_dest, int32_t* ret); // See SCAN for SSCAN documentation. Status SScan(const Slice& key, int64_t cursor, const std::string& pattern, int64_t count, diff --git a/src/storage/include/storage/util.h b/src/storage/include/storage/util.h index 039888158..adf8f8325 100644 --- a/src/storage/include/storage/util.h +++ b/src/storage/include/storage/util.h @@ -29,4 +29,3 @@ bool isTailWildcard(const std::string& pattern); void GetFilepath(const char* path, const char* filename, char* filepath); bool DeleteFiles(const char* path); } // namespace storage - diff --git a/src/storage/src/base_data_key_format.h b/src/storage/src/base_data_key_format.h index 696aec65a..8057905bf 100644 --- a/src/storage/src/base_data_key_format.h +++ b/src/storage/src/base_data_key_format.h @@ -10,8 +10,7 @@ namespace storage { class BaseDataKey { public: - BaseDataKey(const Slice& key, int32_t version, const Slice& data) - : key_(key), version_(version), data_(data) {} + BaseDataKey(const Slice& key, int32_t version, const Slice& data) : key_(key), version_(version), data_(data) {} ~BaseDataKey() { if (start_ != space_) { diff --git a/src/storage/src/base_filter.h b/src/storage/src/base_filter.h index 25180aef0..fc8f4f031 100644 --- a/src/storage/src/base_filter.h +++ b/src/storage/src/base_filter.h @@ -59,9 +59,7 @@ class BaseMetaFilterFactory : public rocksdb::CompactionFilterFactory { class BaseDataFilter : public rocksdb::CompactionFilter { public: BaseDataFilter(rocksdb::DB* db, std::vector* cf_handles_ptr) - : db_(db), - cf_handles_ptr_(cf_handles_ptr) - {} + : db_(db), cf_handles_ptr_(cf_handles_ptr) {} bool Filter(int level, const Slice& key, const rocksdb::Slice& value, std::string* new_value, bool* value_changed) const override { diff --git a/src/storage/src/base_value_format.h b/src/storage/src/base_value_format.h index 292732a71..caf4faef4 100644 --- a/src/storage/src/base_value_format.h +++ b/src/storage/src/base_value_format.h @@ -16,8 +16,7 @@ namespace storage { class InternalValue { public: - explicit InternalValue(const rocksdb::Slice& user_value) - : user_value_(user_value) {} + explicit InternalValue(const rocksdb::Slice& user_value) : user_value_(user_value) {} virtual ~InternalValue() { if (start_ != space_) { delete[] start_; @@ -74,7 +73,7 @@ class ParsedInternalValue { // since we use this in Compaction process, all we need to do is parsing // the rocksdb::Slice, so don't need to modify the original value, value_ can be // set to nullptr - explicit ParsedInternalValue(const rocksdb::Slice& value) {} + explicit ParsedInternalValue(const rocksdb::Slice& value) {} virtual ~ParsedInternalValue() = default; @@ -119,7 +118,7 @@ class ParsedInternalValue { virtual void SetTimestampToValue() = 0; std::string* value_ = nullptr; rocksdb::Slice user_value_; - int32_t version_ = 0 ; + int32_t version_ = 0; int32_t timestamp_ = 0; }; diff --git a/src/storage/src/custom_comparator.h b/src/storage/src/custom_comparator.h index 4df0813a0..c59eac67a 100644 --- a/src/storage/src/custom_comparator.h +++ b/src/storage/src/custom_comparator.h @@ -9,8 +9,8 @@ #include -#include "src/coding.h" #include "rocksdb/comparator.h" +#include "src/coding.h" namespace storage { @@ -104,7 +104,7 @@ class ZSetsScoreKeyComparatorImpl : public rocksdb::Comparator { ptr_a += key_a_len + 2 * sizeof(int32_t); ptr_b += key_b_len + 2 * sizeof(int32_t); int ret = key_a_prefix.compare(key_b_prefix); - if (ret) { + if (ret) { return ret; } @@ -129,7 +129,7 @@ class ZSetsScoreKeyComparatorImpl : public rocksdb::Comparator { rocksdb::Slice key_a_member(ptr_a, a_size - (ptr_a - a.data())); rocksdb::Slice key_b_member(ptr_b, b_size - (ptr_b - b.data())); ret = key_a_member.compare(key_b_member); - if (ret) { + if (ret) { return ret; } } @@ -157,7 +157,8 @@ class ZSetsScoreKeyComparatorImpl : public rocksdb::Comparator { ptr += sizeof(uint64_t); std::string member(ptr, str.size() - (key_len + 2 * sizeof(int32_t) + sizeof(uint64_t))); - LOG(INFO) << from.data() << ": total_len[" << str.size() << "], key_len[" << key_len << "], key[" << key.data() << "], " + LOG(INFO) << from.data() << ": total_len[" << str.size() << "], key_len[" << key_len << "], key[" << key.data() + << "], " << "version[ " << version << "], score[" << score << "], member[" << member.data() << "]"; } diff --git a/src/storage/src/debug.h b/src/storage/src/debug.h index 000c20017..040c2ed5e 100644 --- a/src/storage/src/debug.h +++ b/src/storage/src/debug.h @@ -9,6 +9,8 @@ # define TRACE(M, ...) fprintf(stderr, "[TRACE] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__) # define DEBUG(M, ...) fprintf(stderr, "[Debug] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__) #else -# define TRACE(M, ...) {} -# define DEBUG(M, ...) {} +# define TRACE(M, ...) \ + {} +# define DEBUG(M, ...) \ + {} #endif // NDEBUG diff --git a/src/storage/src/lists_data_key_format.h b/src/storage/src/lists_data_key_format.h index 8bcc7b232..207bece5b 100644 --- a/src/storage/src/lists_data_key_format.h +++ b/src/storage/src/lists_data_key_format.h @@ -13,7 +13,7 @@ namespace storage { class ListsDataKey { public: ListsDataKey(const rocksdb::Slice& key, int32_t version, uint64_t index) - : key_(key), version_(version), index_(index) {} + : key_(key), version_(version), index_(index) {} ~ListsDataKey() { if (start_ != space_) { diff --git a/src/storage/src/lists_filter.h b/src/storage/src/lists_filter.h index 4da2124d9..a2f0d1530 100644 --- a/src/storage/src/lists_filter.h +++ b/src/storage/src/lists_filter.h @@ -60,16 +60,15 @@ class ListsMetaFilterFactory : public rocksdb::CompactionFilterFactory { class ListsDataFilter : public rocksdb::CompactionFilter { public: ListsDataFilter(rocksdb::DB* db, std::vector* cf_handles_ptr) - : db_(db), - cf_handles_ptr_(cf_handles_ptr) - {} + : db_(db), cf_handles_ptr_(cf_handles_ptr) {} bool Filter(int level, const rocksdb::Slice& key, const rocksdb::Slice& value, std::string* new_value, bool* value_changed) const override { ParsedListsDataKey parsed_lists_data_key(key); TRACE("==========================START=========================="); - TRACE("[DataFilter], key: %s, index = %llu, data = %s, version = %d", parsed_lists_data_key.key().ToString().c_str(), - parsed_lists_data_key.index(), value.ToString().c_str(), parsed_lists_data_key.version()); + TRACE("[DataFilter], key: %s, index = %llu, data = %s, version = %d", + parsed_lists_data_key.key().ToString().c_str(), parsed_lists_data_key.index(), value.ToString().c_str(), + parsed_lists_data_key.version()); if (parsed_lists_data_key.key().ToString() != cur_key_) { cur_key_ = parsed_lists_data_key.key().ToString(); diff --git a/src/storage/src/lists_meta_value_format.h b/src/storage/src/lists_meta_value_format.h index 70f0ea922..7ec170ca8 100644 --- a/src/storage/src/lists_meta_value_format.h +++ b/src/storage/src/lists_meta_value_format.h @@ -82,11 +82,11 @@ class ListsMetaValue : public InternalValue { class ParsedListsMetaValue : public ParsedInternalValue { public: // Use this constructor after rocksdb::DB::Get(); - explicit ParsedListsMetaValue(std::string* internal_value_str) - : ParsedInternalValue(internal_value_str) { + explicit ParsedListsMetaValue(std::string* internal_value_str) : ParsedInternalValue(internal_value_str) { assert(internal_value_str->size() >= kListsMetaValueSuffixLength); if (internal_value_str->size() >= kListsMetaValueSuffixLength) { - user_value_ = rocksdb::Slice(internal_value_str->data(), internal_value_str->size() - kListsMetaValueSuffixLength); + user_value_ = + rocksdb::Slice(internal_value_str->data(), internal_value_str->size() - kListsMetaValueSuffixLength); version_ = DecodeFixed32(internal_value_str->data() + internal_value_str->size() - sizeof(int32_t) * 2 - sizeof(int64_t) * 2); timestamp_ = DecodeFixed32(internal_value_str->data() + internal_value_str->size() - sizeof(int32_t) - @@ -102,7 +102,8 @@ class ParsedListsMetaValue : public ParsedInternalValue { : ParsedInternalValue(internal_value_slice) { assert(internal_value_slice.size() >= kListsMetaValueSuffixLength); if (internal_value_slice.size() >= kListsMetaValueSuffixLength) { - user_value_ = rocksdb::Slice(internal_value_slice.data(), internal_value_slice.size() - kListsMetaValueSuffixLength); + user_value_ = + rocksdb::Slice(internal_value_slice.data(), internal_value_slice.size() - kListsMetaValueSuffixLength); version_ = DecodeFixed32(internal_value_slice.data() + internal_value_slice.size() - sizeof(int32_t) * 2 - sizeof(int64_t) * 2); timestamp_ = DecodeFixed32(internal_value_slice.data() + internal_value_slice.size() - sizeof(int32_t) - diff --git a/src/storage/src/lru_cache.h b/src/storage/src/lru_cache.h index 07eead022..e15c38d63 100644 --- a/src/storage/src/lru_cache.h +++ b/src/storage/src/lru_cache.h @@ -122,7 +122,7 @@ class LRUCache { }; template -LRUCache::LRUCache() { +LRUCache::LRUCache() { // Make empty circular linked lists. lru_.next = &lru_; lru_.prev = &lru_; diff --git a/src/storage/src/redis.cc b/src/storage/src/redis.cc index 6a7bea8e6..7f823a612 100644 --- a/src/storage/src/redis.cc +++ b/src/storage/src/redis.cc @@ -88,85 +88,84 @@ Status Redis::SetOptions(const OptionType& option_type, const std::unordered_map return s; } -void Redis::GetRocksDBInfo(std::string &info, const char *prefix) { - std::ostringstream string_stream; - string_stream << "#" << prefix << "RocksDB" << "\r\n"; - - auto write_stream_key_value=[&](const Slice& property, const char *metric) { - uint64_t value; - db_->GetAggregatedIntProperty(property, &value); - string_stream << prefix << metric << ':' << value << "\r\n"; - }; - - auto mapToString=[&](const std::map& map_data, const char *prefix) { - for (const auto& kv : map_data) { - std::string str_data; - str_data += kv.first + ": " + kv.second + "\r\n"; - string_stream << prefix << str_data; - } - }; - - // memtables num - write_stream_key_value(rocksdb::DB::Properties::kNumImmutableMemTable, "num_immutable_mem_table"); - write_stream_key_value(rocksdb::DB::Properties::kNumImmutableMemTableFlushed, "num_immutable_mem_table_flushed"); - write_stream_key_value(rocksdb::DB::Properties::kMemTableFlushPending, "mem_table_flush_pending"); - write_stream_key_value(rocksdb::DB::Properties::kNumRunningFlushes, "num_running_flushes"); - - // compaction - write_stream_key_value(rocksdb::DB::Properties::kCompactionPending, "compaction_pending"); - write_stream_key_value(rocksdb::DB::Properties::kNumRunningCompactions, "num_running_compactions"); - - // background errors - write_stream_key_value(rocksdb::DB::Properties::kBackgroundErrors, "background_errors"); - - // memtables size - write_stream_key_value(rocksdb::DB::Properties::kCurSizeActiveMemTable, "cur_size_active_mem_table"); - write_stream_key_value(rocksdb::DB::Properties::kCurSizeAllMemTables, "cur_size_all_mem_tables"); - write_stream_key_value(rocksdb::DB::Properties::kSizeAllMemTables, "size_all_mem_tables"); - - // keys - write_stream_key_value(rocksdb::DB::Properties::kEstimateNumKeys, "estimate_num_keys"); - - // table readers mem - write_stream_key_value(rocksdb::DB::Properties::kEstimateTableReadersMem, "estimate_table_readers_mem"); - - // snapshot - write_stream_key_value(rocksdb::DB::Properties::kNumSnapshots, "num_snapshots"); - - // version - write_stream_key_value(rocksdb::DB::Properties::kNumLiveVersions, "num_live_versions"); - write_stream_key_value(rocksdb::DB::Properties::kCurrentSuperVersionNumber, "current_super_version_number"); - - // live data size - write_stream_key_value(rocksdb::DB::Properties::kEstimateLiveDataSize, "estimate_live_data_size"); - - // sst files - write_stream_key_value(rocksdb::DB::Properties::kTotalSstFilesSize, "total_sst_files_size"); - write_stream_key_value(rocksdb::DB::Properties::kLiveSstFilesSize, "live_sst_files_size"); - - // pending compaction bytes - write_stream_key_value(rocksdb::DB::Properties::kEstimatePendingCompactionBytes, "estimate_pending_compaction_bytes"); - - // block cache - write_stream_key_value(rocksdb::DB::Properties::kBlockCacheCapacity, "block_cache_capacity"); - write_stream_key_value(rocksdb::DB::Properties::kBlockCacheUsage, "block_cache_usage"); - write_stream_key_value(rocksdb::DB::Properties::kBlockCachePinnedUsage, "block_cache_pinned_usage"); - - // blob files - write_stream_key_value(rocksdb::DB::Properties::kNumBlobFiles, "num_blob_files"); - write_stream_key_value(rocksdb::DB::Properties::kBlobStats, "blob_stats"); - write_stream_key_value(rocksdb::DB::Properties::kTotalBlobFileSize, "total_blob_file_size"); - write_stream_key_value(rocksdb::DB::Properties::kLiveBlobFileSize, "live_blob_file_size"); - - // column family stats - std::map mapvalues; - db_->rocksdb::DB::GetMapProperty(rocksdb::DB::Properties::kCFStats,&mapvalues); - mapToString(mapvalues,prefix); - info.append(string_stream.str()); -} +void Redis::GetRocksDBInfo(std::string& info, const char* prefix) { + std::ostringstream string_stream; + string_stream << "#" << prefix << "RocksDB" + << "\r\n"; + + auto write_stream_key_value = [&](const Slice& property, const char* metric) { + uint64_t value; + db_->GetAggregatedIntProperty(property, &value); + string_stream << prefix << metric << ':' << value << "\r\n"; + }; + + auto mapToString = [&](const std::map& map_data, const char* prefix) { + for (const auto& kv : map_data) { + std::string str_data; + str_data += kv.first + ": " + kv.second + "\r\n"; + string_stream << prefix << str_data; + } + }; + + // memtables num + write_stream_key_value(rocksdb::DB::Properties::kNumImmutableMemTable, "num_immutable_mem_table"); + write_stream_key_value(rocksdb::DB::Properties::kNumImmutableMemTableFlushed, "num_immutable_mem_table_flushed"); + write_stream_key_value(rocksdb::DB::Properties::kMemTableFlushPending, "mem_table_flush_pending"); + write_stream_key_value(rocksdb::DB::Properties::kNumRunningFlushes, "num_running_flushes"); + + // compaction + write_stream_key_value(rocksdb::DB::Properties::kCompactionPending, "compaction_pending"); + write_stream_key_value(rocksdb::DB::Properties::kNumRunningCompactions, "num_running_compactions"); + + // background errors + write_stream_key_value(rocksdb::DB::Properties::kBackgroundErrors, "background_errors"); + + // memtables size + write_stream_key_value(rocksdb::DB::Properties::kCurSizeActiveMemTable, "cur_size_active_mem_table"); + write_stream_key_value(rocksdb::DB::Properties::kCurSizeAllMemTables, "cur_size_all_mem_tables"); + write_stream_key_value(rocksdb::DB::Properties::kSizeAllMemTables, "size_all_mem_tables"); + + // keys + write_stream_key_value(rocksdb::DB::Properties::kEstimateNumKeys, "estimate_num_keys"); + + // table readers mem + write_stream_key_value(rocksdb::DB::Properties::kEstimateTableReadersMem, "estimate_table_readers_mem"); -void Redis::SetWriteWalOptions(const bool is_wal_disable) { - default_write_options_.disableWAL = is_wal_disable; + // snapshot + write_stream_key_value(rocksdb::DB::Properties::kNumSnapshots, "num_snapshots"); + + // version + write_stream_key_value(rocksdb::DB::Properties::kNumLiveVersions, "num_live_versions"); + write_stream_key_value(rocksdb::DB::Properties::kCurrentSuperVersionNumber, "current_super_version_number"); + + // live data size + write_stream_key_value(rocksdb::DB::Properties::kEstimateLiveDataSize, "estimate_live_data_size"); + + // sst files + write_stream_key_value(rocksdb::DB::Properties::kTotalSstFilesSize, "total_sst_files_size"); + write_stream_key_value(rocksdb::DB::Properties::kLiveSstFilesSize, "live_sst_files_size"); + + // pending compaction bytes + write_stream_key_value(rocksdb::DB::Properties::kEstimatePendingCompactionBytes, "estimate_pending_compaction_bytes"); + + // block cache + write_stream_key_value(rocksdb::DB::Properties::kBlockCacheCapacity, "block_cache_capacity"); + write_stream_key_value(rocksdb::DB::Properties::kBlockCacheUsage, "block_cache_usage"); + write_stream_key_value(rocksdb::DB::Properties::kBlockCachePinnedUsage, "block_cache_pinned_usage"); + + // blob files + write_stream_key_value(rocksdb::DB::Properties::kNumBlobFiles, "num_blob_files"); + write_stream_key_value(rocksdb::DB::Properties::kBlobStats, "blob_stats"); + write_stream_key_value(rocksdb::DB::Properties::kTotalBlobFileSize, "total_blob_file_size"); + write_stream_key_value(rocksdb::DB::Properties::kLiveBlobFileSize, "live_blob_file_size"); + + // column family stats + std::map mapvalues; + db_->rocksdb::DB::GetMapProperty(rocksdb::DB::Properties::kCFStats, &mapvalues); + mapToString(mapvalues, prefix); + info.append(string_stream.str()); } +void Redis::SetWriteWalOptions(const bool is_wal_disable) { default_write_options_.disableWAL = is_wal_disable; } + } // namespace storage diff --git a/src/storage/src/redis.h b/src/storage/src/redis.h index fde38a6e9..50191167e 100644 --- a/src/storage/src/redis.h +++ b/src/storage/src/redis.h @@ -54,7 +54,7 @@ class Redis { Status SetMaxCacheStatisticKeys(size_t max_cache_statistic_keys); Status SetSmallCompactionThreshold(size_t small_compaction_threshold); - void GetRocksDBInfo(std::string &info, const char *prefix); + void GetRocksDBInfo(std::string& info, const char* prefix); protected: Storage* const storage_; diff --git a/src/storage/src/redis_hashes.cc b/src/storage/src/redis_hashes.cc index 549970898..419466057 100644 --- a/src/storage/src/redis_hashes.cc +++ b/src/storage/src/redis_hashes.cc @@ -168,7 +168,7 @@ Status RedisHashes::PKPatternMatchDel(const std::string& pattern, int32_t* ret) if (static_cast(batch.Count()) >= BATCH_DELETE_LIMIT) { s = db_->Write(default_write_options_, &batch); if (s.ok()) { - total_delete += static_cast( batch.Count()); + total_delete += static_cast(batch.Count()); batch.Clear(); } else { *ret = total_delete; @@ -193,7 +193,7 @@ Status RedisHashes::HDel(const Slice& key, const std::vector& field uint32_t statistic = 0; std::vector filtered_fields; std::unordered_set field_set; - for (const auto & iter : fields) { + for (const auto& iter : fields) { const std::string& field = iter; if (field_set.find(field) == field_set.end()) { field_set.insert(field); @@ -234,7 +234,7 @@ Status RedisHashes::HDel(const Slice& key, const std::vector& field } } *ret = del_cnt; - if (!parsed_hashes_meta_value.CheckModifyCount(-del_cnt)){ + if (!parsed_hashes_meta_value.CheckModifyCount(-del_cnt)) { return Status::InvalidArgument("hash size overflow"); } parsed_hashes_meta_value.ModifyCount(-del_cnt); @@ -351,7 +351,7 @@ Status RedisHashes::HIncrby(const Slice& key, const Slice& field, int64_t value, statistic++; } else if (s.IsNotFound()) { Int64ToStr(value_buf, 32, value); - if (!parsed_hashes_meta_value.CheckModifyCount(1)){ + if (!parsed_hashes_meta_value.CheckModifyCount(1)) { return Status::InvalidArgument("hash size overflow"); } parsed_hashes_meta_value.ModifyCount(1); @@ -427,7 +427,7 @@ Status RedisHashes::HIncrbyfloat(const Slice& key, const Slice& field, const Sli statistic++; } else if (s.IsNotFound()) { LongDoubleToStr(long_double_by, new_value); - if (!parsed_hashes_meta_value.CheckModifyCount(1)){ + if (!parsed_hashes_meta_value.CheckModifyCount(1)) { return Status::InvalidArgument("hash size overflow"); } parsed_hashes_meta_value.ModifyCount(1); @@ -596,7 +596,7 @@ Status RedisHashes::HMSet(const Slice& key, const std::vector& fvs) return s; } } - if (!parsed_hashes_meta_value.CheckModifyCount(count)){ + if (!parsed_hashes_meta_value.CheckModifyCount(count)) { return Status::InvalidArgument("hash size overflow"); } parsed_hashes_meta_value.ModifyCount(count); @@ -649,7 +649,7 @@ Status RedisHashes::HSet(const Slice& key, const Slice& field, const Slice& valu statistic++; } } else if (s.IsNotFound()) { - if (!parsed_hashes_meta_value.CheckModifyCount(1)){ + if (!parsed_hashes_meta_value.CheckModifyCount(1)) { return Status::InvalidArgument("hash size overflow"); } parsed_hashes_meta_value.ModifyCount(1); @@ -701,7 +701,7 @@ Status RedisHashes::HSetnx(const Slice& key, const Slice& field, const Slice& va if (s.ok()) { *ret = 0; } else if (s.IsNotFound()) { - if (!parsed_hashes_meta_value.CheckModifyCount(1)){ + if (!parsed_hashes_meta_value.CheckModifyCount(1)) { return Status::InvalidArgument("hash size overflow"); } parsed_hashes_meta_value.ModifyCount(1); diff --git a/src/storage/src/redis_lists.cc b/src/storage/src/redis_lists.cc index 06e182a85..0289666df 100644 --- a/src/storage/src/redis_lists.cc +++ b/src/storage/src/redis_lists.cc @@ -362,14 +362,14 @@ Status RedisLists::LPop(const Slice& key, int64_t count, std::vector(parsed_lists_meta_value.count()); int32_t version = parsed_lists_meta_value.version(); int32_t start_index = 0; - auto stop_index = static_cast(count<=size?count-1:size-1); + auto stop_index = static_cast(count <= size ? count - 1 : size - 1); int32_t cur_index = 0; - ListsDataKey lists_data_key(key, version, parsed_lists_meta_value.left_index()+1); + ListsDataKey lists_data_key(key, version, parsed_lists_meta_value.left_index() + 1); rocksdb::Iterator* iter = db_->NewIterator(default_read_options_, handles_[1]); for (iter->Seek(lists_data_key.Encode()); iter->Valid() && cur_index <= stop_index; iter->Next(), ++cur_index) { statistic++; elements->push_back(iter->value().ToString()); - batch.Delete(handles_[1],iter->key()); + batch.Delete(handles_[1], iter->key()); parsed_lists_meta_value.ModifyCount(-1); parsed_lists_meta_value.ModifyLeftIndex(-1); @@ -734,14 +734,15 @@ Status RedisLists::RPop(const Slice& key, int64_t count, std::vector(parsed_lists_meta_value.count()); int32_t version = parsed_lists_meta_value.version(); int32_t start_index = 0; - auto stop_index = static_cast(count<=size?count-1:size-1); + auto stop_index = static_cast(count <= size ? count - 1 : size - 1); int32_t cur_index = 0; - ListsDataKey lists_data_key(key, version, parsed_lists_meta_value.right_index()-1); + ListsDataKey lists_data_key(key, version, parsed_lists_meta_value.right_index() - 1); rocksdb::Iterator* iter = db_->NewIterator(default_read_options_, handles_[1]); - for (iter->SeekForPrev(lists_data_key.Encode()); iter->Valid() && cur_index <= stop_index; iter->Prev(), ++cur_index) { + for (iter->SeekForPrev(lists_data_key.Encode()); iter->Valid() && cur_index <= stop_index; + iter->Prev(), ++cur_index) { statistic++; elements->push_back(iter->value().ToString()); - batch.Delete(handles_[1],iter->key()); + batch.Delete(handles_[1], iter->key()); parsed_lists_meta_value.ModifyCount(-1); parsed_lists_meta_value.ModifyRightIndex(-1); diff --git a/src/storage/src/redis_sets.cc b/src/storage/src/redis_sets.cc index 308e6278f..b4fdacc2c 100644 --- a/src/storage/src/redis_sets.cc +++ b/src/storage/src/redis_sets.cc @@ -10,8 +10,8 @@ #include #include -#include #include +#include #include "src/base_filter.h" #include "src/scope_record_lock.h" @@ -73,7 +73,8 @@ rocksdb::Status RedisSets::Open(const StorageOptions& storage_options, const std return rocksdb::DB::Open(db_ops, db_path, column_families, &handles_, &db_); } -rocksdb::Status RedisSets::CompactRange(const rocksdb::Slice* begin, const rocksdb::Slice* end, const ColumnFamilyType& type) { +rocksdb::Status RedisSets::CompactRange(const rocksdb::Slice* begin, const rocksdb::Slice* end, + const ColumnFamilyType& type) { if (type == kMeta || type == kMetaAndData) { db_->CompactRange(default_compact_range_options_, handles_[0], begin, end); } @@ -246,7 +247,7 @@ rocksdb::Status RedisSets::SAdd(const Slice& key, const std::vector if (cnt == 0) { return rocksdb::Status::OK(); } else { - if (!parsed_sets_meta_value.CheckModifyCount(cnt)){ + if (!parsed_sets_meta_value.CheckModifyCount(cnt)) { return Status::InvalidArgument("set size overflow"); } parsed_sets_meta_value.ModifyCount(cnt); @@ -354,7 +355,8 @@ rocksdb::Status RedisSets::SDiff(const std::vector& keys, std::vect return rocksdb::Status::OK(); } -rocksdb::Status RedisSets::SDiffstore(const Slice& destination, const std::vector& keys, std::vector& value_to_dest, int32_t* ret) { +rocksdb::Status RedisSets::SDiffstore(const Slice& destination, const std::vector& keys, + std::vector& value_to_dest, int32_t* ret) { if (keys.empty()) { return rocksdb::Status::Corruption("SDiffsotre invalid parameter, no keys"); } @@ -426,9 +428,9 @@ rocksdb::Status RedisSets::SDiffstore(const Slice& destination, const std::vecto ParsedSetsMetaValue parsed_sets_meta_value(&meta_value); statistic = parsed_sets_meta_value.count(); version = parsed_sets_meta_value.InitialMetaValue(); - if (!parsed_sets_meta_value.check_set_count(static_cast(members.size()))) { - return Status::InvalidArgument("set size overflow"); - } + if (!parsed_sets_meta_value.check_set_count(static_cast(members.size()))) { + return Status::InvalidArgument("set size overflow"); + } parsed_sets_meta_value.set_count(static_cast(members.size())); batch.Put(handles_[0], destination, meta_value); } else if (s.IsNotFound()) { @@ -526,7 +528,8 @@ rocksdb::Status RedisSets::SInter(const std::vector& keys, std::vec return rocksdb::Status::OK(); } -rocksdb::Status RedisSets::SInterstore(const Slice& destination, const std::vector& keys, std::vector& value_to_dest, int32_t* ret) { +rocksdb::Status RedisSets::SInterstore(const Slice& destination, const std::vector& keys, + std::vector& value_to_dest, int32_t* ret) { if (keys.empty()) { return rocksdb::Status::Corruption("SInterstore invalid parameter, no keys"); } @@ -726,7 +729,7 @@ rocksdb::Status RedisSets::SMove(const Slice& source, const Slice& destination, s = db_->Get(default_read_options_, handles_[1], sets_member_key.Encode(), &member_value); if (s.ok()) { *ret = 1; - if (!parsed_sets_meta_value.CheckModifyCount(-1)){ + if (!parsed_sets_meta_value.CheckModifyCount(-1)) { return Status::InvalidArgument("set size overflow"); } parsed_sets_meta_value.ModifyCount(-1); @@ -762,7 +765,7 @@ rocksdb::Status RedisSets::SMove(const Slice& source, const Slice& destination, SetsMemberKey sets_member_key(destination, version, member); s = db_->Get(default_read_options_, handles_[1], sets_member_key.Encode(), &member_value); if (s.IsNotFound()) { - if (!parsed_sets_meta_value.CheckModifyCount(1)){ + if (!parsed_sets_meta_value.CheckModifyCount(1)) { return Status::InvalidArgument("set size overflow"); } parsed_sets_meta_value.ModifyCount(1); @@ -811,20 +814,16 @@ rocksdb::Status RedisSets::SPop(const Slice& key, std::vector* memb int32_t version = parsed_sets_meta_value.version(); SetsMemberKey sets_member_key(key, version, Slice()); auto iter = db_->NewIterator(default_read_options_, handles_[1]); - for (iter->Seek(sets_member_key.Encode()); - iter->Valid() && cur_index < size; - iter->Next(), cur_index++) { - - batch.Delete(handles_[1], iter->key()); - ParsedSetsMemberKey parsed_sets_member_key(iter->key()); - members->push_back(parsed_sets_member_key.member().ToString()); - + for (iter->Seek(sets_member_key.Encode()); iter->Valid() && cur_index < size; iter->Next(), cur_index++) { + batch.Delete(handles_[1], iter->key()); + ParsedSetsMemberKey parsed_sets_member_key(iter->key()); + members->push_back(parsed_sets_member_key.member().ToString()); } - //parsed_sets_meta_value.ModifyCount(-cnt); - //batch.Put(handles_[0], key, meta_value); + // parsed_sets_meta_value.ModifyCount(-cnt); + // batch.Put(handles_[0], key, meta_value); batch.Delete(handles_[0], key); - delete iter; + delete iter; } else { engine.seed(time(nullptr)); @@ -835,11 +834,9 @@ rocksdb::Status RedisSets::SPop(const Slice& key, std::vector* memb std::unordered_set sets_index; int32_t modnum = size; - for (int64_t cur_round = 0; - cur_round < cnt; - cur_round++) { + for (int64_t cur_round = 0; cur_round < cnt; cur_round++) { do { - target_index = static_cast( engine() % modnum); + target_index = static_cast(engine() % modnum); } while (sets_index.find(target_index) != sets_index.end()); sets_index.insert(target_index); } @@ -847,9 +844,7 @@ rocksdb::Status RedisSets::SPop(const Slice& key, std::vector* memb SetsMemberKey sets_member_key(key, version, Slice()); int64_t del_count = 0; auto iter = db_->NewIterator(default_read_options_, handles_[1]); - for (iter->Seek(sets_member_key.Encode()); - iter->Valid() && cur_index < size; - iter->Next(), cur_index++) { + for (iter->Seek(sets_member_key.Encode()); iter->Valid() && cur_index < size; iter->Next(), cur_index++) { if (del_count == cnt) { break; } @@ -861,15 +856,13 @@ rocksdb::Status RedisSets::SPop(const Slice& key, std::vector* memb } } - if (!parsed_sets_meta_value.CheckModifyCount(static_cast(-cnt))){ + if (!parsed_sets_meta_value.CheckModifyCount(static_cast(-cnt))) { return Status::InvalidArgument("set size overflow"); } parsed_sets_meta_value.ModifyCount(static_cast(-cnt)); batch.Put(handles_[0], key, meta_value); delete iter; - } - } } else { return s; @@ -877,8 +870,7 @@ rocksdb::Status RedisSets::SPop(const Slice& key, std::vector* memb uint64_t count = 0; uint64_t duration = pstd::NowMicros() - start_us; AddAndGetSpopCount(key.ToString(), &count); - if (duration >= SPOP_COMPACT_THRESHOLD_DURATION - || count >= SPOP_COMPACT_THRESHOLD_COUNT) { + if (duration >= SPOP_COMPACT_THRESHOLD_DURATION || count >= SPOP_COMPACT_THRESHOLD_COUNT) { *need_compact = true; ResetSpopCount(key.ToString()); } @@ -995,7 +987,7 @@ rocksdb::Status RedisSets::SRem(const Slice& key, const std::vector } } *ret = cnt; - if (!parsed_sets_meta_value.CheckModifyCount(-cnt)){ + if (!parsed_sets_meta_value.CheckModifyCount(-cnt)) { return Status::InvalidArgument("set size overflow"); } parsed_sets_meta_value.ModifyCount(-cnt); @@ -1026,7 +1018,7 @@ rocksdb::Status RedisSets::SUnion(const std::vector& keys, std::vec std::vector vaild_sets; rocksdb::Status s; - for (const auto & key : keys) { + for (const auto& key : keys) { s = db_->Get(read_options, handles_[0], key, &meta_value); if (s.ok()) { ParsedSetsMetaValue parsed_sets_meta_value(&meta_value); @@ -1057,7 +1049,8 @@ rocksdb::Status RedisSets::SUnion(const std::vector& keys, std::vec return rocksdb::Status::OK(); } -rocksdb::Status RedisSets::SUnionstore(const Slice& destination, const std::vector& keys, std::vector& value_to_dest, int32_t* ret) { +rocksdb::Status RedisSets::SUnionstore(const Slice& destination, const std::vector& keys, + std::vector& value_to_dest, int32_t* ret) { if (keys.empty()) { return rocksdb::Status::Corruption("SUnionstore invalid parameter, no keys"); } @@ -1074,7 +1067,7 @@ rocksdb::Status RedisSets::SUnionstore(const Slice& destination, const std::vect std::vector vaild_sets; rocksdb::Status s; - for (const auto & key : keys) { + for (const auto& key : keys) { s = db_->Get(read_options, handles_[0], key, &meta_value); if (s.ok()) { ParsedSetsMetaValue parsed_sets_meta_value(&meta_value); @@ -1136,7 +1129,7 @@ rocksdb::Status RedisSets::SUnionstore(const Slice& destination, const std::vect } rocksdb::Status RedisSets::SScan(const Slice& key, int64_t cursor, const std::string& pattern, int64_t count, - std::vector* members, int64_t* next_cursor) { + std::vector* members, int64_t* next_cursor) { *next_cursor = 0; members->clear(); if (cursor < 0) { @@ -1204,8 +1197,8 @@ rocksdb::Status RedisSets::SScan(const Slice& key, int64_t cursor, const std::st return rocksdb::Status::OK(); } -rocksdb::Status RedisSets::PKScanRange(const Slice& key_start, const Slice& key_end, const Slice& pattern, int32_t limit, - std::vector* keys, std::string* next_key) { +rocksdb::Status RedisSets::PKScanRange(const Slice& key_start, const Slice& key_end, const Slice& pattern, + int32_t limit, std::vector* keys, std::string* next_key) { next_key->clear(); std::string key; @@ -1257,8 +1250,8 @@ rocksdb::Status RedisSets::PKScanRange(const Slice& key_start, const Slice& key_ return rocksdb::Status::OK(); } -rocksdb::Status RedisSets::PKRScanRange(const Slice& key_start, const Slice& key_end, const Slice& pattern, int32_t limit, - std::vector* keys, std::string* next_key) { +rocksdb::Status RedisSets::PKRScanRange(const Slice& key_start, const Slice& key_end, const Slice& pattern, + int32_t limit, std::vector* keys, std::string* next_key) { next_key->clear(); std::string key; @@ -1519,8 +1512,8 @@ void RedisSets::ScanDatabase() { } LOG(INFO) << fmt::format("[key : {:<30}] [count : {:<10}] [timestamp : {:<10}] [version : {}] [survival_time : {}]", - meta_iter->key().ToString(), parsed_sets_meta_value.count(), parsed_sets_meta_value.timestamp(), - parsed_sets_meta_value.version(), survival_time); + meta_iter->key().ToString(), parsed_sets_meta_value.count(), + parsed_sets_meta_value.timestamp(), parsed_sets_meta_value.version(), survival_time); } delete meta_iter; diff --git a/src/storage/src/redis_sets.h b/src/storage/src/redis_sets.h index 4245abbac..64ae3e783 100644 --- a/src/storage/src/redis_sets.h +++ b/src/storage/src/redis_sets.h @@ -38,9 +38,11 @@ class RedisSets : public Redis { Status SAdd(const Slice& key, const std::vector& members, int32_t* ret); Status SCard(const Slice& key, int32_t* ret); Status SDiff(const std::vector& keys, std::vector* members); - Status SDiffstore(const Slice& destination, const std::vector& keys, std::vector& value_to_dest, int32_t* ret); + Status SDiffstore(const Slice& destination, const std::vector& keys, + std::vector& value_to_dest, int32_t* ret); Status SInter(const std::vector& keys, std::vector* members); - Status SInterstore(const Slice& destination, const std::vector& keys, std::vector& value_to_dest, int32_t* ret); + Status SInterstore(const Slice& destination, const std::vector& keys, + std::vector& value_to_dest, int32_t* ret); Status SIsmember(const Slice& key, const Slice& member, int32_t* ret); Status SMembers(const Slice& key, std::vector* members); Status SMove(const Slice& source, const Slice& destination, const Slice& member, int32_t* ret); @@ -48,7 +50,8 @@ class RedisSets : public Redis { Status SRandmember(const Slice& key, int32_t count, std::vector* members); Status SRem(const Slice& key, const std::vector& members, int32_t* ret); Status SUnion(const std::vector& keys, std::vector* members); - Status SUnionstore(const Slice& destination, const std::vector& keys, std::vector& value_to_dest, int32_t* ret); + Status SUnionstore(const Slice& destination, const std::vector& keys, + std::vector& value_to_dest, int32_t* ret); Status SScan(const Slice& key, int64_t cursor, const std::string& pattern, int64_t count, std::vector* members, int64_t* next_cursor); Status PKScanRange(const Slice& key_start, const Slice& key_end, const Slice& pattern, int32_t limit, diff --git a/src/storage/src/redis_strings.cc b/src/storage/src/redis_strings.cc index 2fa750158..9b4430faa 100644 --- a/src/storage/src/redis_strings.cc +++ b/src/storage/src/redis_strings.cc @@ -131,7 +131,8 @@ Status RedisStrings::PKPatternMatchDel(const std::string& pattern, int32_t* ret) key = iter->key().ToString(); value = iter->value().ToString(); ParsedStringsValue parsed_strings_value(&value); - if (!parsed_strings_value.IsStale() && (StringMatch(pattern.data(), pattern.size(), key.data(), key.size(), 0) != 0)) { + if (!parsed_strings_value.IsStale() && + (StringMatch(pattern.data(), pattern.size(), key.data(), key.size(), 0) != 0)) { batch.Delete(key); } // In order to be more efficient, we use batch deletion here @@ -150,7 +151,7 @@ Status RedisStrings::PKPatternMatchDel(const std::string& pattern, int32_t* ret) if (batch.Count() != 0U) { s = db_->Write(default_write_options_, &batch); if (s.ok()) { - total_delete += static_cast( batch.Count()); + total_delete += static_cast(batch.Count()); batch.Clear(); } } @@ -290,7 +291,7 @@ std::string BitOpOperate(BitOpType op, const std::vector& src_value } Status RedisStrings::BitOp(BitOpType op, const std::string& dest_key, const std::vector& src_keys, - std::string &value_to_dest, int64_t* ret) { + std::string& value_to_dest, int64_t* ret) { Status s; if (op == kBitOpNot && src_keys.size() != 1) { return Status::InvalidArgument("the number of source keys is not right"); @@ -301,7 +302,7 @@ Status RedisStrings::BitOp(BitOpType op, const std::string& dest_key, const std: int64_t max_len = 0; int64_t value_len = 0; std::vector src_values; - for (const auto & src_key : src_keys) { + for (const auto& src_key : src_keys) { std::string value; s = db_->Get(default_read_options_, src_key, &value); if (s.ok()) { @@ -578,7 +579,7 @@ Status RedisStrings::MGet(const std::vector& keys, std::vector& kvs) { std::vector keys; keys.reserve(kvs.size()); -for (const auto& kv : kvs) { + for (const auto& kv : kvs) { keys.push_back(kv.key); } @@ -596,7 +597,7 @@ Status RedisStrings::MSetnx(const std::vector& kvs, int32_t* ret) { bool exists = false; *ret = 0; std::string value; - for (const auto & kv : kvs) { + for (const auto& kv : kvs) { s = db_->Get(default_read_options_, kv.key, &value); if (s.ok()) { ParsedStringsValue parsed_strings_value(&value); @@ -1350,10 +1351,9 @@ void RedisStrings::ScanDatabase() { survival_time = parsed_strings_value.timestamp() - current_time > 0 ? parsed_strings_value.timestamp() - current_time : -1; } - LOG(INFO) << fmt::format("[key : {:<30}] [value : {:<30}] [timestamp : {:<10}] [version : {}] [survival_time : {}]", iter->key().ToString(), - parsed_strings_value.value().ToString(), parsed_strings_value.timestamp(), parsed_strings_value.version(), - survival_time); - + LOG(INFO) << fmt::format("[key : {:<30}] [value : {:<30}] [timestamp : {:<10}] [version : {}] [survival_time : {}]", + iter->key().ToString(), parsed_strings_value.value().ToString(), + parsed_strings_value.timestamp(), parsed_strings_value.version(), survival_time); } delete iter; } diff --git a/src/storage/src/redis_strings.h b/src/storage/src/redis_strings.h index 1680673e6..3f46d1029 100644 --- a/src/storage/src/redis_strings.h +++ b/src/storage/src/redis_strings.h @@ -30,7 +30,8 @@ class RedisStrings : public Redis { // Strings Commands Status Append(const Slice& key, const Slice& value, int32_t* ret); Status BitCount(const Slice& key, int64_t start_offset, int64_t end_offset, int32_t* ret, bool have_range); - Status BitOp(BitOpType op, const std::string& dest_key, const std::vector& src_keys, std::string &value_to_dest, int64_t* ret); + Status BitOp(BitOpType op, const std::string& dest_key, const std::vector& src_keys, + std::string& value_to_dest, int64_t* ret); Status Decrby(const Slice& key, int64_t value, int64_t* ret); Status Get(const Slice& key, std::string* value); Status GetBit(const Slice& key, int64_t offset, int32_t* ret); diff --git a/src/storage/src/redis_zsets.cc b/src/storage/src/redis_zsets.cc index 67d38ebd2..ab1d5c59f 100644 --- a/src/storage/src/redis_zsets.cc +++ b/src/storage/src/redis_zsets.cc @@ -10,8 +10,8 @@ #include #include -#include #include +#include #include "iostream" #include "src/scope_record_lock.h" @@ -35,8 +35,8 @@ Status RedisZSets::Open(const StorageOptions& storage_options, const std::string rocksdb::Options ops(storage_options.options); Status s = rocksdb::DB::Open(ops, db_path, &db_); if (s.ok()) { - rocksdb::ColumnFamilyHandle *dcf = nullptr; - rocksdb::ColumnFamilyHandle *scf = nullptr; + rocksdb::ColumnFamilyHandle* dcf = nullptr; + rocksdb::ColumnFamilyHandle* scf = nullptr; s = db_->CreateColumnFamily(rocksdb::ColumnFamilyOptions(), "data_cf", &dcf); if (!s.ok()) { return s; @@ -242,7 +242,7 @@ Status RedisZSets::ZPopMax(const Slice& key, const int64_t count, std::vectorkey()); } delete iter; - if (!parsed_zsets_meta_value.CheckModifyCount(-del_cnt)){ + if (!parsed_zsets_meta_value.CheckModifyCount(-del_cnt)) { return Status::InvalidArgument("zset size overflow"); } parsed_zsets_meta_value.ModifyCount(-del_cnt); @@ -287,7 +287,7 @@ Status RedisZSets::ZPopMin(const Slice& key, const int64_t count, std::vectorkey()); } delete iter; - if (!parsed_zsets_meta_value.CheckModifyCount(-del_cnt)){ + if (!parsed_zsets_meta_value.CheckModifyCount(-del_cnt)) { return Status::InvalidArgument("zset size overflow"); } parsed_zsets_meta_value.ModifyCount(-del_cnt); @@ -366,7 +366,7 @@ Status RedisZSets::ZAdd(const Slice& key, const std::vector& score_ cnt++; } } - if (!parsed_zsets_meta_value.CheckModifyCount(cnt)){ + if (!parsed_zsets_meta_value.CheckModifyCount(cnt)) { return Status::InvalidArgument("zset size overflow"); } parsed_zsets_meta_value.ModifyCount(cnt); @@ -503,7 +503,7 @@ Status RedisZSets::ZIncrby(const Slice& key, const Slice& member, double increme statistic++; } else if (s.IsNotFound()) { score = increment; - if (!parsed_zsets_meta_value.CheckModifyCount(1)){ + if (!parsed_zsets_meta_value.CheckModifyCount(1)) { return Status::InvalidArgument("zset size overflow"); } parsed_zsets_meta_value.ModifyCount(1); @@ -729,7 +729,7 @@ Status RedisZSets::ZRem(const Slice& key, const std::vector& member } } *ret = del_cnt; - if (!parsed_zsets_meta_value.CheckModifyCount(-del_cnt)){ + if (!parsed_zsets_meta_value.CheckModifyCount(-del_cnt)) { return Status::InvalidArgument("zset size overflow"); } parsed_zsets_meta_value.ModifyCount(-del_cnt); @@ -783,7 +783,7 @@ Status RedisZSets::ZRemrangebyrank(const Slice& key, int32_t start, int32_t stop } delete iter; *ret = del_cnt; - if (!parsed_zsets_meta_value.CheckModifyCount(-del_cnt)){ + if (!parsed_zsets_meta_value.CheckModifyCount(-del_cnt)) { return Status::InvalidArgument("zset size overflow"); } parsed_zsets_meta_value.ModifyCount(-del_cnt); @@ -850,7 +850,7 @@ Status RedisZSets::ZRemrangebyscore(const Slice& key, double min, double max, bo } delete iter; *ret = del_cnt; - if (!parsed_zsets_meta_value.CheckModifyCount(-del_cnt)){ + if (!parsed_zsets_meta_value.CheckModifyCount(-del_cnt)) { return Status::InvalidArgument("zset size overflow"); } parsed_zsets_meta_value.ModifyCount(-del_cnt); @@ -1050,7 +1050,8 @@ Status RedisZSets::ZScore(const Slice& key, const Slice& member, double* score) } Status RedisZSets::ZUnionstore(const Slice& destination, const std::vector& keys, - const std::vector& weights, const AGGREGATE agg, std::map& value_to_dest, int32_t* ret) { + const std::vector& weights, const AGGREGATE agg, + std::map& value_to_dest, int32_t* ret) { *ret = 0; uint32_t statistic = 0; rocksdb::WriteBatch batch; @@ -1146,7 +1147,8 @@ Status RedisZSets::ZUnionstore(const Slice& destination, const std::vector& keys, - const std::vector& weights, const AGGREGATE agg, std::vector& value_to_dest, int32_t* ret) { + const std::vector& weights, const AGGREGATE agg, + std::vector& value_to_dest, int32_t* ret) { if (keys.empty()) { return Status::Corruption("ZInterstore invalid parameter, no keys"); } @@ -1387,7 +1389,7 @@ Status RedisZSets::ZRemrangebylex(const Slice& key, const Slice& min, const Slic delete iter; } if (del_cnt > 0) { - if (!parsed_zsets_meta_value.CheckModifyCount(-del_cnt)){ + if (!parsed_zsets_meta_value.CheckModifyCount(-del_cnt)) { return Status::InvalidArgument("zset size overflow"); } parsed_zsets_meta_value.ModifyCount(-del_cnt); @@ -1788,8 +1790,8 @@ void RedisZSets::ScanDatabase() { } LOG(INFO) << fmt::format("[key : {:<30}] [count : {:<10}] [timestamp : {:<10}] [version : {}] [survival_time : {}]", - meta_iter->key().ToString(), parsed_zsets_meta_value.count(), parsed_zsets_meta_value.timestamp(), - parsed_zsets_meta_value.version(), survival_time); + meta_iter->key().ToString(), parsed_zsets_meta_value.count(), + parsed_zsets_meta_value.timestamp(), parsed_zsets_meta_value.version(), survival_time); } delete meta_iter; @@ -1812,10 +1814,10 @@ void RedisZSets::ScanDatabase() { auto score_iter = db_->NewIterator(iterator_options, handles_[2]); for (score_iter->SeekToFirst(); score_iter->Valid(); score_iter->Next()) { ParsedZSetsScoreKey parsed_zsets_score_key(score_iter->key()); - + LOG(INFO) << fmt::format("[key : {:<30}] [score : {:<20}] [member : {:<20}] [version : {}]", parsed_zsets_score_key.key().ToString(), parsed_zsets_score_key.score(), - parsed_zsets_score_key.member().ToString(), parsed_zsets_score_key.version()); + parsed_zsets_score_key.member().ToString(), parsed_zsets_score_key.version()); } delete score_iter; } diff --git a/src/storage/src/storage.cc b/src/storage/src/storage.cc index 29e7e1336..cd4f31d5f 100644 --- a/src/storage/src/storage.cc +++ b/src/storage/src/storage.cc @@ -187,7 +187,7 @@ Status Storage::BitCount(const Slice& key, int64_t start_offset, int64_t end_off } Status Storage::BitOp(BitOpType op, const std::string& dest_key, const std::vector& src_keys, - std::string &value_to_dest, int64_t* ret) { + std::string& value_to_dest, int64_t* ret) { return strings_db_->BitOp(op, dest_key, src_keys, value_to_dest, ret); } @@ -295,7 +295,8 @@ Status Storage::SDiff(const std::vector& keys, std::vectorSDiff(keys, members); } -Status Storage::SDiffstore(const Slice& destination, const std::vector& keys, std::vector& value_to_dest, int32_t* ret) { +Status Storage::SDiffstore(const Slice& destination, const std::vector& keys, + std::vector& value_to_dest, int32_t* ret) { return sets_db_->SDiffstore(destination, keys, value_to_dest, ret); } @@ -303,7 +304,8 @@ Status Storage::SInter(const std::vector& keys, std::vectorSInter(keys, members); } -Status Storage::SInterstore(const Slice& destination, const std::vector& keys, std::vector& value_to_dest, int32_t* ret) { +Status Storage::SInterstore(const Slice& destination, const std::vector& keys, + std::vector& value_to_dest, int32_t* ret) { return sets_db_->SInterstore(destination, keys, value_to_dest, ret); } @@ -340,7 +342,8 @@ Status Storage::SUnion(const std::vector& keys, std::vectorSUnion(keys, members); } -Status Storage::SUnionstore(const Slice& destination, const std::vector& keys, std::vector& value_to_dest, int32_t* ret) { +Status Storage::SUnionstore(const Slice& destination, const std::vector& keys, + std::vector& value_to_dest, int32_t* ret) { return sets_db_->SUnionstore(destination, keys, value_to_dest, ret); } @@ -365,9 +368,13 @@ Status Storage::LTrim(const Slice& key, int64_t start, int64_t stop) { return li Status Storage::LLen(const Slice& key, uint64_t* len) { return lists_db_->LLen(key, len); } -Status Storage::LPop(const Slice& key, int64_t count, std::vector* elements) { return lists_db_->LPop(key, count, elements); } +Status Storage::LPop(const Slice& key, int64_t count, std::vector* elements) { + return lists_db_->LPop(key, count, elements); +} -Status Storage::RPop(const Slice& key, int64_t count, std::vector* elements) { return lists_db_->RPop(key, count, elements); } +Status Storage::RPop(const Slice& key, int64_t count, std::vector* elements) { + return lists_db_->RPop(key, count, elements); +} Status Storage::LIndex(const Slice& key, int64_t index, std::string* element) { return lists_db_->LIndex(key, index, element); @@ -476,12 +483,14 @@ Status Storage::ZScore(const Slice& key, const Slice& member, double* ret) { } Status Storage::ZUnionstore(const Slice& destination, const std::vector& keys, - const std::vector& weights, const AGGREGATE agg, std::map& value_to_dest, int32_t* ret) { + const std::vector& weights, const AGGREGATE agg, + std::map& value_to_dest, int32_t* ret) { return zsets_db_->ZUnionstore(destination, keys, weights, agg, value_to_dest, ret); } Status Storage::ZInterstore(const Slice& destination, const std::vector& keys, - const std::vector& weights, const AGGREGATE agg, std::vector& value_to_dest, int32_t* ret) { + const std::vector& weights, const AGGREGATE agg, + std::vector& value_to_dest, int32_t* ret) { return zsets_db_->ZInterstore(destination, keys, weights, agg, value_to_dest, ret); } diff --git a/src/storage/src/strings_value_format.h b/src/storage/src/strings_value_format.h index d2139b3fa..60282f9d0 100644 --- a/src/storage/src/strings_value_format.h +++ b/src/storage/src/strings_value_format.h @@ -37,7 +37,8 @@ class ParsedStringsValue : public ParsedInternalValue { // Use this constructor in rocksdb::CompactionFilter::Filter(); explicit ParsedStringsValue(const rocksdb::Slice& internal_value_slice) : ParsedInternalValue(internal_value_slice) { if (internal_value_slice.size() >= kStringsValueSuffixLength) { - user_value_ = rocksdb::Slice(internal_value_slice.data(), internal_value_slice.size() - kStringsValueSuffixLength); + user_value_ = + rocksdb::Slice(internal_value_slice.data(), internal_value_slice.size() - kStringsValueSuffixLength); timestamp_ = DecodeFixed32(internal_value_slice.data() + internal_value_slice.size() - kStringsValueSuffixLength); } } diff --git a/src/storage/src/util.cc b/src/storage/src/util.cc index 9effa0768..94846f368 100644 --- a/src/storage/src/util.cc +++ b/src/storage/src/util.cc @@ -28,20 +28,17 @@ namespace storage { * * Modified in order to handle signed integers since the original code was * designed for unsigned integers. */ -int Int64ToStr(char* dst, size_t dstlen, int64_t svalue) { - return pstd::Ll2string(dst, dstlen, svalue); -} +int Int64ToStr(char* dst, size_t dstlen, int64_t svalue) { return pstd::Ll2string(dst, dstlen, svalue); } /* Convert a string into a long long. Returns 1 if the string could be parsed * into a (non-overflowing) long long, 0 otherwise. The value will be set to * the parsed value when appropriate. */ -int StrToInt64(const char* s, size_t slen, int64_t* value) { - return pstd::String2int(s, slen, value); -} +int StrToInt64(const char* s, size_t slen, int64_t* value) { return pstd::String2int(s, slen, value); } /* Glob-style pattern matching. */ int StringMatch(const char* pattern, uint64_t pattern_len, const char* str, uint64_t string_len, int nocase) { - return pstd::StringMatchLen(pattern, static_cast(pattern_len), str, static_cast(string_len), nocase); + return pstd::StringMatchLen(pattern, static_cast(pattern_len), str, static_cast(string_len), + nocase); } int StrToLongDouble(const char* s, size_t slen, long double* ldval) { diff --git a/src/storage/src/zsets_data_key_format.h b/src/storage/src/zsets_data_key_format.h index 525f54020..c376badec 100644 --- a/src/storage/src/zsets_data_key_format.h +++ b/src/storage/src/zsets_data_key_format.h @@ -14,7 +14,7 @@ namespace storage { class ZSetsScoreKey { public: ZSetsScoreKey(const Slice& key, int32_t version, double score, const Slice& member) - : key_(key), version_(version), score_(score), member_(member) {} + : key_(key), version_(version), score_(score), member_(member) {} ~ZSetsScoreKey() { if (start_ != space_) { From 5a88cf36056ddc53bdac903b8e8c80f1f2c4ea6b Mon Sep 17 00:00:00 2001 From: Tao_ta <745103130@qq.com> Date: Tue, 12 Dec 2023 08:44:15 +0800 Subject: [PATCH 3/4] solving format --- src/storage/src/redis_hyperloglog.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/storage/src/redis_hyperloglog.cc b/src/storage/src/redis_hyperloglog.cc index 52dae4246..8ba1757a1 100644 --- a/src/storage/src/redis_hyperloglog.cc +++ b/src/storage/src/redis_hyperloglog.cc @@ -35,8 +35,9 @@ std::string HyperLogLog::Add(const char* value, uint32_t len) { MurmurHash3_x86_32(value, static_cast(len), HLL_HASH_SEED, static_cast(&hash_value)); uint32_t index = hash_value & ((1 << b_) - 1); uint8_t rank = Nctz((hash_value >> b_), static_cast(32 - b_)); - if (rank > register_[index]) { register_[index] = static_cast(rank); -} + if (rank > register_[index]) { + register_[index] = static_cast(rank); + } std::string result(m_, 0); for (uint32_t i = 0; i < m_; ++i) { result[i] = register_[i]; From d304a983617491ac4894e97916419d9eaa517ed1 Mon Sep 17 00:00:00 2001 From: Tao_ta <745103130@qq.com> Date: Mon, 18 Dec 2023 23:03:38 +0800 Subject: [PATCH 4/4] fix build error --- src/base_cmd.h | 4 ---- src/cmd_kv.cc | 12 ++++++------ src/cmd_table_manager.cc | 34 +--------------------------------- 3 files changed, 7 insertions(+), 43 deletions(-) diff --git a/src/base_cmd.h b/src/base_cmd.h index 742ca36d6..9d4dfb666 100644 --- a/src/base_cmd.h +++ b/src/base_cmd.h @@ -37,11 +37,7 @@ const std::string kCmdNameIncrby = "incrby"; const std::string kCmdNameDecrby = "decrby"; const std::string kCmdNameIncrbyFloat = "incrbyfloat"; const std::string kCmdNameStrlen = "strlen"; -const std::string kCmdNameSetex = "setex"; -const std::string kCmdNamePsetex = "psetex"; -const std::string kCmdNameSetnx = "setnx"; const std::string kCmdNameSetBit = "setbit"; -const std::string kCmdNameIncrbyfloat = "incrbyfloat"; const std::string kCmdNameSetEx = "setex"; const std::string kCmdNamePSetEx = "psetex"; const std::string kCmdNameBitOp = "bitop"; diff --git a/src/cmd_kv.cc b/src/cmd_kv.cc index 61031c649..eb6b1b31d 100644 --- a/src/cmd_kv.cc +++ b/src/cmd_kv.cc @@ -572,7 +572,7 @@ void GetBitCmd::DoCmd(PClient* client) { } SetBitCmd::SetBitCmd(const std::string& name, int16_t arity) - : BaseCmd(name, arity, CmdFlagsWrite, AclCategoryWrite | AclCategoryString) {} + : BaseCmd(name, arity, kCmdFlagsWrite, kAclCategoryWrite | kAclCategoryString) {} bool SetBitCmd::DoInitial(PClient* client) { client->SetKey(client->argv_[1]); @@ -581,13 +581,13 @@ bool SetBitCmd::DoInitial(PClient* client) { void SetBitCmd::DoCmd(PClient* client) { PObject* value = nullptr; - PError err = PSTORE.GetValueByType(client->Key(), value, PType_string); - if (err == PError_notExist) { + PError err = PSTORE.GetValueByType(client->Key(), value, kPTypeString); + if (err == kPErrorNotExist) { value = PSTORE.SetValue(client->Key(), PObject::CreateString("")); - err = PError_ok; + err = kPErrorOK; } - if (err != PError_ok) { + if (err != kPErrorOK) { client->AppendInteger(0); return; } @@ -629,7 +629,7 @@ void SetBitCmd::DoCmd(PClient* client) { } value->Reset(new PString(newVal)); - value->encoding = PEncode_raw; + value->encoding = kPEncodeRaw; client->AppendInteger((oldByte & (0x1 << bits)) ? 1 : 0); return; } diff --git a/src/cmd_table_manager.cc b/src/cmd_table_manager.cc index a1506b129..29a39d167 100644 --- a/src/cmd_table_manager.cc +++ b/src/cmd_table_manager.cc @@ -42,39 +42,6 @@ void CmdTableManager::InitCmdTable() { ADD_COMMAND(Exists, 2); // kv - std::unique_ptr getPtr = std::make_unique(kCmdNameGet, 2); - cmds_->insert(std::make_pair(kCmdNameGet, std::move(getPtr))); - std::unique_ptr setPtr = std::make_unique(kCmdNameSet, -3); - cmds_->insert(std::make_pair(kCmdNameSet, std::move(setPtr))); - - std::unique_ptr bitOpPtr = std::make_unique(kCmdNameBitOp, -4); - cmds_->insert(std::make_pair(kCmdNameBitOp, std::move(bitOpPtr))); - std::unique_ptr appendPtr = std::make_unique(kCmdNameAppend, 3); - cmds_->insert(std::make_pair(kCmdNameAppend, std::move(appendPtr))); - std::unique_ptr getsetPtr = std::make_unique(kCmdNameGetset, 3); - cmds_->insert(std::make_pair(kCmdNameGetset, std::move(getsetPtr))); - std::unique_ptr mgetPtr = std::make_unique(kCmdNameMget, -2); - cmds_->insert(std::make_pair(kCmdNameMget, std::move(mgetPtr))); - std::unique_ptr msetPtr = std::make_unique(kCmdNameMset, -3); - cmds_->insert(std::make_pair(kCmdNameMset, std::move(msetPtr))); - std::unique_ptr bitcountPtr = std::make_unique(kCmdNameBitCount, -2); - cmds_->insert(std::make_pair(kCmdNameBitCount, std::move(bitcountPtr))); - std::unique_ptr incrbyPtr = std::make_unique(kCmdNameIncrby, 3); - cmds_->insert(std::make_pair(kCmdNameIncrby, std::move(incrbyPtr))); - std::unique_ptr incrbyfloatPtr = std::make_unique(kCmdNameIncrbyfloat, 3); - cmds_->insert(std::make_pair(kCmdNameIncrbyfloat, std::move(incrbyfloatPtr))); - std::unique_ptr strlenPtr = std::make_unique(kCmdNameStrlen, 2); - cmds_->insert(std::make_pair(kCmdNameStrlen, std::move(strlenPtr))); - std::unique_ptr setexPtr = std::make_unique(kCmdNameSetex, 4); - cmds_->insert(std::make_pair(kCmdNameSetex, std::move(setexPtr))); - std::unique_ptr psetexPtr = std::make_unique(kCmdNamePsetex, 4); - cmds_->insert(std::make_pair(kCmdNamePsetex, std::move(psetexPtr))); - std::unique_ptr setnxPtr = std::make_unique(kCmdNameSetnx, 3); - cmds_->insert(std::make_pair(kCmdNameSetnx, std::move(setnxPtr))); - std::unique_ptr setbitPtr = std::make_unique(kCmdNameSetBit, 4); - cmds_->insert(std::make_pair(kCmdNameSetBit, std::move(setbitPtr))); - std::unique_ptr getbitPtr = std::make_unique(kCmdNameGetBit, 3); - cmds_->insert(std::make_pair(kCmdNameGetBit, std::move(getbitPtr))); ADD_COMMAND(Get, 2); ADD_COMMAND(Set, -3); ADD_COMMAND(MGet, -2); @@ -91,6 +58,7 @@ void CmdTableManager::InitCmdTable() { ADD_COMMAND(BitOp, -4); ADD_COMMAND(BitCount, -2); ADD_COMMAND(GetBit, 3); + ADD_COMMAND(SetBit, 4); // hash ADD_COMMAND(HSet, -4);