[Swift-commit] r2672 - trunk/src/org/griphyn/vdl/mapping

noreply at svn.ci.uchicago.edu noreply at svn.ci.uchicago.edu
Mon Mar 9 15:04:18 CDT 2009


Author: hategan
Date: 2009-03-09 15:04:18 -0500 (Mon, 09 Mar 2009)
New Revision: 2672

Modified:
   trunk/src/org/griphyn/vdl/mapping/AbstractDataNode.java
Log:
optimized some fairly inefficient code and reformatted a bit

Modified: trunk/src/org/griphyn/vdl/mapping/AbstractDataNode.java
===================================================================
--- trunk/src/org/griphyn/vdl/mapping/AbstractDataNode.java	2009-03-09 17:18:20 UTC (rev 2671)
+++ trunk/src/org/griphyn/vdl/mapping/AbstractDataNode.java	2009-03-09 20:04:18 UTC (rev 2672)
@@ -24,23 +24,28 @@
 
 	static final String DATASET_URI_PREFIX = "tag:benc at ci.uchicago.edu,2008:swift:dataset:";
 
-	public static final Logger logger = Logger.getLogger(AbstractDataNode.class);
-	
-	public static final MappingParam PARAM_PREFIX = new MappingParam("prefix", null);
+	public static final Logger logger = Logger
+			.getLogger(AbstractDataNode.class);
 
-	/** Datasets are identified within a run by this sequence number and the
-	    partial ID field.
-	    The initial value is chosen to aid human recognition of sequence
-	    numbers in the wild. There is no requirement that it start at this
-	    (or any other) particular value. Note that this introduces a
-	    maximum on the number of datasets which can be dealt with in any
-	    run to be about 2^62. */
+	public static final MappingParam PARAM_PREFIX = new MappingParam("prefix",
+			null);
+
+	/**
+	 * Datasets are identified within a run by this sequence number and the
+	 * partial ID field. The initial value is chosen to aid human recognition of
+	 * sequence numbers in the wild. There is no requirement that it start at
+	 * this (or any other) particular value. Note that this introduces a maximum
+	 * on the number of datasets which can be dealt with in any run to be about
+	 * 2^62.
+	 */
 	private static long datasetIDCounter = 720000000000l;
 
-	/** This is used to provide a (hopefully) globally unique identifier for
-	    each time the datasetIDCounter is reset (whenever this class is
-	    loaded, which will usually happen once per JVM). No meaning should be
-	    inferred from this value - it exists purely for making unique URIs. */
+	/**
+	 * This is used to provide a (hopefully) globally unique identifier for each
+	 * time the datasetIDCounter is reset (whenever this class is loaded, which
+	 * will usually happen once per JVM). No meaning should be inferred from
+	 * this value - it exists purely for making unique URIs.
+	 */
 	private static final String datasetIDPartialID = Loader.getUUID();
 
 	private Field field;
@@ -49,6 +54,7 @@
 	private boolean closed;
 	private List listeners;
 	final String identifierURI = makeIdentifierURIString();
+	private Path pathFromRoot;
 
 	protected AbstractDataNode(Field field) {
 		this.field = field;
@@ -89,8 +95,9 @@
 					return String.valueOf(n.intValue());
 				}
 				catch (ClassCastException e) {
-					throw new RuntimeException("Internal type error. Value is not a Number for "
-							+ getDisplayableName() + getPathFromRoot());
+					throw new RuntimeException(
+							"Internal type error. Value is not a Number for "
+									+ getDisplayableName() + getPathFromRoot());
 				}
 			}
 			else {
@@ -100,38 +107,44 @@
 		return getIdentifyingString();
 	}
 
