summaryrefslogtreecommitdiffstats
path: root/storage/tokudb/PerconaFT/ftcxx/cursor.hpp
blob: bde5dbf2c192e5555719989f9a468a2f0665d75d (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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#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 <utility>

#include <db.h>

#include "buffer.hpp"
#include "db.hpp"
#include "db_env.hpp"
#include "db_txn.hpp"
#include "slice.hpp"

namespace ftcxx {

    class DB;

    struct IterationStrategy {
        bool forward;
        bool prelock;

        IterationStrategy(bool forward_, bool prelock_)
            : forward(forward_),
              prelock(prelock_)
        {}

        int getf_flags() const {
            if (prelock) {
                return DB_PRELOCKED | DB_PRELOCKED_WRITE;
            } else {
                return DBC_DISABLE_PREFETCHING;
            }
        }
    };

    class Bounds {
        const ::DB *_db;
        Slice _left;
        Slice _right;
        DBT _left_dbt;
        DBT _right_dbt;
        bool _left_infinite;
        bool _right_infinite;
        bool _end_exclusive;

    public:
        Bounds(const DB &db, const Slice &left, const Slice &right, bool end_exclusive)
            : _db(db.db()),
              _left(left.owned()),
              _right(right.owned()),
              _left_dbt(_left.dbt()),
              _right_dbt(_right.dbt()),
              _left_infinite(false),
              _right_infinite(false),
              _end_exclusive(end_exclusive)
        {}

        struct Infinite {};

        Bounds(const DB &db, Infinite, const Slice &right, bool end_exclusive)
            : _db(db.db()),
              _left(),
              _right(right.owned()),
              _left_dbt(_left.dbt()),
              _right_dbt(_right.dbt()),
              _left_infinite(true),
              _right_infinite(false),
              _end_exclusive(end_exclusive)
        {}

        Bounds(const DB &db, const Slice &left, Infinite, bool end_exclusive)
            : _db(db.db()),
              _left(left.owned()),
              _right(),
              _left_dbt(_left.dbt()),
              _right_dbt(_right.dbt()),
              _left_infinite(false),
              _right_infinite(true),
              _end_exclusive(end_exclusive)
        {}

        Bounds(const DB &db, Infinite, Infinite, bool end_exclusive)
            : _db(db.db()),
              _left(),
              _right(),
              _left_dbt(_left.dbt()),
              _right_dbt(_right.dbt()),
              _left_infinite(true),
              _right_infinite(true),
              _end_exclusive(end_exclusive)
        {}

        Bounds(const Bounds &other) = delete;
        Bounds& operator=(const Bounds &) = delete;

        Bounds(Bounds &&o)
            : _db(nullptr),
              _left(),
              _right(),
              _left_infinite(o._left_infinite),
              _right_infinite(o._right_infinite),
              _end_exclusive(o._end_exclusive)
        {
            std::swap(_db, o._db);
            std::swap(_left, o._left);
            std::swap(_right, o._right);
            _left_dbt = _left.dbt();
            _right_dbt = _right.dbt();
        }

        Bounds& operator=(Bounds&& other) {
            std::swap(_db, other._db);
            std::swap(_left, other._left);
            std::swap(_right, other._right);
            _left_dbt = _left.dbt();
            _right_dbt = _right.dbt();
            _left_infinite = other._left_infinite;
            _right_infinite = other._right_infinite;
            _end_exclusive = other._end_exclusive;
            return *this;
        }

        const DBT *left_dbt() const {
            if (_left_infinite) {
                return _db->dbt_neg_infty();
            } else {
                return &_left_dbt;
            }
        }

        const DBT *right_dbt() const {
            if (_right_infinite) {
                return _db->dbt_pos_infty();
            } else {
                return &_right_dbt;
            }
        }

        void set_left(const Slice &left) {
            _left = left.owned();
            _left_dbt = _left.dbt();
            _left_infinite = false;
        }

        void set_right(const Slice &right) {
            _right = right.owned();
            _right_dbt = _right.dbt();
            _right_infinite = false;
        }

        bool left_infinite() const { return _left_infinite; }
        bool right_infinite() const { return _right_infinite; }

        template<class Comparator>
        bool check(Comparator &cmp, const IterationStrategy &strategy, const Slice &key) const;
    };

    /**
     * DBC is a simple RAII wrapper around a DBC object.
     */
    class DBC {
    public:
        DBC(const DB &db, const DBTxn &txn=DBTxn(), int flags=0);
        ~DBC();

        // Directory cursor.
        DBC(const DBEnv &env, const DBTxn &txn=DBTxn());

        DBC(const DBC &) = delete;
        DBC& operator=(const DBC &) = delete;

        DBC(DBC &&o)
            : _txn(),
              _dbc(nullptr)
        {
            std::swap(_txn, o._txn);
            std::swap(_dbc, o._dbc);
        }

        DBC& operator=(DBC &&o) {
            std::swap(_txn, o._txn);
            std::swap(_dbc, o._dbc);
            return *this;
        }

        ::DBC *dbc() const { return _dbc; }

        void set_txn(const DBTxn &txn) const {
            _dbc->c_set_txn(_dbc, txn.txn());
        }

        void close();

        bool set_range(const IterationStrategy &strategy, const Bounds &bounds, YDB_CALLBACK_FUNCTION callback, void *extra) const;

        bool advance(const IterationStrategy &strategy, YDB_CALLBACK_FUNCTION callback, void *extra) const;

    protected:

        // the ordering here matters, for destructors
        DBTxn _txn;
        ::DBC *_dbc;
    };

    /**
     * Cursor supports iterating a cursor over a key range,
     * with bulk fetch buffering, and optional filtering.
     */
    template<class Comparator, class Handler>
    class CallbackCursor {
    public:

        /**
         * Directory cursor.
         */
        CallbackCursor(const DBEnv &env, const DBTxn &txn,
                       Comparator &&cmp, Handler &&handler);

        /**
         * Constructs an cursor.  Better to use DB::cursor instead to
         * avoid template parameters.
         */
        CallbackCursor(const DB &db, const DBTxn &txn, int flags,
                       IterationStrategy iteration_strategy,
                       Bounds bounds,
                       Comparator &&cmp, Handler &&handler);

        /**
         * Gets the next key/val pair in the iteration.  Returns true
         * if there is more data, and fills in key and val.  If the
         * range is exhausted, returns false.
         */
        bool consume_batch();

        void seek(const Slice &key);

        bool finished() const { return _finished; }

        bool ok() const { return !finished(); }

        void set_txn(const DBTxn &txn) const { _dbc.set_txn(txn); }

    private:

        DBC _dbc;
        IterationStrategy _iteration_strategy;
        Bounds _bounds;
        Comparator _cmp;
        Handler _handler;

        bool _finished;

        void init();

        static int getf_callback(const DBT *key, const DBT *val, void *extra) {
            CallbackCursor *i = static_cast<CallbackCursor *>(extra);
            return i->getf(key, val);
        }

        int getf(const DBT *key, const DBT *val);
    };

    template<class Predicate>
    class BufferAppender {
        Buffer &_buf;
        Predicate _filter;

    public:
        BufferAppender(Buffer &buf, Predicate &&filter)
            : _buf(buf),
              _filter(std::forward<Predicate>(filter))
        {}

        bool operator()(const DBT *key, const DBT *val);

        static size_t marshalled_size(size_t keylen, size_t vallen) {
            return (sizeof(((DBT *)0)->size)) + (sizeof(((DBT *)0)->size)) + keylen + vallen;
        }

        static void marshall(char *dest, const DBT *key, const DBT *val);

        static void unmarshall(char *src, DBT *key, DBT *val);
        static void unmarshall(char *src, Slice &key, Slice &val);
    };

    template<class Comparator, class Predicate>
    class BufferedCursor {
    public:

        /**
         * Directory cursor.
         */
        BufferedCursor(const DBEnv &env, const DBTxn &txn,
                       Comparator &&cmp, Predicate &&filter);

        /**
         * Constructs an buffered cursor.  Better to use
         * DB::buffered_cursor instead to avoid template parameters.
         */
        BufferedCursor(const DB &db, const DBTxn &txn, int flags,
                       IterationStrategy iteration_strategy,
                       Bounds bounds,
                       Comparator &&cmp, Predicate &&filter);

        /**
         * Gets the next key/val pair in the iteration.  Returns true
         * if there is more data, and fills in key and val.  If the
         * range is exhausted, returns false.
         */
        bool next(DBT *key, DBT *val);
        bool next(Slice &key, Slice &val);

        void seek(const Slice &key);

        bool ok() const {
            return _cur.ok() || _buf.more();
        }

        void set_txn(const DBTxn &txn) const { _cur.set_txn(txn); }

    private:

        typedef BufferAppender<Predicate> Appender;

        Buffer _buf;
        CallbackCursor<Comparator, Appender> _cur;
    };

    template<class Comparator>
    class SimpleCursor {
    public:
        SimpleCursor(const DBEnv &env, const DBTxn &txn,
                     Comparator &&cmp, Slice &key, Slice &val);

        SimpleCursor(const DB &db, const DBTxn &txn, int flags,
                     IterationStrategy iteration_strategy,
                     Bounds bounds, Comparator &&cmp,
                     Slice &key, Slice &val);

        /**
         * Gets the next key/val pair in the iteration.  Copies data
         * directly into key and val, which will own their buffers.
         */
        bool next();

        void seek(const Slice &key);

        bool ok() const {
            return _cur.ok();
        }

        void set_txn(const DBTxn &txn) const { _cur.set_txn(txn); }

        class SliceCopier {
            Slice &_key;
            Slice &_val;

        public:
            SliceCopier(Slice &key, Slice &val)
                : _key(key),
                  _val(val)
            {}

            bool operator()(const DBT *key, const DBT *val) {
                _key = Slice(*key).owned();
                _val = Slice(*val).owned();

                // Don't bulk fetch.
                return false;
            }
        };

    private:

        SliceCopier _copier;
        CallbackCursor<Comparator, SliceCopier&> _cur;
    };

} // namespace ftcxx

#include "cursor-inl.hpp"