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.


Nested Class Summary
static class MVStore.Builder
          A builder for an MVStore.
 
Field Summary
static boolean ASSERT
          Whether assertions are enabled.
 
Method Summary
 void close()
          Close the file and the store.
 long commit()
          Commit the changes.
 boolean compact(int fillRate)
          Try to reduce the file size.
 long getCommittedVersion()
          Get the last committed version.
 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.
 long getRetainVersion()
           
 int getRetentionTime()
           
 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, without committing the changes.
 boolean isReadOnly()
           
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>
M
openMap(java.lang.String name, MVMap.MapBuilder<M,K,V> builder)
          Open a map with the given builder.
 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 setRetainVersion(long retainVersion)
          Which version to retain in memory.
 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.
 long store()
          Commit all changes and persist them to disk.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, 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. For a file-based store, the parent directory must already exist.

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

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

openMap

public <M extends MVMap<K,V>,K,V> M openMap(java.lang.String name,
                                            MVMap.MapBuilder<M,K,V> 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.

Type Parameters:
K - the key type
V - the value type
Parameters:
name - the name of the map
builder - the map builder
Returns:
the 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:

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

Returns:
the metadata map

close

public void close()
Close the file and the store. If there are any committed but unsaved changes, they are written to disk first. If any temporary data was written but not committed, this is rolled back. All open maps are closed.


incrementVersion

public long incrementVersion()
Increment the current version, without committing the changes.

Returns:
the new version

commit

public long commit()
Commit the changes. This method marks the changes as committed and increments the version.

Unless the write delay is disabled, this method does not write to the file. Instead, data is written after the delay, manually by calling the store method, when the write buffer is full, or when closing the store.

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 increments the current version and stores the data (for file based stores).

One store operation may run at any time.

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

getRetentionTime

public int getRetentionTime()

setRetentionTime

public 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 is 45000 (45 seconds). 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.

This setting is not persisted.

Parameters:
ms - how many milliseconds to retain old chunks (0 to overwrite them as early as possible)

setRetainVersion

public void setRetainVersion(long retainVersion)
Which version to retain in memory. If not set, all versions back 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. Rollback to version 0 means all data is removed.

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

getCommittedVersion

public long getCommittedVersion()
Get the last committed version.

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

isReadOnly

public boolean isReadOnly()