Skip to content

Commit 187a520

Browse files
committed
db: use bulk insert for writing blocks
1 parent e7e586d commit 187a520

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/db/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ impl Db {
3333
}
3434
}
3535

36-
pub fn insert_block(&self, block: Block) -> anyhow::Result<usize> {
36+
pub fn insert_blocks(&self, blocks: Vec<Block>) -> anyhow::Result<usize> {
3737
Ok(diesel::insert_into(blocks::table)
38-
.values(block)
38+
.values(blocks)
3939
.execute(&mut self.pool.get()?)?)
4040
}
4141

src/parser/mod.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,32 @@ impl BlockchainParser {
5353
tracing::debug!(target: "parser", "Starting worker ...");
5454

5555
self.on_start(self.cur_height);
56+
let block_buffer_size = 2;
57+
let mut blocks = Vec::with_capacity(block_buffer_size);
5658
while let Some(header) = self.chain_storage.get_header(self.cur_height) {
5759
Self::on_header(&header, self.cur_height);
5860
let block = self.chain_storage.get_block(self.cur_height).unwrap();
59-
self.on_block(&block, self.cur_height)?;
61+
62+
tracing::trace!(target: "parser", "on_block(height={}) called", self.cur_height);
63+
blocks.push(crate::db::Block {
64+
height: self.cur_height.try_into()?,
65+
version: block.header.version.to_consensus(),
66+
time: block.header.time.try_into()?,
67+
encoded_target: block.header.bits.to_consensus().try_into()?,
68+
nonce: block.header.nonce.try_into()?,
69+
tx_count: block.txdata.len().try_into()?,
70+
size: block.size().try_into()?,
71+
weight: block.weight().to_wu().try_into()?,
72+
});
73+
if blocks.len() == block_buffer_size {
74+
self.db.insert_blocks(blocks)?;
75+
blocks = Vec::with_capacity(block_buffer_size);
76+
}
6077
self.print_progress(self.cur_height);
6178
self.cur_height += 1;
6279
}
80+
81+
self.db.insert_blocks(blocks)?;
6382
self.on_complete(self.cur_height.saturating_sub(1));
6483
Ok(())
6584
}
@@ -83,25 +102,6 @@ impl BlockchainParser {
83102
tracing::trace!(target: "parser", "on_header(height={}) called", height);
84103
}
85104

86-
fn on_block(
87-
&mut self,
88-
block: &bitcoin::Block,
89-
height: u64,
90-
) -> anyhow::Result<()> {
91-
tracing::trace!(target: "parser", "on_block(height={}) called", height);
92-
self.db.insert_block(crate::db::Block {
93-
height: self.cur_height.try_into()?,
94-
version: block.header.version.to_consensus(),
95-
time: block.header.time.try_into()?,
96-
encoded_target: block.header.bits.to_consensus().try_into()?,
97-
nonce: block.header.nonce.try_into()?,
98-
tx_count: block.txdata.len().try_into()?,
99-
size: block.size().try_into()?,
100-
weight: block.weight().to_wu().try_into()?,
101-
})?;
102-
Ok(())
103-
}
104-
105105
fn on_complete(&mut self, height: u64) {
106106
tracing::info!(target: "parser", "Done. Processed blocks up to height {} in {:.2} minutes.",
107107
height, self.stats.started_at.elapsed().as_secs_f32() / 60.0);

0 commit comments

Comments
 (0)