Another lookup cache checkpoint.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/WCM-DEV2/root@4405 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Britt Park
2006-11-20 22:41:46 +00:00
parent 454a26767e
commit 837095a411
3 changed files with 319 additions and 0 deletions

View File

@@ -127,6 +127,9 @@
<property name="avmNodeDAO"> <property name="avmNodeDAO">
<ref bean="avmNodeDAO"/> <ref bean="avmNodeDAO"/>
</property> </property>
<property name="maxSize">
<value>200</value>
</property>
</bean> </bean>
<bean id="rawServices" class="org.alfresco.repo.avm.util.RawServices"/> <bean id="rawServices" class="org.alfresco.repo.avm.util.RawServices"/>

View File

@@ -3,6 +3,13 @@
*/ */
package org.alfresco.repo.avm; package org.alfresco.repo.avm;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import org.alfresco.repo.avm.util.SimplePath; import org.alfresco.repo.avm.util.SimplePath;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@@ -14,6 +21,34 @@ public class LookupCache
{ {
private static Logger fgLogger = Logger.getLogger(LookupCache.class); private static Logger fgLogger = Logger.getLogger(LookupCache.class);
/**
* The Map of of keys to lookups.
*/
private Map<LookupKey, Lookup> fCache;
/**
* The Map of time stamps to keys.
*/
private SortedMap<Long, LookupKey> fTimeStamps;
/**
* The inverse map of keys to timestamps.
*/
private Map<LookupKey, Long> fInverseTimeStamps;
/**
* The timestamp to next issue.
*/
private long fTimeStamp;
/**
* The maximum number of lines to have in the cache.
*/
private int fMaxSize;
/**
* Reference to the Node DAO.
*/
private AVMNodeDAO fAVMNodeDAO; private AVMNodeDAO fAVMNodeDAO;
/** /**
@@ -21,6 +56,11 @@ public class LookupCache
*/ */
public LookupCache() public LookupCache()
{ {
fCache = new HashMap<LookupKey, Lookup>();
fTimeStamps = new TreeMap<Long, LookupKey>();
fInverseTimeStamps = new HashMap<LookupKey, Long>();
fTimeStamp = 0L;
fMaxSize = 100;
} }
/** /**
@@ -32,9 +72,33 @@ public class LookupCache
fAVMNodeDAO = dao; fAVMNodeDAO = dao;
} }
/**
* Set the maximum cache size.
* @param maxSize
*/
public void setMaxSize(int maxSize)
{
fMaxSize = maxSize;
}
/**
* Lookup a path. Try to fulfill the request from the cache.
* @param store The AVMStore.
* @param version The versions.
* @param path The path we are looking up.
* @param write Whether this is a write lookup.
* @param includeDeleted
* @return
*/
public Lookup lookup(AVMStore store, int version, SimplePath path, public Lookup lookup(AVMStore store, int version, SimplePath path,
boolean write, boolean includeDeleted) boolean write, boolean includeDeleted)
{ {
LookupKey key = new LookupKey(version, path, store.getName(), write, includeDeleted);
Lookup found = findInCache(key);
if (found != null)
{
return found;
}
// Make up a Lookup to hold the results. // Make up a Lookup to hold the results.
if (path.size() == 0) if (path.size() == 0)
{ {
@@ -61,6 +125,7 @@ public class LookupCache
dir = (DirectoryNode)result.getCurrentNode(); dir = (DirectoryNode)result.getCurrentNode();
if (path.size() == 1 && path.get(0).equals("")) if (path.size() == 1 && path.get(0).equals(""))
{ {
updateCache(key, result);
return result; return result;
} }
// Now look up each path element in sequence up to one // Now look up each path element in sequence up to one
@@ -89,6 +154,127 @@ public class LookupCache
return null; return null;
} }
result.add(child, path.get(path.size() - 1), write); result.add(child, path.get(path.size() - 1), write);
updateCache(key, result);
return result; return result;
} }
/**
* Try to find a match in the cache.
* @param key The lookup key.
* @return A valid for this session Lookup or null if not found.
*/
private synchronized Lookup findInCache(LookupKey key)
{
return null;
}
/**
* Add or update an entry in the cache.
* @param key
* @param lookup
*/
private synchronized void updateCache(LookupKey key, Lookup lookup)
{
if (fCache.containsKey(key))
{
fCache.remove(key);
Long oldTime = fInverseTimeStamps.get(key);
fInverseTimeStamps.remove(key);
fTimeStamps.remove(oldTime);
}
long timeStamp = fTimeStamp++;
fTimeStamps.put(timeStamp, key);
fInverseTimeStamps.put(key, timeStamp);
fCache.put(key, lookup);
if (fCache.size() > fMaxSize)
{
// Get rid of the oldest entry.
Long oldTime = fTimeStamps.firstKey();
LookupKey old = fTimeStamps.remove(oldTime);
fInverseTimeStamps.remove(old);
fCache.remove(old);
}
}
/**
* Remove a List of entries.
* @param keys The List of entries.
*/
private void purgeEntries(List<LookupKey> keys)
{
for (LookupKey key : keys)
{
fCache.remove(key);
Long time = fInverseTimeStamps.remove(key);
fTimeStamps.remove(time);
}
}
// Following are the cache invalidation calls.
/**
* Called when a simple write operation occurs. This
* invalidates all read lookups and all layered lookups.
*/
public synchronized void onWrite(String storeName)
{
List<LookupKey> toDelete = new ArrayList<LookupKey>();
for (Map.Entry<LookupKey, Lookup> entry : fCache.entrySet())
{
if ((entry.getKey().getStoreName().equals(storeName) &&
!entry.getKey().isWrite()) || entry.getValue().isLayered())
{
toDelete.add(entry.getKey());
}
}
purgeEntries(toDelete);
}
/**
* Called when a delete has occurred in a store. This invalidates both
* reads and write lookups in that store.
*/
public synchronized void onDelete(String storeName)
{
List<LookupKey> toDelete = new ArrayList<LookupKey>();
for (Map.Entry<LookupKey, Lookup> entry : fCache.entrySet())
{
if (entry.getKey().getStoreName().equals(storeName) ||
entry.getValue().isLayered())
{
toDelete.add(entry.getKey());
}
}
purgeEntries(toDelete);
}
/**
* Called when a snapshot occurs in a store. This invalidates write
* lookups. Read lookups stay untouched.
*/
public synchronized void onSnapshot(String storeName)
{
List<LookupKey> toDelete = new ArrayList<LookupKey>();
for (Map.Entry<LookupKey, Lookup> entry : fCache.entrySet())
{
if ((entry.getKey().getStoreName().equals(storeName) &&
entry.getKey().isWrite()) ||
entry.getValue().isLayered())
{
toDelete.add(entry.getKey());
}
}
purgeEntries(toDelete);
}
/**
* Called when a rollback has occurred. This invalidates the entire
* cache. Heavy handed but quick.
*/
public synchronized void onRollback()
{
fCache.clear();
fTimeStamps.clear();
fInverseTimeStamps.clear();
}
} }

