Top Description Fields Constructors Methods
org.h2.mvstore

public abstract Class RandomAccessStore

extends FileStore<SFChunk>
Class Inheritance
Known Direct Subclasses
org.h2.mvstore.SingleFileStore, org.h2.mvstore.OffHeapStore
Imports
java.nio.ByteBuffer, java.nio.charset.StandardCharsets, java.util.ArrayList, .Collection, .Collections, .Comparator, .HashMap, .Iterator, .Map, .PriorityQueue, .Queue

Class RandomAccessStore.
Author
Andrei Tokar

Field Summary

Modifier and TypeField and Description
protected final FreeSpaceBitSet
freeSpace

The free spaces between the chunks.

private long
private long
private volatile boolean
reuseSpace

Allocation mode: false - new chunk is always allocated at the end of file true - new chunk is allocated as close to the beginning of file, as possible

private boolean
Inherited from org.h2.mvstore.FileStore:
BLOCK_SIZEchunksHDR_BLOCKHDR_BLOCK_SIZEHDR_CHUNKHDR_CLEANHDR_CREATEDHDR_FLETCHERHDR_FORMATHDR_FORMAT_READHDR_HHDR_VERSIONlastChunkMETA_ID_KEYmvStorePIPE_LENGTHreadBytesreadCountrecoveryModesaveChunkLockstoreHeaderwriteByteswriteCount

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected final void
adjustStoreToLastChunk()

Implements abstract org.h2.mvstore.FileStore.adjustStoreToLastChunk.

Make persistent changes after lastChunk was reset

private long

Returns:

the start position in bytes
allocate
(int
the number of bytes to allocate
length
,
long
start block index of the reserved area (inclusive)
reservedLow
,
long
end block index of the reserved area (exclusive), special value -1 means beginning of the infinite free area
reservedHigh
)

Allocate a number of blocks and mark them as used.

protected final void
allocateChunkSpace(SFChunk
to allocate space for
chunk
,
WriteBuffer
to allocate space for
buff
)

Implements abstract org.h2.mvstore.FileStore.allocateChunkSpace.

Allocate logical space and assign position of the buffer within the store.

public void
clear()

Overrides org.h2.mvstore.FileStore.clear.

Mark the file as empty.

public void
compactMoveChunks(int
do nothing if the file store fill rate is higher than this
targetFillRate
,
long
the number of bytes to move
moveSize
,
MVStore
owner of this store
mvStore
)

Compact the store by moving all chunks next to each other, if there is free space between chunks.

private void
compactMoveChunks(long moveSize)

private void
protected void
compactStore(int
do not compact if store fill rate above this value (0-100)
thresholdFillRate
,
long
the maximum time in milliseconds to compact
maxCompactTime
,
int
the maximum amount of data to be written as part of this call
maxWriteSize
,
MVStore
that owns this FileStore
mvStore
)

Implements abstract org.h2.mvstore.FileStore.compactStore.

Compact store file, that is, compact blocks that have a low fill rate, and move chunks next to each other.

protected final SFChunk
createChunk(int newChunkId)

Implements abstract org.h2.mvstore.FileStore.createChunk.

public SFChunk
createChunk(String
the string
s
)

Implements abstract org.h2.mvstore.FileStore.createChunk.

Build a Chunk from the given string.

protected SFChunk
protected void
private Iterable<SFChunk>
findChunksToMove(long startBlock, long moveSize)

protected void
free(long
the position in bytes
pos
,
int
the number of bytes
length
)

Mark the space as free.

protected void
freeChunkSpace(Iterable<SFChunk>
chunks to be processed
chunks
)

Implements abstract org.h2.mvstore.FileStore.freeChunkSpace.

Mark the space occupied by specified chunks as free.

private void
private long

Returns:

block index
getAfterLastBlock
()

Get the index of the first block after last occupied one.

protected long
pack-priv long
public int
pack-priv long
private int
public int

Returns:

priority, bigger number indicate that chunk need to be moved sooner
getMovePriority
(int
where chunk starts
block
)

Calculates relative "priority" for chunk to be moved.

public Collection<SFChunk>
private int
getTargetFillRate(boolean idle)

protected void
private boolean
public boolean
public void
markUsed(long
the position in bytes
pos
,
int
the number of bytes
length
)

Implements abstract org.h2.mvstore.FileStore.markUsed.

Mark the space as in use.

private long
private boolean

Returns:

true if block was moved, false otherwise
moveChunk
(SFChunk
to move
chunk
,
long
low boundary of reserved area, inclusive
reservedAreaLow
,
long
high boundary of reserved area, exclusive
reservedAreaHigh
)

