1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
use std::hash::Hash; use std::collections::HashMap; use std::collections::hash_map::Entry; #[derive(Debug, PartialEq, Eq, Copy, Clone)] pub enum TemporalRelation { Equal, Caused, EffectOf, Concurrent, } #[derive(PartialEq, Eq, Debug)] pub struct VectorClock<HostType: Hash + Eq> { entries: HashMap<HostType, u64>, } impl<HostType: Clone + Hash + Eq> VectorClock<HostType> { pub fn new() -> VectorClock<HostType> { VectorClock { entries: HashMap::new(), } } pub fn incremented(&self, host: HostType) -> Self { let mut entries = self.entries.clone(); match entries.entry(host) { Entry::Vacant(e) => { e.insert(1); }, Entry::Occupied(mut e) => { let v = *e.get(); e.insert(v + 1); }, }; VectorClock { entries: entries, } } pub fn temporal_relation(&self, other: &Self) -> TemporalRelation { if self == other { TemporalRelation::Equal } else if self.superseded_by(other) { TemporalRelation::Caused } else if other.superseded_by(self) { TemporalRelation::EffectOf } else { TemporalRelation::Concurrent } } fn superseded_by(&self, other: &Self) -> bool { let mut has_smaller = false; for (host, &self_n) in self.entries.iter() { let other_n = *other.entries.get(host).unwrap_or(&0); if self_n > other_n { return false; } has_smaller = has_smaller || (self_n < other_n); } for (host, &other_n) in other.entries.iter() { let self_n = *self.entries.get(host).unwrap_or(&0); if self_n > other_n { return false; } has_smaller = has_smaller || (self_n < other_n); } has_smaller } pub fn merge_with(&self, other: &Self) -> Self { let mut entries = self.entries.clone(); for (host, &other_n) in other.entries.iter() { match entries.entry(host.clone()) { Entry::Vacant(e) => { e.insert(other_n); }, Entry::Occupied(mut e) => { let self_n = *e.get(); if other_n > self_n { e.insert(other_n); } } } } VectorClock { entries: entries, } } } #[cfg(test)] mod test { use super::{VectorClock, TemporalRelation}; type StrVectorClock = VectorClock<&'static str>; #[test] fn test_empty_ordering() { let c1 = StrVectorClock::new(); let c2 = StrVectorClock::new(); assert_eq!(c1, c2); assert!(c1.temporal_relation(&c2) == TemporalRelation::Equal); assert!(c2.temporal_relation(&c1) == TemporalRelation::Equal); } #[test] fn test_incremented_ordering() { let c1 = StrVectorClock::new(); let c2 = c1.incremented("A"); assert!(!(c1 == c2)); assert!(c1.temporal_relation(&c2) == TemporalRelation::Caused); assert!(c2.temporal_relation(&c1) == TemporalRelation::EffectOf); } #[test] fn test_diverged() { let base = StrVectorClock::new(); let c1 = base.incremented("A"); let c2 = base.incremented("B"); assert!(!(c1 == c2)); assert!(c1.temporal_relation(&c2) == TemporalRelation::Concurrent); assert!(c2.temporal_relation(&c1) == TemporalRelation::Concurrent); } #[test] fn test_merged() { let base = StrVectorClock::new(); let c1 = base.incremented("A"); let c2 = base.incremented("B"); let m = c1.merge_with(&c2); assert!(m.temporal_relation(&c1) == TemporalRelation::EffectOf); assert!(c1.temporal_relation(&m) == TemporalRelation::Caused); assert!(m.temporal_relation(&c2) == TemporalRelation::EffectOf); assert!(c2.temporal_relation(&m) == TemporalRelation::Caused); } }