public final class LWWRegister<A> extends java.lang.Object implements ReplicatedData, ReplicatedDataSerialization
It is described in the paper A comprehensive study of Convergent and Commutative Replicated Data Types.
Merge takes the register with highest timestamp. Note that this
relies on synchronized clocks. LWWRegister
should only be used when the choice of
value is not important for concurrent updates occurring within the clock skew.
Merge takes the register updated by the node with lowest address (UniqueAddress
is ordered)
if the timestamps are exactly the same.
Instead of using timestamps based on System.currentTimeMillis()
time it is possible to
use a timestamp value based on something else, for example an increasing version number
from a database record that is used for optimistic concurrency control.
The defaultClock
is using max value of System.currentTimeMillis()
and currentTimestamp + 1
.
This means that the timestamp is increased for changes on the same node that occurs within
the same millisecond. It also means that it is safe to use the LWWRegister
without
synchronized clocks when there is only one active writer, e.g. a Cluster Singleton. Such a
single writer should then first read current value with ReadMajority
(or more) before
changing and writing the value with WriteMajority
(or more).
For first-write-wins semantics you can use the reverseClock()
instead of the
defaultClock()
This class is immutable, i.e. "modifying" methods return a new instance.
Modifier and Type | Class and Description |
---|---|
static interface |
LWWRegister.Clock<A> |
Constructor and Description |
---|
LWWRegister(UniqueAddress node,
A value,
long timestamp) |
Modifier and Type | Method and Description |
---|---|
static <A> LWWRegister<A> |
apply(A initialValue,
Cluster node,
LWWRegister.Clock<A> clock) |
static <A> LWWRegister<A> |
apply(UniqueAddress node,
A initialValue,
LWWRegister.Clock<A> clock)
INTERNAL API
|
static <A> LWWRegister<A> |
create(Cluster node,
A initialValue)
Java API
|
static <A> LWWRegister<A> |
create(Cluster node,
A initialValue,
LWWRegister.Clock<A> clock)
Java API
|
static <A> LWWRegister.Clock<A> |
defaultClock()
The default
LWWRegister.Clock is using max value of System.currentTimeMillis()
and currentTimestamp + 1 . |
boolean |
equals(java.lang.Object o) |
A |
getValue()
Java API
|
int |
hashCode() |
LWWRegister<A> |
merge(LWWRegister<A> that)
INTERNAL API
|
UniqueAddress |
node() |
static <A> LWWRegister.Clock<A> |
reverseClock()
This
LWWRegister.Clock can be used for first-write-wins semantics. |
long |
timestamp() |
java.lang.String |
toString() |
static <A> scala.Option<A> |
unapply(LWWRegister<A> c)
Extract the
value() . |
UniqueAddress |
updatedBy()
The current
value was set by this node. |
A |
value() |
LWWRegister<A> |
withValue(A value,
Cluster node,
LWWRegister.Clock<A> clock)
Change the value of the register.
|
LWWRegister<A> |
withValue(Cluster node,
A value)
Change the value of the register.
|
LWWRegister<A> |
withValue(Cluster node,
A value,
LWWRegister.Clock<A> clock)
Change the value of the register.
|
LWWRegister<A> |
withValue(UniqueAddress node,
A value,
LWWRegister.Clock<A> clock)
INTERNAL API
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
merge
public LWWRegister(UniqueAddress node, A value, long timestamp)
public static <A> LWWRegister.Clock<A> defaultClock()
LWWRegister.Clock
is using max value of System.currentTimeMillis()
and currentTimestamp + 1
.public static <A> LWWRegister.Clock<A> reverseClock()
LWWRegister.Clock
can be used for first-write-wins semantics. It is using min value of
-System.currentTimeMillis()
and currentTimestamp + 1
, i.e. it is counting backwards.public static <A> LWWRegister<A> apply(UniqueAddress node, A initialValue, LWWRegister.Clock<A> clock)
node
- (undocumented)initialValue
- (undocumented)clock
- (undocumented)public static <A> LWWRegister<A> apply(A initialValue, Cluster node, LWWRegister.Clock<A> clock)
public static <A> LWWRegister<A> create(Cluster node, A initialValue)
node
- (undocumented)initialValue
- (undocumented)public static <A> LWWRegister<A> create(Cluster node, A initialValue, LWWRegister.Clock<A> clock)
node
- (undocumented)initialValue
- (undocumented)clock
- (undocumented)public static <A> scala.Option<A> unapply(LWWRegister<A> c)
value()
.c
- (undocumented)public UniqueAddress node()
public A value()
public long timestamp()
public A getValue()
public LWWRegister<A> withValue(A value, Cluster node, LWWRegister.Clock<A> clock)
You can provide your clock
implementation instead of using timestamps based
on System.currentTimeMillis()
time. The timestamp can for example be an
increasing version number from a database record that is used for optimistic
concurrency control.
value
- (undocumented)node
- (undocumented)clock
- (undocumented)public LWWRegister<A> withValue(Cluster node, A value)
node
- (undocumented)value
- (undocumented)public LWWRegister<A> withValue(Cluster node, A value, LWWRegister.Clock<A> clock)
You can provide your clock
implementation instead of using timestamps based
on System.currentTimeMillis()
time. The timestamp can for example be an
increasing version number from a database record that is used for optimistic
concurrency control.
node
- (undocumented)value
- (undocumented)clock
- (undocumented)public UniqueAddress updatedBy()
value
was set by this node.public LWWRegister<A> withValue(UniqueAddress node, A value, LWWRegister.Clock<A> clock)
node
- (undocumented)value
- (undocumented)clock
- (undocumented)public LWWRegister<A> merge(LWWRegister<A> that)
that
- (undocumented)public java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object