-
Notifications
You must be signed in to change notification settings - Fork 607
feat(hash): Support hash field expiration #2402
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
Open
jjz921024
wants to merge
4
commits into
apache:unstable
Choose a base branch
from
jjz921024:hfe
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| #include "error_constants.h" | ||
| #include "scan_base.h" | ||
| #include "server/server.h" | ||
| #include "time_util.h" | ||
| #include "types/redis_hash.h" | ||
|
|
||
| namespace redis { | ||
|
|
@@ -444,6 +445,238 @@ class CommandHRandField : public Commander { | |
| bool no_parameters_ = true; | ||
| }; | ||
|
|
||
| class CommandFieldExpireBase : public Commander { | ||
| protected: | ||
| Status commonParse(const std::vector<std::string> &args, int start_idx) { | ||
| CommandParser parser(args, start_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>()); | ||
| break; | ||
| } else if (parser.EatEqICaseFlag("NX", expire_flag)) { | ||
| field_expire_type_ = HashFieldExpireType::NX; | ||
| } else if (parser.EatEqICaseFlag("XX", expire_flag)) { | ||
| field_expire_type_ = HashFieldExpireType::XX; | ||
| } else if (parser.EatEqICaseFlag("GT", expire_flag)) { | ||
| field_expire_type_ = HashFieldExpireType::GT; | ||
| } else if (parser.EatEqICaseFlag("LT", expire_flag)) { | ||
| field_expire_type_ = HashFieldExpireType::LT; | ||
| } else { | ||
| return parser.InvalidSyntax(); | ||
| } | ||
| } | ||
|
|
||
| auto remains = parser.Remains(); | ||
| auto size = args.size(); | ||
| if (remains != fields_num) { | ||
| return {Status::RedisParseErr, errWrongNumOfArguments}; | ||
| } | ||
|
|
||
| for (size_t i = size - remains; i < size; i++) { | ||
| fields_.emplace_back(args_[i]); | ||
| } | ||
|
|
||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status expireFieldExecute(engine::Context &ctx, Server *srv, Connection *conn, std::string *output) { | ||
| if (!srv->storage->GetConfig()->hash_field_expiration) { | ||
| return {Status::RedisExecErr, "field expiration feature is disabled"}; | ||
| } | ||
|
|
||
| std::vector<int8_t> ret; | ||
| redis::Hash hash_db(srv->storage, conn->GetNamespace()); | ||
| auto s = hash_db.ExpireFields(ctx, args_[1], expire_, fields_, field_expire_type_, &ret); | ||
| if (!s.ok()) { | ||
| return {Status::RedisExecErr, s.ToString()}; | ||
| } | ||
|
|
||
| *output = redis::MultiLen(ret.size()); | ||
| for (const auto &i : ret) { | ||
| output->append(redis::Integer(i)); | ||
| } | ||
|
|
||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status ttlExpireExecute(engine::Context &ctx, Server *srv, Connection *conn, std::vector<int64_t> &ret) { | ||
| redis::Hash hash_db(srv->storage, conn->GetNamespace()); | ||
| auto s = hash_db.TTLFields(ctx, args_[1], fields_, &ret); | ||
| if (!s.ok()) { | ||
| return {Status::RedisExecErr, s.ToString()}; | ||
| } | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| uint64_t expire_ = 0; | ||
| HashFieldExpireType field_expire_type_ = HashFieldExpireType::None; | ||
| std::vector<Slice> fields_; | ||
| }; | ||
|
|
||
| class CommandHExpire : public CommandFieldExpireBase { | ||
| public: | ||
| Status Parse(const std::vector<std::string> &args) override { | ||
| auto parse_result = ParseInt<uint64_t>(args[2], 10); | ||
| if (!parse_result) return {Status::RedisParseErr, errValueNotInteger}; | ||
|
|
||
| expire_ = *parse_result * 1000 + util::GetTimeStampMS(); | ||
| return CommandFieldExpireBase::commonParse(args, 3); | ||
| } | ||
|
|
||
| Status Execute(engine::Context &ctx, Server *srv, Connection *conn, std::string *output) override { | ||
| return expireFieldExecute(ctx, srv, conn, output); | ||
| } | ||
| }; | ||
|
|
||
| class CommandHExpireAt : public CommandFieldExpireBase { | ||
| public: | ||
| Status Parse(const std::vector<std::string> &args) override { | ||
| auto parse_result = ParseInt<uint64_t>(args[2], 10); | ||
| if (!parse_result) return {Status::RedisParseErr, errValueNotInteger}; | ||
|
|
||
| expire_ = *parse_result * 1000; | ||
| return CommandFieldExpireBase::commonParse(args, 3); | ||
| } | ||
|
|
||
| Status Execute(engine::Context &ctx, Server *srv, Connection *conn, std::string *output) override { | ||
| return expireFieldExecute(ctx, srv, conn, output); | ||
| } | ||
| }; | ||
|
|
||
| class CommandHPExpire : public CommandFieldExpireBase { | ||
| public: | ||
| Status Parse(const std::vector<std::string> &args) override { | ||
| auto parse_result = ParseInt<uint64_t>(args[2], 10); | ||
| if (!parse_result) return {Status::RedisParseErr, errValueNotInteger}; | ||
|
|
||
| expire_ = *parse_result + util::GetTimeStampMS(); | ||
| return CommandFieldExpireBase::commonParse(args, 3); | ||
| } | ||
|
|
||
| Status Execute(engine::Context &ctx, Server *srv, Connection *conn, std::string *output) override { | ||
| return expireFieldExecute(ctx, srv, conn, output); | ||
| } | ||
| }; | ||
|
|
||
| class CommandHPExpireAt : public CommandFieldExpireBase { | ||
| public: | ||
| Status Parse(const std::vector<std::string> &args) override { | ||
| auto parse_result = ParseInt<uint64_t>(args[2], 10); | ||
| if (!parse_result) return {Status::RedisParseErr, errValueNotInteger}; | ||
|
|
||
| expire_ = *parse_result; | ||
| return CommandFieldExpireBase::commonParse(args, 3); | ||
| } | ||
|
|
||
| Status Execute(engine::Context &ctx, Server *srv, Connection *conn, std::string *output) override { | ||
| return expireFieldExecute(ctx, srv, conn, output); | ||
| } | ||
| }; | ||
|
|
||
| class CommandHExpireTime : public CommandFieldExpireBase { | ||
| public: | ||
| Status Parse(const std::vector<std::string> &args) override { return CommandFieldExpireBase::commonParse(args, 2); } | ||
|
|
||
| Status Execute(engine::Context &ctx, Server *srv, Connection *conn, std::string *output) override { | ||
| std::vector<int64_t> ret; | ||
| auto s = ttlExpireExecute(ctx, srv, conn, ret); | ||
| if (!s.IsOK()) { | ||
| return {Status::RedisExecErr, s.Msg()}; | ||
| } | ||
| auto now = util::GetTimeStampMS(); | ||
| *output = redis::MultiLen(ret.size()); | ||
| for (const auto &ttl : ret) { | ||
| if (ttl > 0) { | ||
| output->append(redis::Integer((now + ttl) / 1000)); | ||
| } else { | ||
| output->append(redis::Integer(ttl)); | ||
| } | ||
| } | ||
| return Status::OK(); | ||
| } | ||
| }; | ||
|
|
||
| class CommandHPExpireTime : public CommandFieldExpireBase { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The command HExpireTime/HTTL/HPTTL doesn't support NX/XX/LT/GT flags. By the way, it should be good to remove the ttlExpireExecute/expireFieldExecute and only add a reply helper function like |
||
| public: | ||
| Status Parse(const std::vector<std::string> &args) override { return CommandFieldExpireBase::commonParse(args, 2); } | ||
|
|
||
| Status Execute(engine::Context &ctx, Server *srv, Connection *conn, std::string *output) override { | ||
| std::vector<int64_t> ret; | ||
| auto s = ttlExpireExecute(ctx, srv, conn, ret); | ||
| if (!s.IsOK()) { | ||
| return {Status::RedisExecErr, s.Msg()}; | ||
| } | ||
| auto now = util::GetTimeStampMS(); | ||
| *output = redis::MultiLen(ret.size()); | ||
| for (const auto &ttl : ret) { | ||
| if (ttl > 0) { | ||
| output->append(redis::Integer(now + ttl)); | ||
| } else { | ||
| output->append(redis::Integer(ttl)); | ||
| } | ||
| } | ||
| return Status::OK(); | ||
| } | ||
| }; | ||
|
|
||
| class CommandHTTL : public CommandFieldExpireBase { | ||
| public: | ||
| Status Parse(const std::vector<std::string> &args) override { return CommandFieldExpireBase::commonParse(args, 2); } | ||
|
|
||
| Status Execute(engine::Context &ctx, Server *srv, Connection *conn, std::string *output) override { | ||
| std::vector<int64_t> ret; | ||
| auto s = ttlExpireExecute(ctx, srv, conn, ret); | ||
| if (!s.IsOK()) { | ||
| return {Status::RedisExecErr, s.Msg()}; | ||
| } | ||
| *output = redis::MultiLen(ret.size()); | ||
| for (const auto &ttl : ret) { | ||
| output->append(redis::Integer(ttl > 0 ? ttl / 1000 : ttl)); | ||
| } | ||
| return Status::OK(); | ||
| } | ||
| }; | ||
|
|
||
| class CommandHPTTL : public CommandFieldExpireBase { | ||
| public: | ||
| Status Parse(const std::vector<std::string> &args) override { return CommandFieldExpireBase::commonParse(args, 2); } | ||
|
|
||
| Status Execute(engine::Context &ctx, Server *srv, Connection *conn, std::string *output) override { | ||
| std::vector<int64_t> ret; | ||
| auto s = ttlExpireExecute(ctx, srv, conn, ret); | ||
| if (!s.IsOK()) { | ||
| return {Status::RedisExecErr, s.Msg()}; | ||
| } | ||
| *output = redis::MultiLen(ret.size()); | ||
| for (const auto &ttl : ret) { | ||
| output->append(redis::Integer(ttl)); | ||
| } | ||
| return Status::OK(); | ||
| } | ||
| }; | ||
|
|
||
| class CommandHPersist : public CommandFieldExpireBase { | ||
| public: | ||
| Status Parse(const std::vector<std::string> &args) override { return CommandFieldExpireBase::commonParse(args, 2); } | ||
|
|
||
| Status Execute(engine::Context &ctx, Server *srv, Connection *conn, std::string *output) override { | ||
| std::vector<int8_t> ret; | ||
| redis::Hash hash_db(srv->storage, conn->GetNamespace()); | ||
| auto s = hash_db.PersistFields(ctx, args_[1], fields_, &ret); | ||
| if (!s.ok()) { | ||
| return {Status::RedisExecErr, s.ToString()}; | ||
| } | ||
|
|
||
| *output = redis::MultiLen(ret.size()); | ||
| for (const auto &i : ret) { | ||
| output->append(redis::Integer(i)); | ||
| } | ||
| return Status::OK(); | ||
| } | ||
| }; | ||
|
|
||
| REDIS_REGISTER_COMMANDS(Hash, MakeCmdAttr<CommandHGet>("hget", 3, "read-only", 1, 1, 1), | ||
| MakeCmdAttr<CommandHIncrBy>("hincrby", 4, "write", 1, 1, 1), | ||
| MakeCmdAttr<CommandHIncrByFloat>("hincrbyfloat", 4, "write", 1, 1, 1), | ||
|
|
@@ -460,6 +693,15 @@ REDIS_REGISTER_COMMANDS(Hash, MakeCmdAttr<CommandHGet>("hget", 3, "read-only", 1 | |
| MakeCmdAttr<CommandHGetAll>("hgetall", 2, "read-only slow", 1, 1, 1), | ||
| MakeCmdAttr<CommandHScan>("hscan", -3, "read-only", 1, 1, 1), | ||
| MakeCmdAttr<CommandHRangeByLex>("hrangebylex", -4, "read-only", 1, 1, 1), | ||
| MakeCmdAttr<CommandHRandField>("hrandfield", -2, "read-only slow", 1, 1, 1), ) | ||
| MakeCmdAttr<CommandHRandField>("hrandfield", -2, "read-only slow", 1, 1, 1), | ||
| MakeCmdAttr<CommandHExpire>("hexpire", -6, "write", 1, 1, 1), | ||
| MakeCmdAttr<CommandHExpireAt>("hexpireat", -6, "write", 1, 1, 1), | ||
| MakeCmdAttr<CommandHExpireTime>("hexpiretime", -5, "read-only", 1, 1, 1), | ||
| MakeCmdAttr<CommandHPExpire>("hpexpire", -6, "write", 1, 1, 1), | ||
| MakeCmdAttr<CommandHPExpireAt>("hpexpireat", -6, "write", 1, 1, 1), | ||
| MakeCmdAttr<CommandHPExpireTime>("hpexpiretime", -5, "read-only", 1, 1, 1), | ||
| MakeCmdAttr<CommandHPersist>("hpersist", -5, "write", 1, 1, 1), | ||
| MakeCmdAttr<CommandHTTL>("httl", -5, "read-only", 1, 1, 1), | ||
| MakeCmdAttr<CommandHPTTL>("hpttl", -5, "read-only", 1, 1, 1), ) | ||
|
|
||
| } // namespace redis | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.