Struct ditto::counter::Counter
[−]
[src]
pub struct Counter { /* fields omitted */ }
A Counter is an i64
value that can be incremented and
decremented via the increment
function.
Internally, Counter is a variant of GCounter that allows
op-based replication via execute_op
and state-based replication via merge
. Both
replication methods are idempotent and can handle out-of-order
delivery.
Counter
has a spatial complexity of O(S), where
S is the number of sites that have incremented the Counter
.
It has the following performance characteristics:
increment
: O(1)execute_op
: O(1)merge
: O(S), where S is the number of sites that have incremented theCounter
Methods
impl Counter
[src]
pub fn new(value: i64) -> Self
[src]
Constructs and returns a new Counter with site id 1.
pub fn get(&self) -> i64
[src]
Returns the counter value.
pub fn increment(&mut self, amount: i64) -> Result<Op, Error>
[src]
Increments the counter by the given amount and
returns an op that can be replicated to other sites.
If the counter does not have a site id, it caches the
op and returns an AwaitingSiteId
error.
pub fn site_id(&self) -> SiteId
[src]
Returns the Counter
's site id.
pub fn state(&self) -> CounterState
[src]
Returns a reference to the Counter
state.
pub fn clone_state(&self) -> CounterState<'static>
[src]
Clones and returns the Counter
state.
pub fn into_state(self) -> CounterState<'static>
[src]
Consumes the Counter
and returns its state.
pub fn from_state(
state: CounterState,
site_id: Option<SiteId>
) -> Result<Self, Error>
[src]
state: CounterState,
site_id: Option<SiteId>
) -> Result<Self, Error>
Constructs a new Counter
from a state and optional site id.
If the site is given, it must be nonzero.
pub fn execute_op(&mut self, op: &Op) -> Option<i64>
[src]
Executes an Op and returns the equivalent increment. If the op has already been executed or superceded, nothing is done.
pub fn validate_and_execute_op(
&mut self,
op: &Op,
site_id: SiteId
) -> Result<Option<i64>, Error>
[src]
&mut self,
op: &Op,
site_id: SiteId
) -> Result<Option<i64>, Error>
Validates that an op comes from a specific site id, then executes the op.
pub fn merge(&mut self, other: CounterState)
[src]
Merges remote state into the Counter.
pub fn add_site_id(&mut self, site_id: SiteId) -> Result<Option<Op>, Error>
[src]
Assigns a site id and returns a cached op if it exists.
Trait Implementations
impl Debug for Counter
[src]
fn fmt(&self, __arg_0: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl Clone for Counter
[src]
fn clone(&self) -> Counter
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more