Class 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
    • 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 wrap
        cbMax - 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 interface BinaryStore
        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 interface BinaryStore
        Parameters:
        binKey - key to store the value under
        binValue - 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 interface BinaryStore
        Parameters:
        binKey - key whose mapping is to be removed from the map
        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 interface BinaryStore
        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 optional onClose consumer or by directly calling close on wrapped store when onClose consumer is null.
        Parameters:
        onClose - optional close consumer to close wrapped BinaryStore
      • 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 be DELETED 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.
        Overrides:
        toString in class Object
        Returns:
        a String description of the AsyncBinaryStore
      • finalize

        protected void finalize()
        Perform cleanup during garbage collection.
        Overrides:
        finalize in class Object
      • 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