Class ConcurrentShardedQueueFile

  • All Implemented Interfaces:
    QueueFile, Closeable, AutoCloseable, Iterable<byte[]>

    public class ConcurrentShardedQueueFile
    extends Object
    implements QueueFile
    A thread-safe QueueFile implementation, that uses multiple smaller "shard" files instead of one large file. This also improves concurrency - when we have more than one file, we can add and remove tasks at the same time without mutually exclusive locking.
    Author:
    [email protected]
    • Constructor Detail

      • ConcurrentShardedQueueFile

        public ConcurrentShardedQueueFile​(String fileNamePrefix,
                                          String fileNameSuffix,
                                          int shardSizeBytes,
                                          QueueFileFactory queueFileFactory)
                                   throws IOException
        Parameters:
        fileNamePrefix - path + file name prefix for shard files
        fileNameSuffix - file name suffix to identify shard files
        shardSizeBytes - target shard size bytes
        queueFileFactory - factory for QueueFile objects
        Throws:
        IOException - if file(s) could not be created or accessed
    • Method Detail

      • add

        public void add​(byte[] data,
                        int offset,
                        int count)
                 throws IOException
        Description copied from interface: QueueFile
        Adds an element to the end of the queue.
        Specified by:
        add in interface QueueFile
        Parameters:
        data - to copy bytes from
        offset - to start from in buffer
        count - number of bytes to copy
        Throws:
        IOException
      • size

        public int size()
        Description copied from interface: QueueFile
        Returns the number of elements in this queue.
        Specified by:
        size in interface QueueFile
      • storageBytes

        public long storageBytes()
        Description copied from interface: QueueFile
        Returns the storage size (on-disk file size) in bytes.
        Specified by:
        storageBytes in interface QueueFile
        Returns:
        file size in bytes.
      • usedBytes

        public long usedBytes()
        Description copied from interface: QueueFile
        Returns the number of bytes used for data.
        Specified by:
        usedBytes in interface QueueFile
        Returns:
        bytes used.
      • availableBytes

        public long availableBytes()
        Description copied from interface: QueueFile
        Returns the number of bytes available for adding new tasks without growing the file.
        Specified by:
        availableBytes in interface QueueFile
        Returns:
        bytes available.