Class AsyncLazyMap<K,V>
- Type Parameters:
K
- the type of keysV
- the type of values
Each key present in the cache behaves similarly to AsyncLazyValue
. The cache starts
empty. Whenever a key is requested, a computation for that key is started, but a future is
immediately returned. If the computation succeeds, the completed future is cached indefinitely,
and the result is recorded. Any subsequent requests for the same key return the same future, even
if the computation for that key has not yet completed. Thus, when it completes, all requests for
that key will be fulfilled by the result of the first request. If the computation completes
exceptionally, the key is optionally removed from the cache. Thus, a subsequent request for a
failed key may retry the computation.
Values can also be provided "out of band." That is, they may be provided by an alternative
computation. This is accomplished using get(Object, Function)
, put(Object)
or
put(Object, Object)
. The last immediately provides a value and completes any outstanding
requests, even if there was an active computation for the key. The first claims the key and
promises to provide the value at a later time.
At any point, an unmodifiable view of the completed, cached values may be obtained.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected BiPredicate
<? super K, ? super Throwable> protected BiPredicate
<? super K, ? super V> protected final Function
<K, CompletableFuture<V>> protected final Map
<K, AsyncLazyMap.KeyedFuture<K, V>> -
Constructor Summary
ConstructorsConstructorDescriptionAsyncLazyMap
(Map<K, V> map, Function<K, CompletableFuture<V>> function) Construct a lazy map for the given function -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clear the lazy map, including pending requestsboolean
containsKey
(K key) Check if a given key is in the map, pending or completedRemove a key from the map, without canceling any pending computationforgetErrors
(BiPredicate<? super K, ? super Throwable> predicate) Sets a predicate to determine which errors to forget (i.e., retry)forgetValues
(BiPredicate<? super K, ? super V> predicate) Sets a predicate to determine which values to forgetRequest the value for a given keyRequest the value for a given key, using an alternative computationGet a view of completed keys with valuesGet a copy of the keys which are requested but not completedProvide an out-of-band value for a given keyboolean
Immediately provide an out-of-band value for a given keyprotected void
putFuture
(K key, AsyncLazyMap.KeyedFuture<K, V> future) rememberErrors
(BiPredicate<? super K, ? super Throwable> predicate) Sets a predicate to determine which errors to rememberrememberValues
(BiPredicate<? super K, ? super V> predicate) Sets a predicate to determine which values to rememberRemove a key from the map, canceling any pending computationvoid
retainKeys
(Collection<K> keys) Retain only those entries whose keys appear in the given collection
-
Field Details
-
futures
-
map
-
unmodifiable
-
function
-
forgetErrors
-
forgetValues
-
-
Constructor Details
-
AsyncLazyMap
Construct a lazy map for the given function- Parameters:
map
- the backing map. The lazy map ought to have an exclusive reference to this map. Mutations to the map outside of those caused by the lazy map may cause undefined behavior.function
- specifies the computation, given a key
-
-
Method Details
-
putFuture
-
forgetErrors
Sets a predicate to determine which errors to forget (i.e., retry)A request resulting in an error that is remembered will not be retried until the cache is invalidated. For a forgotten error, the request is retried if re-requested later.
This will replace the behavior of any previous error-testing predicate.
- Parameters:
predicate
- the predicate- Returns:
- this lazy map
-
rememberErrors
Sets a predicate to determine which errors to remember- Parameters:
predicate
- the predicate- Returns:
- this lazy map
- See Also:
-
forgetValues
Sets a predicate to determine which values to forgetThe predicate is applied to a cached entry when its key is re-requested. If forgotten, the request will launch a fresh computation. The predicate is also applied at the time a computation is completed. An entry that is forgotten still completes normally; however, it never enters the cache, thus a subsequent request for the same key will launch a fresh computation.
This will replace the behavior of any previous value-testing predicate.
- Parameters:
predicate
- the rule for forgetting entries- Returns:
- this lazy map
-
rememberValues
Sets a predicate to determine which values to remember- Parameters:
predicate
- the rule for not forgetting entries- Returns:
- this lazy map
- See Also:
-
get
Request the value for a given key, using an alternative computationIf this is called before any other get or put, the given function is launched for the given key. A
CompletableFuture
is returned immediately. Subsequent gets or puts on the same key will return the same future without starting any new computation.- Parameters:
key
- the keyfunc
- an alternative computation function, given a key- Returns:
- a future, possibly already completed, for the key's value
-
get
Request the value for a given keyIf this is called before any other get or put, the computation given at construction is launched for the given key. A
CompletableFuture
is returned immediately. Subsequent calls gets or puts on the same key return the same future without starting any new computation.- Parameters:
key
- the key- Returns:
- a future, possible already completed, for the key's value
-
put
Immediately provide an out-of-band value for a given keyOn occasion, the value for a key may become known outside of the specified computation. This method circumvents the function given during construction by providing the value for a key. If there is an outstanding request for the key's value -- a rare occasion -- it is completed immediately with the provided value. Calling this method for a key that has already completed has no effect.
This is equivalent to the code
map.put(k).complete(value)
, but atomic.- Parameters:
key
- the key whose value to providevalue
- the provided value- Returns:
- true if the key was completed by this call, false if the key had already been completed
-
put
Provide an out-of-band value for a given keyIf this is called before
get(Object)
, the computation given at construction is ignored for the given key. A newCompletableFuture
is returned instead. The caller must see to this future's completion. Subsequent calls to eitherget(Object)
orput(Object)
on the same key return this same future without starting any computation.Under normal circumstances, the caller cannot determine whether or not it has "claimed" the computation for the key. If the usual computation is already running, then the computations are essentially in a race. As such, it is essential that alternative computations result in the same value for a given key as the usual computation. In other words, the functions must not differ, but the means of computation can differ. Otherwise, race conditions may arise.
- Parameters:
key
- the key whose value to provide- Returns:
- a promise that the caller must fulfill or arrange to have fulfilled
-
forget
Remove a key from the map, without canceling any pending computationIf the removed future has not yet completed, its value will never be added to the map of values. Subsequent gets or puts to the invalidated key will behave as if the key had never been requested.
- Parameters:
key
- the key to remove- Returns:
- the invalidated future
-
remove
Remove a key from the map, canceling any pending computation- Parameters:
key
- the key to remove- Returns:
- the previous value, if completed
-
getCompletedMap
Get a view of completed keys with valuesThe view is unmodifiable, but the backing map may still be modified as more keys are completed. Thus, access to the view ought to be synchronized on this lazy map.
- Returns:
- a map view of keys to values
-
getPendingKeySet
Get a copy of the keys which are requested but not completedThis should only be used for diagnostics.
- Returns:
- a copy of the pending key set
-
clear
public void clear()Clear the lazy map, including pending requestsPending requests will be cancelled
-
retainKeys
Retain only those entries whose keys appear in the given collectionAll removed entries with pending computations will be canceled
- Parameters:
keys
- the keys to retain
-
containsKey
Check if a given key is in the map, pending or completed- Parameters:
key
- the key to check- Returns:
- true if present, false otherwise
-