mod.rs 939 B

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