use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use async_trait::async_trait;
use crate::table::Table;
use crate::transaction::action::{ActionCommit, TransactionAction};
use crate::{Error, ErrorKind, Result, TableUpdate};
pub struct UpdatePropertiesAction {
updates: HashMap<String, String>,
removals: HashSet<String>,
}
impl UpdatePropertiesAction {
pub fn new() -> Self {
UpdatePropertiesAction {
updates: HashMap::default(),
removals: HashSet::default(),
}
}
pub fn set(mut self, key: String, value: String) -> Self {
self.updates.insert(key, value);
self
}
pub fn remove(mut self, key: String) -> Self {
self.removals.insert(key);
self
}
}
impl Default for UpdatePropertiesAction {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl TransactionAction for UpdatePropertiesAction {
async fn commit(self: Arc<Self>, _table: &Table) -> Result<ActionCommit> {
if let Some(overlapping_key) = self.removals.iter().find(|k| self.updates.contains_key(*k))
{
return Err(Error::new(
ErrorKind::PreconditionFailed,
format!("Key {overlapping_key} is present in both removal set and update set"),
));
}
let updates: Vec<TableUpdate> = vec![
TableUpdate::SetProperties {
updates: self.updates.clone(),
},
TableUpdate::RemoveProperties {
removals: self.removals.clone().into_iter().collect::<Vec<String>>(),
},
];
Ok(ActionCommit::new(updates, vec![]))
}
}
#[cfg(test)]
mod tests {
use std::collections::{HashMap, HashSet};
use as_any::Downcast;
use crate::transaction::Transaction;
use crate::transaction::action::ApplyTransactionAction;
use crate::transaction::tests::make_v2_table;
use crate::transaction::update_properties::UpdatePropertiesAction;
#[test]
fn test_update_table_property() {
let table = make_v2_table();
let tx = Transaction::new(&table);
let tx = tx
.update_table_properties()
.set("a".to_string(), "b".to_string())
.remove("b".to_string())
.apply(tx)
.unwrap();
assert_eq!(tx.actions.len(), 1);
let action = (*tx.actions[0])
.downcast_ref::<UpdatePropertiesAction>()
.unwrap();
assert_eq!(
action.updates,
HashMap::from([("a".to_string(), "b".to_string())])
);
assert_eq!(action.removals, HashSet::from(["b".to_string()]));
}
}