Class BTreeMap<K,V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- org.mapdb.BTreeMap<K,V>
-
- All Implemented Interfaces:
- java.io.Closeable, java.io.Serializable, java.lang.AutoCloseable, java.util.concurrent.ConcurrentMap<K,V>, java.util.concurrent.ConcurrentNavigableMap<K,V>, java.util.Map<K,V>, java.util.NavigableMap<K,V>, java.util.SortedMap<K,V>, Bind.MapWithModificationListener<K,V>
public class BTreeMap<K,V> extends java.util.AbstractMap<K,V> implements java.util.concurrent.ConcurrentNavigableMap<K,V>, Bind.MapWithModificationListener<K,V>, java.io.Closeable, java.io.Serializable
A scalable concurrent
ConcurrentNavigableMapimplementation. The map is sorted according to the natural ordering of its keys, or by aComparatorprovided at map creation time.Insertion, removal, update, and access operations safely execute concurrently by multiple threads. Iterators are weakly consistent, returning elements reflecting the state of the map at some point at or since the creation of the iterator. They do not throw
ConcurrentModificationException, and may proceed concurrently with other operations. Ascending key ordered views and their iterators are faster than descending ones.It is possible to obtain consistent iterator by using
snapshot()method.All Map.Entry pairs returned by methods in this class and its views represent snapshots of mappings at the time they were produced. They do not support the Entry.setValue method. (Note however that it is possible to change mappings in the associated map using put, putIfAbsent, or replace, depending on exactly which effect you need.)
This collection has optional size counter. If this is enabled Map size is kept in
Atomic.Longvariable. Keeping counter brings considerable overhead on inserts and removals. If the size counter is not enabled the size method is not a constant-time operation. Determining the current number of elements requires a traversal of the elements.Additionally, the bulk operations putAll, equals, and clear are not guaranteed to be performed atomically. For example, an iterator operating concurrently with a putAll operation might view only some of the added elements. NOTE: there is an optional
This class and its views and iterators implement all of the optional methods of the
MapandIteratorinterfaces. Like most other concurrent collections, this class does not permit the use of null keys or values because some null return values cannot be reliably distinguished from the absence of elements.Theoretical design of BTreeMap is based on 1986 paper Concurrent operations on B∗-trees with overtaking written by Yehoshua Sagiv. More practical aspects of BTreeMap implementation are based on notes and demo application from Thomas Dinsdale-Young. Also more work from Thomas: A Simple Abstraction for Complex Concurrent Indexes
B-Linked-Tree used here does not require locking for read. Updates and inserts locks only one, two or three nodes.
This B-Linked-Tree structure does not support removal well, entry deletion does not collapse tree nodes. Massive deletion causes empty nodes and performance lost. There is workaround in form of compaction process, but it is not implemented yet.
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static classBTreeMap.BNodecommon interface for BTree nodestatic classBTreeMap.DirNodestatic classBTreeMap.KeySet<E>static classBTreeMap.LeafNode
-
Constructor Summary
Constructors Constructor and Description BTreeMap(Engine engine, boolean closeEngine, long rootRecidRef, int maxNodeSize, boolean valsOutsideNodes, long counterRecid, BTreeKeySerializer keySerializer, Serializer<V> valueSerializer, int numberOfNodeMetas)Constructor used to create new BTreeMap.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description java.util.Map.Entry<K,V>ceilingEntry(K key)KceilingKey(K key)voidcheckStructure()voidclear()voidclose()java.util.Comparator<? super K>comparator()booleancontainsKey(java.lang.Object key)booleancontainsValue(java.lang.Object value)java.util.NavigableSet<K>descendingKeySet()java.util.concurrent.ConcurrentNavigableMap<K,V>descendingMap()java.util.Set<java.util.Map.Entry<K,V>>entrySet()java.util.Map.Entry<K,V>firstEntry()KfirstKey()java.util.Map.Entry<K,V>floorEntry(K key)KfloorKey(K key)Vget(java.lang.Object key)EnginegetEngine()java.util.concurrent.ConcurrentNavigableMap<K,V>headMap(K toKey)java.util.concurrent.ConcurrentNavigableMap<K,V>headMap(K toKey, boolean inclusive)java.util.Map.Entry<K,V>higherEntry(K key)KhigherKey(K key)booleanisEmpty()java.util.NavigableSet<K>keySet()java.util.Map.Entry<K,V>lastEntry()KlastKey()java.util.Map.Entry<K,V>lowerEntry(K key)KlowerKey(K key)longmappingCount()voidmodificationListenerAdd(Bind.MapListener<K,V> listener)Add new modification listener notified when Map has been updatedvoidmodificationListenerRemove(Bind.MapListener<K,V> listener)Remove registered notification listenerjava.util.NavigableSet<K>navigableKeySet()java.util.Map.Entry<K,V>pollFirstEntry()java.util.Map.Entry<K,V>pollLastEntry()voidprintTreeStructure()Vput(K key, V value)VputIfAbsent(K key, V value)Vremove(java.lang.Object key)booleanremove(java.lang.Object key, java.lang.Object value)Vreplace(K key, V value)booleanreplace(K key, V oldValue, V newValue)intsize()longsizeLong()java.util.NavigableMap<K,V>snapshot()Make readonly snapshot view of current Map.java.util.concurrent.ConcurrentNavigableMap<K,V>subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)java.util.concurrent.ConcurrentNavigableMap<K,V>subMap(K fromKey, K toKey)java.util.concurrent.ConcurrentNavigableMap<K,V>tailMap(K fromKey)java.util.concurrent.ConcurrentNavigableMap<K,V>tailMap(K fromKey, boolean inclusive)java.util.Collection<V>values()
-
-
-
Constructor Detail
-
BTreeMap
public BTreeMap(Engine engine, boolean closeEngine, long rootRecidRef, int maxNodeSize, boolean valsOutsideNodes, long counterRecid, BTreeKeySerializer keySerializer, Serializer<V> valueSerializer, int numberOfNodeMetas)
Constructor used to create new BTreeMap.- Parameters:
engine- used for persistencecloseEngine- if this object was created without DB. If true shutdown everything on close method, otherwise DB takes care of shutdownrootRecidRef- reference to root recidmaxNodeSize- maximal BTree Node size. Node will split if number of entries is highervalsOutsideNodes- Store Values outside of BTree Nodes in separate record?counterRecid- recid under whichAtomic.Longis stored, or0for no counterkeySerializer- Serializer used for keys. May be null for default value.valueSerializer- Serializer used for values. May be null for default valuenumberOfNodeMetas- number of meta records associated with each BTree node
-
-
Method Detail
-
get
public V get(java.lang.Object key)
-
remove
public V remove(java.lang.Object key)
-
clear
public void clear()
-
isEmpty
public boolean isEmpty()
-
size
public int size()
-
sizeLong
public long sizeLong()
- Specified by:
sizeLongin interfaceBind.MapWithModificationListener<K,V>- Returns:
- size of map, but in 64bit long which does not overflow at 2e9 items.
-
mappingCount
public long mappingCount()
-
remove
public boolean remove(java.lang.Object key, java.lang.Object value)
-
comparator
public java.util.Comparator<? super K> comparator()
-
containsKey
public boolean containsKey(java.lang.Object key)
-
containsValue
public boolean containsValue(java.lang.Object value)
-
subMap
public java.util.concurrent.ConcurrentNavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
-
tailMap
public java.util.concurrent.ConcurrentNavigableMap<K,V> tailMap(K fromKey, boolean inclusive)
-
keySet
public java.util.NavigableSet<K> keySet()
-
navigableKeySet
public java.util.NavigableSet<K> navigableKeySet()
-
values
public java.util.Collection<V> values()
-
descendingKeySet
public java.util.NavigableSet<K> descendingKeySet()
-
snapshot
public java.util.NavigableMap<K,V> snapshot()
Make readonly snapshot view of current Map. Snapshot is immutable and not affected by modifications made by other threads. Useful if you need consistent view on Map. Maintaining snapshot have some overhead, underlying Engine is closed after Map view is GCed. Please make sure to release reference to this Map view, so snapshot view can be garbage collected.- Returns:
- snapshot
-
modificationListenerAdd
public void modificationListenerAdd(Bind.MapListener<K,V> listener)
Description copied from interface:Bind.MapWithModificationListenerAdd new modification listener notified when Map has been updated- Specified by:
modificationListenerAddin interfaceBind.MapWithModificationListener<K,V>- Parameters:
listener- callback interface notified when map changes
-
modificationListenerRemove
public void modificationListenerRemove(Bind.MapListener<K,V> listener)
Description copied from interface:Bind.MapWithModificationListenerRemove registered notification listener- Specified by:
modificationListenerRemovein interfaceBind.MapWithModificationListener<K,V>- Parameters:
listener- callback interface notified when map changes
-
getEngine
public Engine getEngine()
-
printTreeStructure
public void printTreeStructure()
-
checkStructure
public void checkStructure()
-
close
public void close()
- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein interfacejava.lang.AutoCloseable
-
-
DataMelt 3.0 © DataMelt by jWork.ORG