You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2023/01/18 17:46:04 UTC

[sis] 01/02: Writable `SingleImageStore` needs to extent `WritableStore` for being effective.

This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit 3f4e03d1e04eb7505fe7dacee336fa0ea44d0363
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Wed Jan 18 17:46:57 2023 +0100

    Writable `SingleImageStore` needs to extent `WritableStore` for being effective.
---
 .../internal/storage/image/SingleImageStore.java   | 37 ++-----------
 .../sis/internal/storage/image/WorldFileStore.java |  1 +
 .../storage/image/WorldFileStoreProvider.java      |  4 +-
 ...ageStore.java => WritableSingleImageStore.java} | 61 +++++++++-------------
 .../sis/internal/storage/image/package-info.java   |  2 +-
 5 files changed, 35 insertions(+), 70 deletions(-)

diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/SingleImageStore.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/SingleImageStore.java
index fbcc9a9061..117448ce0d 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/SingleImageStore.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/SingleImageStore.java
@@ -27,7 +27,6 @@ import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.storage.GridCoverageResource;
 import org.apache.sis.storage.RasterLoadingStrategy;
 import org.apache.sis.storage.UnsupportedQueryException;
-import org.apache.sis.storage.WritableGridCoverageResource;
 import org.apache.sis.storage.Query;
 
 
@@ -36,11 +35,13 @@ import org.apache.sis.storage.Query;
  * This class is used for image formats that are restricted to one image per file.
  * Examples: PNG and BMP image formats.
  *
+ * <p>See {@link WritableSingleImageStore} for the writable variant of this class.</p>
+ *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.2
+ * @version 1.4
  * @since   1.2
  */