-
 	public String getIdentifyingString() {
+		StringBuffer sb = new StringBuffer();
+		sb.append(this.getClass().getName());
 
-		String prefix = this.getClass().getName();
+		sb.append(" identifier ");
+		sb.append(this.getIdentifier());
 
-		prefix = prefix + " identifier "+this.getIdentifier(); 
+		sb.append(" type ");
+		sb.append(getType());
 
-		prefix = prefix + " type "+getType();
-
-		if(value == null) {
- 			prefix = prefix + " with no value at dataset=";
-		} else if (value instanceof Throwable) {
-			prefix = prefix + " containing throwable "+value.getClass();
-		} else {
- 			prefix = prefix + " value="+this.value.toString()+" dataset=";
+		if (value == null) {
+			sb.append(" with no value at dataset=");
 		}
+		else if (value instanceof Throwable) {
+			sb.append(" containing throwable ");
+			sb.append(value.getClass());
+		}
+		else {
+			sb.append(" value=");
+			sb.append(this.value.toString());
+			sb.append(" dataset=");
+		}
 
-		prefix = prefix + getDisplayableName();
+		sb.append(getDisplayableName());
 
 		if (!Path.EMPTY_PATH.equals(getPathFromRoot())) {
-			prefix = prefix + " path="+ getPathFromRoot().toString();
+			sb.append(" path=");
+			sb.append(getPathFromRoot().toString());
 		}
 
-		if(closed) {
-			prefix = prefix + " (closed)";
+		if (closed) {
+			sb.append(" (closed)");
 		}
 		else {
-			prefix = prefix + " (not closed)";
+			sb.append(" (not closed)");
 		}
 
-		return prefix;
-
+		return sb.toString();
 	}
 
 	protected String getDisplayableName() {
@@ -163,13 +176,15 @@
 		}
 	}
 
-	public Collection getFields(Path path) throws InvalidPathException, HandleOpenException {
+	public Collection getFields(Path path) throws InvalidPathException,
+			HandleOpenException {
 		List fields = new ArrayList();
 		getFields(fields, path);
 		return fields;
 	}
 
-	private void getFields(List fields, Path path) throws InvalidPathException, HandleOpenException {
+	private void getFields(List fields, Path path) throws InvalidPathException,
+			HandleOpenException {
 		if (path.isEmpty()) {
 			fields.add(this);
 		}
@@ -187,7 +202,8 @@
 			}
 			else {
 				try {
-					((AbstractDataNode) getField(path.getFirst())).getFields(fields, rest);
+					((AbstractDataNode) getField(path.getFirst())).getFields(
+							fields, rest);
 				}
 				catch (NoSuchFieldException e) {
 					throw new InvalidPathException(path, this);
@@ -199,7 +215,8 @@
 	public void set(DSHandle handle) {
 		// TODO check type
 		if (closed) {
-			throw new IllegalArgumentException(this.getDisplayableName() + " is already assigned");
+			throw new IllegalArgumentException(this.getDisplayableName()
+					+ " is already assigned");
 		}
 		if (getParent() == null) {
 			/*
@@ -220,7 +237,8 @@
 		}
 	}
 
-	protected synchronized DSHandle getField(String name) throws NoSuchFieldException {
+	protected synchronized DSHandle getField(String name)
+			throws NoSuchFieldException {
 		DSHandle handle = getHandle(name);
 		if (handle == null) {
 			if (closed) {
@@ -246,10 +264,11 @@
 		}
 	}
 
-	public DSHandle createDSHandle(String fieldName) throws NoSuchFieldException {
+	public DSHandle createDSHandle(String fieldName)
+			throws NoSuchFieldException {
 		if (closed) {
-			throw new RuntimeException("Cannot write to closed handle: " + this + " (" + fieldName
-					+ ")");
+			throw new RuntimeException("Cannot write to closed handle: " + this
+					+ " (" + fieldName + ")");
 		}
 
 		AbstractDataNode child;
@@ -264,15 +283,17 @@
 		synchronized (handles) {
 			Object o = handles.put(fieldName, child);
 			if (o != null) {
-				throw new RuntimeException("Trying to create a handle that already exists ("
-						+ fieldName + ") in " + this);
+				throw new RuntimeException(
+						"Trying to create a handle that already exists ("
+								+ fieldName + ") in " + this);
 			}
 		}
 		return child;
 	}
 
 	protected Field getChildField(String fieldName) throws NoSuchFieldException {
-		return Field.Factory.createField(fieldName, field.getType().getField(fieldName).getType());
+		return Field.Factory.createField(fieldName, field.getType().getField(
+				fieldName).getType());
 	}
 
 	protected void checkDataException() {
@@ -300,7 +321,8 @@
 	public Map getArrayValue() {
 		checkDataException();
 		if (!field.getType().isArray()) {
-			throw new RuntimeException("getArrayValue called on a struct: " + this);
+			throw new RuntimeException("getArrayValue called on a struct: "
+					+ this);
 		}
 		return handles;
 	}
@@ -312,7 +334,7 @@
 	public void setValue(Object value) {
 		if (this.closed) {
 			throw new IllegalArgumentException(this.getDisplayableName()
-					+ " is closed with a value of "+this.value);
+					+ " is closed with a value of " + this.value);
 		}
 		if (this.value != null) {
 			throw new IllegalArgumentException(this.getDisplayableName()
@@ -328,7 +350,8 @@
 		return list;
 	}
 
-	public void getFringePaths(List list, Path parentPath) throws HandleOpenException {
+	public void getFringePaths(List list, Path parentPath)
+			throws HandleOpenException {
 		checkMappingException();
 		if (getField().getType().getBaseType() != null) {
 			list.add(Path.EMPTY_PATH);
@@ -346,8 +369,10 @@
 							"Inconsistency between type declaration and handle for field '"
 									+ field.getName() + "'");
 				}
-				//[m] Hmm. Should there be a difference between field and mapper.getField()?
-				//Path fullPath = parentPath.addLast(mapper.getField().getName());
+				// [m] Hmm. Should there be a difference between field and
+				// mapper.getField()?
+				// Path fullPath =
+				// parentPath.addLast(mapper.getField().getName());
 				Path fullPath = parentPath.addLast(field.getName());
 				if (!mapper.field.getType().isPrimitive() && !mapper.isArray()) {
 					list.add(fullPath);
@@ -360,53 +385,70 @@
 	}
 
 	public synchronized void closeShallow() {
+		if (this.closed) {
+			return;
+		}
 		this.closed = true;
 		notifyListeners();
-		logger.info("closed "+this.getIdentifyingString());
-// so because its closed, we can dump the contents
+		logger.info("closed " + this.getIdentifyingString());
+		// so because its closed, we can dump the contents
 		try {
 			logContent();
-		} catch(Exception e) {
-			logger.warn("Exception whilst logging dataset content for "+this,e);
 		}
-// TODO record retrospective provenance information for this dataset here
-// we should do it at closing time because that's the point at which we
-// know the dataset has its values (all the way down the tree) assigned.
+		catch (Exception e) {
+			logger.warn("Exception whilst logging dataset content for " + this,
+					e);
+		}
+		// TODO record retrospective provenance information for this dataset
+		// here
+		// we should do it at closing time because that's the point at which we
+		// know the dataset has its values (all the way down the tree) assigned.
 
-// provenance-id for this dataset should have been assigned at creation time,
-// though, so that we can refer to this dataset elsewhere before it is
-// closed.
+		// provenance-id for this dataset should have been assigned at creation
+		// time,
+		// though, so that we can refer to this dataset elsewhere before it is
+		// closed.
 
-// is this method the only one called to set this.closed? or do subclasses
-// or other methods ever change it?
+		// is this method the only one called to set this.closed? or do
+		// subclasses
+		// or other methods ever change it?
 	}
 
 	public synchronized void logContent() {
-		if(this.getPathFromRoot() != null) {
-			logger.info("ROOTPATH dataset="+this.getIdentifier()+" path="+
-				this.getPathFromRoot());
-			if(this.getType().isPrimitive()) {
-				logger.info("VALUE dataset="+this.getIdentifier()+" VALUE="+this.toString());
+		String identifier = this.getIdentifier();
+		Path pathFromRoot = this.getPathFromRoot();
+		if (this.getPathFromRoot() != null) {
+			logger.info("ROOTPATH dataset=" + identifier + " path="
+					+ pathFromRoot);
+			if (this.getType().isPrimitive()) {
+				logger.info("VALUE dataset=" + identifier + " VALUE="
+						+ this.toString());
 			}
 
 			Mapper m;
 
 			try {
 				m = this.getMapper();
-			} catch(VDL2FutureException fe) {
+			}
+			catch (VDL2FutureException fe) {
 				m = null; // no mapping info if mapper isn't initialised yet
 			}
-			if(m != null) {
-// TODO proper type here
-// Not sure catching exception here is really the right thing to do here
-// anyway - should perhaps only be trying to map leafnodes? Mapping
-// non-leaf stuff is giving wierd paths anyway
+			if (m != null) {
+				// TODO proper type here
+				// Not sure catching exception here is really the right thing to
+				// do here
+				// anyway - should perhaps only be trying to map leafnodes?
+				// Mapping
+				// non-leaf stuff is giving wierd paths anyway
 				try {
-					Object path=m.map(this.getPathFromRoot());
-					logger.info("FILENAME dataset="+this.getIdentifier()+" filename="+path) ;
-				} catch(Exception e) {
-					logger.info("dataset "+this.getIdentifier()+" exception while mapping path from root",e);
+					Object path = m.map(pathFromRoot);
+					logger.info("FILENAME dataset=" + identifier + " filename="
+							+ path);
 				}
+				catch (Exception e) {
+					logger.info("dataset " + identifier
+							+ " exception while mapping path from root", e);
+				}
 			}
 		}
 
@@ -415,7 +457,8 @@
 			while (i.hasNext()) {
 				Map.Entry e = (Map.Entry) i.next();
 				AbstractDataNode node = (AbstractDataNode) e.getValue();
-				logger.info("CONTAINMENT parent="+this.getIdentifier()+" child="+node.getIdentifier());
+				logger.info("CONTAINMENT parent=" + identifier + " child="
+						+ node.getIdentifier());
 				node.logContent();
 			}
 		}
@@ -440,16 +483,20 @@
 		}
 	}
 
-	public Path getPathFromRoot() {
-		AbstractDataNode parent = (AbstractDataNode) this.getParent();
-		Path myPath;
-		if (parent != null) {
-			myPath = parent.getPathFromRoot();
-			return myPath.addLast(getField().getName(), parent.getField().getType().isArray());
+	public synchronized Path getPathFromRoot() {
+		if (pathFromRoot == null) {
+			AbstractDataNode parent = (AbstractDataNode) this.getParent();
+			Path myPath;
+			if (parent != null) {
+				myPath = parent.getPathFromRoot();
+				pathFromRoot = myPath.addLast(getField().getName(), parent
+						.getField().getType().isArray());
+			}
+			else {
+				pathFromRoot = Path.EMPTY_PATH;
+			}
 		}
-		else {
-			return Path.EMPTY_PATH;
-		}
+		return pathFromRoot;
 	}
 
 	public Mapper getMapper() {
@@ -462,7 +509,8 @@
 
 	public synchronized void addListener(DSHandleListener listener) {
 		if (logger.isInfoEnabled()) {
-			logger.info("Adding handle listener \"" + listener + "\" to \"" + getIdentifyingString() + "\"");
+			logger.info("Adding handle listener \"" + listener + "\" to \""
+					+ getIdentifyingString() + "\"");
 		}
 		if (listeners == null) {
 			listeners = new LinkedList();
@@ -480,7 +528,8 @@
 				DSHandleListener listener = (DSHandleListener) i.next();
 				i.remove();
 				if (logger.isInfoEnabled()) {
-					logger.info("Notifying listener \"" + listener + "\" about \"" + getIdentifyingString() + "\"");
+					logger.info("Notifying listener \"" + listener
+							+ "\" about \"" + getIdentifyingString() + "\"");
 				}
 				listener.handleClosed(this);
 			}
@@ -494,6 +543,6 @@
 
 	String makeIdentifierURIString() {
 		datasetIDCounter++;
-		return DATASET_URI_PREFIX + datasetIDPartialID + ":" + datasetIDCounter; 
+		return DATASET_URI_PREFIX + datasetIDPartialID + ":" + datasetIDCounter;
 	}
 }




More information about the Swift-commit mailing list