Class WeakCounterImpl

  • All Implemented Interfaces:
    org.infinispan.counter.api.WeakCounter, CounterEventGenerator, TopologyChangeListener, InternalCounterAdmin

    public class WeakCounterImpl
    extends Object
    implements org.infinispan.counter.api.WeakCounter, CounterEventGenerator, TopologyChangeListener, InternalCounterAdmin
    A weak consistent counter implementation.

    Implementation: The counter is split in multiple keys and they are stored in the cache.

    Write: A write operation will pick a key to update. If the node is a primary owner of one of the key, that key is chosen based on thread-id. This will take advantage of faster write operations. If the node is not a primary owner, one of the key in key set is chosen.

    Read: A read operation needs to read all the key set (including the remote keys). This is slower than atomic counter.

    Weak Read: A snapshot of all the keys values is kept locally and they are updated via cluster listeners.

    Reset: The reset operation is not atomic and intermediate results may be observed.

    Since:
    9.0
    Author:
    Pedro Ruivo
    • Method Detail

      • removeWeakCounter

        public static CompletionStage<Void> removeWeakCounter​(org.infinispan.Cache<WeakCounterKey,​CounterValue> cache,
                                                              org.infinispan.counter.api.CounterConfiguration configuration,
                                                              String counterName)
        It removes a weak counter from the cache, identified by the counterName.
        Parameters:
        cache - The Cache to remove the counter from.
        configuration - The counter's configuration.
        counterName - The counter's name.
      • getName

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

        public long getValue()
        Specified by:
        getValue in interface org.infinispan.counter.api.WeakCounter
      • add

        public CompletableFuture<Void> add​(long delta)
        Specified by:
        add in interface org.infinispan.counter.api.WeakCounter
      • asWeakCounter

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

        public <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.WeakCounter
      • getConfiguration

        public org.infinispan.counter.api.CounterConfiguration getConfiguration()
        Specified by:
        getConfiguration in interface org.infinispan.counter.api.WeakCounter
      • 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.WeakCounter
      • sync

        public org.infinispan.counter.api.SyncWeakCounter sync()
        Specified by:
        sync in interface org.infinispan.counter.api.WeakCounter
      • getPreferredKeys

        public WeakCounterKey[] getPreferredKeys()
        Debug only!