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