java.lang.Object
com.aoapps.persistence.AbstractPersistentBuffer
com.aoapps.persistence.MappedPersistentBuffer
- All Implemented Interfaces:
PersistentBuffer
,Closeable
,AutoCloseable
Uses
MappedByteBuffer
for persistence. It maps the entire file
at once into a single buffer. For this reason, it is limited to a maximum
buffer size of 2^31-1. To use memory mapping for larger buffers, at a slight
performance cost, use LargeMappedPersistentBuffer
.- Author:
- AO Industries, Inc.
- See Also:
-
Field Summary
Fields inherited from class com.aoapps.persistence.AbstractPersistentBuffer
protectionLevel
-
Constructor Summary
ConstructorDescriptionCreates a read-write buffer backed by a temporary file.MappedPersistentBuffer
(File file) Creates a read-write buffer withProtectionLevel.BARRIER
protection level.MappedPersistentBuffer
(File file, ProtectionLevel protectionLevel) Creates a buffer.MappedPersistentBuffer
(RandomAccessFile raf, ProtectionLevel protectionLevel) Creates a buffer using the providedRandomAccessFile
.MappedPersistentBuffer
(String name) Creates a read-write buffer withProtectionLevel.BARRIER
protection level.MappedPersistentBuffer
(String name, ProtectionLevel protectionLevel) Creates a buffer. -
Method Summary
Modifier and TypeMethodDescriptionvoid
barrier
(boolean force) There is not currently a way to provide a barrier without usingforce
.long
capacity()
Gets the capacity of this buffer.void
close()
Closes this buffer.void
ensureZeros
(long position, long len) Ensures that all values from the position for the provided length are zeros.byte
get
(long position) Gets a single byte from the buffer.void
get
(long position, byte[] buff, int off, int len) Implemented as calls toPersistentBuffer.getSome(long, byte[], int, int)
.boolean
getBoolean
(long position) Implemented as call toAbstractPersistentBuffer.get(long)
.int
getInt
(long position) Implemented as call toAbstractPersistentBuffer.get(long, byte[], int, int)
.long
getLong
(long position) Implemented as call toAbstractPersistentBuffer.get(long, byte[], int, int)
.int
getSome
(long position, byte[] buff, int off, int len) Reads to the providedbyte[]
, may read fewer thanlen
bytes, but will always read at least one byte.boolean
isClosed()
Checks if this buffer is closed.void
put
(long position, byte value) Puts a single byte in the buffer.void
put
(long position, byte[] buff, int off, int len) Writes the bytes to the provided position.void
putInt
(long position, int value) Implemented as call toPersistentBuffer.put(long, byte[], int, int)
.void
putLong
(long position, long value) Implemented as call toPersistentBuffer.put(long, byte[], int, int)
.void
setCapacity
(long newLength) Sets the capacity of this buffer.Methods inherited from class com.aoapps.persistence.AbstractPersistentBuffer
getInputStream, getOutputStream, getProtectionLevel
-
Constructor Details
-
MappedPersistentBuffer
Creates a read-write buffer backed by a temporary file. The protection level is set toProtectionLevel.NONE
. The temporary file will be deleted when this buffer is closed or on JVM shutdown.- Throws:
IOException
-
MappedPersistentBuffer
Creates a read-write buffer withProtectionLevel.BARRIER
protection level.- Throws:
IOException
-
MappedPersistentBuffer
Creates a buffer.- Throws:
IOException
-
MappedPersistentBuffer
Creates a read-write buffer withProtectionLevel.BARRIER
protection level.- Throws:
IOException
-
MappedPersistentBuffer
Creates a buffer.- Throws:
IOException
-
MappedPersistentBuffer
public MappedPersistentBuffer(RandomAccessFile raf, ProtectionLevel protectionLevel) throws IOException Creates a buffer using the providedRandomAccessFile
.- Throws:
IOException
-
-
Method Details
-
isClosed
public boolean isClosed()Description copied from interface:PersistentBuffer
Checks if this buffer is closed. -
close
Description copied from interface:PersistentBuffer
Closes this buffer. It is OK to close an already closed buffer.- Throws:
IOException
-
capacity
Description copied from interface:PersistentBuffer
Gets the capacity of this buffer.- Throws:
IOException
-
setCapacity
Description copied from interface:PersistentBuffer
Sets the capacity of this buffer. If the buffer is increased in size, the new space will be zero-filled. Setting the capacity may impose an automaticbarrier(true)
, depending on implementation. This should be considered an expensive operation.- Throws:
IOException
-
get
Description copied from class:AbstractPersistentBuffer
Implemented as calls toPersistentBuffer.getSome(long, byte[], int, int)
.- Specified by:
get
in interfacePersistentBuffer
- Overrides:
get
in classAbstractPersistentBuffer
- Throws:
IOException
- See Also:
-
getSome
Description copied from interface:PersistentBuffer
Reads to the providedbyte[]
, may read fewer thanlen
bytes, but will always read at least one byte. Blocks if no data is available.- Throws:
IOException
-
get
Gets a single byte from the buffer.- Specified by:
get
in interfacePersistentBuffer
- Overrides:
get
in classAbstractPersistentBuffer
- Throws:
IOException
- See Also:
-
ensureZeros
Description copied from interface:PersistentBuffer
Ensures that all values from the position for the provided length are zeros. This may or may not modify the buffer in the process. The values will all be zero upon return. Some implementations may choose to overwrite zeros and return modified, others may choose to detect zeros and avoid modifications. Thus it is possible for existing zeros to still result in a modification.- Throws:
IOException
-
put
Puts a single byte in the buffer.- Specified by:
put
in interfacePersistentBuffer
- Overrides:
put
in classAbstractPersistentBuffer
- Throws:
IOException
- See Also:
-
put
Description copied from interface:PersistentBuffer
Writes the bytes to the provided position. The buffer will not be expanded automatically.- Throws:
IOException
-
barrier
There is not currently a way to provide a barrier without usingforce
. This just uses force for both.- Throws:
IOException
- See Also:
-
getBoolean
Description copied from class:AbstractPersistentBuffer
Implemented as call toAbstractPersistentBuffer.get(long)
.- Specified by:
getBoolean
in interfacePersistentBuffer
- Overrides:
getBoolean
in classAbstractPersistentBuffer
- Throws:
IOException
- See Also:
-
getInt
Description copied from class:AbstractPersistentBuffer
Implemented as call toAbstractPersistentBuffer.get(long, byte[], int, int)
.- Specified by:
getInt
in interfacePersistentBuffer
- Overrides:
getInt
in classAbstractPersistentBuffer
- Throws:
IOException
- See Also:
-
getLong
Description copied from class:AbstractPersistentBuffer
Implemented as call toAbstractPersistentBuffer.get(long, byte[], int, int)
.- Specified by:
getLong
in interfacePersistentBuffer
- Overrides:
getLong
in classAbstractPersistentBuffer
- Throws:
IOException
- See Also:
-
putInt
Description copied from class:AbstractPersistentBuffer
Implemented as call toPersistentBuffer.put(long, byte[], int, int)
.- Specified by:
putInt
in interfacePersistentBuffer
- Overrides:
putInt
in classAbstractPersistentBuffer
- Throws:
IOException
- See Also:
-
putLong
Description copied from class:AbstractPersistentBuffer
Implemented as call toPersistentBuffer.put(long, byte[], int, int)
.- Specified by:
putLong
in interfacePersistentBuffer
- Overrides:
putLong
in classAbstractPersistentBuffer
- Throws:
IOException
- See Also:
-