Top Description Inners Fields Constructors Methods
org.h2.mvstore

public Class MVMap<K, V>

extends AbstractMap<K, V>
implements ConcurrentMap<K, V>
Class Inheritance
All Implemented Interfaces
java.util.concurrent.ConcurrentMap, java.util.Map
Known Direct Subclasses
org.h2.mvstore.rtree.MVRTreeMap, org.h2.mvstore.tx.TransactionStore.TxMapBuilder.TMVMap
Type Parameters
<K>
the key class
<V>
the value class
Static Imports
org.h2.engine.Constants.MEMORY_POINTER

A stored map.

All read and write operations can happen concurrently with all other operations, without risk of corruption.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class
MVMap.BasicBuilder<M extends MVMap<K, V>,
the key type
K
,
the value type
V
>

A builder for this class.

public static class
MVMap.Builder<
the key type
K
,
the value type
V
>

A builder for this class.

public static enum
MVMap.Decision

The decision on what to do on an update.

public abstract static class
MVMap.DecisionMaker<
value type of the map
V
>

Class DecisionMaker provides callback interface (and should become a such in Java 8) for MVMap.operate method.

private static class
private static class
public static interface
MVMap.MapBuilder<
the map type
M extends MVMap<K, V>
,
the key type
K
,
the value type
V
>

A builder for maps.

private static class

Field Summary

Modifier and TypeField and Description
private final AtomicLong
private final AtomicLong
private volatile boolean
closed

Whether the map is closed.

private final long
private final int
private boolean
private final K[]
private final int
private final DataType<K>
private final Object
private volatile boolean
private boolean
private final AtomicReference<RootReference<K, V>>
root

Reference to the current root page.

private final boolean
public final MVStore
store

The store.

private final V[]
private final DataType<V>

Constructor Summary

AccessConstructor and Description
protected
MVMap(Map<String, Object> config, DataType<K> keyType, DataType<V> valueType)

protected
MVMap(MVMap<K, V> source)

pack-priv
MVMap(MVStore store, int id, DataType<K> keyType, DataType<V> valueType)

private
MVMap(MVStore store, DataType<K> keyType, DataType<V> valueType, int id, long createVersion, AtomicReference<RootReference<K, V>> root, int keysPerPage, boolean singleWriter)

Method Summary

Modifier and TypeMethod and Description
public void
append(K
should be higher in map's order than any existing key
key
,
V
to be appended
value
)

Appends entry to this map.

pack-priv static <
type of values to compare
X
>
boolean

Returns:

true if they are equal
areValuesEqual
(DataType<X>
to use for comparison
datatype
,
X
the first value
a
,
X
the second value
b
)

Check whether the two values are equal.

protected String

Returns:

the string
asString
(String
the map name (or null)
name
)

Get the map metadata as a string.

protected final void
beforeWrite()

This method is called before writing to the map.

private static <T> int
calculateMemory(DataType<T> keyType, T[] storage, int count)

public final K

Returns:

the result
ceilingKey
(K
the key
key
)

Get the smallest key that is larger or equal to this key.

public void
clear()

Overrides java.util.AbstractMap.clear.

Implements java.util.Map.clear.

Remove all entries.

pack-priv RootReference<K, V>

Returns:

the new root reference
clearIt
()

Remove all entries and return the root reference.

protected MVMap<K, V>

Returns:

clone of this.
cloneIt
()

Clone the current map.

pack-priv final void
close()

Close the map.

pack-priv final int

Returns:

-1 if the first key is smaller, 1 if bigger, 0 if equal
compare
(K
the first key
a
,
K
the second key
b
)

Compare two keys.

pack-priv final boolean

Returns:

whether updating worked
compareAndSetRoot
(RootReference<K, V>
the old (expected)
expectedRootReference
,
RootReference<K, V>
the new
updatedRootReference
)

Compare and set the root reference.

public final boolean
containsKey(Object
key whose presence in this map is to be tested
key
)

Overrides java.util.AbstractMap.containsKey.

Implements java.util.Map.containsKey.