-class SingleImageStore extends WorldFileStore implements GridCoverageResource {
+final class SingleImageStore extends WorldFileStore implements GridCoverageResource {
     /**
      * The singleton resource in this aggregate. Fetched when first needed.
      */
@@ -54,7 +55,7 @@ class SingleImageStore extends WorldFileStore implements GridCoverageResource {
      * @throws IOException if an error occurred while creating the image reader instance.
      */
     SingleImageStore(final FormatFinder format) throws DataStoreException, IOException {
-        super(format, false);
+        super(format, true);
     }
 
     /**
@@ -159,32 +160,4 @@ class SingleImageStore extends WorldFileStore implements GridCoverageResource {
     public final boolean setLoadingStrategy(RasterLoadingStrategy strategy) throws DataStoreException {
         return delegate().setLoadingStrategy(strategy);
     }
-
-    /**
-     * The writable variant of {@link MultiImageStore}.
-     */
-    static final class Writable extends SingleImageStore implements WritableGridCoverageResource {
-        /**
-         * Creates a new store from the given file, URL or stream.
-         *
-         * @param  format  information about the storage (URL, stream, <i>etc</i>) and the reader/writer to use.
-         * @throws DataStoreException if an error occurred while opening the stream.
-         * @throws IOException if an error occurred while creating the image reader instance.
-         */
-        Writable(final FormatFinder format) throws DataStoreException, IOException {
-            super(format);
-        }
-
-        /**
-         * Writes a new coverage in the data store for this resource. If a coverage already exists for this resource,
-         * then it will be overwritten only if the {@code TRUNCATE} or {@code UPDATE} option is specified.
-         *
-         * @param  coverage  new data to write in the data store for this resource.
-         * @param  options   configuration of the write operation.
-         */
-        @Override
-        public void write(GridCoverage coverage, Option... options) throws DataStoreException {
-            ((WritableResource) delegate()).write(coverage, options);
-        }
-    }
 }
diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WorldFileStore.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WorldFileStore.java
index 54dbb4cd87..3915013420 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WorldFileStore.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WorldFileStore.java
@@ -556,6 +556,7 @@ loop:   for (int convention=0;; convention++) {
      * Returns all images in this store. Note that fetching the size of the list is a potentially costly operation.
      *
      * @return list of images in this store.
+     * @throws DataStoreException if an error occurred while fetching components.
      */
     @SuppressWarnings("ReturnOfCollectionOrArrayField")
     public synchronized Collection<? extends GridCoverageResource> components() throws DataStoreException {
diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WorldFileStoreProvider.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WorldFileStoreProvider.java
index 99b1c9430d..3118c5e109 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WorldFileStoreProvider.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WorldFileStoreProvider.java
@@ -129,8 +129,8 @@ public final class WorldFileStoreProvider extends PRJDataStore.Provider {
                 }
             }
             if (format.isWritable) {
-                store = isSingleton ? new SingleImageStore.Writable(format)
-                                    : new  MultiImageStore.Writable(format);
+                store = isSingleton ? new WritableSingleImageStore(format)
+                                    : new MultiImageStore.Writable(format);
             } else {
                 store = isSingleton ? new SingleImageStore(format)
                                     : new  MultiImageStore(format);
diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/SingleImageStore.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WritableSingleImageStore.java
similarity index 75%
copy from storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/SingleImageStore.java
copy to storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WritableSingleImageStore.java
index fbcc9a9061..28cc1fe185 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/SingleImageStore.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/WritableSingleImageStore.java
@@ -23,6 +23,7 @@ import org.opengis.geometry.Envelope;
 import org.apache.sis.coverage.SampleDimension;
 import org.apache.sis.coverage.grid.GridCoverage;
 import org.apache.sis.coverage.grid.GridGeometry;
+import org.apache.sis.internal.storage.MemoryGridResource;
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.storage.GridCoverageResource;
 import org.apache.sis.storage.RasterLoadingStrategy;
@@ -32,19 +33,17 @@ import org.apache.sis.storage.Query;
 
 
 /**
- * A world file store which is expected to contain exactly one image.
- * This class is used for image formats that are restricted to one image per file.
- * Examples: PNG and BMP image formats.
+ * The writable variant of {@link SingleImageStore}.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.2
- * @since   1.2
+ * @version 1.4
+ * @since   1.4
  */
-class SingleImageStore extends WorldFileStore implements GridCoverageResource {
+final class WritableSingleImageStore extends WritableStore implements WritableGridCoverageResource {
     /**
      * The singleton resource in this aggregate. Fetched when first needed.
      */
-    private volatile WorldFileResource delegate;
+    private volatile WritableResource delegate;
 
     /**
      * Creates a new store from the given file, URL or stream.
@@ -53,8 +52,8 @@ class SingleImageStore extends WorldFileStore implements GridCoverageResource {
      * @throws DataStoreException if an error occurred while opening the stream.
      * @throws IOException if an error occurred while creating the image reader instance.
      */
-    SingleImageStore(final FormatFinder format) throws DataStoreException, IOException {
-        super(format, false);
+    WritableSingleImageStore(final FormatFinder format) throws DataStoreException, IOException {
+        super(format);
     }
 
     /**
@@ -74,10 +73,10 @@ class SingleImageStore extends WorldFileStore implements GridCoverageResource {
      *   <li>{@link #getMetadata()} because it is richer than {@link WorldFileResource#getMetadata()}.</li>
      * </ul>
      */
-    final WorldFileResource delegate() throws DataStoreException {
-        WorldFileResource r = delegate;
+    final WritableResource delegate() throws DataStoreException {
+        WritableResource r = delegate;
         if (r == null) {
-            delegate = r = ((Components) components()).get(MAIN_IMAGE);
+            delegate = r = (WritableResource) ((Components) components()).get(MAIN_IMAGE);
         }
         return r;
     }
@@ -161,30 +160,22 @@ class SingleImageStore extends WorldFileStore implements GridCoverageResource {
     }
 
     /**
-     * The writable variant of {@link MultiImageStore}.
+     * Writes a new coverage in the data store for this resource. If a coverage already exists for this resource,
+     * then it will be overwritten only if the {@code TRUNCATE} or {@code UPDATE} option is specified.
+     *
+     * @param  coverage  new data to write in the data store for this resource.
+     * @param  options   configuration of the write operation.
      */
-    static final class Writable extends SingleImageStore implements WritableGridCoverageResource {
-        /**
-         * Creates a new store from the given file, URL or stream.
-         *
-         * @param  format  information about the storage (URL, stream, <i>etc</i>) and the reader/writer to use.
-         * @throws DataStoreException if an error occurred while opening the stream.
-         * @throws IOException if an error occurred while creating the image reader instance.
-         */
-        Writable(final FormatFinder format) throws DataStoreException, IOException {
-            super(format);
-        }
-
-        /**
-         * Writes a new coverage in the data store for this resource. If a coverage already exists for this resource,
-         * then it will be overwritten only if the {@code TRUNCATE} or {@code UPDATE} option is specified.
-         *
-         * @param  coverage  new data to write in the data store for this resource.
-         * @param  options   configuration of the write operation.
-         */
-        @Override
-        public void write(GridCoverage coverage, Option... options) throws DataStoreException {
-            ((WritableResource) delegate()).write(coverage, options);
+    @Override
+    public void write(final GridCoverage coverage, final Option... options) throws DataStoreException {
+        try {
+            if (isMultiImages() == 0) {
+                add(new MemoryGridResource(listeners, coverage));
+            } else {
+                delegate().write(coverage, options);
+            }
+        } catch (IOException e) {
+            throw new DataStoreException(e);
         }
     }
 }
diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/package-info.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/package-info.java
index 1be3dc8e23..2a34b3bc34 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/package-info.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/image/package-info.java
@@ -40,7 +40,7 @@
  * then move in a public package with {@code imageio} package name.</p>
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.3
+ * @version 1.4
  *
  * @see <a href="https://en.wikipedia.org/wiki/World_file">World File format description on Wikipedia</a>
  *