public final class DefaultAtomCache extends Object implements HGAtomCache
A default, simple implementation of a run-time cache of hypergraph atoms.
IMPLEMENTATION NOTE: This implementation maintains usage statistics (access count and last time of access), calculates an importance function based on those statistics and maintains a priority queue based on that importance. This incurs a significant memory overhead on a per atom basis. But the importance-based eviction policy is quite accurate. This implementation makes more sense when atoms are relatively large, but we need to come up and experiment with other schemas for cache maintenance. Something based on Java weak reference might work well, for example.
TODO: this implementation is NOT thread-safe. Mutexes need to be inserted at various cache manipulation points.Constructor and Description |
---|
DefaultAtomCache() |
Modifier and Type | Method and Description |
---|---|
HGLiveHandle |
atomAdded(HGPersistentHandle pHandle,
Object atom,
HGAtomAttrib attrib)
Inform the cache that a new atom has just been added to the database.
|
HGLiveHandle |
atomRead(HGPersistentHandle pHandle,
Object atom,
HGAtomAttrib attrib)
Associate an atom instance and a persistent handle with a live handle.
|
HGLiveHandle |
atomRefresh(HGLiveHandle handle,
Object atom,
boolean replace)
Replace the runtime instance of an atom with a new value.
|
void |
close()
Close the cache.
|
void |
freeze(HGLiveHandle handle)
Freezing an atom in the cache would prevent it from ever being removed.
|
HGLiveHandle |
get(HGPersistentHandle pHandle)
Lookup in the cache for a live handle corresponding to a persistent
handle.
|
HGLiveHandle |
get(Object atom)
Retrieve the live handle of an atom instance.
|
HGCache<HGPersistentHandle,IncidenceSet> |
getIncidenceCache()
Return the incidence set cache.
|
long |
getMaxAtoms() |
long |
getMaxIncidenceSets() |
double |
importanceOf(org.hypergraphdb.cache.DefaultAtomCache.LiveHandle cached) |
boolean |
isFrozen(HGLiveHandle handle)
Find out whether a given atom is frozen in the cache.
|
void |
remove(HGHandle handle)
Remove a live handle and all its associations from the cache.
|
void |
setHyperGraph(HyperGraph hg)
Set the
HyperGraph within which this cache is operating. |
void |
setIncidenceCache(HGCache<HGPersistentHandle,IncidenceSet> cache)
Set the implementation of the incidence sets cache to use.
|
void |
setMaxAtoms(long maxAtoms) |
void |
setMaxIncidenceSets(long maxIncidenceSets) |
void |
unfreeze(HGLiveHandle handle)
Unfreezing a previously frozen atom makes it available for eviction.
|
public double importanceOf(org.hypergraphdb.cache.DefaultAtomCache.LiveHandle cached)
public void setIncidenceCache(HGCache<HGPersistentHandle,IncidenceSet> cache)
HGAtomCache
Set the implementation of the incidence sets cache to use.
setIncidenceCache
in interface HGAtomCache
public HGCache<HGPersistentHandle,IncidenceSet> getIncidenceCache()
HGAtomCache
Return the incidence set cache. The incidence set cache is maintained separately from the main atom cache. Incidence sets don't hold actual atoms, but only their handles. Also, they are ordered and can be queried for membership efficiently.
getIncidenceCache
in interface HGAtomCache
public void setMaxAtoms(long maxAtoms)
public long getMaxAtoms()
public void setMaxIncidenceSets(long maxIncidenceSets)
public long getMaxIncidenceSets()
public void setHyperGraph(HyperGraph hg)
HGAtomCache
Set the HyperGraph
within which this cache is operating. A given
cache implementation may or may not need a reference to the HyperGraph
instance, depending on the level of sophistication of the cache. For example,
some cache policies may want to persist certain usage statistics in the
HGStore
, others may want to attach particular importance to certain
atom types etc.
setHyperGraph
in interface HGAtomCache
hg
- The HyperGraph
instance.public void close()
HGAtomCache
Close the cache. This will clear the cache, completely and perform any cleaning operations such as unloading of atoms and the like.
Once the cache is closed, it cannot be used again.
This method is normally invoked by HyperGraph only. The method is not guaranteed to be thread-safe. It can rely on the fact that no other threads are accessing the cache while it is executing.
close
in interface HGAtomCache
public HGLiveHandle get(HGPersistentHandle pHandle)
Lookup in the cache for a live handle corresponding to a persistent handle.
get
in interface HGAtomCache
pHandle
- The HGPersistentHandle
of the desired atom.HGLiveHandle
of the atom or null
if
the atom is not in the cache.public HGLiveHandle get(Object atom)
Retrieve the live handle of an atom instance.
get
in interface HGAtomCache
atom
- The atom object.HGLiveHandle
of that atom or null
if
the atom is not in the cache.public HGLiveHandle atomAdded(HGPersistentHandle pHandle, Object atom, HGAtomAttrib attrib)
HGAtomCache
Inform the cache that a new atom has just been added to the database.
The cache is free to decide whether that atom should be actually cached or not,
but in any case it must construct and return HGLiveHandle
for
the atom.
atomAdded
in interface HGAtomCache
pHandle
- The persistent handle of the atom.atom
- The run-time instance of the atom.attrib
- Atom management related attributes that must be stored as part of its live handle.HGLiveHandle
to the atom.public HGLiveHandle atomRead(HGPersistentHandle pHandle, Object atom, HGAtomAttrib attrib)
Associate an atom instance and a persistent handle with a live handle.
atomRead
in interface HGAtomCache
pHandle
- The persistent handle of the atom.atom
- The run-time instance of the atom.attrib
- Atom management related attributes that must be stored as part of its live handle.HGLiveHandle
to the atom.public HGLiveHandle atomRefresh(HGLiveHandle handle, Object atom, boolean replace)
HGAtomCache
Replace the runtime instance of an atom with a new value. This method is invoked when HyperGraph
needs to inform the cache about a change of the value of an atom. The cache must
assign the new reference to the cached live handle and, as the case may be, reinsert the atom
into its caching structures.
Note that there are two important cases of "refreshing" an atom in the cache - the atom
being reloaded from permanent storage or when there is an actual value change. In the
former case, a transaction abort does not need to roll back changes in the caching
structures while in the latter it does! The two cases are distinguished by the third
parameter of this method. ??? Note that the cache should perform a replace if handle.getRef() != null
&& handle.getRef() != atom
because this signals that a new value must be stored.
If the atom's value hasn't changed (i.e. if handle.getRef() == atom
) then
nothing is done.
atomRefresh
in interface HGAtomCache
handle
- The HGLiveHandle
handle of the atom to be refreshed.atom
- The atom value. HyperGraph
will obtain this value
either from the cache, in case the atom has already been re-fetched from storage
after the eviction event, or it will retrieve and create a new run-time instance.replace
- true
if this is a new atom value (old must be restored
if the transaction aborts) and false
if this is simply a reload from
permanent storage where the effects of that reload may (or may not, depending on the
implementation) be reversed in case of a transaction abort.handle
parameter depending
on the implementation. If a new instance is return, the reference of the old instance
is cleared.public void freeze(HGLiveHandle handle)
HGAtomCache
Freezing an atom in the cache would prevent it from ever being removed.
freeze
in interface HGAtomCache
handle
- The HGLiveHandle
of the atom to be frozen. The atom
must already be in the cache.public void unfreeze(HGLiveHandle handle)
HGAtomCache
Unfreezing a previously frozen atom makes it available for eviction. It is ok to unfreeze an atom that has never been frozen.
unfreeze
in interface HGAtomCache
handle
- The HGLiveHandle
of the atom to be unfrozen. The atom
must be in the cache.public boolean isFrozen(HGLiveHandle handle)
HGAtomCache
Find out whether a given atom is frozen in the cache.
isFrozen
in interface HGAtomCache
handle
- The live handle of the atom.true
if the atom is frozen (i.e. would never be evicted from the cache)
and false
otherwise.public void remove(HGHandle handle)
Remove a live handle and all its associations from the cache.
remove
in interface HGAtomCache
handle
- The HGLiveHandle
of the atom to be removed. If the atom is
currently not in the cache, nothing should be done.Copyright © 2015. All rights reserved.