[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Fri Jun 8 20:30:22 UTC 2007


gordonp
Fri Jun  8 16:30:22 EDT 2007
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/client
In directory dev.open-bio.org:/tmp/cvs-serv29870/src/main/org/biomoby/client

Modified Files:
	CentralCachedCallsImpl.java 
Log Message:
Code revamp to deal with new disk cache
moby-live/Java/src/main/org/biomoby/client CentralCachedCallsImpl.java,1.2,1.3
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralCachedCallsImpl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralCachedCallsImpl.java	2007/06/07 23:55:42	1.2
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralCachedCallsImpl.java	2007/06/08 20:30:22	1.3
@@ -8,11 +8,15 @@
 
 package org.biomoby.client;
 
+import org.biomoby.registry.meta.RegistryCache;
+
 import org.biomoby.shared.MobyException;
+
+import java.io.*;
 import java.util.*;
 
 public class CentralCachedCallsImpl extends CentralImpl{
-
+    protected static String CHAR_ENCODING = "UTF-8";
     protected static final String SYNTAX_TYPE = "xml";
     protected static final long THE_EPOCH = 0;  // don't care when the call is made, it shouldn't affect the onto mapping of calls to IDs
     protected static final Properties PROPERTIES = null; // don't have any properties for the call
@@ -111,6 +115,71 @@
 	return result;
     }
 
+    // check existence of a cached object
+    public boolean existsInCache (String id) {
+	if(!getCacheMode()){
+	    return false;
+	}
+	if(super.existsInCache(id)){
+	    return true;
+	}
+	
+	File cachedDataFile = RegistryCache.getCentralCallFile(getRegistryEndpoint(), id);
+	// See if it's in the disk cache, if so, load it to memory
+	if(cachedDataFile != null){
+	    try{
+		Object cachedValue = loadDataFromFile(cachedDataFile);
+		// Save the disk data to memory
+		super.setContents(id, cachedValue);
+	    } catch(Exception e){
+		e.printStackTrace();
+		System.err.println("Could not load data from cache file " + cachedDataFile);
+		return false;
+	    }
+	    return true;
+	}
+	else{
+	    return false;
+	}
+    }
+
+    protected Object loadDataFromFile(File cacheFile) throws Exception{
+	char[] contentsBuffer = new char[(int) cacheFile.length()];  // assumes filesize < 2GB
+	FileReader cacheFileReader = new FileReader(cacheFile);
+	cacheFileReader.read(contentsBuffer);	
+	return new String(contentsBuffer);
+    }
+
+    protected void storeDataToFile(File cacheFile, Object data) throws Exception{
+	if(data instanceof CharSequence){
+	    FileWriter cacheFileWriter = new FileWriter(cacheFile);
+	    cacheFileWriter.write(((CharSequence) data).toString());
+	    cacheFileWriter.close();
+	}
+	else if(data instanceof Serializable){
+	    throw new Exception("Serialization in cache not yet implemented");
+	}
+	else{
+	    throw new Exception("Asked to serialize data that is neither a CharSequence, " +
+				"nor serializable.  Found " + data.getClass().getName());
+	}
+    }
+
+    // cache an object
+    public void setContents (String id, java.lang.Object data) {
+	if(!getCacheMode()){
+	    return;
+	}
+
+	super.setContents(id, data);
+	File cachedDataFile = RegistryCache.calcCentralCallFile(getRegistryEndpoint(), id);
+	try{
+	    storeDataToFile(cachedDataFile, data);
+	} catch(Exception e){
+	    System.err.println("Could not store data to cache file " + cachedDataFile);
+	}
+    }    
+
     protected String createId(String method, Object[] parameters){
 	// Set the semantics to the call parameters's string representation concatenated
 	// If we were to be pedantic, we should covert the xml into a canonical format




More information about the MOBY-guts mailing list