Package org.jrobin.core
Class RrdBackend
- java.lang.Object
-
- org.jrobin.core.RrdBackend
-
- Direct Known Subclasses:
RrdFileBackend
,RrdJRobin14FileBackend
,RrdMemoryBackend
public abstract class RrdBackend extends Object
Base implementation class for all backend classes. Each Round Robin Database object (RrdDb
object) is backed with a single RrdBackend object which performs actual I/O operations on the underlying storage. JRobin supports three different bakcends out of the box:RrdFileBackend
: objects of this class are created from theRrdFileBackendFactory
class. This was the default backend used in all JRobin releases prior to 1.4.0. It uses java.io.* package and RandomAccessFile class to store RRD data in files on the disk.RrdNioBackend
: objects of this class are created from theRrdNioBackendFactory
class. The backend uses java.io.* and java.nio.* classes (mapped ByteBuffer) to store RRD data in files on the disk. This backend is fast, very fast, but consumes a lot of memory (borrowed not from the JVM but from the underlying operating system directly). This is the default backend used in JRobin since 1.4.0 release.RrdMemoryBackend
: objects of this class are created from theRrdMemoryBackendFactory
class. This backend stores all data in memory. Once JVM exits, all data gets lost. The backend is extremely fast and memory hungry.
To create your own backend in order to provide some custom type of RRD storage, you should do the following:
- Create your custom RrdBackend class (RrdCustomBackend, for example)
by extending RrdBackend class. You have to implement all abstract methods defined
in the base class.
- Create your custom RrdBackendFactory class (RrdCustomBackendFactory,
for example) by extending RrdBackendFactory class. You have to implement all
abstract methods defined in the base class. Your custom factory class will actually
create custom backend objects when necessary.
- Create instance of your custom RrdBackendFactory and register it as a regular
factory available to JRobin framework. See javadoc for
RrdBackendFactory
to find out how to do this
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
RrdBackend(String path)
Creates backend for a RRD storage with the given path.protected
RrdBackend(String path, boolean readOnly)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
close()
Closes the underlying backend.abstract long
getLength()
Returns the number of RRD bytes in the underlying storage.String
getPath()
Returns path to the storage.protected boolean
isCachingAllowed()
This method suggests the caching policy to the JRobin frontend (high-level) classes.(package private) static boolean
isInstanceCreated()
boolean
isReadOnly()
Is the RRD ReadOnly?protected abstract void
read(long offset, byte[] b)
Reads an array of bytes from the underlying storage starting from the given storage offset.byte[]
readAll()
Reads all RRD bytes from the underlying storage(package private) double
readDouble(long offset)
(package private) double[]
readDouble(long offset, int count)
(package private) int
readInt(long offset)
(package private) long
readLong(long offset)
(package private) String
readString(long offset)
protected abstract void
setLength(long length)
Sets the number of bytes in the underlying RRD storage.protected abstract void
write(long offset, byte[] b)
Writes an array of bytes to the underlying storage starting from the given storage offset.(package private) void
writeDouble(long offset, double value)
(package private) void
writeDouble(long offset, double[] values)
(package private) void
writeDouble(long offset, double value, int count)
(package private) void
writeInt(long offset, int value)
(package private) void
writeLong(long offset, long value)
(package private) void
writeString(long offset, String rawValue)
-
-
-
Constructor Detail
-
RrdBackend
protected RrdBackend(String path)
Creates backend for a RRD storage with the given path.- Parameters:
path
- String identifying RRD storage. For files on the disk, this argument should represent file path. Other storage types might interpret this argument differently.
-
RrdBackend
protected RrdBackend(String path, boolean readOnly)
-
-
Method Detail
-
getPath
public String getPath()
Returns path to the storage.- Returns:
- Storage path
-
isReadOnly
public boolean isReadOnly()
Is the RRD ReadOnly?- Returns:
- True if the RRD is read only, false if not.
-
write
protected abstract void write(long offset, byte[] b) throws IOException
Writes an array of bytes to the underlying storage starting from the given storage offset.- Parameters:
offset
- Storage offset.b
- Array of bytes that should be copied to the underlying storage- Throws:
IOException
- Thrown in case of I/O error
-
read
protected abstract void read(long offset, byte[] b) throws IOException
Reads an array of bytes from the underlying storage starting from the given storage offset.- Parameters:
offset
- Storage offset.b
- Array which receives bytes from the underlying storage- Throws:
IOException
- Thrown in case of I/O error
-
getLength
public abstract long getLength() throws IOException
Returns the number of RRD bytes in the underlying storage.- Returns:
- Number of RRD bytes in the storage.
- Throws:
IOException
- Thrown in case of I/O error.
-
setLength
protected abstract void setLength(long length) 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.- Parameters:
length
- Length of the underlying RRD storage in bytes.- Throws:
IOException
- Thrown in case of I/O error.
-
close
public void close() throws IOException
Closes the underlying backend.- Throws:
IOException
- Thrown in case of I/O error.
-
isCachingAllowed
protected boolean isCachingAllowed()
This method suggests the caching policy to the JRobin frontend (high-level) classes. Iftrue
is returned, frontent classes will cache frequently used parts of a RRD file in memory to improve performance. Iffalse
is returned, high level classes will never cache RRD file sections in memory.- Returns:
true
if file caching is enabled,false
otherwise. By default, the method returnstrue
but it can be overriden in subclasses.
-
readAll
public final byte[] readAll() throws IOException
Reads all RRD bytes from the underlying storage- Returns:
- RRD bytes
- Throws:
IOException
- Thrown in case of I/O error
-
writeInt
final void writeInt(long offset, int value) throws IOException
- Throws:
IOException
-
writeLong
final void writeLong(long offset, long value) throws IOException
- Throws:
IOException
-
writeDouble
final void writeDouble(long offset, double value) throws IOException
- Throws:
IOException
-
writeDouble
final void writeDouble(long offset, double value, int count) throws IOException
- Throws:
IOException
-
writeDouble
final void writeDouble(long offset, double[] values) throws IOException
- Throws:
IOException
-
writeString
final void writeString(long offset, String rawValue) throws IOException
- Throws:
IOException
-
readInt
final int readInt(long offset) throws IOException
- Throws:
IOException
-
readLong
final long readLong(long offset) throws IOException
- Throws:
IOException
-
readDouble
final double readDouble(long offset) throws IOException
- Throws:
IOException
-
readDouble
final double[] readDouble(long offset, int count) throws IOException
- Throws:
IOException
-
readString
final String readString(long offset) throws IOException
- Throws:
IOException
-
isInstanceCreated
static boolean isInstanceCreated()
-
-