From efbdebaf9456b4e1a571c683b459239783c80a1f Mon Sep 17 00:00:00 2001 From: Roman Gershman Date: Mon, 28 Mar 2022 17:15:33 +0300 Subject: [PATCH] Fix Size() method for compact object --- src/core/compact_object.cc | 33 ++++++++++++++++++++------------- src/core/compact_object_test.cc | 6 ++++++ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/core/compact_object.cc b/src/core/compact_object.cc index ef3415b47..89fb45e7a 100644 --- a/src/core/compact_object.cc +++ b/src/core/compact_object.cc @@ -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 { diff --git a/src/core/compact_object_test.cc b/src/core/compact_object_test.cc index eed9feafd..42f2bfa4d 100644 --- a/src/core/compact_object_test.cc +++ b/src/core/compact_object_test.cc @@ -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(); }