edu.rice.cs.drjava.model.cache
Class DocumentCache

java.lang.Object
  extended by edu.rice.cs.drjava.model.cache.DocumentCache

public class DocumentCache
extends Object

The document cache is a structure that maps OpenDefinitionsDocuments to DefinitionsDocuments (which contain the actual document text). Since the latter can consume a lot of memory, the cache virtualizes some of them using DefinitionsDocument reconstructors (DDReconstructor). It tries to limit the number of DefinitionsDocuments loaded in memory at one time, but it must of course retain all modified DefinitionsDocuments.

The cache creates a DocManager for each OpenDefinitionsDocument entered (registered) in the cache. The managers maintain the actual links to DefinitionsDocuments. Since the Managers themselves implement the DCacheAdapter interface, the model goes directly to the manager to get the instance of the DefinitionsDocument.

When a document is accessed through the document manager by the model, the cache informs the manager, which tells the active queue to add the manager to the end of the queue--if it isn't already in the queue. If the active queue had already reached maximum size, it deletes the last document in the queue to keep the queue from growing larger than its maximum size.

The resident queue only contains documents that have not been modified since their last save (except in the process of responding to notification that a document has been modified). When a document is modified for the first time, it is immediately removed from the resident queue and marked as UNMANAGED by its document manager. An UNMANAGED document remains in memory until it is saved or closed without being saved. If such a document is saved, it is inserted again in the resident queue.

Since the cache and document managers can both be concurrently accessed from multiple threads, the methods in the DocumentCache and DocManager classes are synchronized. Some operations require locks on both the cache and a document manager, but the code is written so that none of require these locks to be held simultaneously.


Nested Class Summary
static interface DocumentCache.RegistrationListener
          This interface allows the unit tests to get a handle on what's going on since the work is spread between the ODD, the cache, and the Adapters.
 
Constructor Summary
DocumentCache()
           
DocumentCache(int size)
           
 
Method Summary
 void addRegistrationListener(DocumentCache.RegistrationListener list)
           
 void clearRegistrationListeners()
           
 int getCacheSize()
           
 int getNumInCache()
           
 DCacheAdapter register(OpenDefinitionsDocument odd, DDReconstructor rec)
          Returns a cache adapter corresponding to the owner of the given reconstructor.
 void removeRegistrationListener(DocumentCache.RegistrationListener list)
           
 void setCacheSize(int size)
          Changes the number of unmodified documents allowed in the cache at one time.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DocumentCache

public DocumentCache(int size)

DocumentCache

public DocumentCache()
Method Detail

register

public DCacheAdapter register(OpenDefinitionsDocument odd,
                              DDReconstructor rec)
Returns a cache adapter corresponding to the owner of the given reconstructor.

Parameters:
odd - The open definitions document that is registering. (Useful for debugging purposes.)
rec - A reconstructor from which to create the document that is to be managed in this cache
Returns:
an adapter that allows its owner to access its definitions document

setCacheSize

public void setCacheSize(int size)
Changes the number of unmodified documents allowed in the cache at one time. Note: modified documents are not managed in the cache except in transitional situations when a queue document becomes modified. Only used in tests.


getCacheSize

public int getCacheSize()

getNumInCache

public int getNumInCache()

toString

public String toString()
Overrides:
toString in class Object

addRegistrationListener

public void addRegistrationListener(DocumentCache.RegistrationListener list)

removeRegistrationListener

public void removeRegistrationListener(DocumentCache.RegistrationListener list)

clearRegistrationListeners

public void clearRegistrationListeners()