Skip to content

Commit

Permalink
polish code
Browse files Browse the repository at this point in the history
Co-authored-by: Twice <twice@apache.org>
  • Loading branch information
jjz921024 and PragmaTwice committed Jul 17, 2024
1 parent cb1ff54 commit ebdedf2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/storage/redis_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ rocksdb::Status Database::typeInternal(const Slice &key, RedisType *type) {
HashMetadata hash_metadata(false);
s = hash_metadata.Decode(value);
if (!s.ok()) return s;
if (!hash_metadata.IsTTLFieldEncoded()) {
if (!hash_metadata.IsFieldExpirationEnabled()) {
*type = metadata.Type();
} else {
redis::Hash hash_db(storage_, namespace_);
Expand Down
6 changes: 2 additions & 4 deletions src/storage/redis_metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,11 @@ bool Metadata::IsEmptyableType() const {

bool Metadata::Expired() const { return ExpireAt(util::GetTimeStampMS()); }

bool HashMetadata::IsTTLFieldEncoded() const { return field_encoding == HashFieldEncoding::WITH_TTL; }
bool HashMetadata::IsFieldExpirationEnabled() const { return field_encoding == HashSubkeyEncoding::WITH_TTL; }

void HashMetadata::Encode(std::string *dst) const {
Metadata::Encode(dst);
if (IsTTLFieldEncoded()) {
PutFixed8(dst, uint8_t(field_encoding));
}
PutFixed8(dst, uint8_t(field_encoding));
}

rocksdb::Status HashMetadata::Decode(Slice *input) {
Expand Down
6 changes: 3 additions & 3 deletions src/storage/redis_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,22 @@ class Metadata {
static uint64_t generateVersion();
};

enum class HashFieldEncoding : uint8_t {
enum class HashSubkeyEncoding : uint8_t {
RAW = 0,
WITH_TTL = 1,
};

class HashMetadata : public Metadata {
public:
HashFieldEncoding field_encoding = HashFieldEncoding::RAW;
HashSubkeyEncoding field_encoding = HashSubkeyEncoding::RAW;

explicit HashMetadata(bool generate_version = true) : Metadata(kRedisHash, generate_version) {}

void Encode(std::string *dst) const override;
using Metadata::Decode;
rocksdb::Status Decode(Slice *input) override;

bool IsTTLFieldEncoded() const;
bool IsFieldExpirationEnabled() const;
};

class SetMetadata : public Metadata {
Expand Down
16 changes: 8 additions & 8 deletions src/types/redis_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ rocksdb::Status Hash::Size(const Slice &user_key, uint64_t *size) {
HashMetadata metadata(false);
rocksdb::Status s = GetMetadata(Database::GetOptions{}, ns_key, &metadata);
if (!s.ok()) return s;
if (!metadata.IsTTLFieldEncoded()) {
if (!metadata.IsFieldExpirationEnabled()) {
*size = metadata.size;
} else {
std::vector<FieldValue> field_values;
Expand Down Expand Up @@ -113,7 +113,7 @@ rocksdb::Status Hash::IncrBy(const Slice &user_key, const Slice &field, int64_t
WriteBatchLogData log_data(kRedisHash);
batch->PutLogData(log_data.Encode());
auto value_str = std::to_string(*new_value);
if (metadata.IsTTLFieldEncoded()) {
if (metadata.IsFieldExpirationEnabled()) {
encodeFieldAndTTL(&value_str, expire);
}
batch->Put(sub_key, value_str);
Expand Down Expand Up @@ -164,7 +164,7 @@ rocksdb::Status Hash::IncrByFloat(const Slice &user_key, const Slice &field, dou
WriteBatchLogData log_data(kRedisHash);
batch->PutLogData(log_data.Encode());
auto value_str = std::to_string(*new_value);
if (metadata.IsTTLFieldEncoded()) {
if (metadata.IsFieldExpirationEnabled()) {
encodeFieldAndTTL(&value_str, expire);
}
batch->Put(sub_key, value_str);
Expand Down Expand Up @@ -308,7 +308,7 @@ rocksdb::Status Hash::MSet(const Slice &user_key, const std::vector<FieldValue>
}

auto value = it->value;
if (metadata.IsTTLFieldEncoded()) {
if (metadata.IsFieldExpirationEnabled()) {
encodeFieldAndTTL(&value, expire);
}
batch->Put(sub_key, value);
Expand Down Expand Up @@ -547,8 +547,8 @@ rocksdb::Status Hash::ExpireFields(const Slice &user_key, uint64_t expire_ms, co
}

// convert rest field encoding
if (!metadata.IsTTLFieldEncoded()) {
metadata.field_encoding = HashFieldEncoding::WITH_TTL;
if (!metadata.IsFieldExpirationEnabled()) {
metadata.field_encoding = HashSubkeyEncoding::WITH_TTL;

std::unordered_set<std::string_view> field_set;
for (auto field : fields) {
Expand Down Expand Up @@ -638,7 +638,7 @@ rocksdb::Status Hash::TTLFields(const Slice &user_key, const std::vector<Slice>
}

bool Hash::IsFieldExpired(Metadata &metadata, const Slice &value) {
if (!(static_cast<HashMetadata *>(&metadata))->IsTTLFieldEncoded()) {
if (!(static_cast<HashMetadata *>(&metadata))->IsFieldExpirationEnabled()) {
return false;
}
uint64_t expire = 0;
Expand All @@ -648,7 +648,7 @@ bool Hash::IsFieldExpired(Metadata &metadata, const Slice &value) {
}

rocksdb::Status Hash::decodeFieldAndTTL(const HashMetadata &metadata, std::string *value, uint64_t &expire) {
if (!metadata.IsTTLFieldEncoded()) {
if (!metadata.IsFieldExpirationEnabled()) {
return rocksdb::Status::OK();
}
rocksdb::Slice data(value->data(), value->size());
Expand Down

0 comments on commit ebdedf2

Please sign in to comment.