From 53b07c02f17bf254dbfeb736a32fbb3a8c991ea6 Mon Sep 17 00:00:00 2001
From: brinn <brinn>
Date: Fri, 5 Aug 2011 09:11:03 +0000
Subject: [PATCH] refactor Hdf5Container for minimal interface (reader)

SVN: 22349
---
 .../cisd/common/hdf5/Hdf5Container.java       | 37 +++++++++--
 .../common/hdf5/IHDF5ContainerReader.java     | 62 +++++++++++++++++++
 ...ContainerBasedHierarchicalContentNode.java | 12 ++--
 .../hdf5/FileToHdf5DuplicationVerifier.java   |  7 +--
 .../cisd/common/hdf5/Hdf5ContainerTest.java   |  5 +-
 ...icalStructureDuplicatorFileToHdf5Test.java |  3 +-
 .../AbstractHdf5StorageProcessorTest.java     |  4 +-
 7 files changed, 109 insertions(+), 21 deletions(-)
 create mode 100644 common/source/java/ch/systemsx/cisd/common/hdf5/IHDF5ContainerReader.java

diff --git a/common/source/java/ch/systemsx/cisd/common/hdf5/Hdf5Container.java b/common/source/java/ch/systemsx/cisd/common/hdf5/Hdf5Container.java
index 4787e44af6e..9f14f263cb8 100644
--- a/common/source/java/ch/systemsx/cisd/common/hdf5/Hdf5Container.java
+++ b/common/source/java/ch/systemsx/cisd/common/hdf5/Hdf5Container.java
@@ -17,6 +17,7 @@
 package ch.systemsx.cisd.common.hdf5;
 
 import java.io.File;
+import java.util.List;
 
 import ch.systemsx.cisd.hdf5.HDF5FactoryProvider;
 import ch.systemsx.cisd.hdf5.IHDF5SimpleReader;
@@ -56,7 +57,7 @@ public class Hdf5Container
         /**
          * Run code using a reader. Implementations do <b>not</b> need to close the reader.
          */
-        public void runWithSimpleReader(IHDF5SimpleReader reader);
+        public void runWithSimpleReader(IHDF5ContainerReader reader);
     }
 
     /**
@@ -80,9 +81,37 @@ public class Hdf5Container
      * 
      * @return A new IHDF5SimpleReader
      */
