org.rrd4j.core

## Class RrdNioBackend

- java.lang.Object
- - org.rrd4j.core.RrdBackend
  - - org.rrd4j.core.ByteBufferBackend
    - - org.rrd4j.core.RrdNioBackend

- All Implemented Interfaces:
    RrdFileBackend

      public class RrdNioBackend
      extends ByteBufferBackend
      implements RrdFileBackend

  Backend which is used to store RRD data to ordinary disk files using java.nio.* package. This is the default backend engine.

  This version buffers all stat files in a single temporary buffer file sequentially, keeping track of each file's offset and length, then flushes all to the mapped file in batch.

- - ### Field Summary

    - ### Fields inherited from class org.rrd4j.core.RrdBackend

      `BYTEORDER`

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `RrdNioBackend(String path, boolean readOnly, RrdSyncThreadPool threadPool, int syncPeriod)` Creates RrdFileBackend object for the given file path, backed by java.nio.* classes.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `bufferStatFile(ByteBuffer data, long targetFileOffset)` Buffers one stat file's data into the single temp buffer file sequentially.
    `void` | `close()` Closes the underlying RRD file.
    `void` | `flushBufferedStatFiles()` Flushes all buffered stat files sequentially from the temp buffer file into their designated positions in the mapped RRD file in one batch operation.
    `String` | `getCanonicalPath()` Returns canonical path to the file on the disk.
    `long` | `getLength()` Returns the number of RRD bytes in the underlying storage.
    `protected void` | `setLength(long newLength)` Sets the number of bytes in the underlying RRD storage.
    `protected void` | `sync()` Override sync to disable unbuffered flush.

    - ### Methods inherited from class org.rrd4j.core.ByteBufferBackend

      `getCharBuffer, isDirty, read, readDouble, readDouble, readInt, readLong, readShort, rrdClose, setByteBuffer, write, writeDouble, writeDouble, writeDouble, writeInt, writeLong, writeShort, writeString`

    - ### Methods inherited from class org.rrd4j.core.RrdBackend

      `done, getFactory, getPath, getUri, isCachingAllowed, isInstanceCreated, readAll, readString, writeString`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### RrdNioBackend

          protected RrdNioBackend(String path,
                                  boolean readOnly,
                                  RrdSyncThreadPool threadPool,
                                  int syncPeriod)
                           throws IOException

      Creates RrdFileBackend object for the given file path, backed by java.nio.* classes.
      Parameters:
        `path` - Path to a file
        `readOnly` - True, if file should be open in read-only mode. False otherwise
        `threadPool` - Sync thread pool; can be null
        `syncPeriod` - Sync period in seconds
      Throws:
        `IOException` - On I/O error

  - ### Method Detail

    - #### bufferStatFile

          public void bufferStatFile(ByteBuffer data,
                                     long targetFileOffset)
                              throws IOException

      Buffers one stat file's data into the single temp buffer file sequentially. This method appends the data to the temp file and records the offset and length, including the target offset in the mapped file where this stat file belongs.
      Parameters:
        `data` - ByteBuffer containing the stat file's bytes to buffer
        `targetFileOffset` - Position in the mapped RRD file to write this stat file to on flush
      Throws:
        `IOException` - If writing to temp file fails

    - #### close

          public void close()
                     throws IOException

      Closes the underlying RRD file. Also closes and deletes the temporary buffer file used for buffering.
      Overrides:
        `close` in class `ByteBufferBackend`
      Throws:
        `IOException` - On I/O error

    - #### flushBufferedStatFiles

          public void flushBufferedStatFiles()

      Flushes all buffered stat files sequentially from the temp buffer file into their designated positions in the mapped RRD file in one batch operation. After flushing, the temp buffer file and index are cleared for the next batch.

    - #### getCanonicalPath

          public String getCanonicalPath()

      Description copied from interface: `RrdFileBackend`
      Returns canonical path to the file on the disk.
      Specified by:
        `getCanonicalPath` in interface `RrdFileBackend`
      Returns:
        Canonical file path

    - #### getLength

          public long getLength()
                         throws IOException

      Description copied from class: `RrdBackend`
      Returns the number of RRD bytes in the underlying storage.
      Specified by:
        `getLength` in class `RrdBackend`
      Returns:
        Number of RRD bytes in the storage.
      Throws:
        `IOException` - Thrown in case of I/O error.

    - #### setLength

          protected void setLength(long newLength)
                            throws IOException

      Sets the number of bytes in the underlying RRD storage. This method is called only once, immediately after a new RRD storage gets created. Sets length of the underlying RRD file. This method is called only once, immediately after new RRD creation.
      Specified by:
        `setLength` in class `RrdBackend`
      Parameters:
        `newLength` - Length of the underlying RRD storage in bytes.
      Throws:
        `IllegalArgumentException` - if the length is bigger than the possible mapping position (2GiB).
        `IOException` - Thrown in case of I/O error.

    - #### sync

          protected void sync()

      Override sync to disable unbuffered flush.