MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / check_unique

Method check_unique

nodedb-crdt/src/constraint_checks.rs:40–76  ·  view source on GitHub ↗
(
        &self,
        state: &CrdtState,
        change: &ProposedChange,
        constraint: &Constraint,
    )

Source from the content-addressed store, hash-verified

38 }
39
40 pub(crate) fn check_unique(
41 &self,
42 state: &CrdtState,
43 change: &ProposedChange,
44 constraint: &Constraint,
45 ) -> Option<Violation> {
46 let field_value = change.fields.iter().find(|(f, _)| f == &constraint.field)?;
47
48 let value = &field_value.1;
49
50 // Bitemporal collections: only consider live (non-superseded) rows,
51 // so a new version of the same logical row with the same value does
52 // not spuriously collide with its prior version.
53 let exists = if self.is_bitemporal(&change.collection) {
54 state.field_value_exists_live(&change.collection, &constraint.field, value)
55 } else {
56 state.field_value_exists(&change.collection, &constraint.field, value)
57 };
58
59 if exists {
60 let value_str = format!("{:?}", value);
61 Some(Violation {
62 constraint_name: constraint.name.clone(),
63 reason: format!(
64 "value {} for field `{}` already exists in `{}`",
65 value_str, constraint.field, constraint.collection
66 ),
67 hint: CompensationHint::RetryWithDifferentValue {
68 field: constraint.field.clone(),
69 conflicting_value: value_str.clone(),
70 suggestion: format!("{value_str}-dedup"),
71 },
72 })
73 } else {
74 None
75 }
76 }
77
78 pub(crate) fn check_foreign_key(
79 &self,

Callers 1

check_constraintMethod · 0.80

Calls 6

field_value_existsMethod · 0.80
findMethod · 0.45
iterMethod · 0.45
is_bitemporalMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected