/*
* $Header: /home/cvspublic/jakarta-slide/src/share/org/apache/slide/util/TxLRUObjectCache.java,v 1.1.2.1 2004/02/05 16:05:14 mholz Exp $
* $Revision: 1.1.2.1 $
* $Date: 2004/02/05 16:05:14 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.slide.util;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.slide.util.logger.Logger;
import org.apache.commons.collections.LRUMap;
/**
* Transactional LRU object cache. Caches objects using a least-recently-used strategy.
*
* It provides basic isolation from other transactions and atomicity of all operations. As
* no locking is used (which is undesirable mainly as a cache should never block and a commit must never fail)
* serializability needs to be guaranteed by underlying stores.
* <br>
* <br>
* <em>Caution</em>: Only global caches are limited by given size.
* Size of temporary data inside a transaction is unlimited.
* <br>
* <br>
* <em>Note</em>: This cache has no idea if the data it caches in transactions are read from or written to store.
* It thus handles both access types the same way. This means read accesses are cached in transactions even though they
* could be cached globally. Like write accesses they will be moved to global cache at commit time.
*
* @author <a href="mailto:ozeigermann@c1-fse.de">Oliver Zeigermann</a>
* @version $Revision: 1.1.2.1 $
*/
public class TxLRUObjectCache {
protected Map globalCache;
protected Map txChangeCaches;
protected Map txDeleteCaches;
protected int hits = 0;
protected int misses = 0;
protected String name;
protected Logger logger;
protected String logChannel;
protected final boolean loggingEnabled;
/**
* Creates a new object cache.
*
* @param globalCacheSize maximum size in objects of global cache
* @param name the name used to construct logging category / channel
* @param logger Slide logger to be used for logging
*/
public TxLRUObjectCache(int globalCacheSize, String name, Logger logger) {
globalCache = new LRUMap(globalCacheSize);
txChangeCaches = new HashMap();
txDeleteCaches = new HashMap();
this.name = name;
this.logger = logger;
logChannel = "TxLRUObjectCache";
if (name != null) {
logChannel += "." + name;
}
// used for guarded logging as preparation is expensive
loggingEnabled = logger.isEnabled(logChannel, Logger.DEBUG);
}
public TxLRUObjectCache(int globalCacheSize) {
this(globalCacheSize, null, null);
}
public synchronized void clear() {
globalCache.clear();
txChangeCaches.clear();
txDeleteCaches.clear();
}
public synchronized Object get(Object txId, Object key) {
if (txId != null) {
Set deleteCache = (Set) txDeleteCaches.get(txId);
if (deleteCache.contains(key)) {
hit(txId, key);
// reflects that entry has been deleted in this tx
return null;
}
Map changeCache = (Map) txChangeCaches.get(txId);
Object changed = changeCache.get(key);
if (changed != null) {
hit(txId, key);
// if object has been changed in this tx, get the local one
return changed;
}
}
// as fall back return value from global cache (if present)
Object global = globalCache.get(key);
if (global != null) {
hit(txId, key);
} else {
miss(txId, key);
}
return global;
}
public synchronized void put(Object txId, Object key, Object value) {
if (txId != null) {
// if it has been deleted before, undo this
Set deleteCache = (Set) txDeleteCaches.get(txId);
deleteCache.remove(key);
Map changeCache = (Map) txChangeCaches.get(txId);
changeCache.put(key, value);
if (loggingEnabled) {
logger.log(txId + " added '" + key + "'", logChannel, Logger.DEBUG);
}
} else {
globalCache.put(key, value);
if (loggingEnabled) {
logger.log("Added '" + key + "'", logChannel, Logger.DEBUG);
}
}
}
public synchronized void remove(Object txId, Object key) {
if (txId != null) {
// if it has been changed before, undo this
Map changeCache = (Map) txChangeCaches.get(txId);
changeCache.remove(key);
Set deleteCache = (Set) txDeleteCaches.get(txId);
deleteCache.add(key);
// guard logging as preparation is expensive
if (loggingEnabled) {
logger.log(txId + " removed '" + key + "'", logChannel, Logger.DEBUG);
}
} else {
globalCache.remove(key);
if (loggingEnabled) {
logger.log("Removed '" + key + "'", logChannel, Logger.DEBUG);
}
}
}
public synchronized void start(Object txId) {
if (txId != null) {
txChangeCaches.put(txId, new HashMap());
txDeleteCaches.put(txId, new HashSet());
}
}
public synchronized void rollback(Object txId) {
if (txId != null) {
if (loggingEnabled) {
Map changeCache = (Map) txChangeCaches.get(txId);
Set deleteCache = (Set) txDeleteCaches.get(txId);
logger.log(
txId
+ " rolled back "
+ changeCache.size()
+ " changes and "
+ deleteCache.size()
+ " scheduled deletes",
logChannel,
Logger.DEBUG);
}
// simply forget about tx
forget(txId);
}
}
public synchronized void commit(Object txId) {
if (txId != null) {
// apply local changes and deletes (is atomic as we have a global lock on this TxCache)
Map changeCache = (Map) txChangeCaches.get(txId);
for (Iterator it = changeCache.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
globalCache.put(entry.getKey(), entry.getValue());
}
Set deleteCache = (Set) txDeleteCaches.get(txId);
for (Iterator it = deleteCache.iterator(); it.hasNext();) {
Object key = it.next();
globalCache.remove(key);
}
if (loggingEnabled) {
logger.log(
txId
+ " committed "
+ changeCache.size()
+ " changes and "
+ deleteCache.size()
+ " scheduled deletes",
logChannel,
Logger.DEBUG);
}
// finally forget about tx
forget(txId);
}
}
public synchronized void forget(Object txId) {
if (txId != null) {
txChangeCaches.remove(txId);
txDeleteCaches.remove(txId);
}
}
protected void hit(Object txId, Object key) {
hits++;
log(txId, key, true);
}
protected void miss(Object txId, Object key) {
misses++;
log(txId, key, false);
}
protected void log(Object txId, Object key, boolean hit) {
if (loggingEnabled) {
StringBuffer log = new StringBuffer();
if (txId != null) {
Map changeCache = (Map) txChangeCaches.get(txId);
Set deleteCache = (Set) txDeleteCaches.get(txId);
log.append(txId + " (" + changeCache.size() + ", " + deleteCache.size() + ") ");
}
log.append(
(hit ? "Cache Hit: '" : "Cache Miss: '")
+ key
+ "' "
+ hits
+ " / "
+ misses
+ " / "
+ globalCache.size());
logger.log(log.toString(), logChannel, Logger.DEBUG);
}
}
}