Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
table.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <utility>
4
5
#include "
common/expect.h
"
6
#include "
lmdb/error.h
"
7
#include "
lmdb/key_stream.h
"
8
#include "
lmdb/util.h
"
9
#include "
lmdb/value_stream.h
"
10
11
namespace
lmdb
12
{
14
struct
table
15
{
16
char
const
*
const
name
;
17
const
unsigned
flags
;
18
MDB_cmp_func
*
const
key_cmp
;
19
MDB_cmp_func
*
const
value_cmp
;
20
22
expect<MDB_dbi>
open
(
MDB_txn
&
write_txn
)
const
noexcept
;
23
};
24
26
template
<
typename
K,
typename
V>
27
struct
basic_table
:
table
28
{
29
using
key_type
= K;
30
using
value_type
= V;
31
33
static
constexpr
unsigned
compute_flags
(
const
unsigned
flags
)
noexcept
34
{
35
return
flags
| ((
flags
&
MDB_DUPSORT
) ?
MDB_DUPFIXED
: 0);
36
}
37
38
constexpr
explicit
basic_table
(
const
char
*
name
,
unsigned
flags
= 0,
MDB_cmp_func
value_cmp
=
nullptr
) noexcept
39
:
table
{
name
,
compute_flags
(
flags
), &
lmdb::less<lmdb::native_type<K>
>,
value_cmp
}
40
{}
41
54
template
<
typename
U,
typename
F = U, std::
size_t
offset = 0>
55
static
expect<F>
get_value
(
MDB_val
value
)
noexcept
56
{
57
static_assert
(std::is_same<U, V>(),
"bad ELECTRONEUM_FIELD?"
);
58
static_assert
(std::is_pod<F>(),
"F must be POD"
);
59
static_assert
(
sizeof
(
F
) + offset <=
sizeof
(U),
"bad field type and/or offset"
);
60
61
if
(
value
.mv_size !=
sizeof
(U))
62
return
{
lmdb::error
(
MDB_BAD_VALSIZE
)};
63
64
F
out;
65
std::memcpy(std::addressof(out),
static_cast<
char
*
>
(
value
.mv_data) + offset,
sizeof
(out));
66
return
out;
67
}
68
76
template
<
typename
D>
77
expect<key_stream<K, V, D>
>
78
static
get_key_stream
(std::unique_ptr<MDB_cursor, D> cur)
noexcept
79
{
80
ELECTRONEUM_PRECOND
(cur !=
nullptr
);
81
82
MDB_val
key
;
83
MDB_val
value
;
84
const
int
err =
mdb_cursor_get
(cur.get(), &
key
, &
value
,
MDB_FIRST
);
85
if
(err)
86
{
87
if
(err !=
MDB_NOTFOUND
)
88
return
{
lmdb::error
(err)};
89
cur.reset();
// return empty set
90
}
91
return
key_stream<K, V, D>
{std::move(cur)};
92
}
93
101
template
<
typename
D>
102
expect<value_stream<V, D>
>
103
static
get_value_stream
(K
const
&
key
, std::unique_ptr<MDB_cursor, D> cur)
noexcept
104
{
105
ELECTRONEUM_PRECOND
(cur !=
nullptr
);
106
107
MDB_val
key_bytes =
lmdb::to_val
(
key
);
108
MDB_val
value
;
109
const
int
err =
mdb_cursor_get
(cur.get(), &key_bytes, &
value
,
MDB_SET
);
110
if
(err)
111
{
112
if
(err !=
MDB_NOTFOUND
)
113
return
{
lmdb::error
(err)};
114
cur.reset();
// return empty set
115
}
116
return
value_stream<V, D>
{std::move(cur)};
117
}
118
};
119
}
// lmdb
120
expect
Definition
expect.h:133
lmdb::key_stream
Definition
key_stream.h:189
lmdb::value_stream
Definition
value_stream.h:187
F
#define F(s)
expect.h
ELECTRONEUM_PRECOND
#define ELECTRONEUM_PRECOND(...)
If precondition fails, return error::kInvalidArgument in current scope.
Definition
expect.h:39
MDB_BAD_VALSIZE
#define MDB_BAD_VALSIZE
Definition
lmdb.h:480
MDB_NOTFOUND
#define MDB_NOTFOUND
Definition
lmdb.h:439
mdb_cursor_get
int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op op)
Retrieve by cursor.
MDB_SET
@ MDB_SET
Definition
lmdb.h:422
MDB_FIRST
@ MDB_FIRST
Definition
lmdb.h:399
MDB_DUPFIXED
#define MDB_DUPFIXED
Definition
lmdb.h:351
MDB_DUPSORT
#define MDB_DUPSORT
Definition
lmdb.h:345
MDB_txn
struct MDB_txn MDB_txn
Opaque structure for a transaction handle.
Definition
lmdb.h:267
MDB_cmp_func
int MDB_cmp_func(const MDB_val *a, const MDB_val *b)
A callback function used to compare two keys in a database.
Definition
lmdb.h:292
key
const char * key
Definition
hmac_keccak.cpp:39
key_stream.h
lmdb
Definition
database.cpp:46
lmdb::write_txn
std::unique_ptr< MDB_txn, abort_write_txn > write_txn
Definition
transaction.h:94
lmdb::less
int less(MDB_val const *left, MDB_val const *right) noexcept
Definition
util.h:111
lmdb::to_val
MDB_val to_val(T &&value) noexcept
Definition
util.h:88
lmdb::error
error
Tracks LMDB error codes.
Definition
error.h:45
value
const GenericPointer< typename T::ValueType > T2 value
Definition
pointer.h:1225
error.h
util.h
MDB_val
Generic structure used for passing keys and data in and out of the database.
Definition
lmdb.h:286
lmdb::basic_table::get_value_stream
static expect< value_stream< V, D > > get_value_stream(K const &key, std::unique_ptr< MDB_cursor, D > cur) noexcept
Definition
table.h:103
lmdb::basic_table::get_value
static expect< F > get_value(MDB_val value) noexcept
Definition
table.h:55
lmdb::basic_table::value_type
V value_type
Definition
table.h:30
lmdb::basic_table::key_type
K key_type
Definition
table.h:29
lmdb::basic_table::get_key_stream
static expect< key_stream< K, V, D > > get_key_stream(std::unique_ptr< MDB_cursor, D > cur) noexcept
Definition
table.h:78
lmdb::basic_table::basic_table
constexpr basic_table(const char *name, unsigned flags=0, MDB_cmp_func value_cmp=nullptr) noexcept
Definition
table.h:38
lmdb::basic_table::compute_flags
static constexpr unsigned compute_flags(const unsigned flags) noexcept
Definition
table.h:33
lmdb::table
Helper for grouping typical LMDB DBI options.
Definition
table.h:15
lmdb::table::open
expect< MDB_dbi > open(MDB_txn &write_txn) const noexcept
Definition
table.cpp:31
lmdb::table::flags
const unsigned flags
Definition
table.h:17
lmdb::table::value_cmp
MDB_cmp_func *const value_cmp
Definition
table.h:19
lmdb::table::name
char const *const name
Definition
table.h:16
lmdb::table::key_cmp
MDB_cmp_func *const key_cmp
Definition
table.h:18
value_stream.h
src
lmdb
table.h
Generated on
for Electroneum by
1.17.0