use std::collections::BTreeMap;
use crate::Result;
use crate::crypto::{CipherId, MasterKey};
use crate::errors::PagedbError;
pub(crate) struct EpochKeyring {
keys: parking_lot::RwLock<BTreeMap<(u64, u8), MasterKey>>,
}
impl EpochKeyring {
pub(crate) fn new(epoch: u64, cipher_id: CipherId, mk: MasterKey) -> Self {
let mut keys = BTreeMap::new();
keys.insert((epoch, cipher_id.as_byte()), mk);
Self {
keys: parking_lot::RwLock::new(keys),
}
}
pub(crate) fn install(&self, epoch: u64, cipher_id: CipherId, mk: MasterKey) {
self.keys.write().insert((epoch, cipher_id.as_byte()), mk);
}
pub(crate) fn lease(&self, epoch: u64, cipher_id: CipherId) -> Result<MasterKey> {
self.keys
.read()
.get(&(epoch, cipher_id.as_byte()))
.cloned()
.ok_or(PagedbError::MissingPersistedKey {
mk_epoch: epoch,
cipher_id: cipher_id.as_byte(),
})
}
pub(crate) fn remove(&self, epoch: u64, cipher_id: CipherId) {
let _ = self.keys.write().remove(&(epoch, cipher_id.as_byte()));
}
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn duplicate(&self) -> Self {
Self {
keys: parking_lot::RwLock::new(self.keys.read().clone()),
}
}
}