use std::sync::Arc;
use kernable_core::proto;
use sha2::{Digest, Sha256};
use crate::{Error, Kernable, Outcome, Submit, Wait};
pub fn reindex_type(aggregate_type: &str) -> String {
format!("_reindex_{aggregate_type}")
}
pub(crate) type KeySubject<S> = Arc<dyn Fn(&S) -> Option<String> + Send + Sync>;
pub struct Unique<S> {
pub(crate) global: bool,
pub(crate) personal: Option<KeySubject<S>>,
}
impl<S> Default for Unique<S> {
fn default() -> Self {
Unique::tenant()
}
}
impl<S> Unique<S> {
pub fn tenant() -> Self {
Unique {
global: false,
personal: None,
}
}
pub fn global() -> Self {
Unique {
global: true,
personal: None,
}
}
#[must_use]
pub fn personal(
mut self,
subject: impl Fn(&S) -> Option<String> + Send + Sync + 'static,
) -> Self {
self.personal = Some(Arc::new(subject));
self
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Index {
pub keys: Vec<UniqueKey>,
pub facts: Vec<String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UniqueKey {
pub name: String,
pub value: String,
pub global: bool,
pub subject: Option<String>,
}
impl UniqueKey {
pub(crate) fn to_wire(&self) -> proto::UniqueKey {
proto::UniqueKey {
name: self.name.clone(),
global: self.global,
value_hash: Sha256::digest(self.value.as_bytes()).to_vec(),
subject: self.subject.clone().unwrap_or_default(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Requirement {
pub domain: String,
pub aggregate_type: String,
pub aggregate_id: String,
pub key: Option<String>,
pub value: Option<String>,
pub fact: String,
pub absent: bool,
}
impl Requirement {
pub fn new(domain: &str, aggregate_type: &str, aggregate_id: &str, fact: &str) -> Requirement {
Requirement {
domain: domain.to_owned(),
aggregate_type: aggregate_type.to_owned(),
aggregate_id: aggregate_id.to_owned(),
key: None,
value: None,
fact: fact.to_owned(),
absent: false,
}
}
pub fn of(aggregate_type: &str, aggregate_id: &str, fact: &str) -> Requirement {
Requirement::new("", aggregate_type, aggregate_id, fact)
}
pub fn by_key(
domain: &str,
aggregate_type: &str,
key: &str,
value: &str,
fact: &str,
) -> Requirement {
Requirement {
key: Some(key.to_owned()),
value: Some(value.to_owned()),
..Requirement::new(domain, aggregate_type, "", fact)
}
}
#[must_use]
pub fn absent(mut self) -> Requirement {
self.absent = true;
self
}
pub(crate) fn to_wire(&self, own: &str) -> proto::Requirement {
proto::Requirement {
domain: if self.domain.is_empty() {
own.to_owned()
} else {
self.domain.clone()
},
aggregate_type: self.aggregate_type.clone(),
aggregate_id: self.aggregate_id.clone(),
fact: self.fact.clone(),
key: self.key.clone().unwrap_or_default(),
value_hash: self
.value
.as_ref()
.map(|v| Sha256::digest(v.as_bytes()).to_vec())
.unwrap_or_default(),
absent: self.absent,
}
}
pub(crate) fn from_wire(q: proto::Requirement) -> Requirement {
Requirement {
domain: q.domain,
aggregate_type: q.aggregate_type,
aggregate_id: q.aggregate_id,
key: Some(q.key).filter(|k| !k.is_empty()),
value: None,
fact: q.fact,
absent: q.absent,
}
}
}
#[derive(Clone, Debug)]
pub struct Reindex {
domain: String,
aggregate_type: String,
aggregate_ids: Vec<String>,
tenant: Option<String>,
}
impl Reindex {
pub fn new(
domain: &str,
aggregate_type: &str,
aggregate_ids: impl IntoIterator<Item = impl Into<String>>,
) -> Reindex {
Reindex {
domain: domain.to_owned(),
aggregate_type: aggregate_type.to_owned(),
aggregate_ids: aggregate_ids.into_iter().map(Into::into).collect(),
tenant: None,
}
}
pub fn tenant(mut self, tenant: impl Into<String>) -> Reindex {
self.tenant = Some(tenant.into());
self
}
}
impl Kernable {
pub async fn reindex(&self, reindex: Reindex) -> Result<Vec<(String, Outcome)>, Error> {
let mut outcomes = Vec::with_capacity(reindex.aggregate_ids.len());
let command = reindex_type(&reindex.aggregate_type);
for id in reindex.aggregate_ids {
let mut submit = Submit::new(&reindex.domain, &reindex.aggregate_type, &id, &command)
.wait(Wait::Committed);
if let Some(tenant) = &reindex.tenant {
submit = submit.tenant(tenant.clone());
}
let outcome = self.submit(submit).await?;
outcomes.push((id, outcome));
}
Ok(outcomes)
}
}