Move specified chunk into free area of the file.

private boolean
moveChunkInside(SFChunk chunkToMove, long boundary)

private long

Returns:

the starting block index
predictAllocation
(int
the number of blocks to allocate
blocks
,
long
start block index of the reserved area (inclusive)
reservedLow
,
long
end block index of the reserved area (exclusive), special value -1 means beginning of the infinite free area
reservedHigh
)

Calculate starting position of the prospective allocation.

protected void
readStoreHeader(boolean recoveryMode)

Implements abstract org.h2.mvstore.FileStore.readStoreHeader.

public void
public boolean
shouldSaveNow(int
amount of unsaved memory, so far
unsavedMemory
,
int
configured limit on amount of unsaved memory
autoCommitMemory
)

Implements abstract org.h2.mvstore.FileStore.shouldSaveNow.

Decision about autocommit is delegated to store

private boolean
shouldWriteStoreHeader(SFChunk c, boolean storeAtEndOfFile)

private void
shrinkIfPossible(int minPercent)

protected void
shrinkStoreIfPossible(int
the minimum percentage to save
minPercent
)

Implements abstract org.h2.mvstore.FileStore.shrinkStoreIfPossible.

Shrink the store if possible, and if at least a given percentage can be saved.

private void
store(long reservedLow, long reservedHigh)

protected abstract void
truncate(long size)

protected final boolean
protected final void
writeChunk(SFChunk
chunk to write
chunk
,
WriteBuffer
to write
buffer
)

Implements abstract org.h2.mvstore.FileStore.writeChunk.

Write buffer associated with chunk into store at chunk's allocated position

protected final void
writeCleanShutdownMark()

Implements abstract org.h2.mvstore.FileStore.writeCleanShutdownMark.

Performs final preparation before store is closed normally

private void
Inherited from org.h2.mvstore.FileStore:
accountForRemovedPagebackupbindcachePagecacheToCclosecompactcompactStorederegisterMapRootdiscoverChunkdropUnusedChunksexecuteFileStoreOperationfindLastChunkWithCompleteValidChunkSetgetAutoCommitDelaygetAutoCompactFillRategetCacheHitRatiogetCacheSizegetCacheSizeUsedgetChunksgetChunksFillRategetChunksFromLayoutMapgetCreationTimegetDefaultRetentionTimegetFileNamegetLayoutMapgetMaxPageSizegetMetaMapIdgetMvStoregetReadBytesgetReadCountgetRetentionTimegetRewritableChunksFillRategetRootPosgetStoreHeadergetTocCacheHitRatiogetWriteBuffergetWriteCounthasChangesSincehasPersistentDatainitinitializeCommonHeaderAttributesisBackgroundThreadisIdleisKnownVersionisReadOnlyisRegularMaplastChunkVersionopenopenpopulateInfoprocessCommonHeaderAttributesreadChunkFooterreadChunkHeaderAndFooterreadChunkHeaderOptionallyreadChunkHeaderOptionallyreadFullyreadFullyreadPageregisterDeadChunkreleaseWriteBufferrewriteChunksrollbackTosaveChunkMetadataChangessetAutoCommitDelaysetCacheSizesetLastChunksetRetentionTimesetSizesizestartstopstorestoreItsyncwriteCleanShutdownwriteFullywriteInBackground

Field Detail

freeSpaceback to summary
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).

reservedHighback to summary
private long reservedHigh
reservedLowback to summary
private long reservedLow
reuseSpaceback to summary
private volatile boolean reuseSpace

Allocation mode: false - new chunk is always allocated at the end of file true - new chunk is allocated as close to the beginning of file, as possible

stopIdleHousekeepingback to summary
private boolean stopIdleHousekeeping

Constructor Detail

RandomAccessStoreback to summary
public RandomAccessStore(Map<String, Object> config)

Method Detail

adjustStoreToLastChunkback to summary
protected final void adjustStoreToLastChunk()

Implements abstract org.h2.mvstore.FileStore.adjustStoreToLastChunk.

Doc from org.h2.mvstore.FileStore.adjustStoreToLastChunk.

Make persistent changes after lastChunk was reset

Annotations
@Override
allocateback to summary
private long allocate(int length, long reservedLow, long reservedHigh)

Allocate a number of blocks and mark them as used.

Parameters
length:int

the number of bytes to allocate

reservedLow:long

start block index of the reserved area (inclusive)

reservedHigh:long

