Package org.mockserver.state
Class InMemoryKeyValueStore<V>
java.lang.Object
org.mockserver.state.InMemoryKeyValueStore<V>
- Type Parameters:
V- the value type
- All Implemented Interfaces:
KeyValueStore<V>
General-purpose in-memory
KeyValueStore backed by a
ConcurrentHashMap. Suitable for scenario states, CRUD entities,
and any unordered KV needs. NOT used for expectations (which need
InMemoryExpectationKeyValueStore for ordering/eviction).-
Nested Class Summary
Nested classes/interfaces inherited from interface org.mockserver.state.KeyValueStore
KeyValueStore.Entry<V> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddInvalidationListener(InvalidationListener listener) Adds an invalidation listener that is notified on mutations.voidclear()Removes all entries.booleancompareAndRemove(String key, long expectedVersion) Atomically removes the entry only if the current version matchesexpectedVersion.booleancompareAndSet(String key, long expectedVersion, V value) Atomically replaces the value only if the current version matchesexpectedVersion.entries()Returns a stream of all entries.Retrieves the versioned value for the given key.longUnconditionally puts a value, creating or replacing any existing entry.putIfAbsent(String key, V value) Atomically inserts the value only if no entry for the key exists yet.booleanUnconditionally removes the entry for the given key.intsize()Returns the number of entries.
-
Constructor Details
-
InMemoryKeyValueStore
public InMemoryKeyValueStore()
-
-
Method Details
-
get
Description copied from interface:KeyValueStoreRetrieves the versioned value for the given key.- Specified by:
getin interfaceKeyValueStore<V>- Parameters:
key- the key- Returns:
- the versioned value, or empty if not present
-
put
Description copied from interface:KeyValueStoreUnconditionally puts a value, creating or replacing any existing entry. Returns the new version.Semantics are last-writer-wins: concurrent
putcalls for the same key race and the final value is whichever write lands last (mirroringConcurrentHashMap). Callers that need to detect/avoid lost updates must useKeyValueStore.compareAndSet(java.lang.String, long, V)with the version from a priorKeyValueStore.get(java.lang.String).- Specified by:
putin interfaceKeyValueStore<V>- Parameters:
key- the keyvalue- the value- Returns:
- the version assigned to this write
-
putIfAbsent
Description copied from interface:KeyValueStoreAtomically 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 emptyOptionalis 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 useKeyValueStore.put(java.lang.String, V); callers that need to detect and defer to a concurrent creator should use this method.- Specified by:
putIfAbsentin interfaceKeyValueStore<V>- Parameters:
key- the keyvalue- the value to insert- Returns:
- empty if the entry was created by this call; otherwise the existing versioned value that was already present
-
compareAndSet
Description copied from interface:KeyValueStoreAtomically replaces the value only if the current version matchesexpectedVersion. Returnstrueon success.- Specified by:
compareAndSetin interfaceKeyValueStore<V>- Parameters:
key- the keyexpectedVersion- the version the caller last readvalue- the new value- Returns:
- true if the swap succeeded
-
compareAndRemove
Description copied from interface:KeyValueStoreAtomically removes the entry only if the current version matchesexpectedVersion. Returnstrueon success.- Specified by:
compareAndRemovein interfaceKeyValueStore<V>- Parameters:
key- the keyexpectedVersion- the version the caller last read- Returns:
- true if the removal succeeded
-
remove
Description copied from interface:KeyValueStoreUnconditionally removes the entry for the given key.- Specified by:
removein interfaceKeyValueStore<V>- Parameters:
key- the key- Returns:
- true if the key was present
-
entries
Description copied from interface:KeyValueStoreReturns a stream of all entries. The iteration order is implementation-defined (unordered for a generic KV; sorted for the expectation store).- Specified by:
entriesin interfaceKeyValueStore<V>- Returns:
- stream of key-versioned-value triples
-
size
public int size()Description copied from interface:KeyValueStoreReturns the number of entries.- Specified by:
sizein interfaceKeyValueStore<V>
-
clear
public void clear()Description copied from interface:KeyValueStoreRemoves all entries.- Specified by:
clearin interfaceKeyValueStore<V>
-
addInvalidationListener
Description copied from interface:KeyValueStoreAdds an invalidation listener that is notified on mutations.- Specified by:
addInvalidationListenerin interfaceKeyValueStore<V>- Parameters:
listener- the listener
-