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

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();

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,
})

View File

@ -39,7 +39,7 @@ export class TauriIPCProvider extends LocalProvider {
}
// we create a default user if we don't have one.
try {
const user = await this.#ipc!.createUser({
const user = await this.#ipc?.createUser({
email: 'xxx@xx.xx',
name: 'xxx',
password: 'xxx',
@ -57,7 +57,7 @@ export class TauriIPCProvider extends LocalProvider {
blocksuiteWorkspace: BlocksuiteWorkspace
) {
this._logger(`Loading ${workspaceID}...`);
const result = await this.#ipc!.getYDocument({ id: workspaceID });
const result = await this.#ipc?.getYDocument({ id: workspaceID });
if (result) {
const updates = result.updates.map(
binaryUpdate => new Uint8Array(binaryUpdate)
@ -65,21 +65,6 @@ export class TauriIPCProvider extends LocalProvider {
const mergedUpdate = Y.mergeUpdates(updates);
await applyUpdate(blocksuiteWorkspace, mergedUpdate);
console.group('#initDocFromIPC');
// DEBUG: console blocksuiteWorkspace.room
console.log(`blocksuiteWorkspace.room`, blocksuiteWorkspace.room);
// DEBUG: console blocksuiteWorkspace.doc.guid
console.log(`blocksuiteWorkspace.doc.guid`, blocksuiteWorkspace.doc.guid);
// DEBUG: console blocksuiteWorkspace
console.log(`blocksuiteWorkspace`, blocksuiteWorkspace);
// DEBUG: console blocksuiteWorkspace.meta
console.log(`blocksuiteWorkspace.meta`, blocksuiteWorkspace.meta);
// DEBUG: console blocksuiteWorkspace.meta.pageMetas
console.log(
`blocksuiteWorkspace.meta.pageMetas`,
blocksuiteWorkspace.meta.pageMetas
);
console.groupEnd();
this._logger(`Loaded: ${workspaceID}`);
}
}
@ -91,33 +76,14 @@ export class TauriIPCProvider extends LocalProvider {
this._logger(`Connecting yDoc for ${workspaceID}...`);
blocksuiteWorkspace.doc.on('update', async (update: Uint8Array) => {
try {
// TODO: need handle potential data race when update is frequent?
// TODO: update seems too frequent upon each keydown, why no batching?
const success = await this.#ipc!.updateYDocument({
update: Array.from(update),
const binary = Y.encodeStateAsUpdate(blocksuiteWorkspace.doc);
const success = await this.#ipc?.updateYDocument({
update: Array.from(binary),
id: workspaceID,
});
if (!success) {
throw new Error(`YDoc update failed, id: ${workspaceID}`);
}
console.group('update');
// DEBUG: console blocksuiteWorkspa?ce.meta
console.log(`blocksuiteWorkspace?.meta`, blocksuiteWorkspace?.meta);
// DEBUG: console blocksuiteWorkspace?.meta?.pageMetas
console.log(
`blocksuiteWorkspace?.meta?.pageMetas`,
blocksuiteWorkspace?.meta?.pageMetas
);
// DEBUG: console doc
console.log(`doc1`, blocksuiteWorkspace.doc);
// DEBUG: console doc.meta
console.log(`doc1.meta`, blocksuiteWorkspace.doc.meta);
// DEBUG: console doc.meta.pageMetas
console.log(
`doc1.meta?.pageMetas`,
blocksuiteWorkspace.doc.meta?.pageMetas
);
console.groupEnd();
} catch (error) {
// TODO: write error log to disk, and add button to open them in settings panel
console.error("#yDocument.on('update'", error);
@ -130,7 +96,7 @@ export class TauriIPCProvider extends LocalProvider {
}
override async warpWorkspace(blocksuiteWorkspace: BlocksuiteWorkspace) {
const { doc, room } = blocksuiteWorkspace;
const { room } = blocksuiteWorkspace;
assert(room);
(await blocksuiteWorkspace.blobs)?.addProvider(new IPCBlobProvider());
@ -144,8 +110,8 @@ export class TauriIPCProvider extends LocalProvider {
meta: CreateWorkspaceInfoParams
): Promise<WorkspaceUnit | undefined> {
this._logger('Creating client app workspace');
const { id } = await this.#ipc!.createWorkspace({
assert(this.#ipc);
const { id } = await this.#ipc.createWorkspace({
name: meta.name,
// TODO: get userID here
user_id: this.#defaultUserID,
@ -165,7 +131,7 @@ export class TauriIPCProvider extends LocalProvider {
const doc = workspaceUnit?.blocksuiteWorkspace?.doc;
if (doc) {
const update = Y.encodeStateAsUpdate(doc);
const success = await this.#ipc!.updateYDocument({
const success = await this.#ipc?.updateYDocument({
update: Array.from(update),
id,
});
@ -174,7 +140,8 @@ export class TauriIPCProvider extends LocalProvider {
}
override async loadWorkspaces(): Promise<WorkspaceUnit[]> {
const { workspaces } = await this.#ipc!.getWorkspaces({
assert(this.#ipc);
const { workspaces } = await this.#ipc.getWorkspaces({
user_id: this.#defaultUserID,
});
const workspaceUnits = await Promise.all(
@ -193,12 +160,3 @@ export class TauriIPCProvider extends LocalProvider {
return workspaceUnits;
}
}
function stringifyUint8Array(uint8: Uint8Array): string {
return (
' ' +
Array.from(uint8)
.map(a => a.toString(16).padStart(2, '0'))
.join(' ')
).replace(/((?: \S+){8})/g, '$1\n');
}