Skip to content

Commit eff2c89

Browse files
authored
Merge pull request #657 from evoskuil/master
Add store prune method.
2 parents b7b2d92 + 2dcaff6 commit eff2c89

File tree

17 files changed

+97
-17
lines changed

17 files changed

+97
-17
lines changed

include/bitcoin/database/error.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ enum error_t : uint8_t
9191
/// tables
9292
create_table,
9393
close_table,
94+
prune_table,
9495
backup_table,
9596
restore_table,
9697
verify_table,

include/bitcoin/database/impl/store.ipp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef LIBBITCOIN_DATABASE_STORE_IPP
2020
#define LIBBITCOIN_DATABASE_STORE_IPP
2121

22+
#include <chrono>
2223
#include <unordered_map>
2324
#include <bitcoin/system.hpp>
2425
#include <bitcoin/database/boost.hpp>
@@ -49,6 +50,7 @@ const std::unordered_map<event_t, std::string> CLASS::events
4950

5051
{ event_t::wait_lock, "wait_lock" },
5152
{ event_t::flush_body, "flush_body" },
53+
{ event_t::prune_table, "prune_table" },
5254
{ event_t::backup_table, "backup_table" },
5355
{ event_t::copy_header, "copy_header" },
5456
{ event_t::archive_snapshot, "archive_snapshot" },
@@ -416,6 +418,61 @@ code CLASS::open(const event_handler& handler) NOEXCEPT
416418
return ec;
417419
}
418420

421+
TEMPLATE
422+
code CLASS::prune(const event_handler& handler) NOEXCEPT
423+
{
424+
while (!transactor_mutex_.try_lock_for(std::chrono::seconds(1)))
425+
{
426+
handler(event_t::wait_lock, table_t::store);
427+
}
428+
429+
code ec{ error::success };
430+
const auto prune = [&handler](code& ec, auto& storage, table_t table) NOEXCEPT
431+
{
432+
if (!ec)
433+
{
434+
handler(event_t::prune_table, table);
435+
if (!storage.reset())
436+
ec = error::prune_table;
437+
}
438+
};
439+
440+
const auto unload = [&handler](code& ec, auto& storage, table_t table) NOEXCEPT
441+
{
442+
if (!ec)
443+
{
444+
handler(event_t::unload_file, table);
445+
ec = storage.unload();
446+
}
447+
};
448+
449+
const auto load = [&handler](code& ec, auto& storage, table_t table) NOEXCEPT
450+
{
451+
if (!ec)
452+
{
453+
handler(event_t::load_file, table);
454+
ec = storage.load();
455+
}
456+
};
457+
458+
// Prevouts resettable if all candidates confirmed (fork is candidate top).
459+
const query<CLASS> query_{ *this };
460+
if (query_.get_fork() == query_.get_top_candidate())
461+
{
462+
// zeroize table head, set body logical size to zero.
463+
prune(ec, prevout, table_t::prevout_table);
464+
465+
// unmap body, setting mapped size to logical size (zero).
466+
unload(ec, prevout_body_, table_t::prevout_body);
467+
468+
// map body, making table usable again.
469+
load(ec, prevout_body_, table_t::prevout_body);
470+
}
471+
472+
transactor_mutex_.unlock();
473+
return ec;
474+
}
475+
419476
TEMPLATE
420477
code CLASS::snapshot(const event_handler& handler) NOEXCEPT
421478
{

include/bitcoin/database/primitives/arraymap.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class arraymap
7676
/// Capacity of body in bytes.
7777
size_t capacity() const NOEXCEPT;
7878

79-
/// Increase count as neccesary to specified.
79+
/// Increase count as necessary to specified.
8080
bool expand(const Link& count) NOEXCEPT;
8181

8282
/// Errors.

include/bitcoin/database/primitives/hashmap.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class hashmap
7979
/// Capacity of body in bytes.
8080
size_t capacity() const NOEXCEPT;
8181

82-
/// Increase count as neccesary to specified.
82+
/// Increase count as necessary to specified.
8383
bool expand(const Link& count) NOEXCEPT;
8484

8585
/// Diagnostic counters.

include/bitcoin/database/primitives/nomap.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class nomap
7171
/// Reduce count as specified.
7272
bool truncate(const Link& count) NOEXCEPT;
7373

74-
/// Increase count as neccesary to specified.
74+
/// Increase count as necessary to specified.
7575
bool expand(const Link& count) NOEXCEPT;
7676

7777
/// Errors.

include/bitcoin/database/store.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class store
6363
/// Open and load the set of tables, set locks.
6464
code open(const event_handler& handler) NOEXCEPT;
6565

66+
/// Prune prunable tables (from loaded, leaves loaded).
67+
code prune(const event_handler& handler) NOEXCEPT;
68+
6669
/// Snapshot the set of tables (from loaded, leaves loaded).
6770
code snapshot(const event_handler& handler) NOEXCEPT;
6871

include/bitcoin/database/tables/event.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ enum class event_t
3636

3737
wait_lock,
3838
flush_body,
39+
prune_table,
3940
backup_table,
4041
copy_header,
4142
archive_snapshot,

src/error.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ DEFINE_ERROR_T_MESSAGE_MAP(error)
8383
// tables
8484
{ create_table, "failed to create table" },
8585
{ close_table, "failed to close table" },
86+
{ prune_table, "failed to prune table" },
8687
{ backup_table, "failed to backup table" },
8788
{ restore_table, "failed to restore table" },
8889
{ verify_table, "failed to verify table" },

test/error.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,15 @@ BOOST_AUTO_TEST_CASE(error_t__code__close_table__true_exected_message)
329329
BOOST_REQUIRE_EQUAL(ec.message(), "failed to close table");
330330
}
331331

332+
BOOST_AUTO_TEST_CASE(error_t__code__prune_table__true_exected_message)
333+
{
334+
constexpr auto value = error::prune_table;
335+
const auto ec = code(value);
336+
BOOST_REQUIRE(ec);
337+
BOOST_REQUIRE(ec == value);
338+
BOOST_REQUIRE_EQUAL(ec.message(), "failed to prune table");
339+
}
340+
332341
BOOST_AUTO_TEST_CASE(error_t__code__backup_table__true_exected_message)
333342
{
334343
constexpr auto value = error::backup_table;

test/query/archive_read.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ struct query_archive_read_setup_fixture
4040

4141
BOOST_FIXTURE_TEST_SUITE(query_archive_read_tests, query_archive_read_setup_fixture)
4242

43-
const auto events_handler = [](auto, auto) {};
44-
43+
////const auto events_handler = [](auto, auto) {};
4544

4645
BOOST_AUTO_TEST_CASE(query_archive_read_test)
4746
{

0 commit comments

Comments
 (0)