Returns true if this map contains a mapping for the specified key.

private void
copy(Page<K, V> source, Page<K, V> parent, int index)

pack-priv final void
copyFrom(MVMap<K, V>
the source map
sourceMap
)

Copy a map.

protected Page<K, V>

Returns:

new page
createEmptyLeaf
()

Create empty leaf node page.

protected Page<K, V>

Returns:

new page
createEmptyNode
()

Create empty internal node page.

public final Cursor<K, V>

Returns:

the cursor
cursor
(K
the first key to return
from
)

Get a cursor to iterate over a number of keys and values in the latest version of this map.

public final Cursor<K, V>

Returns:

the cursor
cursor
(K
the first key to return
from
,
K
the last key to return
to
,
boolean
if true, iterate in reverse (descending) order
reverse
)

Get a cursor to iterate over a number of keys and values in the latest version of this map.

public Cursor<K, V>

Returns:

the cursor
cursor
(RootReference<K, V>
of this map's version to iterate over
rootReference
,
K
the first key to return
from
,
K
the last key to return
to
,
boolean
if true, iterate in reverse (descending) order
reverse
)

Get a cursor to iterate over a number of keys and values.

public final Set<Map.Entry<K, V>>
entrySet()

Implements abstract java.util.AbstractMap.entrySet.

Implements java.util.Map.entrySet.

Returns a Set view of the mappings contained in this map.

public final boolean
equals(Object
object to be compared for equality with this map
o
)

Overrides java.util.AbstractMap.equals.

Implements java.util.Map.equals.

Compares the specified object with this map for equality.

pack-priv final int
pack-priv final int
evaluateMemoryForKeys(K[] storage, int count)

pack-priv final int
pack-priv final int
evaluateMemoryForValues(V[] storage, int count)

public final K

Returns:

the first key, or null
firstKey
()

Get the first key, or null if the map is empty.

public final K

Returns:

the result
floorKey
(K
the key
key
)

Get the largest key that is smaller or equal to this key.

public RootReference<K, V>

Returns:

current root reference
flushAndGetRoot
()

Get the root reference, flushing any current append buffer.

private RootReference<K, V>

Returns:

potentially updated RootReference
flushAppendBuffer
(RootReference<K, V>
current RootReference
rootReference
,
boolean
whether buffer should be completely flushed, otherwise just a single empty slot is required
fullFlush
)

If map was used in append mode, this method will ensure that append buffer is flushed - emptied with all entries inserted into map as a new leaf.

public final V

Returns:

the value, or null if not found
get
(Object
the key
key
)

Overrides java.util.AbstractMap.get.

Implements java.util.Map.get.

Get the value for the given key, or null if not found.

public V

Returns:

the value, or null if not found
get
(Page<K, V>
the root of a snapshot
p
,
K
the key
key
)

Get the value for the given key from a snapshot, or null if not found.

protected int

Returns:

the number of direct children
getChildPageCount
(Page<K, V>
the page
p
)

Get the child page count for this page.

pack-priv final long
private K

Returns:

the key, or null if the map is empty
getFirstLast
(boolean
whether to retrieve the first key
first
)

Get the first (lowest) or last (largest) key.

private K
getFirstLast(Page<K, V> p, boolean first)

public final int

Returns:

the map id
getId
()

Get the map id. Please note the map id may be different after compacting a store.

public final K

Returns:

the key
getKey
(long
the index
index
)

Get the key at the given index.

public final long

Returns:

the index
getKeyIndex
(K
the key
key
)

Get the index of the given key in the map.

public final DataType<K>

Returns:

the key type
getKeyType
()

Get the key type.

pack-priv static String

Returns:

the metadata key
getMapKey
(int
the map id
mapId
)

Get the metadata key for the given map id.

pack-priv static String

Returns:

the metadata key
getMapRootKey
(int
the map id
mapId
)

Get the metadata key for the root of the given map id.

private K

Returns:

