Skip to main content

rustium_core/
traits.rs

1use std::time::SystemTime;
2
3use async_trait::async_trait;
4use serde::{Deserialize, Serialize};
5use tokio::sync::{mpsc, watch};
6use tokio_util::sync::CancellationToken;
7
8use crate::{
9    ChangeEvent, ConnectorStateEnvelope, DeliveryBatch, EncodedEvent, Result, SignalDelivery,
10    SourcePosition, SourceRecord,
11};
12
13pub const CHECKPOINT_SCHEMA_VERSION: u32 = 2;
14
15#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
16pub struct Checkpoint {
17    pub schema_version: u32,
18    pub connector_name: String,
19    pub generation: uuid::Uuid,
20    pub source_position: SourcePosition,
21    pub snapshot_completed: bool,
22    pub config_fingerprint: String,
23    pub updated_at: SystemTime,
24    #[serde(default, skip_serializing_if = "Option::is_none")]
25    pub connector_state: Option<ConnectorStateEnvelope>,
26}
27
28#[async_trait]
29pub trait CheckpointStore: Send + Sync {
30    async fn load(&self, connector_name: &str) -> Result<Option<Checkpoint>>;
31    async fn save(&self, checkpoint: &Checkpoint) -> Result<()>;
32    async fn delete(&self, connector_name: &str) -> Result<()>;
33}
34
35pub struct SourceContext {
36    pub output: mpsc::Sender<Result<SourceRecord>>,
37    pub acknowledged: watch::Receiver<Option<SourcePosition>>,
38    pub initial_checkpoint: Option<Checkpoint>,
39    pub signals: mpsc::Receiver<SignalDelivery>,
40    pub cancellation: CancellationToken,
41}
42
43#[async_trait]
44pub trait SourceConnector: Send {
45    fn source_type(&self) -> &'static str;
46    async fn validate(&mut self) -> Result<()>;
47    async fn run(&mut self, context: SourceContext) -> Result<()>;
48}
49
50pub trait EventEncoder: Send + Sync {
51    fn content_type(&self) -> &'static str;
52    fn encode(&self, event: &ChangeEvent) -> Result<EncodedEvent>;
53
54    fn encode_batch(&self, event: &ChangeEvent) -> Result<Vec<EncodedEvent>> {
55        Ok(vec![self.encode(event)?])
56    }
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
60pub enum Durability {
61    BestEffort,
62    Durable,
63}
64
65#[async_trait]
66pub trait Sink: Send {
67    fn name(&self) -> &'static str;
68    fn durability(&self) -> Durability;
69    async fn validate(&mut self) -> Result<()>;
70    async fn write(&mut self, batch: &DeliveryBatch) -> Result<()>;
71    async fn flush(&mut self) -> Result<()>;
72    async fn shutdown(&mut self) -> Result<()> {
73        self.flush().await
74    }
75}