aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/on_disk/chunks/writer.cpp
blob: 8344045e14dd78e51368bd5a81214a17dafbb5db (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
#include <util/ysaveload.h>

#include "writer.h"

static inline void WriteAux(IOutputStream* out, const TVector<ui64>& data) { 
    ::SavePodArray(out, data.data(), data.size());
}

/*************************** TBuffersWriter ***************************/

TChunkedDataWriter::TChunkedDataWriter(IOutputStream& slave)
    : Slave(slave)
    , Offset(0)
{
}

TChunkedDataWriter::~TChunkedDataWriter() { 
}

void TChunkedDataWriter::NewBlock() {
    if (Offsets.size()) {
        Lengths.push_back(Offset - Offsets.back());
    }

    Pad(16);
    Offsets.push_back(Offset);
}

void TChunkedDataWriter::WriteFooter() {
    Lengths.push_back(Offset - Offsets.back());
    WriteAux(this, Lengths);
    WriteAux(this, Offsets);
    WriteBinary<ui64>(Offsets.size());
    WriteBinary<ui64>(Version);
    WriteBinary<ui64>(0);
}

size_t TChunkedDataWriter::GetCurrentBlockOffset() const {
    Y_ASSERT(!Offsets.empty());
    Y_ASSERT(Offset >= Offsets.back());
    return Offset - Offsets.back();
}

size_t TChunkedDataWriter::GetBlockCount() const {
    return Offsets.size();
}