1
0
Fork 0
mirror of https://github.com/dragonflydb/dragonfly.git synced 2024-12-14 11:58:02 +00:00

fix(rdb): Fix loading of small integers and remove code duplication (#1571)

* fix(rdb): Fix loading of small integers and remove code duplication

* Add a test
This commit is contained in:
Roy Jacobson 2023-07-19 14:21:14 +02:00 committed by Roman Gershman
parent 7397c3de85
commit 913b4b4c30
2 changed files with 14 additions and 12 deletions

View file

@ -1160,20 +1160,14 @@ auto RdbLoaderBase::FetchLzfStringObject() -> io::Result<string> {
}
auto RdbLoaderBase::FetchIntegerObject(int enctype) -> io::Result<string> {
long long val;
io::Result<long long> val = ReadIntObj(enctype);
if (enctype == RDB_ENC_INT8) {
SET_OR_UNEXPECT(FetchInt<int8_t>(), val);
} else if (enctype == RDB_ENC_INT16) {
SET_OR_UNEXPECT(FetchInt<uint16_t>(), val);
} else if (enctype == RDB_ENC_INT32) {
SET_OR_UNEXPECT(FetchInt<uint32_t>(), val);
} else {
return Unexpected(errc::invalid_encoding);
if (!val.has_value()) {
return val.get_unexpected();
}
char buf[32];
absl::numbers_internal::FastIntToBuffer(val, buf);
absl::numbers_internal::FastIntToBuffer(*val, buf);
return string(buf);
}
@ -1318,9 +1312,9 @@ io::Result<long long> RdbLoaderBase::ReadIntObj(int enctype) {
if (enctype == RDB_ENC_INT8) {
SET_OR_UNEXPECT(FetchInt<int8_t>(), val);
} else if (enctype == RDB_ENC_INT16) {
SET_OR_UNEXPECT(FetchInt<uint16_t>(), val);
SET_OR_UNEXPECT(FetchInt<int16_t>(), val);
} else if (enctype == RDB_ENC_INT32) {
SET_OR_UNEXPECT(FetchInt<uint32_t>(), val);
SET_OR_UNEXPECT(FetchInt<int32_t>(), val);
} else {
return Unexpected(errc::invalid_encoding);
}

View file

@ -220,6 +220,10 @@ TEST_F(RdbTest, Reload) {
Run({"zadd", "zs1", "1.1", "a", "-1.1", "b"});
Run({"zadd", "zs2", "1.1", string(510, 'a'), "-1.1", string(502, 'b')});
Run({"hset", "large_keyname", string(240, 'X'), "-5"});
Run({"hset", "large_keyname", string(240, 'Y'), "-500"});
Run({"hset", "large_keyname", string(240, 'Z'), "-50000"});
auto resp = Run({"debug", "reload"});
ASSERT_EQ(resp, "OK");
@ -230,6 +234,10 @@ TEST_F(RdbTest, Reload) {
EXPECT_EQ(4, CheckedInt({"LLEN", "list_key2"}));
EXPECT_EQ(2, CheckedInt({"ZCARD", "zs1"}));
EXPECT_EQ(2, CheckedInt({"ZCARD", "zs2"}));
EXPECT_EQ(-5, CheckedInt({"hget", "large_keyname", string(240, 'X')}));
EXPECT_EQ(-500, CheckedInt({"hget", "large_keyname", string(240, 'Y')}));
EXPECT_EQ(-50000, CheckedInt({"hget", "large_keyname", string(240, 'Z')}));
}
TEST_F(RdbTest, ReloadTtl) {