use crate::{
address::record_scanner::{SegmentRawIter, SegmentSnapshotRawIter, TxSegmentRawIter},
persy::PersyImpl,
PersyId, Transaction,
};
use std::sync::Arc;
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)
}
}
impl Drop for SegmentIter {
fn drop(&mut self) {
self.iter_impl.release(&self.persy_impl).unwrap()
}
}
struct RawIterDrop {
iter_impl: TxSegmentRawIter,
persy_impl: Arc<PersyImpl>,
}
impl<'a> Drop for RawIterDrop {
fn drop(&mut self) {
self.iter_impl.release(&self.persy_impl).unwrap()
}
}
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,
persy_impl: tx.persy_impl.clone(),
},
tx,
}
}
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
}
}
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))
}
}
pub struct SnapshotSegmentIter {
iter_impl: SegmentSnapshotRawIter,
persy_impl: Arc<PersyImpl>,
}
impl<'a> SnapshotSegmentIter {
pub(crate) fn new(iter_impl: SegmentSnapshotRawIter, persy_impl: Arc<PersyImpl>) -> SnapshotSegmentIter {
SnapshotSegmentIter { iter_impl, persy_impl }
}
}
impl<'a> Iterator for SnapshotSegmentIter {
type Item = (PersyId, Vec<u8>);
fn next(&mut self) -> Option<Self::Item> {
self.iter_impl.next(&self.persy_impl)
}
}