aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/io/node_table_writer.cpp
blob: 916dec7ae4e341069886df1a597b6ef568581dba (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
#include "node_table_writer.h"

#include <yt/cpp/mapreduce/common/node_visitor.h>

#include <yt/cpp/mapreduce/interface/io.h>

#include <yt/cpp/mapreduce/interface/logging/yt_log.h>

#include <library/cpp/yson/writer.h>

namespace NYT {

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

TNodeTableWriter::TNodeTableWriter(THolder<IProxyOutput> output, NYson::EYsonFormat format)
    : Output_(std::move(output))
{
    for (size_t i = 0; i < Output_->GetStreamCount(); ++i) {
        Writers_.push_back(
            MakeHolder<::NYson::TYsonWriter>(Output_->GetStream(i), format, NYT::NYson::EYsonType::ListFragment));
    }
}

TNodeTableWriter::~TNodeTableWriter()
{ }

size_t TNodeTableWriter::GetBufferMemoryUsage() const
{
    return Output_->GetBufferMemoryUsage();
}

size_t TNodeTableWriter::GetTableCount() const
{
    return Output_->GetStreamCount();
}

void TNodeTableWriter::FinishTable(size_t tableIndex) {
    Output_->GetStream(tableIndex)->Finish();
}

void TNodeTableWriter::AddRow(const TNode& row, size_t tableIndex)
{
    if (row.HasAttributes()) {
        ythrow TIOException() << "Row cannot have attributes";
    }

    static const TNode emptyMap = TNode::CreateMap();
    const TNode* outRow = &emptyMap;
    if (row.GetType() != TNode::Undefined) {
        if (!row.IsMap()) {
            ythrow TIOException() << "Row should be a map node";
        } else {
            outRow = &row;
        }
    }

    auto* writer = Writers_[tableIndex].Get();
    writer->OnListItem();

    TNodeVisitor visitor(writer);
    visitor.Visit(*outRow);

    Output_->OnRowFinished(tableIndex);
}

void TNodeTableWriter::AddRow(TNode&& row, size_t tableIndex) {
    AddRow(row, tableIndex);
}

void TNodeTableWriter::Abort()
{
    Output_->Abort();
}

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

} // namespace NYT