use super::{
ColumnDefinition, RowFields, RowImage, RowValue, Rows, RowsDecoder, RowsEvent, RowsKind,
};
use crate::{
Error, Result,
config::RowImageMode,
error::invalid,
events::{BinlogMode, TableMapEvent},
};
use std::{fmt::Debug, iter::FusedIterator, marker::PhantomData};
mod sealed {
pub trait Sealed {}
}
pub trait ImageMode: sealed::Sealed + BinlogMode + Send + Sync + 'static {
type Image<'a>: TryFrom<RowImage<'a>, Error = Error> + AsRef<RowImage<'a>> + Debug + Clone;
#[doc(hidden)]
const REQUIRE_COMPLETE: bool;
const ROW_IMAGE: RowImageMode;
#[doc(hidden)]
fn validate_rows(rows: &Rows<'_>) -> Result<()>;
}
#[derive(Debug, Clone, Copy)]
pub struct Minimal;
impl sealed::Sealed for Minimal {}
impl ImageMode for Minimal {
type Image<'a> = SparseImage<'a>;
const REQUIRE_COMPLETE: bool = false;
const ROW_IMAGE: RowImageMode = RowImageMode::Minimal;
fn validate_rows(_: &Rows<'_>) -> Result<()> {
Ok(())
}
}
#[derive(Debug, Clone, Copy)]
pub struct Full;
impl sealed::Sealed for Full {}
impl ImageMode for Full {
type Image<'a> = CompleteImage<'a>;
const REQUIRE_COMPLETE: bool = true;
const ROW_IMAGE: RowImageMode = RowImageMode::Full;
fn validate_rows(rows: &Rows<'_>) -> Result<()> {
rows.require_complete()
}
}
#[derive(Debug, Clone)]
pub struct SparseImage<'a> {
image: RowImage<'a>,
}
impl<'a> SparseImage<'a> {
pub fn column_count(&self) -> usize {
self.image.column_count()
}
pub fn column_indices(&self) -> &[u16] {
self.image.column_indices()
}
pub fn fields(&self) -> RowFields<'_, 'a> {
self.image.fields()
}
pub fn field(&self, column_index: usize) -> Result<Option<RowValue<'a>>> {
field(&self.image, column_index)
}
}
impl<'a> TryFrom<RowImage<'a>> for SparseImage<'a> {
type Error = Error;
fn try_from(image: RowImage<'a>) -> Result<Self> {
Ok(Self { image })
}
}
impl<'a> AsRef<RowImage<'a>> for SparseImage<'a> {
fn as_ref(&self) -> &RowImage<'a> {
&self.image
}
}
#[derive(Debug, Clone)]
pub struct CompleteImage<'a> {
image: RowImage<'a>,
}
impl<'a> CompleteImage<'a> {
pub fn column_count(&self) -> usize {
self.image.column_count()
}
pub fn fields(&self) -> RowFields<'_, 'a> {
self.image.fields()
}
pub fn field(&self, column_index: usize) -> Result<RowValue<'a>> {
if column_index >= self.column_count() {
return Err(invalid("column index outside complete row image"));
}
field(&self.image, column_index)?.ok_or_else(|| invalid("incomplete FULL row image"))
}
}
impl<'a> TryFrom<RowImage<'a>> for CompleteImage<'a> {
type Error = Error;
fn try_from(image: RowImage<'a>) -> Result<Self> {
if image.column_indices().len() != image.column_count() {
return Err(Error::Unsupported(
"FULL row images must contain every table column".into(),
));
}
Ok(Self { image })
}
}
impl<'a> AsRef<RowImage<'a>> for CompleteImage<'a> {
fn as_ref(&self) -> &RowImage<'a> {
&self.image
}
}
fn field<'a>(image: &RowImage<'a>, index: usize) -> Result<Option<RowValue<'a>>> {
for field in image.fields() {
let field = field?;
let column_index = usize::from(field.column_index);
if column_index == index {
return Ok(Some(field.value));
}
if column_index > index {
break;
}
}
Ok(None)
}
#[derive(Debug, Clone)]
pub struct TypedRow<'a, M: ImageMode> {
pub before: Option<M::Image<'a>>,
pub after: Option<M::Image<'a>>,
}
pub struct TypedRowsEvent<'a, M: ImageMode> {
event: RowsEvent<'a>,
rows: Rows<'a>,
column_count: usize,
mode: PhantomData<M>,
}
impl<'a, M: ImageMode> TypedRowsEvent<'a, M> {
pub(super) fn new(event: RowsEvent<'a>, decoder: &RowsDecoder) -> Result<Self> {
let rows = decoder.rows(&event)?;
M::validate_rows(&rows)?;
Ok(Self {
event,
rows,
column_count: decoder.columns().len(),
mode: PhantomData,
})
}
pub fn table_id(&self) -> u64 {
self.event.table_id
}
pub fn flags(&self) -> u16 {
self.event.flags
}
pub fn kind(&self) -> RowsKind {
self.event.kind
}
pub fn column_count(&self) -> usize {
self.column_count
}
pub fn rows(self) -> TypedRows<'a, M> {
TypedRows {
rows: self.rows,
done: false,
mode: PhantomData,
}
}
}
pub struct TypedRows<'a, M: ImageMode> {
rows: Rows<'a>,
done: bool,
mode: PhantomData<M>,
}
impl<'a, M: ImageMode> Iterator for TypedRows<'a, M> {
type Item = Result<TypedRow<'a, M>>;
fn next(&mut self) -> Option<Self::Item> {
if self.done {
return None;
}
let result = self.rows.next()?.and_then(|row| {
Ok(TypedRow {
before: row.before.map(M::Image::try_from).transpose()?,
after: row.after.map(M::Image::try_from).transpose()?,
})
});
self.done = result.is_err();
Some(result)
}
}
impl<M: ImageMode> FusedIterator for TypedRows<'_, M> {}
#[derive(Debug)]
pub struct TypedRowsDecoder<M: ImageMode> {
decoder: RowsDecoder,
mode: PhantomData<M>,
}
impl<M: ImageMode> TypedRowsDecoder<M> {
pub fn new(table: &TableMapEvent<'_>) -> Result<Self> {
RowsDecoder::new(table).map(Self::from)
}
pub fn table_id(&self) -> u64 {
self.decoder.table_id()
}
pub fn columns(&self) -> &[ColumnDefinition] {
self.decoder.columns()
}
pub fn event<'a>(&self, event: &RowsEvent<'a>) -> Result<TypedRowsEvent<'a, M>> {
event.with_mode::<M>(&self.decoder)
}
pub fn rows<'a>(&self, event: &RowsEvent<'a>) -> Result<TypedRows<'a, M>> {
self.event(event).map(TypedRowsEvent::rows)
}
}
impl<M: ImageMode> From<RowsDecoder> for TypedRowsDecoder<M> {
fn from(decoder: RowsDecoder) -> Self {
Self {
decoder,
mode: PhantomData,
}
}
}