rev_manager.rs 12 KB

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