git-hulk commented on code in PR #2402:
URL: https://github.com/apache/kvrocks/pull/2402#discussion_r1680977258
##########
src/types/redis_hash.cc:
##########
@@ -418,4 +472,208 @@ rocksdb::Status Hash::RandField(const Slice &user_key,
int64_t command_count, st
return rocksdb::Status::OK();
}
+rocksdb::Status Hash::ExpireFields(const Slice &user_key, uint64_t expire_ms,
const std::vector<Slice> &fields,
+ HashFieldExpireType type, bool is_persist,
std::vector<int8_t> *ret) {
+ std::string ns_key = AppendNamespacePrefix(user_key);
+ HashMetadata metadata(false);
+ LatestSnapShot ss(storage_);
+ rocksdb::Status s = GetMetadata(GetOptions{ss.GetSnapShot()}, ns_key,
&metadata);
+ if (!s.ok()) {
+ ret->resize(fields.size(), -2);
+ return rocksdb::Status::OK();
+ }
+
+ rocksdb::ReadOptions read_options = storage_->DefaultMultiGetOptions();
+ read_options.snapshot = ss.GetSnapShot();
+
+ std::vector<rocksdb::Slice> keys;
+ keys.reserve(fields.size());
+ std::vector<std::string> sub_keys;
+ sub_keys.resize(fields.size());
+ for (size_t i = 0; i < fields.size(); i++) {
+ auto &field = fields[i];
+ sub_keys[i] = InternalKey(ns_key, field, metadata.version,
storage_->IsSlotIdEncoded()).Encode();
+ keys.emplace_back(sub_keys[i]);
+ }
+
+ auto batch = storage_->GetWriteBatchBase();
+ WriteBatchLogData log_data(kRedisHash);
+ batch->PutLogData(log_data.Encode());
+
+ // expire special field
+ std::vector<rocksdb::PinnableSlice> values_vector;
+ values_vector.resize(sub_keys.size());
+ std::vector<rocksdb::Status> statuses_vector;
+ statuses_vector.resize(sub_keys.size());
+ storage_->MultiGet(read_options, storage_->GetDB()->DefaultColumnFamily(),
keys.size(), keys.data(),
+ values_vector.data(), statuses_vector.data());
+
+ auto now = util::GetTimeStampMS();
+ for (size_t i = 0; i < keys.size(); i++) {
+ if (!statuses_vector[i].ok() && !statuses_vector[i].IsNotFound()) return
statuses_vector[i];
+
+ // no such field exists
+ if (statuses_vector[i].IsNotFound()) {
+ ret->emplace_back(-2);
+ continue;
+ }
+
+ InternalKey sub_ikey(ns_key, fields[i], metadata.version,
storage_->IsSlotIdEncoded());
+
+ // expire with a pass time
+ if (expire_ms <= now && !is_persist) {
+ batch->Delete(sub_ikey.Encode());
+ ret->emplace_back(2);
Review Comment:
Should be `-2`?
##########
src/types/redis_hash.cc:
##########
@@ -418,4 +472,208 @@ rocksdb::Status Hash::RandField(const Slice &user_key,
int64_t command_count, st
return rocksdb::Status::OK();
}
+rocksdb::Status Hash::ExpireFields(const Slice &user_key, uint64_t expire_ms,
const std::vector<Slice> &fields,
+ HashFieldExpireType type, bool is_persist,
std::vector<int8_t> *ret) {
+ std::string ns_key = AppendNamespacePrefix(user_key);
+ HashMetadata metadata(false);
+ LatestSnapShot ss(storage_);
+ rocksdb::Status s = GetMetadata(GetOptions{ss.GetSnapShot()}, ns_key,
&metadata);
+ if (!s.ok()) {
+ ret->resize(fields.size(), -2);
+ return rocksdb::Status::OK();
+ }
+
+ rocksdb::ReadOptions read_options = storage_->DefaultMultiGetOptions();
+ read_options.snapshot = ss.GetSnapShot();
+
+ std::vector<rocksdb::Slice> keys;
+ keys.reserve(fields.size());
+ std::vector<std::string> sub_keys;
+ sub_keys.resize(fields.size());
+ for (size_t i = 0; i < fields.size(); i++) {
+ auto &field = fields[i];
+ sub_keys[i] = InternalKey(ns_key, field, metadata.version,
storage_->IsSlotIdEncoded()).Encode();
+ keys.emplace_back(sub_keys[i]);
+ }
+
+ auto batch = storage_->GetWriteBatchBase();
+ WriteBatchLogData log_data(kRedisHash);
+ batch->PutLogData(log_data.Encode());
+
+ // expire special field
+ std::vector<rocksdb::PinnableSlice> values_vector;
+ values_vector.resize(sub_keys.size());
+ std::vector<rocksdb::Status> statuses_vector;
+ statuses_vector.resize(sub_keys.size());
+ storage_->MultiGet(read_options, storage_->GetDB()->DefaultColumnFamily(),
keys.size(), keys.data(),
+ values_vector.data(), statuses_vector.data());
+
+ auto now = util::GetTimeStampMS();
+ for (size_t i = 0; i < keys.size(); i++) {
+ if (!statuses_vector[i].ok() && !statuses_vector[i].IsNotFound()) return
statuses_vector[i];
+
+ // no such field exists
+ if (statuses_vector[i].IsNotFound()) {
+ ret->emplace_back(-2);
+ continue;
+ }
+
+ InternalKey sub_ikey(ns_key, fields[i], metadata.version,
storage_->IsSlotIdEncoded());
Review Comment:
Why not use `subkeys[i]` directly?
##########
src/types/redis_hash.cc:
##########
@@ -418,4 +472,208 @@ rocksdb::Status Hash::RandField(const Slice &user_key,
int64_t command_count, st
return rocksdb::Status::OK();
}
+rocksdb::Status Hash::ExpireFields(const Slice &user_key, uint64_t expire_ms,
const std::vector<Slice> &fields,
Review Comment:
can remove `is_persist` by checking if `expire_ms` is equal to 0?
##########
src/commands/cmd_hash.cc:
##########
@@ -429,6 +430,228 @@ class CommandHRandField : public Commander {
bool no_parameters_ = true;
};
+class CommandFieldExpireBase : public Commander {
+ protected:
+ Status commonParse(const std::vector<std::string> &args, int start_idx) {
+ int idx = start_idx;
+ CommandParser parser(args, idx);
+ std::string_view expire_flag, num_flag;
+ uint64_t fields_num = 0;
+ while (parser.Good()) {
+ if (parser.EatEqICaseFlag("FIELDS", num_flag)) {
+ fields_num = GET_OR_RET(parser.template TakeInt<uint64_t>());
+ idx += 2;
Review Comment:
We don't need to record the index here, it's good to use `parse.Good()` to
check if there are remaining arguments to be consumed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]