2021-12-08 14:17:40 +08:00
|
|
|
use crate::{
|
2021-12-14 18:04:51 +08:00
|
|
|
errors::FlowyError,
|
2021-12-19 21:10:50 +08:00
|
|
|
services::doc::revision::cache::{
|
|
|
|
disk::{Persistence, RevisionDiskCache},
|
|
|
|
memory::{RevisionMemoryCache, RevisionMemoryCacheDelegate},
|
2021-12-13 22:46:35 +08:00
|
|
|
},
|
2022-01-01 16:16:06 +08:00
|
|
|
sql_tables::{RevisionChangeset, RevisionTableState},
|
2021-12-08 14:17:40 +08:00
|
|
|
};
|
2021-12-19 21:10:50 +08:00
|
|
|
use dashmap::DashMap;
|
2022-01-01 14:23:58 +08:00
|
|
|
use flowy_collaboration::entities::revision::{Revision, RevisionRange, RevisionState};
|
2021-12-08 14:17:40 +08:00
|
|
|
use flowy_database::ConnectionPool;
|
2021-12-14 18:04:51 +08:00
|
|
|
use flowy_error::{internal_error, FlowyResult};
|
2021-12-13 13:55:44 +08:00
|
|
|
use lib_infra::future::FutureResult;
|
2021-12-22 21:13:52 +08:00
|
|
|
use lib_ot::errors::OTError;
|
2021-12-19 21:10:50 +08:00
|
|
|
use std::{
|
|
|
|
collections::VecDeque,
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicI64, Ordering::SeqCst},
|
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
use tokio::{sync::RwLock, task::spawn_blocking};
|
2021-12-08 14:17:40 +08:00
|
|
|
|
|
|
|
pub struct RevisionCache {
|
|
|
|
doc_id: String,
|
2022-01-01 14:23:58 +08:00
|
|
|
disk_cache: Arc<dyn RevisionDiskCache<Error = FlowyError>>,
|
2021-12-08 14:17:40 +08:00
|
|
|
memory_cache: Arc<RevisionMemoryCache>,
|
2021-12-18 18:35:45 +08:00
|
|
|
latest_rev_id: AtomicI64,
|
2021-12-08 14:17:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RevisionCache {
|
2021-12-18 00:23:26 +08:00
|
|
|
pub fn new(user_id: &str, doc_id: &str, pool: Arc<ConnectionPool>) -> RevisionCache {
|
|
|
|
let disk_cache = Arc::new(Persistence::new(user_id, pool));
|
|
|
|
let memory_cache = Arc::new(RevisionMemoryCache::new(doc_id, Arc::new(disk_cache.clone())));
|
2021-12-08 14:17:40 +08:00
|
|
|
let doc_id = doc_id.to_owned();
|
|
|
|
Self {
|
|
|
|
doc_id,
|
2021-12-18 00:23:26 +08:00
|
|
|
disk_cache,
|
2021-12-08 14:17:40 +08:00
|
|
|
memory_cache,
|
2021-12-18 18:35:45 +08:00
|
|
|
latest_rev_id: AtomicI64::new(0),
|
2021-12-08 14:17:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-01 16:16:06 +08:00
|
|
|
pub async fn add(&self, revision: Revision, state: RevisionState) -> FlowyResult<RevisionRecord> {
|
2021-12-13 22:46:35 +08:00
|
|
|
if self.memory_cache.contains(&revision.rev_id) {
|
2021-12-20 20:59:33 +08:00
|
|
|
return Err(FlowyError::internal().context(format!("Duplicate remote revision id: {}", revision.rev_id)));
|
2021-12-13 22:46:35 +08:00
|
|
|
}
|
2021-12-18 18:35:45 +08:00
|
|
|
let rev_id = revision.rev_id;
|
2022-01-01 16:16:06 +08:00
|
|
|
let record = RevisionRecord { revision, state };
|
|
|
|
self.memory_cache.add(&record).await;
|
|
|
|
self.set_latest_rev_id(rev_id);
|
|
|
|
Ok(record)
|
2021-12-08 14:17:40 +08:00
|
|
|
}
|
|
|
|
|
2022-01-01 16:16:06 +08:00
|
|
|
pub async fn ack(&self, rev_id: i64) { self.memory_cache.ack(&rev_id).await; }
|
2021-12-09 19:01:58 +08:00
|
|
|
|
2022-01-01 16:16:06 +08:00
|
|
|
pub async fn get(&self, rev_id: i64) -> Option<RevisionRecord> {
|
|
|
|
match self.memory_cache.get(&rev_id).await {
|
|
|
|
None => match self.disk_cache.read_revision_records(&self.doc_id, Some(vec![rev_id])) {
|
2022-01-01 14:23:58 +08:00
|
|
|
Ok(mut records) => {
|
|
|
|
if records.is_empty() {
|
|
|
|
tracing::warn!("Can't find revision in {} with rev_id: {}", &self.doc_id, rev_id);
|
|
|
|
}
|
|
|
|
assert_eq!(records.len(), 1);
|
|
|
|
records.pop()
|
2021-12-18 18:35:45 +08:00
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("{}", e);
|
|
|
|
None
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Some(revision) => Some(revision),
|
2021-12-08 14:17:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-01 16:16:06 +08:00
|
|
|
pub fn batch_get(&self, doc_id: &str) -> FlowyResult<Vec<RevisionRecord>> {
|
|
|
|
self.disk_cache.read_revision_records(doc_id, None)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn latest_revision(&self) -> Revision {
|
|
|
|
let rev_id = self.latest_rev_id.load(SeqCst);
|
|
|
|
self.get(rev_id).await.unwrap().revision
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:04:51 +08:00
|
|
|
pub async fn revisions_in_range(&self, range: RevisionRange) -> FlowyResult<Vec<Revision>> {
|
2022-01-01 16:16:06 +08:00
|
|
|
let mut records = self.memory_cache.get_with_range(&range).await?;
|
2021-12-18 18:35:45 +08:00
|
|
|
let range_len = range.len() as usize;
|
|
|
|
if records.len() != range_len {
|
|
|
|
let disk_cache = self.disk_cache.clone();
|
|
|
|
let doc_id = self.doc_id.clone();
|
2022-01-01 16:16:06 +08:00
|
|
|
records = spawn_blocking(move || disk_cache.read_revision_records_with_range(&doc_id, &range))
|
2021-12-18 18:35:45 +08:00
|
|
|
.await
|
|
|
|
.map_err(internal_error)??;
|
|
|
|
|
|
|
|
if records.len() != range_len {
|
|
|
|
log::error!("Revisions len is not equal to range required");
|
|
|
|
}
|
|
|
|
}
|
2021-12-18 00:23:26 +08:00
|
|
|
Ok(records
|
|
|
|
.into_iter()
|
|
|
|
.map(|record| record.revision)
|
|
|
|
.collect::<Vec<Revision>>())
|
2021-12-08 14:17:40 +08:00
|
|
|
}
|
|
|
|
|
2022-01-01 16:16:06 +08:00
|
|
|
#[tracing::instrument(level = "debug", skip(self, doc_id, revisions))]
|
|
|
|
pub fn reset_document(&self, doc_id: &str, revisions: Vec<Revision>) -> FlowyResult<()> {
|
2021-12-18 00:23:26 +08:00
|
|
|
let disk_cache = self.disk_cache.clone();
|
2022-01-01 16:16:06 +08:00
|
|
|
let conn = disk_cache.db_pool().get().map_err(internal_error)?;
|
|
|
|
let records = revisions
|
|
|
|
.into_iter()
|
|
|
|
.map(|revision| RevisionRecord {
|
|
|
|
revision,
|
|
|
|
state: RevisionState::Local,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
conn.immediate_transaction::<_, FlowyError, _>(|| {
|
|
|
|
let _ = disk_cache.delete_revision_records(doc_id, None, &*conn)?;
|
|
|
|
let _ = disk_cache.write_revision_records(records, &*conn)?;
|
|
|
|
Ok(())
|
2021-12-08 14:17:40 +08:00
|
|
|
})
|
|
|
|
}
|
2022-01-01 16:16:06 +08:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn set_latest_rev_id(&self, rev_id: i64) {
|
|
|
|
let _ = self.latest_rev_id.fetch_update(SeqCst, SeqCst, |_e| Some(rev_id));
|
|
|
|
}
|
2021-12-08 14:17:40 +08:00
|
|
|
}
|
|
|
|
|
2021-12-18 18:35:45 +08:00
|
|
|
impl RevisionMemoryCacheDelegate for Arc<Persistence> {
|
2022-01-01 16:16:06 +08:00
|
|
|
fn checkpoint_tick(&self, records: Vec<RevisionRecord>) -> FlowyResult<()> {
|
2022-01-01 14:23:58 +08:00
|
|
|
let conn = &*self.pool.get().map_err(internal_error)?;
|
2022-01-01 16:16:06 +08:00
|
|
|
self.write_revision_records(records, &conn)
|
2022-01-01 14:23:58 +08:00
|
|
|
}
|
2021-12-08 14:17:40 +08:00
|
|
|
|
2021-12-18 18:35:45 +08:00
|
|
|
fn receive_ack(&self, doc_id: &str, rev_id: i64) {
|
2022-01-01 14:23:58 +08:00
|
|
|
let changeset = RevisionChangeset {
|
2021-12-18 18:35:45 +08:00
|
|
|
doc_id: doc_id.to_string(),
|
|
|
|
rev_id: rev_id.into(),
|
2022-01-01 16:16:06 +08:00
|
|
|
state: RevisionTableState::Ack,
|
2021-12-18 18:35:45 +08:00
|
|
|
};
|
2022-01-01 16:16:06 +08:00
|
|
|
match self.update_revision_record(vec![changeset]) {
|
2021-12-18 18:35:45 +08:00
|
|
|
Ok(_) => {},
|
|
|
|
Err(e) => tracing::error!("{}", e),
|
|
|
|
}
|
2021-12-08 14:17:40 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-08 21:51:06 +08:00
|
|
|
|
2021-12-19 21:10:50 +08:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct RevisionRecord {
|
|
|
|
pub revision: Revision,
|
2022-01-01 14:23:58 +08:00
|
|
|
pub state: RevisionState,
|
2021-12-19 21:10:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RevisionRecord {
|
2022-01-01 14:23:58 +08:00
|
|
|
pub fn ack(&mut self) { self.state = RevisionState::Ack; }
|
2021-12-19 21:10:50 +08:00
|
|
|
}
|