summaryrefslogtreecommitdiff
path: root/storage/tokudb/PerconaFT/ft/serialize/rbtree_mhs.h
blob: eb8c953b08c138cda7e8417b8d8a67550c0d27a0 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
#ident "$Id$"
/*======
This file is part of PerconaFT.


Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.

    PerconaFT is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2,
    as published by the Free Software Foundation.

    PerconaFT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.

----------------------------------------

    PerconaFT is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License, version 3,
    as published by the Free Software Foundation.

    PerconaFT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
======= */

#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."

#pragma once

#include <db.h>

#include "portability/toku_pthread.h"
#include "portability/toku_stdint.h"
#include "portability/toku_stdlib.h"

// RBTree(Red-black tree) with max hole sizes for subtrees.

// This is a tentative data struct to improve the block allocation time
// complexity from the linear time to the log time. Please be noted this DS only
// supports first-fit for now. It is actually easier to do it with
// best-fit.(just
// sort by size).

// RBTree is a classic data struct with O(log(n)) for insertion, deletion and
// search. Many years have seen its efficiency.

// a *hole* is the representation of an available BlockPair for allocation.
// defined as (start_address,size) or (offset, size) interchangably.

// each node has a *label* to indicate a pair of the max hole sizes for its
// subtree.

// We are implementing a RBTree with max hole sizes for subtree. It is a red
// black tree that is sorted by the start_address but also labeld with the max
// hole sizes of the subtrees.

//        [(6,3)]  -> [(offset, size)], the hole
//        [{2,5}]  -> [{mhs_of_left, mhs_of_right}], the label
/*        /     \           */
// [(0, 1)]    [(10,  5)]
// [{0, 2}]    [{0,   0}]
/*        \                 */
//       [(3,  2)]
//       [{0,  0}]
// request of allocation size=2 goes from root to [(3,2)].

// above example shows a simplified RBTree_max_holes.
// it is easier to tell the search time is O(log(n)) as we can make a decision
// on each descent until we get to the target.

// the only question is if we can keep the maintenance cost low -- and i think
// it is not a problem becoz an insertion/deletion is only going to update the
// max_hole_sizes of the nodes along the path from the root to the node to be
// deleted/inserted. The path can be cached and search is anyway O(log(n)).

// unlike the typical rbtree, Tree has to handle the inserts and deletes
// with more care: an allocation that triggers the delete might leave some
// unused space which we can simply update the start_addr and size without
// worrying overlapping. An free might not only mean the insertion but also
// *merging* with the adjacent holes.

namespace MhsRbTree {

#define offset_t uint64_t
    enum class EColor { RED, BLACK };
    enum class EDirection { NONE = 0, LEFT, RIGHT };

    // I am a bit tired of fixing overflow/underflow, just quickly craft some
    // int
    // class that has an infinity-like max value and prevents overflow and
    // underflow. If you got a file offset larger than MHS_MAX_VAL, it is not
    // a problem here. :-/  - JYM
    class OUUInt64 {
       public:
        static const uint64_t MHS_MAX_VAL = 0xffffffffffffffff;
        OUUInt64() : _value(0) {}
        OUUInt64(uint64_t s) : _value(s) {}
        OUUInt64(const OUUInt64& o) : _value(o._value) {}
        bool operator<(const OUUInt64 &r) const {
            invariant(!(_value == MHS_MAX_VAL && r.ToInt() == MHS_MAX_VAL));
            return _value < r.ToInt();
        }
        bool operator>(const OUUInt64 &r) const {
            invariant(!(_value == MHS_MAX_VAL && r.ToInt() == MHS_MAX_VAL));
            return _value > r.ToInt();
        }
        bool operator<=(const OUUInt64 &r) const {
            invariant(!(_value == MHS_MAX_VAL && r.ToInt() == MHS_MAX_VAL));
            return _value <= r.ToInt();
        }
        bool operator>=(const OUUInt64 &r) const {
            invariant(!(_value == MHS_MAX_VAL && r.ToInt() == MHS_MAX_VAL));
            return _value >= r.ToInt();
        }
        OUUInt64 operator+(const OUUInt64 &r) const {
            if (_value == MHS_MAX_VAL || r.ToInt() == MHS_MAX_VAL) {
                OUUInt64 tmp(MHS_MAX_VAL);
                return tmp;
            } else {
                // detecting overflow
                invariant((MHS_MAX_VAL - _value) >= r.ToInt());
                uint64_t plus = _value + r.ToInt();
                OUUInt64 tmp(plus);
                return tmp;
            }
        }
        OUUInt64 operator-(const OUUInt64 &r) const {
            invariant(r.ToInt() != MHS_MAX_VAL);
            if (_value == MHS_MAX_VAL) {
                return *this;
            } else {
                invariant(_value >= r.ToInt());
                uint64_t minus = _value - r.ToInt();
                OUUInt64 tmp(minus);
                return tmp;
            }
        }
        OUUInt64 operator-=(const OUUInt64 &r) {
            if (_value != MHS_MAX_VAL) {
                invariant(r.ToInt() != MHS_MAX_VAL);
                invariant(_value >= r.ToInt());
                _value -= r.ToInt();
            }
            return *this;
        }
        OUUInt64 operator+=(const OUUInt64 &r) {
            if (_value != MHS_MAX_VAL) {
                if (r.ToInt() == MHS_MAX_VAL) {
                    _value = MHS_MAX_VAL;
                } else {
                    invariant((MHS_MAX_VAL - _value) >= r.ToInt());
                    this->_value += r.ToInt();
                }
            }
            return *this;
        }
        bool operator==(const OUUInt64 &r) const {
            return _value == r.ToInt();
        }
        bool operator!=(const OUUInt64 &r) const {
            return _value != r.ToInt();
        }
        OUUInt64 operator=(const OUUInt64 &r) {
            _value = r.ToInt();
            return *this;
        }
        uint64_t ToInt() const { return _value; }

