Electroneum
value_stream.cpp
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 #include "value_stream.h"
28 
29 #include <stdexcept>
30 
31 #include "common/expect.h"
32 #include "lmdb/error.h"
33 #include "lmdb/util.h"
34 
35 namespace lmdb
36 {
37  namespace stream
38  {
40  {
41  mdb_size_t out = 0;
42  if (cur)
43  {
44  const int rc = mdb_cursor_count(cur, &out);
45  if (rc)
46  ELECTRONEUM_THROW(lmdb::error(rc), "mdb_cursor_count");
47  }
48  return out;
49  }
50 
51  std::pair<epee::span<const std::uint8_t>, epee::span<const std::uint8_t>>
52  get(MDB_cursor& cur, MDB_cursor_op op, std::size_t key, std::size_t value)
53  {
54  MDB_val key_bytes{};
55  MDB_val value_bytes{};
56  const int rc = mdb_cursor_get(&cur, &key_bytes, &value_bytes, op);
57  if (rc)
58  {
59  if (rc == MDB_NOTFOUND)
60  return {};
61  ELECTRONEUM_THROW(lmdb::error(rc), "mdb_cursor_get");
62  }
63 
64  if (key && key != key_bytes.mv_size)
65  ELECTRONEUM_THROW(lmdb::error(MDB_BAD_VALSIZE), "mdb_cursor_get key");
66 
67  if (value && (value_bytes.mv_size % value != 0 || value_bytes.mv_size == 0))
68  ELECTRONEUM_THROW(lmdb::error(MDB_BAD_VALSIZE), "mdb_cursor_get value");
69 
70  return {lmdb::to_byte_span(key_bytes), lmdb::to_byte_span(value_bytes)};
71  }
72  }
73 }
74 
#define MDB_NOTFOUND
Definition: lmdb.h:439
int mdb_cursor_count(MDB_cursor *cursor, mdb_size_t *countp)
Return count of duplicates for current key.
const char * key
Definition: hmac_keccak.cpp:39
#define MDB_BAD_VALSIZE
Definition: lmdb.h:480
mdb_size_t count(MDB_cursor *cur)
MDB_cursor_op
Cursor Get operations.
Definition: lmdb.h:398
int mdb_cursor_get(MDB_cursor *cursor, MDB_val *key, MDB_val *data, MDB_cursor_op op)
Retrieve by cursor.
struct MDB_cursor MDB_cursor
Opaque structure for navigating through a database.
Definition: lmdb.h:273
Generic structure used for passing keys and data in and out of the database.
Definition: lmdb.h:286
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
#define ELECTRONEUM_THROW(code, msg)
Definition: expect.h:66
size_t mdb_size_t
Definition: lmdb.h:196
constexpr epee::span< const std::uint8_t > to_byte_span(MDB_val value) noexcept
Definition: util.h:97
error
Tracks LMDB error codes.
Definition: error.h:44