public class WeakTopicIndex
extends java.lang.Object
implements java.lang.Runnable
WeakTopicIndex keeps an index of every topic and association created with it.
Database topic map creates all topics and associations with it instead of
creating them directly. The index is done
with WeakReference
s so that Java garbage collector can collect
the indexed topics at any time if there are no strong references to them
elsewhere. With this kind of index, it is possible to make sure that
at any time there is at most one topic object for each individual topic in the
database but at the same time allow garbage collector to clean topics that are
not referenced anywhere else than the index.
WeakTopicIndex needs to have a cleaner thread running that cleans the WeakReferences when garbage collector has collected the referred objects.
In addition, a strong reference to 2000 most used topics is maintained. This
acts as a sort of cache to keep frequently accessed topics in memory even if
strong references are not kept outside the index. This cache is maintained with
the WeakTopicIndex.CacheList
class.
Modifier and Type | Class and Description |
---|---|
static class |
WeakTopicIndex.CacheList<E>
Cache list is a list of objects where the most used objects are at the top
of the list and least used objects are at the bottom.
|
Modifier and Type | Field and Description |
---|---|
private java.util.HashMap<java.lang.String,java.lang.ref.WeakReference<DatabaseAssociation>> |
associationIDIndex |
private java.util.HashMap<java.lang.ref.WeakReference<DatabaseAssociation>,java.lang.String> |
associationInvIDIndex |
private java.lang.ref.ReferenceQueue<DatabaseAssociation> |
associationRefQueue |
private boolean |
running |
private java.lang.Thread |
thread |
private java.util.HashMap<java.lang.String,java.lang.ref.WeakReference<DatabaseTopic>> |
topicBNIndex |
private WeakTopicIndex.CacheList<DatabaseTopic> |
topicCache
A CacheList that keeps (strong) references to topics so they do not get
carbage collected.
|
private java.util.HashMap<java.lang.String,java.lang.ref.WeakReference<DatabaseTopic>> |
topicIDIndex |
private java.util.HashMap<java.lang.ref.WeakReference<DatabaseTopic>,java.lang.String> |
topicInvBNIndex |
private java.util.HashMap<java.lang.ref.WeakReference<DatabaseTopic>,java.lang.String> |
topicInvIDIndex |
private MultiHashMap<java.lang.ref.WeakReference<DatabaseTopic>,Locator> |
topicInvSIIndex |
private java.lang.ref.ReferenceQueue<DatabaseTopic> |
topicRefQueue |
private java.util.HashMap<Locator,java.lang.ref.WeakReference<DatabaseTopic>> |
topicSIIndex |
private boolean |
useRefQueue
if references queues are being used, that is if we are keeping track
of topics/associations that have been garbage collected
|
Constructor and Description |
---|
WeakTopicIndex()
Creates a new instance of WeakTopicIndex
|
Modifier and Type | Method and Description |
---|---|
void |
addNullBN(java.lang.String bn) |
void |
addNullSI(Locator si) |
void |
clearTopicCache()
Clears the topic cache allowing any topic that isn't (strongly) referenced
elsewhere to be gargbage collected and removed from the index.
|
boolean |
containsKeyWithBN(java.lang.String bn) |
boolean |
containsKeyWithID(java.lang.String id) |
boolean |
containsKeyWithSI(Locator si) |
DatabaseAssociation |
createAssociation(java.lang.String id,
DatabaseTopicMap tm) |
DatabaseTopic |
createTopic(java.lang.String id,
DatabaseTopicMap tm)
Constructs a DatabaseTopic with the given ID.
|
void |
destroy() |
DatabaseAssociation |
getAssociation(java.lang.String id,
DatabaseTopicMap tm) |
DatabaseTopic |
getTopicWithBN(java.lang.String bn) |
DatabaseTopic |
getTopicWithID(java.lang.String id) |
DatabaseTopic |
getTopicWithSI(Locator si) |
boolean |
isFullIndex()
If the indexes are complete.
|
boolean |
isNullBN(java.lang.String bn) |
boolean |
isNullSI(Locator si) |
DatabaseAssociation |
newAssociation(DatabaseTopic type,
DatabaseTopicMap tm) |
DatabaseTopic |
newTopic(DatabaseTopicMap tm)
Creates a new topic and adds it to the index.
|
DatabaseTopic |
newTopic(java.lang.String id,
DatabaseTopicMap tm)
Creates a new topic and adds it to the index.
|
void |
printDebugInfo() |
private void |
removeAssociationKey(java.lang.ref.Reference<? extends DatabaseAssociation> ref) |
private void |
removeTopicKey(java.lang.ref.Reference<? extends DatabaseTopic> ref)
Removes entries that refer to the given WeakReferenc from topic indexes.
|
void |
run() |
void |
startCleanerThread()
Restarts the thread that is cleaning indexes of topics/associations that
have been garbage collected.
|
void |
stopCleanerThread()
Stops the thread that is cleaning indexes of topics/associations that have
been garbage collected.
|
private DatabaseTopic |
topicAccessed(DatabaseTopic topic)
Accesses a topic moving it up or adding it to the CacheList.
|
void |
topicBNChanged(Topic t,
java.lang.String old) |
void |
topicRemoved(Topic t) |
void |
topicSIChanged(DatabaseTopic t,
Locator deleted,
Locator added) |
private boolean running
private java.lang.Thread thread
private java.lang.ref.ReferenceQueue<DatabaseTopic> topicRefQueue
private java.util.HashMap<java.lang.String,java.lang.ref.WeakReference<DatabaseTopic>> topicIDIndex
private java.util.HashMap<Locator,java.lang.ref.WeakReference<DatabaseTopic>> topicSIIndex
private java.util.HashMap<java.lang.String,java.lang.ref.WeakReference<DatabaseTopic>> topicBNIndex
private java.util.HashMap<java.lang.ref.WeakReference<DatabaseTopic>,java.lang.String> topicInvIDIndex
private MultiHashMap<java.lang.ref.WeakReference<DatabaseTopic>,Locator> topicInvSIIndex
private java.util.HashMap<java.lang.ref.WeakReference<DatabaseTopic>,java.lang.String> topicInvBNIndex
private java.lang.ref.ReferenceQueue<DatabaseAssociation> associationRefQueue
private java.util.HashMap<java.lang.String,java.lang.ref.WeakReference<DatabaseAssociation>> associationIDIndex
private java.util.HashMap<java.lang.ref.WeakReference<DatabaseAssociation>,java.lang.String> associationInvIDIndex
private WeakTopicIndex.CacheList<DatabaseTopic> topicCache
private boolean useRefQueue
public void clearTopicCache()
public void stopCleanerThread()
public void startCleanerThread()
public boolean isFullIndex()
public void destroy()
public DatabaseTopic newTopic(java.lang.String id, DatabaseTopicMap tm) throws TopicMapException
TopicMapException
public DatabaseTopic newTopic(DatabaseTopicMap tm) throws TopicMapException
TopicMapException
private DatabaseTopic topicAccessed(DatabaseTopic topic)
public DatabaseTopic createTopic(java.lang.String id, DatabaseTopicMap tm)
public DatabaseTopic getTopicWithID(java.lang.String id)
public boolean containsKeyWithID(java.lang.String id)
public boolean containsKeyWithSI(Locator si)
public boolean containsKeyWithBN(java.lang.String bn)
public boolean isNullSI(Locator si)
public boolean isNullBN(java.lang.String bn)
public DatabaseTopic getTopicWithSI(Locator si)
public DatabaseTopic getTopicWithBN(java.lang.String bn)
public void topicSIChanged(DatabaseTopic t, Locator deleted, Locator added)
public void addNullSI(Locator si)
public void addNullBN(java.lang.String bn)
public void topicBNChanged(Topic t, java.lang.String old) throws TopicMapException
TopicMapException
public void topicRemoved(Topic t) throws TopicMapException
TopicMapException
public DatabaseAssociation newAssociation(DatabaseTopic type, DatabaseTopicMap tm) throws TopicMapException
TopicMapException
public DatabaseAssociation createAssociation(java.lang.String id, DatabaseTopicMap tm)
public DatabaseAssociation getAssociation(java.lang.String id, DatabaseTopicMap tm)
private void removeTopicKey(java.lang.ref.Reference<? extends DatabaseTopic> ref)
private void removeAssociationKey(java.lang.ref.Reference<? extends DatabaseAssociation> ref)
public void printDebugInfo()
public void run()
run
in interface java.lang.Runnable
Copyright 2004-2015 Wandora Team