the key, or null if no such key exists
getMinMax
(K
the key
key
,
boolean
whether to retrieve the smallest key
min
,
boolean
if the given upper/lower bound is exclusive
excluding
)

Get the smallest or largest key using the given bounds.

private K
getMinMax(RootReference<K, V> rootRef, K key, boolean min, boolean excluding)

private K
getMinMax(Page<K, V> p, K key, boolean min, boolean excluding)

public final String

Returns:

the name
getName
()

Get the map name.

public RootReference<K, V>
public final Page<K, V>

Returns:

the root page
getRootPage
()

The current root page (may not be null).

public final MVStore
public String

Returns:

the map type
getType
()

Get the map type.

public final DataType<V>

Returns:

the value type
getValueType
()

Get the value type.

public final long

Returns:

version
getVersion
()

Get version of the map, which is the version of the store, at the moment when map was modified last time.

pack-priv final boolean

Returns:

true if has changes
hasChangesSince
(long
root version
version
)

Does the root have changes since the specified version?

public final int
hashCode()

Overrides java.util.AbstractMap.hashCode.

Implements java.util.Map.hashCode.

Returns the hash code value for this map.

public final K

Returns:

the result
higherKey
(K
the key
key
)

Get the smallest key that is larger than the given key (next key in ascending order), or null if no such key exists.

public final K

Returns:

the result
higherKey
(RootReference<K, V>
the root reference of the map
rootRef
,
K
to start from
key
)

Get the smallest key that is larger than the given key, for the given root page, or null if no such key exists.

public final boolean
public boolean
isEmpty()

Overrides java.util.AbstractMap.isEmpty.

Implements java.util.Map.isEmpty.

Returns true if this map contains no key-value mappings.

pack-priv final boolean
protected final boolean
public final boolean
pack-priv boolean
public final boolean

Returns:

whether this map is volatile
isVolatile
()

Whether this is volatile map, meaning that changes are not persisted.

public final Iterator<K>

Returns:

the iterator
keyIterator
(K
the first key to return
from
)

Iterate over a number of keys.

public final Iterator<K>

Returns:

the iterator
keyIteratorReverse
(K
the first key to return
from
)

Iterate over a number of keys in reverse order

public final List<K>

Returns:

the key list
keyList
()

Get the key list.

public Set<K>
keySet()

Overrides java.util.AbstractMap.keySet.

Implements java.util.Map.keySet.

Returns a Set view of the keys contained in this map.

public final K

Returns:

the last key, or null
lastKey
()

Get the last key, or null if the map is empty.

private RootReference<K, V>
lockRoot(RootReference<K, V> rootReference, int attempt)

public final K

Returns:

the result
lowerKey
(K
the key
key
)

Get the largest key that is smaller than the given key, or null if no such key exists.

public final K

Returns:

the result
lowerKey
(RootReference<K, V>
the root page
rootRef
,
K
the key
key
)

Get the largest key that is smaller than the given key, for the given root page, or null if no such key exists.

private void
pack-priv final MVMap<K, V>

Returns:

the opened map
openReadOnly
(long
position of the root page
rootPos
,
long
to open
version
)

Open a copy of the map in read-only mode.

private MVMap<K, V>
openReadOnly(Page<K, V> root, long version)

public final MVMap<K, V>

Returns:

the map
openVersion
(long
the version
version
)

Open an old version for the given map.

public V

Returns:

previous value, if mapping for that key existed, or null otherwise
operate
(K
the key (may not be null)
key
,
V
new value, it may be null when removal is intended
value
,
MVMap.DecisionMaker<? super V>
command object to make choices during transaction.
decisionMaker
)

Add, replace or remove a key-value pair.

public V

Returns:

the old value if the key existed, or null otherwise
put
(K
the key (may not be null)
key
,
V
the value (may not be null)
value
)

Overrides java.util.AbstractMap.put.

Implements java.util.Map.put.

Add or replace a key-value pair.

public final V

Returns:

the old value if the key existed, or null otherwise
putIfAbsent
(K
the key (may not be null)
key
,
V
the new value
value
)