View File

@@ -0,0 +1,130 @@
/**
*
*/
package org.alfresco.repo.avm;
import org.alfresco.repo.avm.util.SimplePath;
/**
* This is the key by which Lookup's are retrieved from the cache.
* @author britt
*/
public class LookupKey
{
/**
* The name of the store.
*/
private String fStoreName;
/**
* The path being looked up.
*/
private SimplePath fPath;
/**
* The version being looked up.
*/
private int fVersion;
/**
* Whether the lookup is a write lookup.
*/
private boolean fWrite;
/**
* Whether the lookup includes deleted nodes.
*/
private boolean fIncludeDeleted;
/**
* Create one from whole cloth.
* @param version The version we're looking under.
* @param path The path.
* @param storeName The name of the store.
* @param write Whether this is a write lookup.
* @param includeDeleted Whether this lookup should include deleted items.
*/
public LookupKey(int version,
SimplePath path,
String storeName,
boolean write,
boolean includeDeleted)
{
fVersion = version;
fPath = path;
fStoreName = storeName;
fWrite = write;
fIncludeDeleted = includeDeleted;
}
/**
* Set the writeness of this key.
*/
public void setWrite(boolean write)
{
fWrite = write;
}
/**
* Get the store name for this key.
* @return The store name.
*/
public String getStoreName()
{
return fStoreName;
}
/**
* Is this a write lookup.
* @return Whether this is a write lookup.
*/
public boolean isWrite()
{
return fWrite;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (!(obj instanceof LookupKey))
{
return false;
}
LookupKey o = (LookupKey)obj;
return fStoreName.equals(o.fStoreName) &&
fVersion == o.fVersion &&
fPath.equals(o.fPath) &&
fWrite == o.fWrite &&
fIncludeDeleted == o.fIncludeDeleted;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode()
{
int hash = fStoreName.hashCode();
hash += fPath.hashCode();
hash += fVersion;
hash += fWrite ? 1 : 0;
hash += fIncludeDeleted ? 1 : 0;
return hash;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
return fStoreName + ":" + fPath;
}
}