[MOBY-guts] biomoby commit

Martin Senger senger at pub.open-bio.org
Wed Nov 9 16:23:32 UTC 2005


senger
Wed Nov  9 11:23:32 EST 2005
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/client
In directory pub.open-bio.org:/tmp/cvs-serv32177/src/main/org/biomoby/client

Modified Files:
	CentralDigestCachedImpl.java 
Log Message:


moby-live/Java/src/main/org/biomoby/client CentralDigestCachedImpl.java,1.11,1.12
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralDigestCachedImpl.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralDigestCachedImpl.java	2005/10/27 04:15:23	1.11
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralDigestCachedImpl.java	2005/11/09 16:23:32	1.12
@@ -23,6 +23,7 @@
 import java.io.PrintWriter;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.HashSet;
 import java.util.TreeMap;
 import java.util.Vector;
 import java.util.Arrays;
@@ -51,6 +52,9 @@
     extends CentralDigestImpl
     implements CentralAll {
 
+    private static org.apache.commons.logging.Log log =
+       org.apache.commons.logging.LogFactory.getLog (CentralDigestCachedImpl.class);
+
     // filename for a list of cached entities
     protected static final String LIST_FILE = "__L__I__S__T__";
 
@@ -132,7 +136,7 @@
      * was initiated for). Otherwise 'id' indicates which part of the
      * cache that will be removed. <p>
      *
-     * @param id should be either null, or one of the fillowing:
+     * @param id should be either null, or one of the following:
      * {@link #CACHE_PART_DATATYPES}, {@link #CACHE_PART_SERVICES},
      * {@link #CACHE_PART_SERVICETYPES}, and {@link
      * #CACHE_PART_NAMESPACES}.
@@ -156,9 +160,42 @@
 		}
 	    }
 	} catch (MobyException e) {
-	    // TBD: keep this here at least for some time (until we
-	    // have proper logging perhaps)
-	    System.err.println ("Removing cache failed: " + e.getMessage());
+	    log.error ("Removing cache failed: " + e.getMessage());
+	}
+    }
+
+    /*************************************************************************
+     * Update the indicated part of the cache. If 'id' is null it
+     * updates the whole cache (for that Moby registry this instance
+     * was initiated for). <p>
+     *
+     * Updates means to fetch a new list of entities, compare it with
+     * existing entities in the cache (only by name, not by contents),
+     * fetch the missing ones and remove the redundant ones. <p>
+     *
+     * @param id should be either null, or one of the following:
+     * {@link #CACHE_PART_DATATYPES}, {@link #CACHE_PART_SERVICES},
+     * {@link #CACHE_PART_SERVICETYPES}, and {@link
+     * #CACHE_PART_NAMESPACES}.
+     *
+     *************************************************************************/
+    public void updateCache (String id)
+	throws MobyException {
+	if (cacheDir != null) {
+	    initCache();
+	    if (id == null || id.equals (CACHE_PART_SERVICES)) {
+		remove (servicesCache, LIST_FILE);
+		fillServicesCache();
+	    } else if (id == null || id.equals (CACHE_PART_DATATYPES)) {
+		remove (dataTypesCache, LIST_FILE);
+		fillDataTypesCache();
+	    } else if (id == null || id.equals (CACHE_PART_SERVICETYPES)) {
+		remove (serviceTypesCache, LIST_FILE);
+		fillServiceTypesCache();
+	    } else if (id == null || id.equals (CACHE_PART_NAMESPACES)) {
+		remove (namespacesCache, LIST_FILE);
+		fillNamespacesCache();
+	    }
 	}
     }
 
@@ -291,6 +328,15 @@
 	}
     }
 
+    // remove a file from a cache
+    protected void remove (File cache, String name) {
+	File file = new File (cache, name);
+	// do not throw here an exception because a missing file
+	// can be a legitimate status (e.g. for LIST_FILE when we
+	// are updating)
+	file.delete();
+    }
+
     /**************************************************************************
      * Read a cached file
      *************************************************************************/
