datastore.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. use std::marker::PhantomData;
  2. use log::debug;
  3. use sled::Batch;
  4. use crate::{
  5. util::serial::{deserialize, serialize, Decodable, Encodable},
  6. Error, 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 last_index: u64 = if let Some(d) = self.tree.last()? {
  48. u64::from_be_bytes(d.0.to_vec().try_into().unwrap())
  49. } else {
  50. 0
  51. };
  52. self.tree.insert(last_index.to_be_bytes(), serialized)?;
  53. Ok(())
  54. }
  55. pub fn wipe_insert_all(&self, data: &Vec<T>) -> Result<()> {
  56. self.tree.clear()?;
  57. let mut batch = Batch::default();
  58. for (i, d) in data.iter().enumerate() {
  59. let serialized = serialize(d);
  60. batch.insert(&(i as u64).to_be_bytes(), serialized);
  61. }
  62. self.tree.apply_batch(batch)?;
  63. Ok(())
  64. }
  65. pub fn get_all(&self) -> Result<Vec<T>> {
  66. let mut ret: Vec<T> = Vec::new();
  67. for i in self.tree.iter() {
  68. let da = deserialize(&i?.1)?;
  69. ret.push(da)
  70. }
  71. Ok(ret)
  72. }
  73. pub fn len(&self) -> u64 {
  74. self.tree.len() as u64
  75. }
  76. pub fn get_last(&self) -> Result<Option<T>> {
  77. if let Some(found) = self.tree.last()? {
  78. let da = deserialize(&found.1)?;
  79. return Ok(Some(da))
  80. }
  81. Ok(None)
  82. }
  83. pub fn get(&self, index: u64) -> Result<T> {
  84. let index_bytes = index.to_be_bytes();
  85. if let Some(found) = self.tree.get(index_bytes)? {
  86. let da = deserialize(&found)?;
  87. return Ok(da)
  88. }
  89. Err(Error::RaftError(format!(
  90. "Unable to get the item with index {} {:?}",
  91. index,
  92. self.is_empty()
  93. )))
  94. }
  95. pub fn is_empty(&self) -> bool {
  96. self.tree.is_empty()
  97. }
  98. }