       private:
        uint64_t _value;
    };

    class Node {
       public:
        class BlockPair {
           public:
            OUUInt64 _offset;
            OUUInt64 _size;

            BlockPair() : _offset(0), _size(0) {}
            BlockPair(uint64_t o, uint64_t s) : _offset(o), _size(s) {}
            BlockPair(OUUInt64 o, OUUInt64 s) : _offset(o), _size(s) {}
            BlockPair(const BlockPair &o)
                : _offset(o._offset), _size(o._size) {}

            int operator<(const BlockPair &rhs) const {
                return _offset < rhs._offset;
            }
            int operator<(const uint64_t &o) const { return _offset < o; }
        };

        struct Pair {
            uint64_t _left;
            uint64_t _right;
            Pair(uint64_t l, uint64_t r) : _left(l), _right(r) {}
        };

        EColor _color;
        BlockPair _hole;
        Pair _label;
        Node *_left;
        Node *_right;
        Node *_parent;

        Node(EColor c,
             Node::BlockPair h,
             Pair lb,
             Node *l,
             Node *r,
             Node *p)
            : _color(c),
              _hole(h),
              _label(lb),
              _left(l),
              _right(r),
              _parent(p) {}
    };

    class Tree {
       private:
        Node *_root;
        uint64_t _align;

       public:
        Tree();
        Tree(uint64_t);
        ~Tree();

        void PreOrder();
        void InOrder();
        void PostOrder();
        // immutable operations
        Node *SearchByOffset(uint64_t addr);
        Node *SearchFirstFitBySize(uint64_t size);

        Node *MinNode();
        Node *MaxNode();

        Node *Successor(Node *);
        Node *Predecessor(Node *);

        // mapped from tree_allocator::free_block
        int Insert(Node::BlockPair pair);
        // mapped from tree_allocator::alloc_block
        uint64_t Remove(size_t size);
        // mapped from tree_allocator::alloc_block_after

        void RawRemove(uint64_t offset);
        void Destroy();
        // print the tree
        void Dump();
        // validation
        // balance
        void ValidateBalance();
        void ValidateInOrder(Node::BlockPair *);
        void InOrderVisitor(void (*f)(void *, Node *, uint64_t), void *);
        void ValidateMhs();

       private:
        void PreOrder(Node *node) const;
        void InOrder(Node *node) const;
        void PostOrder(Node *node) const;
        Node *SearchByOffset(Node *node, offset_t addr) const;
        Node *SearchFirstFitBySize(Node *node, size_t size) const;

        Node *MinNode(Node *node);
        Node *MaxNode(Node *node);

        // rotations to fix up. we will have to update the labels too.
        void LeftRotate(Node *&root, Node *x);
        void RightRotate(Node *&root, Node *y);

        int Insert(Node *&root, Node::BlockPair pair);
        int InsertFixup(Node *&root, Node *node);

        void RawRemove(Node *&root, Node *node);
        uint64_t Remove(Node *&root, Node *node, size_t size);
        void RawRemoveFixup(Node *&root, Node *node, Node *parent);

        void Destroy(Node *&tree);
        void Dump(Node *tree, Node::BlockPair pair, EDirection dir);
        void RecalculateMhs(Node *node);
        void IsNewNodeMergable(Node *, Node *, Node::BlockPair, bool *, bool *);
        void AbsorbNewNode(Node *, Node *, Node::BlockPair, bool, bool, bool);
        Node *SearchFirstFitBySizeHelper(Node *x, uint64_t size);

        Node *SuccessorHelper(Node *y, Node *x);

        Node *PredecessorHelper(Node *y, Node *x);

        void InOrderVisitor(Node *,
                            void (*f)(void *, Node *, uint64_t),
                            void *,
                            uint64_t);
        uint64_t ValidateMhs(Node *);

        uint64_t EffectiveSize(Node *);
// mixed with some macros.....
#define rbn_parent(r) ((r)->_parent)
#define rbn_color(r) ((r)->_color)
#define rbn_is_red(r) ((r)->_color == EColor::RED)
#define rbn_is_black(r) ((r)->_color == EColor::BLACK)
#define rbn_set_black(r)     \
    do {                     \
        (r)->_color = EColor::BLACK; \
    } while (0)
#define rbn_set_red(r)     \
    do {                   \
        (r)->_color = EColor::RED; \
    } while (0)
#define rbn_set_parent(r, p) \
    do {                     \
        (r)->_parent = (p);  \
    } while (0)
#define rbn_set_color(r, c) \
    do {                    \
        (r)->_color = (c);  \
    } while (0)
#define rbn_set_offset(r)         \
    do {                          \
        (r)->_hole._offset = (c); \
    } while (0)
#define rbn_set_size(r, c)      \
    do {                        \
        (r)->_hole._size = (c); \
    } while (0)
#define rbn_set_left_mhs(r, c)   \
    do {                         \
        (r)->_label._left = (c); \
    } while (0)
#define rbn_set_right_mhs(r, c)   \
    do {                          \
        (r)->_label._right = (c); \
    } while (0)
#define rbn_size(r) ((r)->_hole._size)
#define rbn_offset(r) ((r)->_hole._offset)
#define rbn_key(r) ((r)->_hole._offset)
#define rbn_left_mhs(r) ((r)->_label._left)
#define rbn_right_mhs(r) ((r)->_label._right)
#define mhs_of_subtree(y) \
    (std::max(std::max(rbn_left_mhs(y), rbn_right_mhs(y)), EffectiveSize(y)))
    };

}  // namespace MhsRbTree