Skip to content

Commit d1ed3f8

Browse files
committed
Style, comments.
1 parent fcd6bcb commit d1ed3f8

12 files changed

Lines changed: 63 additions & 64 deletions

File tree

include/bitcoin/database/impl/primitives/arrayhead.ipp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ inline size_t CLASS::size() const NOEXCEPT
4141
TEMPLATE
4242
inline size_t CLASS::buckets() const NOEXCEPT
4343
{
44-
const auto count = position_to_link(size()).value;
45-
BC_ASSERT(count < Link::terminal);
46-
return system::possible_narrow_cast<size_t>(count);
44+
using namespace system;
45+
return possible_narrow_cast<size_t>(position_to_link(size()).value);
4746
}
4847

4948
TEMPLATE
@@ -99,7 +98,7 @@ TEMPLATE
9998
bool CLASS::get_body_count(Link& count) const NOEXCEPT
10099
{
101100
const auto ptr = file_.get();
102-
if (!ptr || size_ > size())
101+
if (!ptr || bucket_size > size())
103102
return false;
104103

105104
count = to_array<Link::size>(ptr->data());
@@ -110,7 +109,7 @@ TEMPLATE
110109
bool CLASS::set_body_count(const Link& count) NOEXCEPT
111110
{
112111
const auto ptr = file_.get();
113-
if (!ptr || size_ > size())
112+
if (!ptr || bucket_size > size())
114113
return false;
115114

116115
// If head is padded then last bytes are fill (0xff).
@@ -135,13 +134,13 @@ Link CLASS::at(size_t key) const NOEXCEPT
135134
// Reads full padded word.
136135
const auto raw = ptr->data();
137136
// xcode clang++16 does not support C++20 std::atomic_ref.
138-
////const std::atomic_ref<integer> head(unsafe_byte_cast<integer>(raw));
139-
const auto& head = *pointer_cast<std::atomic<integer>>(raw);
137+
////const std::atomic_ref<Link::integer> head(unsafe_byte_cast<Link::integer>(raw));
138+
const auto& head = *pointer_cast<std::atomic<Link::integer>>(raw);
140139
return head.load(std::memory_order_relaxed);
141140
}
142141
else
143142
{
144-
const auto& head = to_array<size_>(ptr->data());
143+
const auto& head = to_array<bucket_size>(ptr->data());
145144
mutex_.lock_shared();
146145
const auto top = head;
147146
mutex_.unlock_shared();
@@ -156,7 +155,7 @@ bool CLASS::push(const Link& link, const Link& index) NOEXCEPT
156155
constexpr auto fill = bit_all<uint8_t>;
157156

158157
// Allocate as necessary and fill allocations.
159-
const auto ptr = file_.set(link_to_position(index), size_, fill);
158+
const auto ptr = file_.set(link_to_position(index), bucket_size, fill);
160159
if (is_null(ptr))
161160
return false;
162161

@@ -165,14 +164,14 @@ bool CLASS::push(const Link& link, const Link& index) NOEXCEPT
165164
// Writes full padded word (0x00 fill).
166165
const auto raw = ptr->data();
167166
// xcode clang++16 does not support C++20 std::atomic_ref.
168-
////const std::atomic_ref<integer> head(unsafe_byte_cast<integer>(raw));
169-
auto& head = *pointer_cast<std::atomic<integer>>(raw);
167+
////const std::atomic_ref<Link::integer> head(unsafe_byte_cast<Link::integer>(raw));
168+
auto& head = *pointer_cast<std::atomic<Link::integer>>(raw);
170169
head.store(link, std::memory_order_relaxed);
171170
}
172171
else
173172
{
174173
bytes current = link;
175-
auto& head = to_array<size_>(ptr->data());
174+
auto& head = to_array<bucket_size>(ptr->data());
176175

177176
mutex_.lock();
178177
head = std::move(current);

include/bitcoin/database/impl/primitives/arraymap.ipp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,21 @@ inline Link CLASS::at(size_t key) const NOEXCEPT
149149
}
150150

151151
TEMPLATE
152-
template <typename Element, if_equal<Element::size, Size>>
152+
ELEMENT_CONSTRAINT
153153
inline bool CLASS::at(size_t key, Element& element) const NOEXCEPT
154154
{
155155
return get(at(key), element);
156156
}
157157

158158
TEMPLATE
159-
template <typename Element, if_equal<Element::size, Size>>
159+
ELEMENT_CONSTRAINT
160160
inline bool CLASS::get(const Link& link, Element& element) const NOEXCEPT
161161
{
162162
return read(body_.get(), link, element);
163163
}
164164

165165
TEMPLATE
166-
template <typename Element, if_equal<Element::size, Size>>
166+
ELEMENT_CONSTRAINT
167167
bool CLASS::put(size_t key, const Element& element) NOEXCEPT
168168
{
169169
using namespace system;
@@ -176,15 +176,16 @@ bool CLASS::put(size_t key, const Element& element) NOEXCEPT
176176
iostream stream{ *ptr };
177177
finalizer sink{ stream };
178178

179-
if constexpr (!is_slab) { BC_DEBUG_ONLY(sink.set_limit(Size * element.count());) }
179+
if constexpr (!is_slab) { BC_DEBUG_ONLY(sink.set_limit(RowSize * element.count());) }
180180
return element.to_data(sink) && head_.push(link, head_.index(key));
181181
}
182182

183183
// protected
184184
// ----------------------------------------------------------------------------
185185

186+
// static
186187
TEMPLATE
187-
template <typename Element, if_equal<Element::size, Size>>
188+
ELEMENT_CONSTRAINT
188189
bool CLASS::read(const memory_ptr& ptr, const Link& link,
189190
Element& element) NOEXCEPT
190191
{
@@ -209,7 +210,7 @@ bool CLASS::read(const memory_ptr& ptr, const Link& link,
209210
iostream stream{ offset, size - position };
210211
reader source{ stream };
211212

212-
if constexpr (!is_slab) { BC_DEBUG_ONLY(source.set_limit(Size * element.count());) }
213+
if constexpr (!is_slab) { BC_DEBUG_ONLY(source.set_limit(RowSize * element.count());) }
213214
return element.from_data(source);
214215
}
215216

include/bitcoin/database/impl/primitives/hashhead.ipp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ CLASS::hashhead(storage& head, size_t bits) NOEXCEPT
3535
buckets_(system::power2<bucket_integer>(bits)),
3636
mask_(system::unmask_right<bucket_integer>(bits))
3737
{
38+
BC_ASSERT_MSG(mask_ < max_size_t, "insufficient domain");
3839
}
3940

4041
TEMPLATE
@@ -107,9 +108,6 @@ TEMPLATE
107108
inline Link CLASS::index(const Key& key) const NOEXCEPT
108109
{
109110
using namespace system;
110-
BC_ASSERT_MSG(mask_ < max_size_t, "insufficient domain");
111-
BC_ASSERT_MSG(is_nonzero(buckets_), "hash table requires buckets");
112-
113111
const auto index = possible_narrow_cast<bucket_integer>(keys::hash<Key>(key));
114112
return bit_and<bucket_integer>(mask_, index);
115113
}

include/bitcoin/database/impl/primitives/hashmap.ipp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ bool CLASS::set(const memory_ptr& ptr, const Link& link, const Key& key,
292292
finalizer sink{ stream };
293293
sink.skip_bytes(index_size);
294294

295-
if constexpr (!is_slab) { BC_DEBUG_ONLY(sink.set_limit(Size * element.count());) }
295+
if constexpr (!is_slab) { BC_DEBUG_ONLY(sink.set_limit(RowSize * element.count());) }
296296
return element.to_data(sink);
297297
}
298298

@@ -507,7 +507,7 @@ bool CLASS::write(const memory_ptr& ptr, const Link& link, const Key& key,
507507
keys::write(sink, key);
508508

509509
// Commit element to body and search (terminal is a valid bucket index).
510-
if constexpr (!is_slab) { BC_DEBUG_ONLY(sink.set_limit(Size * element.count());) }
510+
if constexpr (!is_slab) { BC_DEBUG_ONLY(sink.set_limit(RowSize * element.count());) }
511511
auto& next = unsafe_array_cast<uint8_t, Link::size>(offset);
512512
return element.to_data(sink) && head_.push(link, next, head_.index(key));
513513
}

include/bitcoin/database/impl/query/archive_write.ipp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,12 @@ code CLASS::set_code(const tx_link& tx_fk, const transaction& tx) NOEXCEPT
147147
if (!store_.point.expand(ins_fk + inputs))
148148
return error::tx_point_allocate;
149149

150-
// This must be set after tx.set and before tx.commit, since searchable and
151-
// produces an association to tx.link, and is also an integral part of tx.
152-
const auto ptr = store_.point.get_memory();
153-
154150
// Collect duplicates to store in duplicate table.
155151
std::vector<chain::cref_point> twins{};
152+
const auto ptr = store_.point.get_memory();
153+
154+
// This must be set after tx.set and before tx.commit, since searchable and
155+
// produces an association to tx.link, and is also an integral part of tx.
156156
for (const auto& in: *ins)
157157
{
158158
///////////////////////////////////////////////////////////////////

include/bitcoin/database/primitives/arrayhead.hpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace database {
3030

3131
/// Dynamically expanding array map header.
3232
/// Less efficient than a fixed-size header.
33-
template <typename Link, bool Align>
33+
template <class Link, bool Align>
3434
class arrayhead
3535
{
3636
public:
@@ -71,12 +71,11 @@ class arrayhead
7171
bool push(const Link& link, const Link& index) NOEXCEPT;
7272

7373
private:
74-
using integer = Link::integer;
75-
static_assert(std::atomic<integer>::is_always_lock_free);
76-
static constexpr auto size_ = Align ? sizeof(integer) : Link::size;
77-
78-
// Body does not use padded link size.
7974
using body = manager<Link, system::data_array<zero>, Link::size>;
75+
static_assert(std::atomic<Link::integer>::is_always_lock_free);
76+
static_assert(is_nonzero(Link::size));
77+
static constexpr auto bucket_size = Align ? sizeof(Link::integer) :
78+
Link::size;
8079

8180
template <size_t Bytes>
8281
static inline auto& to_array(memory::iterator it) NOEXCEPT
@@ -87,19 +86,19 @@ class arrayhead
8786
static constexpr Link position_to_link(size_t position) NOEXCEPT
8887
{
8988
using namespace system;
90-
static_assert(is_nonzero(size_));
91-
const auto link = floored_subtract(position / size_, one);
92-
return possible_narrow_cast<integer>(link);
89+
const auto offset = floored_divide(position, bucket_size);
90+
const auto link = floored_subtract(offset, one);
91+
return possible_narrow_cast<Link::integer>(link);
9392
}
9493

9594
// Byte offset of bucket index within head file.
9695
// [body_size][[bucket[0]...bucket[buckets-1]]]
9796
static constexpr size_t link_to_position(const Link& index) NOEXCEPT
9897
{
9998
using namespace system;
100-
BC_ASSERT(!is_multiply_overflow<size_t>(index, size_));
101-
BC_ASSERT(!is_add_overflow(size_, index * size_));
102-
return possible_narrow_cast<size_t>(size_ + index * size_);
99+
BC_ASSERT(!is_multiply_overflow<size_t>(index, bucket_size));
100+
BC_ASSERT(!is_add_overflow(bucket_size, index * bucket_size));
101+
return possible_narrow_cast<size_t>(add1(index) * bucket_size);
103102
}
104103

105104
// These are thread safe.
@@ -111,7 +110,7 @@ class arrayhead
111110
} // namespace database
112111
} // namespace libbitcoin
113112

114-
#define TEMPLATE template <typename Link, bool Align>
113+
#define TEMPLATE template <class Link, bool Align>
115114
#define CLASS arrayhead<Link, Align>
116115

117116
#include <bitcoin/database/impl/primitives/arrayhead.ipp>

include/bitcoin/database/primitives/arraymap.hpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace database {
3535
/// Readers and writers are always prepositioned at data, and are limited to
3636
/// the extent the record/slab size is known (limit can always be removed).
3737
/// Streams are always initialized from first element byte up to file limit.
38-
template <typename Link, size_t Size, bool Align>
38+
template <class Link, size_t RowSize, bool Align>
3939
class arraymap
4040
{
4141
public:
@@ -95,29 +95,28 @@ class arraymap
9595
inline Link at(size_t key) const NOEXCEPT;
9696

9797
/// Get first element matching key, false if not found/error (unverified).
98-
template <typename Element, if_equal<Element::size, Size> = true>
98+
template <typename Element, if_equal<Element::size, RowSize> = true>
9999
inline bool at(size_t key, Element& element) const NOEXCEPT;
100100

101101
/// Get element at link, false if deserialize error.
102-
template <typename Element, if_equal<Element::size, Size> = true>
102+
template <typename Element, if_equal<Element::size, RowSize> = true>
103103
inline bool get(const Link& link, Element& element) const NOEXCEPT;
104104

105105
/// Allocate, set, commit element to key.
106106
/// Expands table AND HEADER as necessary.
107-
template <typename Element, if_equal<Element::size, Size> = true>
107+
template <typename Element, if_equal<Element::size, RowSize> = true>
108108
bool put(size_t key, const Element& element) NOEXCEPT;
109109

110110
protected:
111111
/// Get element at link using memory object, false if deserialize error.
112-
template <typename Element, if_equal<Element::size, Size> = true>
112+
template <typename Element, if_equal<Element::size, RowSize> = true>
113113
static bool read(const memory_ptr& ptr, const Link& link,
114114
Element& element) NOEXCEPT;
115115

116116
private:
117-
static constexpr auto is_slab = (Size == max_size_t);
118-
117+
static constexpr auto is_slab = (RowSize == max_size_t);
119118
using head = database::arrayhead<Link, Align>;
120-
using body = database::manager<Link, system::data_array<0>, Size>;
119+
using body = database::manager<Link, system::data_array<0>, RowSize>;
121120

122121
// Thread safe (index/top/push).
123122
// Not thread safe (create/open/close/backup/restore).
@@ -133,8 +132,10 @@ using array_map = arraymap<linkage<Element::pk>, Element::size, Element::align>;
133132
} // namespace database
134133
} // namespace libbitcoin
135134

136-
#define TEMPLATE template <typename Link, size_t Size, bool Align>
137-
#define CLASS arraymap<Link, Size, Align>
135+
#define TEMPLATE template <class Link, size_t RowSize, bool Align>
136+
#define CLASS arraymap<Link, RowSize, Align>
137+
#define ELEMENT_CONSTRAINT template <class Element, \
138+
if_equal<Element::size, RowSize>>
138139

139140
#include <bitcoin/database/impl/primitives/arraymap.ipp>
140141

include/bitcoin/database/primitives/hashhead.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ class hashhead
7575
using bucket_integer = Link::integer;
7676
using cell_integer = unsigned_type<CellSize>;
7777
static_assert(std::atomic<cell_integer>::is_always_lock_free);
78-
static constexpr auto aligned = (CellSize == sizeof(cell_integer));
78+
static_assert(is_nonzero(Link::size));
7979
static constexpr auto bucket_size = Link::size;
8080
static constexpr auto filter_size = CellSize - bucket_size;
81+
static constexpr auto aligned = (CellSize == sizeof(cell_integer));
8182

8283
template <size_t Bytes>
8384
static inline auto& to_array(memory::iterator it) NOEXCEPT
@@ -91,8 +92,8 @@ class hashhead
9192
{
9293
using namespace system;
9394
BC_ASSERT(!is_multiply_overflow<size_t>(index, CellSize));
94-
BC_ASSERT(!is_add_overflow(size_, index * CellSize));
95-
return possible_narrow_cast<size_t>(CellSize + index * CellSize);
95+
BC_ASSERT(!is_add_overflow(CellSize, index * CellSize));
96+
return possible_narrow_cast<size_t>(add1(index) * CellSize);
9697
}
9798

9899
// These are thread safe.

include/bitcoin/database/primitives/iterator.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace database {
4141

4242
/// This class is not thread safe.
4343
/// Size non-max implies record manager (ordinal record links).
44-
template <typename Link, typename Key, size_t Size = max_size_t>
44+
template <class Link, class Key, size_t Size = max_size_t>
4545
class iterator
4646
{
4747
public:
@@ -93,7 +93,7 @@ class iterator
9393
} // namespace libbitcoin
9494

9595
#define TEMPLATE \
96-
template <typename Link, typename Key, size_t Size>
96+
template <class Link, class Key, size_t Size>
9797
#define CLASS iterator<Link, Key, Size>
9898

9999
#include <bitcoin/database/impl/primitives/iterator.ipp>

include/bitcoin/database/primitives/keys.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace libbitcoin {
2525
namespace database {
2626
namespace keys {
2727

28-
template <typename Key>
28+
template <class Key>
2929
constexpr size_t size() NOEXCEPT
3030
{
3131
if constexpr (is_same_type<Key, system::chain::point>)
@@ -39,7 +39,7 @@ constexpr size_t size() NOEXCEPT
3939
}
4040
}
4141

42-
template <typename Key>
42+
template <class Key>
4343
inline size_t hash(const Key& value) NOEXCEPT
4444
{
4545
if constexpr (is_same_type<Key, system::chain::point>)
@@ -56,7 +56,7 @@ inline size_t hash(const Key& value) NOEXCEPT
5656
}
5757
}
5858

59-
template <typename Key>
59+
template <class Key>
6060
inline void write(writer& sink, const Key& key) NOEXCEPT
6161
{
6262
if constexpr (is_same_type<Key, system::chain::point>)
@@ -70,7 +70,7 @@ inline void write(writer& sink, const Key& key) NOEXCEPT
7070
}
7171
}
7272

73-
template <typename Array, typename Key>
73+
template <class Array, class Key>
7474
inline bool compare(const Array& bytes, const Key& key) NOEXCEPT
7575
{
7676
using namespace system;

0 commit comments

Comments
 (0)