storage.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. use raft::storage::{
  2. RaftLogEntryRef, RaftStorageMonitorTrait, RaftStoragePersisterTrait,
  3. RaftStorageTrait, RaftStoredState,
  4. };
  5. use raft::{Index, Term};
  6. #[derive(Default)]
  7. pub struct DoNothingRaftStorage;
  8. impl RaftStorageTrait for DoNothingRaftStorage {
  9. type RaftStoragePersister<LogEntry: RaftLogEntryRef> =
  10. DoNothingRaftStoragePersister;
  11. type RaftStorageMonitor = DoNothingRaftStorageMonitor;
  12. fn persister<LogEntry>(
  13. self,
  14. ) -> std::sync::Arc<DoNothingRaftStoragePersister>
  15. where
  16. LogEntry: RaftLogEntryRef,
  17. {
  18. std::sync::Arc::new(DoNothingRaftStoragePersister)
  19. }
  20. fn read_state(&self) -> std::io::Result<RaftStoredState> {
  21. Ok(RaftStoredState {
  22. current_term: Term(0),
  23. voted_for: "".to_string(),
  24. log: vec![],
  25. snapshot_index: 0,
  26. snapshot_term: Term(0),
  27. snapshot: vec![],
  28. })
  29. }
  30. fn log_compaction_required(&self) -> bool {
  31. false
  32. }
  33. fn monitor(&self) -> DoNothingRaftStorageMonitor {
  34. DoNothingRaftStorageMonitor
  35. }
  36. }
  37. pub struct DoNothingRaftStorageMonitor;
  38. impl RaftStorageMonitorTrait for DoNothingRaftStorageMonitor {
  39. fn should_compact_log_now(&self) -> bool {
  40. return false;
  41. }
  42. }
  43. pub struct DoNothingRaftStoragePersister;
  44. impl<LogEntry: RaftLogEntryRef> RaftStoragePersisterTrait<LogEntry>
  45. for DoNothingRaftStoragePersister
  46. {
  47. fn save_term_vote(&self, _term: Term, _voted_for: String) {}
  48. fn append_one_entry(&self, _entry: &LogEntry) {}
  49. fn append_entries<'a, LogEntryList>(&self, _entries: LogEntryList)
  50. where
  51. LogEntry: 'a,
  52. LogEntryList: IntoIterator<Item = &'a LogEntry>,
  53. {
  54. }
  55. fn update_snapshot(&self, _index: Index, _term: Term, _snapshot: &[u8]) {}
  56. }