chore: write collab to disk if it's not exist (#6023)

* chore: write collab to disk if it's not exist

* chore: write collab if it's not exit

* chore: fix test
This commit is contained in:
Nathan.fooo 2024-08-21 14:04:53 +08:00 committed by GitHub
parent b9a34f6fc2
commit 0ce43ca5fa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 151 additions and 91 deletions

View File

@ -963,7 +963,7 @@ dependencies = [
[[package]]
name = "collab"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"arc-swap",
@ -988,7 +988,7 @@ dependencies = [
[[package]]
name = "collab-database"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"async-trait",
@ -1018,7 +1018,7 @@ dependencies = [
[[package]]
name = "collab-document"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"arc-swap",
@ -1038,7 +1038,7 @@ dependencies = [
[[package]]
name = "collab-entity"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"bytes",
@ -1057,7 +1057,7 @@ dependencies = [
[[package]]
name = "collab-folder"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"arc-swap",
@ -1100,7 +1100,7 @@ dependencies = [
[[package]]
name = "collab-plugins"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"async-stream",
@ -1180,7 +1180,7 @@ dependencies = [
[[package]]
name = "collab-user"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"collab",
@ -2429,6 +2429,7 @@ dependencies = [
"chrono",
"client-api",
"collab",
"collab-database",
"collab-document",
"collab-entity",
"collab-folder",

View File

@ -116,13 +116,13 @@ custom-protocol = ["tauri/custom-protocol"]
# To switch to the local path, run:
# scripts/tool/update_collab_source.sh
# ⚠️⚠️⚠️️
collab = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-entity = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-folder = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-document = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-database = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-plugins = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-user = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-entity = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-folder = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-document = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-database = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-plugins = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-user = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
# Working directory: frontend
# To update the commit ID, run:

View File

@ -946,7 +946,7 @@ dependencies = [
[[package]]
name = "collab"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"arc-swap",
@ -971,7 +971,7 @@ dependencies = [
[[package]]
name = "collab-database"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"async-trait",
@ -1001,7 +1001,7 @@ dependencies = [
[[package]]
name = "collab-document"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"arc-swap",
@ -1021,7 +1021,7 @@ dependencies = [
[[package]]
name = "collab-entity"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"bytes",
@ -1040,7 +1040,7 @@ dependencies = [
[[package]]
name = "collab-folder"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"arc-swap",
@ -1083,7 +1083,7 @@ dependencies = [
[[package]]
name = "collab-plugins"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"async-stream",
@ -1163,7 +1163,7 @@ dependencies = [
[[package]]
name = "collab-user"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"collab",
@ -2459,6 +2459,7 @@ dependencies = [
"chrono",
"client-api",
"collab",
"collab-database",
"collab-document",
"collab-entity",
"collab-folder",

View File

@ -116,13 +116,13 @@ custom-protocol = ["tauri/custom-protocol"]
# To switch to the local path, run:
# scripts/tool/update_collab_source.sh
# ⚠️⚠️⚠️️
collab = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-entity = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-folder = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-document = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-database = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-plugins = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-user = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-entity = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-folder = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-document = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-database = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-plugins = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-user = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
# Working directory: frontend
# To update the commit ID, run:

View File

@ -824,7 +824,7 @@ dependencies = [
[[package]]
name = "collab"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"arc-swap",
@ -849,7 +849,7 @@ dependencies = [
[[package]]
name = "collab-database"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"async-trait",
@ -879,7 +879,7 @@ dependencies = [
[[package]]
name = "collab-document"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"arc-swap",
@ -899,7 +899,7 @@ dependencies = [
[[package]]
name = "collab-entity"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"bytes",
@ -918,7 +918,7 @@ dependencies = [
[[package]]
name = "collab-folder"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"arc-swap",
@ -961,7 +961,7 @@ dependencies = [
[[package]]
name = "collab-plugins"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"async-stream",
@ -1041,7 +1041,7 @@ dependencies = [
[[package]]
name = "collab-user"
version = "0.2.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=2cc23e05010a7ed72e5c63c59dfa3db7d789620c#2cc23e05010a7ed72e5c63c59dfa3db7d789620c"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=6a5e7e49c159fcf782df84208cdb26c212c28ede#6a5e7e49c159fcf782df84208cdb26c212c28ede"
dependencies = [
"anyhow",
"collab",
@ -2260,6 +2260,7 @@ dependencies = [
"chrono",
"client-api",
"collab",
"collab-database",
"collab-document",
"collab-entity",
"collab-folder",

View File

@ -136,13 +136,13 @@ rocksdb = { git = "https://github.com/rust-rocksdb/rust-rocksdb", rev = "1710120
# To switch to the local path, run:
# scripts/tool/update_collab_source.sh
# ⚠️⚠️⚠️️
collab = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-entity = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-folder = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-document = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-database = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-plugins = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab-user = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "2cc23e05010a7ed72e5c63c59dfa3db7d789620c" }
collab = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-entity = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-folder = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-document = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-database = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-plugins = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
collab-user = { version = "0.2", git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "6a5e7e49c159fcf782df84208cdb26c212c28ede" }
# Working directory: frontend
# To update the commit ID, run:

View File

@ -15,6 +15,7 @@ use collab_entity::{CollabObject, CollabType};
use collab_folder::{Folder, FolderData, FolderNotify};
use collab_plugins::connect_state::{CollabConnectReachability, CollabConnectState};
use collab_plugins::local_storage::kv::snapshot::SnapshotPersistence;
if_native! {
use collab_plugins::local_storage::rocksdb::rocksdb_plugin::{RocksdbBackup, RocksdbDiskPlugin};
}
@ -155,9 +156,19 @@ impl AppFlowyCollabBuilder {
builder_config: CollabBuilderConfig,
data: Option<DocumentData>,
) -> Result<Arc<RwLock<Document>>, Error> {
assert_eq!(object.collab_type, CollabType::Document);
let expected_collab_type = CollabType::Document;
assert_eq!(object.collab_type, expected_collab_type);
let collab = self.build_collab(&object, &collab_db, data_source)?;
let document = Document::open_with(collab, data)?;
self.flush_collab_if_not_exist(
object.uid,
&object.object_id,
collab_db.clone(),
&expected_collab_type,
&document,
)?;
let document = Arc::new(RwLock::new(document));
self.finalize(object, builder_config, document)
}
@ -176,9 +187,19 @@ impl AppFlowyCollabBuilder {
folder_notifier: Option<FolderNotify>,
folder_data: Option<FolderData>,
) -> Result<Arc<RwLock<Folder>>, Error> {
assert_eq!(object.collab_type, CollabType::Folder);
let expected_collab_type = CollabType::Folder;
assert_eq!(object.collab_type, expected_collab_type);
let collab = self.build_collab(&object, &collab_db, doc_state)?;
let folder = Folder::open_with(object.uid, collab, folder_notifier, folder_data);
self.flush_collab_if_not_exist(
object.uid,
&object.object_id,
collab_db.clone(),
&expected_collab_type,
&folder,
)?;
let folder = Arc::new(RwLock::new(folder));
self.finalize(object, builder_config, folder)
}
@ -196,9 +217,19 @@ impl AppFlowyCollabBuilder {
builder_config: CollabBuilderConfig,
notifier: Option<UserAwarenessNotifier>,
) -> Result<Arc<RwLock<UserAwareness>>, Error> {
assert_eq!(object.collab_type, CollabType::UserAwareness);
let expected_collab_type = CollabType::UserAwareness;
assert_eq!(object.collab_type, expected_collab_type);
let collab = self.build_collab(&object, &collab_db, doc_state)?;
let user_awareness = UserAwareness::open(collab, notifier);
self.flush_collab_if_not_exist(
object.uid,
&object.object_id,
collab_db.clone(),
&expected_collab_type,
&user_awareness,
)?;
let user_awareness = Arc::new(RwLock::new(user_awareness));
self.finalize(object, builder_config, user_awareness)
}
@ -216,9 +247,19 @@ impl AppFlowyCollabBuilder {
builder_config: CollabBuilderConfig,
collab_service: impl DatabaseCollabService,
) -> Result<Arc<RwLock<WorkspaceDatabase>>, Error> {
assert_eq!(object.collab_type, CollabType::WorkspaceDatabase);
let expected_collab_type = CollabType::WorkspaceDatabase;
assert_eq!(object.collab_type, expected_collab_type);
let collab = self.build_collab(&object, &collab_db, doc_state)?;
let workspace = WorkspaceDatabase::open(object.uid, collab, collab_db.clone(), collab_service);
self.flush_collab_if_not_exist(
object.uid,
&object.object_id,
collab_db.clone(),
&expected_collab_type,
&workspace,
)?;
let workspace = Arc::new(RwLock::new(workspace));
self.finalize(object, builder_config, workspace)
}
@ -290,6 +331,39 @@ impl AppFlowyCollabBuilder {
drop(write_collab);
Ok(collab)
}
/// Remove all updates in disk and write the final state vector to disk.
pub fn flush_collab_if_not_exist<T>(
&self,
uid: i64,
object_id: &str,
collab_db: Weak<CollabKVDB>,
collab_type: &CollabType,
collab: &T,
) -> Result<(), Error>
where
T: BorrowMut<Collab> + Send + Sync + 'static,
{
if let Some(collab_db) = collab_db.upgrade() {
let write_txn = collab_db.write_txn();
let is_not_exist_on_disk = !write_txn.is_exist(uid, object_id);
if is_not_exist_on_disk {
trace!("flush collab:{}-{} to disk", collab_type, object_id);
let collab: &Collab = collab.borrow();
let encode_collab =
collab.encode_collab_v1(|collab| collab_type.validate_require_data(collab))?;
write_txn.flush_doc(
uid,
object_id,
encode_collab.state_vector.to_vec(),
encode_collab.doc_state.to_vec(),
)?;
}
}
Ok(())
}
}
pub struct CollabBuilderConfig {

View File

@ -135,6 +135,9 @@ async fn collab_db_backup_test() {
EventIntegrationTest::new_with_user_data_path(user_db_path, DEFAULT_NAME.to_string()).await;
let uid = test.get_user_profile().await.unwrap().id;
// sleep a bit to make sure the backup is generated
tokio::time::sleep(Duration::from_secs(10)).await;
let backups = test.user_manager.get_collab_backup_list(uid);
assert_eq!(backups.len(), 1);

View File

@ -109,10 +109,11 @@ impl DatabaseManager {
let workspace_id = self.user.workspace_id()?;
let workspace_database_object_id = self.user.workspace_database_object_id()?;
let mut workspace_database_doc_state =
let mut workspace_database_data_source =
KVDBCollabPersistenceImpl::new(collab_db.clone(), uid).into_data_source();
// If the workspace database not exist in disk, try to fetch from remote.
if !self.is_collab_exist(uid, &collab_db, &workspace_database_object_id) {
let is_exist_in_disk = self.is_collab_exist(uid, &collab_db, &workspace_database_object_id);
// 4.If the workspace database not exist in disk, try to fetch from remote.
if !is_exist_in_disk {
trace!("workspace database not exist, try to fetch from remote");
match self
.cloud_service
@ -125,7 +126,7 @@ impl DatabaseManager {
{
Ok(value) => {
if let Some(encode_collab) = value {
workspace_database_doc_state = DataSource::from(encode_collab);
workspace_database_data_source = DataSource::from(encode_collab);
}
},
Err(err) => {
@ -140,7 +141,7 @@ impl DatabaseManager {
// Construct the workspace database.
event!(
tracing::Level::INFO,
"open aggregate database views object: {}",
"create workspace database object: {}",
&workspace_database_object_id
);
@ -154,9 +155,10 @@ impl DatabaseManager {
&workspace_database_object_id,
CollabType::WorkspaceDatabase,
)?;
let workspace_database = self.collab_builder.create_workspace_database(
collab_object,
workspace_database_doc_state,
workspace_database_data_source,
collab_db,
CollabBuilderConfig::default().sync_enable(true),
collab_builder,

View File

@ -310,8 +310,8 @@ impl DocumentManager {
// clear the awareness state when close the document
let mut lock = document.write().await;
lock.clean_awareness_local_state();
lock.flush();
}
let clone_doc_id = doc_id.clone();
trace!("move document to removing_documents: {}", doc_id);
self.removing_documents.insert(doc_id, document);

View File

@ -31,6 +31,7 @@ collab-plugins = { workspace = true }
collab-document = { workspace = true }
collab-entity = { workspace = true }
collab-folder = { workspace = true }
collab-database = { workspace = true }
hex = "0.4.3"
postgrest = "1.0"
lib-infra = { workspace = true }

View File

@ -1,13 +1,7 @@
use anyhow::Error;
use collab::core::transaction::DocTransactionExtension;
use collab::entity::EncodedCollab;
use collab::preclude::Collab;
use collab_entity::define::{DATABASE, DATABASE_ROW_DATA, WORKSPACE_DATABASES};
use collab_entity::CollabType;
use yrs::{ArrayPrelim, Map, MapPrelim};
use flowy_database_pub::cloud::{DatabaseCloudService, DatabaseSnapshot, EncodeCollabByOid};
use lib_infra::async_trait::async_trait;
pub(crate) struct LocalServerDatabaseCloudServiceImpl();
@ -16,33 +10,11 @@ pub(crate) struct LocalServerDatabaseCloudServiceImpl();
impl DatabaseCloudService for LocalServerDatabaseCloudServiceImpl {
async fn get_database_encode_collab(
&self,
object_id: &str,
collab_type: CollabType,
_object_id: &str,
_collab_type: CollabType,
_workspace_id: &str,
) -> Result<Option<EncodedCollab>, Error> {
let object_id = object_id.to_string();
// create the minimal required data for the given collab type
let mut collab = Collab::new(1, object_id, collab_type.clone(), vec![], false);
let mut txn = collab.context.transact_mut();
match collab_type {
CollabType::Database => {
collab.data.insert(&mut txn, DATABASE, MapPrelim::default());
},
CollabType::WorkspaceDatabase => {
collab
.data
.insert(&mut txn, WORKSPACE_DATABASES, ArrayPrelim::default());
},
CollabType::DatabaseRow => {
collab
.data
.insert(&mut txn, DATABASE_ROW_DATA, MapPrelim::default());
},
_ => { /* do nothing */ },
};
Ok(Some(txn.get_encoded_collab_v1()))
Ok(None)
}
async fn batch_get_database_encode_collab(

View File

@ -89,7 +89,12 @@ where
let collab = Collab::new_with_origin(origin.clone(), &view.id, vec![], false);
let document = Document::open_with(collab, Some(default_document_data(&view.id)))?;
let encode = document.encode_collab_v1(|_| Ok::<(), PersistenceError>(()))?;
write_txn.flush_doc_with(user_id, &view.id, &encode.doc_state, &encode.state_vector)?;
write_txn.flush_doc(
user_id,
&view.id,
encode.state_vector.to_vec(),
encode.doc_state.to_vec(),
)?;
event!(
tracing::Level::INFO,
"Did migrate empty document {}",

View File

@ -55,11 +55,11 @@ impl UserDataMigration for FavoriteV1AndWorkspaceArrayMigration {
let encode = folder
.encode_collab()
.map_err(|err| PersistenceError::Internal(err.into()))?;
write_txn.flush_doc_with(
write_txn.flush_doc(
session.user_id,
&session.user_workspace.id,
&encode.doc_state,
&encode.state_vector,
encode.state_vector.to_vec(),
encode.doc_state.to_vec(),
)?;
}
Ok(())

View File

@ -49,11 +49,11 @@ impl UserDataMigration for WorkspaceTrashMapToSectionMigration {
let encode = folder
.encode_collab()
.map_err(|err| PersistenceError::Internal(err.into()))?;
write_txn.flush_doc_with(
write_txn.flush_doc(
session.user_id,
&session.user_workspace.id,
&encode.doc_state,
&encode.state_vector,
encode.state_vector.to_vec(),
encode.doc_state.to_vec(),
)?;
}
Ok(())