MVStore

A persistent storage for maps.

Methods
static MVStore open(String fileName)
Open a store in exclusive mode.
static MVStore open(String fileName)
Open a store in exclusive mode. For a file-based store, the parent directory must already exist.
Parameters:
fileName - the file name (null for in-memory)
Returns:
the store
MVStore(Map config)
Create and open the store.
MVStore(Map config)
Create and open the store.
Parameters:
config - the configuration to use
Throws:
IllegalStateException - if the file is corrupt, or an exception occurred while opening
IllegalArgumentException - if the directory does not exist
void beforeWrite(MVMap map)
This method is called before writing to a map.
void beforeWrite(MVMap map)
This method is called before writing to a map.
Parameters:
map - the map
void cachePage(long pos, Page page, int memory)
Put the page in the cache.
void cachePage(long pos, Page page, int memory)
Put the page in the cache.
Parameters:
pos - the page position
page - the page
memory - the memory used
void close()
Close the file and the store.
void close()
Close the file and the store. Unsaved changes are written to disk first.
void closeImmediately()
Close the file and the store, without writing anything.
void closeImmediately()
Close the file and the store, without writing anything. This will stop the background thread. This method ignores all errors.
long commit()
Commit the changes.
long commit()
Commit the changes.

For in-memory stores, this method increments the version.

For persistent stores, it also writes changes to disk. It does nothing if there are no unsaved changes, and returns the old version. It is not necessary to call this method when auto-commit is enabled (the default setting), as in this case it is automatically called from time to time or when enough changes have accumulated. However, it may still be called to flush all changes to disk.

Returns:
the new version
boolean compact(int targetFillRate, int write)
Try to increase the fill rate by re-writing partially full chunks.
boolean compact(int targetFillRate, int write)
Try to increase the fill rate by re-writing partially full chunks. Chunks with a low number of live items are re-written.

If the current fill rate is higher than the target fill rate, nothing is done.

Please note this method will not necessarily reduce the file size, as empty chunks are not overwritten.

Only data of open maps can be moved. For maps that are not open, the old chunk is still referenced. Therefore, it is recommended to open all maps before calling this method.

Parameters:
targetFillRate - the minimum percentage of live entries
write - the minimum number of bytes to write
Returns:
if a chunk was re-written
boolean compactMoveChunks()
Compact by moving all chunks next to each other.
boolean compactMoveChunks()
Compact by moving all chunks next to each other.
Returns:
if anything was written
boolean compactMoveChunks(int targetFillRate, long moveSize)
Compact the store by moving all chunks next to each other, if there is free space between chunks.
boolean compactMoveChunks(int targetFillRate, long moveSize)
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 - do nothing if the file store fill rate is higher than this
moveSize - the number of bytes to move
Returns:
if anything was written
boolean compactRewriteFully()
Compact the store by moving all live pages to new chunks.
boolean compactRewriteFully()
Compact the store by moving all live pages to new chunks.
Returns:
if anything was written
int getAutoCommitDelay()
Get the auto-commit delay.
int getAutoCommitDelay()
Get the auto-commit delay.
Returns:
the delay in milliseconds, or 0 if auto-commit is disabled.
int getAutoCommitMemory()
Get the maximum memory (in bytes) used for unsaved pages.
int getAutoCommitMemory()
Get the maximum memory (in bytes) used for unsaved pages. If this number is exceeded, unsaved changes are stored to disk.
Returns:
the memory in bytes
CacheLongKeyLIRS getCache()
Get the cache.
CacheLongKeyLIRS getCache()
Get the cache.
Returns:
the cache
int getCacheSize()
Get the maximum cache size, in MB.
int getCacheSize()
Get the maximum cache size, in MB. Note that this does not include the page chunk references cache, which is 25% of the size of the page cache.
Returns:
the cache size
int getCacheSizeUsed()
Get the amount of memory used for caching, in MB.
int getCacheSizeUsed()
Get the amount of memory used for caching, in MB. Note that this does not include the page chunk references cache, which is 25% of the size of the page cache.
Returns:
the amount of memory used for caching
int getCompressionLevel()
int getCompressionLevel()
Compressor getCompressorFast()
Compressor getCompressorFast()
Compressor getCompressorHigh()
Compressor getCompressorHigh()
int getCurrentFillRate()
Get the current fill rate (percentage of used space in the file).
int getCurrentFillRate()
Get the current fill rate (percentage of used space in the file). Unlike the fill rate of the store, here we only account for chunk data; the fill rate here is how much of the chunk data is live (still referenced). Young chunks are considered live.
Returns:
the fill rate, in percent (100 is completely full)
long getCurrentVersion()
Get the current version of the data.
long getCurrentVersion()
Get the current version of the data. When a new store is created, the version is 0.
Returns:
the version
FileStore getFileStore()
Get the file store.
FileStore getFileStore()
Get the file store.
Returns:
the file store
String getMapName(int id)
Get the name of the given map.
String getMapName(int id)
Get the name of the given map.
Parameters:
id - the map id
Returns:
the name, or null if not found
Set getMapNames()
Get the set of all map names.
Set getMapNames()
Get the set of all map names.
Returns:
the set of names
MVMap getMetaMap()
Get the metadata map.
MVMap getMetaMap()
Get the metadata map. This data is for informational purposes only. The data is subject to change in future versions.

