Class LWWRegister<V>
java.lang.Object
com.loomcache.server.datastructures.crdt.LWWRegister<V>
Last-Writer-Wins Register CRDT (LWW-Register).
An LWW-Register is a state-based CRDT that represents a register (single value) where the last write (by timestamp) is the value returned. In case of timestamp ties, the node ID is used as a tiebreaker (lexicographic ordering).
Key properties:
- Stores a single value with a timestamp
- Merge: higher timestamp wins
- Ties broken by node ID (lexicographic)
- Always produces a deterministic result
- Convergent: all nodes reach the same state given the same history
- Since:
- 1.3
-
Constructor Summary
ConstructorsConstructorDescriptionLWWRegister(@NonNull String name, @NonNull String nodeId, int instanceNumber) -
Method Summary
Modifier and TypeMethodDescription@Nullable Vget()Get the current value in the register.longGet the timestamp of the current value.@Nullable StringGet the node ID that wrote the current value.booleanisSet()Returns true once the register has observed at least one write.voidmerge(LWWRegister<V> other) Merge another LWW-Register into this one (anti-entropy).voidreset()Reset is NOT supported for LWW-Registers.voidrestoreFromSnapshot(@Nullable V value, long timestamp, @Nullable String writerId) Restore a register from a persisted snapshot.voidSet the value in the register.voidsetReplicated(@Nullable V value, long timestamp, @NonNull String writerId) Apply an already-stamped value from the replicated state-machine log.Export the current register state for Raft snapshot persistence.
-
Constructor Details
-
LWWRegister
-
-
Method Details
-
set
Set the value in the register. The timestamp is set to the current time, and the writer ID is this node.- Parameters:
value- the value to set
-
setReplicated
Apply an already-stamped value from the replicated state-machine log.Raft log order is authoritative for the wire path; the leader stamps a deterministic timestamp/writer pair before submission so every replica applies identical register state. Anti-entropy conflict resolution remains in
merge(LWWRegister).- Parameters:
value- the serialized value to store, including null clearstimestamp- the leader-stamped timestamp, must be positivewriterId- the leader node id that stamped the value
-
get
Get the current value in the register.- Returns:
- the current value (null if no value has been set)
-
getTimestamp
public long getTimestamp()Get the timestamp of the current value.- Returns:
- the timestamp in milliseconds since epoch
-
isSet
public boolean isSet()Returns true once the register has observed at least one write.This distinguishes a pristine register from an explicit
set(null)write, which has a positive timestamp and null value. -
getWriterId
Get the node ID that wrote the current value.- Returns:
- the writer node ID (null if no value has been set)
-
toSnapshot
-
restoreFromSnapshot
-
merge
Merge another LWW-Register into this one (anti-entropy). The value with the higher timestamp wins. If timestamps are equal, the value from the lexicographically larger node ID wins. Merge is idempotent and commutative.- Parameters:
other- the other LWW-Register to merge- Throws:
IllegalArgumentException- if names don't match
-
reset
public void reset()Reset is NOT supported for LWW-Registers. Setting timestamp to 0 makes the reset older than any previously replicated value; the next merge will silently restore the pre-reset value.- Throws:
UnsupportedOperationException- always — reset violates CRDT merge invariants
-