K
- V
- DiskMap implements the ConcurrentMapcommit(Map)
and
restore(Object)
methods that respectively write to and
read from the disk.
DiskMap is nearly indistinguishable in performance from
ConcurrentHashMap when all map entries fit in memory. If not, its
performance depends on how frequently entries are reused. If
entries are rarely reused, e.g., a sweep over a very large number
of map entries, then every access will on average force a disk
read (to unpause the entry being accessed) and a write (to pause
long idle entries). Normally, only entries that have been idle for
at least idleThreshold
paused to disk, so the caller must
use setIdleThreshold(long)
so as to ensure that either no
more than sizeThreshold
entries are accessed in that
interval or there is sufficient memory to hold all entries
accessed in the last idleThreshold
time. If not, a random
fraction of entries, including possibly recently used entries,
will be paused to disk in order to make room in memory.
DiskMap can use MultiArrayMap as its underlying in-memory map if
that is supplied in the constructor. Although not necessary, the
use of MultiArrayMap makes the map more compact because of its
cuckoo hashing design, but it requires values to implement the
KeyablePausable
, DiskMap incurs just ~6B overhead per value as it
does not have to maintain its own stats for last active times for
map entries.
Note: There is no way to ensure that the value pointed to in the
map is not modified after it has been paused to disk. It does not
help to verify that the value pointed to is the same as the
serialized value paused to disk as the value pointed to in the map
could be modified by a caller even after its serialized form has
been paused to disk. As a result, we can not support the
"expectation" that modifications to the value object by the caller
will be reflected in the map. So, the caller is forced to reckon
with the possibility that any modifications to the value object
may be lost unless the caller explicitly invokes a put
subsequently.public abstract class DiskMap<K,V> extends java.lang.Object implements java.util.concurrent.ConcurrentMap<K,V>, Diskable<K,V>
Modifier and Type | Class and Description |
---|---|
class |
DiskMap.LastActive |
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_CAPACITY |
Constructor and Description |
---|
DiskMap(java.util.concurrent.ConcurrentMap<K,V> map)
The supplied
map will be used as the underlying in-memory map. |
DiskMap(long inMemoryCapacity) |
Modifier and Type | Method and Description |
---|---|
void |
clear() |
void |
close() |
void |
close(boolean commitAll)
Will stop the GC thread but will have no other effect.
|
void |
commit()
Commits all in-memory entries in the map.
|
protected java.util.Set<K> |
commit(K key) |
abstract java.util.Set<K> |
commit(java.util.Map<K,V> toCommit)
This method should return only after successfully persisting the
key,value pair, otherwise it should throw an exception.
|
boolean |
containsKey(java.lang.Object key) |
boolean |
containsValue(java.lang.Object value) |
void |
enablePriorityQueue() |
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet()
This method only returns the in-memory entry set.
|
V |
get(java.lang.Object key) |
protected void |
hintRestore(K key,
V value) |
boolean |
isEmpty() |
java.util.Set<K> |
keySet()
This method only returns the in-memory key set.
|
V |
put(K key,
V value) |
void |
putAll(java.util.Map<? extends K,? extends V> m) |
V |
putIfAbsent(K key,
V value) |
V |
remove(java.lang.Object key) |
boolean |
remove(java.lang.Object key,
java.lang.Object value) |
V |
replace(K key,
V value) |
boolean |
replace(K key,
V oldValue,
V newValue) |
abstract V |
restore(K key) |
void |
setIdleThreshold(long idleTime)
Minimum idle time in order to be pausable.
|
void |
setPauseThreadPeriod(long period)
Period after which a pausing attempt is made by the GC thread.
|
int |
size() |
java.lang.String |
toString() |
java.util.Collection<V> |
values()
This method only returns the in-memory value set.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
public static final int DEFAULT_CAPACITY
public DiskMap(long inMemoryCapacity)
inMemoryCapacity
- Capacity for in-memory map. If the in-memory map size reaches
this value, GC forcibly kicks in.public void setIdleThreshold(long idleTime)
idleTime
- public void enablePriorityQueue()
public void setPauseThreadPeriod(long period)
period
- public abstract java.util.Set<K> commit(java.util.Map<K,V> toCommit) throws java.io.IOException
public boolean containsKey(java.lang.Object key)
public boolean containsValue(java.lang.Object value)
public java.util.Set<K> keySet()
public java.util.Collection<V> values()
public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
public boolean remove(java.lang.Object key, java.lang.Object value)
protected java.util.Set<K> commit(K key) throws java.io.IOException
java.io.IOException
public void commit()
public java.lang.String toString()
toString
in class java.lang.Object
public void close(boolean commitAll)
commitAll
- public void close()