aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/on_disk/chunks/reader.h
blob: c5fe783319fddd57f8534cbc410cafe4f0c5708a (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
#pragma once

#include <util/generic/array_ref.h>
#include <util/generic/vector.h>
#include <util/generic/yexception.h>

class TBlob;

class TChunkedDataReader {
public:
    TChunkedDataReader(const TBlob& blob);

    inline const void* GetBlock(size_t index) const {
        CheckIndex(index);
        return Offsets[index];
    }

    inline size_t GetBlockLen(size_t index) const {
        CheckIndex(index);

        if (Version == 0) {
            if (index + 1 < Offsets.size()) {
                return Offsets[index + 1] - Offsets[index];
            }

            return Size - (Offsets.back() - Offsets.front());
        }

        return Lengths[index];
    }

    TBlob GetBlob(size_t index) const;

    template <typename T>
    TArrayRef<const T> GetRegion(size_t index) const {
        size_t len = GetBlockLen(index);
        Y_ENSURE(len % sizeof(T) == 0, "wrong data padding");
        return TArrayRef<const T>(reinterpret_cast<const T*>(GetBlock(index)), len / sizeof(T));
    }

    inline size_t GetBlocksCount() const {
        return Offsets.size();
    }

private:
    inline void CheckIndex(size_t index) const {
        if (index >= GetBlocksCount()) {
            ythrow yexception() << "requested block " << index << " of " << GetBlocksCount() << " blocks";
        }
    }

private:
    ui64 Version = 0;
    TVector<const char*> Offsets;
    TVector<size_t> Lengths;
    size_t Size = 0;
};