public class PersistitMap<K,V> extends AbstractMap<K,V> implements SortedMap<K,V>
A persistent java.util.SortedMap
over a Persistit database. Keys
and values inserted into this map are serialized and stored within Persistit
using the encoding methods of Key
and Value
.
To construct a PersistitMap
you supply an Exchange
. The
keys inserted into this map are appended to the key within the Tree
originally supplied by the Exchange
. For example, the code
Exchange ex = new Exchange("demo", "composers", true);
ex.append("My Favorite Johns");
Map<String, String> map = new PersistitMap<String, String>(ex);
map.put("Brahms", "Johannes");
map.put("Bach", "Johann");
is equivalent to
Exchange ex = new Exchange("demo", "composers", true);
ex.getValue().put("Johannes");
ex.clear().append("My Favorite Johns", "Brahms").store();
ex.getValue().put("Johann");
ex.clear().append("My Favorite Johns", "Bach").store();
By default any Iterator
s created by PersistitMap
's
collection views implement fail-fast behavior, meaning that methods of
the the Iterator
will throw a
ConcurrentModificationException
if the backing store has changed
after the Iterator
was created. However, in a large persistent
database designed for concurrent access, it may be preferable to allow the
Iterator
to function continuously even when the underlying
database is changing. The setAllowConcurrentModification(boolean)
method
provides control over this behavior.
PersistitMap departs from the general contract for SortedMap
with respect to the Comparable
interface. The ordering of items
within the map is determined by the encoding of key values into their
underlying serialized byte array representation (see Key Ordering
). Generally this ordering corresponds to the default Comparable
implementation for each supported type. However, PersistitMap
permits storage of key values that may not permit comparison under their
compareTo
implementations, does not require key values to
implement Comparable
, and ignores the ordering implemented by
Comparable
. Similarly, PersistitMap
does not allow
installation of a custom Comparator
. To
customize the ordering of key values in a PersistitMap
,
implement and register a custom KeyCoder
.
Unlike other Map
implementations, the methods of this class
are thread-safe. Each of the Map
methods is synchronized
on this PersistitMap
instance, and each of the methods of any
collection view Iterator
created by this Map is synchronized on
that iterator. Thus this class enforces serialized access to its internal
memory structures. However, for maximum concurrency an application that
shares data among multiple threads should create a separate instance of
PersistitMap for each thread backed by the same tree rather than sharing a
single PersistitMap object. These PersistitMap
instances can
concurrently modify and query the same collection of data.
Unlike an in-memory implementation of SortedMap
, this
implementation serializes and deserializes object values into and from byte
arrays stored as Persistit key/value pairs. Thus the put(K, V)
and
remove(java.lang.Object)
methods not only read the former value from the database, but
also incur the overhead of deserializing it. In many cases applications do
not use the former value, and therefore this computation is often
unnecessary. For maximum performance an application can use the
putFast(java.lang.Object, java.lang.Object)
and removeFast(java.lang.Object)
methods. These methods are analogous
to put(K, V)
and remove(java.lang.Object)
but do not deserialize or return the
former object value.
The Iterator
implementations returned by the keySet()
,
values()
and entrySet()
provide methods to set and access a
KeyFilter
for the iterator's traversal of keys in the map. With a
filter in place, the iterator only returns keys, values or entries for
records whose keys are selected by the filter.
Note on generic types: compile-time type verification cannot check the contents of the data stored in the backing B-Tree. Therefore the types of Key and Value objects stored in the B-Tree are not guaranteed to match the expected types. For example, the following will compile correctly but throw a ClassCastException at runtime:
Exchange ex = new Exchange("demo", "composers", true);
PersistitMap<String, Integer> map1 = new PersistitMap<String, Integer>();
map1.put("Adams", Integer.valueOf(1));
PersistitMap<String, String> map2 = new PersistitMap<String, String>();
String lastName = map2.get("Adams");
Note that any PersistitException
that may occur during execution of
one of the SortedMap
methods is thrown within the unchecked
wrapper class PersistitMap.PersistitMapException
. Applications using
PersistitMap
should catch and handled
PersistitMapException
.
Modifier and Type | Class and Description |
---|---|
class |
PersistitMap.ExchangeEntry<K,V>
Implement
java.util.Map.Entry using a backing Persistit
tree. |
class |
PersistitMap.ExchangeIterator<E>
Implements
java.util.Iterator using an underlying Persistit
tree as the source of keys and values to be traversed. |
static class |
PersistitMap.PersistitMapException
Unchecked wrapper for
PersistitException s that may be thrown by
methods of SortedMap . |
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Constructor and Description |
---|
PersistitMap(Exchange ex)
Construct a PersistitMap over a particular Exchange.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all mappings from this map.
|
protected Object |
clone()
Returns a shallow copy of this
AbstractMap instance: the
keys and values themselves are not cloned. |
Comparator<K> |
comparator()
Returns null because PersistitMap always uses Persistit's natural
ordering and no other Comparator is possible.
|
boolean |
containsKey(Object key)
Determine whether this map contains a mapping for the specified key.
|
boolean |
containsValue(Object value)
Determine whether this map maps one or more keys to this value.
|
Set<Map.Entry<K,V>> |
entrySet()
Returns a Set of Map.Entry values corresponding with key-value pairs in
the backing B-Tree.
|
boolean |
equals(Object o)
If the supplied object is also a Map, then this method determines whether
the supplied map and this map contain the same entries.
|
K |
firstKey()
Returns the first (lowest) key currently in this
PersistitMap determined by the key ordering specification. |
V |
get(Object key)
Return the value to which this map maps the specified key.
|
int |
hashCode()
Returns the hash code value for this map.
|
SortedMap<K,V> |
headMap(Object toKey)
Returns a view of the portion of this
PersistitMap whose
keys are strictly less than toKey . |
boolean |
isAllowConcurrentModification()
Indicate whether a new
Iterator s should throw a
ConcurrentModificationException in the event the Persistit
Tree backing this Map changes. |
boolean |
isEmpty()
Return
true if this map contains no key-value mappings. |
Set<K> |
keySet()
Returns a Set view of the keys contained in this map.
|
K |
lastKey()
Returns the last (highest) key currently in this
PersistitMap as determined by the key ordering specification. |
V |
put(K key,
V value)
Stores the specified value with the specified key in this map.
|
void |
putAll(Map t)
Copies all of the mappings from the specified map to this map.
|
void |
putFast(Object key,
Object value)
Associates the specified value with the specified key in this map.
|
V |
remove(Object key)
Removes the mapping for this key from this map if present, and returns
the former value.
|
void |
removeFast(Object key)
Removes the mapping for this key from this map if present.
|
void |
setAllowConcurrentModification(boolean allow)
Control whether iterators of the collection views of this Map will throw
ConcurrentModificationException in the event the underlying
physical database changes. |
int |
size()
Return the number of key-value mappings in this map.
|
SortedMap<K,V> |
subMap(Object fromKey,
Object toKey)
Returns a view of the portion of this
PersistitMap whose
keys range from fromKey , inclusive, to toKey ,
exclusive. |
SortedMap<K,V> |
tailMap(Object fromKey)
Returns a view of the portion of this
PersistitMap whose
keys are greater than or equal to than fromKey . |
String |
toString()
Returns a string representation of this map.
|
Collection<V> |
values()
Returns a collection view of the values contained in this map.
|
finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
public PersistitMap(Exchange ex)
ex
- A Exchange
that serves as the parent of the Map's
keys. This constructor makes a copy of the
Exchange
. The original Exchange is unchanged, and
may be reused by the caller.public boolean isAllowConcurrentModification()
Iterator
s should throw a
ConcurrentModificationException
in the event the Persistit
Tree backing this Map changes.true
if concurrent modifications are currently
allowed, otherwise false
.public void setAllowConcurrentModification(boolean allow)
ConcurrentModificationException
in the event the underlying
physical database changes. This property is used in constructing an new
Iterator
on a collection view. Changing this property after
an Iterator
has been constructed does not change that
Iterator
's behavior.allow
- Specify true to allow changes in the backing Tree.
Specify false to cause newly constructed
Iterator
s to throw a
ConcurrentModificationException
if the Persistit
Tree backing this Map changes.public int size()
Integer.MAX_VALUE
elements,
the value returned is Integer.MAX_VALUE
.
This implementation enumerates all the members of the Map, which for a large database could be time-consuming.
public boolean isEmpty()
true
if this map contains no key-value mappings.
This implementation is relatively efficient because it enumerates at most one child node of the Exchange to determine whether there are any children.
public boolean containsValue(Object value)
true
if and only if this map contains at
least one mapping to a value v
such that
(value==null ? v==null : value.equals(v))
This implementation implements a linear search across the child nodes of
the Exchange, and can therefore be extremely time-consuming and
resource-intensive for large databases.containsValue
in interface Map<K,V>
containsValue
in class AbstractMap<K,V>
value
- value whose presence in this map is to be tested.true
if this map maps one or more keys to this
value.public boolean containsKey(Object key)
containsKey
in interface Map<K,V>
containsKey
in class AbstractMap<K,V>
key
- key whose presence in this map is to be tested.true
if this map contains a mapping for the
specified key.NullPointerException
- key is null
and this map does not not permit
null
keys.public V get(Object key)
Return the value to which this map maps the specified key. Returns
null
if the map contains no mapping for this key. A return
value of null
does not necessarily indicate that the
map contains no mapping for the key; it's also possible that the map
explicitly maps the key to null
. The containsKey operation
may be used to distinguish these two cases.
This implementation iterates over entrySet()
searching for
an entry with the specified key. If such an entry is found, the entry's
value is returned. If the iteration terminates without finding such an
entry, null
is returned. Note that this implementation
requires linear time in the size of the map; many implementations will
override this method.
get
in interface Map<K,V>
get
in class AbstractMap<K,V>
key
- key whose associated value is to be returned.NullPointerException
- if the key is null
and this map does not not
permit null
keys.containsKey(Object)
public V put(K key, V value)
Stores the specified value with the specified key in this map. If the map
previously contained a mapping for this key, the old value is replaced.
This method returns the old value, if present, or null
if
there was no former value. An application that does not require the
former value can use putFast(java.lang.Object, java.lang.Object)
to avoid incurring the cost of
deserializing that value.
put
in interface Map<K,V>
put
in class AbstractMap<K,V>
key
- key with which the specified value is to be associated.value
- value to be associated with the specified key.null
if there was no mapping for key. (A
null
return can also indicate that the map
previously associated null
with the specified key,
if the implementation supports null
values.)ClassCastException
- if the class of the specified key or value prevents it from
being stored in this map.IllegalArgumentException
- if some aspect of this key or value * prevents it from being
stored in this map.public void putFast(Object key, Object value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.
This method differs from put(K, V)
by not returning the previous
value. In order to return the previous value, put
must
deserialize it, which can be costly. Applications that don't need that
value can benefit from calling putFast
instead of
put
.
key
- key with which the specified value is to be associated.value
- value to be associated with the specified key.ClassCastException
- if the class of the specified key or value prevents it from
being stored in this map.IllegalArgumentException
- if some aspect of this key or value * prevents it from being
stored in this map.public V remove(Object key)
Removes the mapping for this key from this map if present, and returns
the former value. Applications that do not require the former value can
use removeFast(java.lang.Object)
to improve performance.
remove
in interface Map<K,V>
remove
in class AbstractMap<K,V>
key
- key whose mapping is to be removed from the map.null
if there was no entry for key. (A
null
return can also indicate that the map
previously associated null
with the specified key.)public void removeFast(Object key)
Removes the mapping for this key from this map if present. Unlike
remove(java.lang.Object)
, this method does not return the former value, and
therefore does not incur the cost of deserializing it.
key
- key whose mapping is to be removed from the map.public void putAll(Map t)
This implementation iterates over the specified map's
entrySet()
collection, and calls this map's put
operation once for each entry returned by the iteration.
putAll
in interface Map<K,V>
putAll
in class AbstractMap<K,V>
t
- mappings to be stored in this map.ClassCastException
- if the class of a key or value in the specified map prevents
it from being stored in this map.IllegalArgumentException
- if some aspect of a key or value in the specified map
prevents it from being stored in this map.NullPointerException
- the specified map is null
.public void clear()
This implementation calls entrySet().clear()
.
public Set<K> keySet()
This implementation returns a Set that subclasses AbstractSet. The subclass's iterator method returns a "wrapper object" over this map's entrySet() iterator. The size method delegates to this map's size method and the contains method delegates to this map's containsKey method.
The Set is created the first time this method is called, and returned in response to all subsequent calls. No synchronization is performed, so there is a slight chance that multiple calls to this method will not all return the same Set.
public Collection<V> values()
Iterator.remove
, Collection.remove
,
removeAll
, retainAll
and clear
operations. It does not support the add
or
addAll
operations.
This implementation returns a collection that subclasses abstract
collection. The subclass's iterator method returns a "wrapper object"
over this map's entrySet()
iterator. The size method
delegates to this map's size method and the contains method delegates to
this map's containsValue method.
The collection is created the first time this method is called, and returned in response to all subsequent calls. No synchronization is performed, so there is a slight chance that multiple calls to this method will not all return the same Collection.
public Set<Map.Entry<K,V>> entrySet()
public Comparator<K> comparator()
comparator
in interface SortedMap<K,V>
public SortedMap<K,V> subMap(Object fromKey, Object toKey)
PersistitMap
whose
keys range from fromKey
, inclusive, to toKey
,
exclusive. The view is another PersistitMap
instance backed
by this one; any change to the collection made by either by either
instance is reflected in the other.public SortedMap<K,V> headMap(Object toKey)
PersistitMap
whose
keys are strictly less than toKey
. The view is another
PersistitMap
instance backed by this one; any change to the
collection made by either by either instance is reflected in the other.public SortedMap<K,V> tailMap(Object fromKey)
PersistitMap
whose
keys are greater than or equal to than fromKey
. The view is
another PersistitMap
instance backed by this one; any change
to the collection made by either by either instance is reflected in the
other.public K firstKey()
PersistitMap
determined by the key ordering specification.firstKey
in interface SortedMap<K,V>
PersistitMap
.NoSuchElementException
- if this map is empty.public K lastKey()
PersistitMap
as determined by the key ordering specification.lastKey
in interface SortedMap<K,V>
PersistitMap
.NoSuchElementException
- if this map is empty.public boolean equals(Object o)
public int hashCode()
hashCode
in interface Map<K,V>
hashCode
in class AbstractMap<K,V>
Map.Entry.hashCode()
,
Object.hashCode()
,
Object.equals(Object)
,
Set.equals(Object)
public String toString()
entrySet
view's iterator, enclosed in braces (
"{}"
). Adjacent mappings are separated by the characters
", "
(comma and space). Each key-value mapping is rendered
as the key followed by an equals sign ("="
) followed by the
associated value. Keys and values are converted to strings as by
String.valueOf(Object)
.
toString
in class AbstractMap<K,V>
protected Object clone() throws CloneNotSupportedException
AbstractMap
instance: the
keys and values themselves are not cloned.clone
in class AbstractMap<K,V>
CloneNotSupportedException
Copyright © 2025 Open Identity Platform Community. All rights reserved.