rev_manager.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. use crate::disk::RevisionState;
  2. use crate::{RevisionPersistence, RevisionSnapshotDiskCache, RevisionSnapshotManager, WSDataProviderDataSource};
  3. use bytes::Bytes;
  4. use flowy_error::{FlowyError, FlowyResult};
  5. use flowy_sync::{
  6. entities::revision::{Revision, RevisionRange},
  7. util::{md5, pair_rev_id_from_revisions, RevIdCounter},
  8. };
  9. use lib_infra::future::FutureResult;
  10. use std::sync::Arc;
  11. pub trait RevisionCloudService: Send + Sync {
  12. /// Read the object's revision from remote
  13. /// Returns a list of revisions that used to build the object
  14. /// # Arguments
  15. ///
  16. /// * `user_id`: the id of the user
  17. /// * `object_id`: the id of the object
  18. ///
  19. fn fetch_object(&self, user_id: &str, object_id: &str) -> FutureResult<Vec<Revision>, FlowyError>;
  20. }
  21. pub trait RevisionObjectDeserializer: Send + Sync {
  22. type Output;
  23. /// Deserialize the list of revisions into an concrete object type.
  24. ///
  25. /// # Arguments
  26. ///
  27. /// * `object_id`: the id of the object
  28. /// * `revisions`: a list of revisions that represent the object
  29. ///
  30. fn deserialize_revisions(object_id: &str, revisions: Vec<Revision>) -> FlowyResult<Self::Output>;
  31. }
  32. pub trait RevisionObjectSerializer: Send + Sync {
  33. /// Serialize a list of revisions into one in `Bytes` format
  34. ///
  35. /// * `revisions`: a list of revisions will be serialized to `Bytes`
  36. ///
  37. fn combine_revisions(revisions: Vec<Revision>) -> FlowyResult<Bytes>;
  38. }
  39. /// `RevisionCompress` is used to compress multiple revisions into one revision
  40. ///
  41. pub trait RevisionCompress: Send + Sync {
  42. fn compress_revisions(
  43. &self,
  44. user_id: &str,
  45. object_id: &str,
  46. mut revisions: Vec<Revision>,
  47. ) -> FlowyResult<Revision> {
  48. if revisions.is_empty() {
  49. return Err(FlowyError::internal().context("Can't compact the empty revisions"));
  50. }
  51. if revisions.len() == 1 {
  52. return Ok(revisions.pop().unwrap());
  53. }
  54. let first_revision = revisions.first().unwrap();
  55. let last_revision = revisions.last().unwrap();
  56. let (base_rev_id, rev_id) = first_revision.pair_rev_id();
  57. let md5 = last_revision.md5.clone();
  58. let bytes = self.combine_revisions(revisions)?;
  59. Ok(Revision::new(object_id, base_rev_id, rev_id, bytes, md5))
  60. }
  61. fn combine_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes>;
  62. }
  63. pub struct RevisionConfiguration {
  64. merge_when_excess_number_of_version: i64,
  65. }
  66. impl std::default::Default for RevisionConfiguration {
  67. fn default() -> Self {
  68. Self {
  69. merge_when_excess_number_of_version: 100,
  70. }
  71. }
  72. }
  73. pub struct RevisionManager<Connection> {
  74. pub object_id: String,
  75. user_id: String,
  76. rev_id_counter: RevIdCounter,
  77. rev_persistence: Arc<RevisionPersistence<Connection>>,
  78. #[allow(dead_code)]
  79. rev_snapshot: Arc<RevisionSnapshotManager>,
  80. rev_compress: Arc<dyn RevisionCompress>,
  81. #[cfg(feature = "flowy_unit_test")]
  82. rev_ack_notifier: tokio::sync::broadcast::Sender<i64>,
  83. // configuration: RevisionConfiguration,
  84. }
  85. impl<Connection: 'static> RevisionManager<Connection> {
  86. pub fn new<SP, C>(
  87. user_id: &str,
  88. object_id: &str,
  89. rev_persistence: RevisionPersistence<Connection>,
  90. rev_compress: C,
  91. snapshot_persistence: SP,
  92. ) -> Self
  93. where
  94. SP: 'static + RevisionSnapshotDiskCache,
  95. C: 'static + RevisionCompress,
  96. {
  97. let rev_id_counter = RevIdCounter::new(0);
  98. let rev_compress = Arc::new(rev_compress);
  99. let rev_persistence = Arc::new(rev_persistence);
  100. let rev_snapshot = Arc::new(RevisionSnapshotManager::new(user_id, object_id, snapshot_persistence));
  101. #[cfg(feature = "flowy_unit_test")]
  102. let (revision_ack_notifier, _) = tokio::sync::broadcast::channel(1);
  103. Self {
  104. object_id: object_id.to_string(),
  105. user_id: user_id.to_owned(),
  106. rev_id_counter,
  107. rev_persistence,
  108. rev_snapshot,
  109. rev_compress,
  110. #[cfg(feature = "flowy_unit_test")]
  111. rev_ack_notifier: revision_ack_notifier,
  112. }
  113. }
  114. #[tracing::instrument(level = "debug", skip_all, fields(object_id) err)]
  115. pub async fn load<B>(&mut self, cloud: Option<Arc<dyn RevisionCloudService>>) -> FlowyResult<B::Output>
  116. where
  117. B: RevisionObjectDeserializer,
  118. {
  119. let (revisions, rev_id) = RevisionLoader {
  120. object_id: self.object_id.clone(),
  121. user_id: self.user_id.clone(),
  122. cloud,
  123. rev_persistence: self.rev_persistence.clone(),
  124. }
  125. .load()
  126. .await?;
  127. self.rev_id_counter.set(rev_id);
  128. tracing::Span::current().record("object_id", &self.object_id.as_str());
  129. B::deserialize_revisions(&self.object_id, revisions)
  130. }
  131. pub async fn load_revisions(&self) -> FlowyResult<Vec<Revision>> {
  132. let revisions = RevisionLoader {
  133. object_id: self.object_id.clone(),
  134. user_id: self.user_id.clone(),
  135. cloud: None,
  136. rev_persistence: self.rev_persistence.clone(),
  137. }
  138. .load_revisions()
  139. .await?;
  140. Ok(revisions)
  141. }
  142. #[tracing::instrument(level = "debug", skip(self, revisions), err)]
  143. pub async fn reset_object(&self, revisions: Vec<Revision>) -> FlowyResult<()> {
  144. let rev_id = pair_rev_id_from_revisions(&revisions).1;
  145. let _ = self.rev_persistence.reset(revisions).await?;
  146. self.rev_id_counter.set(rev_id);
  147. Ok(())
  148. }
  149. #[tracing::instrument(level = "debug", skip(self, revision), err)]
  150. pub async fn add_remote_revision(&self, revision: &Revision) -> Result<(), FlowyError> {
  151. if revision.bytes.is_empty() {
  152. return Err(FlowyError::internal().context("Remote revisions is empty"));
  153. }
  154. let _ = self.rev_persistence.add_ack_revision(revision).await?;
  155. // self.rev_history.add_revision(revision).await;
  156. self.rev_id_counter.set(revision.rev_id);
  157. Ok(())
  158. }
  159. #[tracing::instrument(level = "debug", skip_all, err)]
  160. pub async fn add_local_revision(&self, revision: &Revision) -> Result<(), FlowyError> {
  161. if revision.bytes.is_empty() {
  162. return Err(FlowyError::internal().context("Local revisions is empty"));
  163. }
  164. let rev_id = self
  165. .rev_persistence
  166. .add_sync_revision(revision, &self.rev_compress)
  167. .await?;
  168. // self.rev_history.add_revision(revision).await;
  169. self.rev_id_counter.set(rev_id);
  170. Ok(())
  171. }
  172. #[tracing::instrument(level = "debug", skip(self), err)]
  173. pub async fn ack_revision(&self, rev_id: i64) -> Result<(), FlowyError> {
  174. if self.rev_persistence.ack_revision(rev_id).await.is_ok() {
  175. #[cfg(feature = "flowy_unit_test")]
  176. let _ = self.rev_ack_notifier.send(rev_id);
  177. }
  178. Ok(())
  179. }
  180. /// Returns the current revision id
  181. pub fn rev_id(&self) -> i64 {
  182. self.rev_id_counter.value()
  183. }
  184. pub async fn next_sync_rev_id(&self) -> Option<i64> {
  185. self.rev_persistence.next_sync_rev_id().await
  186. }
  187. pub fn next_rev_id_pair(&self) -> (i64, i64) {
  188. let cur = self.rev_id_counter.value();
  189. let next = self.rev_id_counter.next_id();
  190. (cur, next)
  191. }
  192. pub async fn get_revisions_in_range(&self, range: RevisionRange) -> Result<Vec<Revision>, FlowyError> {
  193. let revisions = self.rev_persistence.revisions_in_range(&range).await?;
  194. Ok(revisions)
  195. }
  196. pub async fn next_sync_revision(&self) -> FlowyResult<Option<Revision>> {
  197. self.rev_persistence.next_sync_revision().await
  198. }
  199. pub async fn get_revision(&self, rev_id: i64) -> Option<Revision> {
  200. self.rev_persistence.get(rev_id).await.map(|record| record.revision)
  201. }
  202. }
  203. impl<Connection: 'static> WSDataProviderDataSource for Arc<RevisionManager<Connection>> {
  204. fn next_revision(&self) -> FutureResult<Option<Revision>, FlowyError> {
  205. let rev_manager = self.clone();
  206. FutureResult::new(async move { rev_manager.next_sync_revision().await })
  207. }
  208. fn ack_revision(&self, rev_id: i64) -> FutureResult<(), FlowyError> {
  209. let rev_manager = self.clone();
  210. FutureResult::new(async move { (*rev_manager).ack_revision(rev_id).await })
  211. }
  212. fn current_rev_id(&self) -> i64 {
  213. self.rev_id()
  214. }
  215. }
  216. #[cfg(feature = "flowy_unit_test")]
  217. impl<Connection> RevisionManager<Connection> {
  218. pub async fn revision_cache(&self) -> Arc<RevisionPersistence<Connection>> {
  219. self.rev_persistence.clone()
  220. }
  221. pub fn ack_notify(&self) -> tokio::sync::broadcast::Receiver<i64> {
  222. self.rev_ack_notifier.subscribe()
  223. }
  224. }
  225. pub struct RevisionLoader<Connection> {
  226. pub object_id: String,
  227. pub user_id: String,
  228. pub cloud: Option<Arc<dyn RevisionCloudService>>,
  229. pub rev_persistence: Arc<RevisionPersistence<Connection>>,
  230. }
  231. impl<Connection: 'static> RevisionLoader<Connection> {
  232. pub async fn load(&self) -> Result<(Vec<Revision>, i64), FlowyError> {
  233. let records = self.rev_persistence.batch_get(&self.object_id)?;
  234. let revisions: Vec<Revision>;
  235. let mut rev_id = 0;
  236. if records.is_empty() && self.cloud.is_some() {
  237. let remote_revisions = self
  238. .cloud
  239. .as_ref()
  240. .unwrap()
  241. .fetch_object(&self.user_id, &self.object_id)
  242. .await?;
  243. for revision in &remote_revisions {
  244. rev_id = revision.rev_id;
  245. let _ = self.rev_persistence.add_ack_revision(revision).await?;
  246. }
  247. revisions = remote_revisions;
  248. } else {
  249. for record in &records {
  250. rev_id = record.revision.rev_id;
  251. if record.state == RevisionState::Sync {
  252. // Sync the records if their state is RevisionState::Sync.
  253. let _ = self.rev_persistence.sync_revision(&record.revision).await?;
  254. }
  255. }
  256. revisions = records.into_iter().map(|record| record.revision).collect::<_>();
  257. }
  258. if let Some(revision) = revisions.last() {
  259. debug_assert_eq!(rev_id, revision.rev_id);
  260. }
  261. Ok((revisions, rev_id))
  262. }
  263. pub async fn load_revisions(&self) -> Result<Vec<Revision>, FlowyError> {
  264. let records = self.rev_persistence.batch_get(&self.object_id)?;
  265. let revisions = records.into_iter().map(|record| record.revision).collect::<_>();
  266. Ok(revisions)
  267. }
  268. }
  269. /// Represents as the md5 of the revision object after applying the
  270. /// revision. For example, RevisionMD5 will be the md5 of the document
  271. /// content.
  272. #[derive(Debug, Clone)]
  273. pub struct RevisionMD5(String);
  274. impl RevisionMD5 {
  275. pub fn from_bytes<T: AsRef<[u8]>>(bytes: T) -> Result<Self, FlowyError> {
  276. Ok(RevisionMD5(md5(bytes)))
  277. }
  278. pub fn into_inner(self) -> String {
  279. self.0
  280. }
  281. pub fn is_equal(&self, s: &str) -> bool {
  282. self.0 == s
  283. }
  284. }
  285. impl std::convert::From<RevisionMD5> for String {
  286. fn from(md5: RevisionMD5) -> Self {
  287. md5.0
  288. }
  289. }
  290. impl std::convert::From<&str> for RevisionMD5 {
  291. fn from(s: &str) -> Self {
  292. Self(s.to_owned())
  293. }
  294. }
  295. impl std::convert::From<String> for RevisionMD5 {
  296. fn from(s: String) -> Self {
  297. Self(s)
  298. }
  299. }
  300. impl std::ops::Deref for RevisionMD5 {
  301. type Target = String;
  302. fn deref(&self) -> &Self::Target {
  303. &self.0
  304. }
  305. }
  306. impl PartialEq<Self> for RevisionMD5 {
  307. fn eq(&self, other: &Self) -> bool {
  308. self.0 == other.0
  309. }
  310. }
  311. impl std::cmp::Eq for RevisionMD5 {}