junobuild_satellite

Function count_assets_store

Source
pub fn count_assets_store(
    caller: Principal,
    collection: &CollectionKey,
    filters: &ListParams,
) -> Result<usize, String>
Expand description

Count assets in a collection.

This function retrieves the count of assets from a collection’s store based on the specified parameters. It returns a Result<usize, String> where Ok(usize) contains the count of assets matching the filter criteria, or an error message as Err(String) if the operation encounters issues.

§Parameters

  • caller: The Principal representing the caller initiating the operation. If used in serverless functions, you can use ic_cdk::id() to pass an administrator controller.
  • collection: A reference to the CollectionKey representing the collection from which to count the assets.
  • filters: A reference to ListParams containing the filter criteria for counting the assets.

§Returns

  • Ok(usize): Contains the count of assets matching the filter criteria.
  • Err(String): An error message if the operation fails.

This function counts assets in a Juno collection’s store by listing them and then determining the length of the result set.

§Note

This implementation can be improved, as it currently relies on list_assets_store underneath, meaning that all assets matching the filter criteria are still read from the store. This might lead to unnecessary overhead, especially for large collections. Optimizing this function to count assets directly without retrieving them could enhance performance.