135 lines
3.8 KiB
Rust
Raw Normal View History

2022-08-16 11:24:37 +08:00
use flowy_error::{FlowyError, FlowyResult};
use flowy_http_model::revision::{RevId, 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 {
2022-01-04 15:05:52 +08:00
type Error: Debug;
2022-11-02 10:21:10 +08:00
fn create_revision_records(&self, revision_records: Vec<SyncRecord>) -> Result<(), Self::Error>;
2022-01-04 15:05:52 +08:00
2022-11-01 18:59:53 +08:00
fn get_connection(&self) -> Result<Connection, Self::Error>;
2022-01-04 15:05:52 +08:00
// Read all the records if the rev_ids is None
2022-11-02 10:21:10 +08:00
fn read_revision_records(&self, object_id: &str, rev_ids: Option<Vec<i64>>)
-> Result<Vec<SyncRecord>, Self::Error>;
2022-01-04 15:05:52 +08:00
2022-01-25 20:37:48 +08:00
// Read the revision which rev_id >= range.start && rev_id <= range.end
2022-01-04 15:05:52 +08:00
fn read_revision_records_with_range(
&self,
2022-01-14 15:23:21 +08:00
object_id: &str,
2022-01-04 15:05:52 +08:00
range: &RevisionRange,
2022-11-02 10:21:10 +08:00
) -> Result<Vec<SyncRecord>, Self::Error>;
2022-01-04 15:05:52 +08:00
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>;
2022-01-04 15:05:52 +08:00
2022-01-26 23:29:18 +08:00
// 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>>,
2022-11-02 10:21:10 +08:00
inserted_records: Vec<SyncRecord>,
2022-01-26 23:29:18 +08:00
) -> 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
2022-11-01 18:59:53 +08:00
T: RevisionDiskCache<Connection, Error = FlowyError>,
2022-08-16 11:24:37 +08:00
{
type Error = FlowyError;
2022-11-02 10:21:10 +08:00
fn create_revision_records(&self, revision_records: Vec<SyncRecord>) -> Result<(), Self::Error> {
2022-08-16 11:24:37 +08:00
(**self).create_revision_records(revision_records)
}
2022-11-01 18:59:53 +08:00
fn get_connection(&self) -> Result<Connection, Self::Error> {
(**self).get_connection()
}
2022-08-16 11:24:37 +08:00
fn read_revision_records(
&self,
object_id: &str,
rev_ids: Option<Vec<i64>>,
2022-11-02 10:21:10 +08:00
) -> Result<Vec<SyncRecord>, Self::Error> {
2022-08-16 11:24:37 +08:00
(**self).read_revision_records(object_id, rev_ids)
}
fn read_revision_records_with_range(
&self,
object_id: &str,
range: &RevisionRange,
2022-11-02 10:21:10 +08:00
) -> Result<Vec<SyncRecord>, Self::Error> {
2022-08-16 11:24:37 +08:00
(**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>>,
2022-11-02 10:21:10 +08:00
inserted_records: Vec<SyncRecord>,
2022-08-16 11:24:37 +08:00
) -> Result<(), Self::Error> {
(**self).delete_and_insert_records(object_id, deleted_rev_ids, inserted_records)
}
}
2022-03-10 22:27:19 +08:00
#[derive(Clone, Debug)]
2022-11-02 10:21:10 +08:00
pub struct SyncRecord {
2022-03-10 22:27:19 +08:00
pub revision: Revision,
pub state: RevisionState,
pub write_to_disk: bool,
}
2022-11-02 10:21:10 +08:00
impl SyncRecord {
2022-07-20 18:27:12 +08:00
pub fn new(revision: Revision) -> Self {
Self {
revision,
state: RevisionState::Sync,
write_to_disk: true,
}
}
2022-03-10 22:27:19 +08:00
pub fn ack(&mut self) {
self.state = RevisionState::Ack;
}
}
pub struct RevisionChangeset {
2022-11-01 18:59:53 +08:00
pub object_id: String,
pub rev_id: RevId,
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 {
Sync = 0,
Ack = 1,
}
impl RevisionState {
pub fn is_need_sync(&self) -> bool {
match self {
RevisionState::Sync => true,
RevisionState::Ack => false,
}
}
}
impl AsRef<RevisionState> for RevisionState {
fn as_ref(&self) -> &RevisionState {
self
}
}