Class KeyWatchTracker
- All Implemented Interfaces:
Closeable
,AutoCloseable
To limit memory consumption, instances are configured with a maximum maximum number of key watches supported, as well as a maximum lifetime for each key watch. When these limits are exceeded, one or more key watches is evicted and a corresponding spurious notification occurs.
Instances can optionally be configured to only weakly reference the returned ListenableFuture
's.
This prevents memory leaks if the user of this class is sloppy and fails to cancel()
them when no longer needed; however, it can also lead to missed notifications if the user of this class relies
on the listener registration functionality provided in the
ListenableFuture
interface, because with a listener registration, there is no longer any need
to directly reference the ListenableFuture
, and so it may be reclaimed before firing. Therefore,
by default strong references are used.
In any case, ListenableFuture
notifications are performed on a separate dedicated notification
thread to avoid re-entrancy issues.
For space efficiency, this class does not track the original values associated with a key. Therefore, spurious notifications can also occur if a value is changed, and then changed back to its original value.
Instances are thread safe.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final long
Default capacity (10000L).static final long
Default maximum lifetime in seconds (2592000L).static final boolean
Default for the weak reference option (false). -
Constructor Summary
ConstructorDescriptionDefault constructor.KeyWatchTracker
(long capacity, long maxLifetime, boolean weakReferences) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
absorb
(KeyWatchTracker that) Absorb all of the watches from the given instance into this one.void
close()
Close this instance.void
Discard all outstanding key watches and fail them with the given exception.int
Count the number of keys being watched.register
(byte[] key) Register a new watch.boolean
trigger
(byte[] key) Trigger all watches associated with the given key.boolean
Trigger all watches associated with keys in the given range.boolean
Trigger all watches associated with the given mutations.boolean
Trigger all watches associated with the given keys.boolean
Trigger all watches.
-
Field Details
-
DEFAULT_CAPACITY
public static final long DEFAULT_CAPACITYDefault capacity (10000L).- See Also:
-
DEFAULT_MAXIMUM_LIFETIME
public static final long DEFAULT_MAXIMUM_LIFETIMEDefault maximum lifetime in seconds (2592000L).- See Also:
-
DEFAULT_WEAK_REFERENCE
public static final boolean DEFAULT_WEAK_REFERENCEDefault for the weak reference option (false).- See Also:
-
-
Constructor Details
-
KeyWatchTracker
public KeyWatchTracker() -
KeyWatchTracker
public KeyWatchTracker(long capacity, long maxLifetime, boolean weakReferences) Constructor.- Parameters:
capacity
- maximum number of key watches allowedmaxLifetime
- maximum lifetime for a key watch in secondsweakReferences
- true to only weakly reference registeredListenableFuture
s- Throws:
IllegalArgumentException
- ifcapacity
ormaxLifetime
is zero or negative
-
-
Method Details
-
register
Register a new watch.If the returned
Future
iscancel()
'ed, the watch is automatically unregistered.- Parameters:
key
- the key to watch- Returns:
- a
ListenableFuture
that returnskey
when the value associated withkey
is modified - Throws:
IllegalArgumentException
- ifkey
is null
-
getNumKeysWatched
public int getNumKeysWatched()Count the number of keys being watched.Note that the same key can be watched more than once, so this only counts keys being watched, not total watches.
- Returns:
- number of keys being watched
-
trigger
public boolean trigger(byte[] key) Trigger all watches associated with the given key.- Parameters:
key
- the key that has been modified- Returns:
- true if any watches were triggered, otherwise false
- Throws:
IllegalArgumentException
- ifkey
is null
-
trigger
Trigger all watches associated with the given keys.- Parameters:
keys
- keys that have been modified- Returns:
- true if any watches were triggered, otherwise false
- Throws:
IllegalArgumentException
- ifkeys
is null
-
trigger
Trigger all watches associated with keys in the given range.- Parameters:
range
- range of keys that have been modified- Returns:
- true if any watches were triggered, otherwise false
- Throws:
IllegalArgumentException
- ifrange
is null
-
trigger
Trigger all watches associated with the given mutations.- Parameters:
mutations
- mutations- Returns:
- true if any watches were triggered, otherwise false
- Throws:
IllegalArgumentException
- ifmutations
is null
-
triggerAll
public boolean triggerAll()Trigger all watches.- Returns:
- true if any watches were triggered, otherwise false
-
failAll
Discard all outstanding key watches and fail them with the given exception.- Parameters:
e
- failing exception- Throws:
IllegalArgumentException
- ife
is null
-
absorb
Absorb all of the watches from the given instance into this one. On return, this instance will contain all of the given instance's watches, and the given instance will be empty.- Parameters:
that
- the instance to absorb into this one
-
close
public void close()Close this instance.All outstanding key watches will be canceled as if by
failAll()
.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-