1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#include <yt/cpp/mapreduce/io/yamr_table_reader.h>
#include <library/cpp/testing/gtest/gtest.h>
using namespace NYT;
template <>
void Out<std::tuple<TString, TString, TString>>(IOutputStream& out, const std::tuple<TString, TString, TString>& value) {
out << "{" << std::get<0>(value) << ", " << std::get<1>(value) << ", " << std::get<2>(value) << "}";
}
////////////////////////////////////////////////////////////////////////////////
class TRowCollection
{
public:
void AddRow(TStringBuf key, TStringBuf subkey, TStringBuf value)
{
TStringStream row;
auto appendLenval = [&] (TStringBuf value) {
ui32 size = value.size();
row.Write(&size, sizeof(size));
row.Write(value);
};
appendLenval(key);
appendLenval(subkey);
appendLenval(value);
RowList_.push_back(row.Str());
}
TString GetStreamDataStartFromRow(ui64 rowIndex) const
{
Y_ABORT_UNLESS(rowIndex < RowList_.size());
TStringStream ss;
ss.Write("\xFC\xFF\xFF\xFF");
ss.Write(&rowIndex, sizeof(rowIndex));
for (size_t i = rowIndex; i != RowList_.size(); ++i) {
ss.Write(RowList_[i]);
}
return ss.Str();
}
size_t ComputeTotalStreamSize() const {
return GetStreamDataStartFromRow(0).size();
}
private:
TVector<TString> RowList_;
};
class TTestRawTableReader
: public TRawTableReader
{
public:
TTestRawTableReader(const TRowCollection& rowCollection)
: RowCollection_(rowCollection)
, DataToRead_(RowCollection_.GetStreamDataStartFromRow(0))
, Input_(MakeHolder<TStringStream>(DataToRead_))
{ }
TTestRawTableReader(const TRowCollection& rowCollection, size_t breakPoint)
: RowCollection_(rowCollection)
, DataToRead_(RowCollection_.GetStreamDataStartFromRow(0).substr(0, breakPoint))
, Input_(MakeHolder<TStringStream>(DataToRead_))
, Broken_(true)
{ }
size_t DoRead(void* buf, size_t size) override
{
Y_ABORT_UNLESS(Input_);
size_t res = Input_->Read(buf, size);
if (!res && Broken_) {
ythrow yexception() << "Stream is broken";
}
return res;
}
bool Retry(
const TMaybe<ui32>& /*rangeIndex*/,
const TMaybe<ui64>& rowIndex,
const std::exception_ptr& /*error*/) override
{
if (--Retries < 0) {
return false;
}
ui64 actualRowIndex = rowIndex ? *rowIndex : 0;
DataToRead_ = RowCollection_.GetStreamDataStartFromRow(actualRowIndex);
Input_ = MakeHolder<TStringInput>(DataToRead_);
Broken_ = false;
return true;
}
void ResetRetries() override
{ }
bool HasRangeIndices() const override
{
return false;
}
private:
TRowCollection RowCollection_;
TString DataToRead_;
THolder<IInputStream> Input_;
bool Broken_ = false;
i32 Retries = 1;
};
TEST(TYamrTableReaderTest, TestReadRetry)
{
const TVector<std::tuple<TString, TString, TString>> expectedResult = {
{"foo1", "bar1", "baz1"},
{"foo2", "bar2", "baz2"},
{"foo3", "bar3", "baz3"},
};
TRowCollection rowCollection;
for (const auto& row : expectedResult) {
rowCollection.AddRow(std::get<0>(row), std::get<1>(row), std::get<2>(row));
}
ssize_t streamSize = rowCollection.ComputeTotalStreamSize();
for (ssize_t breakPoint = -1; breakPoint < streamSize; ++breakPoint) {
::TIntrusivePtr<TRawTableReader> rawReader;
if (breakPoint == -1) {
rawReader = ::MakeIntrusive<TTestRawTableReader>(rowCollection);
} else {
rawReader = ::MakeIntrusive<TTestRawTableReader>(rowCollection, static_cast<size_t>(breakPoint));
}
TYaMRTableReader tableReader(rawReader);
TVector<std::tuple<TString, TString, TString>> actualResult;
for (; tableReader.IsValid(); tableReader.Next()) {
EXPECT_TRUE(!tableReader.IsRawReaderExhausted());
auto row = tableReader.GetRow();
actualResult.emplace_back(row.Key, row.SubKey, row.Value);
}
EXPECT_TRUE(tableReader.IsRawReaderExhausted());
EXPECT_EQ(actualResult, expectedResult);
}
}
TEST(TYamrTableReaderTest, TestSkipRetry)
{
const TVector<std::tuple<TString, TString, TString>> expectedResult = {
{"foo1", "bar1", "baz1"},
{"foo2", "bar2", "baz2"},
{"foo3", "bar3", "baz3"},
};
TRowCollection rowCollection;
for (const auto& row : expectedResult) {
rowCollection.AddRow(std::get<0>(row), std::get<1>(row), std::get<2>(row));
}
ssize_t streamSize = rowCollection.ComputeTotalStreamSize();
for (ssize_t breakPoint = -1; breakPoint < streamSize; ++breakPoint) {
try {
::TIntrusivePtr<TRawTableReader> rawReader;
if (breakPoint == -1) {
rawReader = ::MakeIntrusive<TTestRawTableReader>(rowCollection);
} else {
rawReader = ::MakeIntrusive<TTestRawTableReader>(rowCollection, static_cast<size_t>(breakPoint));
}
TYaMRTableReader tableReader(rawReader);
ui32 rowCount = 0;
for (; tableReader.IsValid(); tableReader.Next()) {
EXPECT_TRUE(!tableReader.IsRawReaderExhausted());
++rowCount;
}
EXPECT_TRUE(tableReader.IsRawReaderExhausted());
EXPECT_EQ(rowCount, 3u);
} catch (const std::exception& ex) {
Cerr << breakPoint << Endl;
Cerr << ex.what() << Endl;
throw;
}
}
}
////////////////////////////////////////////////////////////////////////////////
|