lib.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. mod disk_cache_impl;
  2. use flowy_error::{FlowyError, FlowyResult};
  3. use revision_model::{Revision, RevisionRange};
  4. use std::fmt::Debug;
  5. use std::sync::Arc;
  6. pub trait RevisionDiskCache<Connection>: Sync + Send {
  7. type Error: Debug;
  8. fn create_revision_records(&self, revision_records: Vec<SyncRecord>) -> Result<(), Self::Error>;
  9. fn get_connection(&self) -> Result<Connection, Self::Error>;
  10. // Read all the records if the rev_ids is None
  11. fn read_revision_records(
  12. &self,
  13. object_id: &str,
  14. rev_ids: Option<Vec<i64>>,
  15. ) -> Result<Vec<SyncRecord>, Self::Error>;
  16. // Read the revision which rev_id >= range.start && rev_id <= range.end
  17. fn read_revision_records_with_range(
  18. &self,
  19. object_id: &str,
  20. range: &RevisionRange,
  21. ) -> Result<Vec<SyncRecord>, Self::Error>;
  22. fn update_revision_record(&self, changesets: Vec<RevisionChangeset>) -> FlowyResult<()>;
  23. // Delete all the records if the rev_ids is None
  24. fn delete_revision_records(
  25. &self,
  26. object_id: &str,
  27. rev_ids: Option<Vec<i64>>,
  28. ) -> Result<(), Self::Error>;
  29. // Delete and insert will be executed in the same transaction.
  30. // It deletes all the records if the deleted_rev_ids is None and then insert the new records
  31. fn delete_and_insert_records(
  32. &self,
  33. object_id: &str,
  34. deleted_rev_ids: Option<Vec<i64>>,
  35. inserted_records: Vec<SyncRecord>,
  36. ) -> Result<(), Self::Error>;
  37. }
  38. impl<T, Connection> RevisionDiskCache<Connection> for Arc<T>
  39. where
  40. T: RevisionDiskCache<Connection, Error = FlowyError>,
  41. {
  42. type Error = FlowyError;
  43. fn create_revision_records(&self, revision_records: Vec<SyncRecord>) -> Result<(), Self::Error> {
  44. (**self).create_revision_records(revision_records)
  45. }
  46. fn get_connection(&self) -> Result<Connection, Self::Error> {
  47. (**self).get_connection()
  48. }
  49. fn read_revision_records(
  50. &self,
  51. object_id: &str,
  52. rev_ids: Option<Vec<i64>>,
  53. ) -> Result<Vec<SyncRecord>, Self::Error> {
  54. (**self).read_revision_records(object_id, rev_ids)
  55. }
  56. fn read_revision_records_with_range(
  57. &self,
  58. object_id: &str,
  59. range: &RevisionRange,
  60. ) -> Result<Vec<SyncRecord>, Self::Error> {
  61. (**self).read_revision_records_with_range(object_id, range)
  62. }
  63. fn update_revision_record(&self, changesets: Vec<RevisionChangeset>) -> FlowyResult<()> {
  64. (**self).update_revision_record(changesets)
  65. }
  66. fn delete_revision_records(
  67. &self,
  68. object_id: &str,
  69. rev_ids: Option<Vec<i64>>,
  70. ) -> Result<(), Self::Error> {
  71. (**self).delete_revision_records(object_id, rev_ids)
  72. }
  73. fn delete_and_insert_records(
  74. &self,
  75. object_id: &str,
  76. deleted_rev_ids: Option<Vec<i64>>,
  77. inserted_records: Vec<SyncRecord>,
  78. ) -> Result<(), Self::Error> {
  79. (**self).delete_and_insert_records(object_id, deleted_rev_ids, inserted_records)
  80. }
  81. }
  82. #[derive(Clone, Debug)]
  83. pub struct SyncRecord {
  84. pub revision: Revision,
  85. pub state: RevisionState,
  86. pub write_to_disk: bool,
  87. }
  88. impl SyncRecord {
  89. pub fn new(revision: Revision) -> Self {
  90. Self {
  91. revision,
  92. state: RevisionState::Sync,
  93. write_to_disk: true,
  94. }
  95. }
  96. pub fn ack(&mut self) {
  97. self.state = RevisionState::Ack;
  98. }
  99. }
  100. pub struct RevisionChangeset {
  101. pub object_id: String,
  102. pub rev_id: i64,
  103. pub state: RevisionState,
  104. }
  105. /// Sync: revision is not synced to the server
  106. /// Ack: revision is synced to the server
  107. #[derive(Debug, Clone, Eq, PartialEq)]
  108. pub enum RevisionState {
  109. Sync = 0,
  110. Ack = 1,
  111. }
  112. impl RevisionState {
  113. pub fn is_need_sync(&self) -> bool {
  114. match self {
  115. RevisionState::Sync => true,
  116. RevisionState::Ack => false,
  117. }
  118. }
  119. }
  120. impl AsRef<RevisionState> for RevisionState {
  121. fn as_ref(&self) -> &RevisionState {
  122. self
  123. }
  124. }