Skip to main content

kora_core/shard/
wal_trait.rs

1//! WAL writer trait for per-shard storage integration.
2//!
3//! This trait allows the storage layer to be injected into the shard engine
4//! without creating a circular dependency between `kora-core` and `kora-storage`.
5
6/// A mutation record that can be logged to a WAL.
7///
8/// This is a core-level type that mirrors the storage layer's `WalEntry`
9/// without depending on the storage crate.
10#[derive(Debug, Clone, PartialEq)]
11pub enum WalRecord {
12    /// SET key value \[ttl_ms\]
13    Set {
14        /// The key.
15        key: Vec<u8>,
16        /// The value.
17        value: Vec<u8>,
18        /// Optional TTL in milliseconds.
19        ttl_ms: Option<u64>,
20    },
21    /// DEL key
22    Del {
23        /// The key to delete.
24        key: Vec<u8>,
25    },
26    /// EXPIRE key ttl_ms
27    Expire {
28        /// The key.
29        key: Vec<u8>,
30        /// TTL in milliseconds.
31        ttl_ms: u64,
32    },
33    /// LPUSH key values...
34    LPush {
35        /// The key.
36        key: Vec<u8>,
37        /// Values to push.
38        values: Vec<Vec<u8>>,
39    },
40    /// RPUSH key values...
41    RPush {
42        /// The key.
43        key: Vec<u8>,
44        /// Values to push.
45        values: Vec<Vec<u8>>,
46    },
47    /// HSET key field value
48    HSet {
49        /// The key.
50        key: Vec<u8>,
51        /// The field-value pairs.
52        fields: Vec<(Vec<u8>, Vec<u8>)>,
53    },
54    /// SADD key members...
55    SAdd {
56        /// The key.
57        key: Vec<u8>,
58        /// Members to add.
59        members: Vec<Vec<u8>>,
60    },
61    /// FLUSHDB — clear all keys.
62    FlushDb,
63    /// DOC.SET collection doc_id json
64    DocSet {
65        /// Collection name.
66        collection: Vec<u8>,
67        /// External document ID.
68        doc_id: Vec<u8>,
69        /// JSON payload bytes.
70        json: Vec<u8>,
71    },
72    /// DOC.DEL collection doc_id
73    DocDel {
74        /// Collection name.
75        collection: Vec<u8>,
76        /// External document ID.
77        doc_id: Vec<u8>,
78    },
79    /// VECSET key dimensions vector\_bytes
80    VecSet {
81        /// The key.
82        key: Vec<u8>,
83        /// Vector dimensions.
84        dimensions: usize,
85        /// Raw f32 values as LE bytes (each f32 is 4 bytes).
86        vector: Vec<u8>,
87    },
88    /// VECDEL key
89    VecDel {
90        /// The key.
91        key: Vec<u8>,
92    },
93}
94
95/// Trait for writing WAL entries from a shard worker thread.
96///
97/// Implementations are expected to be `Send` so they can be moved into
98/// worker threads. WAL writes happen synchronously on the shard's own thread.
99pub trait WalWriter: Send {
100    /// Append a mutation record to the WAL.
101    fn append(&mut self, record: &WalRecord);
102
103    /// Truncate the WAL file as part of maintenance operations.
104    ///
105    /// Default implementation is a no-op for in-memory or non-persistent backends.
106    fn truncate(&mut self) -> Result<(), String> {
107        Ok(())
108    }
109}