MVMap

A stored map.

Read operations can happen concurrently with all other operations, without risk of corruption.

Write operations first read the relevant area from disk to memory concurrently, and only then modify the data. The in-memory part of write operations is synchronized. For scalable concurrent in-memory write operations, the map should be split into multiple smaller sub-maps that are then synchronized independently.

Methods
static String getMapKey(int mapId)
Get the metadata key for the given map id.
static String getMapKey(int mapId)
Get the metadata key for the given map id.
Parameters:
mapId - the map id
Returns:
the metadata key
static String getMapRootKey(int mapId)
Get the metadata key for the root of the given map id.
static String getMapRootKey(int mapId)
Get the metadata key for the root of the given map id.
Parameters:
mapId - the map id
Returns:
the metadata key
boolean areValuesEqual(Object a, Object b)
Check whether the two values are equal.
boolean areValuesEqual(Object a, Object b)
Check whether the two values are equal.
Parameters:
a - the first value
b - the second value
Returns:
true if they are equal
String asString(String name)
Get the map metadata as a string.
String asString(String name)
Get the map metadata as a string.
Parameters:
name - the map name (or null)
Returns:
the string
void beforeWrite()
This method is called before writing to the map.
void beforeWrite()
This method is called before writing to the map. The default implementation checks whether writing is allowed, and tries to detect concurrent modification.
Throws:
UnsupportedOperationException - if the map is read-only, or if another thread is concurrently writing
Object binarySearch(Page p, Object key)
Get the value for the given key, or null if not found.
Object binarySearch(Page p, Object key)
Get the value for the given key, or null if not found.
Parameters:
p - the page
key - the key
Returns:
the value or null
K ceilingKey(K key)
Get the smallest key that is larger or equal to this key.
K ceilingKey(K key)
Get the smallest key that is larger or equal to this key.
Parameters:
key - the key
Returns:
the result
void clear()
Remove all entries.
void clear()
Remove all entries.
void close()
Close the map.
void close()
Close the map. Accessing the data is still possible (to allow concurrent reads), but it is marked as closed.
int compare(Object a, Object b)
Compare two keys.
int compare(Object a, Object b)
Compare two keys.
Parameters:
a - the first key
b - the second key
Returns:
-1 if the first key is smaller, 1 if bigger, 0 if equal
boolean containsKey(Object key)
boolean containsKey(Object key)
void copyFrom(MVMap sourceMap)
Copy a map.
void copyFrom(MVMap sourceMap)
Copy a map. All pages are copied.
Parameters:
sourceMap - the source map
Cursor cursor(K from)
Get a cursor to iterate over a number of keys and values.
Cursor cursor(K from)
Get a cursor to iterate over a number of keys and values.
Parameters:
from - the first key to return
Returns:
the cursor
Set entrySet()
Set entrySet()
boolean equals(Object o)
boolean equals(Object o)
K firstKey()
Get the first key, or null if the map is empty.
K firstKey()
Get the first key, or null if the map is empty.
Returns:
the first key, or null
K floorKey(K key)
Get the largest key that is smaller or equal to this key.
K floorKey(K key)
Get the largest key that is smaller or equal to this key.
Parameters:
key - the key
Returns:
the result
V get(Object key)
Get a value.
V get(Object key)
Get a value.
Parameters:
key - the key
Returns:
the value, or null if not found
int getChildPageCount(Page p)
Get the child page count for this page.
int getChildPageCount(Page p)
Get the child page count for this page. This is to allow another map implementation to override the default, in case the last child is not to be used.
Parameters:
p - the page
Returns:
the number of direct children
long getCreateVersion()
long getCreateVersion()
K getFirstLast(boolean first)
Get the first (lowest) or last (largest) key.
K getFirstLast(boolean first)
Get the first (lowest) or last (largest) key.
Parameters:
first - whether to retrieve the first key
Returns:
the key, or null if the map is empty
int getId()
Get the map id.
int getId()
Get the map id. Please note the map id may be different after compacting a store.
Returns:
the map id
K getKey(long index)
Get the key at the given index.
K getKey(long index)
Get the key at the given index.

