-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
46 lines (37 loc) · 1.77 KB
/
Copy pathmain.go
File metadata and controls
46 lines (37 loc) · 1.77 KB
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
package main
import (
"fmt"
"github.com/iamseth/yin"
)
func main() {
// A CRDT (conflict-free replicated data type) is meant to have many copies
// that can be edited independently and then merged later without a central
// coordinator deciding the winner.
//
// LWWRegister is the simplest shape here: it stores exactly one value. LWW
// means "last writer wins", but in yin "last" is NOT wall-clock time. It is
// a deterministic ordering made from:
// 1. a Lamport counter, then
// 2. ReplicaID as a tie-breaker when counters are equal.
// Every independent copy/writer needs a stable logical id. This is not a
// network address; it is just the name that will be attached to local writes.
replicaID := yin.ReplicaID("replica-a")
// NewLWWRegister creates an empty register whose future local writes are
// stamped as coming from replica-a. The type parameter says this register
// stores string values.
register := yin.NewLWWRegister[string](replicaID)
// Set is a local write. The first write advances the Lamport counter from 0
// to 1 and records "replica-a" as the writer.
register.Set("hello from yin")
// Capture these once so every printed line describes the same state.
timestamp := register.Timestamp()
version := register.Version()
// Timestamp describes the winning write: who wrote it and what Lamport counter
// it received. Merge uses this ordering to decide which value wins.
fmt.Printf("value: %s\n", register.Value())
fmt.Printf("writer: %s\n", register.Writer())
fmt.Printf("timestamp counter: %d\n", timestamp.Counter())
// VersionVector is a sync cursor: "what causal progress does this document
// contain?" For this register, the current state contains replica-a's write #1.
fmt.Printf("version counter: %d\n", version.Get(replicaID))
}