Class InfinispanKeyValueStore<V>

java.lang.Object
org.mockserver.state.infinispan.InfinispanKeyValueStore<V>
Type Parameters:
V - the value type
All Implemented Interfaces:
KeyValueStore<V>

public class InfinispanKeyValueStore<V> extends Object implements KeyValueStore<V>
KeyValueStore backed by an Infinispan Cache. Supports both LOCAL (non-clustered) and REPL_SYNC (clustered) cache modes. Versioning is managed explicitly via a VersionedWrapper stored as the cache value.

CAS (compareAndSet(java.lang.String, long, V)) uses cache.replace(key, oldValue, newValue) which is atomic in both LOCAL and clustered modes. The previous cache.compute()-based CAS approach was unsafe in REPL_SYNC mode because Infinispan may re-execute the compute lambda on retry/conflict, causing a side-channel AtomicBoolean to be overwritten by the retry invocation.

  • Constructor Details

  • Method Details

    • get

      public Optional<Versioned<V>> get(String key)
      Description copied from interface: KeyValueStore
      Retrieves the versioned value for the given key.
      Specified by:
      get in interface KeyValueStore<V>
      Parameters:
      key - the key
      Returns:
      the versioned value, or empty if not present
    • put

      public long put(String key, V value)
      Description copied from interface: KeyValueStore
      Unconditionally puts a value, creating or replacing any existing entry. Returns the new version.

      Semantics are last-writer-wins: concurrent put calls for the same key race and the final value is whichever write lands last (mirroring ConcurrentHashMap). Callers that need to detect/avoid lost updates must use KeyValueStore.compareAndSet(java.lang.String, long, V) with the version from a prior KeyValueStore.get(java.lang.String).

      Specified by:
      put in interface KeyValueStore<V>
      Parameters:
      key - the key
      value - the value
      Returns:
      the version assigned to this write
    • putIfAbsent

      public Optional<Versioned<V>> putIfAbsent(String key, V value)
      Description copied from interface: KeyValueStore
      Atomically inserts the value only if no entry for the key exists yet. If the key is already present, the store is not modified and the existing versioned value is returned. If insertion succeeds, an empty Optional is returned.

      This is the create-only counterpart of KeyValueStore.put(java.lang.String, V): it never overwrites an existing entry. Callers that need last-writer-wins semantics should use KeyValueStore.put(java.lang.String, V); callers that need to detect and defer to a concurrent creator should use this method.

      Specified by:
      putIfAbsent in interface KeyValueStore<V>
      Parameters:
      key - the key
      value - the value to insert
      Returns:
      empty if the entry was created by this call; otherwise the existing versioned value that was already present
    • compareAndSet

      public boolean compareAndSet(String key, long expectedVersion, V value)
      Description copied from interface: KeyValueStore
      Atomically replaces the value only if the current version matches expectedVersion. Returns true on success.
      Specified by:
      compareAndSet in interface KeyValueStore<V>
      Parameters:
      key - the key
      expectedVersion - the version the caller last read
      value - the new value
      Returns:
      true if the swap succeeded
    • compareAndRemove

      public boolean compareAndRemove(String key, long expectedVersion)
      Description copied from interface: KeyValueStore
      Atomically removes the entry only if the current version matches expectedVersion. Returns true on success.
      Specified by:
      compareAndRemove in interface KeyValueStore<V>
      Parameters:
      key - the key
      expectedVersion - the version the caller last read
      Returns:
      true if the removal succeeded
    • remove

      public boolean remove(String key)
      Description copied from interface: KeyValueStore
      Unconditionally removes the entry for the given key.
      Specified by:
      remove in interface KeyValueStore<V>
      Parameters:
      key - the key
      Returns:
      true if the key was present
    • entries

      public Stream<KeyValueStore.Entry<V>> entries()
      Description copied from interface: KeyValueStore
      Returns a stream of all entries. The iteration order is implementation-defined (unordered for a generic KV; sorted for the expectation store).
      Specified by:
      entries in interface KeyValueStore<V>
      Returns:
      stream of key-versioned-value triples
    • size

      public int size()
      Description copied from interface: KeyValueStore
      Returns the number of entries.
      Specified by:
      size in interface KeyValueStore<V>
    • clear

      public void clear()
      Description copied from interface: KeyValueStore
      Removes all entries.
      Specified by:
      clear in interface KeyValueStore<V>
    • addInvalidationListener

      public void addInvalidationListener(InvalidationListener listener)
      Description copied from interface: KeyValueStore
      Adds an invalidation listener that is notified on mutations.
      Specified by:
      addInvalidationListener in interface KeyValueStore<V>
      Parameters:
      listener - the listener