K
- the key classV
- the value classpublic class MVMap<K,V> extends AbstractMap<K,V> implements ConcurrentMap<K,V>
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.
Modifier and Type | Class and Description |
---|---|
static class |
MVMap.Builder<K,V>
A builder for this class.
|
static interface |
MVMap.MapBuilder<M extends MVMap<K,V>,K,V>
A builder for maps.
|
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Modifier and Type | Field and Description |
---|---|
protected Page |
root
The current root page (may not be null).
|
protected MVStore |
store
The store.
|
protected long |
writeVersion
The version used for writing.
|
Modifier | Constructor and Description |
---|---|
protected |
MVMap(DataType keyType,
DataType valueType) |
Modifier and Type | Method and Description |
---|---|
boolean |
areValuesEqual(Object a,
Object b)
Check whether the two values are equal.
|
protected void |
beforeWrite()
This method is called before writing to the map.
|
protected Object |
binarySearch(Page p,
Object key)
Get the value for the given key, or null if not found.
|
protected Page |
binarySearchPage(Page p,
Object key)
Get the value for the given key, or null if not found.
|
K |
ceilingKey(K key)
Get the smallest key that is larger or equal to this key.
|
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 |
floorKey(K key)
Get the largest key that is smaller or equal to this key.
|
V |
get(Object key)
Get a value.
|
protected int |
getChildPageCount(Page p)
Get the child page count for this page.
|
long |
getCreateVersion() |
protected K |
getFirstLast(boolean first)
Get the first (lowest) or last (largest) key.
|
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.
|
protected K |
getMinMax(K key,
boolean min,
boolean excluding)
Get the smallest or largest key using the given bounds.
|
String |
getName()
Get the map name.
|
Page |
getRoot()
Get the root page.
|
MVStore |
getStore() |
String |
getType()
Get the map type.
|
DataType |
getValueType()
Get the value type.
|
long |
getVersion() |
int |
hashCode() |
K |
higherKey(K key)
Get the smallest key that is larger than the given key, or null if no
such key exists.
|
protected void |
init(MVStore store,
HashMap<String,Object> config)
Open this map with the given store and configuration.
|
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 |
lowerKey(K key)
Get the largest key that is smaller than the given key, or null if no
such key exists.
|
protected void |
newRoot(Page newRoot)
Use the new root page from now on.
|
MVMap<K,V> |
openVersion(long version)
Open an old version for the given map.
|
V |
put(K key,
V value)
Add or replace a key-value pair.
|
protected Object |
put(Page p,
long writeVersion,
Object key,
Object value)
Add or update 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.
|
protected Object |
remove(Page p,
long writeVersion,
Object key)
Remove a key-value pair.
|
protected void |
removePage(long pos,
int memory)
Remove the given page (make the space available).
|
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.
|
protected Page |
splitRootIfNeeded(Page p,
long writeVersion)
Split the root page if necessary.
|
String |
toString() |
clone, containsValue, putAll, values
finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
containsValue, putAll, values
protected MVStore store
protected volatile Page root
protected volatile long writeVersion
protected void init(MVStore store, HashMap<String,Object> config)
store
- the storeconfig
- the configurationprotected Page splitRootIfNeeded(Page p, long writeVersion)
p
- the pagewriteVersion
- the write versionprotected Object put(Page p, long writeVersion, Object key, Object value)
p
- the pagewriteVersion
- the write versionkey
- the key (may not be null)value
- the value (may not be null)public K firstKey()
public K lastKey()
public K getKey(long index)
This is a O(log(size)) operation.
index
- the indexpublic List<K> keyList()
The get and indexOf methods are O(log(size)) operations. The result of indexOf is cast to an int.
public long getKeyIndex(K key)
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.
key
- the keyprotected K getFirstLast(boolean first)
first
- whether to retrieve the first keypublic K higherKey(K key)
key
- the keypublic K ceilingKey(K key)
key
- the keypublic K floorKey(K key)
key
- the keypublic K lowerKey(K key)
key
- the keyprotected K getMinMax(K key, boolean min, boolean excluding)
key
- the keymin
- whether to retrieve the smallest keyexcluding
- if the given upper/lower bound is exclusiveprotected Object binarySearch(Page p, Object key)
p
- the pagekey
- the keypublic boolean containsKey(Object key)
containsKey
in interface Map<K,V>
containsKey
in class AbstractMap<K,V>
protected Page binarySearchPage(Page p, Object key)
p
- the parent pagekey
- the keypublic void clear()
public boolean isClosed()
public V putIfAbsent(K key, V value)
putIfAbsent
in interface ConcurrentMap<K,V>
putIfAbsent
in interface Map<K,V>
key
- the key (may not be null)value
- the new valuepublic boolean remove(Object key, Object value)
public boolean areValuesEqual(Object a, Object b)
a
- the first valueb
- the second valuepublic boolean replace(K key, V oldValue, V newValue)
protected Object remove(Page p, long writeVersion, Object key)
p
- the page (may not be null)writeVersion
- the write versionkey
- the keyprotected void newRoot(Page newRoot)
newRoot
- the new root pagepublic DataType getKeyType()
public DataType getValueType()
public Iterator<K> keyIterator(K from)
from
- the first key to returnpublic Cursor<K,V> cursor(K from)
from
- the first key to returnpublic Page getRoot()
public String getName()
public MVStore getStore()
public int getId()
public boolean isReadOnly()
public void setVolatile(boolean isVolatile)
isVolatile
- the volatile flagpublic boolean isVolatile()
protected void beforeWrite()
UnsupportedOperationException
- if the map is read-only,
or if another thread is concurrently writingpublic int hashCode()
public boolean equals(Object o)
public int size()
public long sizeAsLong()
public boolean isEmpty()
public long getCreateVersion()
protected void removePage(long pos, int memory)
pos
- the position of the page to removememory
- the number of bytes used for this pagepublic MVMap<K,V> openVersion(long version)
version
- the versionpublic long getVersion()
protected int getChildPageCount(Page p)
p
- the pagepublic String getType()
public String toString()
toString
in class AbstractMap<K,V>
Copyright © 2017 JBoss by Red Hat. All rights reserved.