Class ConcurrentMapStateRepository<V>

java.lang.Object
de.otto.synapse.state.ConcurrentMapStateRepository<V>
Type Parameters:
V -
All Implemented Interfaces:
StateRepository<V>, AutoCloseable
Direct Known Subclasses:
ChronicleMapStateRepository

public class ConcurrentMapStateRepository<V> extends Object implements StateRepository<V>
High performance in-memory StateRepository implemented using a ConcurrentMap.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a StateRepository with the given name, that is using a ConcurrentHashMap to store event-sourced entities.
    Creates a StateRepository with the given name, that is using the given ConcurrentMap to store event-sourced entities.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all of the mappings from this repository (optional operation).
    void
    Closes the StateRepository.
    compute(String key, BiFunction<? super String,? super Optional<V>,? extends V> remappingFunction)
    Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
    void
    consumeAll(BiConsumer<? super String,? super V> consumer)
    Computes each entry within the repository.
    get(String key)
    Returns the optional value to which the specified key is mapped, or Optional.empty() if this repository contains no mapping for the key.
    The unique name of the StateRepository.
    Returns an immutable set of the keys in this repository.
    put(String key, V value)
    Associates the specified value with the specified key in this repository.
    Removes the mapping for a key from this repository if it is present.
    long
    Returns the number of key-value mappings in this repository.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ConcurrentMapStateRepository

      public ConcurrentMapStateRepository(String name)
      Creates a StateRepository with the given name, that is using a ConcurrentHashMap to store event-sourced entities.
      Parameters:
      name - the name of the repository.
    • ConcurrentMapStateRepository

      public ConcurrentMapStateRepository(String name, ConcurrentMap<String,V> map)
      Creates a StateRepository with the given name, that is using the given ConcurrentMap to store event-sourced entities.

      Usable to create StateRepository instances from ConcurrentMap implementations like, for example, ChronicleMap

      Parameters:
      name - the name of the repository.
      map - the delegate map used to store the entity state
  • Method Details

    • getName

      public String getName()
      Description copied from interface: StateRepository
      The unique name of the StateRepository.

      Using the same name for multiple repositories will lead to

      Specified by:
      getName in interface StateRepository<V>
      Returns:
      name
    • compute

      public Optional<V> compute(String key, BiFunction<? super String,? super Optional<V>,? extends V> remappingFunction)
      Description copied from interface: StateRepository
      Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). For example, to either create or append a String msg to a value mapping:
       
       repository.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg))

      If the function returns null, the mapping is removed (or remains absent if initially absent). If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.

      The default implementation is equivalent to performing the following steps for this repository, then returning the current value or null if absent:

       
       OptionalV> oldValue = repository.get(key);
       V newValue = remappingFunction.apply(key, oldValue);
       if (!oldValue.isEmpty()) {
          if (newValue != null)
             repository.put(key, newValue);
          else
             repository.remove(key);
       } else {
          if (newValue != null)
             repository.put(key, newValue);
          else
             return Optional.empty();
       }
       
      Specified by:
      compute in interface StateRepository<V>
      Parameters:
      key - key with which the specified value is to be associated
      remappingFunction - the function to compute a value
      Returns:
      the Optional containing the new value associated with the specified key, or Optional.empty() if none
    • consumeAll

      public void consumeAll(BiConsumer<? super String,? super V> consumer)
      Description copied from interface: StateRepository
      Computes each entry within the repository.
      Specified by:
      consumeAll in interface StateRepository<V>
      Parameters:
      consumer - the consumer that will be applied on each repository entry
    • put

      public Optional<V> put(String key, V value)
      Description copied from interface: StateRepository
      Associates the specified value with the specified key in this repository. If the repository previously contained a mapping for the key, the old value is replaced by the specified value. (A repository m is said to contain a mapping for a key k if and only if m.get(k) would return a non-empty value.)
      Specified by:
      put in interface StateRepository<V>
      Parameters:
      key - key with which the specified value is to be associated
      value - value to be associated with the specified key
      Returns:
      the previous value associated with 'key', or 'Optional.empty()' if there was no mapping for 'key'
    • remove

      public Optional<V> remove(String key)
      Description copied from interface: StateRepository
      Removes the mapping for a key from this repository if it is present. More formally, if this repository contains a mapping from key 'k' to value 'v' such that (key==null ? k==null : key.equals(k)), that mapping is removed. (The repository can contain at most one such mapping.)

      Returns the optional value to which this repository previously associated the key, or 'Optional.empty()' if the repository contained no mapping for the key.

      The repository will not contain a mapping for the specified key once the call returns.

      Specified by:
      remove in interface StateRepository<V>
      Parameters:
      key - key whose mapping is to be removed from the repository
      Returns:
      the optional previous value associated with 'key', or 'Optional.empty()' if there was no mapping for 'key'.
    • clear

      public void clear()
      Description copied from interface: StateRepository
      Removes all of the mappings from this repository (optional operation). The repository will be empty after this call returns.
      Specified by:
      clear in interface StateRepository<V>
    • get

      public Optional<V> get(String key)
      Description copied from interface: StateRepository
      Returns the optional value to which the specified key is mapped, or Optional.empty() if this repository contains no mapping for the key.

      More formally, if this repository contains a mapping from a key k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns Optional.of(v); otherwise it returns Optional.empty(). (There can be at most one such mapping.)

      Specified by:
      get in interface StateRepository<V>
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      the Optional containing the value to which the specified key is mapped, or Optional.empty() if this repository contains no mapping for the key
    • keySet

      public Set<String> keySet()
      Description copied from interface: StateRepository
      Returns an immutable set of the keys in this repository.
      Specified by:
      keySet in interface StateRepository<V>
      Returns:
      a set view of the keys contained in this repository
    • size

      public long size()
      Description copied from interface: StateRepository
      Returns the number of key-value mappings in this repository. If the repository contains more than 'Integer.MAX_VALUE' elements, returns 'Integer.MAX_VALUE'.
      Specified by:
      size in interface StateRepository<V>
      Returns:
      the number of key-value mappings in this repository
    • close

      public void close() throws Exception
      Description copied from interface: StateRepository
      Closes the StateRepository.

      Depending on the implementation of the interface, it might be required to close() the repository on shutdown in order to prevent data loss.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface StateRepository<V>
      Throws:
      Exception - if closing the repository fails for some reason.