Documentation
¶
Overview ¶
Package counter implements counter CRDT primitives.
Index ¶
- Variables
- type GCounter
- func (c *GCounter) ApplyDelta(delta GCounterDelta) error
- func (c *GCounter) Counts() map[string]uint64
- func (c *GCounter) Increment(amount uint64) (GCounterDelta, error)
- func (c *GCounter) MarshalBinary() ([]byte, error)
- func (c *GCounter) Merge(other *GCounter) error
- func (c *GCounter) State() crdt.StateSnapshot
- func (c *GCounter) UnmarshalBinary(data []byte) error
- func (c *GCounter) UnmarshalBinaryWithLimits(data []byte, limits frame.DecoderLimits) error
- func (c *GCounter) Value() (uint64, error)
- type GCounterDelta
- type PNCounter
- func (c *PNCounter) ApplyDelta(delta PNCounterDelta) error
- func (c *PNCounter) Decrement(amount uint64) (PNCounterDelta, error)
- func (c *PNCounter) Increment(amount uint64) (PNCounterDelta, error)
- func (c *PNCounter) MarshalBinary() ([]byte, error)
- func (c *PNCounter) Merge(other *PNCounter) error
- func (c *PNCounter) NegativeCounts() map[string]uint64
- func (c *PNCounter) PositiveCounts() map[string]uint64
- func (c *PNCounter) State() crdt.StateSnapshot
- func (c *PNCounter) UnmarshalBinary(data []byte) error
- func (c *PNCounter) UnmarshalBinaryWithLimits(data []byte, limits frame.DecoderLimits) error
- func (c *PNCounter) Value() (*big.Int, error)
- func (c *PNCounter) ValueInt64() (int64, error)
- type PNCounterDelta
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidReplicaID indicates that a counter or delta contains an invalid // logical replica identifier. ErrInvalidReplicaID = errors.New("counter: invalid replica ID") // ErrNilCounter indicates an operation received a nil counter. ErrNilCounter = errors.New("counter: nil G-Counter") // ErrCounterOverflow indicates a local component, aggregate, or fixed-width // counter value would overflow. ErrCounterOverflow = errors.New("counter: value overflows uint64") )
var ( // ErrNilPNCounter indicates an operation received a nil PN-Counter. ErrNilPNCounter = errors.New("counter: nil PN-Counter") )
Functions ¶
This section is empty.
Types ¶
type GCounter ¶
type GCounter struct {
// contains filtered or unexported fields
}
GCounter is a grow-only, state-based counter. Each replica owns one map entry, and Merge takes the maximum value for every replica ID.
func NewGCounter ¶
NewGCounter creates a G-Counter owned by replicaID.
func (*GCounter) ApplyDelta ¶
func (c *GCounter) ApplyDelta(delta GCounterDelta) error
ApplyDelta joins delta into c.
func (*GCounter) Increment ¶
func (c *GCounter) Increment(amount uint64) (GCounterDelta, error)
Increment adds amount to this counter's local component and returns a delta that represents the new component value.
func (*GCounter) MarshalBinary ¶
MarshalBinary returns a deterministic framed representation of c.
func (*GCounter) Merge ¶
Merge joins other into c. It copies other before taking c's write lock so concurrent cross-merges cannot deadlock.
func (*GCounter) State ¶
func (c *GCounter) State() crdt.StateSnapshot
State returns an immutable diagnostic summary.
func (*GCounter) UnmarshalBinary ¶
UnmarshalBinary validates data completely before atomically replacing c's state.
func (*GCounter) UnmarshalBinaryWithLimits ¶
func (c *GCounter) UnmarshalBinaryWithLimits(data []byte, limits frame.DecoderLimits) error
UnmarshalBinaryWithLimits validates data completely before atomically replacing c's state using caller-supplied decoder limits.
type GCounterDelta ¶
type GCounterDelta struct {
// contains filtered or unexported fields
}
GCounterDelta is a joinable partial GCounter state. Its contents are opaque; Counts returns a copy for diagnostics or a future transport encoder.
func UnmarshalGCounterDelta ¶
func UnmarshalGCounterDelta(data []byte) (GCounterDelta, error)
UnmarshalGCounterDelta validates and returns one G-Counter delta frame.
func UnmarshalGCounterDeltaWithLimits ¶
func UnmarshalGCounterDeltaWithLimits(data []byte, limits frame.DecoderLimits) (GCounterDelta, error)
UnmarshalGCounterDeltaWithLimits validates and returns one G-Counter delta frame using caller-supplied decoder limits.
func (GCounterDelta) Counts ¶
func (d GCounterDelta) Counts() map[string]uint64
Counts returns a copy of the components represented by d.
func (GCounterDelta) MarshalBinary ¶
func (d GCounterDelta) MarshalBinary() ([]byte, error)
MarshalBinary returns a deterministic framed representation of d.
func (GCounterDelta) Merge ¶
func (d GCounterDelta) Merge(other GCounterDelta) (GCounterDelta, error)
Merge joins other into d and returns a new delta without modifying either input delta.
type PNCounter ¶
type PNCounter struct {
// contains filtered or unexported fields
}
PNCounter is a state-based positive-negative counter. Each component is a per-replica G-Counter: positive records increments and negative records decrements. Merge takes the maximum component value in both maps.
func NewPNCounter ¶
NewPNCounter creates a PN-Counter owned by replicaID.
func (*PNCounter) ApplyDelta ¶
func (c *PNCounter) ApplyDelta(delta PNCounterDelta) error
ApplyDelta joins delta into c.
func (*PNCounter) Decrement ¶
func (c *PNCounter) Decrement(amount uint64) (PNCounterDelta, error)
Decrement adds amount to this replica's negative component and returns the resulting component as a delta.
func (*PNCounter) Increment ¶
func (c *PNCounter) Increment(amount uint64) (PNCounterDelta, error)
Increment adds amount to this replica's positive component and returns the resulting component as a delta.
func (*PNCounter) MarshalBinary ¶
MarshalBinary returns a deterministic framed representation of c.
func (*PNCounter) Merge ¶
Merge joins other into c. It snapshots other before taking c's write lock so concurrent cross-merges cannot deadlock.
func (*PNCounter) NegativeCounts ¶
NegativeCounts returns a copy of the per-replica decrement components.
func (*PNCounter) PositiveCounts ¶
PositiveCounts returns a copy of the per-replica increment components.
func (*PNCounter) State ¶
func (c *PNCounter) State() crdt.StateSnapshot
State returns an immutable diagnostic summary.
func (*PNCounter) UnmarshalBinary ¶
UnmarshalBinary validates data completely before atomically replacing c's state.
func (*PNCounter) UnmarshalBinaryWithLimits ¶
func (c *PNCounter) UnmarshalBinaryWithLimits(data []byte, limits frame.DecoderLimits) error
UnmarshalBinaryWithLimits validates data completely before atomically replacing c's state using caller-supplied decoder limits.
func (*PNCounter) Value ¶
Value returns the exact signed counter value as a newly allocated integer. It uses big.Int so every valid uint64 component state remains representable.
func (*PNCounter) ValueInt64 ¶
ValueInt64 returns the signed value when it fits in int64.
type PNCounterDelta ¶
type PNCounterDelta struct {
// contains filtered or unexported fields
}
PNCounterDelta is a joinable partial PN-Counter state. Its contents are opaque; the count accessors return copies for diagnostics and transport.
func UnmarshalPNCounterDelta ¶
func UnmarshalPNCounterDelta(data []byte) (PNCounterDelta, error)
UnmarshalPNCounterDelta validates and returns one PN-Counter delta frame.
func UnmarshalPNCounterDeltaWithLimits ¶
func UnmarshalPNCounterDeltaWithLimits(data []byte, limits frame.DecoderLimits) (PNCounterDelta, error)
UnmarshalPNCounterDeltaWithLimits validates and returns one PN-Counter delta frame using caller-supplied decoder limits.
func (PNCounterDelta) MarshalBinary ¶
func (d PNCounterDelta) MarshalBinary() ([]byte, error)
MarshalBinary returns a deterministic framed representation of d.
func (PNCounterDelta) Merge ¶
func (d PNCounterDelta) Merge(other PNCounterDelta) (PNCounterDelta, error)
Merge joins other into d and returns a new delta without modifying either input delta.
func (PNCounterDelta) NegativeCounts ¶
func (d PNCounterDelta) NegativeCounts() map[string]uint64
NegativeCounts returns a copy of the decrement components represented by d.
func (PNCounterDelta) PositiveCounts ¶
func (d PNCounterDelta) PositiveCounts() map[string]uint64
PositiveCounts returns a copy of the increment components represented by d.