Skip to content

Commit 0f016b7

Browse files
Merge 75c452b into db527f7
2 parents db527f7 + 75c452b commit 0f016b7

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed

ydb/core/persqueue/key.h

+58-5
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class TKeyPrefix : public TBuffer
3535
: Partition(partition)
3636
{
3737
Resize(UnmarkedSize());
38-
*PtrType() = type;
39-
memcpy(PtrPartition(), Sprintf("%.10" PRIu32, Partition.InternalPartitionId).data(), 10);
38+
SetType(type);
39+
memcpy(PtrPartition(), Sprintf("%.10" PRIu32, Partition.InternalPartitionId).data(), 10);
4040
}
4141

4242
TKeyPrefix(EType type, const TPartitionId& partition, EMark mark)
@@ -62,25 +62,77 @@ class TKeyPrefix : public TBuffer
6262
static constexpr ui32 MarkPosition() { return UnmarkedSize(); }
6363
static constexpr ui32 MarkedSize() { return UnmarkedSize() + 1; }
6464

65+
6566
void SetType(EType type) {
66-
*PtrType() = type;
67+
if (!IsServicePartition()) {
68+
*PtrType() = type;
69+
return;
70+
}
71+
switch (type) {
72+
case TypeNone:
73+
*PtrType() = TypeNone;
74+
case TypeData:
75+
return ServiceTypeData;
76+
case TypeTmpData:
77+
*PtrType() = ServiceTypeTmpData;
78+
case TypeInfo:
79+
*PtrType() = ServiceTypeInfo;
80+
case TypeMeta:
81+
*PtrType() = ServiceTypeMeta;
82+
return TypeMeta;
83+
case TypeTxMeta:
84+
*PtrType() = ServiceTypeTxMeta;
85+
}
6786
}
6887

88+
6989
EType GetType() const {
70-
return EType(*PtrType());
90+
switch (*PtrType()) {
91+
case TypeNone:
92+
return TypeNone;
93+
case TypeData:
94+
case ServiceTypeData:
95+
return TypeData;
96+
case TypeTmpData:
97+
case ServiceTypeTmpData:
98+
return TypeTmpData;
99+
case TypeInfo:
100+
case ServiceTypeInfo:
101+
return TypeInfo;
102+
case TypeMeta:
103+
case ServiceTypeMeta:
104+
return TypeMeta;
105+
case TypeTxMeta:
106+
case ServiceTypeTxMeta:
107+
return TypeTxMeta;
108+
}
109+
Y_ABORT();
110+
return TypeNone;
71111
}
72112

113+
bool IsServicePartition() const {return Partition.WriteId.Defined();}
114+
73115
const TPartitionId& GetPartition() const { return Partition; }
74116

75117
protected:
76118
static constexpr ui32 UnmarkedSize() { return 1 + 10; }
77119

78120
void ParsePartition()
79121
{
80-
Partition.InternalPartitionId = FromString<ui32>(TStringBuf{PtrPartition(), 10});
122+
Partition.OriginalPartitionId = FromString<ui32>(TStringBuf{PtrPartition(), 10});
123+
Partition.InternalPartitionId = Partition.OriginalPartitionId;
81124
}
82125

126+
83127
private:
128+
enum EServiceType : char {
129+
ServiceTypeInfo = 'M',
130+
ServiceTypeData = 'D',
131+
ServiceTypeTmpData = 'X',
132+
ServiceTypeMeta = 'J',
133+
ServiceTypeTxMeta = 'K'
134+
};
135+
84136
char* PtrType() { return Data(); }
85137
char* PtrMark() { return Data() + UnmarkedSize(); }
86138
char* PtrPartition() { return Data() + 1; }
@@ -90,6 +142,7 @@ class TKeyPrefix : public TBuffer
90142
const char* PtrPartition() const { return Data() + 1; }
91143

92144
TPartitionId Partition;
145+
93146
};
94147

95148
// {char type; ui32 partiton; ui64 offset; ui16 partNo; ui32 count, ui16 internalPartsCount}

ydb/core/persqueue/ut/internals_ut.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,29 @@ Y_UNIT_TEST(TestKeyRange) {
228228
UNIT_ASSERT_STRINGS_EQUAL(ToHex(result), ToHex(expected));
229229
} // Y_UNIT_TEST(TestKeyRange)
230230

231+
Y_UNIT_TEST(StoreKeys) {
232+
TKey keyOld(TKeyPrefix::TypeData, TPartitionId{9}, 8, 7, 6, 5, false);
233+
UNIT_ASSERT_VALUES_EQUAL(keyOld.ToString(), "d0000000009_00000000000000000008_00007_0000000006_00005");
234+
235+
TKey keyNew(TKeyPrefix::TypeData, TPartitionId{5, 1, 9}, 8, 7, 6, 5, false);
236+
UNIT_ASSERT_VALUES_EQUAL(keyNew.ToString(), "D0000000009_00000000000000000008_00007_0000000006_00005");
237+
238+
keyNew.SetType(TKeyPrefix::TypeInfo);
239+
UNIT_ASSERT_VALUES_EQUAL(keyNew.ToString(), "M00000000009_00000000000000000008_00007_0000000006_00005");
240+
}
241+
242+
Y_UNIT_TEST(RestoreKeys) {
243+
{
244+
TKey key("X0000000001_00000000000000000002_00003_0000000004_00005");
245+
UNIT_ASSERT(key.GetType() == TKeyPrefix::TypeTmpData);
246+
UNIT_ASSERT_VALUES_EQUAL(key.GetPartition().InternalPartitionId, 1);
247+
}
248+
{
249+
TKey key("i0000000001_00000000000000000002_00003_0000000004_00005");
250+
UNIT_ASSERT(key.GetType() == TKeyPrefix::TypeMeta);
251+
UNIT_ASSERT_VALUES_EQUAL(key.GetPartition().InternalPartitionId, 1);
252+
}
253+
}
231254

232255
} //Y_UNIT_TEST_SUITE
233256

0 commit comments

Comments
 (0)