Skip to main content

danube_core/metadata/
store.rs

1use async_trait::async_trait;
2use serde_json::Value;
3use std::time::Duration;
4
5use super::errors::Result;
6use super::watch::WatchStream;
7
8/// A key-value-version tuple returned by bulk queries.
9#[derive(Debug)]
10pub struct KeyValueVersion {
11    pub key: String,
12    pub value: Vec<u8>,
13    pub version: i64,
14}
15
16/// Backend-agnostic options for metadata store operations.
17#[derive(Debug)]
18pub enum MetaOptions {
19    None,
20    /// Treat the key as a prefix and return all matching entries.
21    WithPrefix,
22    /// Return the previous key-value pair before the operation.
23    WithPrevKey,
24}
25
26#[async_trait]
27pub trait MetadataStore: Send + Sync + 'static {
28    async fn get(&self, key: &str, get_options: MetaOptions) -> Result<Option<Value>>;
29    async fn get_childrens(&self, path: &str) -> Result<Vec<String>>;
30    async fn put(&self, key: &str, value: Value, put_options: MetaOptions) -> Result<()>;
31    async fn delete(&self, key: &str) -> Result<()>;
32    async fn watch(&self, prefix: &str) -> Result<WatchStream>;
33
34    /// Put a key with a time-to-live. The key is automatically deleted after `ttl`.
35    async fn put_with_ttl(&self, key: &str, value: Value, ttl: Duration) -> Result<()>;
36
37    /// Retrieve all key-value pairs under a given prefix.
38    async fn get_bulk(&self, prefix: &str) -> Result<Vec<KeyValueVersion>>;
39
40    /// Atomically increment and return a monotonic counter stored at `counter_key`.
41    ///
42    /// Used for schema ID allocation — eliminates the read-modify-write race
43    /// by going through Raft consensus as a single atomic command.
44    async fn allocate_monotonic_id(&self, counter_key: &str) -> Result<u64>;
45}