use super::batch::{BatchValues, BatchValuesIterator};
use super::row::{RowSerializationContext, SerializedValues};
use super::{RowWriter, SerializationError};
pub trait RawBatchValues {
type RawBatchValuesIter<'r>: RawBatchValuesIterator<'r>
where
Self: 'r;
fn batch_values_iter(&self) -> Self::RawBatchValuesIter<'_>;
}
pub trait RawBatchValuesIterator<'a> {
fn serialize_next(&mut self, writer: &mut RowWriter) -> Option<Result<(), SerializationError>>;
fn is_empty_next(&mut self) -> Option<bool>;
fn skip_next(&mut self) -> Option<()>;
#[inline]
fn count(mut self) -> usize
where
Self: Sized,
{
std::iter::from_fn(|| self.skip_next()).count()
}
}
impl RawBatchValues for Vec<SerializedValues> {
type RawBatchValuesIter<'r>
= std::slice::Iter<'r, SerializedValues>
where
Self: 'r;
fn batch_values_iter(&self) -> Self::RawBatchValuesIter<'_> {
self.iter()
}
}
impl<'r> RawBatchValuesIterator<'r> for std::slice::Iter<'r, SerializedValues> {
#[inline]
fn serialize_next(&mut self, writer: &mut RowWriter) -> Option<Result<(), SerializationError>> {
self.next().map(|sv| {
writer.append_serialize_row(sv);
Ok(())
})
}
fn is_empty_next(&mut self) -> Option<bool> {
self.next().map(|sv| sv.is_empty())
}
#[inline]
fn skip_next(&mut self) -> Option<()> {
self.next().map(|_| ())
}
#[inline]
fn count(self) -> usize {
<_ as Iterator>::count(self)
}
}
pub struct RawBatchValuesAdapter<BV, CTX> {
batch_values: BV,
contexts: CTX,
}
impl<BV, CTX> RawBatchValuesAdapter<BV, CTX> {
#[inline]
pub fn new(batch_values: BV, contexts: CTX) -> Self {
Self {
batch_values,
contexts,
}
}
}
impl<'ctx, BV, CTX> RawBatchValues for RawBatchValuesAdapter<BV, CTX>
where
BV: BatchValues,
CTX: Iterator<Item = RowSerializationContext<'ctx>> + Clone,
{
type RawBatchValuesIter<'r>
= RawBatchValuesIteratorAdapter<BV::BatchValuesIter<'r>, CTX>
where
Self: 'r;
#[inline]
fn batch_values_iter(&self) -> Self::RawBatchValuesIter<'_> {
RawBatchValuesIteratorAdapter {
batch_values_iterator: self.batch_values.batch_values_iter(),
contexts: self.contexts.clone(),
}
}
}
pub struct RawBatchValuesIteratorAdapter<BVI, CTX> {
batch_values_iterator: BVI,
contexts: CTX,
}
impl<'bvi, 'ctx, BVI, CTX> RawBatchValuesIterator<'bvi> for RawBatchValuesIteratorAdapter<BVI, CTX>
where
BVI: BatchValuesIterator<'bvi>,
CTX: Iterator<Item = RowSerializationContext<'ctx>>,
{
#[inline]
fn serialize_next(&mut self, writer: &mut RowWriter) -> Option<Result<(), SerializationError>> {
let ctx = self
.contexts
.next()
.unwrap_or(RowSerializationContext::empty());
self.batch_values_iterator.serialize_next(&ctx, writer)
}
fn is_empty_next(&mut self) -> Option<bool> {
let _ = self.contexts.next();
let ret = self.batch_values_iterator.is_empty_next()?;
Some(ret)
}
#[inline]
fn skip_next(&mut self) -> Option<()> {
let _ = self.contexts.next();
self.batch_values_iterator.skip_next()?;
Some(())
}
}