1mod merkle;
17mod store;
18mod sync;
19mod types;
20
21pub use merkle::MerkleTree;
22pub use store::{CrdtStore, LwwRegister};
23pub use sync::CrdtSyncService;
24pub use types::{GCounter, MvRegister, OrSet, PnCounter};
25
26use async_trait::async_trait;
27use bytes::Bytes;
28use pollen_types::{NodeId, Result};
29use serde::{de::DeserializeOwned, Serialize};
30use std::sync::Arc;
31use tokio::sync::broadcast;
32
33pub trait CrdtValue: Serialize + DeserializeOwned + Clone + Send + Sync + 'static {
35 fn merge(&mut self, other: &Self);
37}
38
39#[async_trait]
41pub trait CrdtKv: Send + Sync + 'static {
42 fn get<T: CrdtValue>(&self, key: &str) -> Option<T>;
44
45 async fn set<T: CrdtValue>(&self, key: &str, value: T) -> Result<()>;
47
48 async fn delete(&self, key: &str) -> Result<()>;
50
51 fn subscribe(&self, prefix: &str) -> broadcast::Receiver<CrdtEvent>;
53
54 async fn sync_with(&self, peer: NodeId) -> Result<()>;
56
57 fn keys(&self) -> Vec<String>;
59
60 fn keys_with_prefix(&self, prefix: &str) -> Vec<String>;
62}
63
64#[derive(Clone, Debug)]
66pub enum CrdtEvent {
67 Updated { key: String },
69 Deleted { key: String },
71}
72
73pub type SharedCrdtKv = Arc<dyn CrdtKv>;
75
76#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
78pub struct CrdtEntry {
79 pub key: String,
81 pub crdt_type: String,
83 pub data: Bytes,
85 pub timestamp: u64,
87 pub deleted: bool,
89}
90
91impl CrdtEntry {
92 pub fn new(key: String, crdt_type: String, data: Bytes, timestamp: u64) -> Self {
94 Self {
95 key,
96 crdt_type,
97 data,
98 timestamp,
99 deleted: false,
100 }
101 }
102
103 pub fn tombstone(key: String, timestamp: u64) -> Self {
105 Self {
106 key,
107 crdt_type: "tombstone".to_string(),
108 data: Bytes::new(),
109 timestamp,
110 deleted: true,
111 }
112 }
113}