end block index of the reserved area (exclusive), special value -1 means beginning of the infinite free area

Returns:long

the start position in bytes

allocateChunkSpaceback to summary
protected final void allocateChunkSpace(SFChunk chunk, WriteBuffer buff)

Implements abstract org.h2.mvstore.FileStore.allocateChunkSpace.

Doc from org.h2.mvstore.FileStore.allocateChunkSpace.

Allocate logical space and assign position of the buffer within the store.

Parameters
chunk:SFChunk

to allocate space for

buff:WriteBuffer

to allocate space for

Annotations
@Override
clearback to summary
public void clear()

Overrides org.h2.mvstore.FileStore.clear.

Mark the file as empty.

Annotations
@Override
compactMoveChunksback to summary
public void compactMoveChunks(int targetFillRate, long moveSize, MVStore mvStore)

Compact the store by moving all chunks next to each other, if there is free space between chunks. This might temporarily increase the file size. Chunks are overwritten irrespective of the current retention time. Before overwriting chunks and before resizing the file, syncFile() is called.

Parameters
targetFillRate:int

do nothing if the file store fill rate is higher than this

moveSize:long

the number of bytes to move

mvStore:MVStore

owner of this store

compactMoveChunksback to summary
private void compactMoveChunks(long moveSize)
compactMoveChunksback to summary
private void compactMoveChunks(Iterable<SFChunk> move)
compactStoreback to summary
protected void compactStore(int thresholdFillRate, long maxCompactTime, int maxWriteSize, MVStore mvStore)

Implements abstract org.h2.mvstore.FileStore.compactStore.

Compact store file, that is, compact blocks that have a low fill rate, and move chunks next to each other. This will typically shrink the file. Changes are flushed to the file, and old chunks are overwritten.

Parameters
thresholdFillRate:int

do not compact if store fill rate above this value (0-100)

maxCompactTime:long

the maximum time in milliseconds to compact

maxWriteSize:int

the maximum amount of data to be written as part of this call

mvStore:MVStore

Doc from org.h2.mvstore.FileStore.compactStore.

that owns this FileStore

Annotations
@Override
createChunkback to summary
protected final SFChunk createChunk(int newChunkId)

Implements abstract org.h2.mvstore.FileStore.createChunk.

Annotations
@Override
createChunkback to summary
public SFChunk createChunk(String s)

Implements abstract org.h2.mvstore.FileStore.createChunk.

Doc from org.h2.mvstore.FileStore.createChunk.

Build a Chunk from the given string.

Parameters
s:String

the string

Returns:SFChunk

the Chunk created

Annotations
@Override
createChunkback to summary
protected SFChunk createChunk(Map<String, String> map)

Implements abstract org.h2.mvstore.FileStore.createChunk.

Annotations
@Override
doHousekeepingback to summary
protected void doHousekeeping(MVStore mvStore) throws InterruptedException

Implements abstract org.h2.mvstore.FileStore.doHousekeeping.

Annotations
@Override
findChunksToMoveback to summary
private Iterable<SFChunk> findChunksToMove(long startBlock, long moveSize)
freeback to summary
protected void free(long pos, int length)

Mark the space as free.

Parameters
pos:long

the position in bytes

length:int

the number of bytes

freeChunkSpaceback to summary
protected void freeChunkSpace(Iterable<SFChunk> chunks)

Implements abstract org.h2.mvstore.FileStore.freeChunkSpace.

Doc from org.h2.mvstore.FileStore.freeChunkSpace.

Mark the space occupied by specified chunks as free.

Parameters
chunks:Iterable<SFChunk>

chunks to be processed

Annotations
@Override
freeChunkSpaceback to summary
private void freeChunkSpace(SFChunk chunk)
getAfterLastBlockback to summary
private long getAfterLastBlock()

Get the index of the first block after last occupied one. It marks the beginning of the last (infinite) free space.

Returns:long

block index

getAfterLastBlock_back to summary
protected long getAfterLastBlock_()
getFileLengthInUseback to summary
pack-priv long getFileLengthInUse()
getFillRateback to summary
public int getFillRate()

Implements abstract org.h2.mvstore.FileStore.getFillRate.

Annotations
@Override
getFirstFreeback to summary
pack-priv long getFirstFree()
getMovePriorityback to summary
private int getMovePriority(SFChunk chunk)
getMovePriorityback to summary
public int getMovePriority(int block)

Calculates relative "priority" for chunk to be moved.

Parameters
block:int

where chunk starts

Returns:int

priority, bigger number indicate that chunk need to be moved sooner

