Skip to content

Commit a3b495c

Browse files
Mikester95copybara-github
authored andcommitted
Wire ObjectId::DebugString() to output ObjectIdStr(base 62 repr)
PiperOrigin-RevId: 718867895 Change-Id: I2a5935f996d48cc839521f28b7be801237c7dbbf
1 parent 98ff8a5 commit a3b495c

File tree

11 files changed

+47
-60
lines changed

11 files changed

+47
-60
lines changed

koladata/internal/object_id.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ ObjectId ObjectId::NoFollowObjectSchemaId() {
5757
return id;
5858
}
5959

60+
std::string ObjectId::DebugString() const {
61+
return ObjectIdStr(*this, /*show_flag_prefix=*/true);
62+
}
63+
6064
bool AllocationIdSet::InsertBigAllocationSlow(AllocationId id) {
6165
if (auto it = std::lower_bound(ids_.begin(), ids_.end(), id);
6266
it == ids_.end() || *it != id) {

koladata/internal/object_id.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,7 @@ class ObjectId {
119119

120120
// Returns the string representation of the object id.
121121
// Format: <allocator_id><allocation id>.<offset>
122-
std::string DebugString() const {
123-
auto bits2hex_width = [](size_t bit_width) {
124-
return bit_width == 0 ? absl::kNoPad
125-
: absl::PadSpec((bit_width - 1) / 4 + 1);
126-
};
127-
return absl::StrCat(
128-
absl::Hex(InternalHigh64(), absl::kZeroPad16),
129-
absl::Hex(id_ >> offset_bits_, bits2hex_width(64 - offset_bits_)), ":",
130-
absl::Hex(Offset(), bits2hex_width(offset_bits_)));
131-
}
122+
std::string DebugString() const;
132123

133124
friend std::ostream& operator<<(std::ostream& os, const ObjectId& obj) {
134125
os << absl::StrCat(obj.DebugString());

koladata/internal/object_id_test.cc

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -549,17 +549,20 @@ TEST(ObjectIdTest, TypedValueRepr) {
549549
AllocationId alloc_id = Allocate(1024);
550550
std::string repr0 =
551551
arolla::TypedValue::FromValue(alloc_id.ObjectByOffset(0)).Repr();
552-
EXPECT_THAT(repr0, testing::MatchesRegex(R"regexp([a-f0-9]*.000)regexp"));
553552
std::string repr1 =
554-
arolla::TypedValue::FromValue(alloc_id.ObjectByOffset(10)).Repr();
555-
EXPECT_THAT(repr1, testing::MatchesRegex(R"regexp([a-f0-9]*.00a)regexp"));
556-
std::string repr2 = absl::StrCat(alloc_id.ObjectByOffset(0xff));
557-
EXPECT_THAT(repr2, testing::MatchesRegex(R"regexp([a-f0-9]*.0ff)regexp"));
553+
arolla::TypedValue::FromValue(alloc_id.ObjectByOffset(1)).Repr();
554+
std::string repr2 =
555+
arolla::TypedValue::FromValue(alloc_id.ObjectByOffset(2)).Repr();
558556

559557
EXPECT_NE(repr0, repr1);
560-
// All but last digit (in base 16) should be the same.
561-
EXPECT_EQ(repr0.substr(0, repr0.size() - 1),
562-
repr1.substr(0, repr1.size() - 1));
558+
// Test that consecutive ids have a common prefix, i.e. all but the last
559+
// digit. It can happen that consecutive ids do not have this common prefix
560+
// when there is carry-over, but in that case the next consecutive pair should
561+
// have this prefix.
562+
EXPECT_TRUE(
563+
(repr0.substr(0, repr0.size() - 1) ==
564+
repr1.substr(0, repr1.size() - 1)) ||
565+
(repr1.substr(0, repr1.size() - 1) == repr2.substr(0, repr2.size() - 1)));
563566
}
564567

565568
TEST(ObjectIdTest, AbslHash) {
@@ -745,18 +748,6 @@ TEST(ObjectIdTest, AllocationIdSetUnionSmall) {
745748
}
746749
}
747750

748-
TEST(ObjectIdTest, ObjectIdDebugStringFormatBoundaryCondition) {
749-
EXPECT_THAT(Allocate(0).ObjectByOffset(0).DebugString(),
750-
MatchesRegex(R"regex([0-9a-f]{32}:0)regex"));
751-
752-
for (size_t i = 1; i <= 10; i++) {
753-
AllocationId alloc_id = Allocate(1ull << (i * 4));
754-
EXPECT_THAT(alloc_id.ObjectByOffset((1ull << (i * 4)) - 1).DebugString(),
755-
MatchesRegex(absl::StrFormat(
756-
R"regex([0-9a-f]{%d}:[f]{%d})regex", (32 - i), i)));
757-
}
758-
}
759-
760751
TEST(ObjectIdTest, ObjectIdStringFormat) {
761752
EXPECT_THAT(ObjectIdStr(CreateUuidObjectWithMetadata(
762753
arolla::FingerprintHasher("").Combine(57).Finish(),

koladata/internal/triples_test.cc

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,17 @@ TEST(TriplesTest, TripleDebugString) {
3636
ObjectId obj = Allocate(15).ObjectByOffset(3);
3737
ObjectId dict = AllocateDicts(15).ObjectByOffset(3);
3838
ObjectId schema = AllocateExplicitSchema();
39-
EXPECT_THAT(
40-
(AttrTriple{obj, "a", DataItem(1)}).DebugString(),
41-
::testing::MatchesRegex("ObjectId=[0-9a-f]{31}:3 attr=a value=1"));
39+
EXPECT_THAT((AttrTriple{obj, "a", DataItem(1)}).DebugString(),
40+
::testing::MatchesRegex(
41+
"ObjectId=Entity:\\$[0-9a-zA-Z]{22} attr=a value=1"));
4242
EXPECT_THAT((DictItemTriple{dict, DataItem(arolla::Text("a")), DataItem(1)})
4343
.DebugString(),
44-
::testing::MatchesRegex("DictId=[0-9a-f]{31}:3 key='a' value=1"));
45-
EXPECT_THAT(
46-
(DictItemTriple{schema, DataItem(arolla::Text("a")), DataItem(1)})
47-
.DebugString(),
48-
::testing::MatchesRegex("SchemaId=[0-9a-f]{32}:0 key='a' value=1"));
44+
::testing::MatchesRegex(
45+
"DictId=Dict:\\$[0-9a-zA-Z]{22} key='a' value=1"));
46+
EXPECT_THAT((DictItemTriple{schema, DataItem(arolla::Text("a")), DataItem(1)})
47+
.DebugString(),
48+
::testing::MatchesRegex(
49+
"SchemaId=Schema:\\$[0-9a-zA-Z]{22} key='a' value=1"));
4950
}
5051

5152
TEST(TriplesTest, SimpleAttr) {
@@ -60,11 +61,11 @@ TEST(TriplesTest, SimpleAttr) {
6061
ASSERT_OK(db->SetAttr(DataItem(obj2), "d", DataItem()));
6162

6263
EXPECT_EQ(Triples(*db->ExtractContent()).DebugString(), R"DB(DataBag {
63-
ObjectId=04000000000000010000000000000000:0 attr=a value=04000000000000020000000000000000:0
64-
ObjectId=04000000000000010000000000000000:0 attr=c value='aaa'
65-
ObjectId=04000000000000020000000000000000:0 attr=b value=5
66-
ObjectId=04000000000000020000000000000000:0 attr=c value=b'bbb'
67-
ObjectId=04000000000000020000000000000000:0 attr=d value=None
64+
ObjectId=Entity:#07Xy61DuGvoVPxWFT5JFKK attr=a value=Entity:#07Xy61DuGvorOdnpUBTWsa
65+
ObjectId=Entity:#07Xy61DuGvoVPxWFT5JFKK attr=c value='aaa'
66+
ObjectId=Entity:#07Xy61DuGvorOdnpUBTWsa attr=b value=5
67+
ObjectId=Entity:#07Xy61DuGvorOdnpUBTWsa attr=c value=b'bbb'
68+
ObjectId=Entity:#07Xy61DuGvorOdnpUBTWsa attr=d value=None
6869
})DB");
6970
}
7071

@@ -86,8 +87,8 @@ TEST(TriplesTest, SimpleDict) {
8687

8788
EXPECT_THAT(Triples(*db->ExtractContent(index)).DebugString(),
8889
::testing::MatchesRegex(R"DB(DataBag \{
89-
DictId=[0-9a-f]+:0 key=1 value=2
90-
DictId=[0-9a-f]+:0 key=3 value=4
90+
DictId=Dict:\$[0-9a-zA-Z]{22} key=1 value=2
91+
DictId=Dict:\$[0-9a-zA-Z]{22} key=3 value=4
9192
\})DB"));
9293

9394
EXPECT_THAT(db, DataBagEqual(db));
@@ -105,8 +106,8 @@ TEST(TriplesTest, SimpleList) {
105106
"DataBag {\n}");
106107
EXPECT_THAT(Triples(*db->ExtractContent()).DebugString(),
107108
::testing::MatchesRegex(R"DB(DataBag \{
108-
ListId=[0-9a-f]+:0 \[5, 3\]
109-
ListId=[0-9a-f]+:0 \[4\]
109+
ListId=List:\$[0-9a-zA-Z]{22} \[5, 3\]
110+
ListId=List:\$[0-9a-zA-Z]{22} \[4\]
110111
\})DB"));
111112
EXPECT_THAT(db, DataBagEqual(db));
112113
EXPECT_THAT(db, Not(DataBagEqual(DataBagImpl::CreateEmptyDatabag())));

koladata/repr_utils_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ TEST(ReprUtilTest, TestAssembleError_NoCommonSchema) {
6969
MatchesRegex(
7070
R"regex((.|\n)*cannot find a common schema for provided schemas(.|\n)*)regex"),
7171
MatchesRegex(
72-
R"regex((.|\n)*the common schema\(s\) [0-9a-f]{32}:0: SCHEMA\(a=INT32, b=STRING\)(.|\n)*)regex"),
72+
R"regex((.|\n)*the common schema\(s\) Schema:\$[0-9a-zA-Z]{22}: SCHEMA\(a=INT32, b=STRING\)(.|\n)*)regex"),
7373
MatchesRegex(
7474
R"regex((.|\n)*the first conflicting schema INT32: INT32(.|\n)*)regex")));
7575
}

py/koladata/functions/tests/new_like_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def test_schema_error_message(self):
338338
exceptions.KodaError,
339339
r"""cannot create Item\(s\)
340340
341-
The cause is: conflicting values for x for [0-9a-z]{32}:0: 1 vs 2""",
341+
The cause is: conflicting values for x for Entity:#[0-9a-zA-Z]{22}: 1 vs 2""",
342342
):
343343
db1.new_like(ds([1, 2, 3]), y=b)
344344

py/koladata/functions/tests/new_shaped_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def test_schema_error_message(self):
337337
exceptions.KodaError,
338338
r"""cannot create Item\(s\)
339339
340-
The cause is: conflicting values for x for [0-9a-z]{32}:0: 1 vs 2""",
340+
The cause is: conflicting values for x for Entity:#[0-9a-zA-Z]{22}: 1 vs 2""",
341341
):
342342
db1.new_shaped(jagged_shape.create_shape(), y=b)
343343

py/koladata/functions/tests/new_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def test_schema_error_message(self):
349349
exceptions.KodaError,
350350
r"""cannot create Item\(s\)
351351
352-
The cause is: conflicting values for x for [0-9a-z]{32}:0: 1 vs 2""",
352+
The cause is: conflicting values for x for Entity:#[0-9a-zA-Z]{22}: 1 vs 2""",
353353
):
354354
db1.new(y=b)
355355

py/koladata/operators/tests/lists_appended_list_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ def test_merge_error(self):
144144
e.set_attr('a', 2)
145145
with self.assertRaisesRegex(
146146
exceptions.KodaError,
147-
'kd.lists.appended_list: conflicting values for a for [0-9a-z:]*: 1'
148-
' vs 2',
147+
'kd.lists.appended_list: conflicting values for a for'
148+
' Entity:#[0-9a-zA-Z]{22}: 1 vs 2',
149149
):
150150
_ = expr_eval.eval(kde.lists.appended_list(lst, e))
151151

py/koladata/operators/tests/lists_concat_lists_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def test_concat_failure(self):
154154
r"""cannot find a common schema for provided schemas
155155
156156
the common schema\(s\) INT32: INT32
157-
the first conflicting schema [0-9a-f]{32}:0: LIST\[INT32\]""",
157+
the first conflicting schema Schema:#[0-9a-zA-Z]{22}: LIST\[INT32\]""",
158158
):
159159
expr_eval.eval(kde.lists.concat_lists(a, b))
160160

0 commit comments

Comments
 (0)