aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/io/yamr_table_reader.cpp
blob: 55f270a6b66027da227eb5b2134a04fb8f1578bf (plain) (blame)
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
#include "yamr_table_reader.h"

#include <yt/cpp/mapreduce/common/helpers.h>
#include <yt/cpp/mapreduce/common/retry_lib.h>
#include <yt/cpp/mapreduce/raw_client/raw_requests.h>

////////////////////////////////////////////////////////////////////

static void CheckedSkip(IInputStream* input, size_t byteCount)
{
    size_t skipped = input->Skip(byteCount);
    Y_ENSURE(skipped == byteCount, "Premature end of YaMR stream");
}

////////////////////////////////////////////////////////////////////

namespace NYT {

using namespace NYT::NDetail::NRawClient;

////////////////////////////////////////////////////////////////////////////////

TYaMRTableReader::TYaMRTableReader(::TIntrusivePtr<TRawTableReader> input)
    : TLenvalTableReader(std::move(input))
{ }

TYaMRTableReader::~TYaMRTableReader()
{ }

const TYaMRRow& TYaMRTableReader::GetRow() const
{
    CheckValidity();
    if (!RowTaken_) {
        const_cast<TYaMRTableReader*>(this)->ReadRow();
    }
    return Row_;
}

bool TYaMRTableReader::IsValid() const
{
    return Valid_;
}

void TYaMRTableReader::Next()
{
    TLenvalTableReader::Next();
}

void TYaMRTableReader::NextKey()
{
    TLenvalTableReader::NextKey();
}

ui32 TYaMRTableReader::GetTableIndex() const
{
    return TLenvalTableReader::GetTableIndex();
}

ui32 TYaMRTableReader::GetRangeIndex() const
{
    return TLenvalTableReader::GetRangeIndex();
}

ui64 TYaMRTableReader::GetRowIndex() const
{
    return TLenvalTableReader::GetRowIndex();
}

TMaybe<size_t> TYaMRTableReader::GetReadByteCount() const
{
    return TLenvalTableReader::GetReadByteCount();
}

bool TYaMRTableReader::IsEndOfStream() const
{
    return TLenvalTableReader::IsEndOfStream();
}

bool TYaMRTableReader::IsRawReaderExhausted() const
{
    return TLenvalTableReader::IsRawReaderExhausted();
}

void TYaMRTableReader::ReadField(TString* result, i32 length)
{
    result->resize(length);
    size_t count = Input_.Load(result->begin(), length);
    Y_ENSURE(count == static_cast<size_t>(length), "Premature end of YaMR stream");
}

void TYaMRTableReader::ReadRow()
{
    while (true) {
        try {
            i32 value = static_cast<i32>(Length_);
            ReadField(&Key_, value);
            Row_.Key = Key_;

            ReadInteger(&value);
            ReadField(&SubKey_, value);
            Row_.SubKey = SubKey_;

            ReadInteger(&value);
            ReadField(&Value_, value);
            Row_.Value = 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) {
            if (!TLenvalTableReader::Retry(std::make_exception_ptr(ex))) {
                throw;
            }
        }
    }
}

void TYaMRTableReader::SkipRow()
{
    while (true) {
        try {
            i32 value = static_cast<i32>(Length_);
            CheckedSkip(&Input_, value);

            ReadInteger(&value);
            CheckedSkip(&Input_, value);

            ReadInteger(&value);
            CheckedSkip(&Input_, value);
            break;
        } catch (const std::exception& ex) {
            if (!TLenvalTableReader::Retry(std::make_exception_ptr(ex))) {
                throw;
            }
        }
    }
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT