use reifydb_core::{
interface::catalog::policy::{Policy, PolicyId},
key::policy::PolicyKey,
};
use reifydb_transaction::transaction::Transaction;
use crate::{
CatalogStore, Result,
store::policy::{convert_policy, shape::policy},
};
impl CatalogStore {
pub(crate) fn find_policy(rx: &mut Transaction<'_>, id: PolicyId) -> Result<Option<Policy>> {
Ok(rx.get(&PolicyKey::encoded(id))?.map(convert_policy))
}
pub(crate) fn find_policy_by_name(rx: &mut Transaction<'_>, name: &str) -> Result<Option<Policy>> {
let stream = rx.range(PolicyKey::full_scan(), 1024)?;
for entry in stream {
let multi = entry?;
let policy_name = policy::SHAPE.get_utf8(&multi.row, policy::NAME);
if !policy_name.is_empty() && name == policy_name {
return Ok(Some(convert_policy(multi)));
}
}
Ok(None)
}
}
#[cfg(test)]
mod tests {
use reifydb_core::interface::catalog::policy::{PolicyTargetType, PolicyToCreate};
use reifydb_engine::test_harness::create_test_admin_transaction;
use reifydb_transaction::transaction::Transaction;
use crate::CatalogStore;
#[test]
fn test_find_policy_by_name() {
let mut txn = create_test_admin_transaction();
let to_create = PolicyToCreate {
name: Some("test_policy".to_string()),
target_type: PolicyTargetType::Table,
target_namespace: None,
target_shape: None,
operations: vec![],
};
CatalogStore::create_policy(&mut txn, to_create).unwrap();
let found =
CatalogStore::find_policy_by_name(&mut Transaction::Admin(&mut txn), "test_policy").unwrap();
assert!(found.is_some());
assert_eq!(found.unwrap().name, Some("test_policy".to_string()));
}
#[test]
fn test_find_policy_by_name_not_found() {
let mut txn = create_test_admin_transaction();
let found =
CatalogStore::find_policy_by_name(&mut Transaction::Admin(&mut txn), "nonexistent").unwrap();
assert!(found.is_none());
}
}