1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
use crate::{
address::record_scanner::{SegmentRawIter, SegmentSnapshotRawIter, TxSegmentRawIter},
persy::PersyImpl,
PersyId, Transaction,
};
use std::sync::Arc;
/// Iterator implementation used to scan a segment
///
/// # Example
///
/// ```rust
/// # use persy::{Persy,Config};
/// # use persy::{OpenOptions};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let persy = OpenOptions::new().memory()?;
/// let mut tx = persy.begin()?;
/// # tx.create_segment("seg")?;
/// let data = vec![1;20];
/// let id = tx.insert("seg", &data)?;
/// let prepared = tx.prepare()?;
/// prepared.commit()?;
/// let mut count = 0;
/// for (id,content) in persy.scan("seg")? {
/// println!("record size:{}",content.len());
/// count+=1;
/// }
/// assert_eq!(count,1);
/// # Ok(())
/// # }
/// ```
pub struct SegmentIter {
iter_impl: SegmentRawIter,
persy_impl: Arc<PersyImpl>,
}
impl SegmentIter {
pub(crate) fn new(iter_impl: SegmentRawIter, persy_impl: Arc<PersyImpl>) -> SegmentIter {
SegmentIter { iter_impl, persy_impl }
}
}
impl Iterator for SegmentIter {
type Item = (PersyId, Vec<u8>);
fn next(&mut self) -> Option<Self::Item> {
self.iter_impl.next(&self.persy_impl)
}
}
struct RawIterDrop {
iter_impl: TxSegmentRawIter,
}
/// Iterator implementation to scan a segment considering in transaction changes.
///
/// # Example
///
/// ```rust
/// # use persy::{OpenOptions};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let persy = OpenOptions::new().memory()?;
/// let mut tx = persy.begin()?;
/// # tx.create_segment("seg")?;
/// let data = vec![1;20];
/// let id = tx.insert("seg", &data)?;
/// let mut count = 0;
/// for (id,content) in tx.scan("seg")? {
/// println!("record size:{}",content.len());
/// count+=1;
/// }
/// assert_eq!(count,1);
/// # Ok(())
/// # }
/// ```
pub struct TxSegmentIter<'a> {
iter_impl: RawIterDrop,
tx: &'a mut Transaction,
}
impl<'a> TxSegmentIter<'a> {
pub(crate) fn new(iter_impl: TxSegmentRawIter, tx: &'a mut Transaction) -> TxSegmentIter<'a> {
TxSegmentIter {
iter_impl: RawIterDrop { iter_impl },
tx,
}
}
/// get the next element in the iterator giving the access on the transaction owned by the
/// iterator
pub fn next_tx(&mut self) -> Option<(PersyId, Vec<u8>, &mut Transaction)> {
if let Some((id, rec, _)) = self
.iter_impl
.iter_impl
.next(&self.tx.persy_impl, self.tx.tx.as_mut().unwrap())
{
Some((id, rec, self.tx))
} else {
None
}
}
/// Direct access to the transaction owned by the iterator
pub fn tx(&mut self) -> &mut Transaction {
self.tx
}
}
impl<'a> Iterator for TxSegmentIter<'a> {
type Item = (PersyId, Vec<u8>);
fn next(&mut self) -> Option<Self::Item> {
self.iter_impl
.iter_impl
.next(&self.tx.persy_impl, self.tx.tx.as_mut().unwrap())
.map(|(id, content, _)| (id, content))
}
}
/// Iterator implementation to scan a segment at the current snapshot state.
///
/// # Example
/// ```rust
/// # use persy::{OpenOptions};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let persy = OpenOptions::new().memory()?;
/// let mut tx = persy.begin()?;
/// # tx.create_segment("seg")?;
/// let data = vec![1;20];
/// let id = tx.insert("seg", &data)?;
/// tx.prepare()?.commit()?;
/// let snapshot = persy.snapshot()?;
/// let mut count = 0;
/// for (id,content) in snapshot.scan("seg")? {
/// println!("record size:{}",content.len());
/// count+=1;
/// }
/// assert_eq!(count,1);
/// # Ok(())
/// # }
/// ```
pub struct SnapshotSegmentIter {
iter_impl: SegmentSnapshotRawIter,
persy_impl: Arc<PersyImpl>,
}
impl SnapshotSegmentIter {
pub(crate) fn new(iter_impl: SegmentSnapshotRawIter, persy_impl: Arc<PersyImpl>) -> SnapshotSegmentIter {
SnapshotSegmentIter { iter_impl, persy_impl }
}
}
impl Iterator for SnapshotSegmentIter {
type Item = (PersyId, Vec<u8>);
fn next(&mut self) -> Option<Self::Item> {
self.iter_impl.next(&self.persy_impl)
}
}