mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: save folder snapshot with custom config (#1670)
This commit is contained in:
@ -23,7 +23,9 @@ use lazy_static::lazy_static;
|
||||
use lib_infra::future::FutureResult;
|
||||
|
||||
use crate::services::clear_current_workspace;
|
||||
use crate::services::persistence::rev_sqlite::SQLiteFolderRevisionPersistence;
|
||||
use crate::services::persistence::rev_sqlite::{
|
||||
SQLiteFolderRevisionPersistence, SQLiteFolderRevisionSnapshotPersistence,
|
||||
};
|
||||
use flowy_http_model::ws_data::ServerRevisionWSData;
|
||||
use flowy_sync::client_folder::FolderPad;
|
||||
use std::convert::TryFrom;
|
||||
@ -174,12 +176,15 @@ impl FolderManager {
|
||||
let configuration = RevisionPersistenceConfiguration::new(100, false);
|
||||
let rev_persistence = RevisionPersistence::new(user_id, object_id, disk_cache, configuration);
|
||||
let rev_compactor = FolderRevisionMergeable();
|
||||
|
||||
let snapshot_object_id = format!("folder:{}", object_id);
|
||||
let snapshot_persistence = SQLiteFolderRevisionSnapshotPersistence::new(&snapshot_object_id, pool);
|
||||
let rev_manager = RevisionManager::new(
|
||||
user_id,
|
||||
folder_id.as_ref(),
|
||||
rev_persistence,
|
||||
rev_compactor,
|
||||
PhantomSnapshotPersistence(),
|
||||
snapshot_persistence,
|
||||
);
|
||||
|
||||
let folder_editor = FolderEditor::new(user_id, &folder_id, token, rev_manager, self.web_socket.clone()).await?;
|
||||
|
@ -0,0 +1,96 @@
|
||||
use bytes::Bytes;
|
||||
use flowy_database::{
|
||||
prelude::*,
|
||||
schema::{folder_rev_snapshot, folder_rev_snapshot::dsl},
|
||||
ConnectionPool,
|
||||
};
|
||||
use flowy_error::{internal_error, FlowyResult};
|
||||
use flowy_revision::{RevisionSnapshot, RevisionSnapshotDiskCache};
|
||||
use lib_infra::util::timestamp;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct SQLiteFolderRevisionSnapshotPersistence {
|
||||
object_id: String,
|
||||
pool: Arc<ConnectionPool>,
|
||||
}
|
||||
|
||||
impl SQLiteFolderRevisionSnapshotPersistence {
|
||||
pub fn new(object_id: &str, pool: Arc<ConnectionPool>) -> Self {
|
||||
Self {
|
||||
object_id: object_id.to_string(),
|
||||
pool,
|
||||
}
|
||||
}
|
||||
|
||||
fn gen_snapshot_id(&self, rev_id: i64) -> String {
|
||||
format!("{}:{}", self.object_id, rev_id)
|
||||
}
|
||||
}
|
||||
|
||||
impl RevisionSnapshotDiskCache for SQLiteFolderRevisionSnapshotPersistence {
|
||||
fn should_generate_snapshot_from_range(&self, start_rev_id: i64, current_rev_id: i64) -> bool {
|
||||
(current_rev_id - start_rev_id) >= 2
|
||||
}
|
||||
|
||||
fn write_snapshot(&self, rev_id: i64, data: Vec<u8>) -> FlowyResult<()> {
|
||||
let conn = self.pool.get().map_err(internal_error)?;
|
||||
let snapshot_id = self.gen_snapshot_id(rev_id);
|
||||
let timestamp = timestamp();
|
||||
let record = (
|
||||
dsl::snapshot_id.eq(&snapshot_id),
|
||||
dsl::object_id.eq(&self.object_id),
|
||||
dsl::rev_id.eq(rev_id),
|
||||
dsl::base_rev_id.eq(rev_id),
|
||||
dsl::timestamp.eq(timestamp),
|
||||
dsl::data.eq(data),
|
||||
);
|
||||
let _ = insert_or_ignore_into(dsl::folder_rev_snapshot)
|
||||
.values(record)
|
||||
.execute(&*conn)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_snapshot(&self, rev_id: i64) -> FlowyResult<Option<RevisionSnapshot>> {
|
||||
let conn = self.pool.get().map_err(internal_error)?;
|
||||
let snapshot_id = self.gen_snapshot_id(rev_id);
|
||||
let record = dsl::folder_rev_snapshot
|
||||
.filter(dsl::snapshot_id.eq(&snapshot_id))
|
||||
.first::<FolderSnapshotRecord>(&*conn)?;
|
||||
|
||||
Ok(Some(record.into()))
|
||||
}
|
||||
|
||||
fn read_last_snapshot(&self) -> FlowyResult<Option<RevisionSnapshot>> {
|
||||
let conn = self.pool.get().map_err(internal_error)?;
|
||||
let latest_record = dsl::folder_rev_snapshot
|
||||
.filter(dsl::object_id.eq(&self.object_id))
|
||||
.order(dsl::rev_id.desc())
|
||||
// .select(max(dsl::rev_id))
|
||||
// .select((dsl::id, dsl::object_id, dsl::rev_id, dsl::data))
|
||||
.first::<FolderSnapshotRecord>(&*conn)?;
|
||||
Ok(Some(latest_record.into()))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Clone, Debug, Queryable, Identifiable, Insertable, Associations)]
|
||||
#[table_name = "folder_rev_snapshot"]
|
||||
#[primary_key("snapshot_id")]
|
||||
struct FolderSnapshotRecord {
|
||||
snapshot_id: String,
|
||||
object_id: String,
|
||||
rev_id: i64,
|
||||
base_rev_id: i64,
|
||||
timestamp: i64,
|
||||
data: Vec<u8>,
|
||||
}
|
||||
|
||||
impl std::convert::From<FolderSnapshotRecord> for RevisionSnapshot {
|
||||
fn from(record: FolderSnapshotRecord) -> Self {
|
||||
RevisionSnapshot {
|
||||
rev_id: record.rev_id,
|
||||
base_rev_id: record.base_rev_id,
|
||||
timestamp: record.timestamp,
|
||||
data: Bytes::from(record.data),
|
||||
}
|
||||
}
|
||||
}
|
@ -1,2 +1,5 @@
|
||||
mod folder_rev_sqlite;
|
||||
mod folder_snapshot_sqlite_impl;
|
||||
|
||||
pub use folder_rev_sqlite::*;
|
||||
pub use folder_snapshot_sqlite_impl::*;
|
||||
|
Reference in New Issue
Block a user