Top Description Inners Fields Constructors Methods
org.h2.mvstore

public abstract Class FileStore<C extends Chunk<C>>

extends Object
Class Inheritance
Known Direct Subclasses
org.h2.mvstore.RandomAccessStore, org.h2.mvstore.AppendOnlyMultiFileStore
Imports
org.h2.engine.Constants, org.h2.mvstore.cache.CacheLongKeyLIRS, org.h2.mvstore.type.StringDataType, org.h2.util.MathUtils, .Utils, java.io.IOException, java.nio.ByteBuffer, java.nio.channels.FileChannel, java.util.ArrayList, .Arrays, .BitSet, .Collection, .Comparator, .Deque, .HashMap, .HashSet, .Iterator, .List, .Map, .NoSuchElementException, .PriorityQueue, .Queue, .Set, .TreeMap, java.util.concurrent.ArrayBlockingQueue, .ConcurrentHashMap, .ConcurrentLinkedDeque, .ConcurrentMap, .ExecutionException, .Future, .PriorityBlockingQueue, .RejectedExecutionException, .ThreadPoolExecutor, java.util.concurrent.atomic.AtomicLong, .AtomicReference, java.util.concurrent.locks.ReentrantLock, java.util.function.BiConsumer, .IntSupplier, java.util.zip.ZipOutputStream

Class FileStore is a base class to allow for different store implementations. FileStore concept revolves around notion of a "chunk", which is a piece of data written into the store at once.
Author
Andrei Tokar

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
FileStore.BackgroundWriterThread

A background writer thread to automatically store changes from time to time.

public class
private static class

Field Summary

Modifier and TypeField and Description
private int
autoCommitDelay

The delay in milliseconds to automatically commit and write changes.

private final int
private long
private final AtomicReference<FileStore.BackgroundWriterThread>
backgroundWriterThread

Reference to a background thread, which is expected to be running, if any.

pack-priv static final int
BLOCK_SIZE

The block size (physical sector size) of the disk.

private ThreadPoolExecutor
bufferSaveExecutor

Single-threaded executor for saving ByteBuffer as a new Chunk

private int
private final CacheLongKeyLIRS<Page<?, ?>>
cache

The page cache.

pack-priv final ConcurrentHashMap<Integer, C>
chunks

The map of chunks.

private final CacheLongKeyLIRS<long[]>
chunksToC

Cache for chunks "Table of Content" used to translate page's sequential number within containing chunk into byte position within chunk's image.

private boolean
private long
creationTime

The time the store was created, in milliseconds since 1970.

private final Deque<C>
private String
fileName

The file name.

private static final int
private static final int
private static final int
private static final int
pack-priv static final String
pack-priv static final String
pack-priv static final String
pack-priv static final String
pack-priv static final String
pack-priv static final String
pack-priv static final String
pack-priv static final String
pack-priv static final String
pack-priv static final String
protected volatile C
lastChunk

The newest chunk.

private int
private long
private MVMap<String, String>
layout

The layout map.

private final int
public static final String
META_ID_KEY

The key for the entry within "layout" map, which contains id of "meta" map.

pack-priv MVStore
public static final int
protected final AtomicLong
readBytes

The number of read bytes.

protected final AtomicLong
readCount

The number of read operations.

private boolean
readOnly

Whether this store is read-only.

protected final boolean
private final Queue<FileStore.RemovedPageInfo>
private int
retentionTime

For how long (in milliseconds) to retain a persisted chunk after it becomes irrelevant (not in use, because it only contains data from some old versions).

protected final ReentrantLock
private ThreadPoolExecutor
serializationExecutor

Single-threaded executor for serialization of the store snapshot into ByteBuffer

private int
private final ReentrantLock
serializationLock

Lock guarding submission to serializationExecutor

private long
size

The file size (cached).

protected final HashMap<String, Object>
private final Queue<WriteBuffer>
protected final AtomicLong
writeBytes

The number of written bytes.

protected final AtomicLong
writeCount

The number of write operations.

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
private void
acceptChunkOccupancyChanges(long time, long version)

Apply the freed space to the chunk metadata.

public void
accountForRemovedPage(long
the position of the page
pos
,
long
at which page was removed
version
,
boolean
whether page is considered pinned
pinned
,
int
sequential page number within chunk
pageNo
)

Remove a page.

protected abstract void
adjustStoreToLastChunk()

Make persistent changes after lastChunk was reset

protected abstract void
allocateChunkSpace(C
to allocate space for
chunk
,
WriteBuffer
to allocate space for
buff
)

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

