vclock

package
v0.0.0-...-b0c7fd6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsEqual

func IsEqual(a, b Version) bool

IsEqual returns true if the two vectors are equal.

func ToString

func ToString(vc Version) string

ToString encodes the vector clock into a string representation that can be used for comparison. The format is as follows: {k1=v1,k2=v2,...,kn=vn} where k is the node ID and v is the counter value. The counter value is encoded using base 36.

Types

type Causality

type Causality int

Causality is a type that represents the causality relationship between two vectors.

const (
	Before Causality = iota + 1
	Concurrent
	After
	Equal
)

func Compare

func Compare(a, b Version) Causality

Compare returns the causality relationship between two vectors. Compare(a, b) == After means that a happened after b, and so on. Comparing values that have rolled over is tricky, so the implementation uses the following rules: if the clock value of a node is greater than the clock value of another node, and the rollover flags are different, it means that the value has wrapped around and we need to invert the comparison. For example, if a clock value is 2^32-1 and the other clock value is 0, and the rollover flags are different, it means that the clock value of the first node has wrapped around and the second node has not. In this case, the first node is considered to be less than the second node.

func (Causality) String

func (c Causality) String() string

type Version

type Version map[uint32]uint64

Version represents a vector clock, which is a mapping of node IDs to clock values. The clock value is a monotonically increasing counter that is incremented every time the node makes an update. The clock value is used to determine the causality relationship between two events. If the clock value of a node is greater than the clock value of another node, it means that the first event happened after the second event. The implementation uses a 32-bit integer to store the clock value. The leftmost bit is used to indicate whether the clock value has rolled over. If the leftmost bit is set, the clock value has rolled over.

func Empty

func Empty() Version

Empty returns a new version vector.

func FromString

func FromString(encoded string) (Version, error)

FromString decodes the vector clock from a string representation. The format is the same as the one used by ToString. If the string is empty, an empty vector clock is returned.

func Merge

func Merge(a, b Version) Version

Merge returns a new vector that is the result of taking the maximum value for each key in the two vectors. The operation is commutative and associative, so that Merge(a, Merge(b, c)) == Merge(Merge(a, b), c).

func MustFromString

func MustFromString(encoded string) Version

MustFromString is like FromString but panics if the string cannot be decoded.

func (Version) Copy

func (vc Version) Copy() Version

Copy returns a copy of the vector clock.

func (Version) Increment

func (vc Version) Increment(nodeID uint32)

Increment increments the clock value of the given node.

func (Version) String

func (vc Version) String() string

String returns a string representation of the vector clock. The string representation is a comma-separated list of key=value pairs, where the key is the node ID and the value is the clock value: {1=1, 2=2}. If the clock value has rolled over, the leftmost bit is set and the value is negative: {1=-1, 2=-2}.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL