/* -*- 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 . ---------------------------------------- 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 . ======= */ #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." #pragma once #include #include #include #include #include "buffer.hpp" #include "db_txn.hpp" #include "exceptions.hpp" #include "slice.hpp" namespace ftcxx { class DB; template bool Bounds::check(Comparator &cmp, const IterationStrategy &strategy, const Slice &key) const { int c; if (strategy.forward) { if (_right_infinite) { return true; } c = cmp(key, _right); } else { if (_left_infinite) { return true; } c = cmp(_left, key); } if (c > 0 || (c == 0 && _end_exclusive)) { return false; } return true; } template CallbackCursor::CallbackCursor(const DBEnv &env, const DBTxn &txn, Comparator &&cmp, Handler &&handler) : _dbc(env, txn), _iteration_strategy(IterationStrategy(true, true)), _bounds(DB(env.env()->get_db_for_directory(env.env())), Bounds::Infinite(), Bounds::Infinite(), false), _cmp(std::forward(cmp)), _handler(std::forward(handler)), _finished(false) { init(); } template CallbackCursor::CallbackCursor(const DB &db, const DBTxn &txn, int flags, IterationStrategy iteration_strategy, Bounds bounds, Comparator &&cmp, Handler &&handler) : _dbc(db, txn, flags), _iteration_strategy(iteration_strategy), _bounds(std::move(bounds)), _cmp(std::forward(cmp)), _handler(std::forward(handler)), _finished(false) { init(); } template void CallbackCursor::init() { if (!_dbc.set_range(_iteration_strategy, _bounds, getf_callback, this)) { _finished = true; } } template int CallbackCursor::getf(const DBT *key, const DBT *val) { if (!_bounds.check(_cmp, _iteration_strategy, Slice(*key))) { _finished = true; return -1; } if (!_handler(key, val)) { return 0; } return TOKUDB_CURSOR_CONTINUE; } template bool CallbackCursor::consume_batch() { if (!_dbc.advance(_iteration_strategy, getf_callback, this)) { _finished = true; } return !_finished; } template void CallbackCursor::seek(const Slice &key) { if (_iteration_strategy.forward) { _bounds.set_left(key); } else { _bounds.set_right(key); } if (!_dbc.set_range(_iteration_strategy, _bounds, getf_callback, this)) { _finished = true; } } template inline void BufferAppender::marshall(char *dest, const DBT *key, const DBT *val) { uint32_t *keylen = reinterpret_cast(&dest[0]); uint32_t *vallen = reinterpret_cast(&dest[sizeof *keylen]); *keylen = key->size; *vallen = val->size; char *p = &dest[(sizeof *keylen) + (sizeof *vallen)]; const char *kp = static_cast(key->data); std::copy(kp, kp + key->size, p); p += key->size; const char *vp = static_cast(val->data); std::copy(vp, vp + val->size, p); } template inline void BufferAppender::unmarshall(char *src, DBT *key, DBT *val) { const uint32_t *keylen = reinterpret_cast(&src[0]); const uint32_t *vallen = reinterpret_cast(&src[sizeof *keylen]); key->size = *keylen; val->size = *vallen; char *p = &src[(sizeof *keylen) + (sizeof *vallen)]; key->data = p; val->data = p + key->size; } template inline void BufferAppender::unmarshall(char *src, Slice &key, Slice &val) { const uint32_t *keylen = reinterpret_cast(&src[0]); const uint32_t *vallen = reinterpret_cast(&src[sizeof *keylen]); char *p = &src[(sizeof *keylen) + (sizeof *vallen)]; key = Slice(p, *keylen); val = Slice(p + *keylen, *vallen); } template inline bool BufferAppender::operator()(const DBT *key, const DBT *val) { if (_filter(Slice(*key), Slice(*val))) { size_t needed = marshalled_size(key->size, val->size); char *dest = _buf.alloc(needed); marshall(dest, key, val); } return !_buf.full(); } template BufferedCursor::BufferedCursor(const DBEnv &env, const DBTxn &txn, Comparator &&cmp, Predicate &&filter) : _buf(), _cur(env, txn, std::forward(cmp), Appender(_buf, std::forward(filter))) {} template BufferedCursor::BufferedCursor(const DB &db, const DBTxn &txn, int flags, IterationStrategy iteration_strategy, Bounds bounds, Comparator &&cmp, Predicate &&filter) : _buf(), _cur(db, txn, flags, iteration_strategy, std::move(bounds), std::forward(cmp), Appender(_buf, std::forward(filter))) {} template bool BufferedCursor::next(DBT *key, DBT *val) { if (!_buf.more() && !_cur.finished()) { _buf.clear(); _cur.consume_batch(); } if (!_buf.more()) { return false; } char *src = _buf.current(); Appender::unmarshall(src, key, val); _buf.advance(Appender::marshalled_size(key->size, val->size)); return true; } template bool BufferedCursor::next(Slice &key, Slice &val) { if (!_buf.more() && !_cur.finished()) { _buf.clear(); _cur.consume_batch(); } if (!_buf.more()) { return false; } char *src = _buf.current(); Appender::unmarshall(src, key, val); _buf.advance(Appender::marshalled_size(key.size(), val.size())); return true; } template void BufferedCursor::seek(const Slice &key) { _buf.clear(); _cur.seek(key); } template SimpleCursor::SimpleCursor(const DBEnv &env, const DBTxn &txn, Comparator &&cmp, Slice &key, Slice &val) : _copier(key, val), _cur(env, txn, std::forward(cmp), _copier) {} template SimpleCursor::SimpleCursor(const DB &db, const DBTxn &txn, int flags, IterationStrategy iteration_strategy, Bounds bounds, Comparator &&cmp, Slice &key, Slice &val) : _copier(key, val), _cur(db, txn, flags, iteration_strategy, std::move(bounds), std::forward(cmp), _copier) {} template bool SimpleCursor::next() { return _cur.consume_batch(); } template void SimpleCursor::seek(const Slice &key) { _cur.seek(key); } template CallbackCursor DB::cursor(const DBTxn &txn, DBT *left, DBT *right, Comparator &&cmp, Handler &&handler, int flags, bool forward, bool end_exclusive, bool prelock) const { IterationStrategy strategy(forward, prelock); return CallbackCursor(*this, txn, flags, strategy, Bounds(*this, Slice(*left), Slice(*right), end_exclusive), std::forward(cmp), std::forward(handler)); } template CallbackCursor DB::cursor(const DBTxn &txn, const Slice &start_key, Comparator &&cmp, Handler &&handler, int flags, bool forward, bool end_exclusive, bool prelock) const { IterationStrategy strategy(forward, prelock); Bounds bounds = forward ? Bounds(*this, start_key, Bounds::Infinite(), end_exclusive) : Bounds(*this, Bounds::Infinite(), start_key, end_exclusive); return CallbackCursor(*this, txn, flags, strategy, std::move(bounds), std::forward(cmp), std::forward(handler)); } template CallbackCursor DB::cursor(const DBTxn &txn, const Slice &left, const Slice &right, Comparator &&cmp, Handler &&handler, int flags, bool forward, bool end_exclusive, bool prelock) const { IterationStrategy strategy(forward, prelock); return CallbackCursor(*this, txn, flags, strategy, Bounds(*this, left, right, end_exclusive), std::forward(cmp), std::forward(handler)); } template CallbackCursor DB::cursor(const DBTxn &txn, Comparator &&cmp, Handler &&handler, int flags, bool forward, bool prelock) const { IterationStrategy strategy(forward, prelock); return CallbackCursor(*this, txn, flags, strategy, Bounds(*this, Bounds::Infinite(), Bounds::Infinite(), false), std::forward(cmp), std::forward(handler)); } template BufferedCursor DB::buffered_cursor(const DBTxn &txn, DBT *left, DBT *right, Comparator &&cmp, Predicate &&filter, int flags, bool forward, bool end_exclusive, bool prelock) const { IterationStrategy strategy(forward, prelock); return BufferedCursor(*this, txn, flags, strategy, Bounds(*this, Slice(*left), Slice(*right), end_exclusive), std::forward(cmp), std::forward(filter)); } template BufferedCursor DB::buffered_cursor(const DBTxn &txn, const Slice &start_key, Comparator &&cmp, Predicate &&filter, int flags, bool forward, bool end_exclusive, bool prelock) const { IterationStrategy strategy(forward, prelock); Bounds bounds = forward ? Bounds(*this, start_key, Bounds::Infinite(), end_exclusive) : Bounds(*this, Bounds::Infinite(), start_key, end_exclusive); return BufferedCursor(*this, txn, flags, strategy, std::move(bounds), std::forward(cmp), std::forward(filter)); } template BufferedCursor DB::buffered_cursor(const DBTxn &txn, const Slice &left, const Slice &right, Comparator &&cmp, Predicate &&filter, int flags, bool forward, bool end_exclusive, bool prelock) const { IterationStrategy strategy(forward, prelock); return BufferedCursor(*this, txn, flags, strategy, Bounds(*this, left, right, end_exclusive), std::forward(cmp), std::forward(filter)); } template BufferedCursor DB::buffered_cursor(const DBTxn &txn, Comparator &&cmp, Predicate &&filter, int flags, bool forward, bool prelock) const { IterationStrategy strategy(forward, prelock); return BufferedCursor(*this, txn, flags, strategy, Bounds(*this, Bounds::Infinite(), Bounds::Infinite(), false), std::forward(cmp), std::forward(filter)); } template SimpleCursor DB::simple_cursor(const DBTxn &txn, DBT *left, DBT *right, Comparator &&cmp, Slice &key, Slice &val, int flags, bool forward, bool end_exclusive, bool prelock) const { IterationStrategy strategy(forward, prelock); return SimpleCursor(*this, txn, flags, strategy, Bounds(*this, Slice(*left), Slice(*right), end_exclusive), std::forward(cmp), key, val); } template SimpleCursor DB::simple_cursor(const DBTxn &txn, const Slice &start_key, Comparator &&cmp, Slice &key, Slice &val, int flags, bool forward, bool end_exclusive, bool prelock) const { IterationStrategy strategy(forward, prelock); Bounds bounds = forward ? Bounds(*this, start_key, Bounds::Infinite(), end_exclusive) : Bounds(*this, Bounds::Infinite(), start_key, end_exclusive); return SimpleCursor(*this, txn, flags, strategy, std::move(bounds), std::forward(cmp), key, val); } template SimpleCursor DB::simple_cursor(const DBTxn &txn, const Slice &left, const Slice &right, Comparator &&cmp, Slice &key, Slice &val, int flags, bool forward, bool end_exclusive, bool prelock) const { IterationStrategy strategy(forward, prelock); return SimpleCursor(*this, txn, flags, strategy, Bounds(*this, left, right, end_exclusive), std::forward(cmp), key, val); } template SimpleCursor DB::simple_cursor(const DBTxn &txn, Comparator &&cmp, Slice &key, Slice &val, int flags, bool forward, bool prelock) const { IterationStrategy strategy(forward, prelock); return SimpleCursor(*this, txn, flags, strategy, Bounds(*this, Bounds::Infinite(), Bounds::Infinite(), false), std::forward(cmp), key, val); } template CallbackCursor DBEnv::cursor(const DBTxn &txn, Comparator &&cmp, Handler &&handler) const { return CallbackCursor(*this, txn, std::forward(cmp), std::forward(handler)); } template BufferedCursor DBEnv::buffered_cursor(const DBTxn &txn, Comparator &&cmp, Predicate &&filter) const { return BufferedCursor(*this, txn, std::forward(cmp), std::forward(filter)); } template SimpleCursor DBEnv::simple_cursor(const DBTxn &txn, Comparator &&cmp, Slice &key, Slice &val) const { return SimpleCursor(*this, txn, std::forward(cmp), key, val); } } // namespace ftcxx