diff --git a/src/types/redis_string.cc b/src/types/redis_string.cc index 85a3a95d9da..a3d27492e0f 100644 --- a/src/types/redis_string.cc +++ b/src/types/redis_string.cc @@ -65,7 +65,9 @@ rocksdb::Status String::getRawValue(engine::Context &ctx, const std::string &ns_ Metadata metadata(kRedisNone, false); Slice slice = *raw_value; - return ParseMetadataWithStats({kRedisString}, &slice, &metadata); + s = ParseMetadataWithStats({kRedisString}, &slice, &metadata); + if (!s.ok()) raw_value->clear(); + return s; } rocksdb::Status String::getValueAndExpire(engine::Context &ctx, const std::string &ns_key, std::string *value, @@ -329,7 +331,6 @@ rocksdb::Status String::IncrBy(engine::Context &ctx, const std::string &user_key if (!s.ok() && !s.IsNotFound()) return s; if (s.IsNotFound()) { Metadata metadata(kRedisString, false); - raw_value.clear(); metadata.Encode(&raw_value); } @@ -368,7 +369,6 @@ rocksdb::Status String::IncrByFloat(engine::Context &ctx, const std::string &use if (s.IsNotFound()) { Metadata metadata(kRedisString, false); - raw_value.clear(); metadata.Encode(&raw_value); } size_t offset = Metadata::GetOffsetAfterExpire(raw_value[0]); diff --git a/tests/cppunit/types/string_test.cc b/tests/cppunit/types/string_test.cc index 386aebb31d9..1b5c1a27c2e 100644 --- a/tests/cppunit/types/string_test.cc +++ b/tests/cppunit/types/string_test.cc @@ -250,6 +250,15 @@ TEST_F(RedisStringTest, SetRange) { string_->Get(*ctx_, key_, &value); EXPECT_EQ(16, value.size()); auto s = string_->Del(*ctx_, key_); + + string_->SetEX(*ctx_, key_, "test-value", 1); + sleep(2); + s = string_->Get(*ctx_, key_, &value); + EXPECT_TRUE(s.IsNotFound()); + string_->SetRange(*ctx_, key_, 0, "12345", &ret); + EXPECT_EQ(5, ret); + string_->Get(*ctx_, key_, &value); + EXPECT_EQ("12345", value); } TEST_F(RedisStringTest, CAS) {