2023-01-12 13:09:08 +08:00
|
|
|
mod disk_cache_impl;
|
|
|
|
|
2022-08-16 11:24:37 +08:00
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
2023-01-30 11:11:19 +08:00
|
|
|
use revision_model::{Revision, RevisionRange};
|
2022-01-04 15:05:52 +08:00
|
|
|
use std::fmt::Debug;
|
2022-08-16 11:24:37 +08:00
|
|
|
use std::sync::Arc;
|
2022-01-04 15:05:52 +08:00
|
|
|
|
2022-11-01 18:59:53 +08:00
|
|
|
pub trait RevisionDiskCache<Connection>: Sync + Send {
|
2023-02-13 09:29:49 +08:00
|
|
|
type Error: Debug;
|
|
|
|
fn create_revision_records(&self, revision_records: Vec<SyncRecord>) -> Result<(), Self::Error>;
|
|
|
|
|
|
|
|
fn get_connection(&self) -> Result<Connection, Self::Error>;
|
|
|
|
|
|
|
|
// Read all the records if the rev_ids is None
|
|
|
|
fn read_revision_records(
|
|
|
|
&self,
|
|
|
|
object_id: &str,
|
|
|
|
rev_ids: Option<Vec<i64>>,
|
|
|
|
) -> Result<Vec<SyncRecord>, Self::Error>;
|
|
|
|
|
|
|
|
// Read the revision which rev_id >= range.start && rev_id <= range.end
|
|
|
|
fn read_revision_records_with_range(
|
|
|
|
&self,
|
|
|
|
object_id: &str,
|
|
|
|
range: &RevisionRange,
|
|
|
|
) -> Result<Vec<SyncRecord>, Self::Error>;
|
|
|
|
|
|
|
|
fn update_revision_record(&self, changesets: Vec<RevisionChangeset>) -> FlowyResult<()>;
|
|
|
|
|
|
|
|
// Delete all the records if the rev_ids is None
|
|
|
|
fn delete_revision_records(
|
|
|
|
&self,
|
|
|
|
object_id: &str,
|
|
|
|
rev_ids: Option<Vec<i64>>,
|
|
|
|
) -> Result<(), Self::Error>;
|
|
|
|
|
|
|
|
// Delete and insert will be executed in the same transaction.
|
|
|
|
// It deletes all the records if the deleted_rev_ids is None and then insert the new records
|
|
|
|
fn delete_and_insert_records(
|
|
|
|
&self,
|
|
|
|
object_id: &str,
|
|
|
|
deleted_rev_ids: Option<Vec<i64>>,
|
|
|
|
inserted_records: Vec<SyncRecord>,
|
|
|
|
) -> Result<(), Self::Error>;
|
2022-01-04 15:05:52 +08:00
|
|
|
}
|
2022-03-10 22:27:19 +08:00
|
|
|
|
2022-11-01 18:59:53 +08:00
|
|
|
impl<T, Connection> RevisionDiskCache<Connection> for Arc<T>
|
2022-08-16 11:24:37 +08:00
|
|
|
where
|
2023-02-13 09:29:49 +08:00
|
|
|
T: RevisionDiskCache<Connection, Error = FlowyError>,
|
2022-08-16 11:24:37 +08:00
|
|
|
{
|
2023-02-13 09:29:49 +08:00
|
|
|
type Error = FlowyError;
|
|
|
|
|
|
|
|
fn create_revision_records(&self, revision_records: Vec<SyncRecord>) -> Result<(), Self::Error> {
|
|
|
|
(**self).create_revision_records(revision_records)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_connection(&self) -> Result<Connection, Self::Error> {
|
|
|
|
(**self).get_connection()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_revision_records(
|
|
|
|
&self,
|
|
|
|
object_id: &str,
|
|
|
|
rev_ids: Option<Vec<i64>>,
|
|
|
|
) -> Result<Vec<SyncRecord>, Self::Error> {
|
|
|
|
(**self).read_revision_records(object_id, rev_ids)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_revision_records_with_range(
|
|
|
|
&self,
|
|
|
|
object_id: &str,
|
|
|
|
range: &RevisionRange,
|
|
|
|
) -> Result<Vec<SyncRecord>, Self::Error> {
|
|
|
|
(**self).read_revision_records_with_range(object_id, range)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_revision_record(&self, changesets: Vec<RevisionChangeset>) -> FlowyResult<()> {
|
|
|
|
(**self).update_revision_record(changesets)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn delete_revision_records(
|
|
|
|
&self,
|
|
|
|
object_id: &str,
|
|
|
|
rev_ids: Option<Vec<i64>>,
|
|
|
|
) -> Result<(), Self::Error> {
|
|
|
|
(**self).delete_revision_records(object_id, rev_ids)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn delete_and_insert_records(
|
|
|
|
&self,
|
|
|
|
object_id: &str,
|
|
|
|
deleted_rev_ids: Option<Vec<i64>>,
|
|
|
|
inserted_records: Vec<SyncRecord>,
|
|
|
|
) -> Result<(), Self::Error> {
|
|
|
|
(**self).delete_and_insert_records(object_id, deleted_rev_ids, inserted_records)
|
|
|
|
}
|
2022-08-16 11:24:37 +08:00
|
|
|
}
|
|
|
|
|
2022-03-10 22:27:19 +08:00
|
|
|
#[derive(Clone, Debug)]
|
2022-11-02 10:21:10 +08:00
|
|
|
pub struct SyncRecord {
|
2023-02-13 09:29:49 +08:00
|
|
|
pub revision: Revision,
|
|
|
|
pub state: RevisionState,
|
|
|
|
pub write_to_disk: bool,
|
2022-03-10 22:27:19 +08:00
|
|
|
}
|
|
|
|
|
2022-11-02 10:21:10 +08:00
|
|
|
impl SyncRecord {
|
2023-02-13 09:29:49 +08:00
|
|
|
pub fn new(revision: Revision) -> Self {
|
|
|
|
Self {
|
|
|
|
revision,
|
|
|
|
state: RevisionState::Sync,
|
|
|
|
write_to_disk: true,
|
2022-07-20 18:27:12 +08:00
|
|
|
}
|
2023-02-13 09:29:49 +08:00
|
|
|
}
|
2022-07-20 18:27:12 +08:00
|
|
|
|
2023-02-13 09:29:49 +08:00
|
|
|
pub fn ack(&mut self) {
|
|
|
|
self.state = RevisionState::Ack;
|
|
|
|
}
|
2022-03-10 22:27:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RevisionChangeset {
|
2023-02-13 09:29:49 +08:00
|
|
|
pub object_id: String,
|
|
|
|
pub rev_id: i64,
|
|
|
|
pub state: RevisionState,
|
2022-03-10 22:27:19 +08:00
|
|
|
}
|
|
|
|
|
2022-07-20 18:27:12 +08:00
|
|
|
/// Sync: revision is not synced to the server
|
|
|
|
/// Ack: revision is synced to the server
|
2022-03-10 22:27:19 +08:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub enum RevisionState {
|
2023-02-13 09:29:49 +08:00
|
|
|
Sync = 0,
|
|
|
|
Ack = 1,
|
2022-03-10 22:27:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RevisionState {
|
2023-02-13 09:29:49 +08:00
|
|
|
pub fn is_need_sync(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
RevisionState::Sync => true,
|
|
|
|
RevisionState::Ack => false,
|
2022-03-10 22:27:19 +08:00
|
|
|
}
|
2023-02-13 09:29:49 +08:00
|
|
|
}
|
2022-03-10 22:27:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<RevisionState> for RevisionState {
|
2023-02-13 09:29:49 +08:00
|
|
|
fn as_ref(&self) -> &RevisionState {
|
|
|
|
self
|
|
|
|
}
|
2022-03-10 22:27:19 +08:00
|
|
|
}
|