This is a O(log(size)) operation.

Parameters:
index - the index
Returns:
the key
long getKeyIndex(K key)
Get the index of the given key in the map.
long getKeyIndex(K key)
Get the index of the given key in the map.

This is a O(log(size)) operation.

If the key was found, the returned value is the index in the key array. If not found, the returned value is negative, where -1 means the provided key is smaller than any keys. See also Arrays.binarySearch.

Parameters:
key - the key
Returns:
the index
DataType getKeyType()
Get the key type.
DataType getKeyType()
Get the key type.
Returns:
the key type
K getMinMax(K key, boolean min, boolean excluding)
Get the smallest or largest key using the given bounds.
K getMinMax(K key, boolean min, boolean excluding)
Get the smallest or largest key using the given bounds.
Parameters:
key - the key
min - whether to retrieve the smallest key
excluding - if the given upper/lower bound is exclusive
Returns:
the key, or null if no such key exists
String getName()
Get the map name.
String getName()
Get the map name.
Returns:
the name
Page getRoot()
Get the root page.
Page getRoot()
Get the root page.
Returns:
the root page
MVStore getStore()
MVStore getStore()
String getType()
Get the map type.
String getType()
Get the map type. When opening an existing map, the map type must match.
Returns:
the map type
DataType getValueType()
Get the value type.
DataType getValueType()
Get the value type.
Returns:
the value type
long getVersion()
long getVersion()
int hashCode()
int hashCode()
K higherKey(K key)
Get the smallest key that is larger than the given key, or null if no such key exists.
K higherKey(K key)
Get the smallest key that is larger than the given key, or null if no such key exists.
Parameters:
key - the key
Returns:
the result
void init(MVStore store, int id, long createVersion)
Open this map with the given store and configuration.
void init(MVStore store, int id, long createVersion)
Open this map with the given store and configuration.
Parameters:
store - the store
id - map id
createVersion - version in which this map was created
boolean isClosed()
boolean isClosed()
boolean isEmpty()
boolean isEmpty()
boolean isReadOnly()
boolean isReadOnly()
boolean isVolatile()
Whether this is volatile map, meaning that changes are not persisted.
boolean isVolatile()
Whether this is volatile map, meaning that changes are not persisted. By default (even if the store is not persisted), maps are not volatile.
Returns:
whether this map is volatile
Iterator keyIterator(K from)
Iterate over a number of keys.
Iterator keyIterator(K from)
Iterate over a number of keys.
Parameters:
from - the first key to return
Returns:
the iterator
List keyList()
Get the key list.
List keyList()
Get the key list. The list is a read-only representation of all keys.

The get and indexOf methods are O(log(size)) operations. The result of indexOf is cast to an int.