-    public IHDF5SimpleReader createSimpleReader()
+    public IHDF5ContainerReader createSimpleReader()
     {
-        return HDF5FactoryProvider.get().openForReading(hdf5Container);
+        return new IHDF5ContainerReader()
+            {
+                final IHDF5SimpleReader innerReader = HDF5FactoryProvider.get().openForReading(hdf5Container);
+
+                public void close()
+                {
+                    innerReader.close();
+                }
+
+                public boolean exists(String objectPath)
+                {
+                    return innerReader.exists(objectPath);
+                }
+
+                public boolean isGroup(String objectPath)
+                {
+                    return innerReader.isGroup(objectPath);
+                }
+
+                public List<String> getGroupMembers(String groupPath)
+                {
+                    return innerReader.getGroupMembers(groupPath);
+                }
+
+                public byte[] readAsByteArray(String objectPath)
+                {
+                    return innerReader.readAsByteArray(objectPath);
+                }
+            };
     }
 
     /**
@@ -120,7 +149,7 @@ public class Hdf5Container
      */
     public void runReaderClient(IHdf5ReaderClient client)
     {
-        IHDF5SimpleReader reader = createSimpleReader();
+        IHDF5ContainerReader reader = createSimpleReader();
         try
         {
             client.runWithSimpleReader(reader);
diff --git a/common/source/java/ch/systemsx/cisd/common/hdf5/IHDF5ContainerReader.java b/common/source/java/ch/systemsx/cisd/common/hdf5/IHDF5ContainerReader.java
new file mode 100644
index 00000000000..61759ed72a8
--- /dev/null
+++ b/common/source/java/ch/systemsx/cisd/common/hdf5/IHDF5ContainerReader.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2011 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.common.hdf5;
+
+import java.util.List;
+
+/**
+ * A simple abstraction of the methods needed to read from an HDF5 container.
+ *
+ * @author Bernd Rinn
+ */
+public interface IHDF5ContainerReader
+{
+
+    /**
+     * Closes this object and the file referenced by this object. This object must not be used after
+     * being closed.
+     */
+    public void close();
+
+    /**
+     * Returns the members of <var>groupPath</var>. The order is <i>not</i> well defined.
+     * 
+     * @param groupPath The path of the group to get the members for.
+     * @throws IllegalArgumentException If <var>groupPath</var> is not a group.
+     */
+    public List<String> getGroupMembers(final String groupPath);
+
+    /**
+     * Returns <code>true</code>, if <var>objectPath</var> exists and <code>false</code> otherwise.
+     */
+    public boolean exists(final String objectPath);
+
+    /**
+     * Returns <code>true</code> if the <var>objectPath</var> exists and represents a group and
+     * <code>false</code> otherwise.
+     */
+    public boolean isGroup(final String objectPath);
+
+    /**
+     * Reads the data set <var>objectPath</var> as byte array (of rank 1).
+     * 
+     * @param objectPath The name (including path information) of the data set object in the file.
+     * @return The data read from the data set.
+     */
+    public byte[] readAsByteArray(final String objectPath);
+
+}
diff --git a/common/source/java/ch/systemsx/cisd/common/io/hierarchical_content/HDF5ContainerBasedHierarchicalContentNode.java b/common/source/java/ch/systemsx/cisd/common/io/hierarchical_content/HDF5ContainerBasedHierarchicalContentNode.java
index 92d2b7c09e2..b33c9b3e61a 100644
--- a/common/source/java/ch/systemsx/cisd/common/io/hierarchical_content/HDF5ContainerBasedHierarchicalContentNode.java
+++ b/common/source/java/ch/systemsx/cisd/common/io/hierarchical_content/HDF5ContainerBasedHierarchicalContentNode.java
@@ -24,11 +24,11 @@ import java.util.List;
 import ch.systemsx.cisd.base.io.IRandomAccessFile;
 import ch.systemsx.cisd.common.filesystem.FileUtilities;
 import ch.systemsx.cisd.common.hdf5.Hdf5Container;
+import ch.systemsx.cisd.common.hdf5.IHDF5ContainerReader;
 import ch.systemsx.cisd.common.io.HDF5DataSetBasedContent;
 import ch.systemsx.cisd.common.io.IContent;
 import ch.systemsx.cisd.common.io.hierarchical_content.api.IHierarchicalContent;
 import ch.systemsx.cisd.common.io.hierarchical_content.api.IHierarchicalContentNode;
-import ch.systemsx.cisd.hdf5.IHDF5SimpleReader;
 
 /**
  * {@link IHierarchicalContent} implementation for HDF5 container.
@@ -47,7 +47,7 @@ public class HDF5ContainerBasedHierarchicalContentNode extends
         this.hdf5Container = new Hdf5Container(hdf5ContainerFile);
     }
 
-    private IHDF5SimpleReader createReader()
+    private IHDF5ContainerReader createReader()
     {
         return hdf5Container.createSimpleReader();
     }
@@ -63,7 +63,7 @@ public class HDF5ContainerBasedHierarchicalContentNode extends
     @Override
     public List<IHierarchicalContentNode> doGetChildNodes()
     {
-        IHDF5SimpleReader reader = createReader();
+        IHDF5ContainerReader reader = createReader();
         try
         {
             List<String> childPaths = reader.getGroupMembers("/");
@@ -82,7 +82,7 @@ public class HDF5ContainerBasedHierarchicalContentNode extends
     /** @return child node with given path relative to this container */
     public IHierarchicalContentNode getChildNode(String childPath)
     {
-        IHDF5SimpleReader reader = createReader();
+        IHDF5ContainerReader reader = createReader();
         try
         {
             return getChildNode(reader, childPath);
@@ -92,7 +92,7 @@ public class HDF5ContainerBasedHierarchicalContentNode extends
         }
     }
 
-    private IHierarchicalContentNode getChildNode(IHDF5SimpleReader reader, String childPath)
+    private IHierarchicalContentNode getChildNode(IHDF5ContainerReader reader, String childPath)
     {
         String fileName = FileUtilities.getFileNameFromRelativePath(childPath);
         if (reader.isGroup(childPath))
@@ -204,7 +204,7 @@ public class HDF5ContainerBasedHierarchicalContentNode extends
         @Override
         protected List<IHierarchicalContentNode> doGetChildNodes()
         {
-            IHDF5SimpleReader reader = createReader();
+            IHDF5ContainerReader reader = createReader();
             try
             {
                 List<IHierarchicalContentNode> result = new ArrayList<IHierarchicalContentNode>();
diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/hdf5/FileToHdf5DuplicationVerifier.java b/common/sourceTest/java/ch/systemsx/cisd/common/hdf5/FileToHdf5DuplicationVerifier.java
index 49c836356a0..c115b7e20ae 100644
--- a/common/sourceTest/java/ch/systemsx/cisd/common/hdf5/FileToHdf5DuplicationVerifier.java
+++ b/common/sourceTest/java/ch/systemsx/cisd/common/hdf5/FileToHdf5DuplicationVerifier.java
@@ -23,7 +23,6 @@ import org.apache.commons.io.FileUtils;
 import org.testng.AssertJUnit;
 
 import ch.systemsx.cisd.common.hdf5.Hdf5Container.IHdf5ReaderClient;
-import ch.systemsx.cisd.hdf5.IHDF5SimpleReader;
 
 /**
  * Helper class that verifies that a file structure is matched by the HDF5 structure.
@@ -37,7 +36,7 @@ public class FileToHdf5DuplicationVerifier extends AssertJUnit
 
     private final Hdf5Container container;
 
-    private final IHDF5SimpleReader reader;
+    private final IHDF5ContainerReader reader;
 
     public static IHdf5ReaderClient createVerifierClient(File sourceFolderOrFile,
             Hdf5Container container)
@@ -57,7 +56,7 @@ public class FileToHdf5DuplicationVerifier extends AssertJUnit
             this.container = container;
         }
 
-        public void runWithSimpleReader(IHDF5SimpleReader reader)
+        public void runWithSimpleReader(IHDF5ContainerReader reader)
         {
             new FileToHdf5DuplicationVerifier(sourceFolderOrFile, container, reader)
                     .verifyDuplicate();
@@ -65,7 +64,7 @@ public class FileToHdf5DuplicationVerifier extends AssertJUnit
     }
 
     public FileToHdf5DuplicationVerifier(File sourceFolderOrFile, Hdf5Container container,
-            IHDF5SimpleReader reader)
+            IHDF5ContainerReader reader)
     {
         this.sourceFolderOrFile = sourceFolderOrFile;
         this.container = container;
diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/hdf5/Hdf5ContainerTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/hdf5/Hdf5ContainerTest.java
index 2fb24a3cdf3..e665da188ac 100644
--- a/common/sourceTest/java/ch/systemsx/cisd/common/hdf5/Hdf5ContainerTest.java
+++ b/common/sourceTest/java/ch/systemsx/cisd/common/hdf5/Hdf5ContainerTest.java
@@ -26,7 +26,6 @@ import org.testng.annotations.Test;
 import ch.systemsx.cisd.base.tests.AbstractFileSystemTestCase;
 import ch.systemsx.cisd.common.hdf5.Hdf5Container.IHdf5ReaderClient;
 import ch.systemsx.cisd.common.hdf5.Hdf5Container.IHdf5WriterClient;
-import ch.systemsx.cisd.hdf5.IHDF5SimpleReader;
 
 /**
  * @author Chandrasekhar Ramakrishnan
@@ -58,7 +57,7 @@ public class Hdf5ContainerTest extends AbstractFileSystemTestCase
 
         hdf5Content.runReaderClient(new IHdf5ReaderClient()
             {
-                public void runWithSimpleReader(IHDF5SimpleReader reader)
+                public void runWithSimpleReader(IHDF5ContainerReader reader)
                 {
                     byte[] readData = reader.readAsByteArray("/test-bytes");
                     assertEquals(byteArray, readData);
@@ -84,7 +83,7 @@ public class Hdf5ContainerTest extends AbstractFileSystemTestCase
 
         hdf5Content.runReaderClient(new IHdf5ReaderClient()
             {
-                public void runWithSimpleReader(IHDF5SimpleReader reader)
+                public void runWithSimpleReader(IHDF5ContainerReader reader)
                 {
                     byte[] readData = reader.readAsByteArray("/test-bytes");
                     assertEquals(byteArray, readData);
diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/hdf5/HierarchicalStructureDuplicatorFileToHdf5Test.java b/common/sourceTest/java/ch/systemsx/cisd/common/hdf5/HierarchicalStructureDuplicatorFileToHdf5Test.java
index 861e747bba5..eebcebfdf0c 100644
--- a/common/sourceTest/java/ch/systemsx/cisd/common/hdf5/HierarchicalStructureDuplicatorFileToHdf5Test.java
+++ b/common/sourceTest/java/ch/systemsx/cisd/common/hdf5/HierarchicalStructureDuplicatorFileToHdf5Test.java
@@ -24,7 +24,6 @@ import org.testng.annotations.Test;
 
 import ch.systemsx.cisd.base.tests.AbstractFileSystemTestCase;
 import ch.systemsx.cisd.common.hdf5.Hdf5Container.IHdf5ReaderClient;
-import ch.systemsx.cisd.hdf5.IHDF5SimpleReader;
 
 /**
  * @author Chandrasekhar Ramakrishnan
@@ -130,7 +129,7 @@ public class HierarchicalStructureDuplicatorFileToHdf5Test extends AbstractFileS
     {
         container.runReaderClient(new IHdf5ReaderClient()
             {
-                public void runWithSimpleReader(IHDF5SimpleReader reader)
+                public void runWithSimpleReader(IHDF5ContainerReader reader)
                 {
                     FileToHdf5DuplicationVerifier verifier =
                             new FileToHdf5DuplicationVerifier(sourceFolderOrFile, container, reader);
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/hdf5/AbstractHdf5StorageProcessorTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/hdf5/AbstractHdf5StorageProcessorTest.java
index 9ecfb554a10..2f4de9f9d2f 100644
--- a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/hdf5/AbstractHdf5StorageProcessorTest.java
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/hdf5/AbstractHdf5StorageProcessorTest.java
@@ -24,8 +24,8 @@ import ch.systemsx.cisd.common.filesystem.FileUtilities;
 import ch.systemsx.cisd.common.hdf5.FileToHdf5DuplicationVerifier;
 import ch.systemsx.cisd.common.hdf5.Hdf5Container;
 import ch.systemsx.cisd.common.hdf5.Hdf5Container.IHdf5ReaderClient;
+import ch.systemsx.cisd.common.hdf5.IHDF5ContainerReader;
 import ch.systemsx.cisd.etlserver.IStorageProcessorTransactional.IStorageProcessorTransaction;
-import ch.systemsx.cisd.hdf5.IHDF5SimpleReader;
 
 /**
  * Tests for {@link Hdf5StorageProcessor}.
@@ -72,7 +72,7 @@ abstract class AbstractHdf5StorageProcessorTest extends AbstractFileSystemTestCa
         container.runReaderClient(new IHdf5ReaderClient()
             {
 
-                public void runWithSimpleReader(IHDF5SimpleReader reader)
+                public void runWithSimpleReader(IHDF5ContainerReader reader)
                 {
                     FileToHdf5DuplicationVerifier verifier =
                             new FileToHdf5DuplicationVerifier(incomingDataSetDirectory, container,
-- 
GitLab