How can I use a principal
in a key of a StableBTreeMap
to filter the map using range
?
Assuming my keys do not contains only the principal, how can I provide a min and max value for the principal?
// Pseudo code
my_stable_btree_map.range(filter()).collect();
fn filter() -> impl RangeBounds<MyKey> {
let start = MyKey {
something: 123,
principal: ????????? // min
}
let end = MyKey {
something: 3456,
principal: ????????? // max
}
start..end
}
Shout-out to @frederikrothenberger for sharing the solution:
pub const PRINCIPAL_MIN: Principal = Principal::from_slice(&[]);
pub const PRINCIPAL_MAX: Principal = Principal::from_slice(&[255; 29]);
pub fn filter_satellites_analytics(
GetAnalytics {
from,
to,
satellite_id,
}: &GetAnalytics,
) -> impl RangeBounds<AnalyticSatelliteKey> {
let start_key = AnalyticSatelliteKey {
satellite_id: satellite_id.unwrap_or(PRINCIPAL_MIN),
collected_at: from.unwrap_or(u64::MIN),
key: "".to_string(),
};
let end_key = AnalyticSatelliteKey {
satellite_id: satellite_id.unwrap_or(PRINCIPAL_MAX),
collected_at: to.unwrap_or(u64::MAX),
key: "".to_string(),
};
start_key..end_key
}
2 Likes