mirror of
https://github.com/dragonflydb/dragonfly.git
synced 2025-05-11 02:15:45 +02: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:
parent
e44cc68b73
commit
3b6b0f5436
2 changed files with 14 additions and 12 deletions
|
@ -1165,20 +1165,14 @@ auto RdbLoaderBase::FetchLzfStringObject() -> io::Result<string> {
|
||||||
}
|
}
|
||||||
|
|
||||||
auto RdbLoaderBase::FetchIntegerObject(int enctype) -> 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) {
|
if (!val.has_value()) {
|
||||||
SET_OR_UNEXPECT(FetchInt<int8_t>(), val);
|
return val.get_unexpected();
|
||||||
} 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char buf[32];
|
char buf[32];
|
||||||
absl::numbers_internal::FastIntToBuffer(val, buf);
|
absl::numbers_internal::FastIntToBuffer(*val, buf);
|
||||||
|
|
||||||
return string(buf);
|
return string(buf);
|
||||||
}
|
}
|
||||||
|
@ -1323,9 +1317,9 @@ io::Result<long long> RdbLoaderBase::ReadIntObj(int enctype) {
|
||||||
if (enctype == RDB_ENC_INT8) {
|
if (enctype == RDB_ENC_INT8) {
|
||||||
SET_OR_UNEXPECT(FetchInt<int8_t>(), val);
|
SET_OR_UNEXPECT(FetchInt<int8_t>(), val);
|
||||||
} else if (enctype == RDB_ENC_INT16) {
|
} 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) {
|
} else if (enctype == RDB_ENC_INT32) {
|
||||||
SET_OR_UNEXPECT(FetchInt<uint32_t>(), val);
|
SET_OR_UNEXPECT(FetchInt<int32_t>(), val);
|
||||||
} else {
|
} else {
|
||||||
return Unexpected(errc::invalid_encoding);
|
return Unexpected(errc::invalid_encoding);
|
||||||
}
|
}
|
||||||
|
|
|
@ -220,6 +220,10 @@ TEST_F(RdbTest, Reload) {
|
||||||
Run({"zadd", "zs1", "1.1", "a", "-1.1", "b"});
|
Run({"zadd", "zs1", "1.1", "a", "-1.1", "b"});
|
||||||
Run({"zadd", "zs2", "1.1", string(510, 'a'), "-1.1", string(502, '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"});
|
auto resp = Run({"debug", "reload"});
|
||||||
ASSERT_EQ(resp, "OK");
|
ASSERT_EQ(resp, "OK");
|
||||||
|
|
||||||
|
@ -230,6 +234,10 @@ TEST_F(RdbTest, Reload) {
|
||||||
EXPECT_EQ(4, CheckedInt({"LLEN", "list_key2"}));
|
EXPECT_EQ(4, CheckedInt({"LLEN", "list_key2"}));
|
||||||
EXPECT_EQ(2, CheckedInt({"ZCARD", "zs1"}));
|
EXPECT_EQ(2, CheckedInt({"ZCARD", "zs1"}));
|
||||||
EXPECT_EQ(2, CheckedInt({"ZCARD", "zs2"}));
|
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) {
|
TEST_F(RdbTest, ReloadTtl) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue