Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize ParseMetadata by introducing RedisTypes #1967

Merged
merged 2 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/storage/redis_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/storage/redis_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 26 additions & 1 deletion src/storage/redis_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <rocksdb/status.h>

#include <atomic>
#include <bitset>
#include <initializer_list>
#include <string>
#include <vector>

Expand All @@ -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,
Expand All @@ -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,
Expand Down