This is an automated email from the ASF dual-hosted git repository.

twice pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new b0b1b4e0 Optimize ParseMetadata by introducing RedisTypes (#1967)
b0b1b4e0 is described below

commit b0b1b4e06a109389a5ba1cf93c52eac5b84f8f5d
Author: Twice <[email protected]>
AuthorDate: Sun Dec 24 23:23:38 2023 +0900

    Optimize ParseMetadata by introducing RedisTypes (#1967)
---
 src/storage/redis_db.cc      |  5 ++---
 src/storage/redis_db.h       |  3 +--
 src/storage/redis_metadata.h | 27 ++++++++++++++++++++++++++-
 3 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/src/storage/redis_db.cc b/src/storage/redis_db.cc
index e58c1f6f..7708b491 100644
--- a/src/storage/redis_db.cc
+++ b/src/storage/redis_db.cc
@@ -40,7 +40,7 @@ Database::Database(engine::Storage *storage, std::string ns) 
: storage_(storage)
 
 // Some data types may support reading multiple types of metadata.
 // For example, bitmap supports reading string metadata and bitmap metadata.
-rocksdb::Status Database::ParseMetadata(const std::vector<RedisType> &types, 
Slice *bytes, Metadata *metadata) {
+rocksdb::Status Database::ParseMetadata(RedisTypes types, Slice *bytes, 
Metadata *metadata) {
   std::string old_metadata;
   metadata->Encode(&old_metadata);
 
@@ -53,9 +53,8 @@ rocksdb::Status Database::ParseMetadata(const 
std::vector<RedisType> &types, Sli
     return rocksdb::Status::NotFound(kErrMsgKeyExpired);
   }
 
-  bool is_type_matched = std::find(types.begin(), types.end(), 
metadata->Type()) != types.end();
   // if type is not matched, we still need to check if the metadata is valid.
-  if (!is_type_matched && (metadata->size > 0 || metadata->IsEmptyableType())) 
{
+  if (!types.Contains(metadata->Type()) && (metadata->size > 0 || 
metadata->IsEmptyableType())) {
     // error discarded here since it already failed
     auto _ [[maybe_unused]] = metadata->Decode(old_metadata);
     return rocksdb::Status::InvalidArgument(kErrMsgWrongType);
diff --git a/src/storage/redis_db.h b/src/storage/redis_db.h
index 252e5d30..181723a4 100644
--- a/src/storage/redis_db.h
+++ b/src/storage/redis_db.h
@@ -34,8 +34,7 @@ class Database {
   static constexpr uint64_t RANDOM_KEY_SCAN_LIMIT = 60;
 
   explicit Database(engine::Storage *storage, std::string ns = "");
-  [[nodiscard]] static rocksdb::Status ParseMetadata(const 
std::vector<RedisType> &types, Slice *bytes,
-                                                     Metadata *metadata);
+  [[nodiscard]] static rocksdb::Status ParseMetadata(RedisTypes types, Slice 
*bytes, Metadata *metadata);
   [[nodiscard]] rocksdb::Status GetMetadata(RedisType type, const Slice 
&ns_key, Metadata *metadata);
   [[nodiscard]] rocksdb::Status GetMetadata(RedisType type, const Slice 
&ns_key, std::string *raw_value,
                                             Metadata *metadata, Slice *rest);
diff --git a/src/storage/redis_metadata.h b/src/storage/redis_metadata.h
index cbdb7a41..8fe52f39 100644
--- a/src/storage/redis_metadata.h
+++ b/src/storage/redis_metadata.h
@@ -23,6 +23,8 @@
 #include <rocksdb/status.h>
 
 #include <atomic>
+#include <bitset>
+#include <initializer_list>
 #include <string>
 #include <vector>
 
@@ -35,7 +37,7 @@ constexpr bool USE_64BIT_COMMON_FIELD_DEFAULT = 
METADATA_ENCODING_VERSION != 0;
 // explicitly since it cannot be changed once confirmed
 // Note that if you want to add a new redis type in `RedisType`
 // you should also add a type name to the `RedisTypeNames` below
-enum RedisType {
+enum RedisType : uint8_t {
   kRedisNone = 0,
   kRedisString = 1,
   kRedisHash = 2,
@@ -49,6 +51,29 @@ enum RedisType {
   kRedisJson = 10,
 };
 
+struct RedisTypes {
+  RedisTypes(std::initializer_list<RedisType> list) {
+    for (auto type : list) {
+      types_.set(type);
+    }
+  }
+
+  static RedisTypes All() {
+    UnderlyingType types;
+    types.set();
+    return RedisTypes(types);
+  }
+
+  bool Contains(RedisType type) { return types_[type]; }
+
+ private:
+  using UnderlyingType = std::bitset<128>;
+
+  explicit RedisTypes(UnderlyingType types) : types_(types) {}
+
+  UnderlyingType types_;
+};
+
 enum RedisCommand {
   kRedisCmdLSet,
   kRedisCmdLInsert,

Reply via email to