PokIsemaine commented on code in PR #2262:
URL: https://github.com/apache/kvrocks/pull/2262#discussion_r1589917903
##########
src/storage/redis_db.cc:
##########
@@ -777,4 +782,195 @@ rocksdb::Status Database::Copy(const std::string &key,
const std::string &new_ke
return storage_->Write(storage_->DefaultWriteOptions(),
batch->GetWriteBatch());
}
+std::optional<std::string> Database::lookupKeyByPattern(const std::string
&pattern, const std::string &subst) {
+ if (pattern == "#") {
+ return subst;
+ }
+
+ auto match_pos = pattern.find('*');
+ if (match_pos == std::string::npos) {
+ return std::nullopt;
+ }
+
+ // hash field
+ std::string field;
+ auto arrow_pos = pattern.find("->", match_pos + 1);
+ if (arrow_pos != std::string::npos && arrow_pos + 2 < pattern.size()) {
+ field = pattern.substr(arrow_pos + 2);
+ }
+
+ std::string key = pattern.substr(0, match_pos + 1);
+ key.replace(match_pos, 1, subst);
+
+ std::string value;
+ if (!field.empty()) {
+ auto hash_db = redis::Hash(storage_, namespace_);
+ RedisType type = RedisType::kRedisNone;
+ if (auto s = hash_db.Type(key, &type); !s.ok() || type !=
RedisType::kRedisHash) {
+ return std::nullopt;
+ }
+
+ hash_db.Get(key, field, &value);
+ } else {
+ auto string_db = redis::String(storage_, namespace_);
+ RedisType type = RedisType::kRedisNone;
+ if (auto s = string_db.Type(key, &type); !s.ok() || type !=
RedisType::kRedisString) {
+ return std::nullopt;
+ }
+ string_db.Get(key, &value);
+ }
+ return value;
+}
+
+rocksdb::Status Database::Sort(const RedisType &type, const std::string &key,
const SortArgument &args,
+ std::vector<std::optional<std::string>> *elems,
SortResult *res) {
+ // Obtain the length of the object to sort.
+ const std::string ns_key = AppendNamespacePrefix(key);
+ Metadata metadata(type, false);
+ auto s = GetMetadata(GetOptions{}, {type}, ns_key, &metadata);
+ if (!s.ok()) {
+ return s;
+ }
+
+ int vectorlen = static_cast<int>(metadata.size);
+
+ // Adjust the offset and count of the limit
+ int offset = args.offset >= vectorlen ? 0 : std::clamp(args.offset, 0,
vectorlen - 1);
+ int count = args.offset >= vectorlen ? 0 : std::clamp(args.count, -1,
vectorlen - offset);
+ if (count == -1) count = vectorlen - offset;
+
+ // Get the elements that need to be sorted
+ std::vector<std::string> str_vec;
+ if (count != 0) {
+ if (type == RedisType::kRedisList) {
+ auto list_db = redis::List(storage_, namespace_);
+
+ if (args.dontsort) {
+ if (args.desc) {
+ list_db.Range(key, -count - offset, -1 - offset, &str_vec);
+ std::reverse(str_vec.begin(), str_vec.end());
+ } else {
+ list_db.Range(key, offset, offset + count - 1, &str_vec);
+ }
+ } else {
+ list_db.Range(key, 0, -1, &str_vec);
+ }
+ } else if (type == RedisType::kRedisSet) {
+ auto set_db = redis::Set(storage_, namespace_);
+ set_db.Members(key, &str_vec);
+
+ if (args.dontsort) {
+ str_vec = std::vector(str_vec.begin() + offset, str_vec.begin() +
offset + count);
Review Comment:
Sorry, I don't quite understand. Do you mean using `std::move` or something
like `std::advance`? Can you paste the desired code directly? Or you can make
changes directly. Thank you so much!
--
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]