Interface KeyValueStore<V>
- Type Parameters:
V- the value type
- All Known Implementing Classes:
InfinispanKeyValueStore,InMemoryExpectationKeyValueStore,InMemoryKeyValueStore
The in-memory implementation wraps the existing concurrent data structures
(e.g. CircularPriorityQueue for expectations, ConcurrentHashMap
for scenario state). A clustered implementation can back this with a
distributed cache while preserving identical semantics.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classA key-value entry with version metadata. -
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.
-
Method Details
-
get
Retrieves the versioned value for the given key.- Parameters:
key- the key- Returns:
- the versioned value, or empty if not present
-
put
Unconditionally 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 usecompareAndSet(java.lang.String, long, V)with the version from a priorget(java.lang.String).- Parameters:
key- the keyvalue- the value- Returns:
- the version assigned to this write
-
putIfAbsent
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 emptyOptionalis returned.This is the create-only counterpart of
put(java.lang.String, V): it never overwrites an existing entry. Callers that need last-writer-wins semantics should useput(java.lang.String, V); callers that need to detect and defer to a concurrent creator should use this method.- 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
Atomically replaces the value only if the current version matchesexpectedVersion. Returnstrueon success.- Parameters:
key- the keyexpectedVersion- the version the caller last readvalue- the new value- Returns:
- true if the swap succeeded
-
compareAndRemove
Atomically removes the entry only if the current version matchesexpectedVersion. Returnstrueon success.- Parameters:
key- the keyexpectedVersion- the version the caller last read- Returns:
- true if the removal succeeded
-
remove
Unconditionally removes the entry for the given key.- Parameters:
key- the key- Returns:
- true if the key was present
-
entries
Stream<KeyValueStore.Entry<V>> entries()Returns a stream of all entries. The iteration order is implementation-defined (unordered for a generic KV; sorted for the expectation store).- Returns:
- stream of key-versioned-value triples
-
size
int size()Returns the number of entries. -
clear
void clear()Removes all entries. -
addInvalidationListener
Adds an invalidation listener that is notified on mutations.- Parameters:
listener- the listener
-