use crate::cascade_delete_enrichment_strategy::CascadeDeleteEnrichmentStrategy;
use crate::create_enrichment_strategy::{missing, CreateEnrichmentStrategy};
use crate::datasource_error::DatasourceError;
use crate::default_create_enrichment_strategy::DefaultCreateEnrichmentStrategy;
use crate::default_update_enrichment_strategy::DefaultUpdateEnrichmentStrategy;
use crate::delete_enrichment_strategy::DeleteEnrichmentStrategy;
use crate::enrichment::Enrichment;
use crate::entity::Entity;
use crate::repository_tree::RepositoryTree;
use crate::result::Result;
use crate::row::Row;
use crate::stored_enrichment::StoredEnrichment;
use crate::update_enrichment_strategy::UpdateEnrichmentStrategy;
use crate::value::Value;
use crate::write_enrichment_strategy::WriteEnrichmentStrategy;
use crate::write_mode::WriteMode;
use async_trait::async_trait;
use std::collections::HashSet;
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::Arc;
pub struct DefaultWriteEnrichmentStrategy<T: Entity = Row> {
create: Arc<dyn CreateEnrichmentStrategy<T>>,
delete: Arc<dyn DeleteEnrichmentStrategy<T>>,
update: Arc<dyn UpdateEnrichmentStrategy<T>>,
skip_unloaded: bool,
use_transaction: bool,
entity: PhantomData<T>,
}
impl<T: Entity> DefaultWriteEnrichmentStrategy<T> {
pub fn new(
delete: Option<Arc<dyn DeleteEnrichmentStrategy<T>>>,
update: Option<Arc<dyn UpdateEnrichmentStrategy<T>>>,
create: Option<Arc<dyn CreateEnrichmentStrategy<T>>>,
use_transaction: bool,
) -> Self {
Self {
create: create.unwrap_or_else(|| Arc::new(DefaultCreateEnrichmentStrategy)),
delete: delete.unwrap_or_else(|| Arc::new(CascadeDeleteEnrichmentStrategy)),
update: update.unwrap_or_else(|| Arc::new(DefaultUpdateEnrichmentStrategy)),
skip_unloaded: false,
use_transaction,
entity: PhantomData,
}
}
pub fn lazy(
delete: Option<Arc<dyn DeleteEnrichmentStrategy<T>>>,
update: Option<Arc<dyn UpdateEnrichmentStrategy<T>>>,
create: Option<Arc<dyn CreateEnrichmentStrategy<T>>>,
use_transaction: bool,
) -> Self {
Self {
skip_unloaded: true,
..Self::new(delete, update, create, use_transaction)
}
}
}
impl<T: Entity> Default for DefaultWriteEnrichmentStrategy<T> {
fn default() -> Self {
Self::new(None, None, None, false)
}
}
#[async_trait]
impl<T: Entity> WriteEnrichmentStrategy<T> for DefaultWriteEnrichmentStrategy<T> {
fn use_transaction(&self) -> bool {
self.use_transaction
}
async fn resolve_lookups(
&self,
tree: &RepositoryTree<T>,
items: &mut [T],
record: &mut Row,
schema: &crate::schema::Schema,
) -> Result<()> {
let entries = tree.parent.enrichments.lock().expect("enrichments").clone();
crate::create_enrichment_strategy::resolve_lookups(&entries, items, record, schema).await
}
fn mapped_sources(
&self,
tree: &RepositoryTree<T>,
column: &dyn Fn(&str) -> String,
) -> Vec<(String, String)> {
let entries = tree.parent.enrichments.lock().expect("enrichments");
crate::create_enrichment_strategy::mapped_sources(&entries, column)
}
async fn lookup(
&self,
tree: &RepositoryTree<T>,
items: &mut [T],
record: &mut Row,
schema: &crate::schema::Schema,
columns: Vec<(String, String)>,
) -> Result<()> {
let entries = tree.parent.enrichments.lock().expect("enrichments").clone();
crate::create_enrichment_strategy::lookup(&entries, items, record, schema, &columns).await
}
fn sources(
&self,
tree: &RepositoryTree<T>,
column: &dyn Fn(&str) -> String,
) -> HashSet<String> {
let entries = tree.parent.enrichments.lock().expect("enrichments");
crate::create_enrichment_strategy::sources(&entries, column)
}
fn destination(&self, tree: &RepositoryTree<T>, name: &str) -> bool {
let entries = tree.parent.enrichments.lock().expect("enrichments");
crate::create_enrichment_strategy::destination(&entries, name)
}
async fn persist(
&self,
tree: &RepositoryTree<T>,
items: &mut [T],
mode: WriteMode,
) -> Result<()> {
let entries = tree.parent.enrichments.lock().expect("enrichments").clone();
self.walk(&entries, items, mode).await
}
async fn persist_bulk(
&self,
tree: &RepositoryTree<T>,
items: &mut [T],
mode: WriteMode,
) -> Result<()> {
if mode == WriteMode::Delete {
return self.persist(tree, items, mode).await;
}
let entries = tree.parent.enrichments.lock().expect("enrichments").clone();
self.walk_bulk(&entries, items, mode).await
}
}
impl<T: Entity> DefaultWriteEnrichmentStrategy<T> {
fn walk<'a>(
&'a self,
entries: &'a [StoredEnrichment<T>],
parents: &'a mut [T],
mode: WriteMode,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
Box::pin(async move {
for entry in entries {
self.persist_entry(entry, parents, mode).await?;
}
Ok(())
})
}
fn walk_bulk<'a>(
&'a self,
entries: &'a [StoredEnrichment<T>],
parents: &'a mut [T],
mode: WriteMode,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
Box::pin(async move {
for entry in entries {
self.persist_entry_bulk(entry, parents, mode).await?;
}
Ok(())
})
}
fn persist_entry_bulk<'a>(
&'a self,
entry: &'a StoredEnrichment<T>,
parents: &'a mut [T],
mode: WriteMode,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
Box::pin(async move {
let enrichment = &entry.enrichment;
if matches!(enrichment.r#type, crate::EnrichmentPropertyType::Custom) {
return Err(DatasourceError::message(format!(
"Unsupported enrichment type \"{:?}\"",
enrichment.r#type
)));
}
if enrichment.association.is_some() {
if let Some(link) = &entry.link {
for parent in parents.iter_mut() {
if self.skip(parent, &enrichment.destination_property, mode) {
continue;
}
link.write(enrichment, parent, mode).await?;
}
}
return Ok(());
}
if enrichment.r#type == crate::EnrichmentPropertyType::List
&& enrichment.related_property.is_some()
{
return Ok(());
}
if enrichment.r#type != crate::EnrichmentPropertyType::List
&& enrichment.r#type != crate::EnrichmentPropertyType::Dictionary
{
return Ok(());
}
let mut children = Vec::new();
let mut chunks = Vec::new();
for parent in parents.iter() {
if self.skip(parent, &enrichment.destination_property, mode) {
chunks.push(None);
continue;
}
let rows = collect_children(parent, enrichment)?;
chunks.push(Some(rows.len()));
children.extend(rows);
}
if children.is_empty() {
return Ok(());
}
let persist = entry.persist.as_ref().ok_or_else(|| {
DatasourceError::message(format!(
"Deep write requires a CRUD repository for \"{}\"",
enrichment.destination_property
))
})?;
if let Some(bulk) = persist.persist_bulk() {
match mode {
WriteMode::Create => {
children = bulk.create_bulk(&mut children).await?;
}
WriteMode::Update => {
bulk.update_bulk(&children, None).await?;
}
WriteMode::Delete => return Ok(()),
}
let mut at = 0;
for (parent, chunk) in parents.iter_mut().zip(chunks) {
let Some(count) = chunk else { continue };
parent.set_payload(
&enrichment.destination_property,
children[at..at + count].to_vec(),
)?;
at += count;
}
return Ok(());
}
for parent in parents.iter_mut() {
if self.skip(parent, &enrichment.destination_property, mode) {
continue;
}
persist.write(enrichment, parent, mode).await?;
}
Ok(())
})
}
fn persist_entry<'a>(
&'a self,
entry: &'a StoredEnrichment<T>,
parents: &'a mut [T],
mode: WriteMode,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
Box::pin(async move {
let enrichment = &entry.enrichment;
if matches!(enrichment.r#type, crate::EnrichmentPropertyType::Custom) {
return Err(DatasourceError::message(format!(
"Unsupported enrichment type \"{:?}\"",
enrichment.r#type
)));
}
if enrichment.association.is_some() {
let link = entry.link.as_ref().ok_or_else(|| {
DatasourceError::message(format!(
"Deep write requires a CRUD repository for \"{}\"",
enrichment.destination_property
))
})?;
for parent in parents.iter_mut() {
if self.skip(parent, &enrichment.destination_property, mode) {
continue;
}
if let Some(writer) = link.row_writer() {
match mode {
WriteMode::Create => {
self.create.create_linked(writer, enrichment, parent).await?
}
WriteMode::Update => {
self.update.update_linked(writer, enrichment, parent).await?
}
WriteMode::Delete => {
self.delete.delete_linked(writer, enrichment, parent).await?
}
}
} else {
link.write(enrichment, parent, mode).await?;
}
}
return Ok(());
}
if enrichment.r#type == crate::EnrichmentPropertyType::List
&& enrichment.related_property.is_some()
{
return Ok(());
}
if enrichment.r#type != crate::EnrichmentPropertyType::List
&& enrichment.r#type != crate::EnrichmentPropertyType::Dictionary
{
return Ok(());
}
let persist = entry.persist.as_ref().ok_or_else(|| {
DatasourceError::message(format!(
"Deep write requires a CRUD repository for \"{}\"",
enrichment.destination_property
))
})?;
for parent in parents.iter_mut() {
if self.skip(parent, &enrichment.destination_property, mode) {
continue;
}
if let Some(writer) = persist.row_writer() {
match mode {
WriteMode::Create => {
self.create.create_children(writer, enrichment, parent).await?
}
WriteMode::Update => {
self.update.update_children(writer, enrichment, parent).await?
}
WriteMode::Delete => {
self.delete
.delete_children(writer, enrichment, parent, self)
.await?
}
}
} else {
persist.write(enrichment, parent, mode).await?;
}
}
Ok(())
})
}
fn skip(&self, parent: &T, into: &str, mode: WriteMode) -> bool {
self.skip_unloaded && mode != WriteMode::Delete && !parent.has(into)
}
}
#[async_trait]
impl<T: Entity> crate::delete_nested::DeleteNested for DefaultWriteEnrichmentStrategy<T> {
async fn delete_nested(&self, _rows: &mut [Row]) -> Result<()> {
Ok(())
}
}
pub(crate) fn related_ids<T: Entity>(parent: &T, enrichment: &Enrichment) -> Result<Vec<Value>> {
let mut ids = Vec::new();
let mut seen = HashSet::new();
for related in parent.payload(&enrichment.destination_property) {
let id = related
.get(&enrichment.related_key)
.cloned()
.ok_or_else(|| {
DatasourceError::message(format!(
"{} requires \"{}\"",
enrichment.destination_property, enrichment.related_key
))
})?;
if missing(&id) {
return Err(DatasourceError::message(format!(
"{} requires \"{}\"",
enrichment.destination_property, enrichment.related_key
)));
}
if seen.insert(id.wire_text()) {
ids.push(id);
}
}
Ok(ids)
}
pub(crate) fn collect_children<T: Entity>(parent: &T, enrichment: &Enrichment) -> Result<Vec<Row>> {
let mut related = parent.payload(&enrichment.destination_property);
if related.is_empty() {
return Ok(related);
}
let key = required(
parent,
&enrichment.source_property,
&enrichment.destination_property,
)?;
for row in &mut related {
row.insert(enrichment.related_key.clone(), key.clone());
}
Ok(related)
}
pub(crate) fn required<T: Entity>(parent: &T, name: &str, into: &str) -> Result<Value> {
parent
.cell(name)
.filter(|value| !missing(value))
.ok_or_else(|| DatasourceError::message(format!("{into} requires \"{name}\"")))
}
pub fn decode_rows(value: Option<&Value>) -> Vec<Row> {
match value {
Some(Value::Text(text)) if !text.is_empty() => text.split('|').map(decode_row).collect(),
_ => Vec::new(),
}
}
fn decode_row(text: &str) -> Row {
text.split(',')
.filter_map(|pair| {
let (key, value) = pair.split_once('=')?;
Some((key.to_string(), parse_value(&unescape(value))))
})
.collect()
}
fn parse_value(text: &str) -> Value {
match text {
"true" => Value::Bool(true),
"false" => Value::Bool(false),
_ => text
.parse::<i64>()
.map(Value::Int)
.unwrap_or_else(|_| Value::Text(text.into())),
}
}
pub fn encode_rows(rows: &[Row]) -> Value {
Value::Text(
rows.iter()
.map(encode_row_text)
.collect::<Vec<_>>()
.join("|"),
)
}
fn unescape(text: &str) -> String {
text.replace("%7C", "|")
.replace("%2C", ",")
.replace("%3D", "=")
.replace("%25", "%")
}
fn escape_cell(text: &str) -> String {
text.replace('%', "%25")
.replace('|', "%7C")
.replace(',', "%2C")
.replace('=', "%3D")
}
fn encode_row_text(row: &Row) -> String {
let mut keys: Vec<_> = row.keys().cloned().collect();
keys.sort();
keys.into_iter()
.filter_map(|key| {
row.get(&key)
.map(|value| format!("{key}={}", escape_cell(&value.wire_text())))
})
.collect::<Vec<_>>()
.join(",")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::converters::{RowTypeConverter, TypeConverter};
use crate::crud_repository::CRUDRepository;
use crate::enrichment_property_type::EnrichmentPropertyType;
use crate::schema::Schema;
use std::collections::HashMap;
use uuid::Uuid;
struct Flag;
impl TypeConverter<Value, Value> for Flag {
fn convert(&self, from: &Value) -> Result<Value> {
Ok(match from {
Value::Int(1) => Value::Bool(true),
other => other.clone(),
})
}
fn reverse_convert(&self, to: &Value) -> Result<Value> {
Ok(match to {
Value::Bool(true) => Value::Int(1),
Value::Bool(false) => Value::Int(0),
other => other.clone(),
})
}
}
struct UuidText;
impl TypeConverter<Value, Value> for UuidText {
fn convert(&self, from: &Value) -> Result<Value> {
Ok(from.clone())
}
fn reverse_convert(&self, to: &Value) -> Result<Value> {
Ok(match to {
Value::Uuid(id) => Value::Text(id.to_string()),
other => other.clone(),
})
}
}
#[tokio::test]
async fn lookup_row_converts_uuid_and_boolean_on_property_keys() {
let types = Arc::new(CRUDRepository::<Row>::memory(vec![]));
let mut parents = CRUDRepository::<Row>::memory(vec![]);
parents.add_enriched(
Enrichment::new(
"TypeName",
"Type",
"id",
Some("Name".into()),
EnrichmentPropertyType::Object,
),
types,
None,
);
let write = DefaultWriteEnrichmentStrategy::<Row>::default();
let id = Uuid::parse_str("11111111-1111-1111-1111-111111111111").unwrap();
let mut item = Row::new();
item.insert("Type".into(), Value::Uuid(id));
item.insert("Active".into(), Value::Bool(true));
let objects = <Row as crate::entity::Entity>::object_converter();
let rows = RowTypeConverter::from(
|| {
HashMap::from([
(
"boolean".into(),
Box::new(Flag) as Box<dyn TypeConverter<Value, Value> + Send + Sync>,
),
(
"uuid".into(),
Box::new(UuidText) as Box<dyn TypeConverter<Value, Value> + Send + Sync>,
),
])
},
&HashMap::from([
("Type".into(), "uuid".into()),
("Active".into(), "boolean".into()),
]),
);
let columns = write.mapped_sources(&parents.tree, &|name| {
if name == "Type" {
"type_id".into()
} else {
name.into()
}
});
let converted = write
.lookup_row(
&parents.tree,
&mut item,
Schema::empty(),
objects,
Some(&rows),
columns,
)
.await
.unwrap();
assert!(converted.contains_key("Type"));
assert!(!converted.contains_key("type_id"));
assert_eq!(converted["Type"], Value::Text(id.to_string()));
assert_eq!(converted["Active"], Value::Int(1));
}
}