Returns:
the key list
Set keySet()
Set keySet()
K lastKey()
Get the last key, or null if the map is empty.
K lastKey()
Get the last key, or null if the map is empty.
Returns:
the last key, or null
K lowerKey(K key)
Get the largest key that is smaller than the given key, or null if no such key exists.
K lowerKey(K key)
Get the largest key that is smaller than the given key, or null if no such key exists.
Parameters:
key - the key
Returns:
the result
void newRoot(Page newRoot)
Use the new root page from now on.
void newRoot(Page newRoot)
Use the new root page from now on.
Parameters:
newRoot - the new root page
MVMap openReadOnly()
Open a copy of the map in read-only mode.
MVMap openReadOnly()
Open a copy of the map in read-only mode.
Returns:
the opened map
MVMap openVersion(long version)
Open an old version for the given map.
MVMap openVersion(long version)
Open an old version for the given map.
Parameters:
version - the version
Returns:
the map
V put(K key, V value)
Add or replace a key-value pair.
V put(K key, V value)
Add or replace a key-value pair.
Parameters:
key - the key (may not be null)
value - the value (may not be null)
Returns:
the old value if the key existed, or null otherwise
Object put(Page p, long writeVersion, Object key, Object value)
Add or update a key-value pair.
Object put(Page p, long writeVersion, Object key, Object value)
Add or update a key-value pair.
Parameters:
p - the page
writeVersion - the write version
key - the key (may not be null)
value - the value (may not be null)
Returns:
the old value, or null
V putIfAbsent(K key, V value)
Add a key-value pair if it does not yet exist.
V putIfAbsent(K key, V value)
Add a key-value pair if it does not yet exist.
Parameters:
key - the key (may not be null)
value - the new value
Returns:
the old value if the key existed, or null otherwise
Page readPage(long pos)
Read a page.
Page readPage(long pos)
Read a page.
Parameters:
pos - the position of the page
Returns:
the page
V remove(Object key)
Remove a key-value pair, if the key exists.
V remove(Object key)
Remove a key-value pair, if the key exists.
Parameters:
key - the key (may not be null)
Returns:
the old value if the key existed, or null otherwise
boolean remove(Object key, Object value)
Remove a key-value pair if the value matches the stored one.
boolean remove(Object key, Object value)
Remove a key-value pair if the value matches the stored one.
Parameters:
key - the key (may not be null)
value - the expected value
Returns:
true if the item was removed
Object remove(Page p, long writeVersion, Object key)
Remove a key-value pair.
Object remove(Page p, long writeVersion, Object key)
Remove a key-value pair.
Parameters:
p - the page (may not be null)
writeVersion - the write version
key - the key
Returns:
the old value, or null if the key did not exist
void removePage(long pos, int memory)
Remove the given page (make the space available).
void removePage(long pos, int memory)
Remove the given page (make the space available).
Parameters:
pos - the position of the page to remove
memory - the number of bytes used for this page
void removeUnusedOldVersions()
Forget those old versions that are no longer needed.
void removeUnusedOldVersions()
Forget those old versions that are no longer needed.
boolean replace(K key, V oldValue, V newValue)
Replace a value for an existing key, if the value matches.
boolean replace(K key, V oldValue, V newValue)
Replace a value for an existing key, if the value matches.
Parameters:
key - the key (may not be null)
oldValue - the expected value
newValue - the new value
Returns:
true if the value was replaced
V replace(K key, V value)
Replace a value for an existing key.
V replace(K key, V value)
Replace a value for an existing key.
Parameters:
key - the key (may not be null)
value - the new value
Returns:
the old value, if the value was replaced, or null
boolean rewrite(Set set)
Re-write any pages that belong to one of the chunks in the given set.
boolean rewrite(Set set)
Re-write any pages that belong to one of the chunks in the given set.
Parameters:
set - the set of chunk ids
Returns:
whether rewriting was successful
void rollbackTo(long version)
Rollback to the given version.
void rollbackTo(long version)
Rollback to the given version.
Parameters:
version - the version
void setRootPos(long rootPos, long version)
Set the position of the root page.
void setRootPos(long rootPos, long version)
Set the position of the root page.
Parameters:
rootPos - the position, 0 for empty
version - the version of the root
void setVolatile(boolean isVolatile)
Set the volatile flag of the map.
void setVolatile(boolean isVolatile)
Set the volatile flag of the map.
Parameters:
isVolatile - the volatile flag
void setWriteVersion(long writeVersion)
void setWriteVersion(long writeVersion)
int size()
Get the number of entries, as a integer.
int size()
Get the number of entries, as a integer. Integer.MAX_VALUE is returned if there are more than this entries.
Returns:
the number of entries, as an integer
long sizeAsLong()
Get the number of entries, as a long.
long sizeAsLong()
Get the number of entries, as a long.
Returns:
the number of entries
Page splitRootIfNeeded(Page p, long writeVersion)
Split the root page if necessary.
Page splitRootIfNeeded(Page p, long writeVersion)
Split the root page if necessary.
Parameters:
p - the page
writeVersion - the write version
Returns:
the new sibling
String toString()
String toString()