mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
This commit is contained in:
parent
a654623c12
commit
df5266d7c9
@ -10,7 +10,7 @@ use backend_service::errors::{invalid_params, ServerError};
|
||||
use bytes::Bytes;
|
||||
use chrono::Utc;
|
||||
use flowy_collaboration::{
|
||||
entities::revision::{RepeatedRevision, RevType, Revision},
|
||||
entities::revision::{RepeatedRevision, Revision},
|
||||
protobuf::CreateDocParams,
|
||||
};
|
||||
use flowy_core_data_model::{
|
||||
|
@ -5,7 +5,7 @@ use flowy_collaboration::{
|
||||
document::{Document, PlainDoc},
|
||||
entities::{
|
||||
doc::{CreateDocParams, DocumentId},
|
||||
revision::{md5, RepeatedRevision, RevType, Revision},
|
||||
revision::{md5, RepeatedRevision, Revision},
|
||||
},
|
||||
};
|
||||
use flowy_core_data_model::entities::{
|
||||
|
@ -3,6 +3,7 @@ import 'package:dartz/dartz.dart';
|
||||
import 'package:flowy_log/flowy_log.dart';
|
||||
// ignore: unnecessary_import
|
||||
import 'package:flowy_sdk/protobuf/dart-ffi/ffi_response.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/dart-ffi/ffi_request.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-net/event.pb.dart';
|
||||
import 'package:flowy_sdk/protobuf/flowy-net/network_state.pb.dart';
|
||||
|
@ -9,7 +9,7 @@ use crate::{
|
||||
use bytes::Bytes;
|
||||
use flowy_collaboration::{
|
||||
document::history::UndoResult,
|
||||
entities::revision::{RevId, RevType, Revision},
|
||||
entities::revision::{RevId, Revision},
|
||||
errors::CollaborateResult,
|
||||
};
|
||||
use flowy_database::ConnectionPool;
|
||||
|
@ -6,20 +6,16 @@ use crate::{
|
||||
},
|
||||
sql_tables::{RevisionChangeset, RevisionTableState},
|
||||
};
|
||||
use dashmap::DashMap;
|
||||
|
||||
use flowy_collaboration::entities::revision::{Revision, RevisionRange, RevisionState};
|
||||
use flowy_database::ConnectionPool;
|
||||
use flowy_error::{internal_error, FlowyResult};
|
||||
use lib_infra::future::FutureResult;
|
||||
use lib_ot::errors::OTError;
|
||||
use std::{
|
||||
collections::VecDeque,
|
||||
sync::{
|
||||
atomic::{AtomicI64, Ordering::SeqCst},
|
||||
Arc,
|
||||
},
|
||||
|
||||
use std::sync::{
|
||||
atomic::{AtomicI64, Ordering::SeqCst},
|
||||
Arc,
|
||||
};
|
||||
use tokio::{sync::RwLock, task::spawn_blocking};
|
||||
use tokio::task::spawn_blocking;
|
||||
|
||||
pub struct RevisionCache {
|
||||
doc_id: String,
|
||||
@ -41,12 +37,21 @@ impl RevisionCache {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn add(&self, revision: Revision, state: RevisionState) -> FlowyResult<RevisionRecord> {
|
||||
pub async fn add(
|
||||
&self,
|
||||
revision: Revision,
|
||||
state: RevisionState,
|
||||
write_to_disk: bool,
|
||||
) -> FlowyResult<RevisionRecord> {
|
||||
if self.memory_cache.contains(&revision.rev_id) {
|
||||
return Err(FlowyError::internal().context(format!("Duplicate remote revision id: {}", revision.rev_id)));
|
||||
}
|
||||
let rev_id = revision.rev_id;
|
||||
let record = RevisionRecord { revision, state };
|
||||
let record = RevisionRecord {
|
||||
revision,
|
||||
state,
|
||||
write_to_disk,
|
||||
};
|
||||
self.memory_cache.add(&record).await;
|
||||
self.set_latest_rev_id(rev_id);
|
||||
Ok(record)
|
||||
@ -111,6 +116,7 @@ impl RevisionCache {
|
||||
.map(|revision| RevisionRecord {
|
||||
revision,
|
||||
state: RevisionState::Local,
|
||||
write_to_disk: true,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
@ -128,9 +134,13 @@ impl RevisionCache {
|
||||
}
|
||||
|
||||
impl RevisionMemoryCacheDelegate for Arc<Persistence> {
|
||||
fn checkpoint_tick(&self, records: Vec<RevisionRecord>) -> FlowyResult<()> {
|
||||
fn checkpoint_tick(&self, mut records: Vec<RevisionRecord>) -> FlowyResult<()> {
|
||||
let conn = &*self.pool.get().map_err(internal_error)?;
|
||||
self.write_revision_records(records, &conn)
|
||||
records.retain(|record| record.write_to_disk);
|
||||
if !records.is_empty() {
|
||||
let _ = self.write_revision_records(records, &conn)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn receive_ack(&self, doc_id: &str, rev_id: i64) {
|
||||
@ -150,6 +160,7 @@ impl RevisionMemoryCacheDelegate for Arc<Persistence> {
|
||||
pub struct RevisionRecord {
|
||||
pub revision: Revision,
|
||||
pub state: RevisionState,
|
||||
pub write_to_disk: bool,
|
||||
}
|
||||
|
||||
impl RevisionRecord {
|
||||
|
@ -7,7 +7,7 @@ use dashmap::DashMap;
|
||||
use flowy_collaboration::{
|
||||
entities::{
|
||||
doc::DocumentInfo,
|
||||
revision::{RepeatedRevision, RevType, Revision, RevisionRange, RevisionState},
|
||||
revision::{RepeatedRevision, Revision, RevisionRange, RevisionState},
|
||||
},
|
||||
util::{md5, RevIdCounter},
|
||||
};
|
||||
@ -68,13 +68,13 @@ impl RevisionManager {
|
||||
#[tracing::instrument(level = "debug", skip(self, revision))]
|
||||
pub async fn add_remote_revision(&self, revision: &Revision) -> Result<(), FlowyError> {
|
||||
self.rev_id_counter.set(revision.rev_id);
|
||||
let _ = self.cache.add(revision.clone(), RevisionState::Ack).await?;
|
||||
let _ = self.cache.add(revision.clone(), RevisionState::Ack, true).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(self, revision))]
|
||||
pub async fn add_local_revision(&self, revision: &Revision) -> Result<(), FlowyError> {
|
||||
let record = self.cache.add(revision.clone(), RevisionState::Local).await?;
|
||||
let record = self.cache.add(revision.clone(), RevisionState::Local, true).await?;
|
||||
self.sync_seq.add_revision(record).await?;
|
||||
Ok(())
|
||||
}
|
||||
@ -206,14 +206,18 @@ impl RevisionLoader {
|
||||
&self.user_id,
|
||||
doc_md5,
|
||||
);
|
||||
let _ = self.cache.add(revision.clone(), RevisionState::Ack).await?;
|
||||
let _ = self.cache.add(revision.clone(), RevisionState::Ack, true).await?;
|
||||
revisions = vec![revision];
|
||||
} else {
|
||||
for record in &records {
|
||||
match record.state {
|
||||
RevisionState::Local => {
|
||||
//
|
||||
match self.cache.add(record.revision.clone(), RevisionState::Local).await {
|
||||
match self
|
||||
.cache
|
||||
.add(record.revision.clone(), RevisionState::Local, false)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {},
|
||||
Err(e) => tracing::error!("{}", e),
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ use crate::services::doc::{
|
||||
use bytes::Bytes;
|
||||
use flowy_collaboration::{
|
||||
entities::{
|
||||
revision::{RepeatedRevision, RevType, Revision, RevisionRange},
|
||||
revision::{RepeatedRevision, Revision, RevisionRange},
|
||||
ws::{DocumentClientWSData, NewDocumentUser},
|
||||
},
|
||||
errors::CollaborateResult,
|
||||
|
@ -80,6 +80,7 @@ pub(crate) fn mk_revision_record_from_table(user_id: &str, table: RevisionTable)
|
||||
RevisionRecord {
|
||||
revision,
|
||||
state: table.state.into(),
|
||||
write_to_disk: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -98,7 +98,7 @@ impl CreateViewParams {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn take_view_data(&mut self) -> String { ::std::mem::replace(&mut self.view_data, String::new()) }
|
||||
pub fn take_view_data(&mut self) -> String { std::mem::take(&mut self.view_data) }
|
||||
}
|
||||
|
||||
impl TryInto<CreateViewParams> for CreateViewRequest {
|
||||
|
Loading…
Reference in New Issue
Block a user