Refactor query accessors to the store

This commit is contained in:
Roman Zeyde 2018-05-17 15:00:31 +03:00
parent 30f892d76d
commit ce49f39948
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB

View File

@ -42,6 +42,33 @@ fn merklize(left: Sha256dHash, right: Sha256dHash) -> Sha256dHash {
Sha256dHash::from_data(&data)
}
impl Store {
fn txrows_by_prefix(&self, txid_prefix: &HashPrefix) -> Vec<TxRow> {
self.scan(&TxRow::filter(&txid_prefix))
.iter()
.map(|row| TxRow::from_row(row))
.collect()
}
fn txids_by_script_hash(&self, script_hash: &[u8]) -> Vec<HashPrefix> {
self.scan(&TxOutRow::filter(script_hash))
.iter()
.map(|row| TxOutRow::from_row(row).txid_prefix)
.collect()
}
fn txids_by_funding_output(
&self,
txn_id: &Sha256dHash,
output_index: usize,
) -> Vec<HashPrefix> {
self.scan(&TxInRow::filter(&txn_id, output_index))
.iter()
.map(|row| TxInRow::from_row(row).txid_prefix)
.collect()
}
}
pub struct Query<'a> {
store: &'a Store,
daemon: &'a Daemon,
@ -63,12 +90,7 @@ impl<'a> Query<'a> {
fn load_txns(&self, prefixes: Vec<HashPrefix>) -> Vec<TxnHeight> {
let mut txns = Vec::new();
for txid_prefix in prefixes {
let tx_rows: Vec<TxRow> = self.store
.scan(&TxRow::filter(&txid_prefix))
.iter()
.map(|row| TxRow::from_row(row))
.collect();
for tx_row in tx_rows {
for tx_row in self.store.txrows_by_prefix(&txid_prefix) {
let txid: Sha256dHash = deserialize(&tx_row.key.txid).unwrap();
let txn: Transaction = self.get_tx(&txid);
txns.push(TxnHeight {
@ -83,10 +105,7 @@ impl<'a> Query<'a> {
fn find_spending_input(&self, funding: &FundingOutput) -> Option<SpendingInput> {
let spending_txns: Vec<TxnHeight> = self.load_txns(
self.store
.scan(&TxInRow::filter(&funding.txn_id, funding.output_index))
.iter()
.map(|row| TxInRow::from_row(row).txid_prefix)
.collect(),
.txids_by_funding_output(&funding.txn_id, funding.output_index),
);
let mut spending_inputs = Vec::new();
for t in &spending_txns {
@ -133,14 +152,7 @@ impl<'a> Query<'a> {
spending: vec![],
};
let funding_txns = self.load_txns(
self.store
.scan(&TxOutRow::filter(script_hash))
.iter()
.map(|row| TxOutRow::from_row(row).txid_prefix)
.collect(),
);
for t in funding_txns {
for t in self.load_txns(self.store.txids_by_script_hash(script_hash)) {
status
.funding
.extend(self.find_funding_outputs(&t, script_hash));