pub struct SwapVec<T>where
for<'a> T: Serialize + Deserialize<'a>,{ /* private fields */ }Expand description
An only growing array type which swaps to disk, based on it’s initial configuration.
Create a mutable instance, and then pass iterators or elements to grow it.
let mut bigvec = swapvec::SwapVec::default();
let iterator = (0..9);
bigvec.consume(iterator);
bigvec.push(99);
let new_iterator = bigvec.into_iter();Implementations§
Source§impl<T> SwapVec<T>
impl<T> SwapVec<T>
Sourcepub fn with_config(config: SwapVecConfig) -> Self
pub fn with_config(config: SwapVecConfig) -> Self
Intialize with non-default configuration.
Examples found in repository?
5fn main() {
6 let element_count = DATA_MB / 8;
7 let big_iterator = 0..element_count * 1024 * 1024;
8
9 let config = swapvec::SwapVecConfig {
10 batch_size: 8 * 1024,
11 ..SwapVecConfig::default()
12 };
13 let mut swapvec: SwapVec<_> = SwapVec::with_config(config);
14 swapvec.consume(big_iterator.into_iter()).unwrap();
15
16 println!("Data size: {}MB", DATA_MB);
17 println!("Done. Batches written: {}", swapvec.batches_written());
18 println!(
19 "Filesize: {}MB",
20 swapvec
21 .file_size()
22 .map(|x| x as f32 / 1024. / 1024.)
23 .unwrap_or(0.)
24 );
25 println!("Read back");
26
27 let read_back: Vec<_> = swapvec.into_iter().map(|x| x.unwrap()).collect();
28
29 println!("Elements read back: {}", read_back.len());
30}Sourcepub fn consume(
&mut self,
it: impl Iterator<Item = T>,
) -> Result<(), SwapVecError>
pub fn consume( &mut self, it: impl Iterator<Item = T>, ) -> Result<(), SwapVecError>
Give away an entire iterator for consumption.
Might return an error, due to possibly triggered batch flush (IO).
Examples found in repository?
5fn main() {
6 let element_count = DATA_MB / 8;
7 let big_iterator = 0..element_count * 1024 * 1024;
8
9 let config = swapvec::SwapVecConfig {
10 batch_size: 8 * 1024,
11 ..SwapVecConfig::default()
12 };
13 let mut swapvec: SwapVec<_> = SwapVec::with_config(config);
14 swapvec.consume(big_iterator.into_iter()).unwrap();
15
16 println!("Data size: {}MB", DATA_MB);
17 println!("Done. Batches written: {}", swapvec.batches_written());
18 println!(
19 "Filesize: {}MB",
20 swapvec
21 .file_size()
22 .map(|x| x as f32 / 1024. / 1024.)
23 .unwrap_or(0.)
24 );
25 println!("Read back");
26
27 let read_back: Vec<_> = swapvec.into_iter().map(|x| x.unwrap()).collect();
28
29 println!("Elements read back: {}", read_back.len());
30}Sourcepub fn push(&mut self, element: T) -> Result<(), SwapVecError>
pub fn push(&mut self, element: T) -> Result<(), SwapVecError>
Push a single element.
Might return an error, due to possibly triggered batch flush (IO).
Will write at most one batch per insert.
If swap_after is bigger than batch_size and a file is created,
every insert will
write one batch to disk, until the elements in memory have a count
smaller than or equal to batch size.
Sourcepub fn written_to_file(&self) -> bool
pub fn written_to_file(&self) -> bool
Check if enough items have been pushed so that
the temporary file has been created.
Will be false if element count is below swap_after and below batch_size
Sourcepub fn file_size(&self) -> Option<usize>
pub fn file_size(&self) -> Option<usize>
Get the file size in bytes of the temporary file. Might do IO and therefore could return some Result.
Examples found in repository?
5fn main() {
6 let element_count = DATA_MB / 8;
7 let big_iterator = 0..element_count * 1024 * 1024;
8
9 let config = swapvec::SwapVecConfig {
10 batch_size: 8 * 1024,
11 ..SwapVecConfig::default()
12 };
13 let mut swapvec: SwapVec<_> = SwapVec::with_config(config);
14 swapvec.consume(big_iterator.into_iter()).unwrap();
15
16 println!("Data size: {}MB", DATA_MB);
17 println!("Done. Batches written: {}", swapvec.batches_written());
18 println!(
19 "Filesize: {}MB",
20 swapvec
21 .file_size()
22 .map(|x| x as f32 / 1024. / 1024.)
23 .unwrap_or(0.)
24 );
25 println!("Read back");
26
27 let read_back: Vec<_> = swapvec.into_iter().map(|x| x.unwrap()).collect();
28
29 println!("Elements read back: {}", read_back.len());
30}Sourcepub fn batches_written(&self) -> usize
pub fn batches_written(&self) -> usize
Basically int(elements pushed / batch size)
Examples found in repository?
5fn main() {
6 let element_count = DATA_MB / 8;
7 let big_iterator = 0..element_count * 1024 * 1024;
8
9 let config = swapvec::SwapVecConfig {
10 batch_size: 8 * 1024,
11 ..SwapVecConfig::default()
12 };
13 let mut swapvec: SwapVec<_> = SwapVec::with_config(config);
14 swapvec.consume(big_iterator.into_iter()).unwrap();
15
16 println!("Data size: {}MB", DATA_MB);
17 println!("Done. Batches written: {}", swapvec.batches_written());
18 println!(
19 "Filesize: {}MB",
20 swapvec
21 .file_size()
22 .map(|x| x as f32 / 1024. / 1024.)
23 .unwrap_or(0.)
24 );
25 println!("Read back");
26
27 let read_back: Vec<_> = swapvec.into_iter().map(|x| x.unwrap()).collect();
28
29 println!("Elements read back: {}", read_back.len());
30}