Package org.syncany.database
Class VectorClock
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.TreeMap<java.lang.String,java.lang.Long>
-
- org.syncany.database.VectorClock
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.util.Map<java.lang.String,java.lang.Long>
,java.util.NavigableMap<java.lang.String,java.lang.Long>
,java.util.SortedMap<java.lang.String,java.lang.Long>
public class VectorClock extends java.util.TreeMap<java.lang.String,java.lang.Long>
Implements a vector clock that records the time stamps of all send and receive events. It contains functions to compare and merge two vector clocks.Vector clocks are a mechanism to track events/actions across multiple distributed clients. Using vector clocks, one can determine relationships between different events. In particular:
- Event A happened before / was caused by event B (cause)
- Event B happened after / caused event B (effect)
- Event A and B happened simultaneously (no cause/effect relationship)
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
VectorClock.VectorClockComparison
-
Field Summary
Fields Modifier and Type Field Description static java.util.regex.Pattern
MACHINE_PATTERN
-
Constructor Summary
Constructors Constructor Description VectorClock()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description VectorClock
clone()
static VectorClock.VectorClockComparison
compare(VectorClock clock1, VectorClock clock2)
VectorClock compare operation.java.lang.Long
get(java.lang.Object unit)
java.lang.Long
getClock(java.lang.String unit)
Retrieve the unit's valuejava.lang.Long
incrementClock(java.lang.String unit)
Increases the component of a unit by 1.static VectorClock
parseVectorClock(java.lang.String serializedVectorClock)
Converts a serialized vector clock back into aVectorClock
object.void
setClock(java.lang.String unit, long value)
Set the component of a unit.java.lang.String
toString()
-
Methods inherited from class java.util.TreeMap
ceilingEntry, ceilingKey, clear, comparator, containsKey, containsValue, descendingKeySet, descendingMap, entrySet, firstEntry, firstKey, floorEntry, floorKey, forEach, headMap, headMap, higherEntry, higherKey, keySet, lastEntry, lastKey, lowerEntry, lowerKey, navigableKeySet, pollFirstEntry, pollLastEntry, put, putAll, remove, replace, replace, replaceAll, size, subMap, subMap, tailMap, tailMap, values
-
-
-
-
Field Detail
-
MACHINE_PATTERN
public static final java.util.regex.Pattern MACHINE_PATTERN
-
-
Constructor Detail
-
VectorClock
public VectorClock()
-
-
Method Detail
-
incrementClock
public java.lang.Long incrementClock(java.lang.String unit)
Increases the component of a unit by 1.- Parameters:
unit
- The identifier of the vector element being increased- Returns:
- Returns the new clock value for the given unit
-
setClock
public void setClock(java.lang.String unit, long value)
Set the component of a unit.- Parameters:
unit
- The identifier of the vector element being setvalue
- The new value of the unit being set
-
getClock
public java.lang.Long getClock(java.lang.String unit)
Retrieve the unit's value- Parameters:
unit
- The identifier of the vector element being retrieved- Returns:
- Returns the value of the unit (if existent), or
null
if it does not exist
-
get
public java.lang.Long get(java.lang.Object unit)
- Specified by:
get
in interfacejava.util.Map<java.lang.String,java.lang.Long>
- Overrides:
get
in classjava.util.TreeMap<java.lang.String,java.lang.Long>
-
clone
public VectorClock clone()
- Overrides:
clone
in classjava.util.TreeMap<java.lang.String,java.lang.Long>
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.util.AbstractMap<java.lang.String,java.lang.Long>
-
parseVectorClock
public static VectorClock parseVectorClock(java.lang.String serializedVectorClock)
Converts a serialized vector clock back into aVectorClock
object.- See Also:
toString()
-
compare
public static VectorClock.VectorClockComparison compare(VectorClock clock1, VectorClock clock2)
VectorClock compare operation. Returns one of four possible values indicating how clock one relates to clock two: VectorComparison.GREATER If One > Two. VectorComparison.EQUAL If One = Two. VectorComparison.SMALLER If One < Two. VectorComparison.SIMULTANEOUS If One != Two.- Parameters:
clock1
- First Clock being compared.clock2
- Second Clock being compared.- Returns:
- VectorComparison value indicating how One relates to Two.
-
-