mod.rs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. mod document_impl;
  2. mod grid_block_impl;
  3. mod grid_impl;
  4. pub use document_impl::*;
  5. pub use grid_block_impl::*;
  6. pub use grid_impl::*;
  7. use flowy_error::FlowyResult;
  8. use flowy_sync::entities::revision::{RevId, Revision, RevisionRange};
  9. use std::fmt::Debug;
  10. pub trait RevisionDiskCache: Sync + Send {
  11. type Error: Debug;
  12. fn create_revision_records(&self, revision_records: Vec<RevisionRecord>) -> Result<(), Self::Error>;
  13. // Read all the records if the rev_ids is None
  14. fn read_revision_records(
  15. &self,
  16. object_id: &str,
  17. rev_ids: Option<Vec<i64>>,
  18. ) -> Result<Vec<RevisionRecord>, Self::Error>;
  19. // Read the revision which rev_id >= range.start && rev_id <= range.end
  20. fn read_revision_records_with_range(
  21. &self,
  22. object_id: &str,
  23. range: &RevisionRange,
  24. ) -> Result<Vec<RevisionRecord>, Self::Error>;
  25. fn update_revision_record(&self, changesets: Vec<RevisionChangeset>) -> FlowyResult<()>;
  26. // Delete all the records if the rev_ids is None
  27. fn delete_revision_records(&self, object_id: &str, rev_ids: Option<Vec<i64>>) -> Result<(), Self::Error>;
  28. // Delete and insert will be executed in the same transaction.
  29. // It deletes all the records if the deleted_rev_ids is None and then insert the new records
  30. fn delete_and_insert_records(
  31. &self,
  32. object_id: &str,
  33. deleted_rev_ids: Option<Vec<i64>>,
  34. inserted_records: Vec<RevisionRecord>,
  35. ) -> Result<(), Self::Error>;
  36. }
  37. #[derive(Clone, Debug)]
  38. pub struct RevisionRecord {
  39. pub revision: Revision,
  40. pub state: RevisionState,
  41. pub write_to_disk: bool,
  42. }
  43. impl RevisionRecord {
  44. pub fn new(revision: Revision) -> Self {
  45. Self {
  46. revision,
  47. state: RevisionState::Sync,
  48. write_to_disk: true,
  49. }
  50. }
  51. pub fn ack(&mut self) {
  52. self.state = RevisionState::Ack;
  53. }
  54. }
  55. pub struct RevisionChangeset {
  56. pub(crate) object_id: String,
  57. pub(crate) rev_id: RevId,
  58. pub(crate) state: RevisionState,
  59. }
  60. /// Sync: revision is not synced to the server
  61. /// Ack: revision is synced to the server
  62. #[derive(Debug, Clone, Eq, PartialEq)]
  63. pub enum RevisionState {
  64. Sync = 0,
  65. Ack = 1,
  66. }
  67. impl RevisionState {
  68. pub fn is_need_sync(&self) -> bool {
  69. match self {
  70. RevisionState::Sync => true,
  71. RevisionState::Ack => false,
  72. }
  73. }
  74. }
  75. impl AsRef<RevisionState> for RevisionState {
  76. fn as_ref(&self) -> &RevisionState {
  77. self
  78. }
  79. }