Skip to content
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

fix issue 2096: The setrange and setbit commands do not retain the expiration time of the original key #2095

Merged
merged 2 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/storage/src/redis_strings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -657,10 +657,12 @@ Status RedisStrings::SetBit(const Slice& key, int64_t offset, int32_t on, int32_
Status s = db_->Get(default_read_options_, key, &meta_value);
if (s.ok() || s.IsNotFound()) {
std::string data_value;
int32_t timestamp = 0;
dingxiaoshuai123 marked this conversation as resolved.
Show resolved Hide resolved
if (s.ok()) {
ParsedStringsValue parsed_strings_value(&meta_value);
if (!parsed_strings_value.IsStale()) {
data_value = parsed_strings_value.value().ToString();
timestamp = parsed_strings_value.timestamp();
}
}
size_t byte = offset >> 3;
Expand All @@ -686,6 +688,7 @@ Status RedisStrings::SetBit(const Slice& key, int64_t offset, int32_t on, int32_
data_value.append(1, byte_val);
}
StringsValue strings_value(data_value);
strings_value.set_timestamp(timestamp);
return db_->Put(rocksdb::WriteOptions(), key, strings_value.Encode());
} else {
return s;
Expand Down Expand Up @@ -802,13 +805,15 @@ Status RedisStrings::Setrange(const Slice& key, int64_t start_offset, const Slic
ScopeRecordLock l(lock_mgr_, key);
Status s = db_->Get(default_read_options_, key, &old_value);
if (s.ok()) {
int32_t timestamp = 0;
ParsedStringsValue parsed_strings_value(&old_value);
parsed_strings_value.StripSuffix();
if (parsed_strings_value.IsStale()) {
std::string tmp(start_offset, '\0');
new_value = tmp.append(value.data());
*ret = static_cast<int32_t>(new_value.length());
} else {
timestamp = parsed_strings_value.timestamp();
if (static_cast<size_t>(start_offset) > old_value.length()) {
old_value.resize(start_offset);
new_value = old_value.append(value.data());
Expand All @@ -823,6 +828,7 @@ Status RedisStrings::Setrange(const Slice& key, int64_t start_offset, const Slic
}
*ret = static_cast<int32_t>(new_value.length());
StringsValue strings_value(new_value);
strings_value.set_timestamp(timestamp);
return db_->Put(default_write_options_, key, strings_value.Encode());
} else if (s.IsNotFound()) {
std::string tmp(start_offset, '\0');
Expand Down
30 changes: 27 additions & 3 deletions tests/integration/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,23 @@ var _ = Describe("String Commands", func() {
Expect(get.Val()).To(Equal("hello"))
})

It("should SetBit", func() {
setBit := client.SetBit(ctx, "key_3s", 7, 1)
Expect(setBit.Err()).NotTo(HaveOccurred())
Expect(setBit.Val()).To(Equal(int64(0)))

Expect(client.Expire(ctx, "key_3s", 3*time.Second).Val()).To(Equal(true))
Expect(client.TTL(ctx, "key_3s").Val()).NotTo(Equal(int64(-2)))

setBit = client.SetBit(ctx, "key_3s", 69, 1)
Expect(client.TTL(ctx, "key_3s").Val()).NotTo(Equal(int64(-2)))
Expect(setBit.Err()).NotTo(HaveOccurred())
Expect(setBit.Val()).To(Equal(int64(0)))

time.Sleep(4 * time.Second)
Expect(client.TTL(ctx, "key_3s").Val()).To(Equal(time.Duration(-2)))
})

It("should GetBit", func() {
setBit := client.SetBit(ctx, "key", 7, 1)
Expect(setBit.Err()).NotTo(HaveOccurred())
Expand Down Expand Up @@ -867,17 +884,24 @@ var _ = Describe("String Commands", func() {
})

It("should SetRange", func() {
set := client.Set(ctx, "key", "Hello World", 0)
set := client.Set(ctx, "key_3s", "Hello World", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))

range_ := client.SetRange(ctx, "key", 6, "Redis")
Expect(client.Expire(ctx, "key_3s", 3*time.Second).Val()).To(Equal(true))
Expect(client.TTL(ctx, "key_3s").Val()).NotTo(Equal(int64(-2)))

range_ := client.SetRange(ctx, "key_3s", 6, "Redis")
Expect(range_.Err()).NotTo(HaveOccurred())
Expect(range_.Val()).To(Equal(int64(11)))

get := client.Get(ctx, "key")
get := client.Get(ctx, "key_3s")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("Hello Redis"))
Expect(client.TTL(ctx, "key_3s").Val()).NotTo(Equal(int64(-2)))

time.Sleep(4 * time.Second)
Expect(client.TTL(ctx, "key_3s").Val()).To(Equal(time.Duration(-2)))
})

It("should StrLen", func() {
Expand Down
Loading