Package org.h2.mvstore
Class FileStore
- java.lang.Object
-
- org.h2.mvstore.FileStore
-
- Direct Known Subclasses:
OffHeapStore
public class FileStore extends java.lang.ObjectThe default storage mechanism of the MVStore. This implementation persists data to a file. The file store is responsible to persist data and for free space management.
-
-
Field Summary
Fields Modifier and Type Field Description private java.nio.channels.FileChannelencryptedFileThe encrypted file (if encryption is used).private java.nio.channels.FileChannelfileThe file.private java.nio.channels.FileLockfileLockThe file lock.private java.lang.StringfileNameThe file name.protected longfileSizeThe file size (cached).protected FreeSpaceBitSetfreeSpaceThe free spaces between the chunks.protected java.util.concurrent.atomic.AtomicLongreadBytesThe number of read bytes.protected java.util.concurrent.atomic.AtomicLongreadCountThe number of read operations.private booleanreadOnlyWhether this store is read-only.protected java.util.concurrent.atomic.AtomicLongwriteBytesThe number of written bytes.protected java.util.concurrent.atomic.AtomicLongwriteCountThe number of write operations.
-
Constructor Summary
Constructors Constructor Description FileStore()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) longallocate(int length, long reservedLow, long reservedHigh)Allocate a number of blocks and mark them as used.voidclear()Mark the file as empty.voidclose()Close this store.voidfree(long pos, int length)Mark the space as free.(package private) longgetAfterLastBlock()intgetDefaultRetentionTime()Get the default retention time for this store in milliseconds.java.nio.channels.FileChannelgetEncryptedFile()Get the encrypted file instance, if encryption is used.java.nio.channels.FileChannelgetFile()Get the file instance in use.(package private) longgetFileLengthInUse()java.lang.StringgetFileName()Get the file name.intgetFillRate()(package private) longgetFirstFree()(package private) intgetMovePriority(int block)Calculates relative "priority" for chunk to be moved.intgetProjectedFillRate(int vacatedBlocks)Calculates a prospective fill rate, which store would have after rewrite of sparsely populated chunk(s) and evacuation of still live data into a new chunk.longgetReadBytes()Get the number of read bytes since this store was opened.longgetReadCount()Get the number of read operations since this store was opened.longgetWriteBytes()Get the number of written bytes since this store was opened.longgetWriteCount()Get the number of write operations since this store was opened.(package private) booleanisFragmented()booleanisReadOnly()voidmarkUsed(long pos, int length)Mark the space as in use.voidopen(java.lang.String fileName, boolean readOnly, char[] encryptionKey)Try to open the file.(package private) longpredictAllocation(int blocks, long reservedLow, long reservedHigh)Calculate starting position of the prospective allocation.java.nio.ByteBufferreadFully(long pos, int len)Read from the file.longsize()Get the file size.voidsync()Flush all changes.java.lang.StringtoString()voidtruncate(long size)Truncate the file.voidwriteFully(long pos, java.nio.ByteBuffer src)Write to the file.
-
-
-
Field Detail
-
readCount
protected final java.util.concurrent.atomic.AtomicLong readCount
The number of read operations.
-
readBytes
protected final java.util.concurrent.atomic.AtomicLong readBytes
The number of read bytes.
-
writeCount
protected final java.util.concurrent.atomic.AtomicLong writeCount
The number of write operations.
-
writeBytes
protected final java.util.concurrent.atomic.AtomicLong writeBytes
The number of written bytes.
-
freeSpace
protected final FreeSpaceBitSet freeSpace
The free spaces between the chunks. The first block to use is block 2 (the first two blocks are the store header).
-
fileName
private java.lang.String fileName
The file name.
-
readOnly
private boolean readOnly
Whether this store is read-only.
-
fileSize
protected long fileSize
The file size (cached).
-
file
private java.nio.channels.FileChannel file
The file.
-
encryptedFile
private java.nio.channels.FileChannel encryptedFile
The encrypted file (if encryption is used).
-
fileLock
private java.nio.channels.FileLock fileLock
The file lock.
-
-
Method Detail
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
readFully
public java.nio.ByteBuffer readFully(long pos, int len)Read from the file.- Parameters:
pos- the write positionlen- the number of bytes to read- Returns:
- the byte buffer
-
writeFully
public void writeFully(long pos, java.nio.ByteBuffer src)Write to the file.- Parameters:
pos- the write positionsrc- the source buffer
-
open
public void open(java.lang.String fileName, boolean readOnly, char[] encryptionKey)Try to open the file.- Parameters:
fileName- the file namereadOnly- whether the file should only be opened in read-only mode, even if the file is writableencryptionKey- the encryption key, or null if encryption is not used
-
close
public void close()
Close this store.
-
sync
public void sync()
Flush all changes.
-
size
public long size()
Get the file size.- Returns:
- the file size
-
truncate
public void truncate(long size)
Truncate the file.- Parameters:
size- the new file size
-
getFile
public java.nio.channels.FileChannel getFile()
Get the file instance in use.The application may read from the file (for example for online backup), but not write to it or truncate it.
- Returns:
- the file
-
getEncryptedFile
public java.nio.channels.FileChannel getEncryptedFile()
Get the encrypted file instance, if encryption is used.The application may read from the file (for example for online backup), but not write to it or truncate it.
- Returns:
- the encrypted file, or null if encryption is not used
-
getWriteCount
public long getWriteCount()
Get the number of write operations since this store was opened. For file based stores, this is the number of file write operations.- Returns:
- the number of write operations
-
getWriteBytes
public long getWriteBytes()
Get the number of written bytes since this store was opened.- Returns:
- the number of write operations
-
getReadCount
public long getReadCount()
Get the number of read operations since this store was opened. For file based stores, this is the number of file read operations.- Returns:
- the number of read operations
-
getReadBytes
public long getReadBytes()
Get the number of read bytes since this store was opened.- Returns:
- the number of write operations
-
isReadOnly
public boolean isReadOnly()
-
getDefaultRetentionTime
public int getDefaultRetentionTime()
Get the default retention time for this store in milliseconds.- Returns:
- the retention time
-
markUsed
public void markUsed(long pos, int length)Mark the space as in use.- Parameters:
pos- the position in byteslength- the number of bytes
-
allocate
long allocate(int length, long reservedLow, long reservedHigh)Allocate a number of blocks and mark them as used.- Parameters:
length- the number of bytes to allocatereservedLow- start block index of the reserved area (inclusive)reservedHigh- end block index of the reserved area (exclusive), special value -1 means beginning of the infinite free area- Returns:
- the start position in bytes
-
predictAllocation
long predictAllocation(int blocks, long reservedLow, long reservedHigh)Calculate starting position of the prospective allocation.- Parameters:
blocks- the number of blocks to allocatereservedLow- start block index of the reserved area (inclusive)reservedHigh- end block index of the reserved area (exclusive), special value -1 means beginning of the infinite free area- Returns:
- the starting block index
-
isFragmented
boolean isFragmented()
-
free
public void free(long pos, int length)Mark the space as free.- Parameters:
pos- the position in byteslength- the number of bytes
-
getFillRate
public int getFillRate()
-
getProjectedFillRate
public int getProjectedFillRate(int vacatedBlocks)
Calculates a prospective fill rate, which store would have after rewrite of sparsely populated chunk(s) and evacuation of still live data into a new chunk.- Parameters:
vacatedBlocks- number of blocks vacated- Returns:
- prospective fill rate (0 - 100)
-
getFirstFree
long getFirstFree()
-
getFileLengthInUse
long getFileLengthInUse()
-
getMovePriority
int getMovePriority(int block)
Calculates relative "priority" for chunk to be moved.- Parameters:
block- where chunk starts- Returns:
- priority, bigger number indicate that chunk need to be moved sooner
-
getAfterLastBlock
long getAfterLastBlock()
-
clear
public void clear()
Mark the file as empty.
-
getFileName
public java.lang.String getFileName()
Get the file name.- Returns:
- the file name
-
-