[jboss-cvs] JBossCache/src/org/jboss/cache/loader ...
Manik Surtani
msurtani at jboss.com
Wed Oct 25 10:36:28 EDT 2006
User: msurtani
Date: 06/10/25 10:36:28
Modified: src/org/jboss/cache/loader Tag: Branch_JBossCache_1_4_0
FileCacheLoader.java
Log:
Fixed javadoc
Revision Changes Path
No revision
No revision
1.15.2.1 +237 -175 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
retrieving revision 1.15.2.1
diff -u -b -r1.15 -r1.15.2.1
--- FileCacheLoader.java 30 May 2006 12:42:31 -0000 1.15
+++ FileCacheLoader.java 25 Oct 2006 14:36:28 -0000 1.15.2.1
@@ -11,57 +11,79 @@
import org.jboss.invocation.MarshalledValueInputStream;
import org.jboss.invocation.MarshalledValueOutputStream;
-import java.io.*;
-import java.util.*;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
/**
* 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 2006/05/30 12:42:31 msurtani Exp $
+ * @version $Id: FileCacheLoader.java,v 1.15.2.1 2006/10/25 14:36:28 msurtani Exp $
*/
-public class FileCacheLoader implements ExtendedCacheLoader {
- File root=null;
- TreeCache cache=null;
- Log log=LogFactory.getLog(getClass());
+public class FileCacheLoader implements ExtendedCacheLoader
+{
+ File root = null;
+ TreeCache cache = null;
+ Log log = LogFactory.getLog(getClass());
RegionManager manager;
- /** 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 */
+ /**
+ * 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
+ */
Map transactions = new ConcurrentHashMap();
/**
* TreeCache data file.
*/
- public static final String DATA="data.dat";
+ public static final String DATA = "data.dat";
/**
* TreeCache directory suffix.
*/
- public static final String DIR_SUFFIX="fdb";
+ public static final String DIR_SUFFIX = "fdb";
- public FileCacheLoader() {
+ public FileCacheLoader()
+ {
}
- public void setConfig(Properties props) {
- String location=props != null? props.getProperty("location") : null;
- if(location != null && location.length() > 0)
- root=new File(location);
+ public void setConfig(Properties props)
+ {
+ String location = props != null ? props.getProperty("location") : null;
+ if (location != null && location.length() > 0)
+ root = new File(location);
}
- public void setCache(TreeCache c) {
- cache=c;
+ public void setCache(TreeCache c)
+ {
+ cache = c;
}
- public void create() throws Exception {
- if(root == null) {
- String tmpLocation=System.getProperty("java.io.tmpdir", "C:\\tmp");
- root=new File(tmpLocation);
+ public void create() throws Exception
+ {
+ if (root == null)
+ {
+ String tmpLocation = System.getProperty("java.io.tmpdir", "C:\\tmp");
+ root = new File(tmpLocation);
}
- if(!root.exists()) {
- if(log.isTraceEnabled())
+ if (!root.exists())
+ {
+ if (log.isTraceEnabled())
log.trace("Creating cache loader location " + root);
- boolean created=root.mkdirs();
- if(!created)
+ boolean created = root.mkdirs();
+ if (!created)
throw new IOException("Unable to create cache loader location " + root);
}
@@ -69,31 +91,36 @@
throw new IOException("Cache loader location [" + root + "] is not a directory!");
}
- public void start() throws Exception {
+ public void start() throws Exception
+ {
}
- public void stop() {
+ public void stop()
+ {
}
- public void destroy() {
+ public void destroy()
+ {
}
-
- public Set getChildrenNames(Fqn fqn) throws Exception {
- File parent=getDirectory(fqn, false);
- if(parent == null) return null;
- File[] children=parent.listFiles();
- HashSet s=new HashSet();
- for(int i=0; i < children.length; i++) {
- File child=children[i];
- if(child.isDirectory() && child.getName().endsWith(DIR_SUFFIX)) {
- String child_name=child.getName();
- child_name=child_name.substring(0, child_name.lastIndexOf(DIR_SUFFIX)-1);
+ public Set getChildrenNames(Fqn fqn) throws Exception
+ {
+ File parent = getDirectory(fqn, false);
+ if (parent == null) return null;
+ File[] children = parent.listFiles();
+ HashSet s = new HashSet();
+ for (int i = 0; i < children.length; i++)
+ {
+ File child = children[i];
+ if (child.isDirectory() && child.getName().endsWith(DIR_SUFFIX))
+ {
+ String child_name = child.getName();
+ child_name = child_name.substring(0, child_name.lastIndexOf(DIR_SUFFIX) - 1);
s.add(child_name);
}
}
- return s.size() == 0? null : s;
+ return s.size() == 0 ? null : s;
}
// See http://jira.jboss.com/jira/browse/JBCACHE-118 for why this is commented out.
@@ -104,54 +131,62 @@
// return m.get(key);
// }
- public Map get(Fqn fqn) throws Exception {
+ public Map get(Fqn fqn) throws Exception
+ {
return loadAttributes(fqn);
// Map m=loadAttributes(fqn);
// if(m == null || m.size() == 0) return null;
// return m;
}
- public boolean exists(Fqn fqn) throws Exception {
- File f=getDirectory(fqn, false);
+ public boolean exists(Fqn fqn) throws Exception
+ {
+ File f = getDirectory(fqn, false);
return f != null;
}
- public Object put(Fqn fqn, Object key, Object value) throws Exception {
+ public Object put(Fqn fqn, Object key, Object value) throws Exception
+ {
Object retval;
- Map m=loadAttributes(fqn);
- if(m == null) m=new HashMap();
- retval=m.put(key, value);
+ Map m = loadAttributes(fqn);
+ if (m == null) m = new HashMap();
+ retval = m.put(key, value);
storeAttributes(fqn, m);
return retval;
}
- public void put(Fqn fqn, Map attributes) throws Exception {
+ public void put(Fqn fqn, Map attributes) throws Exception
+ {
put(fqn, attributes, false);
}
- public void put(Fqn fqn, Map attributes, boolean erase) throws Exception {
- Map m=erase? new HashMap() : loadAttributes(fqn);
- if(m == null) m=new HashMap();
- if(attributes != null)
+ public void put(Fqn fqn, Map attributes, boolean erase) throws Exception
+ {
+ Map m = erase ? new HashMap() : loadAttributes(fqn);
+ if (m == null) m = new HashMap();
+ if (attributes != null)
m.putAll(attributes);
storeAttributes(fqn, m);
}
- void put(Fqn fqn) throws Exception {
+ void put(Fqn fqn) throws Exception
+ {
getDirectory(fqn, true);
}
/**
- *
* @param modifications List<Modification>
* @throws Exception
*/
- public void put(List modifications) throws Exception {
- if(modifications == null) return;
- for(Iterator it=modifications.iterator(); it.hasNext();) {
- Modification m=(Modification)it.next();
- switch(m.getType()) {
+ public void put(List modifications) throws Exception
+ {
+ if (modifications == null) return;
+ for (Iterator it = modifications.iterator(); it.hasNext();)
+ {
+ Modification m = (Modification) it.next();
+ switch (m.getType())
+ {
case Modification.PUT_DATA:
put(m.getFqn(), m.getData());
break;
@@ -177,61 +212,72 @@
}
}
- public Object remove(Fqn fqn, Object key) throws Exception {
+ public Object remove(Fqn fqn, Object key) throws Exception
+ {
Object retval;
- Map m=loadAttributes(fqn);
- if(m == null) return null;
- retval=m.remove(key);
+ Map m = loadAttributes(fqn);
+ if (m == null) return null;
+ retval = m.remove(key);
storeAttributes(fqn, m);
return retval;
}
- public void remove(Fqn fqn) throws Exception {
- File dir=getDirectory(fqn, false);
- if(dir != null) {
- boolean flag=removeDirectory(dir, true);
- if(!flag)
+ public void remove(Fqn fqn) throws Exception
+ {
+ File dir = getDirectory(fqn, false);
+ if (dir != null)
+ {
+ boolean flag = removeDirectory(dir, true);
+ if (!flag)
log.warn("failed removing " + fqn);
}
}
- public void removeData(Fqn fqn) throws Exception {
- File f=getDirectory(fqn, false);
- if(f != null) {
- File data=new File(f, DATA);
- if(data.exists()) {
- boolean flag=data.delete();
- if(!flag)
+ public void removeData(Fqn fqn) throws Exception
+ {
+ File f = getDirectory(fqn, false);
+ if (f != null)
+ {
+ File data = new File(f, DATA);
+ if (data.exists())
+ {
+ boolean flag = data.delete();
+ if (!flag)
log.warn("failed removing file " + data.getName());
}
}
}
- public void prepare(Object tx, List modifications, boolean one_phase) throws Exception {
- if(one_phase)
+ public void prepare(Object tx, List modifications, boolean one_phase) throws Exception
+ {
+ if (one_phase)
put(modifications);
else
transactions.put(tx, modifications);
}
- public void commit(Object tx) throws Exception {
- List modifications=(List)transactions.remove(tx);
+ public void commit(Object tx) throws Exception
+ {
+ List modifications = (List) transactions.remove(tx);
if (modifications == null)
throw new Exception("transaction " + tx + " not found in transaction table");
put(modifications);
}
- public void rollback(Object tx) {
+ public void rollback(Object tx)
+ {
transactions.remove(tx);
}
/**
* Loads the entire state from the filesystem and returns it as a byte buffer. The format of the byte buffer
* must be a list of NodeData elements
+ *
* @return
* @throws Exception
*/
- public byte[] loadEntireState() throws Exception {
+ public byte[] loadEntireState() throws Exception
+ {
return loadState(Fqn.ROOT);
}
@@ -244,8 +290,8 @@
// Set the TCCL to any classloader registered for subtree
setUnmarshallingClassLoader(subtree);
- ByteArrayOutputStream out_stream=new ByteArrayOutputStream(1024);
- ObjectOutputStream out=new MarshalledValueOutputStream(out_stream);
+ ByteArrayOutputStream out_stream = new ByteArrayOutputStream(1024);
+ ObjectOutputStream out = new MarshalledValueOutputStream(out_stream);
loadStateFromFilessystem(subtree, out);
out.close();
@@ -260,10 +306,12 @@
/**
* Stores the state given as a byte buffer to the filesystem. The byte
* buffer contains a list of zero or more NodeData elements
+ *
* @param state
* @throws Exception
*/
- public void storeEntireState(byte[] state) throws Exception {
+ public void storeEntireState(byte[] state) throws Exception
+ {
storeState(state, Fqn.ROOT);
}
@@ -275,8 +323,8 @@
// Set the TCCL to any classloader registered for subtree
setUnmarshallingClassLoader(subtree);
- ByteArrayInputStream in_stream=new ByteArrayInputStream(state);
- MarshalledValueInputStream in=new MarshalledValueInputStream(in_stream);
+ ByteArrayInputStream in_stream = new ByteArrayInputStream(state);
+ MarshalledValueInputStream in = new MarshalledValueInputStream(in_stream);
NodeData nd;
// remove entire existing state
@@ -289,14 +337,14 @@
Fqn fqn;
while (in_stream.available() > 0)
{
- nd=(NodeData)in.readObject();
+ nd = (NodeData) in.readObject();
if (moveToBuddy)
fqn = BuddyManager.getBackupFqn(subtree, nd.fqn);
else
fqn = nd.fqn;
- if(nd.attrs != null)
+ if (nd.attrs != null)
this.put(fqn, nd.attrs, true); // creates a node with 0 or more attributes
else
this.put(fqn); // creates a node with null attributes
@@ -318,11 +366,13 @@
/**
* Do a preorder traversal: visit the node first, then the node's children
+ *
* @param fqn Start node
* @param out
* @throws Exception
*/
- protected void loadStateFromFilessystem(Fqn fqn, ObjectOutputStream out) throws Exception {
+ protected void loadStateFromFilessystem(Fqn fqn, ObjectOutputStream out) throws Exception
+ {
Map attrs;
Set children_names;
String child_name;
@@ -330,29 +380,32 @@
NodeData nd;
// first handle the current node
- attrs=get(fqn);
- if(attrs == null || attrs.size() == 0)
- nd=new NodeData(fqn);
+ attrs = get(fqn);
+ if (attrs == null || attrs.size() == 0)
+ nd = new NodeData(fqn);
else
- nd=new NodeData(fqn, attrs);
+ nd = new NodeData(fqn, attrs);
out.writeObject(nd);
// then visit the children
- children_names=getChildrenNames(fqn);
- if(children_names == null)
+ children_names = getChildrenNames(fqn);
+ if (children_names == null)
return;
- for(Iterator it=children_names.iterator(); it.hasNext();) {
- child_name=(String)it.next();
- tmp_fqn=new Fqn(fqn, child_name);
+ for (Iterator it = children_names.iterator(); it.hasNext();)
+ {
+ child_name = (String) it.next();
+ tmp_fqn = new Fqn(fqn, child_name);
loadStateFromFilessystem(tmp_fqn, out);
}
}
- File getDirectory(Fqn fqn, boolean create) {
- File f=new File(getFullPath(fqn));
- if(!f.exists()) {
- if(create)
+ File getDirectory(Fqn fqn, boolean create)
+ {
+ File f = new File(getFullPath(fqn));
+ if (!f.exists())
+ {
+ if (create)
f.mkdirs();
else
return null;
@@ -365,76 +418,85 @@
* Recursively removes this and all subdirectories, plus all DATA files in them. To prevent damage, we only
* remove files that are named DATA (data.dat) and directories which end in ".fdb". If there is a dir or file
* that isn't named this way, the recursive removal will fail
- * @param fqn
+ *
* @return <code>true</code> if directory was removed,
* <code>false</code> if not.
*/
- boolean removeDirectory(File dir, boolean include_start_dir) {
- boolean success=true;
- File[] subdirs=dir.listFiles();
- for(int i=0; i < subdirs.length; i++) {
- File file=subdirs[i];
- if(file.isFile() && file.getName().equals(DATA)) {
- if(!file.delete())
- success=false;
+ boolean removeDirectory(File dir, boolean include_start_dir)
+ {
+ boolean success = true;
+ File[] subdirs = dir.listFiles();
+ for (int i = 0; i < subdirs.length; i++)
+ {
+ File file = subdirs[i];
+ if (file.isFile() && file.getName().equals(DATA))
+ {
+ if (!file.delete())
+ success = false;
continue;
}
- if(file.isDirectory() && file.getName().endsWith(DIR_SUFFIX)) {
- if(!removeDirectory(file, false))
- success=false;
- if(!file.delete())
- success=false;
+ if (file.isDirectory() && file.getName().endsWith(DIR_SUFFIX))
+ {
+ if (!removeDirectory(file, false))
+ success = false;
+ if (!file.delete())
+ success = false;
}
}
- if(include_start_dir) {
+ if (include_start_dir)
+ {
if (!dir.equals(root))
{
if (dir.delete())
{return success;}
- success=false;
+ success = false;
}
}
return success;
}
- String getFullPath(Fqn fqn) {
- StringBuffer sb=new StringBuffer(root.getAbsolutePath() + File.separator);
- for(int i=0; i < fqn.size(); i++) {
- Object tmp=fqn.get(i);
+ String getFullPath(Fqn fqn)
+ {
+ StringBuffer sb = new StringBuffer(root.getAbsolutePath() + File.separator);
+ for (int i = 0; i < fqn.size(); i++)
+ {
+ Object tmp = fqn.get(i);
String tmp_dir;
- if(tmp instanceof String)
- tmp_dir=(String)tmp;
+ if (tmp instanceof String)
+ tmp_dir = (String) tmp;
else
- tmp_dir=tmp.toString();
+ tmp_dir = tmp.toString();
sb.append(tmp_dir).append(".").append(DIR_SUFFIX).append(File.separator);
}
return sb.toString();
}
- protected Map loadAttributes(Fqn fqn) throws Exception {
- File f=getDirectory(fqn, false);
- if(f == null) return null; // i.e., this node does not exist.
+ protected Map loadAttributes(Fqn fqn) throws Exception
+ {
+ File f = getDirectory(fqn, false);
+ if (f == null) return null; // i.e., this node does not exist.
// this node exists so we should never return a null after this... at worst case, an empty HashMap.
- File child=new File(f, DATA);
- if(!child.exists()) return new HashMap(0); // no node attribs exist hence the empty HashMap.
+ File child = new File(f, DATA);
+ if (!child.exists()) return new HashMap(0); // no node attribs exist hence the empty HashMap.
//if(!child.exists()) return null;
- FileInputStream in=new FileInputStream(child);
- MarshalledValueInputStream input=new MarshalledValueInputStream(in);
- Map m=(Map)input.readObject();
+ FileInputStream in = new FileInputStream(child);
+ MarshalledValueInputStream input = new MarshalledValueInputStream(in);
+ Map m = (Map) input.readObject();
in.close();
return m;
}
- protected void storeAttributes(Fqn fqn, Map attrs) throws Exception {
- File f=getDirectory(fqn, true);
- File child=new File(f, DATA);
+ protected void storeAttributes(Fqn fqn, Map attrs) throws Exception
+ {
+ File f = getDirectory(fqn, true);
+ File child = new File(f, DATA);
if (!child.exists())
if (!child.createNewFile())
throw new IOException("Unable to create file: " + child);
- FileOutputStream out=new FileOutputStream(child);
- ObjectOutputStream output=new ObjectOutputStream(out);
+ FileOutputStream out = new FileOutputStream(child);
+ ObjectOutputStream output = new ObjectOutputStream(out);
output.writeObject(attrs);
out.close();
}
More information about the jboss-cvs-commits
mailing list