use crate::{
index::{
config::{IndexType, IndexTypeUnwrap},
raw_iter::IndexRawIter,
raw_iter_tx::TxIndexRawIter,
},
persy::PersyImpl,
Transaction, ValueIter,
};
use std::sync::Arc;
pub struct IndexIter<K: IndexType, V: IndexType> {
iter_impl: IndexRawIter<K::Wrapper, V::Wrapper>,
persy_impl: Arc<PersyImpl>,
}
impl<K: IndexType, V: IndexType> IndexIter<K, V> {
pub(crate) fn new(iter_impl: IndexRawIter<K::Wrapper, V::Wrapper>, persy_impl: Arc<PersyImpl>) -> IndexIter<K, V> {
IndexIter { iter_impl, persy_impl }
}
}
impl<K, V> Iterator for IndexIter<K, V>
where
K: IndexType,
V: IndexType,
{
type Item = (K, ValueIter<V>);
fn next(&mut self) -> Option<Self::Item> {
self.iter_impl
.next(&self.persy_impl)
.map(|(k, v)| (k.unwrap(), ValueIter::from(v)))
}
}
impl<K, V> DoubleEndedIterator for IndexIter<K, V>
where
K: IndexType,
V: IndexType,
{
fn next_back(&mut self) -> Option<Self::Item> {
self.iter_impl
.next_back(&self.persy_impl)
.map(|(k, v)| (k.unwrap(), ValueIter::from(v)))
}
}
pub struct TxIndexIter<'a, K: IndexType, V: IndexType> {
iter_impl: TxIndexRawIter<K::Wrapper, V::Wrapper>,
tx: &'a mut Transaction,
}
impl<'a, K: IndexType, V: IndexType> TxIndexIter<'a, K, V> {
pub(crate) fn new(
iter_impl: TxIndexRawIter<K::Wrapper, V::Wrapper>,
tx: &'a mut Transaction,
) -> TxIndexIter<'a, K, V> {
TxIndexIter { iter_impl, tx }
}
pub fn next_tx(&mut self) -> Option<(K, ValueIter<V>, &mut Transaction)> {
if let Some((k, v)) = self.iter_impl.next(&self.tx.persy_impl, self.tx.tx.as_mut().unwrap()) {
Some((k.unwrap(), ValueIter::from(v), self.tx))
} else {
None
}
}
pub fn tx(&mut self) -> &mut Transaction {
self.tx
}
}
impl<'a, K, V> Iterator for TxIndexIter<'a, K, V>
where
K: IndexType,
V: IndexType,
{
type Item = (K, ValueIter<V>);
fn next(&mut self) -> Option<Self::Item> {
self.iter_impl
.next(&self.tx.persy_impl, self.tx.tx.as_mut().unwrap())
.map(|(k, v)| (k.unwrap(), ValueIter::from(v)))
}
}
impl<'a, K, V> DoubleEndedIterator for TxIndexIter<'a, K, V>
where
K: IndexType,
V: IndexType,
{
fn next_back(&mut self) -> Option<Self::Item> {
self.iter_impl
.next_back(&self.tx.persy_impl, self.tx.tx.as_mut().unwrap())
.map(|(k, v)| (k.unwrap(), ValueIter::from(v)))
}
}