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

Fix Size() method for compact object

This commit is contained in:
Roman Gershman 2022-03-28 17:15:33 +03:00
parent c98bc934f2
commit efbdebaf94
2 changed files with 26 additions and 13 deletions

View file

@ -494,23 +494,30 @@ CompactObj& CompactObj::operator=(CompactObj&& o) noexcept {
}
size_t CompactObj::Size() const {
size_t raw_size = 0;
if (IsInline()) {
return taglen_;
}
raw_size = taglen_;
} else {
switch (taglen_) {
case SMALL_TAG:
raw_size = u_.small_str.size();
break;
case INT_TAG: {
absl::AlphaNum an(u_.ival);
raw_size = an.size();
break;
}
switch (taglen_) {
case SMALL_TAG:
return u_.small_str.size();
case INT_TAG: {
absl::AlphaNum an(u_.ival);
return an.size();
case ROBJ_TAG:
raw_size = u_.r_obj.Size();
break;
default:
LOG(DFATAL) << "Should not reach " << int(taglen_);
}
case ROBJ_TAG:
return u_.r_obj.Size();
}
LOG(DFATAL) << "Should not reach " << int(taglen_);
return 0;
uint8_t encoded = (mask_ & kEncMask);
return encoded ? DecodedLen(raw_size) : raw_size;
}
uint64_t CompactObj::HashCode() const {

View file

@ -97,6 +97,7 @@ TEST_F(CompactObjectTest, NonInline) {
s.assign(25, 'b');
obj.SetString(s);
EXPECT_EQ(s, obj);
EXPECT_EQ(s.size(), obj.Size());
}
TEST_F(CompactObjectTest, InlineAsciiEncoded) {
@ -104,6 +105,7 @@ TEST_F(CompactObjectTest, InlineAsciiEncoded) {
uint64_t expected_val = XXH3_64bits_withSeed(s.data(), s.size(), kSeed);
CompactObj obj{s};
EXPECT_EQ(expected_val, obj.HashCode());
EXPECT_EQ(s.size(), obj.Size());
}
@ -125,8 +127,12 @@ TEST_F(CompactObjectTest, Int) {
TEST_F(CompactObjectTest, MediumString) {
string tmp(512, 'b');
cobj_.SetString(tmp);
EXPECT_EQ(tmp.size(), cobj_.Size());
cobj_.SetString(tmp);
EXPECT_EQ(tmp.size(), cobj_.Size());
cobj_.Reset();
}