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

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

public class DocumentCache
extends java.lang.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
private  class DocumentCache.DocManager
          Manages the retrieval of a document for a corresponding open definitions document.
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.
 
Field Summary
private  java.lang.Object _cacheLock
           
private static Log _log
          Log file.
private  java.util.LinkedList<DocumentCache.RegistrationListener> _regListeners
           
private  java.util.LinkedHashSet<DocumentCache.DocManager> _residentQueue
           
private  int CACHE_SIZE
          invariant _residentQueue.size() <= CACHE_SIZE
private static int IN_QUEUE
           
private static int INIT_CACHE_SIZE
           
private static int NOT_IN_QUEUE
           
private static int UNMANAGED
           
private static int UNTITLED
           
 
Constructor Summary
DocumentCache()
           
DocumentCache(int size)
           
 
Method Summary
 void addRegistrationListener(DocumentCache.RegistrationListener list)
           
 void clearRegistrationListeners()
           
 int getCacheSize()
           
 int getNumInCache()
           
private  void notifyRegistrationListeners(OpenDefinitionsDocument odd, DocumentCache.DocManager man)
           
 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.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_log

private static final Log _log
Log file.


INIT_CACHE_SIZE

private static final int INIT_CACHE_SIZE
See Also:
Constant Field Values

CACHE_SIZE

private int CACHE_SIZE
invariant _residentQueue.size() <= CACHE_SIZE


_residentQueue

private java.util.LinkedHashSet<DocumentCache.DocManager> _residentQueue

_cacheLock

private java.lang.Object _cacheLock

IN_QUEUE

private static final int IN_QUEUE
See Also:
Constant Field Values

UNTITLED

private static final int UNTITLED
See Also:
Constant Field Values

NOT_IN_QUEUE

private static final int NOT_IN_QUEUE
See Also:
Constant Field Values

UNMANAGED

private static final int UNMANAGED
See Also:
Constant Field Values

_regListeners

private java.util.LinkedList<DocumentCache.RegistrationListener> _regListeners
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 java.lang.String toString()
Overrides:
toString in class java.lang.Object

addRegistrationListener

public void addRegistrationListener(DocumentCache.RegistrationListener list)

removeRegistrationListener

public void removeRegistrationListener(DocumentCache.RegistrationListener list)

clearRegistrationListeners

public void clearRegistrationListeners()

notifyRegistrationListeners

private void notifyRegistrationListeners(OpenDefinitionsDocument odd,
                                         DocumentCache.DocManager man)