mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
refactor: md5 of revision
This commit is contained in:
@ -28,9 +28,9 @@ impl Document {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn md5(&self) -> String {
|
||||
// format!("{:x}", md5::compute(bytes))
|
||||
"".to_owned()
|
||||
pub fn document_md5(&self) -> String {
|
||||
let bytes = self.tree.to_bytes();
|
||||
format!("{:x}", md5::compute(&bytes))
|
||||
}
|
||||
|
||||
pub fn get_tree(&self) -> &NodeTree {
|
||||
|
@ -63,7 +63,7 @@ impl DocumentQueue {
|
||||
Command::ComposeTransaction { transaction, ret } => {
|
||||
self.document.write().await.apply_transaction(transaction.clone())?;
|
||||
let _ = self
|
||||
.save_local_operations(transaction, self.document.read().await.md5())
|
||||
.save_local_operations(transaction, self.document.read().await.document_md5())
|
||||
.await?;
|
||||
let _ = ret.send(Ok(()));
|
||||
}
|
||||
|
@ -12,11 +12,8 @@ use flowy_revision::{
|
||||
RevisionCloudService, RevisionManager, RevisionPersistence, RevisionWebSocket, SQLiteRevisionSnapshotPersistence,
|
||||
};
|
||||
use flowy_sync::client_document::initial_delta_document_content;
|
||||
use flowy_sync::entities::{
|
||||
document::DocumentIdPB,
|
||||
revision::{md5, RepeatedRevision, Revision},
|
||||
ws_data::ServerRevisionWSData,
|
||||
};
|
||||
use flowy_sync::entities::{document::DocumentIdPB, revision::Revision, ws_data::ServerRevisionWSData};
|
||||
use flowy_sync::util::md5;
|
||||
use lib_infra::future::FutureResult;
|
||||
use lib_ws::WSConnectState;
|
||||
use std::any::Any;
|
||||
@ -139,7 +136,7 @@ impl DocumentManager {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn create_document<T: AsRef<str>>(&self, doc_id: T, revisions: RepeatedRevision) -> FlowyResult<()> {
|
||||
pub async fn create_document<T: AsRef<str>>(&self, doc_id: T, revisions: Vec<Revision>) -> FlowyResult<()> {
|
||||
let doc_id = doc_id.as_ref().to_owned();
|
||||
let db_pool = self.persistence.database.db_pool()?;
|
||||
// Maybe we could save the document to disk without creating the RevisionManager
|
||||
|
@ -3,7 +3,7 @@ use crate::DocumentUser;
|
||||
use async_stream::stream;
|
||||
use flowy_database::ConnectionPool;
|
||||
use flowy_error::FlowyError;
|
||||
use flowy_revision::{OperationsMD5, RevisionManager, TransformOperations};
|
||||
use flowy_revision::{RevisionMD5, RevisionManager, TransformOperations};
|
||||
use flowy_sync::{
|
||||
client_document::{history::UndoResult, ClientDocument},
|
||||
entities::revision::{RevId, Revision},
|
||||
@ -70,7 +70,7 @@ impl EditDocumentQueue {
|
||||
EditorCommand::ComposeLocalOperations { operations, ret } => {
|
||||
let mut document = self.document.write().await;
|
||||
let _ = document.compose_operations(operations.clone())?;
|
||||
let md5 = document.md5();
|
||||
let md5 = document.document_md5();
|
||||
drop(document);
|
||||
let _ = self.save_local_operations(operations, md5).await?;
|
||||
let _ = ret.send(Ok(()));
|
||||
@ -78,16 +78,16 @@ impl EditDocumentQueue {
|
||||
EditorCommand::ComposeRemoteOperation { client_operations, ret } => {
|
||||
let mut document = self.document.write().await;
|
||||
let _ = document.compose_operations(client_operations.clone())?;
|
||||
let md5 = document.md5();
|
||||
let md5 = document.document_md5();
|
||||
drop(document);
|
||||
let _ = ret.send(Ok(md5));
|
||||
let _ = ret.send(Ok(md5.into()));
|
||||
}
|
||||
EditorCommand::ResetOperations { operations, ret } => {
|
||||
let mut document = self.document.write().await;
|
||||
let _ = document.set_operations(operations);
|
||||
let md5 = document.md5();
|
||||
let md5 = document.document_md5();
|
||||
drop(document);
|
||||
let _ = ret.send(Ok(md5));
|
||||
let _ = ret.send(Ok(md5.into()));
|
||||
}
|
||||
EditorCommand::TransformOperations { operations, ret } => {
|
||||
let f = || async {
|
||||
@ -114,14 +114,14 @@ impl EditDocumentQueue {
|
||||
EditorCommand::Insert { index, data, ret } => {
|
||||
let mut write_guard = self.document.write().await;
|
||||
let operations = write_guard.insert(index, data)?;
|
||||
let md5 = write_guard.md5();
|
||||
let md5 = write_guard.document_md5();
|
||||
let _ = self.save_local_operations(operations, md5).await?;
|
||||
let _ = ret.send(Ok(()));
|
||||
}
|
||||
EditorCommand::Delete { interval, ret } => {
|
||||
let mut write_guard = self.document.write().await;
|
||||
let operations = write_guard.delete(interval)?;
|
||||
let md5 = write_guard.md5();
|
||||
let md5 = write_guard.document_md5();
|
||||
let _ = self.save_local_operations(operations, md5).await?;
|
||||
let _ = ret.send(Ok(()));
|
||||
}
|
||||
@ -132,14 +132,14 @@ impl EditDocumentQueue {
|
||||
} => {
|
||||
let mut write_guard = self.document.write().await;
|
||||
let operations = write_guard.format(interval, attribute)?;
|
||||
let md5 = write_guard.md5();
|
||||
let md5 = write_guard.document_md5();
|
||||
let _ = self.save_local_operations(operations, md5).await?;
|
||||
let _ = ret.send(Ok(()));
|
||||
}
|
||||
EditorCommand::Replace { interval, data, ret } => {
|
||||
let mut write_guard = self.document.write().await;
|
||||
let operations = write_guard.replace(interval, data)?;
|
||||
let md5 = write_guard.md5();
|
||||
let md5 = write_guard.document_md5();
|
||||
let _ = self.save_local_operations(operations, md5).await?;
|
||||
let _ = ret.send(Ok(()));
|
||||
}
|
||||
@ -152,14 +152,14 @@ impl EditDocumentQueue {
|
||||
EditorCommand::Undo { ret } => {
|
||||
let mut write_guard = self.document.write().await;
|
||||
let UndoResult { operations } = write_guard.undo()?;
|
||||
let md5 = write_guard.md5();
|
||||
let md5 = write_guard.document_md5();
|
||||
let _ = self.save_local_operations(operations, md5).await?;
|
||||
let _ = ret.send(Ok(()));
|
||||
}
|
||||
EditorCommand::Redo { ret } => {
|
||||
let mut write_guard = self.document.write().await;
|
||||
let UndoResult { operations } = write_guard.redo()?;
|
||||
let md5 = write_guard.md5();
|
||||
let md5 = write_guard.document_md5();
|
||||
let _ = self.save_local_operations(operations, md5).await?;
|
||||
let _ = ret.send(Ok(()));
|
||||
}
|
||||
@ -197,11 +197,11 @@ pub(crate) enum EditorCommand {
|
||||
},
|
||||
ComposeRemoteOperation {
|
||||
client_operations: DeltaTextOperations,
|
||||
ret: Ret<OperationsMD5>,
|
||||
ret: Ret<RevisionMD5>,
|
||||
},
|
||||
ResetOperations {
|
||||
operations: DeltaTextOperations,
|
||||
ret: Ret<OperationsMD5>,
|
||||
ret: Ret<RevisionMD5>,
|
||||
},
|
||||
TransformOperations {
|
||||
operations: DeltaTextOperations,
|
||||
|
@ -136,7 +136,7 @@ impl ConflictResolver<DeltaDocumentResolveOperations> for DocumentConflictResolv
|
||||
fn compose_operations(
|
||||
&self,
|
||||
operations: DeltaDocumentResolveOperations,
|
||||
) -> BoxResultFuture<OperationsMD5, FlowyError> {
|
||||
) -> BoxResultFuture<RevisionMD5, FlowyError> {
|
||||
let tx = self.edit_cmd_tx.clone();
|
||||
let operations = operations.into_inner();
|
||||
Box::pin(async move {
|
||||
@ -172,10 +172,7 @@ impl ConflictResolver<DeltaDocumentResolveOperations> for DocumentConflictResolv
|
||||
})
|
||||
}
|
||||
|
||||
fn reset_operations(
|
||||
&self,
|
||||
operations: DeltaDocumentResolveOperations,
|
||||
) -> BoxResultFuture<OperationsMD5, FlowyError> {
|
||||
fn reset_operations(&self, operations: DeltaDocumentResolveOperations) -> BoxResultFuture<RevisionMD5, FlowyError> {
|
||||
let tx = self.edit_cmd_tx.clone();
|
||||
let operations = operations.into_inner();
|
||||
Box::pin(async move {
|
||||
|
@ -4,9 +4,9 @@ use crate::DocumentDatabase;
|
||||
use bytes::Bytes;
|
||||
use flowy_database::kv::KV;
|
||||
use flowy_error::FlowyResult;
|
||||
use flowy_revision::disk::{RevisionDiskCache, RevisionRecord};
|
||||
use flowy_sync::entities::revision::{md5, Revision};
|
||||
use flowy_sync::util::make_operations_from_revisions;
|
||||
use flowy_revision::disk::{RevisionDiskCache, SyncRecord};
|
||||
use flowy_sync::entities::revision::Revision;
|
||||
use flowy_sync::util::{make_operations_from_revisions, md5};
|
||||
use std::sync::Arc;
|
||||
|
||||
const V1_MIGRATION: &str = "DOCUMENT_V1_MIGRATION";
|
||||
@ -44,7 +44,7 @@ impl DocumentMigration {
|
||||
let bytes = Bytes::from(transaction.to_bytes()?);
|
||||
let md5 = format!("{:x}", md5::compute(&bytes));
|
||||
let revision = Revision::new(&document_id, 0, 1, bytes, &self.user_id, md5);
|
||||
let record = RevisionRecord::new(revision);
|
||||
let record = SyncRecord::new(revision);
|
||||
match disk_cache.create_revision_records(vec![record]) {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
|
@ -7,7 +7,7 @@ use flowy_database::{
|
||||
ConnectionPool,
|
||||
};
|
||||
use flowy_error::{internal_error, FlowyError, FlowyResult};
|
||||
use flowy_revision::disk::{RevisionChangeset, RevisionDiskCache, RevisionRecord, RevisionState};
|
||||
use flowy_revision::disk::{RevisionChangeset, RevisionDiskCache, RevisionState, SyncRecord};
|
||||
use flowy_sync::{
|
||||
entities::revision::{RevType, Revision, RevisionRange},
|
||||
util::md5,
|
||||
@ -23,7 +23,7 @@ pub struct SQLiteDeltaDocumentRevisionPersistence {
|
||||
impl RevisionDiskCache<Arc<ConnectionPool>> for SQLiteDeltaDocumentRevisionPersistence {
|
||||
type Error = FlowyError;
|
||||
|
||||
fn create_revision_records(&self, revision_records: Vec<RevisionRecord>) -> Result<(), Self::Error> {
|
||||
fn create_revision_records(&self, revision_records: Vec<SyncRecord>) -> Result<(), Self::Error> {
|
||||
let conn = self.pool.get().map_err(internal_error)?;
|
||||
let _ = DeltaRevisionSql::create(revision_records, &*conn)?;
|
||||
Ok(())
|
||||
@ -37,7 +37,7 @@ impl RevisionDiskCache<Arc<ConnectionPool>> for SQLiteDeltaDocumentRevisionPersi
|
||||
&self,
|
||||
object_id: &str,
|
||||
rev_ids: Option<Vec<i64>>,
|
||||
) -> Result<Vec<RevisionRecord>, Self::Error> {
|
||||
) -> Result<Vec<SyncRecord>, Self::Error> {
|
||||
let conn = self.pool.get().map_err(internal_error)?;
|
||||
let records = DeltaRevisionSql::read(&self.user_id, object_id, rev_ids, &*conn)?;
|
||||
Ok(records)
|
||||
@ -47,7 +47,7 @@ impl RevisionDiskCache<Arc<ConnectionPool>> for SQLiteDeltaDocumentRevisionPersi
|
||||
&self,
|
||||
object_id: &str,
|
||||
range: &RevisionRange,
|
||||
) -> Result<Vec<RevisionRecord>, Self::Error> {
|
||||
) -> Result<Vec<SyncRecord>, Self::Error> {
|
||||
let conn = &*self.pool.get().map_err(internal_error)?;
|
||||
let revisions = DeltaRevisionSql::read_with_range(&self.user_id, object_id, range.clone(), conn)?;
|
||||
Ok(revisions)
|
||||
@ -74,7 +74,7 @@ impl RevisionDiskCache<Arc<ConnectionPool>> for SQLiteDeltaDocumentRevisionPersi
|
||||
&self,
|
||||
object_id: &str,
|
||||
deleted_rev_ids: Option<Vec<i64>>,
|
||||
inserted_records: Vec<RevisionRecord>,
|
||||
inserted_records: Vec<SyncRecord>,
|
||||
) -> Result<(), Self::Error> {
|
||||
let conn = self.pool.get().map_err(internal_error)?;
|
||||
conn.immediate_transaction::<_, FlowyError, _>(|| {
|
||||
@ -97,7 +97,7 @@ impl SQLiteDeltaDocumentRevisionPersistence {
|
||||
pub struct DeltaRevisionSql {}
|
||||
|
||||
impl DeltaRevisionSql {
|
||||
fn create(revision_records: Vec<RevisionRecord>, conn: &SqliteConnection) -> Result<(), FlowyError> {
|
||||
fn create(revision_records: Vec<SyncRecord>, conn: &SqliteConnection) -> Result<(), FlowyError> {
|
||||
// Batch insert: https://diesel.rs/guides/all-about-inserts.html
|
||||
|
||||
let records = revision_records
|
||||
@ -143,7 +143,7 @@ impl DeltaRevisionSql {
|
||||
object_id: &str,
|
||||
rev_ids: Option<Vec<i64>>,
|
||||
conn: &SqliteConnection,
|
||||
) -> Result<Vec<RevisionRecord>, FlowyError> {
|
||||
) -> Result<Vec<SyncRecord>, FlowyError> {
|
||||
let mut sql = dsl::rev_table.filter(dsl::doc_id.eq(object_id)).into_boxed();
|
||||
if let Some(rev_ids) = rev_ids {
|
||||
sql = sql.filter(dsl::rev_id.eq_any(rev_ids));
|
||||
@ -162,7 +162,7 @@ impl DeltaRevisionSql {
|
||||
object_id: &str,
|
||||
range: RevisionRange,
|
||||
conn: &SqliteConnection,
|
||||
) -> Result<Vec<RevisionRecord>, FlowyError> {
|
||||
) -> Result<Vec<SyncRecord>, FlowyError> {
|
||||
let rev_tables = dsl::rev_table
|
||||
.filter(dsl::rev_id.ge(range.start))
|
||||
.filter(dsl::rev_id.le(range.end))
|
||||
@ -244,7 +244,7 @@ impl std::default::Default for TextRevisionState {
|
||||
}
|
||||
}
|
||||
|
||||
fn mk_revision_record_from_table(user_id: &str, table: RevisionTable) -> RevisionRecord {
|
||||
fn mk_revision_record_from_table(user_id: &str, table: RevisionTable) -> SyncRecord {
|
||||
let md5 = md5(&table.data);
|
||||
let revision = Revision::new(
|
||||
&table.doc_id,
|
||||
@ -254,7 +254,7 @@ fn mk_revision_record_from_table(user_id: &str, table: RevisionTable) -> Revisio
|
||||
user_id,
|
||||
md5,
|
||||
);
|
||||
RevisionRecord {
|
||||
SyncRecord {
|
||||
revision,
|
||||
state: table.state.into(),
|
||||
write_to_disk: false,
|
||||
|
@ -7,7 +7,7 @@ use flowy_database::{
|
||||
ConnectionPool,
|
||||
};
|
||||
use flowy_error::{internal_error, FlowyError, FlowyResult};
|
||||
use flowy_revision::disk::{RevisionChangeset, RevisionDiskCache, RevisionRecord, RevisionState};
|
||||
use flowy_revision::disk::{RevisionChangeset, RevisionDiskCache, RevisionState, SyncRecord};
|
||||
use flowy_sync::{
|
||||
entities::revision::{Revision, RevisionRange},
|
||||
util::md5,
|
||||
@ -22,7 +22,7 @@ pub struct SQLiteDocumentRevisionPersistence {
|
||||
impl RevisionDiskCache<Arc<ConnectionPool>> for SQLiteDocumentRevisionPersistence {
|
||||
type Error = FlowyError;
|
||||
|
||||
fn create_revision_records(&self, revision_records: Vec<RevisionRecord>) -> Result<(), Self::Error> {
|
||||
fn create_revision_records(&self, revision_records: Vec<SyncRecord>) -> Result<(), Self::Error> {
|
||||
let conn = self.pool.get().map_err(internal_error)?;
|
||||
let _ = DocumentRevisionSql::create(revision_records, &*conn)?;
|
||||
Ok(())
|
||||
@ -36,7 +36,7 @@ impl RevisionDiskCache<Arc<ConnectionPool>> for SQLiteDocumentRevisionPersistenc
|
||||
&self,
|
||||
object_id: &str,
|
||||
rev_ids: Option<Vec<i64>>,
|
||||
) -> Result<Vec<RevisionRecord>, Self::Error> {
|
||||
) -> Result<Vec<SyncRecord>, Self::Error> {
|
||||
let conn = self.pool.get().map_err(internal_error)?;
|
||||
let records = DocumentRevisionSql::read(&self.user_id, object_id, rev_ids, &*conn)?;
|
||||
Ok(records)
|
||||
@ -46,7 +46,7 @@ impl RevisionDiskCache<Arc<ConnectionPool>> for SQLiteDocumentRevisionPersistenc
|
||||
&self,
|
||||
object_id: &str,
|
||||
range: &RevisionRange,
|
||||
) -> Result<Vec<RevisionRecord>, Self::Error> {
|
||||
) -> Result<Vec<SyncRecord>, Self::Error> {
|
||||
let conn = &*self.pool.get().map_err(internal_error)?;
|
||||
let revisions = DocumentRevisionSql::read_with_range(&self.user_id, object_id, range.clone(), conn)?;
|
||||
Ok(revisions)
|
||||
@ -73,7 +73,7 @@ impl RevisionDiskCache<Arc<ConnectionPool>> for SQLiteDocumentRevisionPersistenc
|
||||
&self,
|
||||
object_id: &str,
|
||||
deleted_rev_ids: Option<Vec<i64>>,
|
||||
inserted_records: Vec<RevisionRecord>,
|
||||
inserted_records: Vec<SyncRecord>,
|
||||
) -> Result<(), Self::Error> {
|
||||
let conn = self.pool.get().map_err(internal_error)?;
|
||||
conn.immediate_transaction::<_, FlowyError, _>(|| {
|
||||
@ -96,7 +96,7 @@ impl SQLiteDocumentRevisionPersistence {
|
||||
struct DocumentRevisionSql {}
|
||||
|
||||
impl DocumentRevisionSql {
|
||||
fn create(revision_records: Vec<RevisionRecord>, conn: &SqliteConnection) -> Result<(), FlowyError> {
|
||||
fn create(revision_records: Vec<SyncRecord>, conn: &SqliteConnection) -> Result<(), FlowyError> {
|
||||
// Batch insert: https://diesel.rs/guides/all-about-inserts.html
|
||||
let records = revision_records
|
||||
.into_iter()
|
||||
@ -142,7 +142,7 @@ impl DocumentRevisionSql {
|
||||
object_id: &str,
|
||||
rev_ids: Option<Vec<i64>>,
|
||||
conn: &SqliteConnection,
|
||||
) -> Result<Vec<RevisionRecord>, FlowyError> {
|
||||
) -> Result<Vec<SyncRecord>, FlowyError> {
|
||||
let mut sql = dsl::document_rev_table
|
||||
.filter(dsl::document_id.eq(object_id))
|
||||
.into_boxed();
|
||||
@ -163,7 +163,7 @@ impl DocumentRevisionSql {
|
||||
object_id: &str,
|
||||
range: RevisionRange,
|
||||
conn: &SqliteConnection,
|
||||
) -> Result<Vec<RevisionRecord>, FlowyError> {
|
||||
) -> Result<Vec<SyncRecord>, FlowyError> {
|
||||
let rev_tables = dsl::document_rev_table
|
||||
.filter(dsl::rev_id.ge(range.start))
|
||||
.filter(dsl::rev_id.le(range.end))
|
||||
@ -220,7 +220,7 @@ impl std::default::Default for DocumentRevisionState {
|
||||
}
|
||||
}
|
||||
|
||||
fn mk_revision_record_from_table(user_id: &str, table: DocumentRevisionTable) -> RevisionRecord {
|
||||
fn mk_revision_record_from_table(user_id: &str, table: DocumentRevisionTable) -> SyncRecord {
|
||||
let md5 = md5(&table.data);
|
||||
let revision = Revision::new(
|
||||
&table.document_id,
|
||||
@ -230,7 +230,7 @@ fn mk_revision_record_from_table(user_id: &str, table: DocumentRevisionTable) ->
|
||||
user_id,
|
||||
md5,
|
||||
);
|
||||
RevisionRecord {
|
||||
SyncRecord {
|
||||
revision,
|
||||
state: table.state.into(),
|
||||
write_to_disk: false,
|
||||
|
Reference in New Issue
Block a user