Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/count #422

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions src/libs/satellite/src/db/state.rs
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ use crate::msg::COLLECTION_NOT_FOUND;
use crate::rules::types::rules::{Memory, Rule};
use crate::types::core::{CollectionKey, Key};
use std::collections::BTreeMap;
use std::ops::RangeBounds;

/// Collections
@@ -166,17 +167,7 @@ pub fn get_docs_stable(
collection: &CollectionKey,
db: &DbStable,
) -> Result<Vec<(StableKey, Doc)>, String> {
let start_key = StableKey {
collection: collection.clone(),
key: "".to_string(),
};

let end_key = StableKey {
collection: range_collection_end(collection).clone(),
key: "".to_string(),
};

let items = db.range(start_key..end_key).collect();
let items = db.range(filter_docs_range(collection)).collect();

Ok(items)
}
@@ -196,6 +187,35 @@ pub fn get_docs_heap<'a>(
}
}

pub fn count_docs_heap(collection: &CollectionKey, db: &DbHeap) -> Result<usize, String> {
let col = db.get(collection);

match col {
None => Err([COLLECTION_NOT_FOUND, collection].join("")),
Some(col) => Ok(col.len()),
}
}

pub fn count_docs_stable(collection: &CollectionKey, db: &DbStable) -> Result<usize, String> {
let length = db.range(filter_docs_range(collection)).count();

Ok(length)
}

fn filter_docs_range(collection: &CollectionKey) -> impl RangeBounds<StableKey> {
let start_key = StableKey {
collection: collection.clone(),
key: "".to_string(),
};

let end_key = StableKey {
collection: range_collection_end(collection).clone(),
key: "".to_string(),
};

start_key..end_key
}

// Insert

