use std::{collections::HashSet, path::PathBuf, sync::Arc};
use async_trait::async_trait;
use cosmian_kmip::{
kmip_0::kmip_types::State,
kmip_2_1::{kmip_attributes::Attributes, kmip_objects::Object},
};
use crate::{InterfaceResult, ObjectWithMetadata, stores::SessionParams};
pub enum AtomicOperation {
Create((String, Object, Attributes, HashSet<String>)),
Upsert((String, Object, Attributes, Option<HashSet<String>>, State)),
UpdateObject((String, Object, Attributes, Option<HashSet<String>>)),
UpdateState((String, State)),
Delete(String),
}
impl AtomicOperation {
#[must_use]
pub fn get_object_uid(&self) -> &str {
match self {
Self::Create((uid, _, _, _))
| Self::Upsert((uid, _, _, _, _))
| Self::UpdateObject((uid, _, _, _))
| Self::UpdateState((uid, _))
| Self::Delete(uid) => uid,
}
}
}
#[async_trait(?Send)]
pub trait ObjectsStore {
fn filename(&self, group_id: u128) -> Option<PathBuf>;
async fn create(
&self,
uid: Option<String>,
owner: &str,
object: &Object,
attributes: &Attributes,
tags: &HashSet<String>,
params: Option<Arc<dyn SessionParams>>,
) -> InterfaceResult<String>;
async fn retrieve(
&self,
uid: &str,
params: Option<Arc<dyn SessionParams>>,
) -> InterfaceResult<Option<ObjectWithMetadata>>;
async fn retrieve_tags(
&self,
uid: &str,
params: Option<Arc<dyn SessionParams>>,
) -> InterfaceResult<HashSet<String>>;
async fn update_object(
&self,
uid: &str,
object: &Object,
attributes: &Attributes,
tags: Option<&HashSet<String>>,
params: Option<Arc<dyn SessionParams>>,
) -> InterfaceResult<()>;
async fn update_state(
&self,
uid: &str,
state: State,
params: Option<Arc<dyn SessionParams>>,
) -> InterfaceResult<()>;
async fn delete(
&self,
uid: &str,
params: Option<Arc<dyn SessionParams>>,
) -> InterfaceResult<()>;
async fn atomic(
&self,
user: &str,
operations: &[AtomicOperation],
params: Option<Arc<dyn SessionParams>>,
) -> InterfaceResult<Vec<String>>;
async fn is_object_owned_by(
&self,
uid: &str,
owner: &str,
params: Option<Arc<dyn SessionParams>>,
) -> InterfaceResult<bool>;
async fn list_uids_for_tags(
&self,
tags: &HashSet<String>,
params: Option<Arc<dyn SessionParams>>,
) -> InterfaceResult<HashSet<String>>;
async fn find(
&self,
researched_attributes: Option<&Attributes>,
state: Option<State>,
user: &str,
user_must_be_owner: bool,
params: Option<Arc<dyn SessionParams>>,
) -> InterfaceResult<Vec<(String, State, Attributes)>>;
}