Skip to content

Commit 587a6ef

Browse files
committed
fix middle of strings
1 parent 1a690bd commit 587a6ef

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

ydb/core/persqueue/partition_key_range/partition_key_range.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace NKikimr {
66
namespace NPQ {
77

88
TString MiddleOf(const TString& from, const TString& to) {
9+
Y_ABORT_UNLESS(to.empty() || from < to);
10+
911
auto GetChar = [](const TString& str, size_t i, unsigned char defaultValue) {
1012
if (i >= str.size()) {
1113
return defaultValue;
@@ -24,10 +26,12 @@ TString MiddleOf(const TString& from, const TString& to) {
2426

2527
size_t maxSize = std::max(from.size(), to.size());
2628
result.reserve(maxSize + 1);
29+
30+
size_t size = std::max(from.size(), to.size());
2731
size_t i = 0;
28-
for (; i < maxSize; ++i) {
32+
for (; i < size; ++i) {
2933
ui16 f = GetChar(from, i, 0);
30-
ui16 t = GetChar(to, i, 0xFF);
34+
ui16 t = GetChar(to, i, 0);
3135

3236
if (f != t) {
3337
diffFound = true;
@@ -76,12 +80,32 @@ TString MiddleOf(const TString& from, const TString& to) {
7680
}
7781
}
7882

83+
if (!splitted) {
84+
for (; i < from.size() && !splitted; ++i) {
85+
ui16 f = GetChar(from, i, 0);
86+
if (f < 0xFF) {
87+
auto n = (f + 0x100u) / 2u;
88+
result << static_cast<unsigned char>(n);
89+
splitted = true;
90+
} else {
91+
result << static_cast<unsigned char>(0xFFu);
92+
}
93+
}
94+
95+
for (; i < to.size() && !splitted; ++i) {
96+
ui16 t = GetChar(to, i, 0);
97+
auto n = t / 2u;
98+
result << static_cast<unsigned char>(n);
99+
splitted = !!n;
100+
}
101+
}
102+
79103
if (result == from) {
80-
size_t j = splitted ? i : maxSize - 1;
104+
size_t j = maxSize - 1;
81105
ui16 f = GetChar(from, j, 0);
82-
ui16 t = GetChar(to, j, 0xFF);
106+
ui16 t = GetChar(to, j, to.empty() ? 0xFF : 0x00);
83107

84-
result << static_cast<unsigned char>((f - t) & 1 ? 0xFFu: 0x7Fu);
108+
result << static_cast<unsigned char>((f - t) & 1 ? 0x7Fu : 0xFFu);
85109
}
86110

87111
return result;

ydb/core/persqueue/ut/autoscaling_ut.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ Y_UNIT_TEST_SUITE(TopicAutoscaling) {
671671

672672
{
673673
auto res = NKikimr::NPQ::MiddleOf(AsString({0x01}), AsString({0x02}));
674-
UNIT_ASSERT_VALUES_EQUAL(ToHex(res), "01 FF");
674+
UNIT_ASSERT_VALUES_EQUAL(ToHex(res), "01 7F");
675675
}
676676

677677
{
@@ -686,7 +686,7 @@ Y_UNIT_TEST_SUITE(TopicAutoscaling) {
686686

687687
{
688688
auto res = NKikimr::NPQ::MiddleOf(AsString({0x01, 0xFF}), AsString({0x02, 0x00}));
689-
UNIT_ASSERT_VALUES_EQUAL(ToHex(res), "01 FF FF");
689+
UNIT_ASSERT_VALUES_EQUAL(ToHex(res), "01 FF 7F");
690690
}
691691

692692
{
@@ -718,8 +718,12 @@ Y_UNIT_TEST_SUITE(TopicAutoscaling) {
718718
UNIT_ASSERT_VALUES_EQUAL(ToHex(res), "02 00 00 07");
719719
}
720720
{
721-
auto res = NKikimr::NPQ::MiddleOf(AsString({0xFF, 0xFF}), AsString({0xFF}));
722-
UNIT_ASSERT_VALUES_EQUAL(ToHex(res), "FF FF 7F");
721+
auto res = NKikimr::NPQ::MiddleOf(AsString({0xFF}), AsString({0xFF, 0xFF}));
722+
UNIT_ASSERT_VALUES_EQUAL(ToHex(res), "FF 7F");
723+
}
724+
{
725+
auto res = NKikimr::NPQ::MiddleOf(AsString({0x99, 0xFF}), AsString({0x9A}));
726+
UNIT_ASSERT_VALUES_EQUAL(ToHex(res), "99 FF 7F");
723727
}
724728
}
725729
}

ydb/core/tx/schemeshard/schemeshard__operation_alter_pq.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,10 @@ class TAlterPQ: public TSubOperation {
520520
bool splitMergeEnabled = AppData()->FeatureFlags.GetEnableTopicSplitMerge();
521521
if (splitMergeEnabled) {
522522

523+
auto Hex = [](const auto& value) {
524+
return HexText(TBasicStringBuf(value));
525+
};
526+
523527
ui32 nextId = topic->NextPartitionId;
524528
ui32 nextGroupId = topic->TotalGroupCount;
525529

@@ -558,15 +562,16 @@ class TAlterPQ: public TSubOperation {
558562
if (keyRange) {
559563
if (keyRange->FromBound && splitBoundary <= *keyRange->FromBound) {
560564
errStr = TStringBuilder()
561-
<< "Split boundary less or equals FromBound of partition: " << splitBoundary
562-
<< " <= " << *keyRange->FromBound;
565+
<< "Split boundary less or equals FromBound of partition: '" << Hex(splitBoundary)
566+
<< "' <= '" << Hex(*keyRange->FromBound) << "'";
563567
result->SetError(NKikimrScheme::StatusInvalidParameter, errStr);
564568
return result;
565569
}
566570
if (keyRange->ToBound && splitBoundary >= *keyRange->ToBound) {
567571
errStr = TStringBuilder()
568-
<< "Split boundary greate or equals ToBound of partition: " << splitBoundary
569-
<< " >= " << *keyRange->ToBound;
572+
<< "Split boundary greate or equals ToBound of partition: '" << Hex(splitBoundary)
573+
<< "' >= '" << Hex(*keyRange->ToBound)
574+
<< "' (FromBound is '" << Hex(keyRange->FromBound ? *keyRange->FromBound : TString{}) << "')";
570575
result->SetError(NKikimrScheme::StatusInvalidParameter, errStr);
571576
return result;
572577
}

0 commit comments

Comments
 (0)