Debian Codegraph @

src/lmdb/value_stream.cpp

raw symbols SCIP
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 #include "value_stream.h"
29
30 #include <stdexcept>
31
32 #include "common/expect.h"
33 #include "lmdb/error.h"
34 #include "lmdb/util.h"
35
36 namespace lmdb
37 {
38 namespace stream
39 {
40 mdb_size_t count(MDB_cursor* cur)
41 {
42 mdb_size_t out = 0;
43 if (cur)
44 {
45 const int rc = mdb_cursor_count(cur, &out);
46 if (rc)
47 MONERO_THROW(lmdb::error(rc), "mdb_cursor_count");
48 }
49 return out;
50 }
51
52 std::pair<epee::span<const std::uint8_t>, epee::span<const std::uint8_t>>
53 get(MDB_cursor& cur, MDB_cursor_op op, std::size_t key, std::size_t value)
54 {
55 MDB_val key_bytes{};
56 MDB_val value_bytes{};
57 const int rc = mdb_cursor_get(&cur, &key_bytes, &value_bytes, op);
58 if (rc)
59 {
60 if (rc == MDB_NOTFOUND)
61 return {};
62 MONERO_THROW(lmdb::error(rc), "mdb_cursor_get");
63 }
64
65 if (key && key != key_bytes.mv_size)
66 MONERO_THROW(lmdb::error(MDB_BAD_VALSIZE), "mdb_cursor_get key");
67
68 if (value && (value_bytes.mv_size % value != 0 || value_bytes.mv_size == 0))
69 MONERO_THROW(lmdb::error(MDB_BAD_VALSIZE), "mdb_cursor_get value");
70
71 return {lmdb::to_byte_span(key_bytes), lmdb::to_byte_span(value_bytes)};
72 }
73 }
74 }
75