Skip to content

Commit 3f1e5db

Browse files
committed
hset_family: Specify keepttl when incrementing a field value
Signed-off-by: Abhijat Malviya <[email protected]>
1 parent 6bc9b0a commit 3f1e5db

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/server/hset_family.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ struct HMapWrap {
122122
}
123123

124124
void AddOrUpdate(std::string_view key, std::string_view value) {
125-
Overloaded ov{[&](StringMap* sm) { sm->AddOrUpdate(key, value); },
125+
Overloaded ov{[&](StringMap* sm) { sm->AddOrUpdate(key, value, UINT32_MAX, true); },
126126
[&](detail::ListpackWrap& lw) { lw.Insert(key, value, false); }};
127127
VisitMut(ov);
128128
}
@@ -320,7 +320,7 @@ OpStatus OpIncrBy(const OpArgs& op_args, string_view key, string_view field, Inc
320320
optional<string_view> res;
321321
if (!add_res.is_new) {
322322
if (auto it = hw.Find(field); it)
323-
res = (*it).second;
323+
res = it->second;
324324
}
325325

326326
if (OpStatus status = IncrementValue(res, param); status != OpStatus::OK)

src/server/hset_family_test.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,22 @@ TEST_F(HSetFamilyTest, HIncrRespected) {
190190
EXPECT_EQ(11, CheckedInt({"hget", "key", "a"}));
191191
}
192192

193+
TEST_F(HSetFamilyTest, HIncrCmdsPreserveTtl) {
194+
Run({"hsetex", "key", "5", "a", "1"});
195+
EXPECT_EQ(5, CheckedInt({"fieldttl", "key", "a"}));
196+
EXPECT_EQ(2, CheckedInt({"hincrby", "key", "a", "1"}));
197+
EXPECT_EQ(5, CheckedInt({"fieldttl", "key", "a"}));
198+
199+
// If the field has already expired by the time hincrby runs, the TTL is default
200+
AdvanceTime(5 * 1000);
201+
EXPECT_EQ(1, CheckedInt({"hincrby", "key", "a", "1"}));
202+
EXPECT_EQ(-1, CheckedInt({"fieldttl", "key", "a"}));
203+
204+
Run({"hsetex", "key", "5", "fl", "1.1"});
205+
EXPECT_EQ(5, CheckedInt({"fieldttl", "key", "fl"}));
206+
EXPECT_EQ("2.2", Run({"hincrbyfloat", "key", "fl", "1.1"}));
207+
}
208+
193209
TEST_F(HSetFamilyTest, HScan) {
194210
auto resp = Run("hscan non-existing-key 100 count 5");
195211
ASSERT_THAT(resp, ArgType(RespExpr::ARRAY));

0 commit comments

Comments
 (0)