Class AbstractStrongCounter

java.lang.Object
org.infinispan.counter.impl.strong.AbstractStrongCounter
All Implemented Interfaces:
org.infinispan.counter.api.StrongCounter, CounterEventGenerator, InternalCounterAdmin
Direct Known Subclasses:
BoundedStrongCounter, UnboundedStrongCounter

public abstract class AbstractStrongCounter extends Object implements org.infinispan.counter.api.StrongCounter, CounterEventGenerator, InternalCounterAdmin
A base strong consistent counter implementation.

Implementation: The value is stored in a single key and it uses the Infinispan's concurrency control and distribution to apply the write and reads. It uses the functional API.

Writes: The writes are performed by the functional API in order. The single key approach allows us to provide atomic properties for the counter value.

Reads: The reads read the value from the cache and it can go remotely.

Weak Reads: This implementation supports weak cached reads. It uses clustered listeners to receive the notifications of the actual value to store it locally.

Since:
9.0
Author:
Pedro Ruivo
  • Method Details

    • removeStrongCounter

      public static CompletionStage<Void> removeStrongCounter(org.infinispan.Cache<StrongCounterKey,CounterValue> cache, String counterName)
      It removes a strong counter from the cache, identified by the counterName.
      Parameters:
      cache - The Cache to remove the counter from.
      counterName - The counter's name.
    • init

      public final CompletionStage<InternalCounterAdmin> init()
    • asStrongCounter

      public org.infinispan.counter.api.StrongCounter asStrongCounter()
      Specified by:
      asStrongCounter in interface InternalCounterAdmin
      Returns:
      The StrongCounter instance or throws an CounterException if the counter is not a StrongCounter.
    • destroy

      public CompletionStage<Void> destroy()
      Description copied from interface: InternalCounterAdmin
      Destroys the counter.

      It drops the counter's value and all listeners registered.

      Specified by:
      destroy in interface InternalCounterAdmin
      Returns:
      A CompletionStage instance which is completed when the operation finishes.
    • value

      public CompletionStage<Long> value()
      Description copied from interface: InternalCounterAdmin
      Returns the counter's value.
      Specified by:
      value in interface InternalCounterAdmin
      Returns:
      A CompletionStage instance which is completed when the operation finishes.
    • isWeakCounter

      public boolean isWeakCounter()
      Description copied from interface: InternalCounterAdmin
      Checks if the counter is a WeakCounter.

      If true, ensures InternalCounterAdmin.asWeakCounter() never throws an CounterException. Otherwise, it ensures InternalCounterAdmin.asStrongCounter() never throws an CounterException.

      Specified by:
      isWeakCounter in interface InternalCounterAdmin
      Returns:
      true if the counter is WeakCounter.
    • getName

      public final String getName()
      Specified by:
      getName in interface org.infinispan.counter.api.StrongCounter
    • getValue

      public final CompletableFuture<Long> getValue()
      Specified by:
      getValue in interface org.infinispan.counter.api.StrongCounter
    • addAndGet

      public final CompletableFuture<Long> addAndGet(long delta)
      Specified by:
      addAndGet in interface org.infinispan.counter.api.StrongCounter
    • reset

      public final CompletableFuture<Void> reset()
      Description copied from interface: InternalCounterAdmin
      Resets the counter to its initial value.
      Specified by:
      reset in interface InternalCounterAdmin
      Specified by:
      reset in interface org.infinispan.counter.api.StrongCounter
      Returns:
      A CompletionStage instance which is completed when the operation finishes.
    • addListener

      public final <T extends org.infinispan.counter.api.CounterListener> org.infinispan.counter.api.Handle<T> addListener(T listener)
      Specified by:
      addListener in interface org.infinispan.counter.api.StrongCounter
    • compareAndSwap

      public CompletableFuture<Long> compareAndSwap(long expect, long update)
      Specified by:
      compareAndSwap in interface org.infinispan.counter.api.StrongCounter
    • getAndSet

      public CompletableFuture<Long> getAndSet(long value)
      Specified by:
      getAndSet in interface org.infinispan.counter.api.StrongCounter
    • generate

      public org.infinispan.counter.api.CounterEvent generate(CounterKey key, CounterValue value)
      Description copied from interface: CounterEventGenerator
      It generates the CounterEvent.

      The value is the new value of CounterEvent.

      Specified by:
      generate in interface CounterEventGenerator
      Parameters:
      key - The counter's key.
      value - The counter's most recent CounterValue.
      Returns:
      The CounterEvent with the updated value.
    • remove

      public CompletableFuture<Void> remove()
      Specified by:
      remove in interface org.infinispan.counter.api.StrongCounter
    • getConfiguration

      public final org.infinispan.counter.api.CounterConfiguration getConfiguration()
      Specified by:
      getConfiguration in interface org.infinispan.counter.api.StrongCounter
    • sync

      public org.infinispan.counter.api.SyncStrongCounter sync()
      Specified by:
      sync in interface org.infinispan.counter.api.StrongCounter
    • handleCASResult

      protected abstract Long handleCASResult(Object state)
    • handleAddResult

      protected abstract long handleAddResult(CounterValue counterValue)
      Extracts and validates the value after a read.

      Any exception should be thrown using CompletionException.

      Parameters:
      counterValue - The new CounterValue.
      Returns:
      The new value stored in CounterValue.
    • handleSetResult

      protected abstract Long handleSetResult(Object state)