-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
making the
DBM
methods return an iterator instead of a collection
Collecting is memory intensive. Colleting in vecs might be better than collecting in hashsets and hashmaps but still incurs a lot of memory usage for big DB loads. This commit tries to avoid collecting data and stream it using an iterator instead.
- Loading branch information
1 parent
92ad4e5
commit f12cb90
Showing
4 changed files
with
64 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use rusqlite::{MappedRows, Params, Result, Row, Statement}; | ||
use std::iter::Map; | ||
|
||
/// A struct that owns a [Statement] and has an `iter` method to iterate over the | ||
/// results of that DB query statement. | ||
pub struct QueryIterator<'db, P, T> { | ||
stmt: Statement<'db>, | ||
params_and_mapper: Option<(P, Box<dyn Fn(&Row) -> T>)>, | ||
} | ||
|
||
impl<'db, P, T> QueryIterator<'db, P, T> | ||
where | ||
P: Params, | ||
{ | ||
/// Construct a new [QueryIterator]. | ||
pub fn new(stmt: Statement<'db>, params: P, f: impl Fn(&Row) -> T + 'static) -> Self { | ||
Self { | ||
stmt, | ||
params_and_mapper: Some((params, Box::new(f))), | ||
} | ||
} | ||
|
||
/// Returns an iterator over the results of the query. | ||
/// | ||
/// This method should be called only once per [QueryIterator] and then consumed. | ||
/// After calling this method, subsequent calls will return [None]. | ||
pub fn iter( | ||
&mut self, | ||
) -> Option<Map<MappedRows<'_, impl FnMut(&Row) -> Result<T>>, impl FnMut(Result<T>) -> T>> | ||
{ | ||
self.params_and_mapper.take().map(move |(params, mapper)| { | ||
self.stmt | ||
.query_map(params, move |row| Ok((mapper)(row))) | ||
.unwrap() | ||
.map(|row| row.unwrap()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters