mysql_replication/model/
change.rs1use crate::TableMeta;
2use serde::{
3 Serialize, Serializer,
4 ser::{SerializeSeq, SerializeStruct},
5};
6use std::{borrow::Cow, sync::Arc};
7
8#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
9#[serde(tag = "type", content = "value", rename_all = "snake_case")]
10pub enum PrimaryKey<'a> {
11 Signed(i64),
12 Unsigned(u64),
13 Text(Cow<'a, str>),
14 Decimal(String),
16}
17
18impl PrimaryKey<'_> {
19 pub fn into_owned(self) -> PrimaryKey<'static> {
20 match self {
21 Self::Signed(v) => PrimaryKey::Signed(v),
22 Self::Unsigned(v) => PrimaryKey::Unsigned(v),
23 Self::Text(v) => PrimaryKey::Text(Cow::Owned(v.into_owned())),
24 Self::Decimal(v) => PrimaryKey::Decimal(v),
25 }
26 }
27 pub(crate) fn heap_bytes(&self) -> usize {
28 match self {
29 Self::Text(v) => v.len(),
30 Self::Decimal(v) => v.len(),
31 _ => 0,
32 }
33 }
34}
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
37#[serde(rename_all = "snake_case")]
38pub enum Operation {
39 Insert,
40 Update,
41 Delete,
42}
43
44#[derive(Debug)]
46pub struct Change<'a> {
47 pub table: &'a Arc<TableMeta>,
48 pub operation: Operation,
49 pub pk: PrimaryKey<'a>,
50 pub previous_pk: Option<PrimaryKey<'a>>,
51 pub changed: &'a Arc<[u16]>,
53}
54
55impl Change<'_> {
56 pub fn into_owned(self) -> OwnedChange {
57 OwnedChange {
58 table: Arc::clone(self.table),
59 operation: self.operation,
60 pk: self.pk.into_owned(),
61 previous_pk: self.previous_pk.map(PrimaryKey::into_owned),
62 changed: Arc::clone(self.changed),
63 }
64 }
65}
66
67#[derive(Debug, Clone)]
68pub struct OwnedChange {
69 pub table: Arc<TableMeta>,
70 pub operation: Operation,
71 pub pk: PrimaryKey<'static>,
72 pub previous_pk: Option<PrimaryKey<'static>>,
73 pub changed: Arc<[u16]>,
74}
75
76impl OwnedChange {
77 pub fn changed_names(&self) -> impl ExactSizeIterator<Item = &str> {
78 self.changed
79 .iter()
80 .map(|&i| self.table.columns[usize::from(i)].name.as_str())
81 }
82 pub(crate) fn memory_bytes(&self) -> usize {
83 std::mem::size_of::<Self>()
84 + self.pk.heap_bytes()
85 + self.previous_pk.as_ref().map_or(0, PrimaryKey::heap_bytes)
86 + self.changed.len() * 2
87 }
88}
89
90struct Names<'a>(&'a TableMeta, &'a [u16]);
91impl Serialize for Names<'_> {
92 fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
93 let mut seq = serializer.serialize_seq(Some(self.1.len()))?;
94 for &i in self.1 {
95 let col = self
96 .0
97 .columns
98 .get(usize::from(i))
99 .ok_or_else(|| serde::ser::Error::custom("invalid column index"))?;
100 seq.serialize_element(&col.name)?;
101 }
102 seq.end()
103 }
104}
105
106impl Serialize for OwnedChange {
107 fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
108 let mut out = serializer
109 .serialize_struct("Change", if self.previous_pk.is_some() { 6 } else { 5 })?;
110 out.serialize_field("schema", &self.table.schema)?;
111 out.serialize_field("table", &self.table.table)?;
112 out.serialize_field("operation", &self.operation)?;
113 out.serialize_field("pk", &self.pk)?;
114 if let Some(pk) = &self.previous_pk {
115 out.serialize_field("previous_pk", pk)?;
116 }
117 out.serialize_field("changed", &Names(&self.table, &self.changed))?;
118 out.end()
119 }
120}