/tmp/bitcoin/src/dbwrapper.h
Line | Count | Source |
1 | | // Copyright (c) 2012-present The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #ifndef BITCOIN_DBWRAPPER_H |
6 | | #define BITCOIN_DBWRAPPER_H |
7 | | |
8 | | #include <attributes.h> |
9 | | #include <serialize.h> |
10 | | #include <span.h> |
11 | | #include <streams.h> |
12 | | #include <util/byte_units.h> |
13 | | #include <util/check.h> |
14 | | #include <util/fs.h> |
15 | | |
16 | | #include <cstddef> |
17 | | #include <exception> |
18 | | #include <memory> |
19 | | #include <optional> |
20 | | #include <stdexcept> |
21 | | #include <string> |
22 | | |
23 | | namespace leveldb { |
24 | | class Env; |
25 | | } // namespace leveldb |
26 | | |
27 | | static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64; |
28 | | static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024; |
29 | | static const size_t DBWRAPPER_MAX_FILE_SIZE{32_MiB}; |
30 | | |
31 | | //! User-controlled performance and debug options. |
32 | | struct DBOptions { |
33 | | //! Compact database on startup. |
34 | | bool force_compact = false; |
35 | | }; |
36 | | |
37 | | //! Application-specific storage settings. |
38 | | struct DBParams { |
39 | | //! Location in the filesystem where leveldb data will be stored. |
40 | | fs::path path; |
41 | | //! Configures various leveldb cache settings. |
42 | | size_t cache_bytes; |
43 | | //! If true, use leveldb's memory environment. |
44 | | bool memory_only = false; |
45 | | //! If true, remove all existing data. |
46 | | bool wipe_data = false; |
47 | | //! If true, store data obfuscated via simple XOR. If false, XOR with a |
48 | | //! zero'd byte array. |
49 | | bool obfuscate = false; |
50 | | //! Passed-through options. |
51 | | DBOptions options{}; |
52 | | //! If non-null, use this as the leveldb::Env instead of the default. |
53 | | //! Caller retains ownership. |
54 | | leveldb::Env* testing_env = nullptr; |
55 | | //! Maximum LevelDB SST file size. Larger values reduce the frequency |
56 | | //! of compactions but increase their duration. |
57 | | size_t max_file_size = DBWRAPPER_MAX_FILE_SIZE; |
58 | | }; |
59 | | |
60 | | class dbwrapper_error : public std::runtime_error |
61 | | { |
62 | | public: |
63 | 10 | explicit dbwrapper_error(const std::string& msg) : std::runtime_error(msg) {} |
64 | | }; |
65 | | |
66 | | class CDBWrapper; |
67 | | |
68 | | /** These should be considered an implementation detail of the specific database. |
69 | | */ |
70 | | namespace dbwrapper_private { |
71 | | |
72 | | /** Work around circular dependency, as well as for testing in dbwrapper_tests. |
73 | | * Database obfuscation should be considered an implementation detail of the |
74 | | * specific database. |
75 | | */ |
76 | | const Obfuscation& GetObfuscation(const CDBWrapper&); |
77 | | }; // namespace dbwrapper_private |
78 | | |
79 | | bool DestroyDB(const std::string& path_str); |
80 | | |
81 | | /** Batch of changes queued to be written to a CDBWrapper */ |
82 | | class CDBBatch |
83 | | { |
84 | | friend class CDBWrapper; |
85 | | |
86 | | private: |
87 | | const CDBWrapper &parent; |
88 | | |
89 | | struct WriteBatchImpl; |
90 | | const std::unique_ptr<WriteBatchImpl> m_impl_batch; |
91 | | |
92 | | DataStream m_key_scratch{}; |
93 | | DataStream m_value_scratch{}; |
94 | | |
95 | | void WriteImpl(std::span<const std::byte> key, DataStream& value); |
96 | | void EraseImpl(std::span<const std::byte> key); |
97 | | |
98 | | public: |
99 | | /** |
100 | | * @param[in] _parent CDBWrapper that this batch is to be submitted to |
101 | | */ |
102 | | explicit CDBBatch(const CDBWrapper& _parent); |
103 | | ~CDBBatch(); |
104 | | void Clear(); |
105 | | |
106 | | template <typename K, typename V> |
107 | | void Write(const K& key, const V& value) |
108 | 421k | { |
109 | 421k | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; |
110 | 421k | m_key_scratch << key; |
111 | 421k | m_value_scratch << value; |
112 | 421k | WriteImpl(m_key_scratch, m_value_scratch); |
113 | 421k | } void CDBBatch::Write<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, uint256>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, uint256 const&) Line | Count | Source | 108 | 8 | { | 109 | 8 | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 8 | m_key_scratch << key; | 111 | 8 | m_value_scratch << value; | 112 | 8 | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 8 | } |
void CDBBatch::Write<unsigned char, unsigned int>(unsigned char const&, unsigned int const&) Line | Count | Source | 108 | 258 | { | 109 | 258 | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 258 | m_key_scratch << key; | 111 | 258 | m_value_scratch << value; | 112 | 258 | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 258 | } |
void CDBBatch::Write<unsigned char, bool>(unsigned char const&, bool const&) Line | Count | Source | 108 | 2 | { | 109 | 2 | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 2 | m_key_scratch << key; | 111 | 2 | m_value_scratch << value; | 112 | 2 | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 2 | } |
void CDBBatch::Write<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool const&) Line | Count | Source | 108 | 2 | { | 109 | 2 | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 2 | m_key_scratch << key; | 111 | 2 | m_value_scratch << value; | 112 | 2 | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 2 | } |
void CDBBatch::Write<unsigned char, uint256>(unsigned char const&, uint256 const&) Line | Count | Source | 108 | 3.77k | { | 109 | 3.77k | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 3.77k | m_key_scratch << key; | 111 | 3.77k | m_value_scratch << value; | 112 | 3.77k | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 3.77k | } |
void CDBBatch::Write<dbwrapper_tests::StringContentsSerializer, unsigned int>(dbwrapper_tests::StringContentsSerializer const&, unsigned int const&) Line | Count | Source | 108 | 100 | { | 109 | 100 | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 100 | m_key_scratch << key; | 111 | 100 | m_value_scratch << value; | 112 | 100 | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 100 | } |
void CDBBatch::Write<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Obfuscation>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, Obfuscation const&) Line | Count | Source | 108 | 513 | { | 109 | 513 | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 513 | m_key_scratch << key; | 111 | 513 | m_value_scratch << value; | 112 | 513 | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 513 | } |
void CDBBatch::Write<unsigned char, CBlockLocator>(unsigned char const&, CBlockLocator const&) Line | Count | Source | 108 | 288 | { | 109 | 288 | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 288 | m_key_scratch << key; | 111 | 288 | m_value_scratch << value; | 112 | 288 | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 288 | } |
void CDBBatch::Write<unsigned char, FlatFilePos>(unsigned char const&, FlatFilePos const&) Line | Count | Source | 108 | 184 | { | 109 | 184 | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 184 | m_key_scratch << key; | 111 | 184 | m_value_scratch << value; | 112 | 184 | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 184 | } |
blockfilterindex.cpp:void CDBBatch::Write<index_util::DBHeightKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHeightKey const&, std::pair<uint256, (anonymous namespace)::DBVal> const&) Line | Count | Source | 108 | 7.54k | { | 109 | 7.54k | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 7.54k | m_key_scratch << key; | 111 | 7.54k | m_value_scratch << value; | 112 | 7.54k | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 7.54k | } |
blockfilterindex.cpp:void CDBBatch::Write<index_util::DBHashKey, (anonymous namespace)::DBVal>(index_util::DBHashKey const&, (anonymous namespace)::DBVal const&) Line | Count | Source | 108 | 111 | { | 109 | 111 | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 111 | m_key_scratch << key; | 111 | 111 | m_value_scratch << value; | 112 | 111 | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 111 | } |
coinstatsindex.cpp:void CDBBatch::Write<index_util::DBHeightKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHeightKey const&, std::pair<uint256, (anonymous namespace)::DBVal> const&) Line | Count | Source | 108 | 3.86k | { | 109 | 3.86k | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 3.86k | m_key_scratch << key; | 111 | 3.86k | m_value_scratch << value; | 112 | 3.86k | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 3.86k | } |
coinstatsindex.cpp:void CDBBatch::Write<index_util::DBHashKey, (anonymous namespace)::DBVal>(index_util::DBHashKey const&, (anonymous namespace)::DBVal const&) Line | Count | Source | 108 | 121 | { | 109 | 121 | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 121 | m_key_scratch << key; | 111 | 121 | m_value_scratch << value; | 112 | 121 | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 121 | } |
void CDBBatch::Write<unsigned char, MuHash3072>(unsigned char const&, MuHash3072 const&) Line | Count | Source | 108 | 126 | { | 109 | 126 | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 126 | m_key_scratch << key; | 111 | 126 | m_value_scratch << value; | 112 | 126 | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 126 | } |
void CDBBatch::Write<std::pair<unsigned char, uint256>, CDiskTxPos>(std::pair<unsigned char, uint256> const&, CDiskTxPos const&) Line | Count | Source | 108 | 3.74k | { | 109 | 3.74k | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 3.74k | m_key_scratch << key; | 111 | 3.74k | m_value_scratch << value; | 112 | 3.74k | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 3.74k | } |
void CDBBatch::Write<char [12], std::pair<unsigned long, unsigned long>>(char const (&) [12], std::pair<unsigned long, unsigned long> const&) Line | Count | Source | 108 | 6 | { | 109 | 6 | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 6 | m_key_scratch << key; | 111 | 6 | m_value_scratch << value; | 112 | 6 | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 6 | } |
void CDBBatch::Write<DBKey, char [1]>(DBKey const&, char const (&) [1]) Line | Count | Source | 108 | 38 | { | 109 | 38 | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 38 | m_key_scratch << key; | 111 | 38 | m_value_scratch << value; | 112 | 38 | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 38 | } |
void CDBBatch::Write<unsigned char, unsigned char>(unsigned char const&, unsigned char const&) Line | Count | Source | 108 | 14 | { | 109 | 14 | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 14 | m_key_scratch << key; | 111 | 14 | m_value_scratch << value; | 112 | 14 | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 14 | } |
void CDBBatch::Write<std::pair<unsigned char, int>, kernel::CBlockFileInfo>(std::pair<unsigned char, int> const&, kernel::CBlockFileInfo const&) Line | Count | Source | 108 | 1.63k | { | 109 | 1.63k | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 1.63k | m_key_scratch << key; | 111 | 1.63k | m_value_scratch << value; | 112 | 1.63k | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 1.63k | } |
void CDBBatch::Write<unsigned char, int>(unsigned char const&, int const&) Line | Count | Source | 108 | 3.40k | { | 109 | 3.40k | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 3.40k | m_key_scratch << key; | 111 | 3.40k | m_value_scratch << value; | 112 | 3.40k | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 3.40k | } |
void CDBBatch::Write<std::pair<unsigned char, uint256>, CDiskBlockIndex>(std::pair<unsigned char, uint256> const&, CDiskBlockIndex const&) Line | Count | Source | 108 | 118k | { | 109 | 118k | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 118k | m_key_scratch << key; | 111 | 118k | m_value_scratch << value; | 112 | 118k | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 118k | } |
void CDBBatch::Write<std::pair<unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, unsigned char>(std::pair<unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> const&, unsigned char const&) Line | Count | Source | 108 | 7 | { | 109 | 7 | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 7 | m_key_scratch << key; | 111 | 7 | m_value_scratch << value; | 112 | 7 | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 7 | } |
void CDBBatch::Write<unsigned char, std::vector<uint256, std::allocator<uint256>>>(unsigned char const&, std::vector<uint256, std::allocator<uint256>> const&) Line | Count | Source | 108 | 3.74k | { | 109 | 3.74k | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 3.74k | m_key_scratch << key; | 111 | 3.74k | m_value_scratch << value; | 112 | 3.74k | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 3.74k | } |
txdb.cpp:void CDBBatch::Write<(anonymous namespace)::CoinEntry, Coin>((anonymous namespace)::CoinEntry const&, Coin const&) Line | Count | Source | 108 | 272k | { | 109 | 272k | ScopedDataStreamUsage scoped_key{m_key_scratch}, scoped_value{m_value_scratch}; | 110 | 272k | m_key_scratch << key; | 111 | 272k | m_value_scratch << value; | 112 | 272k | WriteImpl(m_key_scratch, m_value_scratch); | 113 | 272k | } |
|
114 | | |
115 | | template <typename K> |
116 | | void Erase(const K& key) |
117 | 40.7k | { |
118 | 40.7k | ScopedDataStreamUsage scoped_key{m_key_scratch}; |
119 | 40.7k | m_key_scratch << key; |
120 | 40.7k | EraseImpl(m_key_scratch); |
121 | 40.7k | } void CDBBatch::Erase<unsigned char>(unsigned char const&) Line | Count | Source | 117 | 7.49k | { | 118 | 7.49k | ScopedDataStreamUsage scoped_key{m_key_scratch}; | 119 | 7.49k | m_key_scratch << key; | 120 | 7.49k | EraseImpl(m_key_scratch); | 121 | 7.49k | } |
void CDBBatch::Erase<DBKey>(DBKey const&) Line | Count | Source | 117 | 6 | { | 118 | 6 | ScopedDataStreamUsage scoped_key{m_key_scratch}; | 119 | 6 | m_key_scratch << key; | 120 | 6 | EraseImpl(m_key_scratch); | 121 | 6 | } |
txdb.cpp:void CDBBatch::Erase<(anonymous namespace)::CoinEntry>((anonymous namespace)::CoinEntry const&) Line | Count | Source | 117 | 33.2k | { | 118 | 33.2k | ScopedDataStreamUsage scoped_key{m_key_scratch}; | 119 | 33.2k | m_key_scratch << key; | 120 | 33.2k | EraseImpl(m_key_scratch); | 121 | 33.2k | } |
|
122 | | |
123 | | size_t ApproximateSize() const; |
124 | | }; |
125 | | |
126 | | class CDBIterator |
127 | | { |
128 | | public: |
129 | | struct IteratorImpl; |
130 | | |
131 | | private: |
132 | | const CDBWrapper &parent; |
133 | | const std::unique_ptr<IteratorImpl> m_impl_iter; |
134 | | DataStream m_scratch{}; |
135 | | |
136 | | void SeekImpl(std::span<const std::byte> key); |
137 | | std::span<const std::byte> GetKeyImpl() const; |
138 | | std::span<const std::byte> GetValueImpl() const; |
139 | | |
140 | | public: |
141 | | |
142 | | /** |
143 | | * @param[in] _parent Parent CDBWrapper instance. |
144 | | * @param[in] _piter The original leveldb iterator. |
145 | | */ |
146 | | CDBIterator(const CDBWrapper& _parent, std::unique_ptr<IteratorImpl> _piter); |
147 | | ~CDBIterator(); |
148 | | |
149 | | bool Valid() const; |
150 | | |
151 | | void SeekToFirst(); |
152 | | |
153 | 4.33k | template<typename K> void Seek(const K& key) { |
154 | 4.33k | ScopedDataStreamUsage scoped_scratch{m_scratch}; |
155 | 4.33k | m_scratch << key; |
156 | 4.33k | SeekImpl(m_scratch); |
157 | 4.33k | } void CDBIterator::Seek<unsigned char>(unsigned char const&) Line | Count | Source | 153 | 1.23k | template<typename K> void Seek(const K& key) { | 154 | 1.23k | ScopedDataStreamUsage scoped_scratch{m_scratch}; | 155 | 1.23k | m_scratch << key; | 156 | 1.23k | SeekImpl(m_scratch); | 157 | 1.23k | } |
void CDBIterator::Seek<dbwrapper_tests::StringContentsSerializer>(dbwrapper_tests::StringContentsSerializer const&) Line | Count | Source | 153 | 2 | template<typename K> void Seek(const K& key) { | 154 | 2 | ScopedDataStreamUsage scoped_scratch{m_scratch}; | 155 | 2 | m_scratch << key; | 156 | 2 | SeekImpl(m_scratch); | 157 | 2 | } |
void CDBIterator::Seek<index_util::DBHeightKey>(index_util::DBHeightKey const&) Line | Count | Source | 153 | 680 | template<typename K> void Seek(const K& key) { | 154 | 680 | ScopedDataStreamUsage scoped_scratch{m_scratch}; | 155 | 680 | m_scratch << key; | 156 | 680 | SeekImpl(m_scratch); | 157 | 680 | } |
void CDBIterator::Seek<std::pair<unsigned char, unsigned long>>(std::pair<unsigned char, unsigned long> const&) Line | Count | Source | 153 | 35 | template<typename K> void Seek(const K& key) { | 154 | 35 | ScopedDataStreamUsage scoped_scratch{m_scratch}; | 155 | 35 | m_scratch << key; | 156 | 35 | SeekImpl(m_scratch); | 157 | 35 | } |
void CDBIterator::Seek<std::pair<unsigned char, uint256>>(std::pair<unsigned char, uint256> const&) Line | Count | Source | 153 | 2.38k | template<typename K> void Seek(const K& key) { | 154 | 2.38k | ScopedDataStreamUsage scoped_scratch{m_scratch}; | 155 | 2.38k | m_scratch << key; | 156 | 2.38k | SeekImpl(m_scratch); | 157 | 2.38k | } |
|
158 | | |
159 | | void Next(); |
160 | | |
161 | 362k | template<typename K> bool GetKey(K& key) { |
162 | 362k | try { |
163 | 362k | SpanReader ssKey{GetKeyImpl()}; |
164 | 362k | ssKey >> key; |
165 | 362k | } catch (const std::exception&) { |
166 | 740 | return false; |
167 | 740 | } |
168 | 361k | return true; |
169 | 362k | } bool CDBIterator::GetKey<unsigned short>(unsigned short&) Line | Count | Source | 161 | 2 | template<typename K> bool GetKey(K& key) { | 162 | 2 | try { | 163 | 2 | SpanReader ssKey{GetKeyImpl()}; | 164 | 2 | ssKey >> key; | 165 | 2 | } catch (const std::exception&) { | 166 | 2 | return false; | 167 | 2 | } | 168 | 0 | return true; | 169 | 2 | } |
bool CDBIterator::GetKey<unsigned char>(unsigned char&) Line | Count | Source | 161 | 390 | template<typename K> bool GetKey(K& key) { | 162 | 390 | try { | 163 | 390 | SpanReader ssKey{GetKeyImpl()}; | 164 | 390 | ssKey >> key; | 165 | 390 | } catch (const std::exception&) { | 166 | 0 | return false; | 167 | 0 | } | 168 | 390 | return true; | 169 | 390 | } |
bool CDBIterator::GetKey<dbwrapper_tests::StringContentsSerializer>(dbwrapper_tests::StringContentsSerializer&) Line | Count | Source | 161 | 150 | template<typename K> bool GetKey(K& key) { | 162 | 150 | try { | 163 | 150 | SpanReader ssKey{GetKeyImpl()}; | 164 | 150 | ssKey >> key; | 165 | 150 | } catch (const std::exception&) { | 166 | 0 | return false; | 167 | 0 | } | 168 | 150 | return true; | 169 | 150 | } |
bool CDBIterator::GetKey<index_util::DBHeightKey>(index_util::DBHeightKey&) Line | Count | Source | 161 | 3.09k | template<typename K> bool GetKey(K& key) { | 162 | 3.09k | try { | 163 | 3.09k | SpanReader ssKey{GetKeyImpl()}; | 164 | 3.09k | ssKey >> key; | 165 | 3.09k | } catch (const std::exception&) { | 166 | 0 | return false; | 167 | 0 | } | 168 | 3.09k | return true; | 169 | 3.09k | } |
bool CDBIterator::GetKey<DBKey>(DBKey&) Line | Count | Source | 161 | 28 | template<typename K> bool GetKey(K& key) { | 162 | 28 | try { | 163 | 28 | SpanReader ssKey{GetKeyImpl()}; | 164 | 28 | ssKey >> key; | 165 | 28 | } catch (const std::exception&) { | 166 | 0 | return false; | 167 | 0 | } | 168 | 28 | return true; | 169 | 28 | } |
bool CDBIterator::GetKey<std::pair<unsigned char, uint256>>(std::pair<unsigned char, uint256>&) Line | Count | Source | 161 | 133k | template<typename K> bool GetKey(K& key) { | 162 | 133k | try { | 163 | 133k | SpanReader ssKey{GetKeyImpl()}; | 164 | 133k | ssKey >> key; | 165 | 133k | } catch (const std::exception&) { | 166 | 738 | return false; | 167 | 738 | } | 168 | 133k | return true; | 169 | 133k | } |
txdb.cpp:bool CDBIterator::GetKey<(anonymous namespace)::CoinEntry>((anonymous namespace)::CoinEntry&) Line | Count | Source | 161 | 224k | template<typename K> bool GetKey(K& key) { | 162 | 224k | try { | 163 | 224k | SpanReader ssKey{GetKeyImpl()}; | 164 | 224k | ssKey >> key; | 165 | 224k | } catch (const std::exception&) { | 166 | 0 | return false; | 167 | 0 | } | 168 | 224k | return true; | 169 | 224k | } |
|
170 | | |
171 | 361k | template<typename V> bool GetValue(V& value) { |
172 | 361k | try { |
173 | 361k | ScopedDataStreamUsage scoped_scratch{m_scratch}; |
174 | 361k | m_scratch.write(GetValueImpl()); |
175 | 361k | dbwrapper_private::GetObfuscation(parent)(m_scratch); |
176 | 361k | m_scratch >> value; |
177 | 361k | } catch (const std::exception&) { |
178 | 2 | return false; |
179 | 2 | } |
180 | 361k | return true; |
181 | 361k | } bool CDBIterator::GetValue<std::pair<uint256, unsigned char>>(std::pair<uint256, unsigned char>&) Line | Count | Source | 171 | 2 | template<typename V> bool GetValue(V& value) { | 172 | 2 | try { | 173 | 2 | ScopedDataStreamUsage scoped_scratch{m_scratch}; | 174 | 2 | m_scratch.write(GetValueImpl()); | 175 | 2 | dbwrapper_private::GetObfuscation(parent)(m_scratch); | 176 | 2 | m_scratch >> value; | 177 | 2 | } catch (const std::exception&) { | 178 | 2 | return false; | 179 | 2 | } | 180 | 0 | return true; | 181 | 2 | } |
bool CDBIterator::GetValue<uint256>(uint256&) Line | Count | Source | 171 | 8 | template<typename V> bool GetValue(V& value) { | 172 | 8 | try { | 173 | 8 | ScopedDataStreamUsage scoped_scratch{m_scratch}; | 174 | 8 | m_scratch.write(GetValueImpl()); | 175 | 8 | dbwrapper_private::GetObfuscation(parent)(m_scratch); | 176 | 8 | m_scratch >> value; | 177 | 8 | } catch (const std::exception&) { | 178 | 0 | return false; | 179 | 0 | } | 180 | 8 | return true; | 181 | 8 | } |
bool CDBIterator::GetValue<unsigned int>(unsigned int&) Line | Count | Source | 171 | 342 | template<typename V> bool GetValue(V& value) { | 172 | 342 | try { | 173 | 342 | ScopedDataStreamUsage scoped_scratch{m_scratch}; | 174 | 342 | m_scratch.write(GetValueImpl()); | 175 | 342 | dbwrapper_private::GetObfuscation(parent)(m_scratch); | 176 | 342 | m_scratch >> value; | 177 | 342 | } catch (const std::exception&) { | 178 | 0 | return false; | 179 | 0 | } | 180 | 342 | return true; | 181 | 342 | } |
blockfilterindex.cpp:bool CDBIterator::GetValue<std::pair<uint256, (anonymous namespace)::DBVal>>(std::pair<uint256, (anonymous namespace)::DBVal>&) Line | Count | Source | 171 | 2.97k | template<typename V> bool GetValue(V& value) { | 172 | 2.97k | try { | 173 | 2.97k | ScopedDataStreamUsage scoped_scratch{m_scratch}; | 174 | 2.97k | m_scratch.write(GetValueImpl()); | 175 | 2.97k | dbwrapper_private::GetObfuscation(parent)(m_scratch); | 176 | 2.97k | m_scratch >> value; | 177 | 2.97k | } catch (const std::exception&) { | 178 | 0 | return false; | 179 | 0 | } | 180 | 2.97k | return true; | 181 | 2.97k | } |
coinstatsindex.cpp:bool CDBIterator::GetValue<std::pair<uint256, (anonymous namespace)::DBVal>>(std::pair<uint256, (anonymous namespace)::DBVal>&) Line | Count | Source | 171 | 121 | template<typename V> bool GetValue(V& value) { | 172 | 121 | try { | 173 | 121 | ScopedDataStreamUsage scoped_scratch{m_scratch}; | 174 | 121 | m_scratch.write(GetValueImpl()); | 175 | 121 | dbwrapper_private::GetObfuscation(parent)(m_scratch); | 176 | 121 | m_scratch >> value; | 177 | 121 | } catch (const std::exception&) { | 178 | 0 | return false; | 179 | 0 | } | 180 | 121 | return true; | 181 | 121 | } |
bool CDBIterator::GetValue<CDiskBlockIndex>(CDiskBlockIndex&) Line | Count | Source | 171 | 133k | template<typename V> bool GetValue(V& value) { | 172 | 133k | try { | 173 | 133k | ScopedDataStreamUsage scoped_scratch{m_scratch}; | 174 | 133k | m_scratch.write(GetValueImpl()); | 175 | 133k | dbwrapper_private::GetObfuscation(parent)(m_scratch); | 176 | 133k | m_scratch >> value; | 177 | 133k | } catch (const std::exception&) { | 178 | 0 | return false; | 179 | 0 | } | 180 | 133k | return true; | 181 | 133k | } |
bool CDBIterator::GetValue<Coin>(Coin&) Line | Count | Source | 171 | 224k | template<typename V> bool GetValue(V& value) { | 172 | 224k | try { | 173 | 224k | ScopedDataStreamUsage scoped_scratch{m_scratch}; | 174 | 224k | m_scratch.write(GetValueImpl()); | 175 | 224k | dbwrapper_private::GetObfuscation(parent)(m_scratch); | 176 | 224k | m_scratch >> value; | 177 | 224k | } catch (const std::exception&) { | 178 | 0 | return false; | 179 | 0 | } | 180 | 224k | return true; | 181 | 224k | } |
|
182 | | }; |
183 | | |
184 | | struct LevelDBContext; |
185 | | |
186 | | class CDBWrapper |
187 | | { |
188 | | friend const Obfuscation& dbwrapper_private::GetObfuscation(const CDBWrapper&); |
189 | | private: |
190 | | //! holds all leveldb-specific fields of this class |
191 | | std::unique_ptr<LevelDBContext> m_db_context; |
192 | | |
193 | | //! the name of this database |
194 | | std::string m_name; |
195 | | |
196 | | //! optional XOR-obfuscation of the database |
197 | | Obfuscation m_obfuscation; |
198 | | |
199 | | //! obfuscation key storage key, null-prefixed to avoid collisions |
200 | | inline static const std::string OBFUSCATION_KEY{"\000obfuscate_key", 14}; // explicit size to avoid truncation at leading \0 |
201 | | |
202 | | std::optional<std::string> ReadImpl(std::span<const std::byte> key) const; |
203 | | bool ExistsImpl(std::span<const std::byte> key) const; |
204 | | size_t EstimateSizeImpl(std::span<const std::byte> key1, std::span<const std::byte> key2) const; |
205 | 12.6M | auto& DBContext() const LIFETIMEBOUND { return *Assert(m_db_context); } |
206 | | |
207 | | public: |
208 | | CDBWrapper(const DBParams& params); |
209 | | ~CDBWrapper(); |
210 | | |
211 | | CDBWrapper(const CDBWrapper&) = delete; |
212 | | CDBWrapper& operator=(const CDBWrapper&) = delete; |
213 | | |
214 | | template <typename K, typename V> |
215 | | bool Read(const K& key, V& value) const |
216 | 6.24M | { |
217 | 6.24M | DataStream ssKey{}; |
218 | 6.24M | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); |
219 | 6.24M | ssKey << key; |
220 | 6.24M | std::optional<std::string> strValue{ReadImpl(ssKey)}; |
221 | 6.24M | if (!strValue) { |
222 | 6.13M | return false; |
223 | 6.13M | } |
224 | 110k | try { |
225 | 110k | std::span ssValue{MakeWritableByteSpan(*strValue)}; |
226 | 110k | m_obfuscation(ssValue); |
227 | 110k | SpanReader{ssValue} >> value; |
228 | 110k | } catch (const std::exception&) { |
229 | 0 | return false; |
230 | 0 | } |
231 | 110k | return true; |
232 | 110k | } bool CDBWrapper::Read<unsigned char, uint256>(unsigned char const&, uint256&) const Line | Count | Source | 216 | 7.70k | { | 217 | 7.70k | DataStream ssKey{}; | 218 | 7.70k | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 7.70k | ssKey << key; | 220 | 7.70k | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 7.70k | if (!strValue) { | 222 | 1.71k | return false; | 223 | 1.71k | } | 224 | 5.98k | try { | 225 | 5.98k | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 5.98k | m_obfuscation(ssValue); | 227 | 5.98k | SpanReader{ssValue} >> value; | 228 | 5.98k | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 5.98k | return true; | 232 | 5.98k | } |
bool CDBWrapper::Read<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, uint256>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, uint256&) const Line | Count | Source | 216 | 8 | { | 217 | 8 | DataStream ssKey{}; | 218 | 8 | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 8 | ssKey << key; | 220 | 8 | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 8 | if (!strValue) { | 222 | 0 | return false; | 223 | 0 | } | 224 | 8 | try { | 225 | 8 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 8 | m_obfuscation(ssValue); | 227 | 8 | SpanReader{ssValue} >> value; | 228 | 8 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 8 | return true; | 232 | 8 | } |
bool CDBWrapper::Read<unsigned char, unsigned int>(unsigned char const&, unsigned int&) const Line | Count | Source | 216 | 2 | { | 217 | 2 | DataStream ssKey{}; | 218 | 2 | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 2 | ssKey << key; | 220 | 2 | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 2 | if (!strValue) { | 222 | 0 | return false; | 223 | 0 | } | 224 | 2 | try { | 225 | 2 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 2 | m_obfuscation(ssValue); | 227 | 2 | SpanReader{ssValue} >> value; | 228 | 2 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 2 | return true; | 232 | 2 | } |
bool CDBWrapper::Read<unsigned char, bool>(unsigned char const&, bool&) const Line | Count | Source | 216 | 2 | { | 217 | 2 | DataStream ssKey{}; | 218 | 2 | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 2 | ssKey << key; | 220 | 2 | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 2 | if (!strValue) { | 222 | 0 | return false; | 223 | 0 | } | 224 | 2 | try { | 225 | 2 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 2 | m_obfuscation(ssValue); | 227 | 2 | SpanReader{ssValue} >> value; | 228 | 2 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 2 | return true; | 232 | 2 | } |
bool CDBWrapper::Read<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool&) const Line | Count | Source | 216 | 2 | { | 217 | 2 | DataStream ssKey{}; | 218 | 2 | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 2 | ssKey << key; | 220 | 2 | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 2 | if (!strValue) { | 222 | 0 | return false; | 223 | 0 | } | 224 | 2 | try { | 225 | 2 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 2 | m_obfuscation(ssValue); | 227 | 2 | SpanReader{ssValue} >> value; | 228 | 2 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 2 | return true; | 232 | 2 | } |
bool CDBWrapper::Read<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Obfuscation>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, Obfuscation&) const Line | Count | Source | 216 | 2.73k | { | 217 | 2.73k | DataStream ssKey{}; | 218 | 2.73k | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 2.73k | ssKey << key; | 220 | 2.73k | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 2.73k | if (!strValue) { | 222 | 1.88k | return false; | 223 | 1.88k | } | 224 | 851 | try { | 225 | 851 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 851 | m_obfuscation(ssValue); | 227 | 851 | SpanReader{ssValue} >> value; | 228 | 851 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 850 | return true; | 232 | 851 | } |
bool CDBWrapper::Read<unsigned char, CBlockLocator>(unsigned char const&, CBlockLocator&) const Line | Count | Source | 216 | 145 | { | 217 | 145 | DataStream ssKey{}; | 218 | 145 | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 145 | ssKey << key; | 220 | 145 | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 145 | if (!strValue) { | 222 | 50 | return false; | 223 | 50 | } | 224 | 95 | try { | 225 | 95 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 95 | m_obfuscation(ssValue); | 227 | 95 | SpanReader{ssValue} >> value; | 228 | 95 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 95 | return true; | 232 | 95 | } |
blockfilterindex.cpp:bool CDBWrapper::Read<index_util::DBHashKey, (anonymous namespace)::DBVal>(index_util::DBHashKey const&, (anonymous namespace)::DBVal&) const Line | Count | Source | 216 | 29 | { | 217 | 29 | DataStream ssKey{}; | 218 | 29 | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 29 | ssKey << key; | 220 | 29 | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 29 | if (!strValue) { | 222 | 0 | return false; | 223 | 0 | } | 224 | 29 | try { | 225 | 29 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 29 | m_obfuscation(ssValue); | 227 | 29 | SpanReader{ssValue} >> value; | 228 | 29 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 29 | return true; | 232 | 29 | } |
bool CDBWrapper::Read<unsigned char, FlatFilePos>(unsigned char const&, FlatFilePos&) const Line | Count | Source | 216 | 42 | { | 217 | 42 | DataStream ssKey{}; | 218 | 42 | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 42 | ssKey << key; | 220 | 42 | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 42 | if (!strValue) { | 222 | 16 | return false; | 223 | 16 | } | 224 | 26 | try { | 225 | 26 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 26 | m_obfuscation(ssValue); | 227 | 26 | SpanReader{ssValue} >> value; | 228 | 26 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 26 | return true; | 232 | 26 | } |
blockfilterindex.cpp:bool CDBWrapper::Read<index_util::DBHeightKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHeightKey const&, std::pair<uint256, (anonymous namespace)::DBVal>&) const Line | Count | Source | 216 | 1.21k | { | 217 | 1.21k | DataStream ssKey{}; | 218 | 1.21k | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 1.21k | ssKey << key; | 220 | 1.21k | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 1.21k | if (!strValue) { | 222 | 202 | return false; | 223 | 202 | } | 224 | 1.01k | try { | 225 | 1.01k | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 1.01k | m_obfuscation(ssValue); | 227 | 1.01k | SpanReader{ssValue} >> value; | 228 | 1.01k | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 1.01k | return true; | 232 | 1.01k | } |
coinstatsindex.cpp:bool CDBWrapper::Read<index_util::DBHashKey, (anonymous namespace)::DBVal>(index_util::DBHashKey const&, (anonymous namespace)::DBVal&) const Line | Count | Source | 216 | 2 | { | 217 | 2 | DataStream ssKey{}; | 218 | 2 | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 2 | ssKey << key; | 220 | 2 | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 2 | if (!strValue) { | 222 | 0 | return false; | 223 | 0 | } | 224 | 2 | try { | 225 | 2 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 2 | m_obfuscation(ssValue); | 227 | 2 | SpanReader{ssValue} >> value; | 228 | 2 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 2 | return true; | 232 | 2 | } |
bool CDBWrapper::Read<unsigned char, MuHash3072>(unsigned char const&, MuHash3072&) const Line | Count | Source | 216 | 45 | { | 217 | 45 | DataStream ssKey{}; | 218 | 45 | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 45 | ssKey << key; | 220 | 45 | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 45 | if (!strValue) { | 222 | 15 | return false; | 223 | 15 | } | 224 | 30 | try { | 225 | 30 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 30 | m_obfuscation(ssValue); | 227 | 30 | SpanReader{ssValue} >> value; | 228 | 30 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 30 | return true; | 232 | 30 | } |
coinstatsindex.cpp:bool CDBWrapper::Read<index_util::DBHeightKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHeightKey const&, std::pair<uint256, (anonymous namespace)::DBVal>&) const Line | Count | Source | 216 | 221 | { | 217 | 221 | DataStream ssKey{}; | 218 | 221 | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 221 | ssKey << key; | 220 | 221 | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 221 | if (!strValue) { | 222 | 1 | return false; | 223 | 1 | } | 224 | 220 | try { | 225 | 220 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 220 | m_obfuscation(ssValue); | 227 | 220 | SpanReader{ssValue} >> value; | 228 | 220 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 220 | return true; | 232 | 220 | } |
Unexecuted instantiation: coinstatsindex.cpp:bool CDBWrapper::Read<index_util::DBHashKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHashKey const&, std::pair<uint256, (anonymous namespace)::DBVal>&) const bool CDBWrapper::Read<std::pair<unsigned char, uint256>, CDiskTxPos>(std::pair<unsigned char, uint256> const&, CDiskTxPos&) const Line | Count | Source | 216 | 243 | { | 217 | 243 | DataStream ssKey{}; | 218 | 243 | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 243 | ssKey << key; | 220 | 243 | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 243 | if (!strValue) { | 222 | 101 | return false; | 223 | 101 | } | 224 | 142 | try { | 225 | 142 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 142 | m_obfuscation(ssValue); | 227 | 142 | SpanReader{ssValue} >> value; | 228 | 142 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 142 | return true; | 232 | 142 | } |
bool CDBWrapper::Read<char [12], std::pair<unsigned long, unsigned long>>(char const (&) [12], std::pair<unsigned long, unsigned long>&) const Line | Count | Source | 216 | 22 | { | 217 | 22 | DataStream ssKey{}; | 218 | 22 | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 22 | ssKey << key; | 220 | 22 | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 22 | if (!strValue) { | 222 | 6 | return false; | 223 | 6 | } | 224 | 16 | try { | 225 | 16 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 16 | m_obfuscation(ssValue); | 227 | 16 | SpanReader{ssValue} >> value; | 228 | 16 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 16 | return true; | 232 | 16 | } |
bool CDBWrapper::Read<std::pair<unsigned char, int>, kernel::CBlockFileInfo>(std::pair<unsigned char, int> const&, kernel::CBlockFileInfo&) const Line | Count | Source | 216 | 2.38k | { | 217 | 2.38k | DataStream ssKey{}; | 218 | 2.38k | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 2.38k | ssKey << key; | 220 | 2.38k | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 2.38k | if (!strValue) { | 222 | 1.63k | return false; | 223 | 1.63k | } | 224 | 752 | try { | 225 | 752 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 752 | m_obfuscation(ssValue); | 227 | 752 | SpanReader{ssValue} >> value; | 228 | 752 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 752 | return true; | 232 | 752 | } |
bool CDBWrapper::Read<unsigned char, int>(unsigned char const&, int&) const Line | Count | Source | 216 | 1.18k | { | 217 | 1.18k | DataStream ssKey{}; | 218 | 1.18k | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 1.18k | ssKey << key; | 220 | 1.18k | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 1.18k | if (!strValue) { | 222 | 448 | return false; | 223 | 448 | } | 224 | 736 | try { | 225 | 736 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 736 | m_obfuscation(ssValue); | 227 | 736 | SpanReader{ssValue} >> value; | 228 | 736 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 736 | return true; | 232 | 736 | } |
bool CDBWrapper::Read<std::pair<unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, unsigned char>(std::pair<unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> const&, unsigned char&) const Line | Count | Source | 216 | 1.18k | { | 217 | 1.18k | DataStream ssKey{}; | 218 | 1.18k | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 1.18k | ssKey << key; | 220 | 1.18k | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 1.18k | if (!strValue) { | 222 | 1.18k | return false; | 223 | 1.18k | } | 224 | 1 | try { | 225 | 1 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 1 | m_obfuscation(ssValue); | 227 | 1 | SpanReader{ssValue} >> value; | 228 | 1 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 1 | return true; | 232 | 1 | } |
txdb.cpp:bool CDBWrapper::Read<(anonymous namespace)::CoinEntry, Coin>((anonymous namespace)::CoinEntry const&, Coin&) const Line | Count | Source | 216 | 6.23M | { | 217 | 6.23M | DataStream ssKey{}; | 218 | 6.23M | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 6.23M | ssKey << key; | 220 | 6.23M | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 6.23M | if (!strValue) { | 222 | 6.12M | return false; | 223 | 6.12M | } | 224 | 100k | try { | 225 | 100k | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 100k | m_obfuscation(ssValue); | 227 | 100k | SpanReader{ssValue} >> value; | 228 | 100k | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 100k | return true; | 232 | 100k | } |
bool CDBWrapper::Read<unsigned char, std::vector<uint256, std::allocator<uint256>>>(unsigned char const&, std::vector<uint256, std::allocator<uint256>>&) const Line | Count | Source | 216 | 1.55k | { | 217 | 1.55k | DataStream ssKey{}; | 218 | 1.55k | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 219 | 1.55k | ssKey << key; | 220 | 1.55k | std::optional<std::string> strValue{ReadImpl(ssKey)}; | 221 | 1.55k | if (!strValue) { | 222 | 1.55k | return false; | 223 | 1.55k | } | 224 | 0 | try { | 225 | 0 | std::span ssValue{MakeWritableByteSpan(*strValue)}; | 226 | 0 | m_obfuscation(ssValue); | 227 | 0 | SpanReader{ssValue} >> value; | 228 | 0 | } catch (const std::exception&) { | 229 | 0 | return false; | 230 | 0 | } | 231 | 0 | return true; | 232 | 0 | } |
|
233 | | |
234 | | template <typename K, typename V> |
235 | | void Write(const K& key, const V& value, bool fSync = false) |
236 | 12.3k | { |
237 | 12.3k | CDBBatch batch(*this); |
238 | 12.3k | batch.Write(key, value); |
239 | 12.3k | WriteBatch(batch, fSync); |
240 | 12.3k | } void CDBWrapper::Write<unsigned char, uint256>(unsigned char const&, uint256 const&, bool) Line | Count | Source | 236 | 30 | { | 237 | 30 | CDBBatch batch(*this); | 238 | 30 | batch.Write(key, value); | 239 | 30 | WriteBatch(batch, fSync); | 240 | 30 | } |
void CDBWrapper::Write<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, uint256>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, uint256 const&, bool) Line | Count | Source | 236 | 8 | { | 237 | 8 | CDBBatch batch(*this); | 238 | 8 | batch.Write(key, value); | 239 | 8 | WriteBatch(batch, fSync); | 240 | 8 | } |
void CDBWrapper::Write<unsigned char, unsigned int>(unsigned char const&, unsigned int const&, bool) Line | Count | Source | 236 | 258 | { | 237 | 258 | CDBBatch batch(*this); | 238 | 258 | batch.Write(key, value); | 239 | 258 | WriteBatch(batch, fSync); | 240 | 258 | } |
void CDBWrapper::Write<unsigned char, bool>(unsigned char const&, bool const&, bool) Line | Count | Source | 236 | 2 | { | 237 | 2 | CDBBatch batch(*this); | 238 | 2 | batch.Write(key, value); | 239 | 2 | WriteBatch(batch, fSync); | 240 | 2 | } |
void CDBWrapper::Write<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool const&, bool) Line | Count | Source | 236 | 2 | { | 237 | 2 | CDBBatch batch(*this); | 238 | 2 | batch.Write(key, value); | 239 | 2 | WriteBatch(batch, fSync); | 240 | 2 | } |
void CDBWrapper::Write<dbwrapper_tests::StringContentsSerializer, unsigned int>(dbwrapper_tests::StringContentsSerializer const&, unsigned int const&, bool) Line | Count | Source | 236 | 100 | { | 237 | 100 | CDBBatch batch(*this); | 238 | 100 | batch.Write(key, value); | 239 | 100 | WriteBatch(batch, fSync); | 240 | 100 | } |
void CDBWrapper::Write<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Obfuscation>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, Obfuscation const&, bool) Line | Count | Source | 236 | 513 | { | 237 | 513 | CDBBatch batch(*this); | 238 | 513 | batch.Write(key, value); | 239 | 513 | WriteBatch(batch, fSync); | 240 | 513 | } |
blockfilterindex.cpp:void CDBWrapper::Write<index_util::DBHeightKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHeightKey const&, std::pair<uint256, (anonymous namespace)::DBVal> const&, bool) Line | Count | Source | 236 | 7.54k | { | 237 | 7.54k | CDBBatch batch(*this); | 238 | 7.54k | batch.Write(key, value); | 239 | 7.54k | WriteBatch(batch, fSync); | 240 | 7.54k | } |
coinstatsindex.cpp:void CDBWrapper::Write<index_util::DBHeightKey, std::pair<uint256, (anonymous namespace)::DBVal>>(index_util::DBHeightKey const&, std::pair<uint256, (anonymous namespace)::DBVal> const&, bool) Line | Count | Source | 236 | 3.86k | { | 237 | 3.86k | CDBBatch batch(*this); | 238 | 3.86k | batch.Write(key, value); | 239 | 3.86k | WriteBatch(batch, fSync); | 240 | 3.86k | } |
void CDBWrapper::Write<char [12], std::pair<unsigned long, unsigned long>>(char const (&) [12], std::pair<unsigned long, unsigned long> const&, bool) Line | Count | Source | 236 | 6 | { | 237 | 6 | CDBBatch batch(*this); | 238 | 6 | batch.Write(key, value); | 239 | 6 | WriteBatch(batch, fSync); | 240 | 6 | } |
void CDBWrapper::Write<unsigned char, unsigned char>(unsigned char const&, unsigned char const&, bool) Line | Count | Source | 236 | 14 | { | 237 | 14 | CDBBatch batch(*this); | 238 | 14 | batch.Write(key, value); | 239 | 14 | WriteBatch(batch, fSync); | 240 | 14 | } |
void CDBWrapper::Write<std::pair<unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, unsigned char>(std::pair<unsigned char, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> const&, unsigned char const&, bool) Line | Count | Source | 236 | 7 | { | 237 | 7 | CDBBatch batch(*this); | 238 | 7 | batch.Write(key, value); | 239 | 7 | WriteBatch(batch, fSync); | 240 | 7 | } |
|
241 | | |
242 | | template <typename K> |
243 | | bool Exists(const K& key) const |
244 | 1.25k | { |
245 | 1.25k | DataStream ssKey{}; |
246 | 1.25k | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); |
247 | 1.25k | ssKey << key; |
248 | 1.25k | return ExistsImpl(ssKey); |
249 | 1.25k | } bool CDBWrapper::Exists<unsigned char>(unsigned char const&) const Line | Count | Source | 244 | 1.21k | { | 245 | 1.21k | DataStream ssKey{}; | 246 | 1.21k | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 247 | 1.21k | ssKey << key; | 248 | 1.21k | return ExistsImpl(ssKey); | 249 | 1.21k | } |
txdb.cpp:bool CDBWrapper::Exists<(anonymous namespace)::CoinEntry>((anonymous namespace)::CoinEntry const&) const Line | Count | Source | 244 | 44 | { | 245 | 44 | DataStream ssKey{}; | 246 | 44 | ssKey.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); | 247 | 44 | ssKey << key; | 248 | 44 | return ExistsImpl(ssKey); | 249 | 44 | } |
|
250 | | |
251 | | template <typename K> |
252 | | void Erase(const K& key, bool fSync = false) |
253 | 13 | { |
254 | 13 | CDBBatch batch(*this); |
255 | 13 | batch.Erase(key); |
256 | 13 | WriteBatch(batch, fSync); |
257 | 13 | } |
258 | | |
259 | | void WriteBatch(CDBBatch& batch, bool fSync = false); |
260 | | |
261 | | // Get an estimate of LevelDB memory usage (in bytes). |
262 | | size_t DynamicMemoryUsage() const; |
263 | | |
264 | | CDBIterator* NewIterator(); |
265 | | |
266 | | /** |
267 | | * Return true if the database managed by this class contains no entries. |
268 | | */ |
269 | | bool IsEmpty(); |
270 | | |
271 | | template<typename K> |
272 | | size_t EstimateSize(const K& key_begin, const K& key_end) const |
273 | 102 | { |
274 | 102 | DataStream ssKey1{}, ssKey2{}; |
275 | 102 | ssKey1.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); |
276 | 102 | ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); |
277 | 102 | ssKey1 << key_begin; |
278 | 102 | ssKey2 << key_end; |
279 | 102 | return EstimateSizeImpl(ssKey1, ssKey2); |
280 | 102 | } |
281 | | }; |
282 | | |
283 | | #endif // BITCOIN_DBWRAPPER_H |