Skip to content

Commit

Permalink
Return from HRANGE if key wasn't found (#923)
Browse files Browse the repository at this point in the history
  • Loading branch information
torwig authored Sep 27, 2022
1 parent 9b6cc11 commit 31ded36
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/redis_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ rocksdb::Status Hash::Range(const Slice &user_key, const Slice &start, const Sli
AppendNamespacePrefix(user_key, &ns_key);
HashMetadata metadata(false);
rocksdb::Status s = GetMetadata(ns_key, &metadata);
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
limit = std::min(static_cast<int64_t>(metadata.size), limit);
std::string start_key, stop_key;
InternalKey(ns_key, start, metadata.version, storage_->IsSlotIdEncoded()).Encode(&start_key);
Expand All @@ -299,7 +300,7 @@ rocksdb::Status Hash::Range(const Slice &user_key, const Slice &start, const Sli

auto iter = DBUtil::UniqueIterator(db_, read_options);
iter->Seek(start_key);
for (int i = 0; iter->Valid() && i <= limit - 1; i++) {
for (int64_t i = 0; iter->Valid() && i <= limit - 1; ++i) {
FieldValue tmp_field_value;
InternalKey ikey(iter->key(), storage_->IsSlotIdEncoded());
tmp_field_value.field = ikey.GetSubKey().ToString();
Expand All @@ -309,6 +310,7 @@ rocksdb::Status Hash::Range(const Slice &user_key, const Slice &start, const Sli
}
return rocksdb::Status::OK();
}

rocksdb::Status Hash::GetAll(const Slice &user_key, std::vector<FieldValue> *field_values, HashFetchType type) {
field_values->clear();

Expand Down
9 changes: 8 additions & 1 deletion tests/cppunit/t_hash_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,11 @@ TEST_F(RedisHashTest, HRange) {
EXPECT_EQ("key2", result[2].field);
hash->Del(key_);
}
}
}

TEST_F(RedisHashTest, HRangeNonExistingKey) {
std::vector<FieldValue> result;
auto s = hash->Range("non-existing-key", "any-start-key", "any-end-key", 10, &result);
EXPECT_TRUE(s.ok());
EXPECT_EQ(result.size(), 0);
}

0 comments on commit 31ded36

Please sign in to comment.