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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
#include "skiff_row_table_reader.h"
#include <yt/cpp/mapreduce/interface/logging/yt_log.h>
#include <yt/cpp/mapreduce/interface/skiff_row.h>
#include <library/cpp/skiff/skiff.h>
#include <library/cpp/yt/logging/logger.h>
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
TSkiffRowTableReader::TSkiffRowTableReader(
::TIntrusivePtr<TRawTableReader> input,
const NSkiff::TSkiffSchemaPtr& schema,
TVector<ISkiffRowSkipperPtr>&& skippers,
NDetail::TCreateSkiffSchemaOptions&& options)
: Input_(std::move(input))
, BufferedInput_(&Input_)
, Parser_({schema, &BufferedInput_})
, Skippers_(std::move(skippers))
, Options_(std::move(options))
{
Next();
}
TSkiffRowTableReader::~TSkiffRowTableReader()
{ }
bool TSkiffRowTableReader::Retry()
{
if (PrepareRetry()) {
RowTaken_ = true;
Next();
return true;
}
return false;
}
bool TSkiffRowTableReader::PrepareRetry()
{
if (Input_.Retry(RangeIndex_, RowIndex_)) {
RowIndex_.Clear();
RangeIndex_.Clear();
BufferedInput_ = TBufferedInput(&Input_);
Parser_.emplace(&BufferedInput_);
return true;
}
return false;
}
void TSkiffRowTableReader::ReadRow(const ISkiffRowParserPtr& parser)
{
while (true) {
try {
parser->Parse(&Parser_.value());
RowTaken_ = true;
// We successfully parsed one more row from the stream,
// so reset retry count to their initial value.
Input_.ResetRetries();
break;
} catch (const std::exception& ex) {
YT_LOG_ERROR("Read error during parsing: %v", ex.what());
if (!Retry()) {
throw;
}
}
}
}
bool TSkiffRowTableReader::IsValid() const
{
return Valid_;
}
void TSkiffRowTableReader::SkipRow()
{
CheckValidity();
while (true) {
try {
Skippers_[TableIndex_]->SkipRow(&Parser_.value());
break;
} catch (const std::exception& ex) {
YT_LOG_ERROR("Read error during skipping row: %v", ex.what());
if (!Retry()) {
throw;
}
}
}
}
void TSkiffRowTableReader::CheckValidity() const {
if (!IsValid()) {
ythrow yexception() << "Iterator is not valid";
}
}
void TSkiffRowTableReader::Next()
{
if (!RowTaken_) {
SkipRow();
}
CheckValidity();
if (Y_UNLIKELY(Finished_ || !Parser_->HasMoreData())) {
Finished_ = true;
Valid_ = false;
return;
}
if (AfterKeySwitch_) {
AfterKeySwitch_ = false;
return;
}
if (RowIndex_) {
++*RowIndex_;
}
while (true) {
try {
auto tag = Parser_->ParseVariant16Tag();
if (tag == NSkiff::EndOfSequenceTag<ui16>()) {
IsEndOfStream_ = true;
break;
} else {
TableIndex_ = tag;
}
if (TableIndex_ >= Skippers_.size()) {
ythrow TIOException() <<
"Table index " << TableIndex_ <<
" is out of range [0, " << Skippers_.size() <<
") in read";
}
if (Options_.HasKeySwitch_) {
auto keySwitch = Parser_->ParseBoolean();
if (keySwitch) {
AfterKeySwitch_ = true;
Valid_ = false;
}
}
auto tagRowIndex = Parser_->ParseVariant8Tag();
if (tagRowIndex == 1) {
RowIndex_ = Parser_->ParseInt64();
} else {
Y_ENSURE(tagRowIndex == 0, "Tag for row_index was expected to be 0 or 1, got " << tagRowIndex);
}
if (Options_.HasRangeIndex_) {
auto tagRangeIndex = Parser_->ParseVariant8Tag();
if (tagRangeIndex == 1) {
RangeIndex_ = Parser_->ParseInt64();
} else {
Y_ENSURE(tagRangeIndex == 0, "Tag for range_index was expected to be 0 or 1, got " << tagRangeIndex);
}
}
break;
} catch (const std::exception& ex) {
YT_LOG_ERROR("Read error: %v", ex.what());
if (!PrepareRetry()) {
throw;
}
}
}
RowTaken_ = false;
}
ui32 TSkiffRowTableReader::GetTableIndex() const
{
CheckValidity();
return TableIndex_;
}
ui32 TSkiffRowTableReader::GetRangeIndex() const
{
CheckValidity();
return RangeIndex_.GetOrElse(0);
}
ui64 TSkiffRowTableReader::GetRowIndex() const
{
CheckValidity();
return RowIndex_.GetOrElse(0ULL);
}
void TSkiffRowTableReader::NextKey() {
while (Valid_) {
Next();
}
if (Finished_) {
return;
}
Valid_ = true;
if (RowIndex_) {
--*RowIndex_;
}
RowTaken_ = true;
}
TMaybe<size_t> TSkiffRowTableReader::GetReadByteCount() const {
return Input_.GetReadByteCount();
}
bool TSkiffRowTableReader::IsEndOfStream() const {
return IsEndOfStream_;
}
bool TSkiffRowTableReader::IsRawReaderExhausted() const {
return Finished_;
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|