Skip to main content

rabia_kvstore_example/
lib.rs

1//! # KVStore SMR Example
2//!
3//! A key-value store implementation that demonstrates how to create a State Machine
4//! Replication (SMR) application using the Rabia consensus protocol.
5//!
6//! ## Features
7//!
8//! - **SMR Implementation**: Clean implementation of the StateMachine trait
9//! - **Key-Value Operations**: Support for SET, GET, DELETE, and EXISTS operations
10//! - **Change Notifications**: Event-driven updates via message bus
11//! - **State Serialization**: Full state serialization for consensus integration
12//! - **Production Ready**: Comprehensive error handling and monitoring
13//!
14//! ## Example Usage
15//!
16//! ```rust
17//! use rabia_kvstore_example::{KVStoreSMR, KVOperation, KVStoreConfig};
18//! use rabia_core::smr::StateMachine;
19//!
20//! #[tokio::main]
21//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
22//!     let mut kvstore = KVStoreSMR::new_default().await?;
23//!     
24//!     let command = KVOperation::Set {
25//!         key: "hello".to_string(),
26//!         value: "world".to_string(),
27//!     };
28//!     
29//!     let response = kvstore.apply_command(command).await;
30//!     println!("Response: {:?}", response);
31//!     
32//!     Ok(())
33//! }
34//! ```
35
36pub mod notifications;
37pub mod operations;
38pub mod smr_impl;
39pub mod store;
40
41pub use notifications::{ChangeNotification, NotificationBus, SubscriptionId};
42pub use operations::{KVOperation, KVResult, StoreError};
43pub use smr_impl::{KVStoreSMR, KVStoreState};
44pub use store::{KVStore, KVStoreConfig, StoreSnapshot};
45
46/// Re-export commonly used types for convenience
47pub use rabia_core::smr::StateMachine;
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[tokio::test]
54    async fn test_kvstore_basic_operations() {
55        let config = KVStoreConfig::default();
56        let store = KVStore::new(config).await.unwrap();
57
58        // Test basic SET/GET operations
59        let result = store.set("key1", "value1").await.unwrap();
60        assert!(result.is_success());
61
62        let value = store.get("key1").await.unwrap();
63        assert_eq!(value.unwrap(), "value1");
64
65        // Test DELETE
66        let result = store.delete("key1").await.unwrap();
67        assert!(result.is_success());
68
69        let value = store.get("key1").await.unwrap();
70        assert!(value.is_none());
71    }
72}