Class LibaioFile<Callback extends SubmitInfo>

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public final class LibaioFile<Callback extends SubmitInfo>
    extends java.lang.Object
    implements java.lang.AutoCloseable
    This is an extension to use libaio.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected boolean open  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void fallocate​(long size)
      It will use fallocate to initialize a file.
      void fill​(int alignment, long size)
      It will preallocate the file with a given size.
      int getBlockSize()  
      long getSize()  
      boolean lock()  
      java.nio.ByteBuffer newBuffer​(int size)
      It will allocate a buffer to be used on libaio operations.
      void read​(long position, int size, java.nio.ByteBuffer buffer, Callback callback)
      It will submit a read to the queue.
      void write​(long position, int size, java.nio.ByteBuffer buffer, Callback callback)
      It will submit a write to the queue.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • open

        protected boolean open
    • Method Detail

      • getBlockSize

        public int getBlockSize()
      • lock

        public boolean lock()
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.io.IOException
      • getSize

        public long getSize()
        Returns:
        The size of the file.
      • write

        public void write​(long position,
                          int size,
                          java.nio.ByteBuffer buffer,
                          Callback callback)
                   throws java.io.IOException
        It will submit a write to the queue. The callback sent here will be received on the LibaioContext.poll(SubmitInfo[], int, int) In case of the libaio queue is full (e.g. returning E_AGAIN) this method will return false.
        Notice: this won't hold a global reference on buffer, callback should hold a reference towards bufferWrite. And don't free the buffer until the callback was called as this could crash the VM.
        Parameters:
        position - The position on the file to write. Notice this has to be a multiple of 512.
        size - The size of the buffer to use while writing.
        buffer - if you are using O_DIRECT the buffer here needs to be allocated by newBuffer(int).
        callback - A callback to be returned on the poll method.
        Throws:
        java.io.IOException - in case of error
      • read

        public void read​(long position,
                         int size,
                         java.nio.ByteBuffer buffer,
                         Callback callback)
                  throws java.io.IOException
        It will submit a read to the queue. The callback sent here will be received on the LibaioContext.poll(SubmitInfo[], int, int). In case of the libaio queue is full (e.g. returning E_AGAIN) this method will return false.
        Notice: this won't hold a global reference on buffer, callback should hold a reference towards bufferWrite. And don't free the buffer until the callback was called as this could crash the VM. *
        Parameters:
        position - The position on the file to read. Notice this has to be a multiple of 512.
        size - The size of the buffer to use while reading.
        buffer - if you are using O_DIRECT the buffer here needs to be allocated by newBuffer(int).
        callback - A callback to be returned on the poll method.
        Throws:
        java.io.IOException - in case of error
        See Also:
        LibaioContext.poll(SubmitInfo[], int, int)
      • newBuffer

        public java.nio.ByteBuffer newBuffer​(int size)
        It will allocate a buffer to be used on libaio operations. Buffers here are allocated with posix_memalign.
        You need to explicitly free the buffer created from here using the LibaioContext.freeBuffer(java.nio.ByteBuffer).
        Parameters:
        size - the size of the buffer.
        Returns:
        the buffer allocated.
      • fill

        public void fill​(int alignment,
                         long size)
        It will preallocate the file with a given size.
        Parameters:
        size - number of bytes to be filled on the file
      • fallocate

        public void fallocate​(long size)
        It will use fallocate to initialize a file.
        Parameters:
        size - number of bytes to be filled on the file