@@ -332,34 +378,95 @@
 	return true;
     }
 
-    /**************************************************************************
-     * Load data types from a moby registry into local cache. If it is
-     * interrupted (by a callback signal) it removes what was already
-     * put into this cache and returns false. Otherwise returns
-     * true. It raises an exception if it cannot fill the cache (from
-     * some other reasons).
+//     /**************************************************************************
+//      * Load data types from a moby registry into local cache. If it is
+//      * interrupted (by a callback signal) it removes what was already
+//      * put into this cache and returns false. Otherwise returns
+//      * true. It raises an exception if it cannot fill the cache (from
+//      * some other reasons).
+//      *************************************************************************/
+//     protected boolean fillDataTypesCache()
+// 	throws MobyException {
+// 	try {
+// 	    fireEvent (DATA_TYPES_START);
+// 	    String typesAsXML = getDataTypeNamesAsXML();
+// 	    store (dataTypesCache, LIST_FILE, typesAsXML);
+// 	    Map types = createDataTypeNamesFromXML (typesAsXML);
+// 	    fireEvent (DATA_TYPES_COUNT, new Integer (types.size()));
+// 	    for (Iterator it = types.entrySet().iterator(); it.hasNext(); ) {
+// 		Map.Entry entry = (Map.Entry)it.next();
+// 		String name = (String)entry.getKey();
+// 		fireEvent (DATA_TYPE_LOADING, name);
+// 		String xml = getDataTypeAsXML (name);
+// 		store (dataTypesCache, name, xml);
+// 		fireEvent (DATA_TYPE_LOADED, name);
+// 		if (stopDT) {
+// 		    removeFromCache (CACHE_PART_DATATYPES);
+// 		    return false;
+// 		}
+// 	    }
+// 	    return true;
+// 	} catch (Exception e) {
+// 	    throw new MobyException (formatException (e));
+// 	} finally {
+// 	    fireEvent (stopDT ? DATA_TYPES_CANCELLED : DATA_TYPES_END);
+// 	    stopDT = false;
+// 	}
+//     }
+
+    /**************************************************************************
+     * Update data types from a moby registry:
+     *   - get a new LIST_FILE (but do not put it into the cache yet)
+     *       if failed do nothing (except reporting it)
+     *   - remove LIST_FILE
+     *   - compare contents of new LIST_FILE with file names in the cache
+     *     and remove them, or fetched missing ones
+     *       if success add there new LIST_FILE
      *************************************************************************/
     protected boolean fillDataTypesCache()
 	throws MobyException {
 	try {
 	    fireEvent (DATA_TYPES_START);
 	    String typesAsXML = getDataTypeNamesAsXML();
-	    store (dataTypesCache, LIST_FILE, typesAsXML);
+ 	    remove (dataTypesCache, LIST_FILE);
 	    Map types = createDataTypeNamesFromXML (typesAsXML);
 	    fireEvent (DATA_TYPES_COUNT, new Integer (types.size()));
+
+	    // list of current files in this cache
+	    HashSet currentFiles = new HashSet();
+	    File[] list = dataTypesCache.listFiles();
+	    if (list == null)
+		throw new MobyException (MSG_CACHE_NOT_DIR (dataTypesCache));
+	    for (int i = 0; i < list.length; i++) {
+		if (! ignored (list[i]))
+		    currentFiles.add (list[i].getName());
+	    }
+
+	    // iterate over LIST_FILE and fetch missing files
 	    for (Iterator it = types.entrySet().iterator(); it.hasNext(); ) {
 		Map.Entry entry = (Map.Entry)it.next();
 		String name = (String)entry.getKey();
-		fireEvent (DATA_TYPE_LOADING, name);
-		String xml = getDataTypeAsXML (name);
-		store (dataTypesCache, name, xml);
-		fireEvent (DATA_TYPE_LOADED, name);
-		if (stopDT) {
-		    removeFromCache (CACHE_PART_DATATYPES);
-		    return false;
+		if ( ! currentFiles.contains (name)) {
+		    // missing file: fetch it from a registry
+		    fireEvent (DATA_TYPE_LOADING, name);
+		    String xml = getDataTypeAsXML (name);
+		    store (dataTypesCache, name, xml);
+		    fireEvent (DATA_TYPE_LOADED, name);
+		    if (stopDT) {
+			return false;
+		    }
 		}
+		currentFiles.remove (name);
 	    }
+
+	    // remove files that are not any more needed
+	    for (Iterator it = currentFiles.iterator(); it.hasNext(); )
+		remove (dataTypesCache, (String)it.next());
+
+	    // finally, put there the new LIST_FILE
+ 	    store (dataTypesCache, LIST_FILE, typesAsXML);
 	    return true;
+
 	} catch (Exception e) {
 	    throw new MobyException (formatException (e));
 	} finally {
@@ -397,27 +504,59 @@
 	}
     }
 
-    //
+    /**************************************************************************
+     * Update service types from a moby registry:
+     *   - get a new LIST_FILE (but do not put it into the cache yet)
+     *       if failed do nothing (except reporting it)
+     *   - remove LIST_FILE
+     *   - compare contents of new LIST_FILE with file names in the cache
+     *     and remove them, or fetched missing ones
+     *       if success add there new LIST_FILE
+     *************************************************************************/
     protected boolean fillServiceTypesCache()
 	throws MobyException {
 	try {
 	    fireEvent (SERVICE_TYPES_START);
 	    String typesAsXML = getServiceTypesAsXML();
-	    store (serviceTypesCache, LIST_FILE, typesAsXML);
+ 	    remove (serviceTypesCache, LIST_FILE);
 	    MobyServiceType[] types = createServiceTypesFromXML (typesAsXML);
 	    fireEvent (SERVICE_TYPES_COUNT, new Integer (types.length));
+
+	    // list of current files in this cache
+	    HashSet currentFiles = new HashSet();
+	    File[] list = serviceTypesCache.listFiles();
+	    if (list == null)
+		throw new MobyException (MSG_CACHE_NOT_DIR (serviceTypesCache));
+	    for (int i = 0; i < list.length; i++) {
+		if (! ignored (list[i]))
+		    currentFiles.add (list[i].getName());
+	    }
+
+	    // iterate over LIST_FILE and fetch missing files
 	    for (int i = 0 ; i < types.length; i++) {
 		String name = types[i].getName();
-		fireEvent (SERVICE_TYPE_LOADING, name);
-		String xml = getServiceTypeRelationshipsAsXML (name, false);
-		store (serviceTypesCache, name, xml);
-		fireEvent (SERVICE_TYPE_LOADED, name);
-		if (stopST) {
-		    removeFromCache (CACHE_PART_SERVICETYPES);
-		    return false;
+		if ( ! currentFiles.contains (name)) {
+		    // missing file: fetch it from a registry
+		    fireEvent (SERVICE_TYPE_LOADING, name);
+		    String xml = getServiceTypeRelationshipsAsXML (name, false);
+		    store (serviceTypesCache, name, xml);
+		    fireEvent (SERVICE_TYPE_LOADED, name);
+		    if (stopST) {
+			log.warn ("Service types cache not fully updated");
+			return false;
+		    }
 		}
+		currentFiles.remove (name);
 	    }
+
+	    // remove files that are not any more needed
+	    for (Iterator it = currentFiles.iterator(); it.hasNext(); )
+		remove (serviceTypesCache, (String)it.next());
+
+	    // finally, put there the new LIST_FILE
+	    store (serviceTypesCache, LIST_FILE, typesAsXML);
 	    return true;
+
 	} catch (Exception e) {
 	    throw new MobyException (formatException (e));
 	} finally {
@@ -426,6 +565,34 @@
 	}
     }
 
+//     protected boolean fillServiceTypesCache()
+// 	throws MobyException {
+// 	try {
+// 	    fireEvent (SERVICE_TYPES_START);
+// 	    String typesAsXML = getServiceTypesAsXML();
+// 	    store (serviceTypesCache, LIST_FILE, typesAsXML);
+// 	    MobyServiceType[] types = createServiceTypesFromXML (typesAsXML);
+// 	    fireEvent (SERVICE_TYPES_COUNT, new Integer (types.length));
+// 	    for (int i = 0 ; i < types.length; i++) {
+// 		String name = types[i].getName();
+// 		fireEvent (SERVICE_TYPE_LOADING, name);
+// 		String xml = getServiceTypeRelationshipsAsXML (name, false);
+// 		store (serviceTypesCache, name, xml);
+// 		fireEvent (SERVICE_TYPE_LOADED, name);
+// 		if (stopST) {
+// 		    removeFromCache (CACHE_PART_SERVICETYPES);
+// 		    return false;
+// 		}
+// 	    }
+// 	    return true;
+// 	} catch (Exception e) {
+// 	    throw new MobyException (formatException (e));
+// 	} finally {
+// 	    fireEvent (stopST ? SERVICE_TYPES_CANCELLED :SERVICE_TYPES_END);
+// 	    stopST = false;
+// 	}
+//     }
+
     //
     protected boolean fillNamespacesCache()
 	throws MobyException {
@@ -458,6 +625,12 @@
 
 	    // get a list file (with all data type names)
 	    String xmlList = getListFile (dataTypesCache);
+	    if (xmlList == null) {
+		initCache();
+		if (! fillDataTypesCache())
+		    // callback stopped filling
+		    return new TreeMap();
+	    }
 	    return createDataTypeNamesFromXML (xmlList);
 	}
     }
@@ -513,6 +686,12 @@
 
 	    // get a list file (with all service names)
 	    String xmlList = getListFile (servicesCache);
+	    if (xmlList == null) {
+		initCache();
+		if (! fillServicesCache())
+		    // callback stopped filling
+		    return new TreeMap();
+	    }
 	    return createServiceNamesByAuthorityFromXML (xmlList);
 	}
     }
