Skip to content

Commit

Permalink
chore: Wasm deps (AppFlowy-IO#151)
Browse files Browse the repository at this point in the history
* chore: fix deps

* chore: fix deps

* chore: fix deps

* chore: fix deps

* chore: fix deps

* chore: fix deps

* chore: fix deps

* chore: fix deps

* chore: fix deps

* chore: fix deps

* chore: fix deps

* chore: fix deps

* chore: fix deps

* chore: fix deps

* chore: fix deps
  • Loading branch information
appflowy authored Jan 21, 2024
1 parent ae417ce commit 3eef93f
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 42 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion collab-database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ futures = "0.3.30"
zip = "0.6.6"

[features]
wasm_build = ["getrandom/js"]
wasm_build = ["getrandom/js", "collab-plugins/wasm_build"]

8 changes: 4 additions & 4 deletions collab-plugins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ dotenv = "0.15.0"
futures = "0.3"

[target.'cfg(target_arch = "wasm32")'.dependencies]
indexed_db_futures = { version = "0.4.1" }
indexed_db_futures = { version = "0.4" }
js-sys = "0.3"
async-stream = "0.3.4"
async-stream = "0.3"
futures = "0.3"
wasm-bindgen = "0.2"
web-sys = { version = "0.3.67", features = ["console", "Window"] }
wasm-bindgen-futures = "0.4.40"
web-sys = { version = "0.3", features = ["console", "Window"] }
wasm-bindgen-futures = "0.4"
tracing-wasm = "0.2"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion collab-plugins/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ if_native! {
}

if_wasm! {
pub type CollabKVDB = local_storage::indexeddb::kv_impl::CollabIndexeddb;
pub type CollabKVDB = local_storage::indexeddb::CollabIndexeddb;
}
93 changes: 65 additions & 28 deletions collab-plugins/src/local_storage/indexeddb/kv_impl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::local_storage::kv::PersistenceError;
use collab::core::collab_plugin::EncodedCollab;
use indexed_db_futures::js_sys::wasm_bindgen::JsValue;
use indexed_db_futures::prelude::*;
use js_sys::{ArrayBuffer, Uint8Array};

Expand All @@ -15,8 +14,7 @@ use indexed_db_futures::web_sys::IdbKeyRange;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::error;
use wasm_bindgen::JsCast;
use web_sys::console;
use wasm_bindgen::{JsCast, JsValue};
use yrs::updates::decoder::Decode;
use yrs::{Doc, Transact, Update};

Expand Down Expand Up @@ -195,45 +193,56 @@ impl CollabIndexeddb {
Ok(EncodedCollab::new_v1(sv, doc_stata))
}

pub async fn is_exist(&self, uid: i64, object_id: &str) -> Result<bool, PersistenceError> {
let read_guard = self.db.read().await;
let transaction =
read_guard.transaction_on_one_with_mode(COLLAB_KV_STORE, IdbTransactionMode::Readonly)?;
let store = store_from_transaction(&transaction)?;
Ok(self.get_doc_id(&store, uid, object_id).await.is_some())
}

pub async fn delete_doc(&self, uid: i64, object_id: &str) -> Result<(), PersistenceError> {
let write_guard = self.db.write().await;
let transaction =
write_guard.transaction_on_one_with_mode(COLLAB_KV_STORE, IdbTransactionMode::Readwrite)?;
let store = store_from_transaction(&transaction)?;
let doc_id = self
.get_doc_id(&store, uid, object_id)
.await
.ok_or_else(|| {
PersistenceError::RecordNotFound(format!("doc_id for object_id:{} is not found", object_id))
})?;

self.delete_all_updates(&store, doc_id).await?;

// delete the doc state and state vector
let doc_state_key = make_doc_state_key(doc_id);
let sv_key = make_state_vector_key(doc_id);
store.delete(&to_js_key(doc_state_key.as_ref()))?;
store.delete(&to_js_key(sv_key.as_ref()))?;
transaction_result_to_result(transaction.await)?;
Ok(())
}

pub async fn flush_doc(
&self,
uid: i64,
object_id: &str,
encoded: &EncodedCollab,
) -> Result<(), PersistenceError> {
let read_guard = self.db.write().await;
let write_guard = self.db.write().await;
let transaction =
read_guard.transaction_on_one_with_mode(COLLAB_KV_STORE, IdbTransactionMode::Readwrite)?;
write_guard.transaction_on_one_with_mode(COLLAB_KV_STORE, IdbTransactionMode::Readwrite)?;
let store = store_from_transaction(&transaction)?;
let doc_id = self
.get_doc_id(&store, uid, object_id)
.await
.ok_or_else(|| {
PersistenceError::RecordNotFound(format!("doc_id for object_id:{} is not found", object_id))
})?;
self.delete_all_updates(&store, doc_id).await?;

let start = to_js_key(make_doc_start_key(doc_id));
let end = to_js_key(make_doc_end_key(doc_id));
let key_range = IdbKeyRange::bound(&start, &end).map_err(|err| {
PersistenceError::Internal(anyhow!("Get last update key fail. error: {:?}", err))
})?;

let cursor_request = store
.open_cursor_with_range(&key_range)?
.await?
.ok_or_else(|| {
PersistenceError::Internal(anyhow!("Open cursor fail. error: {:?}", "cursor is none"))
})?;

// Delete the first key
let _ = cursor_request.delete();
while cursor_request.continue_cursor()?.await? {
console::log_1(&JsValue::from_str("delete cursor"));
if let Err(err) = cursor_request.delete() {
error!("failed to delete cursor: {:?}", err)
}
}

// save the new doc state and state vector
let doc_state_key = make_doc_state_key(doc_id);
let sv_key = make_state_vector_key(doc_id);
self
Expand All @@ -242,7 +251,6 @@ impl CollabIndexeddb {
self
.set_data_with_store(&store, sv_key, &encoded.state_vector)
.await?;

transaction_result_to_result(transaction.await)?;
Ok(())
}
Expand All @@ -268,6 +276,35 @@ impl CollabIndexeddb {
Ok(())
}

async fn delete_all_updates(
&self,
store: &IdbObjectStore<'_>,
doc_id: DocID,
) -> Result<(), PersistenceError> {
let start = to_js_key(make_doc_start_key(doc_id));
let end = to_js_key(make_doc_end_key(doc_id));
let key_range = IdbKeyRange::bound(&start, &end).map_err(|err| {
PersistenceError::Internal(anyhow!("Get last update key fail. error: {:?}", err))
})?;

let cursor_request = store
.open_cursor_with_range(&key_range)?
.await?
.ok_or_else(|| {
PersistenceError::Internal(anyhow!("Open cursor fail. error: {:?}", "cursor is none"))
})?;

// Delete the first key
let _ = cursor_request.delete();
while cursor_request.continue_cursor()?.await? {
if let Err(err) = cursor_request.delete() {
error!("failed to delete cursor: {:?}", err)
}
}

Ok(())
}

pub async fn get_all_updates(
&self,
uid: i64,
Expand Down
7 changes: 5 additions & 2 deletions collab-plugins/src/local_storage/indexeddb/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
pub mod indexeddb_plugin;
pub mod kv_impl;
mod indexeddb_plugin;
mod kv_impl;

pub use indexeddb_plugin::*;
pub use kv_impl::*;
12 changes: 12 additions & 0 deletions collab-plugins/src/local_storage/rocksdb/kv_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::ops::RangeBounds;
use std::path::Path;
use std::sync::Arc;

use crate::local_storage::kv::doc::CollabKVAction;

use crate::local_storage::kv::{KVEntry, KVStore, KVTransactionDB, PersistenceError};
use rocksdb::Direction::Forward;
use rocksdb::{
Expand Down Expand Up @@ -109,6 +111,16 @@ impl KVTransactionDBRocksdbImpl {

Ok(Self { db: Arc::new(db) })
}

pub async fn is_exist(&self, uid: i64, object_id: &str) -> Result<bool, PersistenceError> {
let read_txn = self.read_txn();
Ok(read_txn.is_exist(uid, object_id))
}

pub async fn delete_doc(&self, uid: i64, doc_id: &str) -> Result<(), PersistenceError> {
self.with_write_txn(|txn| txn.delete_doc(uid, doc_id))?;
Ok(())
}
}

impl KVTransactionDB for KVTransactionDBRocksdbImpl {
Expand Down
4 changes: 2 additions & 2 deletions collab-plugins/tests/web/edit_collab_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use assert_json_diff::assert_json_eq;
use collab::core::collab::MutexCollab;
use collab::preclude::CollabBuilder;
use collab_entity::CollabType;
use collab_plugins::local_storage::indexeddb::indexeddb_plugin::IndexeddbDiskPlugin;
use collab_plugins::local_storage::indexeddb::kv_impl::CollabIndexeddb;
use collab_plugins::local_storage::indexeddb::CollabIndexeddb;
use collab_plugins::local_storage::indexeddb::IndexeddbDiskPlugin;
use js_sys::Promise;
use serde_json::json;
use std::sync::{Arc, Once};
Expand Down
2 changes: 1 addition & 1 deletion collab-plugins/tests/web/indexeddb_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use collab::core::collab_plugin::EncodedCollab;
use collab_plugins::local_storage::indexeddb::kv_impl::CollabIndexeddb;
use collab_plugins::local_storage::indexeddb::CollabIndexeddb;
use uuid::Uuid;
use wasm_bindgen_test::*;
use yrs::Doc;
Expand Down
3 changes: 2 additions & 1 deletion collab/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ bincode = "1.3.3"
serde_repr = "0.1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "0.3.67"}
web-sys = { version = "0.3"}
js-sys = "0.3"

[dev-dependencies]
tokio = { version = "1.26", features = ["rt"] }
Expand Down
4 changes: 2 additions & 2 deletions collab/src/core/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ if_wasm! {

impl Timer {
fn start() -> Self {
Self { start: web_sys::js_sys::Date::now() }
Self { start: js_sys::Date::now() }
}

fn elapsed(&self) -> Duration {
let now = web_sys::js_sys::Date::now();
let now = js_sys::Date::now();
let elapsed_ms = now - self.start;
Duration::from_millis(elapsed_ms as u64)
}
Expand Down

0 comments on commit 3eef93f

Please sign in to comment.