datastore.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. use std::marker::PhantomData;
  2. use log::debug;
  3. use sled::Batch;
  4. use crate::{
  5. util::serial::{deserialize, serialize, Decodable, Encodable},
  6. Result,
  7. };
  8. use super::primitives::{Log, NodeId};
  9. const SLED_LOGS_TREE: &[u8] = b"_logs";
  10. const SLED_COMMITS_TREE: &[u8] = b"_commits";
  11. const _SLED_COMMITS_LENGTH_TREE: &[u8] = b"_commit_length";
  12. const SLED_VOTED_FOR_TREE: &[u8] = b"_voted_for";
  13. const SLED_CURRENT_TERM_TREE: &[u8] = b"_current_term";
  14. pub struct DataStore<T> {
  15. _db: sled::Db,
  16. pub logs: DataTree<Log>,
  17. pub commits: DataTree<T>,
  18. pub voted_for: DataTree<Option<NodeId>>,
  19. pub current_term: DataTree<u64>,
  20. }
  21. impl<T: Encodable + Decodable> DataStore<T> {
  22. pub fn new(db_path: &str) -> Result<Self> {
  23. let _db = sled::open(db_path)?;
  24. let logs = DataTree::new(&_db, SLED_LOGS_TREE)?;
  25. let commits = DataTree::new(&_db, SLED_COMMITS_TREE)?;
  26. let voted_for = DataTree::new(&_db, SLED_VOTED_FOR_TREE)?;
  27. let current_term = DataTree::new(&_db, SLED_CURRENT_TERM_TREE)?;
  28. Ok(Self { _db, logs, commits, voted_for, current_term })
  29. }
  30. pub async fn flush(&self) -> Result<()> {
  31. debug!(target: "raft", "DataStore flush");
  32. self._db.flush_async().await?;
  33. Ok(())
  34. }
  35. }
  36. pub struct DataTree<T> {
  37. tree: sled::Tree,
  38. phantom: PhantomData<T>,
  39. }
  40. impl<T: Decodable + Encodable> DataTree<T> {
  41. pub fn new(db: &sled::Db, tree_name: &[u8]) -> Result<Self> {
  42. let tree = db.open_tree(tree_name)?;
  43. Ok(Self { tree, phantom: PhantomData })
  44. }
  45. pub fn insert(&self, data: &T) -> Result<()> {
  46. let serialized = serialize(data);
  47. let datahash = blake3::hash(&serialized);
  48. self.tree.insert(datahash.as_bytes(), serialized)?;
  49. Ok(())
  50. }
  51. pub fn wipe_insert_all(&self, data: &Vec<T>) -> Result<()> {
  52. self.tree.clear()?;
  53. let mut batch = Batch::default();
  54. for i in data {
  55. let serialized = serialize(i);
  56. let hash = blake3::hash(&serialized);
  57. batch.insert(hash.as_bytes(), serialized);
  58. }
  59. self.tree.apply_batch(batch)?;
  60. Ok(())
  61. }
  62. pub fn get_all(&self) -> Result<Vec<T>> {
  63. let mut ret: Vec<T> = Vec::new();
  64. for i in self.tree.iter() {
  65. let da = deserialize(&i?.1)?;
  66. ret.push(da)
  67. }
  68. Ok(ret)
  69. }
  70. pub fn len(&self) -> u64 {
  71. self.tree.len() as u64
  72. }
  73. pub fn get_last(&self) -> Result<Option<T>> {
  74. if let Some(found) = self.tree.last()? {
  75. let da = deserialize(&found.1)?;
  76. return Ok(Some(da))
  77. }
  78. Ok(None)
  79. }
  80. pub fn is_empty(&self) -> bool {
  81. self.tree.is_empty()
  82. }
  83. }