-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
PrincipalToUserIdMap
backed by stable memory (#7017)
- Loading branch information
Showing
8 changed files
with
135 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "principal_to_user_id_map" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
ic_principal = { workspace = true } | ||
stable_memory_map = { path = "../stable_memory_map" } | ||
types = { path = "../types" } | ||
serde = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use ic_principal::Principal; | ||
use serde::{Deserialize, Serialize}; | ||
use stable_memory_map::{with_map, with_map_mut, KeyPrefix, PrincipalToUserIdKeyPrefix}; | ||
use types::UserId; | ||
|
||
#[derive(Serialize, Deserialize, Default)] | ||
pub struct PrincipalToUserIdMap { | ||
prefix: PrincipalToUserIdKeyPrefix, | ||
count: u32, | ||
} | ||
|
||
impl PrincipalToUserIdMap { | ||
pub fn get(&self, principal: &Principal) -> Option<UserId> { | ||
with_map(|m| m.get(self.prefix.create_key(principal)).map(bytes_to_user_id)) | ||
} | ||
|
||
pub fn insert(&mut self, principal: Principal, user_id: UserId) { | ||
if with_map_mut(|m| m.insert(self.prefix.create_key(&principal), user_id.as_slice().to_vec())).is_none() { | ||
self.count += 1; | ||
} | ||
} | ||
|
||
pub fn remove(&mut self, principal: &Principal) -> Option<UserId> { | ||
let bytes = with_map_mut(|m| m.remove(self.prefix.create_key(principal)))?; | ||
self.count = self.count.saturating_sub(1); | ||
Some(bytes_to_user_id(bytes)) | ||
} | ||
|
||
pub fn len(&self) -> u32 { | ||
self.count | ||
} | ||
|
||
pub fn is_empty(&self) -> bool { | ||
self.count == 0 | ||
} | ||
} | ||
|
||
fn bytes_to_user_id(bytes: Vec<u8>) -> UserId { | ||
UserId::from(Principal::from_slice(&bytes)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
backend/libraries/stable_memory_map/src/keys/principal_to_user_id.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use crate::keys::macros::key; | ||
use crate::{BaseKey, KeyPrefix, KeyType}; | ||
use ic_principal::Principal; | ||
|
||
key!(PrincipalToUserIdKey, PrincipalToUserIdKeyPrefix, KeyType::PrincipalToUserId); | ||
|
||
impl PrincipalToUserIdKeyPrefix { | ||
pub fn new() -> Self { | ||
// KeyType::PrincipalToUserId 1 byte | ||
PrincipalToUserIdKeyPrefix(vec![KeyType::PrincipalToUserId as u8]) | ||
} | ||
} | ||
|
||
impl KeyPrefix for PrincipalToUserIdKeyPrefix { | ||
type Key = PrincipalToUserIdKey; | ||
type Suffix = Principal; | ||
|
||
fn create_key(&self, principal: &Principal) -> PrincipalToUserIdKey { | ||
let principal_bytes = principal.as_slice(); | ||
let mut bytes = Vec::with_capacity(principal_bytes.len() + 1); | ||
bytes.push(KeyType::PrincipalToUserId as u8); | ||
bytes.extend_from_slice(principal_bytes); | ||
PrincipalToUserIdKey(bytes) | ||
} | ||
} | ||
|
||
impl Default for PrincipalToUserIdKeyPrefix { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl PrincipalToUserIdKey { | ||
pub fn principal(&self) -> Principal { | ||
Principal::from_slice(&self.0[1..]) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::Key; | ||
use rand::{thread_rng, RngCore}; | ||
|
||
#[test] | ||
fn principal_to_user_id_key_e2e() { | ||
for _ in 0..100 { | ||
let prefix = PrincipalToUserIdKeyPrefix::new(); | ||
let principal = Principal::from_slice(&thread_rng().next_u32().to_be_bytes()); | ||
let key = BaseKey::from(prefix.create_key(&principal)); | ||
let principal_to_user_id_key = PrincipalToUserIdKey::try_from(key.clone()).unwrap(); | ||
|
||
assert_eq!(*principal_to_user_id_key.0.first().unwrap(), KeyType::PrincipalToUserId as u8); | ||
assert_eq!(principal_to_user_id_key.0.len(), principal.as_slice().len() + 1); | ||
assert!(principal_to_user_id_key.matches_prefix(&prefix)); | ||
assert_eq!(principal_to_user_id_key.principal(), principal); | ||
|
||
let serialized = msgpack::serialize_then_unwrap(&principal_to_user_id_key); | ||
assert_eq!(serialized.len(), principal_to_user_id_key.0.len() + 2); | ||
let deserialized: PrincipalToUserIdKey = msgpack::deserialize_then_unwrap(&serialized); | ||
assert_eq!(deserialized, principal_to_user_id_key); | ||
assert_eq!(deserialized.0, key.0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters