Skip to main content

contextdb_engine/
plugin.rs

1use crate::sync_types::{ChangeSet, DdlChange};
2use contextdb_core::Result;
3use contextdb_tx::WriteSet;
4use std::time::Duration;
5
6/// Source of a commit operation.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum CommitSource {
10    User,
11    AutoCommit,
12    SyncPull,
13}
14
15/// Outcome of a query execution.
16#[derive(Debug, Clone, PartialEq)]
17pub enum QueryOutcome {
18    Success { row_count: usize },
19    Error { error: String },
20}
21
22/// Health status reported by a plugin.
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub enum PluginHealth {
25    Healthy,
26    Degraded(String),
27    Unhealthy(String),
28}
29
30/// Lifecycle hooks for the database engine.
31pub trait DatabasePlugin: Send + Sync {
32    fn pre_commit(&self, _ws: &WriteSet, _source: CommitSource) -> Result<()> {
33        Ok(())
34    }
35    fn post_commit(&self, _ws: &WriteSet, _source: CommitSource) {}
36    fn on_open(&self) -> Result<()> {
37        Ok(())
38    }
39    fn on_close(&self) -> Result<()> {
40        Ok(())
41    }
42    fn on_ddl(&self, _change: &DdlChange) -> Result<()> {
43        Ok(())
44    }
45    fn on_query(&self, _sql: &str) -> Result<()> {
46        Ok(())
47    }
48    fn post_query(&self, _sql: &str, _duration: Duration, _outcome: &QueryOutcome) {}
49    fn health(&self) -> PluginHealth {
50        PluginHealth::Healthy
51    }
52    fn describe(&self) -> serde_json::Value {
53        serde_json::json!({})
54    }
55    fn on_sync_push(&self, _changeset: &mut ChangeSet) -> Result<()> {
56        Ok(())
57    }
58    fn on_sync_pull(&self, _changeset: &mut ChangeSet) -> Result<()> {
59        Ok(())
60    }
61}
62
63/// Lightweight summary of each commit, delivered to subscribers.
64#[derive(Debug, Clone, PartialEq)]
65pub struct CommitEvent {
66    pub source: CommitSource,
67    pub lsn: u64,
68    pub tables_changed: Vec<String>,
69    pub row_count: usize,
70}
71
72/// Health metrics for the subscription system.
73#[derive(Debug, Clone, PartialEq, Eq)]
74pub struct SubscriptionMetrics {
75    pub active_channels: usize,
76    pub events_sent: u64,
77    pub events_dropped: u64,
78}
79
80/// Default plugin — all no-ops. Every Database gets this automatically.
81pub struct CorePlugin;
82impl DatabasePlugin for CorePlugin {}