use std::sync::Arc;
use aa_storage::{AgentId, PolicyDocument, PolicyStore, Result, StorageError};
use async_trait::async_trait;
use dashmap::DashMap;
#[derive(Clone, Default)]
pub struct MemoryPolicyStore {
policies: Arc<DashMap<[u8; 16], PolicyDocument>>,
}
impl MemoryPolicyStore {
pub fn new() -> Self {
Self::default()
}
pub fn insert(&self, agent_id: &AgentId, policy: PolicyDocument) {
self.policies.insert(*agent_id.as_bytes(), policy);
}
}
#[async_trait]
impl PolicyStore for MemoryPolicyStore {
async fn get_policy(&self, agent_id: &AgentId) -> Result<PolicyDocument> {
self.policies
.get(agent_id.as_bytes())
.map(|entry| entry.value().clone())
.ok_or_else(|| StorageError::NotFound(format!("policy for agent {:?}", agent_id.as_bytes())))
}
async fn invalidate(&self, _agent_id: &AgentId) -> Result<()> {
Ok(())
}
}