public abstract void
public final void
bind(MVStore mvStore)

pack-priv void
cachePage(Page<?, ?>
the page
page
)

Put the page in the cache.

pack-priv void
cacheToC(C chunk, long[] toc)

private int
calculatePageNo(long pos)

private static <C extends Chunk<C>> boolean
canOverwriteChunk(C c, long oldestVersionToKeep)

private long[]
cleanToCCache(C chunk)

public void
private void
public void
public boolean

Returns:

if any chunk was re-written
compact
(int
the minimum percentage of live entries
targetFillRate
,
int
the minimum number of bytes to write
write
)

Try to increase the fill rate by re-writing partially full chunks.

private int
public void
compactStore(long maxCompactTime)

protected abstract 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
)

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

private C
createChunk(long time, long version)

protected abstract C
createChunk(int id)

public abstract C

Returns:

the Chunk created
createChunk
(String
the string
s
)

Build a Chunk from the given string.

protected abstract C
private C[]
private static <C extends Chunk<C>> HashSet<Integer>
createIdSet(Iterable<C> toCompact)

public final boolean

Returns:

true if root was removed, false if it is not there
deregisterMapRoot
(int
to deregister
mapId
)

Performs final stage of map removal - delete root location info from the layout map.

protected final C

Returns:

valid chunk or null if none found
discoverChunk
(long
to start search from (found chunk footer should be no further than block-1)
block
)

Discover a valid chunk, searching file backwards from the given block

protected abstract void
public final void
public void
protected final boolean
findLastChunkWithCompleteValidChunkSet(Comparator<C> chunkComparator, Map<Long, C> validChunksByLocation, boolean afterFullScan)

private int
private Iterable<C>
findOldChunks(int writeLimit, int targetFillRate)

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

Mark the space occupied by specified chunks as free.

public final int

Returns:

the delay in milliseconds, or 0 if auto-commit is disabled.
getAutoCommitDelay
()

Get the auto-commit delay.

protected final int
public int
private static int
public int

Returns:

the cache size
getCacheSize
()

Get the maximum cache size, in MB.

public int

Returns:

the amount of memory used for caching
getCacheSizeUsed
()

Get the amount of memory used for caching, in MB.

private C

Returns:

the chunk
getChunk
(long
the position
pos
)

Get the chunk for the given position.

private int

Returns:

number of existing chunks in store.
getChunkCount
()

Get data chunks count.

private C
getChunkForVersion(long version)

protected final ConcurrentMap<Integer, C>
public int

Returns:

the fill rate, in percent (100 is completely full)
getChunksFillRate
()

Get the current fill rate (percentage of used space in the file).

private int
getChunksFillRate(boolean all)

protected Iterable<C>
private Iterable<C>
public long

Returns:

creation time
getCreationTime
()

The time the store was created, in milliseconds since 1970.

public int

Returns:

the retention time
getDefaultRetentionTime
()

Get the default retention time for this store in milliseconds.

public String

Returns:

the file name
getFileName
()

Get the file name.

public abstract int
public final Map<String, String>

Returns:

the metadata map
getLayoutMap
()

Get this store's layout map.

private MVMap<String, String>
getLayoutMap(long version)

private int

Returns:

number of existing live pages in store.
getLivePageCount
()

Get live data pages count.

public final long
public final int
getMetaMapId(IntSupplier nextIdSupplier)

protected final MVStore
private int

Returns:

number of existing pages in store.
getPageCount
()

Get data pages count.

public long

Returns:

the number of write operations
getReadBytes
()

Get the number of read bytes since this store was opened.

public long

Returns:

the number of read operations
getReadCount
()

Get the number of read operations since this store was opened.

public final int
pack-priv int
protected Collection<C>
public final long

Returns:

opaque "position" value, that should be used to read the page
getRootPos
(int
to get root position for
mapId
)

Get "position" of the root page for the specified map

public Map<String, Object>

Returns:

the store header
getStoreHeader
()

Get the store header.

private long
private long[]
getToC(C chunk)

public int
private MVStoreException
getUnsupportedWriteFormatException(long format, int expectedFormat, String s)

public WriteBuffer

Returns:

the buffer
getWriteBuffer
()

Get a buffer for writing.

private long

Returns:

the number of write operations
getWriteBytes
()

Get the number of written bytes since this store was opened.

public long

Returns:

the number of write operations
getWriteCount
()

Get the number of write operations since this store was opened.

public final boolean

Returns:

