Skip to content

Commit

Permalink
Merge pull request #460 from evoskuil/master
Browse files Browse the repository at this point in the history
Add general store fault summarization.
  • Loading branch information
evoskuil committed May 6, 2024
2 parents fdff587 + ab8f723 commit aa9be58
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
6 changes: 6 additions & 0 deletions include/bitcoin/database/impl/query/query.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ bool CLASS::is_full() const NOEXCEPT
return store_.get_error(error::disk_full);
}

TEMPLATE
bool CLASS::is_fault() const NOEXCEPT
{
return !!store_.get_first_error();
}

} // namespace database
} // namespace libbitcoin

Expand Down
57 changes: 56 additions & 1 deletion include/bitcoin/database/impl/store.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,30 @@ const typename CLASS::transactor CLASS::get_transactor() NOEXCEPT
return transactor{ transactor_mutex_ };
}

TEMPLATE
code CLASS::get_first_error() const NOEXCEPT
{
code ec{};
if ((ec = header_body_.get_error())) return ec;
if ((ec = input_body_.get_error())) return ec;
if ((ec = output_body_.get_error())) return ec;
if ((ec = point_body_.get_error())) return ec;
if ((ec = puts_body_.get_error())) return ec;
if ((ec = spend_body_.get_error())) return ec;
if ((ec = tx_body_.get_error())) return ec;
if ((ec = txs_body_.get_error())) return ec;
if ((ec = candidate_body_.get_error())) return ec;
if ((ec = confirmed_body_.get_error())) return ec;
if ((ec = strong_tx_body_.get_error())) return ec;
if ((ec = validated_bk_body_.get_error())) return ec;
if ((ec = validated_tx_body_.get_error())) return ec;
if ((ec = address_body_.get_error())) return ec;
if ((ec = neutrino_body_.get_error())) return ec;
////if ((ec = bootstrap_body_.get_error())) return ec;
////if ((ec = buffer_body_.get_error())) return ec;
return ec;
}

TEMPLATE
bool CLASS::get_error(const code& ec) const NOEXCEPT
{
Expand Down Expand Up @@ -462,6 +486,33 @@ void CLASS::clear_error() NOEXCEPT
////buffer_body_.clear_error();
}

TEMPLATE
void CLASS::report_errors(const error_handler& handler) NOEXCEPT
{
const auto report = [&handler](const auto& storage, table_t table) NOEXCEPT
{
handler(storage.get_error(), table);
};

report(header_body_, table_t::header_body);
report(input_body_, table_t::input_body);
report(output_body_, table_t::output_body);
report(point_body_, table_t::point_body);
report(puts_body_, table_t::puts_body);
report(spend_body_, table_t::spend_body);
report(tx_body_, table_t::tx_body);
report(txs_body_, table_t::txs_body);
report(candidate_body_, table_t::candidate_body);
report(confirmed_body_, table_t::confirmed_body);
report(strong_tx_body_, table_t::strong_tx_body);
report(validated_bk_body_, table_t::validated_bk_body);
report(validated_tx_body_, table_t::validated_tx_body);
report(address_body_, table_t::address_body);
report(neutrino_body_, table_t::neutrino_body);
////report(bootstrap_body_, table_t::bootstrap_body);
////report(buffer_body_, table_t::buffer_body);
}

// protected
// ----------------------------------------------------------------------------

Expand Down Expand Up @@ -714,7 +765,11 @@ code CLASS::backup(const event_handler& handler) NOEXCEPT
// Dump /heads memory maps to /primary.
if (!file::clear_directory(primary)) return error::create_directory;
const auto ec = dump(primary, handler);
if (ec) /* bool */ file::clear_directory(primary);

// If failed clear primary and rename secondary to primary.
if (ec && file::clear_directory(primary) && file::remove(primary))
/* bool */ file::rename(secondary, primary);

return ec;
}

Expand Down
3 changes: 3 additions & 0 deletions include/bitcoin/database/query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class query
/// True if disk is full and no other store error condition.
bool is_full() const NOEXCEPT;

/// True if there is any store error condition.
bool is_fault() const NOEXCEPT;

/// Initialization (natural-keyed).
/// -----------------------------------------------------------------------
/// Not reliable during organization.
Expand Down
10 changes: 9 additions & 1 deletion include/bitcoin/database/store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <shared_mutex>
#include <bitcoin/database/boost.hpp>
#include <bitcoin/database/define.hpp>
#include <bitcoin/database/error.hpp>
#include <bitcoin/database/settings.hpp>
#include <bitcoin/database/locks/locks.hpp>
#include <bitcoin/database/memory/memory.hpp>
Expand All @@ -43,6 +44,7 @@ class store
DELETE_COPY_MOVE_DESTRUCT(store);

typedef std::function<void(event_t, table_t)> event_handler;
typedef std::function<void(const code&, table_t)> error_handler;
typedef std::shared_lock<std::shared_timed_mutex> transactor;

/// Construct a store from settings.
Expand All @@ -66,12 +68,18 @@ class store
/// Get a transactor object.
const transactor get_transactor() NOEXCEPT;

/// Detect the specified exclusive error condition.
/// Detect the first error condition.
code get_first_error() const NOEXCEPT;

/// Detect the specified error condition (exclusive).
bool get_error(const code& ec) const NOEXCEPT;

/// Clear all error conditions.
void clear_error() NOEXCEPT;

/// Dump all error conditions to error handler.
void report_errors(const error_handler& handler) NOEXCEPT;

/// Archives.
table::header header;
table::input input;
Expand Down

0 comments on commit aa9be58

Please sign in to comment.