use dson::{
CausalDotStore, Identifier, OrMap,
crdts::{mvreg::MvRegValue, snapshot::ToValue},
transaction::CrdtValue,
};
fn main() {
let mut replica_a = CausalDotStore::<OrMap<String>>::default();
let mut replica_b = CausalDotStore::<OrMap<String>>::default();
let id_a = Identifier::new(0, 0);
let id_b = Identifier::new(1, 0);
let delta_a = {
let mut tx = replica_a.transact(id_a);
tx.write_register("data", MvRegValue::String("text value".to_string()));
tx.commit()
};
let delta_b = {
let mut tx = replica_b.transact(id_b);
tx.in_map("data", |data_tx| {
data_tx.write_register("count", MvRegValue::U64(42));
});
tx.commit()
};
replica_a.join_or_replace_with(delta_b.0.store, &delta_b.0.context);
replica_b.join_or_replace_with(delta_a.0.store, &delta_a.0.context);
assert_eq!(replica_a, replica_b);
{
let tx = replica_a.transact(id_a);
match tx.get(&"data".to_string()) {
Some(CrdtValue::Conflicted(conflicts)) => {
let has_register = conflicts.has_register();
let has_map = conflicts.has_map();
let conflict_count = conflicts.conflict_count();
println!("Type conflict detected!");
println!(" Has register: {has_register}");
println!(" Has map: {has_map}");
println!(" Total conflicts: {conflict_count}");
if let Some(reg) = conflicts.register() {
if let Ok(MvRegValue::String(s)) = reg.value() {
println!(" Register value: {s}");
}
}
if let Some(_map) = conflicts.map() {
println!(" Map value is present");
}
println!("\nThe transaction API makes conflicts explicit!");
println!("Your application can decide how to resolve them.");
}
_ => println!("Expected a type conflict"),
}
}
}