org.h2.mvstore
Class MVStore

java.lang.Object
  extended by org.h2.mvstore.MVStore

public class MVStore
extends java.lang.Object

A persistent storage for maps.


Field Summary
static boolean ASSERT
          Whether assertions are enabled.
 
Method Summary
 void close()
          Close the file.
 boolean compact(int fillRate)
          Try to reduce the file size.
 long getCurrentVersion()
          Get the current version of the data.
 java.nio.channels.FileChannel getFile()
          Get the file instance in use, if a file is used.
 java.util.Map<java.lang.String,java.lang.String> getFileHeader()
          Get the file header.
 java.lang.String getFileName()
          Get the file name, or null for in-memory stores.
 int getFileReadCount()
          Get the number of file read operations since this store was opened.
 int getFileWriteCount()
          Get the number of file write operations since this store was opened.
 MVMap<java.lang.String,java.lang.String> getMetaMap()
          Get the metadata map.
 int getPageSize()
          Get the page size, in bytes.
 int getRetainChunk()
           
 long getRetainVersion()
           
 boolean getReuseSpace()
           
 int getStoreVersion()
          Get the store version.
 int getUnsavedPageCount()
          Get the estimated number of unsaved pages.
 boolean hasUnsavedChanges()
          Check whether there are any unsaved changes.
 long incrementVersion()
          Increment the current version.
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 previous key and value type (if the map already exists), or Object if not.
<K,V> MVMap<K,V>
openMap(java.lang.String name, java.lang.Class<K> keyClass, java.lang.Class<V> valueClass)
          Open a map.
<T extends MVMap<K,V>,K,V>
T
openMap(java.lang.String name, T template)
          Open a map using the given template.
 void rollbackTo(long version)
          Revert to the beginning of the given version.
 void setPageSize(int pageSize)
          Set the amount of memory a page should contain at most, in bytes.
 void setRetainChunk(int retainChunk)
          Which chunk to retain.
 void setRetainVersion(long retainVersion)
          Which version to retain.
 void setReuseSpace(boolean reuseSpace)
          Whether empty space in the file should be re-used.
 void setStoreVersion(int version)
          Update the store version.
 long store()
          Commit all changes and persist them to disk.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ASSERT

public static final boolean ASSERT
Whether assertions are enabled.

See Also:
Constant Field Values
Method Detail

open

public static MVStore open(java.lang.String fileName)
Open a store in exclusive mode.

Parameters:
fileName - the file name (null for in-memory)
Returns:
the store

openMap

public <K,V> MVMap<K,V> openMap(java.lang.String name)
Open a map with the previous key and value type (if the map already exists), or Object if not.

Type Parameters:
K - the key type
V - the value type
Parameters:
name - the name of the map
Returns:
the map

openMap

public <K,V> MVMap<K,V> openMap(java.lang.String name,
                                java.lang.Class<K> keyClass,
                                java.lang.Class<V> valueClass)
Open a map.

Type Parameters:
K - the key type
V - the value type
Parameters:
name - the name of the map
keyClass - the key class
valueClass - the value class
Returns:
the map

openMap

public <T extends MVMap<K,V>,K,V> T openMap(java.lang.String name,
                                            T template)
Open a map using the given template. The returned map is of the same type as the template, and contains the same key and value types. If a map with this name is already open, this map is returned. If it is not open, the template object is opened with the applicable configuration.

Type Parameters:
T - the map type
Parameters:
name - the name of the map
template - the template map
Returns:
the opened map

getMetaMap

public MVMap<java.lang.String,java.lang.String> getMetaMap()
Get the metadata map. 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).

It contains the following entries:

 map.{name} = {map metadata}
 root.{mapId} = {root position}
 chunk.{chunkId} = {chunk metadata}
 

Returns:
the metadata map

close

public void close()
Close the file. Uncommitted changes are ignored, and all open maps are closed.


incrementVersion

public long incrementVersion()
Increment the current version.

Returns:
the new version

store

public long store()
Commit all changes and persist them to disk. This method does nothing if there are no unsaved changes, otherwise it stores the data and increments the current version.

Returns:
the new version (incremented if there were changes)

hasUnsavedChanges

public boolean hasUnsavedChanges()
Check whether there are any unsaved changes.

Returns:
if there are any changes

compact

public boolean compact(int fillRate)
Try to reduce the file size. Chunks with a low number of live items will be re-written. If the current fill rate is higher than the target fill rate, no optimization is done.

Parameters:
fillRate - the minimum percentage of live entries
Returns:
if anything was written

setPageSize

public void setPageSize(int pageSize)
Set the amount of memory a page should contain at most, in bytes. Larger pages are split. The default is 6 KB. This is not a limit in the page size (pages with one entry can get larger), it is just the point where pages are split.

Parameters:
pageSize - the page size

getPageSize

public int getPageSize()
Get the page size, in bytes.

Returns:
the page size

getReuseSpace

public boolean getReuseSpace()

setReuseSpace

public 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

getRetainChunk

public int getRetainChunk()

setRetainChunk

public void setRetainChunk(int retainChunk)
Which chunk to retain. If not set, old chunks are re-used as soon as possible, which may make it impossible to roll back beyond a save operation, or read a older version before.

This setting is not persisted.

Parameters:
retainChunk - the earliest chunk to retain (0 to retain all chunks, -1 to re-use space as early as possible)

setRetainVersion

public void setRetainVersion(long retainVersion)
Which version to retain. If not set, all versions up to the last stored version are retained.

Parameters:
retainVersion - the oldest version to retain

getRetainVersion

public long getRetainVersion()

getUnsavedPageCount

public int getUnsavedPageCount()
Get the estimated number of unsaved pages. The returned value is not accurate, specially after rollbacks, but can be used to estimate the memory usage for unsaved data.

Returns:
the number of unsaved pages

getStoreVersion

public 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

setStoreVersion

public void setStoreVersion(int version)
Update the store version.

Parameters:
version - the new store version

rollbackTo

public 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.

Parameters:
version - the version to revert to

getCurrentVersion

public long getCurrentVersion()
Get the current version of the data. When a new store is created, the version is 0.

Returns:
the version

getFileWriteCount

public int getFileWriteCount()
Get the number of file write operations since this store was opened.

Returns:
the number of write operations

getFileReadCount

public int getFileReadCount()
Get the number of file read operations since this store was opened.

Returns:
the number of read operations

getFileName

public java.lang.String getFileName()
Get the file name, or null for in-memory stores.

Returns:
the file name

getFileHeader

public java.util.Map<java.lang.String,java.lang.String> getFileHeader()
Get the file 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 file header

getFile

public java.nio.channels.FileChannel getFile()
Get the file instance in use, if a file is used. The application may read from the file (for example for online backup), but not write to it or truncate it.

Returns:
the file, or null

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object