if there are any changes
hasChangesSince
(long
version to take as a base for changes
lastStoredVersion
)

Check whether there are any unsaved changes since specified version.

protected final boolean
protected final void
init(String fileName, boolean readOnly)

protected final void
protected abstract void
pack-priv boolean
protected final boolean
public final boolean

Returns:

true if all data can be read
isKnownVersion
(long
the version
version
)

Check whether all data can be read from this version.

public boolean
public final boolean
isRegularMap(MVMap<?, ?> map)

private boolean
isRewritable(C chunk, long time)

private boolean
isSeasonedChunk(C chunk, long time)

public boolean
private boolean

Returns:

true if Chunk exists in the file and is valid, false otherwise
isValidChunk
(C
to verify existence
chunk
)

Read a chunk header and footer, and verify the stored data is consistent.

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

Mark the space as in use.

public abstract void
open(String fileName, boolean readOnly, char[] encryptionKey)

public abstract FileStore<C>
open(String fileName, boolean readOnly)

public void
protected final void
protected final C

Returns:

the chunk, or null if not successful
readChunkFooter
(long
the index of the next block after the chunk
block
)

Try to read a chunk footer.

private C
readChunkHeader(long block)

protected final C

Returns:

the chunk, or null if the header or footer don't match or are not consistent
readChunkHeaderAndFooter
(long
the block
block
,
int
of the chunk
expectedId
)

Read a chunk header and footer, and verify the stored data is consistent.

protected final C
readChunkHeaderOptionally(long block, int expectedId)

protected final C
public abstract ByteBuffer

Returns:

the byte buffer with data requested
readFully
(C
that owns data to be read
chunk
,
long
the read "position"
pos
,
int
the number of bytes to read
len
)

Read data from the store.

protected final ByteBuffer
readFully(FileChannel file, long pos, int len)

pack-priv <K, V> Page<K, V>

Returns:

the page
readPage
(MVMap<K, V>
the map
map
,
long
the page position
pos
)

Read a page.

private <K, V> Page<K, V>
protected abstract void
readStoreHeader(boolean recoveryMode)

protected final void
public void
releaseWriteBuffer(WriteBuffer
the buffer than can be re-used
buff
)

Release a buffer for writing.

protected boolean
rewriteChunks(int writeLimit, int targetFillRate)

private int
rewriteChunks(Set<Integer> set, boolean secondPass)

public final void
rollbackTo(long version)

public void
saveChunkMetadataChanges(C
to save
chunk
)

Store chunk's serialized metadata as an entry in a layout map.

private void
private void
serializeAndStore(boolean syncRun, ArrayList<Page<?, ?>> changed, long time, long version)

private void
serializeToBuffer(WriteBuffer buff, ArrayList<Page<?, ?>> changed, C c, C previousChunk)

public final void
setAutoCommitDelay(int
the maximum delay
millis
)

Set the maximum delay in milliseconds to auto-commit changes.

public void
setCacheSize(int
the cache size in MB.
mb
)

Set the read cache size in MB.

protected final void
setLastChunk(C last)

public final void
setRetentionTime(int
how many milliseconds to retain old chunks (0 to overwrite them as early as possible)
ms
)

How long to retain old, persisted chunks, in milliseconds.

public void
setReuseSpace(boolean reuseSpace)

protected final void
setSize(long size)

public abstract boolean

Returns:

true if commit should happen now
shouldSaveNow
(int
amount of unsaved memory, so far
unsavedMemory
,
int
configured limit on amount of unsaved memory
autoCommitMemory
)

Decision about autocommit is delegated to store

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

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

private void
public long

Returns:

the file size
size
()

Get the file size.

public MVMap<String, String>
public final void
stop(long allowedCompactionTime)

private void
stopBackgroundThread(boolean waitForIt)

protected final void
private void
pack-priv final void
storeIt(ArrayList<Page<?, ?>> changed, long version, boolean syncWrite)

private static int
submitOrRun(ThreadPoolExecutor executor, Runnable action, boolean syncRun, int threshold, int hwm)

public void
sync()

protected abstract boolean
protected abstract void
writeChunk(C
chunk to write
chunk
,
WriteBuffer
to write
buffer
)

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

protected void
protected abstract void
writeCleanShutdownMark()

Performs final preparation before store is closed normally

protected abstract void
writeFully(C
to write
chunk
,
long
the write position
pos
,
ByteBuffer
the source buffer
src
)

Write to the file.

pack-priv void
writeInBackground()

Commit and save all changes, if there are any, and compact the store if needed.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait