Skip to main content

crabka_replicator/mm2/
checkpoint.rs

1use crate::error::ReplicatorError;
2
3use super::{Reader, Writer};
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct Checkpoint {
7    pub group: String,
8    pub topic: String,
9    pub partition: i32,
10    pub upstream: i64,
11    pub downstream: i64,
12    pub metadata: String,
13}
14
15impl Checkpoint {
16    #[must_use]
17    pub fn topic_name(source_alias: &str) -> String {
18        format!("{source_alias}.checkpoints.internal")
19    }
20
21    #[must_use]
22    pub fn key_bytes(&self) -> Vec<u8> {
23        Writer::keyless()
24            .string(&self.group)
25            .string(&self.topic)
26            .i32(self.partition)
27            .finish()
28    }
29
30    #[must_use]
31    pub fn value_bytes(&self) -> Vec<u8> {
32        Writer::new()
33            .i64(self.upstream)
34            .i64(self.downstream)
35            .string(&self.metadata)
36            .finish()
37    }
38
39    pub fn from_bytes(key: &[u8], val: &[u8]) -> Result<Self, ReplicatorError> {
40        let mut k = Reader::keyless(key);
41        let mut v = Reader::new(val)?;
42        Ok(Self {
43            group: k.string()?,
44            topic: k.string()?,
45            partition: k.i32()?,
46            upstream: v.i64()?,
47            downstream: v.i64()?,
48            metadata: v.string()?,
49        })
50    }
51}