aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/tablet_flat/flat_bloom_writer.h
blob: 9335b7313c82464603dd49c887d22dd8f9e4e72e (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
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
#pragma once

#include "flat_page_bloom.h"
#include "flat_bloom_hash.h"
#include "flat_util_binary.h"
#include "util_deref.h"

#include <library/cpp/actors/util/shared_data.h>

#include <util/generic/ymath.h>
#include <util/system/sanitizers.h>

namespace NKikimr {
namespace NTable {
namespace NBloom {

    class IWriter {
    public:
        virtual ~IWriter() = default;

        virtual void Reset() = 0;
        virtual ui64 EstimateBytesUsed(size_t extraItems) const = 0;
        virtual void Add(TArrayRef<const TCell> row) = 0;
        virtual TSharedData Make() = 0;
    };

    class TEstimator {
    public:
        TEstimator(float error)
        {
            Y_ABORT_UNLESS(error > 0. && error < 1.,
                "Invalid error estimation, should be in (0, 1)");

            double log2err = Log2(error);

            Amp = -1.44 * log2err;
            Y_ABORT_UNLESS(Amp < 256., "Too high rows amplification factor");

            HashCount = Min(ui64(Max<ui16>()), ui64(ceil(-log2err)));
        }

        ui64 Bits(ui64 rows) const noexcept
        {
            Y_ABORT_UNLESS(!(rows >> 54),
                "Too many rows, probably an invalid value passed");

            return ((Max(ui64(ceil(Amp * rows)), ui64(1)) + 63) >> 6) << 6;
        }

        ui16 Hashes() const noexcept
        {
            return HashCount;
        }

    private:
        double Amp;
        ui16 HashCount;
    };

    class TWriter final : public IWriter {
    public:
        TWriter(ui64 rows, float error)
        {
            TEstimator estimator(error);
            Hashes = estimator.Hashes();
            Items = estimator.Bits(rows);
            Y_ABORT_UNLESS(Hashes && Items);

            Reset();
        }

        void Reset() override
        {
            using THeader = NPage::TBloom::THeader;

            ui64 array = (Items >> 6) * sizeof(ui64);

            auto size = sizeof(NPage::TLabel) + sizeof(THeader) + array;

            Raw = TSharedData::Uninitialized(size);

            NUtil::NBin::TPut out(Raw.mutable_begin());

            WriteUnaligned<NPage::TLabel>(out.Skip<NPage::TLabel>(),
                NPage::TLabel::Encode(NPage::EPage::Bloom, 0, size));

            if (auto *post = out.Skip<THeader>()) {
                Zero(*post);

                post->Type = 0;
                post->Hashes = Hashes;
                post->Items = Items;
            }

            Array = { TDeref<ui64>::At(*out, 0), size_t(Items >> 6) };

            Y_ABORT_UNLESS(size_t(*out) % sizeof(ui64) == 0, "Invalid aligment");
            Y_ABORT_UNLESS(TDeref<char>::At(Array.end(), 0) == Raw.mutable_end());

            std::fill(Array.begin(), Array.end(), 0);
        }

        ui64 EstimateBytesUsed(size_t) const override
        {
            return Raw.size();
        }

        void Add(THashRoot root)
        {
            THash hash(root);

            for (ui32 seq = 0; seq++ < Hashes; ) {
                const ui64 num = hash.Next() % Items;

                Array[num >> 6] |= ui64(1) << (num & 0x3f);
            }
        }

        void Add(TArrayRef<const TCell> row) override
        {
            const TPrefix prefix(row);
            Add(THash::Root(prefix.Get(row.size())));
        }

        TSharedData Make() override
        {
            NSan::CheckMemIsInitialized(Raw.data(), Raw.size());

            return std::move(Raw);
        }

    private:
        ui32 Hashes = 0;
        ui64 Items = 0;
        TSharedData Raw;
        TArrayRef<ui64> Array;
    };

    class TQueue final : public IWriter {
    public:
        TQueue(float error)
            : Estimator(error)
            , Error(error)
        {
        }

        void Reset() override
        {
            Roots.clear();
        }

        ui64 EstimateBytesUsed(size_t extraItems) const override
        {
            using THeader = NPage::TBloom::THeader;

            ui64 array = (Estimator.Bits(Roots.size() + extraItems) >> 6) * sizeof(ui64);

            return sizeof(NPage::TLabel) + sizeof(THeader) + array;
        }

        void Add(TArrayRef<const TCell> row) override
        {
            const TPrefix prefix(row);
            Roots.push_back(THash::Root(prefix.Get(row.size())));
        }

        TSharedData Make() override
        {
            if (!Roots) {
                return { };
            }

            TWriter writer(Roots.size(), Error);
            for (const auto& root : Roots) {
                writer.Add(root);
            }
            return writer.Make();
        }

    private:
        TVector<THashRoot> Roots;
        TEstimator Estimator;
        float Error;
    };

}
}
}