cache.rs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. use crate::{
  2. errors::FlowyError,
  3. services::doc::revision::{
  4. cache::{
  5. disk::{Persistence, RevisionDiskCache},
  6. memory::{RevisionMemoryCache, RevisionMemoryCacheDelegate},
  7. sync::RevisionSyncSeq,
  8. },
  9. RevisionRecord,
  10. },
  11. sql_tables::{RevChangeset, RevTableState},
  12. };
  13. use flowy_database::ConnectionPool;
  14. use flowy_error::{internal_error, FlowyResult};
  15. use lib_infra::future::FutureResult;
  16. use lib_ot::revision::{RevState, Revision, RevisionRange};
  17. use std::sync::{
  18. atomic::{AtomicI64, Ordering::SeqCst},
  19. Arc,
  20. };
  21. use tokio::task::spawn_blocking;
  22. type DocRevisionDiskCache = dyn RevisionDiskCache<Error = FlowyError>;
  23. pub struct RevisionCache {
  24. doc_id: String,
  25. pub disk_cache: Arc<DocRevisionDiskCache>,
  26. memory_cache: Arc<RevisionMemoryCache>,
  27. sync_seq: Arc<RevisionSyncSeq>,
  28. latest_rev_id: AtomicI64,
  29. }
  30. impl RevisionCache {
  31. pub fn new(user_id: &str, doc_id: &str, pool: Arc<ConnectionPool>) -> RevisionCache {
  32. let disk_cache = Arc::new(Persistence::new(user_id, pool));
  33. let memory_cache = Arc::new(RevisionMemoryCache::new(doc_id, Arc::new(disk_cache.clone())));
  34. let sync_seq = Arc::new(RevisionSyncSeq::new());
  35. let doc_id = doc_id.to_owned();
  36. Self {
  37. doc_id,
  38. disk_cache,
  39. memory_cache,
  40. sync_seq,
  41. latest_rev_id: AtomicI64::new(0),
  42. }
  43. }
  44. #[tracing::instrument(level = "debug", skip(self, revision))]
  45. pub async fn add_local_revision(&self, revision: Revision) -> FlowyResult<()> {
  46. if self.memory_cache.contains(&revision.rev_id) {
  47. return Err(FlowyError::internal().context(format!("Duplicate revision id: {}", revision.rev_id)));
  48. }
  49. let rev_id = revision.rev_id;
  50. let record = RevisionRecord {
  51. revision,
  52. state: RevState::StateLocal,
  53. };
  54. let _ = self.memory_cache.add_revision(&record).await;
  55. self.sync_seq.add_revision(record).await?;
  56. let _ = self.latest_rev_id.fetch_update(SeqCst, SeqCst, |_e| Some(rev_id));
  57. Ok(())
  58. }
  59. #[tracing::instrument(level = "debug", skip(self, revision))]
  60. pub async fn add_remote_revision(&self, revision: Revision) -> FlowyResult<()> {
  61. if self.memory_cache.contains(&revision.rev_id) {
  62. return Err(FlowyError::internal().context(format!("Duplicate revision id: {}", revision.rev_id)));
  63. }
  64. let rev_id = revision.rev_id;
  65. let record = RevisionRecord {
  66. revision,
  67. state: RevState::Acked,
  68. };
  69. self.memory_cache.add_revision(&record).await;
  70. let _ = self.latest_rev_id.fetch_update(SeqCst, SeqCst, |_e| Some(rev_id));
  71. Ok(())
  72. }
  73. #[tracing::instrument(level = "debug", skip(self, rev_id), fields(rev_id = %rev_id))]
  74. pub async fn ack_revision(&self, rev_id: i64) {
  75. if self.sync_seq.ack_revision(&rev_id).await.is_ok() {
  76. self.memory_cache.ack_revision(&rev_id).await;
  77. }
  78. }
  79. pub fn latest_rev_id(&self) -> i64 { self.latest_rev_id.load(SeqCst) }
  80. pub async fn get_revision(&self, doc_id: &str, rev_id: i64) -> Option<RevisionRecord> {
  81. match self.memory_cache.get_revision(&rev_id).await {
  82. None => match self.disk_cache.read_revision(&self.doc_id, rev_id) {
  83. Ok(Some(revision)) => Some(revision),
  84. Ok(None) => {
  85. tracing::warn!("Can't find revision in {} with rev_id: {}", doc_id, rev_id);
  86. None
  87. },
  88. Err(e) => {
  89. tracing::error!("{}", e);
  90. None
  91. },
  92. },
  93. Some(revision) => Some(revision),
  94. }
  95. }
  96. pub async fn revisions_in_range(&self, range: RevisionRange) -> FlowyResult<Vec<Revision>> {
  97. let mut records = self.memory_cache.get_revisions_in_range(&range).await?;
  98. let range_len = range.len() as usize;
  99. if records.len() != range_len {
  100. let disk_cache = self.disk_cache.clone();
  101. let doc_id = self.doc_id.clone();
  102. records = spawn_blocking(move || disk_cache.revisions_in_range(&doc_id, &range))
  103. .await
  104. .map_err(internal_error)??;
  105. if records.len() != range_len {
  106. log::error!("Revisions len is not equal to range required");
  107. }
  108. }
  109. Ok(records
  110. .into_iter()
  111. .map(|record| record.revision)
  112. .collect::<Vec<Revision>>())
  113. }
  114. pub(crate) fn next_sync_revision(&self) -> FutureResult<Option<Revision>, FlowyError> {
  115. let sync_seq = self.sync_seq.clone();
  116. let disk_cache = self.disk_cache.clone();
  117. let doc_id = self.doc_id.clone();
  118. FutureResult::new(async move {
  119. match sync_seq.next_sync_revision().await {
  120. None => match sync_seq.next_sync_rev_id().await {
  121. None => Ok(None),
  122. Some(rev_id) => match disk_cache.read_revision(&doc_id, rev_id)? {
  123. None => Ok(None),
  124. Some(record) => Ok(Some(record.revision)),
  125. },
  126. },
  127. Some((_, record)) => Ok(Some(record.revision)),
  128. }
  129. })
  130. }
  131. }
  132. impl RevisionMemoryCacheDelegate for Arc<Persistence> {
  133. fn receive_checkpoint(&self, records: Vec<RevisionRecord>) -> FlowyResult<()> { self.create_revisions(records) }
  134. fn receive_ack(&self, doc_id: &str, rev_id: i64) {
  135. let changeset = RevChangeset {
  136. doc_id: doc_id.to_string(),
  137. rev_id: rev_id.into(),
  138. state: RevTableState::Acked,
  139. };
  140. match self.update_revisions(vec![changeset]) {
  141. Ok(_) => {},
  142. Err(e) => tracing::error!("{}", e),
  143. }
  144. }
  145. }
  146. #[cfg(feature = "flowy_unit_test")]
  147. impl RevisionCache {
  148. pub fn disk_cache(&self) -> Arc<DocRevisionDiskCache> { self.disk_cache.clone() }
  149. pub fn memory_cache(&self) -> Arc<RevisionSyncSeq> { self.sync_seq.clone() }
  150. }