Skip to content

Commit

Permalink
Impl list manifest on web platform
Browse files Browse the repository at this point in the history
  • Loading branch information
FirelightFlagboy committed Jul 31, 2024
1 parent 1a335c2 commit 98ff323
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion libparsec/crates/platform_storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ libparsec_testbed = { workspace = true, optional = true }
log = { workspace = true }
paste = { workspace = true }
lazy_static = { workspace = true, optional = true }
thiserror = { workspace = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { workspace = true, features = ["fs", "sync"] }
Expand All @@ -32,8 +33,9 @@ libsqlite3-sys = { workspace = true, features = ["bundled"] }
sqlx = { workspace = true, features = ["sqlite", "runtime-tokio", "macros"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
indexed_db_futures = { workspace = true, features = ["indices"] }
indexed_db_futures = { workspace = true, features = ["indices", "cursors"] }
js-sys = { workspace = true }
web-sys = { workspace = true, features = ["IdbKeyRange"] }
serde = { workspace = true }
serde-wasm-bindgen = { workspace = true }

Expand Down
4 changes: 2 additions & 2 deletions libparsec/crates/platform_storage/src/native/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl PlatformWorkspaceStorage {
page: u32,
limit: u32,
) -> anyhow::Result<Vec<RawEncryptedManifest>> {
db_list_manifest(&mut self.conn, page, limit).await
db_list_manifests(&mut self.conn, page, limit).await
}

pub async fn get_chunk(&mut self, chunk_id: ChunkID) -> anyhow::Result<Option<Vec<u8>>> {
Expand Down Expand Up @@ -650,7 +650,7 @@ async fn db_get_manifest(
}
}

async fn db_list_manifest(
async fn db_list_manifests(
executor: impl sqlx::Executor<'_, Database = sqlx::Sqlite>,
page: u32,
limit: u32,
Expand Down
53 changes: 53 additions & 0 deletions libparsec/crates/platform_storage/src/web/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,59 @@ where
.map_err(|e| anyhow::anyhow!("{e:?}"))
}

pub(super) async fn list<V>(
tx: &IdbTransaction<'_>,
store: &str,
page: u32,
limit: u32,
) -> anyhow::Result<Vec<V>>
where
V: DeserializeOwned,
{
use js_sys::Number;
use web_sys::IdbKeyRange;

// Index start at 1
let start = page * limit + 1;
let end = start + limit;

let range = IdbKeyRange::bound_with_lower_open_and_upper_open(
&Number::from(start),
&Number::from(end),
false,
true,
)
.map_err(|e| anyhow::anyhow!("{e:?}"))?;

let store = tx
.object_store(store)
.map_err(|e| anyhow::anyhow!("{e:?}"))?;

let Some(cursor) = store
.open_cursor_with_range_owned(range)
.map_err(|e| anyhow::anyhow!("{e:?}"))?
.await
.map_err(|e| anyhow::anyhow!("{e:?}"))?
else {
return Ok(Vec::new());
};

cursor
.into_vec(0)
.await
.map_err(|e| anyhow::anyhow!("{e:?}"))
.and_then(|v: Vec<_>| {
v.into_iter()
.map(|key_val|
// TODO: Sad that KeyVal does not provide it's internal value without a reference.
// That reference force us to clone the value, which is not optimal.
// Could be improved if https://github.com/Alorel/rust-indexed-db/issues/39 is fixed
serde_wasm_bindgen::from_value(key_val.value().clone())
.map_err(|e| anyhow::anyhow!("{e:?}")))
.collect::<anyhow::Result<Vec<V>>>()
})
}

pub(super) async fn count(
tx: &IdbTransaction<'_>,
store: &str,
Expand Down
9 changes: 9 additions & 0 deletions libparsec/crates/platform_storage/src/web/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,15 @@ impl Vlob {
super::db::get_all(&tx, Self::STORE).await
}

pub(super) async fn list(
conn: &IdbDatabase,
page: u32,
limit: u32,
) -> anyhow::Result<Vec<Self>> {
let tx = Self::read(conn)?;
super::db::list(&tx, Self::STORE, page, limit).await
}

pub(super) async fn remove(tx: &IdbTransaction<'_>, vlob_id: &Bytes) -> anyhow::Result<()> {
let vlob_id =
serde_wasm_bindgen::to_value(vlob_id).map_err(|e| anyhow::anyhow!("{e:?}"))?;
Expand Down
17 changes: 15 additions & 2 deletions libparsec/crates/platform_storage/src/web/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
model::{RealmCheckpoint, Vlob},
DB_VERSION,
},
workspace::{PopulateManifestOutcome, UpdateManifestData},
workspace::{PopulateManifestOutcome, RawEncryptedManifest, UpdateManifestData},

Check failure on line 19 in libparsec/crates/platform_storage/src/web/workspace.rs

View workflow job for this annotation

GitHub Actions / web / 🌐 Web tests

unresolved import `crate::workspace::UpdateManifestData`
};

use super::{db, model::PreventSyncPattern};
Expand Down Expand Up @@ -130,7 +130,10 @@ impl PlatformWorkspaceStorage {
.collect::<Result<Vec<_>, _>>()
}

pub async fn get_manifest(&mut self, entry_id: VlobID) -> anyhow::Result<Option<Vec<u8>>> {
pub async fn get_manifest(
&mut self,
entry_id: VlobID,
) -> anyhow::Result<Option<RawEncryptedManifest>> {
let transaction = Vlob::read(&self.conn)?;

Ok(
Expand All @@ -140,6 +143,16 @@ impl PlatformWorkspaceStorage {
)
}

pub async fn list_manifests(
&mut self,
page: u32,
limit: u32,
) -> anyhow::Result<Vec<RawEncryptedManifest>> {
Vlob::list(&self.conn, page, limit)
.await
.map(|vlobs| vlobs.into_iter().map(|vlob| vlob.blob.to_vec()).collect())
}

pub async fn get_chunk(&mut self, chunk_id: ChunkID) -> anyhow::Result<Option<Vec<u8>>> {
let transaction = super::model::Chunk::read(&self.conn)?;
db_get_chunk(&transaction, chunk_id).await
Expand Down

0 comments on commit 98ff323

Please sign in to comment.