| 1 | /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 | #ident "$Id$" |
| 3 | /*====== |
| 4 | This file is part of PerconaFT. |
| 5 | |
| 6 | |
| 7 | Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved. |
| 8 | |
| 9 | PerconaFT is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License, version 2, |
| 11 | as published by the Free Software Foundation. |
| 12 | |
| 13 | PerconaFT is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | ---------------------------------------- |
| 22 | |
| 23 | PerconaFT is free software: you can redistribute it and/or modify |
| 24 | it under the terms of the GNU Affero General Public License, version 3, |
| 25 | as published by the Free Software Foundation. |
| 26 | |
| 27 | PerconaFT is distributed in the hope that it will be useful, |
| 28 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 29 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 30 | GNU Affero General Public License for more details. |
| 31 | |
| 32 | You should have received a copy of the GNU Affero General Public License |
| 33 | along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. |
| 34 | ======= */ |
| 35 | |
| 36 | #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." |
| 37 | |
| 38 | #pragma once |
| 39 | |
| 40 | #include <db.h> |
| 41 | |
| 42 | #include "db_env.hpp" |
| 43 | #include "exceptions.hpp" |
| 44 | |
| 45 | namespace ftcxx { |
| 46 | |
| 47 | class DBTxn { |
| 48 | public: |
| 49 | DBTxn() |
| 50 | : _flags(0), |
| 51 | _txn(nullptr) |
| 52 | {} |
| 53 | |
| 54 | explicit DBTxn(const DBEnv &env, int flags=0) |
| 55 | : _flags(flags), |
| 56 | _txn(nullptr) |
| 57 | { |
| 58 | DB_TXN *t; |
| 59 | int r = env.env()->txn_begin(env.env(), nullptr, &t, _flags); |
| 60 | handle_ft_retval(r); |
| 61 | _txn = t; |
| 62 | } |
| 63 | |
| 64 | DBTxn(const DBEnv &env, const DBTxn &parent, int flags=0) |
| 65 | : _flags(flags), |
| 66 | _txn(nullptr) |
| 67 | { |
| 68 | DB_TXN *t; |
| 69 | int r = env.env()->txn_begin(env.env(), parent.txn(), &t, _flags); |
| 70 | handle_ft_retval(r); |
| 71 | _txn = t; |
| 72 | } |
| 73 | |
| 74 | ~DBTxn() { |
| 75 | if (_txn) { |
| 76 | abort(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | DBTxn(const DBTxn &) = delete; |
| 81 | DBTxn& operator=(const DBTxn &) = delete; |
| 82 | |
| 83 | DBTxn(DBTxn &&o) |
| 84 | : _flags(0), |
| 85 | _txn(nullptr) |
| 86 | { |
| 87 | std::swap(_flags, o._flags); |
| 88 | std::swap(_txn, o._txn); |
| 89 | } |
| 90 | |
| 91 | DBTxn& operator=(DBTxn &&o) { |
| 92 | std::swap(_flags, o._flags); |
| 93 | std::swap(_txn, o._txn); |
| 94 | return *this; |
| 95 | } |
| 96 | |
| 97 | DB_TXN *txn() const { return _txn; } |
| 98 | |
| 99 | void commit(int flags=0) { |
| 100 | int r = _txn->commit(_txn, flags); |
| 101 | handle_ft_retval(r); |
| 102 | _txn = nullptr; |
| 103 | } |
| 104 | |
| 105 | void abort() { |
| 106 | int r = _txn->abort(_txn); |
| 107 | handle_ft_retval(r); |
| 108 | _txn = nullptr; |
| 109 | } |
| 110 | |
| 111 | bool is_read_only() const { |
| 112 | return _flags & DB_TXN_READ_ONLY; |
| 113 | } |
| 114 | |
| 115 | uint64_t id() const { |
| 116 | if (!_txn) { |
| 117 | return 0; |
| 118 | } |
| 119 | return _txn->id64(_txn); |
| 120 | } |
| 121 | |
| 122 | private: |
| 123 | int _flags; |
| 124 | DB_TXN *_txn; |
| 125 | }; |
| 126 | |
| 127 | } // namespace ftcxx |
| 128 | |