The data in this map should not be modified (changing system data may corrupt the store). If modifications are needed, they need be synchronized on the store.

The metadata map contains the following entries:

 chunk.{chunkId} = {chunk metadata}
 name.{name} = {mapId}
 map.{mapId} = {map metadata}
 root.{mapId} = {root position}
 setting.storeVersion = {version}
Returns:
the metadata map
long getOldestVersionToKeep()
Get the oldest version to retain in memory, which is the manually set retain version, or the current store version (whatever is older).
long getOldestVersionToKeep()
Get the oldest version to retain in memory, which is the manually set retain version, or the current store version (whatever is older).
Returns:
the version
int getPageSplitSize()
int getPageSplitSize()
int getRetentionTime()
int getRetentionTime()
boolean getReuseSpace()
boolean getReuseSpace()
Map getStoreHeader()
Get the store header.
Map getStoreHeader()
Get the store header. This data is for informational purposes only. The data is subject to change in future versions. The data should not be modified (doing so may corrupt the store).
Returns:
the store header
int getStoreVersion()
Get the store version.
int getStoreVersion()
Get the store version. The store version is usually used to upgrade the structure of the store after upgrading the application. Initially the store version is 0, until it is changed.
Returns:
the store version
int getUnsavedMemory()
Get the estimated memory (in bytes) of unsaved data.
int getUnsavedMemory()
Get the estimated memory (in bytes) of unsaved data. If the value exceeds the auto-commit memory, the changes are committed.

The returned value is an estimation only.

Returns:
the memory in bytes
long getVersionsToKeep()
Get the oldest version to retain in memory (for in-memory stores).
long getVersionsToKeep()
Get the oldest version to retain in memory (for in-memory stores).
Returns:
the version
boolean hasMap(String name)
Check whether a given map exists.
boolean hasMap(String name)
Check whether a given map exists.
Parameters:
name - the map name
Returns:
true if it exists
boolean hasUnsavedChanges()
Check whether there are any unsaved changes.
boolean hasUnsavedChanges()
Check whether there are any unsaved changes.
Returns:
if there are any changes
boolean isClosed()
boolean isClosed()
boolean isReadOnly()
Whether the store is read-only.
boolean isReadOnly()
Whether the store is read-only.
Returns:
true if it is
MVMap openMap(String name)
Open a map with the default settings.
MVMap openMap(String name)
Open a map with the default settings. The map is automatically create if it does not yet exist. If a map with this name is already open, this map is returned.
Parameters:
name - the name of the map
Returns:
the map
M openMap(String name, MVMap.MapBuilder builder)
Open a map with the given builder.
M openMap(String name, MVMap.MapBuilder builder)
Open a map with the given builder. The map is automatically create if it does not yet exist. If a map with this name is already open, this map is returned.
Parameters:
name - the name of the map
builder - the map builder
Returns:
the map
T openMapVersion(long version, int mapId, MVMap template)
Open an old, stored version of a map.
T openMapVersion(long version, int mapId, MVMap template)
Open an old, stored version of a map.
Parameters:
version - the version
mapId - the map id
template - the template map
Returns:
the read-only map
Page readPage(MVMap map, long pos)
Read a page.
Page readPage(MVMap map, long pos)
Read a page.
Parameters:
map - the map
pos - the page position
Returns:
the page
void registerUnsavedPage(int memory)
Increment the number of unsaved pages.
void registerUnsavedPage(int memory)
Increment the number of unsaved pages.
Parameters:
memory - the memory usage of the page
void removeMap(MVMap map)
Remove a map.
void removeMap(MVMap map)
Remove a map. Please note rolling back this operation does not restore the data; if you need this ability, use Map.clear().
Parameters:
map - the map to remove
void removePage(MVMap map, long pos, int memory)
Remove a page.
void removePage(MVMap map, long pos, int memory)
Remove a page.
Parameters:
map - the map the page belongs to
pos - the position of the page
memory - the memory usage
void renameMap(MVMap map, String newName)
Rename a map.
void renameMap(MVMap map, String newName)
Rename a map.
Parameters:
map - the map
newName - the new name
void rollback()
Revert to the beginning of the current version, reverting all uncommitted changes.
void rollback()
Revert to the beginning of the current version, reverting all uncommitted changes.
void rollbackTo(long version)
Revert to the beginning of the given version.
void rollbackTo(long version)
Revert to the beginning of the given version. All later changes (stored or not) are forgotten. All maps that were created later are closed. A rollback to a version before the last stored version is immediately persisted. Rollback to version 0 means all data is removed.
Parameters:
version - the version to revert to
void setAutoCommitDelay(int millis)
Set the maximum delay in milliseconds to auto-commit changes.
void setAutoCommitDelay(int millis)
Set the maximum delay in milliseconds to auto-commit changes.

