Vector clock implementation for distributed causality tracking.
Vector clocks are a mechanism for determining the partial ordering of events in a distributed system. They are used to detect causality violations and concurrent updates in distributed databases.
This implementation uses a map where keys represent devices in a distributed db, and values represent the version last contributed by that device.
Vector clock implementation for distributed causality tracking. Vector clocks are a mechanism for determining the partial ordering of events in a distributed system. They are used to detect causality violations and concurrent updates in distributed databases. This implementation uses a map where keys represent devices in a distributed db, and values represent the version last contributed by that device.
(check this-clock other-clock)Compare two vector clocks to determine their causal relationship.
Returns one of:
:concurrent - Clocks represent concurrent events (neither causally precedes the other):ancestor - this-clock causally precedes other-clock:descendant - this-clock causally follows other-clock:same - Clocks are identicalExample:
(def c1 {"a" 1 "b" 0})
(def c2 {"a" 0 "b" 1})
(def c3 {"a" 1 "b" 1})
(check c1 c2) ; => :concurrent
(check c1 c3) ; => :ancestor
(check c3 c1) ; => :descendant
(check c1 c1) ; => :same
Compare two vector clocks to determine their causal relationship.
Returns one of:
- `:concurrent` - Clocks represent concurrent events (neither causally precedes the other)
- `:ancestor` - `this-clock` causally precedes `other-clock`
- `:descendant` - `this-clock` causally follows `other-clock`
- `:same` - Clocks are identical
Example:
```clojure
(def c1 {"a" 1 "b" 0})
(def c2 {"a" 0 "b" 1})
(def c3 {"a" 1 "b" 1})
(check c1 c2) ; => :concurrent
(check c1 c3) ; => :ancestor
(check c3 c1) ; => :descendant
(check c1 c1) ; => :same
```(make-clock device-id)Create a new vector clock for the given device ID.
Example:
(make-clock "device-1") ; => {"device-1" 0}
Create a new vector clock for the given device ID.
Example:
```clojure
(make-clock "device-1") ; => {"device-1" 0}
```(merge-clocks clock1 clock2)Merge two vector clocks by taking the maximum value for each device.
This is used to synchronize clocks across devices, taking the maximum observed event count for each device.
Example:
(def c1 {"a" 3 "b" 1})
(def c2 {"a" 2 "b" 5 "c" 1})
(merge-clocks c1 c2) ; => {"a" 3 "b" 5 "c" 1}
Merge two vector clocks by taking the maximum value for each device.
This is used to synchronize clocks across devices, taking the maximum
observed event count for each device.
Example:
```clojure
(def c1 {"a" 3 "b" 1})
(def c2 {"a" 2 "b" 5 "c" 1})
(merge-clocks c1 c2) ; => {"a" 3 "b" 5 "c" 1}
```(tick clock device-id)Increment the clock for the current device.
Returns a new clock with the device's counter incremented by 1.
Example:
(def clock (make-clock "device-1"))
(tick clock "device-1") ; => {"device-1" 1}
(tick (tick clock "device-1") "device-1") ; => {"device-1" 2}
Increment the clock for the current device.
Returns a new clock with the device's counter incremented by 1.
Example:
```clojure
(def clock (make-clock "device-1"))
(tick clock "device-1") ; => {"device-1" 1}
(tick (tick clock "device-1") "device-1") ; => {"device-1" 2}
```cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |