fix: store full state as update as solution for serialization

This commit is contained in:
linonetwo
2023-02-08 15:36:04 +08:00
parent b1943aaad9
commit 0c21ccb04b
3 changed files with 13 additions and 84 deletions
+2 -26
View File
@@ -1,16 +1,9 @@
use ipc_types::document::{
CreateDocumentParameter, GetDocumentParameter, GetDocumentResponse, YDocumentUpdate,
};
use jwst::encode_update;
use jwst::DocStorage;
use jwst::Workspace as OctoBaseWorkspace;
use lib0::any::Any;
use std::sync::{Arc, RwLock};
use y_sync::sync::Message;
use y_sync::sync::MessageReader;
use y_sync::sync::SyncMessage;
use yrs::updates::decoder::DecoderV1;
use yrs::{updates::decoder::Decode, Doc, StateVector, Update};
use crate::state::AppState;
@@ -53,7 +46,6 @@ pub async fn get_doc<'s>(
) -> Result<GetDocumentResponse, String> {
// TODO: check user permission
let state = &state.0.lock().await;
let doc_store = &state.doc_store;
let doc_db = &state.doc_db;
if let Ok(all_updates_of_workspace) = doc_db.all(&parameters.id).await {
@@ -61,18 +53,8 @@ pub async fn get_doc<'s>(
.iter()
.map(|model| model.blob.clone())
.collect::<Vec<Vec<u8>>>();
all_updates
.iter()
.for_each(|update_blob| {
let mut tx = doc_store.doc().transact();
let update = Update::decode_v1(&update_blob).unwrap();
tx.apply_update(update);
tx.commit();
});
let merged_update = doc_store.doc().transact().encode_update_v1();
// TODO: store merged update here
Ok(GetDocumentResponse {
updates: vec![merged_update],
updates: all_updates,
})
} else {
Err(format!(
@@ -88,16 +70,10 @@ pub async fn update_y_document<'s>(
parameters: YDocumentUpdate,
) -> Result<bool, String> {
let state = &state.0.lock().await;
let doc_store = &state.doc_store;
let mut tx = doc_store.doc().transact();
let update = Update::decode_v1(&parameters.update).unwrap();
tx.apply_update(update);
let merged_update = tx.encode_update_v1();
tx.commit();
let doc_db = &state.doc_db;
doc_db
.replace_with(&parameters.id.clone(), merged_update)
.replace_with(&parameters.id.clone(), parameters.update)
.await
.ok();
-5
View File
@@ -7,8 +7,6 @@ use tokio::sync::Mutex;
pub struct AppStateRaw {
pub doc_db: DocAutoStorage,
/// yDoc for receiving yjs update and merge them, before serialize update into sqlite
pub doc_store: Workspace,
pub blob_storage: BlobAutoStorage,
pub metadata_db: SqliteDBContext,
}
@@ -41,9 +39,6 @@ impl AppStateRaw {
Some(Self {
doc_db: DocAutoStorage::init_pool(&doc_db_env).await.unwrap(),
// with fake id, we only use yDoc inside of it
// TODO: use workspace pool, to handle multiple workspace
doc_store: Workspace::new(""),
blob_storage: BlobAutoStorage::init_pool(&blob_db_env).await.unwrap(),
metadata_db: SqliteDBContext::new(metadata_db_env).await,
})