PNCounter

class PNCounter : DeltaCRDT

A delta-based CRDT PN-counter.

Initialized to 0, it can be concurrently incremented and decremented.

For each replica, both sum of increments and decrements performed on the replica are retained as grow-only counters. The PN-Counter value is computed as the difference between increments and decrements of all replicas.

When merging, the maximum value of every increment and decrement counter is retained (following grow-only semantic).

Its JSON serialization respects the following schema:

{
"type": "PNCounter",
"metadata": {
"increment": [
(( ClientUId.toJson(), {
"first": $value, // $value is an integer
"second": Timestamp.toJson()
}, )*( ClientUId.toJson(), {
"first": $value, // $value is an integer
"second": Timestamp.toJson()
} ))?
],
"decrement": [
(( ClientUId.toJson(), {
"first": $value, // $value is an integer
"second": Timestamp.toJson()
}, )*( ClientUId.toJson(), {
"first": $value, // $value is an integer
"second": Timestamp.toJson()
} ))?
]
},
"value": $value // $value is an integer
}

Constructors

PNCounter
Link copied to clipboard
common
fun PNCounter()
Default constructor.
PNCounter
Link copied to clipboard
common
fun PNCounter(env: Environment)

Types

Companion
Link copied to clipboard
common
object Companion

Functions

decrement
Link copied to clipboard
common
fun decrement(amount: Int): PNCounter
Decrements the counter by the given amount.
equals
Link copied to clipboard
common
open operator fun equals(other: Any?): Boolean
generateDelta
Link copied to clipboard
common
open override fun generateDelta(vv: VersionVector): PNCounter
Return a delta from a given version vector vv to current state.
get
Link copied to clipboard
common
fun get(): Int
Gets the value of the counter.
hashCode
Link copied to clipboard
common
open fun hashCode(): Int
increment
Link copied to clipboard
common
fun increment(amount: Int): PNCounter
Increments the counter by the given amount.
merge
Link copied to clipboard
common
open override fun merge(delta: DeltaCRDT)
Merge a given delta into this DeltaCRDTThe merge is unidirectional: only the local replica is modified.
toJson
Link copied to clipboard
common
open override fun toJson(): String
Serialize this DeltaCRDT to a JSON string.
toString
Link copied to clipboard
common
open fun toString(): String