Class MVMap<K,V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- org.dizitart.no2.mvstore.compat.v1.mvstore.MVMap<K,V>
-
- Type Parameters:
K
- the key classV
- the value class
- All Implemented Interfaces:
ConcurrentMap<K,V>
,Map<K,V>
public class MVMap<K,V> extends AbstractMap<K,V> implements ConcurrentMap<K,V>
A stored map.All read and write operations can happen concurrently with all other operations, without risk of corruption.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MVMap.BasicBuilder<M extends MVMap<K,V>,K,V>
A builder for this class.static class
MVMap.Builder<K,V>
A builder for this class.static class
MVMap.Decision
static class
MVMap.DecisionMaker<V>
Class DecisionMaker provides callback interface (and should become a such in Java 8) for MVMap.operate method.static interface
MVMap.MapBuilder<M extends MVMap<K,V>,K,V>
A builder for maps.-
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
append(K key, V value)
Appends entry to this map.K
ceilingKey(K key)
Get the smallest key that is larger or equal to this key.K
ceilingKey(Page p, K key)
Get the smallest key that is larger or equal to this key, for the given root page.void
clear()
Remove all entries.boolean
containsKey(Object key)
Cursor<K,V>
cursor(K from)
Get a cursor to iterate over a number of keys and values.Set<Map.Entry<K,V>>
entrySet()
boolean
equals(Object o)
K
firstKey()
Get the first key, or null if the map is empty.K
firstKey(Page p)
Get the first key of this page.K
floorKey(K key)
Get the largest key that is smaller or equal to this key.K
floorKey(Page p, K key)
Get the largest key that is smaller or equal to this key, for the given root page.RootReference
flushAndGetRoot()
Get the root reference, flushing any current append buffer.V
get(Object key)
Get the value for the given key, or null if not found.V
get(Page p, Object key)
Get the value for the given key from a snapshot, or null if not found.long
getCreateVersion()
int
getId()
Get the map id.K
getKey(long index)
Get the key at the given index.long
getKeyIndex(K key)
Get the index of the given key in the map.DataType
getKeyType()
Get the key type.String
getName()
Get the map name.RootReference
getRoot()
Page
getRootPage()
The current root page (may not be null).MVStore
getStore()
String
getType()
Get the map type.DataType
getValueType()
Get the value type.long
getVersion()
Get version of the map, which is the version of the store, at the moment when map was modified last time.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(Page p, K key)
Get the smallest key that is larger than the given key, for the given root page, or null if no such key exists.boolean
isClosed()
boolean
isEmpty()
boolean
isReadOnly()
boolean
isVolatile()
Whether this is volatile map, meaning that changes are not persisted.Iterator<K>
keyIterator(K from)
Iterate over a number of keys.List<K>
keyList()
Get the key list.Set<K>
keySet()
K
lastKey()
Get the last key, or null if the map is empty.K
lastKey(Page p)
Get the last key of this page.K
lowerKey(K key)
Get the largest key that is smaller than the given key, or null if no such key exists.K
lowerKey(Page p, K key)
Get the largest key that is smaller than the given key, for the given root page, or null if no such key exists.MVMap<K,V>
openVersion(long version)
Open an old version for the given map.V
operate(K key, V value, MVMap.DecisionMaker<? super V> decisionMaker)
Add, replace or remove a key-value pair.V
put(K key, V value)
Add or replace a key-value pair.V
putIfAbsent(K key, V value)
Add a key-value pair if it does not yet exist.V
remove(Object key)
Remove a key-value pair, if the key exists.boolean
remove(Object key, Object value)
Remove a key-value pair if the value matches the stored one.V
replace(K key, V value)
Replace a value for an existing key.boolean
replace(K key, V oldValue, V newValue)
Replace a value for an existing key, if the value matches.void
setVolatile(boolean isVolatile)
Set the volatile flag of the map.int
size()
Get the number of entries, as a integer.long
sizeAsLong()
Get the number of entries, as a long.String
toString()
void
trimLast()
Removes last entry from this map.-
Methods inherited from class java.util.AbstractMap
containsValue, putAll, values
-
Methods inherited from interface java.util.concurrent.ConcurrentMap
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
-
Methods inherited from interface java.util.Map
containsValue, putAll, values
-
-
-
-
Field Detail
-
store
public final MVStore store
The store.
-
-
Method Detail
-
firstKey
public final K firstKey()
Get the first key, or null if the map is empty.- Returns:
- the first key, or null
-
firstKey
public final K firstKey(Page p)
Get the first key of this page.- Parameters:
p
- the page- Returns:
- the key, or null
-
lastKey
public final K lastKey()
Get the last key, or null if the map is empty.- Returns:
- the last key, or null
-
lastKey
public final K lastKey(Page p)
Get the last key of this page.- Parameters:
p
- the page- Returns:
- the key, or null
-
getKey
public final 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
-
keyList
public final List<K> 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
-
getKeyIndex
public final 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
-
higherKey
public final 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
-
higherKey
public final K higherKey(Page p, K key)
Get the smallest key that is larger than the given key, for the given root page, or null if no such key exists.- Parameters:
p
- the root pagekey
- the key- Returns:
- the result
-
ceilingKey
public final K ceilingKey(K key)
Get the smallest key that is larger or equal to this key.- Parameters:
key
- the key- Returns:
- the result
-
ceilingKey
public final K ceilingKey(Page p, K key)
Get the smallest key that is larger or equal to this key, for the given root page.- Parameters:
p
- the root pagekey
- the key- Returns:
- the result
-
floorKey
public final K floorKey(K key)
Get the largest key that is smaller or equal to this key.- Parameters:
key
- the key- Returns:
- the result
-
floorKey
public final K floorKey(Page p, K key)
Get the largest key that is smaller or equal to this key, for the given root page.- Parameters:
p
- the root pagekey
- the key- Returns:
- the result
-
lowerKey
public final 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
-
lowerKey
public final K lowerKey(Page p, K key)
Get the largest key that is smaller than the given key, for the given root page, or null if no such key exists.- Parameters:
p
- the root pagekey
- the key- Returns:
- the result
-
get
public final V get(Object key)
Get the value for the given key, or null if not found.- Specified by:
get
in interfaceMap<K,V>
- Overrides:
get
in classAbstractMap<K,V>
- Parameters:
key
- the key- Returns:
- the value, or null if not found
- Throws:
ClassCastException
- if type of the specified key is not compatible with this map
-
get
public V get(Page p, Object key)
Get the value for the given key from a snapshot, or null if not found.- Parameters:
p
- the root of a snapshotkey
- the key- Returns:
- the value, or null if not found
- Throws:
ClassCastException
- if type of the specified key is not compatible with this map
-
containsKey
public final boolean containsKey(Object key)
- Specified by:
containsKey
in interfaceMap<K,V>
- Overrides:
containsKey
in classAbstractMap<K,V>
-
clear
public void clear()
Remove all entries.
-
isClosed
public final boolean isClosed()
-
remove
public V remove(Object key)
Remove a key-value pair, if the key exists.- Specified by:
remove
in interfaceMap<K,V>
- Overrides:
remove
in classAbstractMap<K,V>
- Parameters:
key
- the key (may not be null)- Returns:
- the old value if the key existed, or null otherwise
- Throws:
ClassCastException
- if type of the specified key is not compatible with this map
-
putIfAbsent
public final V putIfAbsent(K key, V value)
Add a key-value pair if it does not yet exist.- Specified by:
putIfAbsent
in interfaceConcurrentMap<K,V>
- Specified by:
putIfAbsent
in interfaceMap<K,V>
- Parameters:
key
- the key (may not be null)value
- the new value- Returns:
- the old value if the key existed, or null otherwise
-
remove
public boolean remove(Object key, Object value)
Remove a key-value pair if the value matches the stored one.
-
replace
public final boolean replace(K key, V oldValue, V newValue)
Replace a value for an existing key, if the value matches.
-
getKeyType
public final DataType getKeyType()
Get the key type.- Returns:
- the key type
-
getValueType
public final DataType getValueType()
Get the value type.- Returns:
- the value type
-
keyIterator
public final Iterator<K> keyIterator(K from)
Iterate over a number of keys.- Parameters:
from
- the first key to return- Returns:
- the iterator
-
cursor
public final Cursor<K,V> 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
-
getName
public final String getName()
Get the map name.- Returns:
- the name
-
getStore
public final MVStore getStore()
-
getId
public final int getId()
Get the map id. Please note the map id may be different after compacting a store.- Returns:
- the map id
-
getRootPage
public final Page getRootPage()
The current root page (may not be null).- Returns:
- the root page
-
getRoot
public RootReference getRoot()
-
flushAndGetRoot
public RootReference flushAndGetRoot()
Get the root reference, flushing any current append buffer.- Returns:
- current root reference
-
isReadOnly
public final boolean isReadOnly()
-
setVolatile
public final void setVolatile(boolean isVolatile)
Set the volatile flag of the map.- Parameters:
isVolatile
- the volatile flag
-
isVolatile
public final 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
-
hashCode
public final int hashCode()
-
equals
public final boolean equals(Object o)
-
size
public final int size()
Get the number of entries, as a integer.Integer.MAX_VALUE
is returned if there are more than this entries.- Specified by:
size
in interfaceMap<K,V>
- Overrides:
size
in classAbstractMap<K,V>
- Returns:
- the number of entries, as an integer
- See Also:
sizeAsLong()
-
sizeAsLong
public final long sizeAsLong()
Get the number of entries, as a long.- Returns:
- the number of entries
-
isEmpty
public boolean isEmpty()
-
getCreateVersion
public final long getCreateVersion()
-
openVersion
public final MVMap<K,V> openVersion(long version)
Open an old version for the given map. It will restore map at last known state of the version specified. (at the point right before the commit() call, which advanced map to the next version) Map is opened in read-only mode.- Parameters:
version
- the version- Returns:
- the map
-
getVersion
public final long getVersion()
Get version of the map, which is the version of the store, at the moment when map was modified last time.- Returns:
- version
-
getType
public String getType()
Get the map type. When opening an existing map, the map type must match.- Returns:
- the map type
-
append
public void append(K key, V value)
Appends entry to this map. this method is NOT thread safe and can not be used neither concurrently, nor in combination with any method that updates this map. Non-updating method may be used concurrently, but latest appended values are not guaranteed to be visible.- Parameters:
key
- should be higher in map's order than any existing keyvalue
- to be appended
-
trimLast
public void trimLast()
Removes last entry from this map. this method is NOT thread safe and can not be used neither concurrently, nor in combination with any method that updates this map. Non-updating method may be used concurrently, but latest removal may not be visible.
-
toString
public final String toString()
- Overrides:
toString
in classAbstractMap<K,V>
-
operate
public V operate(K key, V value, MVMap.DecisionMaker<? super V> decisionMaker)
Add, replace or remove a key-value pair.- Parameters:
key
- the key (may not be null)value
- new value, it may be null when removal is intendeddecisionMaker
- command object to make choices during transaction.- Returns:
- previous value, if mapping for that key existed, or null otherwise
-
-