Electroneum
transaction.h
Go to the documentation of this file.
1 // Copyright (c) 2018, The Monero Project
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without modification, are
5 // permitted provided that the following conditions are met:
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this list of
8 // conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 // of conditions and the following disclaimer in the documentation and/or other
12 // materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its contributors may be
15 // used to endorse or promote products derived from this software without specific
16 // prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
19 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #pragma once
28 
29 #include <lmdb.h>
30 #include <memory>
31 
32 #include "lmdb/error.h"
33 
35 #define ELECTRONEUM_CURSOR(name) \
36  struct close_ ## name : ::lmdb::close_cursor {}; \
37  using name = std::unique_ptr< MDB_cursor, close_ ## name >;
38 
39 namespace lmdb
40 {
41  struct abort_txn
42  {
43  void operator()(MDB_txn* ptr) const noexcept
44  {
45  if (ptr)
46  mdb_txn_abort(ptr);
47  }
48  };
49 
55  {
56  void operator()(MDB_txn* ptr) const noexcept;
57  // implementation in database.cpp
58  };
59 
65  {
66  void operator()(MDB_txn* ptr) const noexcept
67  {
68  release_read_txn{}(ptr);
69  }
70  };
71 
72  struct close_cursor
73  {
74  void operator()(MDB_cursor* ptr) const noexcept
75  {
76  if (ptr)
77  mdb_cursor_close(ptr);
78  }
79  };
80 
81  template<typename D>
83  open_cursor(MDB_txn& txn, MDB_dbi tbl) noexcept
84  {
85  MDB_cursor* cur = nullptr;
86  ELECTRONEUM_LMDB_CHECK(mdb_cursor_open(&txn, tbl, &cur));
87  return std::unique_ptr<MDB_cursor, D>{cur};
88  }
89 
90  // The below use the C++ type system to designate `MDB_txn` status.
91 
92  using suspended_txn = std::unique_ptr<MDB_txn, abort_txn>;
93  using read_txn = std::unique_ptr<MDB_txn, release_read_txn>;
94  using write_txn = std::unique_ptr<MDB_txn, abort_write_txn>;
95 } // lmdb
void mdb_cursor_close(MDB_cursor *cursor)
Close a cursor handle.
expect< std::unique_ptr< MDB_cursor, D > > open_cursor(MDB_txn &txn, MDB_dbi tbl) noexcept
Definition: transaction.h:83
void operator()(MDB_txn *ptr) const noexcept
Definition: transaction.h:66
Lightning memory-mapped database library.
#define ELECTRONEUM_LMDB_CHECK(...)
Executes a LMDB command, and returns errors via lmdb::error enum.
Definition: error.h:33
std::unique_ptr< MDB_txn, abort_txn > suspended_txn
Definition: transaction.h:92
std::unique_ptr< MDB_txn, abort_write_txn > write_txn
Definition: transaction.h:94
std::unique_ptr< MDB_txn, release_read_txn > read_txn
Definition: transaction.h:93
struct MDB_txn MDB_txn
Opaque structure for a transaction handle.
Definition: lmdb.h:267
Definition: expect.h:70
unsigned int MDB_dbi
A handle for an individual database in the DB environment.
Definition: lmdb.h:270
void mdb_txn_abort(MDB_txn *txn)
Abandon all the operations of the transaction instead of saving them.
struct MDB_cursor MDB_cursor
Opaque structure for navigating through a database.
Definition: lmdb.h:273
void operator()(MDB_txn *ptr) const noexcept
Definition: database.cpp:63
void operator()(MDB_cursor *ptr) const noexcept
Definition: transaction.h:74
void operator()(MDB_txn *ptr) const noexcept
Definition: transaction.h:43
int mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **cursor)
Create a cursor handle.