cache.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. use crate::{
  2. errors::FlowyError,
  3. services::doc::revision::{
  4. cache::{disk::RevisionDiskCache, memory::RevisionMemoryCache},
  5. RevisionRecord,
  6. RevisionServer,
  7. },
  8. sql_tables::RevTableSql,
  9. };
  10. use bytes::Bytes;
  11. use flowy_collaboration::{entities::doc::Doc, util::md5};
  12. use flowy_database::ConnectionPool;
  13. use flowy_error::{internal_error, FlowyResult};
  14. use lib_infra::future::FutureResult;
  15. use lib_ot::{
  16. core::{Operation, OperationTransformable},
  17. revision::{RevState, RevType, Revision, RevisionRange},
  18. rich_text::RichTextDelta,
  19. };
  20. use std::{sync::Arc, time::Duration};
  21. use tokio::{
  22. sync::{mpsc, RwLock},
  23. task::{spawn_blocking, JoinHandle},
  24. };
  25. pub trait RevisionIterator: Send + Sync {
  26. fn next(&self) -> FutureResult<Option<RevisionRecord>, FlowyError>;
  27. }
  28. type DocRevisionDeskCache = dyn RevisionDiskCache<Error = FlowyError>;
  29. pub struct RevisionCache {
  30. user_id: String,
  31. doc_id: String,
  32. dish_cache: Arc<DocRevisionDeskCache>,
  33. memory_cache: Arc<RevisionMemoryCache>,
  34. defer_save: RwLock<Option<JoinHandle<()>>>,
  35. server: Arc<dyn RevisionServer>,
  36. }
  37. impl RevisionCache {
  38. pub fn new(
  39. user_id: &str,
  40. doc_id: &str,
  41. pool: Arc<ConnectionPool>,
  42. server: Arc<dyn RevisionServer>,
  43. ) -> RevisionCache {
  44. let doc_id = doc_id.to_owned();
  45. let dish_cache = Arc::new(Persistence::new(user_id, pool));
  46. let memory_cache = Arc::new(RevisionMemoryCache::new());
  47. Self {
  48. user_id: user_id.to_owned(),
  49. doc_id,
  50. dish_cache,
  51. memory_cache,
  52. defer_save: RwLock::new(None),
  53. server,
  54. }
  55. }
  56. #[tracing::instrument(level = "debug", skip(self, revision))]
  57. pub async fn add_local_revision(&self, revision: Revision) -> FlowyResult<()> {
  58. if self.memory_cache.contains(&revision.rev_id) {
  59. return Err(FlowyError::internal().context(format!("Duplicate revision id: {}", revision.rev_id)));
  60. }
  61. let record = RevisionRecord {
  62. revision,
  63. state: RevState::StateLocal,
  64. };
  65. self.memory_cache.add_revision(record).await?;
  66. self.save_revisions().await;
  67. Ok(())
  68. }
  69. #[tracing::instrument(level = "debug", skip(self, revision))]
  70. pub async fn add_remote_revision(&self, revision: Revision) -> FlowyResult<()> {
  71. if self.memory_cache.contains(&revision.rev_id) {
  72. return Err(FlowyError::internal().context(format!("Duplicate revision id: {}", revision.rev_id)));
  73. }
  74. let record = RevisionRecord {
  75. revision,
  76. state: RevState::StateLocal,
  77. };
  78. self.memory_cache.add_revision(record).await?;
  79. self.save_revisions().await;
  80. Ok(())
  81. }
  82. #[tracing::instrument(level = "debug", skip(self, rev_id), fields(rev_id = %rev_id))]
  83. pub async fn ack_revision(&self, rev_id: i64) {
  84. self.memory_cache.ack_revision(&rev_id).await;
  85. self.save_revisions().await;
  86. }
  87. pub async fn query_revision(&self, doc_id: &str, rev_id: i64) -> Option<RevisionRecord> {
  88. match self.memory_cache.query_revision(&rev_id).await {
  89. None => match self.dish_cache.read_revision(doc_id, rev_id) {
  90. Ok(revision) => revision,
  91. Err(e) => {
  92. log::error!("query_revision error: {:?}", e);
  93. None
  94. },
  95. },
  96. Some(record) => Some(record),
  97. }
  98. }
  99. async fn save_revisions(&self) {
  100. if let Some(handler) = self.defer_save.write().await.take() {
  101. handler.abort();
  102. }
  103. if self.memory_cache.is_empty() {
  104. return;
  105. }
  106. let memory_cache = self.memory_cache.clone();
  107. let disk_cache = self.dish_cache.clone();
  108. *self.defer_save.write().await = Some(tokio::spawn(async move {
  109. tokio::time::sleep(Duration::from_millis(300)).await;
  110. let (ids, records) = memory_cache.revisions();
  111. match disk_cache.create_revisions(records) {
  112. Ok(_) => {
  113. memory_cache.remove_revisions(ids);
  114. },
  115. Err(e) => log::error!("Save revision failed: {:?}", e),
  116. }
  117. }));
  118. }
  119. pub async fn revisions_in_range(&self, range: RevisionRange) -> FlowyResult<Vec<Revision>> {
  120. let revs = self.memory_cache.revisions_in_range(&range).await?;
  121. if revs.len() == range.len() as usize {
  122. Ok(revs)
  123. } else {
  124. let doc_id = self.doc_id.clone();
  125. let disk_cache = self.dish_cache.clone();
  126. let records = spawn_blocking(move || disk_cache.revisions_in_range(&doc_id, &range))
  127. .await
  128. .map_err(internal_error)??;
  129. let revisions = records
  130. .into_iter()
  131. .map(|record| record.revision)
  132. .collect::<Vec<Revision>>();
  133. Ok(revisions)
  134. }
  135. }
  136. pub async fn load_document(&self) -> FlowyResult<Doc> {
  137. // Loading the document from disk and it will be sync with server.
  138. let result = load_from_disk(&self.doc_id, self.memory_cache.clone(), self.dish_cache.clone()).await;
  139. if result.is_ok() {
  140. return result;
  141. }
  142. // The document doesn't exist in local. Try load from server
  143. let doc = self.server.fetch_document(&self.doc_id).await?;
  144. let delta_data = Bytes::from(doc.data.clone());
  145. let doc_md5 = md5(&delta_data);
  146. let revision = Revision::new(
  147. &doc.id,
  148. doc.base_rev_id,
  149. doc.rev_id,
  150. delta_data,
  151. RevType::Remote,
  152. &self.user_id,
  153. doc_md5,
  154. );
  155. self.add_remote_revision(revision).await?;
  156. Ok(doc)
  157. }
  158. }
  159. impl RevisionIterator for RevisionCache {
  160. fn next(&self) -> FutureResult<Option<RevisionRecord>, FlowyError> {
  161. let memory_cache = self.memory_cache.clone();
  162. let disk_cache = self.dish_cache.clone();
  163. let doc_id = self.doc_id.clone();
  164. FutureResult::new(async move {
  165. match memory_cache.front_local_revision().await {
  166. None => match memory_cache.front_local_rev_id().await {
  167. None => Ok(None),
  168. Some(rev_id) => match disk_cache.read_revision(&doc_id, rev_id)? {
  169. None => Ok(None),
  170. Some(record) => Ok(Some(record)),
  171. },
  172. },
  173. Some((_, record)) => Ok(Some(record)),
  174. }
  175. })
  176. }
  177. }
  178. async fn load_from_disk(
  179. doc_id: &str,
  180. memory_cache: Arc<RevisionMemoryCache>,
  181. disk_cache: Arc<DocRevisionDeskCache>,
  182. ) -> FlowyResult<Doc> {
  183. let doc_id = doc_id.to_owned();
  184. let (tx, mut rx) = mpsc::channel(2);
  185. let doc = spawn_blocking(move || {
  186. let records = disk_cache.read_revisions(&doc_id)?;
  187. if records.is_empty() {
  188. return Err(FlowyError::record_not_found().context("Local doesn't have this document"));
  189. }
  190. let (base_rev_id, rev_id) = records.last().unwrap().revision.pair_rev_id();
  191. let mut delta = RichTextDelta::new();
  192. for (_, record) in records.into_iter().enumerate() {
  193. // Opti: revision's clone may cause memory issues
  194. match RichTextDelta::from_bytes(record.revision.clone().delta_data) {
  195. Ok(local_delta) => {
  196. delta = delta.compose(&local_delta)?;
  197. match tx.blocking_send(record) {
  198. Ok(_) => {},
  199. Err(e) => tracing::error!("❌Load document from disk error: {}", e),
  200. }
  201. },
  202. Err(e) => {
  203. tracing::error!("Deserialize delta from revision failed: {}", e);
  204. },
  205. }
  206. }
  207. correct_delta_if_need(&mut delta);
  208. Result::<Doc, FlowyError>::Ok(Doc {
  209. id: doc_id,
  210. data: delta.to_json(),
  211. rev_id,
  212. base_rev_id,
  213. })
  214. })
  215. .await
  216. .map_err(internal_error)?;
  217. while let Some(record) = rx.recv().await {
  218. match memory_cache.add_revision(record).await {
  219. Ok(_) => {},
  220. Err(e) => log::error!("{:?}", e),
  221. }
  222. }
  223. doc
  224. }
  225. fn correct_delta_if_need(delta: &mut RichTextDelta) {
  226. if delta.ops.last().is_none() {
  227. return;
  228. }
  229. let data = delta.ops.last().as_ref().unwrap().get_data();
  230. if !data.ends_with('\n') {
  231. log::error!("❌The op must end with newline. Correcting it by inserting newline op");
  232. delta.ops.push(Operation::Insert("\n".into()));
  233. }
  234. }
  235. pub(crate) struct Persistence {
  236. user_id: String,
  237. pub(crate) pool: Arc<ConnectionPool>,
  238. }
  239. impl RevisionDiskCache for Persistence {
  240. type Error = FlowyError;
  241. fn create_revisions(&self, revisions: Vec<RevisionRecord>) -> Result<(), Self::Error> {
  242. let conn = &*self.pool.get().map_err(internal_error)?;
  243. conn.immediate_transaction::<_, FlowyError, _>(|| {
  244. let _ = RevTableSql::create_rev_table(revisions, conn)?;
  245. Ok(())
  246. })
  247. }
  248. fn revisions_in_range(&self, doc_id: &str, range: &RevisionRange) -> Result<Vec<RevisionRecord>, Self::Error> {
  249. let conn = &*self.pool.get().map_err(internal_error).unwrap();
  250. let revisions = RevTableSql::read_rev_tables_with_range(&self.user_id, doc_id, range.clone(), conn)?;
  251. Ok(revisions)
  252. }
  253. fn read_revision(&self, doc_id: &str, rev_id: i64) -> Result<Option<RevisionRecord>, Self::Error> {
  254. let conn = self.pool.get().map_err(internal_error)?;
  255. let some = RevTableSql::read_rev_table(&self.user_id, doc_id, &rev_id, &*conn)?;
  256. Ok(some)
  257. }
  258. fn read_revisions(&self, doc_id: &str) -> Result<Vec<RevisionRecord>, Self::Error> {
  259. let conn = self.pool.get().map_err(internal_error)?;
  260. let some = RevTableSql::read_rev_tables(&self.user_id, doc_id, &*conn)?;
  261. Ok(some)
  262. }
  263. }
  264. impl Persistence {
  265. pub(crate) fn new(user_id: &str, pool: Arc<ConnectionPool>) -> Self {
  266. Self {
  267. user_id: user_id.to_owned(),
  268. pool,
  269. }
  270. }
  271. }
  272. #[cfg(feature = "flowy_unit_test")]
  273. impl RevisionCache {
  274. pub fn dish_cache(&self) -> Arc<DocRevisionDeskCache> { self.dish_cache.clone() }
  275. pub fn memory_cache(&self) -> Arc<RevisionMemoryCache> { self.memory_cache.clone() }
  276. }