[MOBY-guts] biomoby commit

Martin Senger senger at pub.open-bio.org
Thu Nov 10 08:54:31 UTC 2005


senger
Thu Nov 10 03:54:31 EST 2005
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/client
In directory pub.open-bio.org:/tmp/cvs-serv3825/src/main/org/biomoby/client

Modified Files:
	CentralDigestCachedImpl.java 
Log Message:


moby-live/Java/src/main/org/biomoby/client CentralDigestCachedImpl.java,1.12,1.13
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralDigestCachedImpl.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralDigestCachedImpl.java	2005/11/09 16:23:32	1.12
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/CentralDigestCachedImpl.java	2005/11/10 08:54:31	1.13
@@ -1,7 +1,9 @@
 // CentralDigestCachedImpl.java
 //
-//    senger at ebi.ac.uk
-//    September 2004
+// Created: September 2004
+//
+// This file is a component of the BioMoby project.
+// Copyright Martin Senger (martin.senger at gmail.com).
 //
 
 package org.biomoby.client;
@@ -13,6 +15,7 @@
 import org.biomoby.shared.MobyService;
 import org.biomoby.shared.MobyServiceType;
 import org.biomoby.shared.NoSuccessException;
+import org.biomoby.shared.Utils;
 
 import java.io.BufferedOutputStream;
 import java.io.BufferedReader;
@@ -28,6 +31,8 @@
 import java.util.Vector;
 import java.util.Arrays;
 import java.util.Comparator;
+import java.util.Properties;
+import java.util.Date;
 
 /**
  * An implementation of {@link org.biomoby.shared.CentralAll},
@@ -44,7 +49,7 @@
  * This class can be used also without caching - just instantiate it
  * with 'cacheDir' set to null in the constructor. <p>
  *
- * @author <A HREF="mailto:senger at ebi.ac.uk">Martin Senger</A>
+ * @author <A HREF="mailto:martin.senger at gmail.com">Martin Senger</A>
  * @version $Id$
  */
 
@@ -70,6 +75,16 @@
     /** An ID used in {@link #removeFromCache} indicating namespaces part. */
     public static final String CACHE_PART_NAMESPACES   = "c4";
 
+    // TBD: this will go to cache interface later, probably
+    // names of properties returned by getCacheInfo()
+    public static final String CACHE_PROP_NAME         = "cache-name";
+    public static final String CACHE_PROP_COUNT        = "cache-count";
+    public static final String CACHE_PROP_OLDEST       = "cache-oldest";
+    public static final String CACHE_PROP_YOUNGEST     = "cache-youngest";
+    public static final String CACHE_PROP_SIZE         = "cache-size";
+    public static final String CACHE_PROP_LOCATION     = "cache-loc";
+    public static final String CACHE_PROP_REGISTRY_URL = "cache-reg-url";
+
     // cache location
     private String cacheDir;        // as defined in the constructor
     protected File dataTypesCache;
@@ -193,7 +208,6 @@
 		remove (serviceTypesCache, LIST_FILE);
 		fillServiceTypesCache();
 	    } else if (id == null || id.equals (CACHE_PART_NAMESPACES)) {
-		remove (namespacesCache, LIST_FILE);
 		fillNamespacesCache();
 	    }
 	}
@@ -378,42 +392,6 @@
 	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).
-//      *************************************************************************/
-//     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)
@@ -475,27 +453,59 @@
 	}
     }
 
+    /**************************************************************************
+     * Update services 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 fillServicesCache()
 	throws MobyException {
 	try {
 	    fireEvent (AUTHORITIES_START);
 	    String byAuthorityAsXML = getServiceNamesByAuthorityAsXML();
-	    store (servicesCache, LIST_FILE, byAuthorityAsXML);
+ 	    remove (servicesCache, LIST_FILE);
 	    Map authorities = createServiceNamesByAuthorityFromXML (byAuthorityAsXML);
+
+	    // list of current files in this cache
+	    HashSet currentFiles = new HashSet();
+	    File[] list = servicesCache.listFiles();
+	    if (list == null)
+		throw new MobyException (MSG_CACHE_NOT_DIR (servicesCache));
+	    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
 	    fireEvent (AUTHORITIES_COUNT, new Integer (authorities.size()));
 	    for (Iterator it = authorities.entrySet().iterator(); it.hasNext(); ) {
 		Map.Entry entry = (Map.Entry)it.next();
 		String authority = (String)entry.getKey();
-		fireEvent (AUTHORITY_LOADING, authority);
-		String xml = getServicesAsXML (new MobyService (MobyService.DUMMY_NAME, authority), null, true, true);
-		store (servicesCache, authority, xml);
-		fireEvent (AUTHORITY_LOADED, authority);
-		if (stopS) {
-		    removeFromCache (CACHE_PART_SERVICES);
-		    return false;
+		if ( ! currentFiles.contains (authority)) {
+		    // missing file: fetch it from a registry
+		    fireEvent (AUTHORITY_LOADING, authority);
+		    String xml = getServicesAsXML (new MobyService (MobyService.DUMMY_NAME, authority), null, true, true);
+		    store (servicesCache, authority, xml);
+		    fireEvent (AUTHORITY_LOADED, authority);
+		    if (stopS) {
+			return false;
+		    }
 		}
+		currentFiles.remove (authority);
 	    }
+
+	    // remove files that are not any more needed
+	    for (Iterator it = currentFiles.iterator(); it.hasNext(); )
+		remove (servicesCache, (String)it.next());
+
+	    // finally, put there the new LIST_FILE
+	    store (servicesCache, LIST_FILE, byAuthorityAsXML);
 	    return true;
+
 	} catch (Exception e) {
 	    throw new MobyException (formatException (e));
 	} finally {
@@ -565,35 +575,10 @@
 	}
     }
 
-//     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;
-// 	}
-//     }
-
-    //
+    /**************************************************************************
+     * Update namespaces from a moby registry - this easier than with
+     * other entities: just get a new LIST_FILE.
+     *************************************************************************/
     protected boolean fillNamespacesCache()
 	throws MobyException {
 	try {
@@ -901,4 +886,100 @@
 	}
     }
 
