Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
lmdb_helpers.cpp
Go to the documentation of this file.
3#include "lmdb.h"
4#include <algorithm>
5#include <cstdint>
6#include <cstring>
7#include <functional>
8#include <vector>
9
10#ifdef __APPLE__
11#include <libkern/OSByteOrder.h>
12#define htole64(x) OSSwapHostToLittleInt64(x)
13#define le64toh(x) OSSwapLittleToHostInt64(x)
14#else
15#include <sys/types.h>
16#endif
17
18namespace bb::lmdblib {
19void throw_error(const std::string& errorString, int error)
20{
21 std::stringstream ss;
22 ss << errorString << ": " << error << " - " << mdb_strerror(error) << std::endl;
23 throw std::runtime_error(ss.str());
24}
25
26std::vector<uint8_t> serialise_key(uint8_t key)
27{
28 return { key };
29}
30
31void deserialise_key(void* data, uint8_t& key)
32{
33 uint8_t* p = static_cast<uint8_t*>(data);
34 key = *p;
35}
36
37// 64 bit integers are stored in little endian byte order
38std::vector<uint8_t> serialise_key(uint64_t key)
39{
40 uint64_t le = htole64(key);
41 const uint8_t* p = reinterpret_cast<uint8_t*>(&le);
42 return std::vector<uint8_t>(p, p + sizeof(key));
43}
44
45void deserialise_key(void* data, uint64_t& key)
46{
47 uint64_t le = 0;
48 std::memcpy(&le, data, sizeof(le));
49 key = le64toh(le);
50}
51
52std::vector<uint8_t> serialise_key(const uint256_t& key)
53{
54 std::vector<uint8_t> buf(32);
55 std::memcpy(buf.data(), key.data, 32);
56 return buf;
57}
58
60{
62}
63
64int size_cmp(const MDB_val* a, const MDB_val* b)
65{
66 if (a->mv_size < b->mv_size) {
67 return -1;
68 }
69 return (a->mv_size > b->mv_size) ? 1 : 0;
70}
71
72std::vector<uint8_t> mdb_val_to_vector(const MDB_val& dbVal)
73{
74 const uint8_t* p = static_cast<uint8_t*>(dbVal.mv_data);
75 return std::vector<uint8_t>(p, p + dbVal.mv_size);
76}
77
78void copy_to_vector(const MDB_val& dbVal, std::vector<uint8_t>& target)
79{
80 std::vector<uint8_t> temp = mdb_val_to_vector(dbVal);
81 target.swap(temp);
82}
83} // namespace bb::lmdblib
const std::vector< FF > data
FF a
FF b
uint8_t const * buf
Definition data_store.hpp:9
int size_cmp(const MDB_val *a, const MDB_val *b)
void deserialise_key(void *data, uint8_t &key)
std::vector< uint8_t > serialise_key(uint8_t key)
std::vector< uint8_t > mdb_val_to_vector(const MDB_val &dbVal)
void throw_error(const std::string &errorString, int error)
void copy_to_vector(const MDB_val &dbVal, std::vector< uint8_t > &target)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13