| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- use bytes::Bytes;
- use parking_lot::Mutex;
- struct State {
- bytes: bytes::Bytes,
- snapshot: Vec<u8>,
- }
- pub struct Persister {
- state: Mutex<State>,
- }
- impl Persister {
- pub fn new() -> Self {
- Self {
- state: Mutex::new(State {
- bytes: bytes::Bytes::new(),
- snapshot: vec![],
- }),
- }
- }
- }
- impl ruaft::Persister for Persister {
- fn read_state(&self) -> bytes::Bytes {
- self.state.lock().bytes.clone()
- }
- fn save_state(&self, data: bytes::Bytes) {
- self.state.lock().bytes = data;
- }
- fn state_size(&self) -> usize {
- self.state.lock().bytes.len()
- }
- fn save_snapshot_and_state(&self, state: Bytes, snapshot: &[u8]) {
- let mut this = self.state.lock();
- this.bytes = state;
- this.snapshot = snapshot.to_vec();
- }
- }
|