Skip to content

Commit c8e9207

Browse files
committed
Add put counts and block to put translations.
1 parent 6e30855 commit c8e9207

File tree

3 files changed

+60
-16
lines changed

3 files changed

+60
-16
lines changed

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

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

22+
#include <numeric>
2223
#include <bitcoin/system.hpp>
2324
#include <bitcoin/database/define.hpp>
2425

@@ -207,6 +208,26 @@ two_counts CLASS::put_counts(const tx_link& link) const NOEXCEPT
207208
return { tx.ins_count, tx.outs_count };
208209
}
209210

211+
TEMPLATE
212+
size_t CLASS::input_count(const tx_links& txs) const NOEXCEPT
213+
{
214+
const auto fn = [this](auto tx) NOEXCEPT { return input_count(tx); };
215+
return std_reduce(bc::par_unseq, txs.begin(), txs.end(), zero, fn);
216+
}
217+
218+
TEMPLATE
219+
size_t CLASS::output_count(const tx_links& txs) const NOEXCEPT
220+
{
221+
const auto fn = [this](auto tx) NOEXCEPT { return output_count(tx); };
222+
return std_reduce(bc::par_unseq, txs.begin(), txs.end(), zero, fn);
223+
}
224+
225+
TEMPLATE
226+
two_counts CLASS::put_counts(const tx_links& txs) const NOEXCEPT
227+
{
228+
return { input_count(txs), output_count(txs) };
229+
}
230+
210231
TEMPLATE
211232
bool CLASS::address_enabled() const NOEXCEPT
212233
{

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

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <algorithm>
2323
#include <iterator>
24+
#include <numeric>
2425
#include <unordered_map>
2526
#include <utility>
2627
#include <bitcoin/system.hpp>
@@ -517,15 +518,13 @@ spend_set CLASS::to_spend_set(const tx_link& link) const NOEXCEPT
517518
return set;
518519
}
519520

520-
// block to puts (forward navigation)
521+
// txs to puts (forward navigation)
521522
// ----------------------------------------------------------------------------
522523

523524
TEMPLATE
524-
spend_links CLASS::to_block_spends(const header_link& link) const NOEXCEPT
525+
spend_links CLASS::to_spends(const tx_links& txs) const NOEXCEPT
525526
{
526527
spend_links spends{};
527-
const auto txs = to_spending_transactions(link);
528-
529528
for (const auto& tx: txs)
530529
{
531530
const auto tx_spends = to_spends(tx);
@@ -536,11 +535,9 @@ spend_links CLASS::to_block_spends(const header_link& link) const NOEXCEPT
536535
}
537536

538537
TEMPLATE
539-
output_links CLASS::to_block_outputs(const header_link& link) const NOEXCEPT
538+
output_links CLASS::to_outputs(const tx_links& txs) const NOEXCEPT
540539
{
541540
output_links outputs{};
542-
const auto txs = to_transactions(link);
543-
544541
for (const auto& tx: txs)
545542
{
546543
const auto tx_outputs = to_outputs(tx);
@@ -551,18 +548,36 @@ output_links CLASS::to_block_outputs(const header_link& link) const NOEXCEPT
551548
}
552549

553550
TEMPLATE
554-
output_links CLASS::to_block_prevouts(const header_link& link) const NOEXCEPT
551+
output_links CLASS::to_prevouts(const tx_links& txs) const NOEXCEPT
555552
{
556-
output_links prevouts{};
557-
const auto txs = to_spending_transactions(link);
553+
const auto ins = to_spends(txs);
554+
output_links outs(ins.size());
555+
const auto fn = [this](auto spend) NOEXCEPT{ return to_prevout(spend); };
558556

559-
for (const auto& tx: txs)
560-
{
561-
const auto tx_prevouts = to_prevouts(tx);
562-
prevouts.insert(prevouts.end(), tx_prevouts.begin(), tx_prevouts.end());
563-
}
557+
// C++17 incomplete on GCC/CLang, so presently parallel only on MSVC++.
558+
std_transform(bc::par_unseq, ins.begin(), ins.end(), outs.begin(), fn);
559+
return outs;
560+
}
564561

565-
return prevouts;
562+
// block to puts (forward navigation)
563+
// ----------------------------------------------------------------------------
564+
565+
TEMPLATE
566+
spend_links CLASS::to_block_spends(const header_link& link) const NOEXCEPT
567+
{
568+
return to_spends(to_spending_transactions(link));
569+
}
570+
571+
TEMPLATE
572+
output_links CLASS::to_block_outputs(const header_link& link) const NOEXCEPT
573+
{
574+
return to_outputs(to_transactions(link));
575+
}
576+
577+
TEMPLATE
578+
output_links CLASS::to_block_prevouts(const header_link& link) const NOEXCEPT
579+
{
580+
return to_prevouts(to_spending_transactions(link));
566581
}
567582

568583
// block to txs (forward navigation)

include/bitcoin/database/query.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ class query
217217
size_t input_count(const tx_link& link) const NOEXCEPT;
218218
size_t output_count(const tx_link& link) const NOEXCEPT;
219219
two_counts put_counts(const tx_link& link) const NOEXCEPT;
220+
size_t input_count(const tx_links& txs) const NOEXCEPT;
221+
size_t output_count(const tx_links& txs) const NOEXCEPT;
222+
two_counts put_counts(const tx_links& txs) const NOEXCEPT;
220223

221224
/// Optional table state.
222225
bool address_enabled() const NOEXCEPT;
@@ -286,6 +289,11 @@ class query
286289
output_links to_outputs(const tx_link& link) const NOEXCEPT;
287290
output_links to_prevouts(const tx_link& link) const NOEXCEPT;
288291

292+
/// txs to puts (forward navigation)
293+
spend_links to_spends(const tx_links& txs) const NOEXCEPT;
294+
output_links to_outputs(const tx_links& txs) const NOEXCEPT;
295+
output_links to_prevouts(const tx_links& txs) const NOEXCEPT;
296+
289297
/// block to puts (forward navigation)
290298
spend_links to_block_spends(const header_link& link) const NOEXCEPT;
291299
output_links to_block_outputs(const header_link& link) const NOEXCEPT;

0 commit comments

Comments
 (0)