To disable auto-commit, set the value to 0. In this case, changes are only committed when explicitly calling commit.

The default is 1000, meaning all changes are committed after at most one second.

Parameters:
millis - the maximum delay
void setCacheSize(int mb)
Set the read cache size in MB.
void setCacheSize(int mb)
Set the read cache size in MB.
Parameters:
mb - the cache size in MB.
void setRetentionTime(int ms)
How long to retain old, persisted chunks, in milliseconds.
void setRetentionTime(int ms)
How long to retain old, persisted chunks, in milliseconds. Chunks that are older may be overwritten once they contain no live data.

The default value is 45000 (45 seconds) when using the default file store. It is assumed that a file system and hard disk will flush all write buffers within this time. Using a lower value might be dangerous, unless the file system and hard disk flush the buffers earlier. To manually flush the buffers, use MVStore.getFile().force(true), however please note that according to various tests this does not always work as expected depending on the operating system and hardware.

The retention time needs to be long enough to allow reading old chunks while traversing over the entries of a map.

This setting is not persisted.

Parameters:
ms - how many milliseconds to retain old chunks (0 to overwrite them as early as possible)
void setReuseSpace(boolean reuseSpace)
Whether empty space in the file should be re-used.
void setReuseSpace(boolean reuseSpace)
Whether empty space in the file should be re-used. If enabled, old data is overwritten (default). If disabled, writes are appended at the end of the file.

This setting is specially useful for online backup. To create an online backup, disable this setting, then copy the file (starting at the beginning of the file). In this case, concurrent backup and write operations are possible (obviously the backup process needs to be faster than the write operations).

Parameters:
reuseSpace - the new value
void setStoreVersion(int version)
Update the store version.
void setStoreVersion(int version)
Update the store version.
Parameters:
version - the new store version
void setVersionsToKeep(int count)
How many versions to retain for in-memory stores.
void setVersionsToKeep(int count)
How many versions to retain for in-memory stores. If not set, 5 old versions are retained.
Parameters:
count - the number of versions to keep
void sync()
Force all stored changes to be written to the storage.
void sync()
Force all stored changes to be written to the storage. The default implementation calls FileChannel.force(true).
void writeInBackground()
Commit and save all changes, if there are any, and compact the store if needed.
void writeInBackground()
Commit and save all changes, if there are any, and compact the store if needed.

Fields
static boolean ASSERT = false
static int BLOCK_SIZE = 4096
static MVStore.BackgroundWriterThread backgroundWriterThread

BLOCK_SIZE = 4096

The block size (physical sector size) of the disk. The store header is written twice, one copy in each block, to ensure it survives a crash.

backgroundWriterThread

The background thread, if any.

ASSERT = false

Whether assertions are enabled.