public class MVStore
extends java.lang.Object
implements java.lang.AutoCloseable
| Modifier and Type | Class and Description |
|---|---|
static class |
MVStore.Builder
A builder for an MVStore.
|
static class |
MVStore.TxCounter
Class TxCounter is a simple data structure to hold version of the store
along with the counter of open transactions,
which are still operating on this version.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Close the file and the store.
|
void |
closeImmediately()
Close the file and the store, without writing anything.
|
long |
commit()
Commit the changes.
|
boolean |
compact(int targetFillRate,
int write)
Try to increase the fill rate by re-writing partially full chunks.
|
void |
compactMoveChunks()
Compact by moving all chunks next to each other.
|
void |
compactMoveChunks(int targetFillRate,
long moveSize)
Compact the store by moving all chunks next to each other, if there is
free space between chunks.
|
boolean |
compactRewriteFully()
Compact the store by moving all live pages to new chunks.
|
void |
deregisterVersionUsage(MVStore.TxCounter txCounter)
De-register (close) completed operation (transaction).
|
int |
getAutoCommitDelay()
Get the auto-commit delay.
|
int |
getAutoCommitMemory()
Get the maximum memory (in bytes) used for unsaved pages.
|
CacheLongKeyLIRS<Page> |
getCache()
Get the cache.
|
int |
getCacheSize()
Get the maximum cache size, in MB.
|
int |
getCacheSizeUsed()
Get the amount of memory used for caching, in MB.
|
int |
getCurrentFillRate()
Get the current fill rate (percentage of used space in the file).
|
long |
getCurrentVersion()
Get the current version of the data.
|
FileStore |
getFileStore()
Get the file store.
|
int |
getKeysPerPage() |
long |
getLastStoredVersion() |
<K,V> MVMap<K,V> |
getMap(int id)
Get map by id.
|
java.lang.String |
getMapName(int id)
Get the name of the given map.
|
java.util.Set<java.lang.String> |
getMapNames()
Get the set of all map names.
|
long |
getMaxPageSize() |
MVMap<java.lang.String,java.lang.String> |
getMetaMap()
Get 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).
|
int |
getPageSplitSize() |
java.lang.IllegalStateException |
getPanicException() |
int |
getRetentionTime() |
boolean |
getReuseSpace() |
java.util.Map<java.lang.String,java.lang.Object> |
getStoreHeader()
Get the store header.
|
int |
getStoreVersion()
Get the store version.
|
int |
getUnsavedMemory()
Get the estimated memory (in bytes) of unsaved data.
|
double |
getUpdateFailureRatio() |
long |
getVersionsToKeep()
Get the oldest version to retain in memory (for in-memory stores).
|
boolean |
hasData(java.lang.String name)
Check whether a given map exists and has data.
|
boolean |
hasMap(java.lang.String name)
Check whether a given map exists.
|
boolean |
hasUnsavedChanges()
Check whether there are any unsaved changes.
|
boolean |
isClosed()
Determine that store is open, or wait for it to be closed (by other thread)
|
boolean |
isReadOnly()
Whether the store is read-only.
|
boolean |
isSaveNeeded() |
static MVStore |
open(java.lang.String fileName)
Open a store in exclusive mode.
|
<K,V> MVMap<K,V> |
openMap(java.lang.String name)
Open a map with the default settings.
|
<M extends MVMap<K,V>,K,V> |
openMap(java.lang.String name,
MVMap.MapBuilder<M,K,V> builder)
Open a map with the given builder.
|
void |
registerUnsavedPage(int memory)
Increment the number of unsaved pages.
|
MVStore.TxCounter |
registerVersionUsage()
Register opened operation (transaction).
|
void |
removeMap(MVMap<?,?> map)
Remove a map.
|
void |
removeMap(MVMap<?,?> map,
boolean delayed)
Remove a map.
|
void |
removeMap(java.lang.String name)
Remove map by name.
|
void |
renameMap(MVMap<?,?> map,
java.lang.String newName)
Rename a map.
|
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 |
setAutoCommitDelay(int millis)
Set the maximum delay in milliseconds to auto-commit changes.
|
void |
setCacheSize(int mb)
Set the read cache size in MB.
|
void |
setRetentionTime(int ms)
How long to retain old, persisted chunks, in milliseconds.
|
void |
setReuseSpace(boolean reuseSpace)
Whether empty space in the file should be re-used.
|
void |
setStoreVersion(int version)
Update the store version.
|
void |
setVersionsToKeep(int count)
How many versions to retain for in-memory stores.
|
void |
sync()
Force all stored changes to be written to the storage.
|
long |
tryCommit()
Unlike regular commit this method returns immediately if there is commit
in progress on another thread, otherwise it acts as regular commit.
|
public java.lang.IllegalStateException getPanicException()
public static MVStore open(java.lang.String fileName)
fileName - the file name (null for in-memory)public <K,V> MVMap<K,V> openMap(java.lang.String name)
K - the key typeV - the value typename - the name of the mappublic <M extends MVMap<K,V>,K,V> M openMap(java.lang.String name, MVMap.MapBuilder<M,K,V> builder)
M - the map typeK - the key typeV - the value typename - the name of the mapbuilder - the map builderpublic <K,V> MVMap<K,V> getMap(int id)
K - the key typeV - the value typeid - map idpublic java.util.Set<java.lang.String> getMapNames()
public MVMap<java.lang.String,java.lang.String> getMetaMap()
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}
public boolean hasMap(java.lang.String name)
name - the map namepublic boolean hasData(java.lang.String name)
name - the map namepublic void close()
close in interface java.lang.AutoCloseablepublic void closeImmediately()
public long tryCommit()
public long commit()
This method does nothing if there are no unsaved changes, otherwise it increments the current version and stores the data (for file based stores).
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.
At most one store operation may run at any time.
public boolean hasUnsavedChanges()
public boolean compactRewriteFully()
public void compactMoveChunks()
public void compactMoveChunks(int targetFillRate,
long moveSize)
targetFillRate - do nothing if the file store fill rate is higher
than thismoveSize - the number of bytes to movepublic void sync()
public boolean compact(int targetFillRate,
int write)
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.
targetFillRate - the minimum percentage of live entrieswrite - the minimum number of bytes to writepublic int getCurrentFillRate()
public int getPageSplitSize()
public int getKeysPerPage()
public long getMaxPageSize()
public boolean getReuseSpace()
public void setReuseSpace(boolean reuseSpace)
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).
reuseSpace - the new valuepublic int getRetentionTime()
public void setRetentionTime(int ms)
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.
ms - how many milliseconds to retain old chunks (0 to overwrite them
as early as possible)public void setVersionsToKeep(int count)
count - the number of versions to keeppublic long getVersionsToKeep()
public long getOldestVersionToKeep()
public void registerUnsavedPage(int memory)
memory - the memory usage of the pagepublic boolean isSaveNeeded()
public int getStoreVersion()
public void setStoreVersion(int version)
version - the new store versionpublic void rollback()
public void rollbackTo(long version)
version - the version to revert topublic long getCurrentVersion()
public long getLastStoredVersion()
public FileStore getFileStore()
public java.util.Map<java.lang.String,java.lang.Object> getStoreHeader()
public void renameMap(MVMap<?,?> map, java.lang.String newName)
map - the mapnewName - the new namepublic void removeMap(MVMap<?,?> map)
map - the map to removepublic void removeMap(MVMap<?,?> map, boolean delayed)
map - the map to removedelayed - whether to delay deleting the metadatapublic void removeMap(java.lang.String name)
name - the map namepublic java.lang.String getMapName(int id)
id - the map idpublic void setCacheSize(int mb)
mb - the cache size in MB.public boolean isClosed()
public void setAutoCommitDelay(int millis)
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.
millis - the maximum delaypublic int getAutoCommitDelay()
public int getAutoCommitMemory()
public int getUnsavedMemory()
The returned value is an estimation only.
public int getCacheSizeUsed()
public int getCacheSize()
public CacheLongKeyLIRS<Page> getCache()
public boolean isReadOnly()
public double getUpdateFailureRatio()
public MVStore.TxCounter registerVersionUsage()
public void deregisterVersionUsage(MVStore.TxCounter txCounter)
txCounter - to be decremented, obtained from registerVersionUsage()