[jboss-cvs] JBossCache/src/org/jboss/cache/loader ...
Galder Zamarreno
galder.zamarreno at jboss.com
Fri Jun 29 09:33:38 EDT 2007
User: gzamarreno
Date: 07/06/29 09:33:38
Modified: src/org/jboss/cache/loader Tag: Branch_JBossCache_1_4_0
FileCacheLoader.java JDBCCacheLoader.java
CacheLoader.java
Log:
[JBCACHE-1103] Finished the backport from head.
Revision Changes Path
No revision
No revision
1.15.2.2 +134 -58 JBossCache/src/org/jboss/cache/loader/FileCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: FileCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/FileCacheLoader.java,v
retrieving revision 1.15.2.1
retrieving revision 1.15.2.2
diff -u -b -r1.15.2.1 -r1.15.2.2
--- FileCacheLoader.java 25 Oct 2006 14:36:28 -0000 1.15.2.1
+++ FileCacheLoader.java 29 Jun 2007 13:33:38 -0000 1.15.2.2
@@ -6,6 +6,7 @@
import org.jboss.cache.Fqn;
import org.jboss.cache.Modification;
import org.jboss.cache.TreeCache;
+import org.jboss.cache.lock.StripedLock;
import org.jboss.cache.buddyreplication.BuddyManager;
import org.jboss.cache.marshall.RegionManager;
import org.jboss.invocation.MarshalledValueInputStream;
@@ -30,7 +31,8 @@
* Simple file-based CacheLoader implementation. Nodes are directories, attributes of a node is a file in the directory
*
* @author Bela Ban
- * @version $Id: FileCacheLoader.java,v 1.15.2.1 2006/10/25 14:36:28 msurtani Exp $
+ * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
+ * @version $Id: FileCacheLoader.java,v 1.15.2.2 2007/06/29 13:33:38 gzamarreno Exp $
*/
public class FileCacheLoader implements ExtendedCacheLoader
{
@@ -39,6 +41,8 @@
Log log = LogFactory.getLog(getClass());
RegionManager manager;
+ protected final StripedLock lock = new StripedLock();
+
/**
* HashMap<Object,List<Modification>>. List of open transactions. Note that this is purely transient, as
* we don't use a log, recovery is not available
@@ -73,6 +77,9 @@
public void create() throws Exception
{
+ lock.acquireLock(Fqn.ROOT, true);
+ try
+ {
if (root == null)
{
String tmpLocation = System.getProperty("java.io.tmpdir", "C:\\tmp");
@@ -90,6 +97,11 @@
if (!root.isDirectory())
throw new IOException("Cache loader location [" + root + "] is not a directory!");
}
+ finally
+ {
+ lock.releaseLock(Fqn.ROOT);
+ }
+ }
public void start() throws Exception
{
@@ -106,6 +118,9 @@
public Set getChildrenNames(Fqn fqn) throws Exception
{
+ lock.acquireLock(fqn, true);
+ try
+ {
File parent = getDirectory(fqn, false);
if (parent == null) return null;
File[] children = parent.listFiles();
@@ -122,6 +137,11 @@
}
return s.size() == 0 ? null : s;
}
+ finally
+ {
+ lock.releaseLock(fqn);
+ }
+ }
// See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
@@ -133,20 +153,39 @@
public Map get(Fqn fqn) throws Exception
{
+ lock.acquireLock(fqn, true);
+ try
+ {
return loadAttributes(fqn);
// Map m=loadAttributes(fqn);
// if(m == null || m.size() == 0) return null;
// return m;
}
+ finally
+ {
+ lock.releaseLock(fqn);
+ }
+ }
public boolean exists(Fqn fqn) throws Exception
{
+ lock.acquireLock(fqn, true);
+ try
+ {
File f = getDirectory(fqn, false);
return f != null;
}
+ finally
+ {
+ lock.releaseLock(fqn);
+ }
+ }
public Object put(Fqn fqn, Object key, Object value) throws Exception
{
+ lock.acquireLock(fqn, true);
+ try
+ {
Object retval;
Map m = loadAttributes(fqn);
if (m == null) m = new HashMap();
@@ -154,6 +193,11 @@
storeAttributes(fqn, m);
return retval;
}
+ finally
+ {
+ lock.releaseLock(fqn);
+ }
+ }
public void put(Fqn fqn, Map attributes) throws Exception
{
@@ -163,12 +207,20 @@
public void put(Fqn fqn, Map attributes, boolean erase) throws Exception
{
+ lock.acquireLock(fqn, true);
+ try
+ {
Map m = erase ? new HashMap() : loadAttributes(fqn);
if (m == null) m = new HashMap();
if (attributes != null)
m.putAll(attributes);
storeAttributes(fqn, m);
}
+ finally
+ {
+ lock.releaseLock(fqn);
+ }
+ }
void put(Fqn fqn) throws Exception
{
@@ -214,6 +266,9 @@
public Object remove(Fqn fqn, Object key) throws Exception
{
+ lock.acquireLock(fqn, true);
+ try
+ {
Object retval;
Map m = loadAttributes(fqn);
if (m == null) return null;
@@ -221,9 +276,17 @@
storeAttributes(fqn, m);
return retval;
}
+ finally
+ {
+ lock.releaseLock(fqn);
+ }
+ }
public void remove(Fqn fqn) throws Exception
{
+ lock.acquireLock(fqn, true);
+ try
+ {
File dir = getDirectory(fqn, false);
if (dir != null)
{
@@ -232,9 +295,17 @@
log.warn("failed removing " + fqn);
}
}
+ finally
+ {
+ lock.releaseLock(fqn);
+ }
+ }
public void removeData(Fqn fqn) throws Exception
{
+ lock.acquireLock(fqn, true);
+ try
+ {
File f = getDirectory(fqn, false);
if (f != null)
{
@@ -247,6 +318,11 @@
}
}
}
+ finally
+ {
+ lock.releaseLock(fqn);
+ }
+ }
public void prepare(Object tx, List modifications, boolean one_phase) throws Exception
{
1.11.4.3 +104 -58 JBossCache/src/org/jboss/cache/loader/JDBCCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: JDBCCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/JDBCCacheLoader.java,v
retrieving revision 1.11.4.2
retrieving revision 1.11.4.3
diff -u -b -r1.11.4.2 -r1.11.4.3
--- JDBCCacheLoader.java 9 Oct 2006 16:34:34 -0000 1.11.4.2
+++ JDBCCacheLoader.java 29 Jun 2007 13:33:38 -0000 1.11.4.3
@@ -11,6 +11,7 @@
import org.jboss.cache.Fqn;
import org.jboss.cache.Modification;
import org.jboss.cache.TreeCache;
+import org.jboss.cache.lock.StripedLock;
import org.jboss.invocation.MarshalledValue;
import org.jboss.invocation.MarshalledValueInputStream;
import org.jboss.invocation.MarshalledValueOutputStream;
@@ -63,7 +64,7 @@
*
* @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
* @author <a href="mailto:hmesha at novell.com">Hany Mesha </a>
- * @version <tt>$Revision: 1.11.4.2 $</tt>
+ * @version <tt>$Revision: 1.11.4.3 $</tt>
*/
public class JDBCCacheLoader
implements CacheLoader
@@ -92,7 +93,7 @@
private String datasourceName;
private ConnectionFactory cf;
-
+ protected StripedLock lock = new StripedLock();
public void setConfig(Properties props)
{
@@ -199,6 +200,7 @@
con = cf.getConnection();
ps = con.prepareStatement(selectChildNamesSql);
ps.setString(1, fqn.toString());
+ lock.acquireLock(fqn, false);
rs = ps.executeQuery();
if(rs.next())
{
@@ -225,6 +227,7 @@
safeClose(rs);
safeClose(ps);
cf.close(con);
+ lock.releaseLock(fqn);
}
return children == null ? null : Collections.unmodifiableSet(children);
@@ -260,9 +263,17 @@
*/
public Map get(Fqn name) throws Exception
{
+ lock.acquireLock(name, false);
+ try
+ {
final Map node = loadNode(name);
return node == NULL_NODE_IN_ROW ? new HashMap(0) : node;
}
+ finally
+ {
+ lock.releaseLock(name);
+ }
+ }
/**
* Checks that there is a row for the fqn in the database.
@@ -273,9 +284,17 @@
*/
public boolean exists(Fqn name) throws Exception
{
+ lock.acquireLock(name, false);
+ try
+ {
final Map node = loadNode(name);
return node != null;// && node != NULL_NODE_IN_ROW;
}
+ finally
+ {
+ lock.releaseLock(name);
+ }
+ }
/**
* Adds/overrides a value in a node for a key.
@@ -291,6 +310,9 @@
*/
public Object put(Fqn name, Object key, Object value) throws Exception
{
+ lock.acquireLock(name, true);
+ try
+ {
Map oldNode = loadNode(name);
Object oldValue;
Map node;
@@ -327,6 +349,11 @@
return oldValue;
}
+ finally
+ {
+ lock.releaseLock(name);
+ }
+ }
/**
* Adds attributes from the passed in map to the existing node.
@@ -382,6 +409,9 @@
*/
public Object remove(Fqn name, Object key) throws Exception
{
+ lock.acquireLock(name, true);
+ try
+ {
Object removedValue = null;
Map node = loadNode(name);
if(node != null && node != NULL_NODE_IN_ROW)
@@ -398,6 +428,11 @@
}
return removedValue;
}
+ finally
+ {
+ lock.releaseLock(name);
+ }
+ }
/**
* Removes a node and all its children.
@@ -421,6 +456,7 @@
con = cf.getConnection();
ps = con.prepareStatement(deleteAllSql);
+ lock.acquireLock(name, true);
int deletedRows = ps.executeUpdate();
if(log.isDebugEnabled())
@@ -465,6 +501,7 @@
}
}
+ lock.acquireLock(name, true);
int deletedRows = ps.executeUpdate();
if(log.isDebugEnabled())
@@ -482,6 +519,7 @@
{
safeClose(ps);
cf.close(con);
+ lock.releaseLock(name);
}
}
@@ -891,6 +929,9 @@
// JBCACHE-769 -- make a defensive copy
Map attrs = (attributes == null ? null : new HashMap(attributes));
+ lock.acquireLock(name, true);
+ try
+ {
Map oldNode = loadNode(name);
if(oldNode != null)
{
@@ -916,6 +957,11 @@
insertNode(name, attrs);
}
}
+ finally
+ {
+ lock.releaseLock(name);
+ }
+ }
/**
* Inserts a node into the database
1.5.4.1 +5 -1 JBossCache/src/org/jboss/cache/loader/CacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/loader/CacheLoader.java,v
retrieving revision 1.5
retrieving revision 1.5.4.1
diff -u -b -r1.5 -r1.5.4.1
--- CacheLoader.java 2 Apr 2006 07:33:15 -0000 1.5
+++ CacheLoader.java 29 Jun 2007 13:33:38 -0000 1.5.4.1
@@ -23,8 +23,12 @@
* called, followed by another {@link #start()}. Finally, when shut down,
* {@link #destroy()} is called, after which the loader is unusable.
*
+ * It is important to note that all implementations are thread safe, as concurrent reads and writes, potentially even to
+ * the same {@link Fqn}, are possible.
+ *
* @author Bela Ban Oct 31, 2003
- * @version $Id: CacheLoader.java,v 1.5 2006/04/02 07:33:15 genman Exp $
+ * @author <a href="mailto:galder.zamarreno at jboss.com">Galder Zamarreno</a>
+ * @version $Id: CacheLoader.java,v 1.5.4.1 2007/06/29 13:33:38 gzamarreno Exp $
*/
public interface CacheLoader extends Service {
More information about the jboss-cvs-commits
mailing list