Overrides default java.util.Map.putIfAbsent.

Implements java.util.concurrent.ConcurrentMap.putIfAbsent.

Add a key-value pair if it does not yet exist.

private Page<K, V>
readOrCreateRootPage(long rootPos)

pack-priv final Page<K, V>

Returns:

the page
readPage
(long
the position of the page
pos
)

Read a page.

protected final void
public V

Returns:

the old value if the key existed, or null otherwise
remove
(Object
the key (may not be null)
key
)

Overrides java.util.AbstractMap.remove.

Implements java.util.Map.remove.

Remove a key-value pair, if the key exists.

public boolean

Returns:

true if the item was removed
remove
(Object
the key (may not be null)
key
,
Object
the expected value
value
)

Overrides default java.util.Map.remove.

Implements java.util.concurrent.ConcurrentMap.remove.

Remove a key-value pair if the value matches the stored one.

private void
removeUnusedOldVersions(RootReference<K, V>
to inspect
rootReference
)

Forget those old versions that are no longer needed.

public final boolean

Returns:

true if the value was replaced
replace
(K
the key (may not be null)
key
,
V
the expected value
oldValue
,
V
the new value
newValue
)

Overrides default java.util.Map.replace.

Implements java.util.concurrent.ConcurrentMap.replace.

Replace a value for an existing key, if the value matches.

public final V

Returns:

the old value, if the value was replaced, or null
replace
(K
the key (may not be null)
key
,
V
the new value
value
)

Overrides default java.util.Map.replace.

Implements java.util.concurrent.ConcurrentMap.replace.

Replace a value for an existing key.

private static <K, V> Page<K, V>
replacePage(CursorPos<K, V> path, Page<K, V> replacement, MVMap.IntValueHolder unsavedMemoryHolder)

pack-priv final boolean
rewritePage(long pagePos)

pack-priv boolean

Returns:

true if rollback was a success, false if there was not enough in-memory history
rollbackRoot
(long
to rollback to
version
)

Roll the root back to the specified version.

pack-priv final void
rollbackTo(long
the version
version
)

Rollback to the given version.

pack-priv static int
pack-priv final void
setInitialRoot(Page<K, V>
root page
rootPage
,
long
initial version
version
)

Set the initial root.

pack-priv final void
setRootPos(long
the position, 0 for empty
rootPos
,
long
to set for this map
version
)

Set the position of the root page.

public final void
setVolatile(boolean
the volatile flag
isVolatile
)

Set the volatile flag of the map.

pack-priv final RootReference<K, V>
setWriteVersion(long writeVersion)

public final int

Returns:

the number of entries, as an integer
size
()

Overrides java.util.AbstractMap.size.

Implements java.util.Map.size.

Get the number of entries, as a integer.

public final long

Returns:

the number of entries
sizeAsLong
()

Get the number of entries, as a long.

public final String
toString()

Overrides java.util.AbstractMap.toString.

Returns a string representation of this map.

public void
trimLast()

Removes last entry from this map.

protected RootReference<K, V>

Returns:

the new root reference
tryLock
(RootReference<K, V>
the old root reference
rootReference
,
int
the number of attempts so far
attempt
)

Try to lock the root.

private RootReference<K, V>

Returns:

the new root reference (never null)
unlockRoot
()

Unlock the root page, the new root being null.

protected RootReference<K, V>

Returns:

the new root reference (never null)
unlockRoot
(Page<K, V>
the new root
newRootPage
)

Unlock the root page.

private void
unlockRoot(int appendCounter)

private RootReference<K, V>
unlockRoot(Page<K, V> newRootPage, int appendCounter)

protected static <
the key class
K
,
the value class
V
>
boolean

Returns:

new RootReference or null if update failed
updateRoot
(RootReference<K, V>
expected current root reference
expectedRootReference
,
Page<K, V>
the new root page
newRootPage
,
int
how many attempt (including current) were made to update root
attemptUpdateCounter
)

Use the new root page from now on.

Inherited from java.util.AbstractMap:
clonecontainsValueputAllvalues