Skip to content

Commit a235a4d

Browse files
committed
Add nomap::expand method and dependencies.
1 parent 4bcffa1 commit a235a4d

11 files changed

Lines changed: 87 additions & 4 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ bool CLASS::truncate(const Link& count) NOEXCEPT
5252
return file_.truncate(link_to_position(count));
5353
}
5454

55+
TEMPLATE
56+
bool CLASS::expand(const Link& count) NOEXCEPT
57+
{
58+
if (count.is_terminal())
59+
return false;
60+
61+
return file_.expand(link_to_position(count));
62+
}
63+
5564
TEMPLATE
5665
Link CLASS::allocate(const Link& size) NOEXCEPT
5766
{

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ bool CLASS::truncate(const Link& count) NOEXCEPT
103103
return manager_.truncate(count);
104104
}
105105

106+
TEMPLATE
107+
bool CLASS::expand(const Link& count) NOEXCEPT
108+
{
109+
return manager_.expand(count);
110+
}
111+
106112
// error condition
107113
// ----------------------------------------------------------------------------
108114

include/bitcoin/database/memory/interfaces/storage.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class storage
6464
/// Reduce logical size to specified (false if size exceeds logical).
6565
virtual bool truncate(size_t size) NOEXCEPT = 0;
6666

67+
/// Increase logical size to specified as requried (false only if fails).
68+
virtual bool expand(size_t size) NOEXCEPT = 0;
69+
6770
/// Allocate bytes and return offset to first allocated (or eof).
6871
virtual size_t allocate(size_t chunk) NOEXCEPT = 0;
6972

include/bitcoin/database/memory/map.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ class BCD_API map
8787
/// Reduce logical size to specified (false if size exceeds logical).
8888
bool truncate(size_t size) NOEXCEPT override;
8989

90+
/// Increase logical size to specified as requried (false only if fails).
91+
bool expand(size_t size) NOEXCEPT override;
92+
9093
/// Allocate bytes and return offset to first allocated (or eof).
9194
size_t allocate(size_t chunk) NOEXCEPT override;
9295

include/bitcoin/database/primitives/manager.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class manager
5252
/// Reduce the number of records (false if not lesser).
5353
bool truncate(const Link& count) NOEXCEPT;
5454

55+
/// Increase the number of records as necessary (false only if fails).
56+
bool expand(const Link& count) NOEXCEPT;
57+
5558
/// Allocate records and return first logical position (eof possible).
5659
/// For record, size is number of records to allocate (link + data).
5760
/// For slab size must include bytes (link + data) [key is part of data].

include/bitcoin/database/primitives/nomap.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class nomap
6868
/// Reduce count as specified.
6969
bool truncate(const Link& count) NOEXCEPT;
7070

71+
/// Increase count as neccesary to specified.
72+
bool expand(const Link& count) NOEXCEPT;
73+
7174
/// Errors.
7275
/// -----------------------------------------------------------------------
7376

src/memory/map.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,32 @@ bool map::truncate(size_t size) NOEXCEPT
229229
return true;
230230
}
231231

232+
bool map::expand(size_t size) NOEXCEPT
233+
{
234+
std::unique_lock field_lock(field_mutex_);
235+
236+
if (fault_ || !loaded_)
237+
return false;
238+
239+
if (size <= logical_)
240+
return true;
241+
242+
if (size > capacity_)
243+
{
244+
const auto capacity = to_capacity(size);
245+
246+
// TODO: Could loop over a try lock here and log deadlock warning.
247+
std::unique_lock remap_lock(remap_mutex_);
248+
249+
// Disk full condition leaves store in valid state despite eof return.
250+
if (!remap_(capacity))
251+
return false;
252+
}
253+
254+
logical_ = size;
255+
return true;
256+
}
257+
232258
// Waits until all access pointers are destructed. Will deadlock if any access
233259
// pointer is waiting on allocation. Lock safety requires that access pointers
234260
// are short-lived and do not block on allocation.

test/mocks/chunk_storage.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ bool chunk_storage::truncate(size_t size) NOEXCEPT
107107
return true;
108108
}
109109

110+
bool chunk_storage::expand(size_t size) NOEXCEPT
111+
{
112+
std::unique_lock field_lock(field_mutex_);
113+
if (size > buffer_.size())
114+
buffer_.resize(size);
115+
116+
return true;
117+
}
118+
110119
size_t chunk_storage::allocate(size_t chunk) NOEXCEPT
111120
{
112121
std::unique_lock field_lock(field_mutex_);

test/mocks/chunk_storage.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class chunk_storage
4848
size_t capacity() const NOEXCEPT override;
4949
size_t size() const NOEXCEPT override;
5050
bool truncate(size_t size) NOEXCEPT override;
51+
bool expand(size_t size) NOEXCEPT override;
5152
size_t allocate(size_t chunk) NOEXCEPT override;
5253
memory_ptr set(size_t offset, size_t size,
5354
uint8_t backfill) NOEXCEPT override;

test/mocks/chunk_store.hpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ class chunk_store
7474
return output_body_.buffer();
7575
}
7676

77-
system::data_chunk& prevout_head() NOEXCEPT
77+
system::data_chunk& ins_head() NOEXCEPT
7878
{
79-
return prevout_head_.buffer();
79+
return ins_head_.buffer();
8080
}
8181

82-
system::data_chunk& prevout_body() NOEXCEPT
82+
system::data_chunk& ins_body() NOEXCEPT
8383
{
84-
return prevout_body_.buffer();
84+
return ins_body_.buffer();
8585
}
8686

8787
system::data_chunk& puts_head() NOEXCEPT
@@ -158,6 +158,16 @@ class chunk_store
158158

159159
// Caches.
160160

161+
system::data_chunk& prevout_head() NOEXCEPT
162+
{
163+
return prevout_head_.buffer();
164+
}
165+
166+
system::data_chunk& prevout_body() NOEXCEPT
167+
{
168+
return prevout_body_.buffer();
169+
}
170+
161171
system::data_chunk& validated_bk_head() NOEXCEPT
162172
{
163173
return validated_bk_head_.buffer();

0 commit comments

Comments
 (0)