@@ -568,6 +747,10 @@
 
 	    // get a list file (with all namespaces)
 	    String xmlList = getListFile (namespacesCache);
+	    if (xmlList == null) {
+		initCache();
+		fillNamespacesCache();
+	    }
 	    return createNamespacesFromXML (xmlList);
 	}
     }
@@ -583,12 +766,17 @@
 	    if (isCacheEmpty (serviceTypesCache)) {
 		initCache();
 		if (! fillServiceTypesCache())
-		    // callback stopped filling
+		    // a callback stopped filling
 		    return new MobyServiceType[] {};
 	    }
 
 	    // get a list file (with all service type names)
 	    String xmlList = getListFile (serviceTypesCache);
+	    if (xmlList == null) {
+		if (! fillServiceTypesCache())
+		    // a callback stopped filling
+		    return new MobyServiceType[] {};
+	    }
 	    MobyServiceType[] types = createServiceTypesFromXML (xmlList);
 
 	    // add details about relationship to get full service types
@@ -606,13 +794,15 @@
     }
 
     /**************************************************************************
-     *
+     * A LIST_FILE is a TOC of a cache object (each cache part has its
+     * own LIST_FILE). Read it and return it. If it does not exist,
+     * return null.
      *************************************************************************/
     protected static String getListFile (File cache)
 	throws MobyException {
 	File listFile = new File (cache, LIST_FILE);
 	if (! listFile.exists())
-	    throw new MobyException ("Corrupted cache '" + cache + "': Missing a LIST file.");
+	    return null;
 	return load (listFile);
     }
 




More information about the MOBY-guts mailing list