mod.rs 865 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. use bytes::Bytes;
  2. use parking_lot::Mutex;
  3. struct State {
  4. bytes: bytes::Bytes,
  5. snapshot: Vec<u8>,
  6. }
  7. pub struct Persister {
  8. state: Mutex<State>,
  9. }
  10. impl Persister {
  11. pub fn new() -> Self {
  12. Self {
  13. state: Mutex::new(State {
  14. bytes: bytes::Bytes::new(),
  15. snapshot: vec![],
  16. }),
  17. }
  18. }
  19. }
  20. impl ruaft::Persister for Persister {
  21. fn read_state(&self) -> bytes::Bytes {
  22. self.state.lock().bytes.clone()
  23. }
  24. fn save_state(&self, data: bytes::Bytes) {
  25. self.state.lock().bytes = data;
  26. }
  27. fn state_size(&self) -> usize {
  28. self.state.lock().bytes.len()
  29. }
  30. fn save_snapshot_and_state(&self, state: Bytes, snapshot: &[u8]) {
  31. let mut this = self.state.lock();
  32. this.bytes = state;
  33. this.snapshot = snapshot.to_vec();
  34. }
  35. }