+    /**************************************************************************
+     * Return as many properties describing the given part of a cache
+     * as possible. The key used for returned properties are publicly
+     * available from this class but other may be returned as well. <p>
+     *
+     * @return properties describing a cache
+     * @param id is a part of cache to be described, or null if the
+     * whole cache should be described (this may return different kind
+     * of properties than for individual cache parts)
+     **************************************************************************/
+    public Properties getCacheInfo (String id) {
+	Properties result = new Properties();
+	result.put (CACHE_PROP_REGISTRY_URL, getRegistryEndpoint());
+	result.put (CACHE_PROP_COUNT, new Integer (0));
+	if (cacheDir == null) return result;
+	String realName = null;
+	try {
+	    File thisPart = null;
+	    if (CACHE_PART_SERVICES.equals (id)) {
+		thisPart = servicesCache;
+		realName = "Cache for Services (authorities)";
+	    } else if (CACHE_PART_DATATYPES.equals (id)) {
+		thisPart = dataTypesCache;
+		realName = "Cache for Data Types";
+	    } else if (CACHE_PART_SERVICETYPES.equals (id)) {
+		thisPart = serviceTypesCache;
+		realName = "Cache for Service Types";
+	    } else if (CACHE_PART_NAMESPACES.equals (id)) {
+		thisPart = namespacesCache;
+		realName = "Cache for Namespaces";
+	    }
+	    if (thisPart == null) return result;
+	    result.put (CACHE_PROP_NAME, realName);
+
+	    File[] list = thisPart.listFiles();
+	    if (list == null) return result;
+	    result.put (CACHE_PROP_LOCATION, thisPart.getAbsolutePath());
+	    int realCount = 0;
+	    long realSize = 0;
+	    long ageOfYoungest = -1;
+	    long ageOfOldest = Long.MAX_VALUE;
+	    for (int i = 0; i < list.length; i++) {
+		if (! ignored (list[i])) {
+		    realCount++;
+		    realSize += list[i].length();
+		    long age = list[i].lastModified();
+		    ageOfYoungest = Math.max (ageOfYoungest, age);
+		    ageOfOldest = Math.min (ageOfOldest, age);
+		}
+	    }
+	    result.put (CACHE_PROP_COUNT, new Integer (realCount));
+	    result.put (CACHE_PROP_SIZE, new Long (realSize));
+	    if (ageOfYoungest > 0)
+		result.put (CACHE_PROP_YOUNGEST, new Long (ageOfYoungest));
+	    if (ageOfOldest < Long.MAX_VALUE)
+		result.put (CACHE_PROP_OLDEST, new Long (ageOfOldest));
+
+	} catch (Exception e) {
+	    log.error ("Getting cache info failed: " + e.toString());
+	}
+	return result;
+    }
+
+    public String getCacheInfoFormatted (String id) {
+	Properties props = getCacheInfo (id);
+	StringBuffer buf = new StringBuffer();
+	buf.append (props.getProperty (CACHE_PROP_NAME) + "\n");
+	add (buf, "Biomoby registry", props.get (CACHE_PROP_REGISTRY_URL));
+	add (buf, "Number of entities", props.get (CACHE_PROP_COUNT));
+	Object value = props.get (CACHE_PROP_OLDEST);
+	if (value != null) {
+	    long age = ((Long)value).longValue();
+	    add (buf, "Oldest entry created", new Date (age));
+	    add (buf, "Oldest entry has age", Utils.ms2Human (new Date().getTime() - age));
+	}
+	value = props.get (CACHE_PROP_YOUNGEST);
+	if (value != null) {
+	    long age = ((Long)value).longValue();
+	    add (buf, "Youngest entry created", new Date (age));
+	    add (buf, "Youngest entry has age", Utils.ms2Human (new Date().getTime() - age));
+	}
+	add (buf, "Size (in bytes)", props.get (CACHE_PROP_SIZE));
+	add (buf, "Location", props.get (CACHE_PROP_LOCATION));
+	return new String (buf);
+    }
+
+    private void add (StringBuffer buf, String name, Object value) {
+	if (value != null) {
+	    buf.append ("\t");
+	    buf.append (name);
+	    buf.append ("\t");
+	    buf.append (value.toString());
+	    buf.append ("\n");
+	}
+    }
+
 }




More information about the MOBY-guts mailing list