Skip to content

Commit

Permalink
feat(storage): persist object count in bucket state
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Mar 3, 2023
1 parent 05e07ec commit e233791
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
7 changes: 2 additions & 5 deletions contracts/cw-storage/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,9 @@ pub mod execute {
pin: bool,
) -> Result<Response, ContractError> {
let size = data.len() as u128;
// TODO: store object count in bucket instead of computing it?
let object_count = objects()
.keys_raw(deps.storage, None, None, Order::Ascending)
.count();
BUCKET.update(deps.storage, |mut bucket| -> Result<_, ContractError> {
bucket.size += size;
bucket.object_count += 1;
match bucket.limits {
Limits {
max_object_size: Some(max),
Expand All @@ -69,7 +66,7 @@ pub mod execute {
Limits {
max_objects: Some(max),
..
} if object_count as u128 >= max.u128() => {
} if bucket.object_count > max.u128() => {
Err(BucketError::MaxObjectsLimitExceeded.into())
}
Limits {
Expand Down
3 changes: 3 additions & 0 deletions contracts/cw-storage/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub struct Bucket {
pub limits: Limits,
/// The total size of the objects contained in the bucket.
pub size: u128,
/// The number of objects in the bucket.
pub object_count: u128,
}

impl Bucket {
Expand All @@ -29,6 +31,7 @@ impl Bucket {
name: n,
limits,
size: 0u128,
object_count: 0u128,
})
}
}
Expand Down

0 comments on commit e233791

Please sign in to comment.