fn insert_doc_stable(
16 changes: 8 additions & 8 deletions src/libs/satellite/src/db/store.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::assert::assert_description_length;
use crate::controllers::store::get_controllers;
use crate::db::state::{
delete_collection as delete_state_collection, delete_doc as delete_state_doc,
get_doc as get_state_doc, get_docs_heap, get_docs_stable, get_rule as get_state_rule,
init_collection as init_state_collection, insert_doc as insert_state_doc,
is_collection_empty as is_state_collection_empty,
count_docs_heap, count_docs_stable, delete_collection as delete_state_collection,
delete_doc as delete_state_doc, get_doc as get_state_doc, get_docs_heap, get_docs_stable,
get_rule as get_state_rule, init_collection as init_state_collection,
insert_doc as insert_state_doc, is_collection_empty as is_state_collection_empty,
};
use crate::db::types::interface::{DelDoc, SetDoc};
use crate::db::types::state::{Doc, DocContext, DocUpsert};
@@ -350,12 +350,12 @@ pub fn count_docs_store(collection: &CollectionKey) -> Result<usize, String> {
match rule.mem() {
Memory::Heap => STATE.with(|state| {
let state_ref = state.borrow();
let docs = get_docs_heap(collection, &state_ref.heap.db.db)?;
Ok(docs.len())
let length = count_docs_heap(collection, &state_ref.heap.db.db)?;
Ok(length)
}),
Memory::Stable => STATE.with(|state| {
let stable = get_docs_stable(collection, &state.borrow().stable.db)?;
Ok(stable.len())
let length = count_docs_stable(collection, &state.borrow().stable.db)?;
Ok(length)
}),
}
}
4 changes: 3 additions & 1 deletion src/libs/satellite/src/lib.rs
Original file line number Diff line number Diff line change
@@ -40,7 +40,9 @@ pub use crate::db::store::{
};
pub use crate::db::types::interface::{DelDoc, SetDoc};
pub use crate::db::types::state::Doc;
pub use crate::storage::store::{count_assets_store, delete_asset_store, get_content_chunks_store, get_asset_store};
pub use crate::storage::store::{
count_assets_store, delete_asset_store, get_asset_store, get_content_chunks_store,
};
use crate::storage::types::state::FullPath;
pub use crate::types::core::{Blob, CollectionKey, Key};
pub use crate::types::hooks::{
43 changes: 35 additions & 8 deletions src/libs/satellite/src/storage/state.rs
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ use crate::types::core::{Blob, CollectionKey};
use crate::types::state::StableState;
use shared::serializers::{deserialize_from_bytes, serialize_to_bytes};
use std::borrow::Cow;
use std::ops::RangeBounds;

/// Assets
@@ -243,6 +244,17 @@ pub fn get_assets_stable(
collection: &CollectionKey,
assets: &AssetsStable,
) -> Vec<(StableKey, Asset)> {
assets.range(filter_assets_range(collection)).collect()
}

pub fn count_assets_stable(
collection: &CollectionKey,
assets: &AssetsStable,
) -> usize {
assets.range(filter_assets_range(collection)).count()
}

fn filter_assets_range(collection: &CollectionKey) -> impl RangeBounds<StableKey> {
let start_key = StableKey {
collection: collection.clone(),
full_path: "".to_string(),
@@ -253,7 +265,7 @@ pub fn get_assets_stable(
full_path: "".to_string(),
};

assets.range(start_key..end_key).collect()
start_key..end_key
}

pub fn get_assets_heap<'a>(
@@ -262,16 +274,31 @@ pub fn get_assets_heap<'a>(
) -> Vec<(&'a FullPath, &'a Asset)> {
assets
.iter()
.filter_map(|(_, asset)| {
if &asset.key.collection == collection {
Some((&asset.key.full_path, asset))
} else {
None
}
})
.filter_map(|(_, asset)| filter_assets_heap(asset, collection))
.collect()
}

pub fn count_assets_heap(
collection: &CollectionKey,
assets: &AssetsHeap,
) -> usize {
assets
.iter()
.filter_map(|(_, asset)| filter_assets_heap(asset, collection))
.count()
}

fn filter_assets_heap<'a>(
asset: &'a Asset,
collection: &CollectionKey,
) -> Option<(&'a FullPath, &'a Asset)> {
if &asset.key.collection == collection {
Some((&asset.key.full_path, asset))
} else {
None
}
}

fn stable_full_path(collection: &CollectionKey, full_path: &FullPath) -> StableKey {
StableKey {
collection: collection.clone(),
18 changes: 5 additions & 13 deletions src/libs/satellite/src/storage/store.rs
Original file line number Diff line number Diff line change
@@ -30,15 +30,7 @@ use crate::storage::runtime::{
insert_batch as insert_runtime_batch, insert_chunk as insert_runtime_chunk,
update_certified_asset as update_runtime_certified_asset,
};
use crate::storage::state::{
delete_asset as delete_state_asset, delete_domain as delete_state_domain,
get_asset as get_state_asset, get_assets_heap, get_assets_stable,
get_config as get_state_config, get_content_chunks as get_state_content_chunks,
get_domain as get_state_domain, get_domains as get_state_domains,
get_public_asset as get_state_public_asset, get_rule as get_state_rule, get_rule,
insert_asset as insert_state_asset, insert_asset_encoding as insert_state_asset_encoding,
insert_config as insert_state_config, insert_domain as insert_state_domain,
};
use crate::storage::state::{count_assets_heap, count_assets_stable, delete_asset as delete_state_asset, delete_domain as delete_state_domain, get_asset as get_state_asset, get_assets_heap, get_assets_stable, get_config as get_state_config, get_content_chunks as get_state_content_chunks, get_domain as get_state_domain, get_domains as get_state_domains, get_public_asset as get_state_public_asset, get_rule as get_state_rule, get_rule, insert_asset as insert_state_asset, insert_asset_encoding as insert_state_asset_encoding, insert_config as insert_state_config, insert_domain as insert_state_domain};
use crate::storage::types::config::StorageConfig;
use crate::storage::types::domain::{CustomDomain, CustomDomains, DomainName};
use crate::storage::types::interface::{AssetNoContent, CommitBatch, InitAssetKey, UploadChunk};
@@ -356,12 +348,12 @@ pub fn count_assets_store(collection: &CollectionKey) -> Result<usize, String> {
match rule.mem() {
Memory::Heap => STATE.with(|state| {
let state_ref = state.borrow();
let assets = get_assets_heap(collection, &state_ref.heap.storage.assets);
Ok(assets.len())
let length = count_assets_heap(collection, &state_ref.heap.storage.assets);
Ok(length)
}),
Memory::Stable => STATE.with(|state| {
let stable = get_assets_stable(collection, &state.borrow().stable.assets);
Ok(stable.len())
let length = count_assets_stable(collection, &state.borrow().stable.assets);
Ok(length)
}),
}
}