getRewriteCandidatesback to summary
public Collection<SFChunk> getRewriteCandidates()

Overrides org.h2.mvstore.FileStore.getRewriteCandidates.

Annotations
@Override
getTargetFillRateback to summary
private int getTargetFillRate(boolean idle)
initializeStoreHeaderback to summary
protected void initializeStoreHeader(long time)

Implements abstract org.h2.mvstore.FileStore.initializeStoreHeader.

Annotations
@Override
isFragmentedback to summary
private boolean isFragmented()
isSpaceReusedback to summary
public boolean isSpaceReused()

Overrides org.h2.mvstore.FileStore.isSpaceReused.

Annotations
@Override
markUsedback to summary
public void markUsed(long pos, int length)

Implements abstract org.h2.mvstore.FileStore.markUsed.

Mark the space as in use.

Parameters
pos:long

the position in bytes

length:int

the number of bytes

Annotations
@Override
measureFileLengthInUseback to summary
private long measureFileLengthInUse()
moveChunkback to summary
private boolean moveChunk(SFChunk chunk, long reservedAreaLow, long reservedAreaHigh)

Move specified chunk into free area of the file. "Reserved" area specifies file interval to be avoided, when un-allocated space will be chosen for a new chunk's location.

Parameters
chunk:SFChunk

to move

reservedAreaLow:long

low boundary of reserved area, inclusive

reservedAreaHigh:long

high boundary of reserved area, exclusive

Returns:boolean

true if block was moved, false otherwise

moveChunkInsideback to summary
private boolean moveChunkInside(SFChunk chunkToMove, long boundary)
predictAllocationback to summary
private long predictAllocation(int blocks, long reservedLow, long reservedHigh)

Calculate starting position of the prospective allocation.

Parameters
blocks:int

the number of blocks to allocate

reservedLow:long

start block index of the reserved area (inclusive)

reservedHigh:long

end block index of the reserved area (exclusive), special value -1 means beginning of the infinite free area

Returns:long

the starting block index

readStoreHeaderback to summary
protected void readStoreHeader(boolean recoveryMode)

Implements abstract org.h2.mvstore.FileStore.readStoreHeader.

Annotations
@Override
setReuseSpaceback to summary
public void setReuseSpace(boolean reuseSpace)

Overrides org.h2.mvstore.FileStore.setReuseSpace.

Annotations
@Override
shouldSaveNowback to summary
public boolean shouldSaveNow(int unsavedMemory, int autoCommitMemory)

Implements abstract org.h2.mvstore.FileStore.shouldSaveNow.

Doc from org.h2.mvstore.FileStore.shouldSaveNow.

Decision about autocommit is delegated to store

Parameters
unsavedMemory:int

amount of unsaved memory, so far

autoCommitMemory:int

configured limit on amount of unsaved memory

Returns:boolean

true if commit should happen now

Annotations
@Override
shouldWriteStoreHeaderback to summary
private boolean shouldWriteStoreHeader(SFChunk c, boolean storeAtEndOfFile)
shrinkIfPossibleback to summary
private void shrinkIfPossible(int minPercent)
shrinkStoreIfPossibleback to summary
protected void shrinkStoreIfPossible(int minPercent)

Implements abstract org.h2.mvstore.FileStore.shrinkStoreIfPossible.

Shrink the store if possible, and if at least a given percentage can be saved.

Parameters
minPercent:int

the minimum percentage to save

Annotations
@Override
storeback to summary
private void store(long reservedLow, long reservedHigh)
truncateback to summary
protected abstract void truncate(long size)
validateFileLengthback to summary
protected final boolean validateFileLength(String msg)

Implements abstract org.h2.mvstore.FileStore.validateFileLength.

Annotations
@Override
writeChunkback to summary
protected final void writeChunk(SFChunk chunk, WriteBuffer buffer)

Implements abstract org.h2.mvstore.FileStore.writeChunk.

Doc from org.h2.mvstore.FileStore.writeChunk.

Write buffer associated with chunk into store at chunk's allocated position

Parameters
chunk:SFChunk

chunk to write

buffer:WriteBuffer

to write

Annotations
@Override
writeCleanShutdownMarkback to summary
protected final void writeCleanShutdownMark()

Implements abstract org.h2.mvstore.FileStore.writeCleanShutdownMark.

Doc from org.h2.mvstore.FileStore.writeCleanShutdownMark.

Performs final preparation before store is closed normally

Annotations
@Override
writeStoreHeaderback to summary
private void writeStoreHeader()