Class AsyncBinaryStore
- java.lang.Object
-
- com.tangosol.util.Base
-
- com.tangosol.io.AsyncBinaryStore
-
- All Implemented Interfaces:
BinaryStore
public class AsyncBinaryStore extends Base implements BinaryStore
An AsyncBinaryStore is a BinaryStore wrapper that performs the "O" (output) portion of its I/O asynchronously on a daemon thread. The output portion consists of store and erase processing.Since the "O" portion is passed along to the wrapped BinaryStore on a separate thread, only read operations are blocking, thus the BinaryStore operations on a whole appear much faster. As such, it is somewhat analogous to a write-behind cache.
If an operation fails on the daemon thread, all further operations will occur synchronously, so that exceptions will propagate successfully up. It is assumed that once one exception occurs, the underlying BinaryStore is in a state that will cause more exceptions to occur. Even when an exception occurs on the daemon thread, that write-behind data will not be "lost" because it will still be available in the internal data structures that keeps track of pending writes.
- Since:
- Coherence 2.5
- Author:
- cp 2004.06.18
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
AsyncBinaryStore.QueueDaemon
A daemon that processes queued writes.-
Nested classes/interfaces inherited from class com.tangosol.util.Base
Base.LoggingWriter
-
Nested classes/interfaces inherited from interface com.tangosol.io.BinaryStore
BinaryStore.KeySetAware, BinaryStore.SizeAware
-
-
Field Summary
Fields Modifier and Type Field Description protected static int
DEFAULT_LIMIT
Default size limit for write-queued data.protected static Binary
DELETED
A special token that is used to signify a queued erase operation.protected static Object
LOCK_ALL
Special key to indicate that all keys should be locked.protected static long
WAIT_FOREVER
Special wait time to indicate that a lock should be blocked on until it becomes available.
-
Constructor Summary
Constructors Constructor Description AsyncBinaryStore(BinaryStore store)
Construct an AsyncBinaryStore.AsyncBinaryStore(BinaryStore store, int cbMax)
Construct an AsyncBinaryStore.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Close the store.protected AsyncBinaryStore.QueueDaemon
ensureQueueDaemon()
Obtain the QueueDaemon, if one already exists; otherwise, create and start one.void
erase(Binary binKey)
Remove the specified key from the underlying store if present.void
eraseAll()
Remove all data from the underlying store.protected void
finalize()
Perform cleanup during garbage collection.void
forceSync()
Indicate the future write operations must be synchronous.BinaryStore
getBinaryStore()
Obtain the BinaryStore that this AsyncBinaryStore wraps.protected ConcurrentMap
getPendingMap()
Obtain the map that contains all of the pending store and erase data.protected AsyncBinaryStore.QueueDaemon
getQueueDaemon()
int
getQueuedLimit()
Determine the size, in bytes, of the data that can be queued to be written asynchronously by the QueueDaemon thread.int
getQueuedSize()
Determine the current number of bytes that are pending being written.protected AsyncBinaryStore.QueueDaemon
instantiateQueueDaemon()
Factory method: Instantiate a QueueDaemon.protected void
internalClose(Consumer<? super BinaryStore> onClose)
Close the store.boolean
isAsync()
Determine if the AsyncBinaryStore is operating in an asynchronous manner.Iterator
keys()
Iterate all keys in the underlying store.Binary
load(Binary binKey)
Return the value associated with the specified key, or null if the key does not have an associated value in the underlying store.protected void
setBinaryStore(BinaryStore store)
Specify the underlying BinaryStore.protected void
setQueueDaemon(AsyncBinaryStore.QueueDaemon daemon)
void
store(Binary binKey, Binary binValue)
Store the specified value under the specific key in the underlying store.String
toString()
Return a human readable description of the AsyncBinaryStore.protected void
updateQueuedSize(int cb)
Update the number of bytes that are pending to be written.-
Methods inherited from class com.tangosol.util.Base
azzert, azzert, azzert, azzertFailed, breakLines, breakLines, capitalize, checkNotEmpty, checkNotNull, checkRange, computeSafeWaitTime, decimalValue, dup, dup, ensureBigDecimal, ensureClassLoader, ensureRuntimeException, ensureRuntimeException, equals, equalsDeep, err, err, err, err, err, escape, formatDateTime, getCallerStackFrame, getCommonMonitor, getCommonMonitor, getCommonMonitor, getContextClassLoader, getContextClassLoader, getDeepMessage, getErr, getLastSafeTimeMillis, getLog, getMaxDecDigits, getMaxHexDigits, getOriginalException, getOut, getProcessRandom, getRandom, getRandomBinary, getRandomBinary, getRandomString, getSafeTimeMillis, getStackFrame, getStackFrames, getStackTrace, getStackTrace, getStackTrace, getThreadFactory, getTimeZone, getUpTimeMillis, hashCode, hexValue, indentString, indentString, isDecimal, isHex, isLogEcho, isOctal, log, log, log, log, log, makeInteger, makeLong, makeThread, mergeArray, mergeBooleanArray, mergeByteArray, mergeCharArray, mergeDoubleArray, mergeFloatArray, mergeIntArray, mergeLongArray, mod, mod, octalValue, out, out, out, out, out, pad, parseBandwidth, parseBandwidth, parseDelimitedString, parseHex, parseHex, parseMemorySize, parseMemorySize, parsePercentage, parseTime, parseTime, parseTimeNanos, parseTimeNanos, printStackTrace, randomize, randomize, randomize, randomize, read, read, read, read, read, read, read, replace, setErr, setLog, setLogEcho, setOut, sleep, toBandwidthString, toBandwidthString, toCharEscape, toCrc, toCrc, toCrc, toCrc, toCrc, toDecString, toDelimitedString, toDelimitedString, toDelimitedString, toDelimitedString, toHex, toHex, toHexDump, toHexEscape, toHexEscape, toHexEscape, toHexEscape, toHexString, toMemorySizeString, toMemorySizeString, toQuotedCharEscape, toQuotedStringEscape, toSqlString, toString, toString, toStringEscape, toUnicodeEscape, trace, trace, trace, trace, trace, trace, trace, trace, trace, truncateString, truncateString, wait
-
-
-
-
Field Detail
-
DELETED
protected static final Binary DELETED
A special token that is used to signify a queued erase operation.
-
DEFAULT_LIMIT
protected static final int DEFAULT_LIMIT
Default size limit for write-queued data.- See Also:
- Constant Field Values
-
LOCK_ALL
protected static final Object LOCK_ALL
Special key to indicate that all keys should be locked.
-
WAIT_FOREVER
protected static final long WAIT_FOREVER
Special wait time to indicate that a lock should be blocked on until it becomes available.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
AsyncBinaryStore
public AsyncBinaryStore(BinaryStore store)
Construct an AsyncBinaryStore. An AsyncBinaryStor wrap other BinaryStore objects to provide asynchronous write operations.The new AsyncBinaryStore will queue a maximum of 4194304 bytes (4 MB) before blocking.
- Parameters:
store
- the BinaryStore to wrap
-
AsyncBinaryStore
public AsyncBinaryStore(BinaryStore store, int cbMax)
Construct an AsyncBinaryStore. An AsyncBinaryStor wrap other BinaryStore objects to provide asynchronous write operations.- Parameters:
store
- the BinaryStore to wrapcbMax
- the maximum number of bytes to queue before blocking
-
-
Method Detail
-
load
public Binary load(Binary binKey)
Return the value associated with the specified key, or null if the key does not have an associated value in the underlying store.- Specified by:
load
in interfaceBinaryStore
- Parameters:
binKey
- key whose associated value is to be returned- Returns:
- the value associated with the specified key, or null if no value is available for that key
-
store
public void store(Binary binKey, Binary binValue)
Store the specified value under the specific key in the underlying store. This method is intended to support both key/value creation and value update for a specific key.- Specified by:
store
in interfaceBinaryStore
- Parameters:
binKey
- key to store the value underbinValue
- value to be stored- Throws:
UnsupportedOperationException
- if this implementation or the underlying store is read-only
-
erase
public void erase(Binary binKey)
Remove the specified key from the underlying store if present.- Specified by:
erase
in interfaceBinaryStore
- Parameters:
binKey
- key whose mapping is to be removed from the map- Throws:
UnsupportedOperationException
- if this implementation or the underlying store is read-only
-
eraseAll
public void eraseAll()
Remove all data from the underlying store.- Specified by:
eraseAll
in interfaceBinaryStore
- Throws:
UnsupportedOperationException
- if this implementation or the underlying store is read-only
-
keys
public Iterator keys()
Iterate all keys in the underlying store.- Specified by:
keys
in interfaceBinaryStore
- Returns:
- a read-only iterator of the keys in the underlying store
- Throws:
UnsupportedOperationException
- if the underlying store is not iterable
-
close
public void close()
Close the store.
-
internalClose
protected void internalClose(Consumer<? super BinaryStore> onClose)
Close the store. The wrapped store is closed either with the optionalonClose
consumer or by directly callingclose
on wrapped store whenonClose
consumer is null.- Parameters:
onClose
- optional close consumer to close wrappedBinaryStore
-
getBinaryStore
public BinaryStore getBinaryStore()
Obtain the BinaryStore that this AsyncBinaryStore wraps. The wrapped BinaryStore is also referred to as the "underlying" BinaryStore. All I/O operations are delegated to the underlying BinaryStore; some write operations are performed asynchronously on the QueueDaemon thread.- Returns:
- the underlying BinaryStore
-
setBinaryStore
protected void setBinaryStore(BinaryStore store)
Specify the underlying BinaryStore. When the AsyncBinaryStore is closed, it should clear out its reference to the underlying BinaryStore to indicate that it no longer is open.- Parameters:
store
- the underlying BinaryStore
-
getQueuedLimit
public int getQueuedLimit()
Determine the size, in bytes, of the data that can be queued to be written asynchronously by the QueueDaemon thread. Once the limit is exceeded, operations will be forced to be synchronous to avoid running out of memory or getting too far behind on the I/O operations.- Returns:
- the number of bytes allowed to be queued to be written
-
getQueuedSize
public int getQueuedSize()
Determine the current number of bytes that are pending being written.- Returns:
- the number of bytes currently queued to be written
-
updateQueuedSize
protected void updateQueuedSize(int cb)
Update the number of bytes that are pending to be written.- Parameters:
cb
- the number of bytes that the queue length changed by
-
isAsync
public boolean isAsync()
Determine if the AsyncBinaryStore is operating in an asynchronous manner.- Returns:
- true if the AsyncBinaryStore is still operating in an async mode
-
forceSync
public void forceSync()
Indicate the future write operations must be synchronous.
-
getPendingMap
protected ConcurrentMap getPendingMap()
Obtain the map that contains all of the pending store and erase data. The key of the map is a Binary key to pass to the underlying store. The corresponding value will beDELETED
to indicate an erase operation, otherwise it will be a Binary value.- Returns:
- the ConcurrentMap that keeps track of all the pending writes
-
toString
public String toString()
Return a human readable description of the AsyncBinaryStore.
-
finalize
protected void finalize()
Perform cleanup during garbage collection.
-
getQueueDaemon
protected AsyncBinaryStore.QueueDaemon getQueueDaemon()
- Returns:
- the daemon that manages the write-behind queue
-
setQueueDaemon
protected void setQueueDaemon(AsyncBinaryStore.QueueDaemon daemon)
- Parameters:
daemon
- the daemon that manages the write-behind queue
-
ensureQueueDaemon
protected AsyncBinaryStore.QueueDaemon ensureQueueDaemon()
Obtain the QueueDaemon, if one already exists; otherwise, create and start one.- Returns:
- the daemon that manages the write-behind queue
-
instantiateQueueDaemon
protected AsyncBinaryStore.QueueDaemon instantiateQueueDaemon()
Factory method: Instantiate a QueueDaemon.- Returns:
- a new QueueDaemon, but not started
-
-