sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] branch geoapi-4.0 updated: Cache tiles at any resolution, not only at full resolution.
Date Fri, 30 Jul 2021 08:53:43 GMT
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


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new 4177632  Cache tiles at any resolution, not only at full resolution.
4177632 is described below

commit 417763275d256a62d52dcc016dc444c6acc91104
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Fri Jul 30 10:42:09 2021 +0200

    Cache tiles at any resolution, not only at full resolution.
---
 .../sis/internal/storage/TiledGridCoverage.java    | 25 ++++++---
 .../sis/internal/storage/TiledGridResource.java    | 59 +++++++++++++++++-----
 2 files changed, 62 insertions(+), 22 deletions(-)

diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/TiledGridCoverage.java
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/TiledGridCoverage.java
index 296e6a9..41169bd 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/TiledGridCoverage.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/TiledGridCoverage.java
@@ -142,8 +142,9 @@ public abstract class TiledGridCoverage extends GridCoverage {
      *
      * @see TiledGridResource#rasters
      * @see AOI#getCachedTile()
+     * @see #createCacheKey(int)
      */
-    private final Map<Integer, WritableRaster> rasters;
+    private final Map<TiledGridResource.CacheKey, WritableRaster> rasters;
 
     /**
      * The sample model for all rasters. The size of this sample model is the values of
@@ -473,7 +474,7 @@ public abstract class TiledGridCoverage extends GridCoverage {
          * @see Snapshot#cache(WritableRaster)
          */
         public WritableRaster getCachedTile() {
-            WritableRaster tile = rasters.get(indexInTileVector);
+            WritableRaster tile = rasters.get(createCacheKey(indexInTileVector));
             if (tile != null) {
                 // Found a tile. Make sure that the raster starts at the expected coordinates.
                 final int x = getTileOrigin(0);
@@ -514,7 +515,7 @@ public abstract class TiledGridCoverage extends GridCoverage {
              *
              *   - `indexInResultArray` is the index in the `rasters` array to be returned.
              *   - `indexInTileVector`  is the corresponding index in the `tileOffsets` vector.
-             *   - `tmcInSubset[]`      contains the Tile Matrix Coordinates (TMC) relative
to this `DataSubset`.
+             *   - `tmcInSubset[]`      contains the Tile Matrix Coordinates (TMC) relative
to this `TiledGridCoverage`.
              *   - `tileOffsetAOI[]`    contains the pixel coordinates relative to the user-specified
AOI.
              *
              * We do not check for integer overflow in this method because if an overflow
is possible,
@@ -548,8 +549,8 @@ public abstract class TiledGridCoverage extends GridCoverage {
         private final TiledGridCoverage coverage;
 
         /**
-         * Tile Matrix Coordinates (TMC) relative to the enclosing {@link DataSubset}.
-         * Tile (0,0) is the tile in the upper-left corner of this {@link DataSubset},
+         * Tile Matrix Coordinates (TMC) relative to the enclosing {@link TiledGridCoverage}.
+         * Tile (0,0) is the tile in the upper-left corner of this {@link TiledGridCoverage},
          * not necessarily the tile in the upper-left corner of the image in the resource.
          */
         private final int[] tmcInSubset;
@@ -560,8 +561,8 @@ public abstract class TiledGridCoverage extends GridCoverage {
         public final int indexInResultArray;
 
         /**
-         * Index of this tile in the {@link DataCube} resource. This is the index of the
tile in the
-         * {@link ImageFileDirectory#tileOffsets} and {@link ImageFileDirectory#tileByteCounts}
vectors.
+         * Index of this tile in the {@link TiledGridResource}. In a GeoTIFF image, this
is
+         * the index of the tile in the {@code tileOffsets} and {@code tileByteCounts} vectors.
          * This index is also used as key in the {@link TiledGridCoverage#rasters} map.
          */
         public final int indexInTileVector;
@@ -655,12 +656,20 @@ public abstract class TiledGridCoverage extends GridCoverage {
          * @see AOI#getCachedTile()
          */
         public WritableRaster cache(final WritableRaster raster) {
-            final WritableRaster existing = coverage.rasters.putIfAbsent(indexInTileVector,
raster);
+            final WritableRaster existing = coverage.rasters.putIfAbsent(
+                    coverage.createCacheKey(indexInTileVector), raster);
             return (existing != null) ? existing : raster;
         }
     }
 
     /**
+     * Creates the key to use for caching the tile at given index.
+     */
+    private TiledGridResource.CacheKey createCacheKey(final int indexInTileVector) {
+        return new TiledGridResource.CacheKey(indexInTileVector, selectedBands, subsampling,
subsamplingOffsets);
+    }
+
+    /**
      * Returns all tiles in the given area of interest. Tile indices are relative to this
{@code TiledGridCoverage}:
      * (0,0) is the tile in the upper-left corner of this {@code TiledGridCoverage} (not
necessarily the upper-left
      * corner of the image in the {@link TiledGridResource}).
diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/TiledGridResource.java
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/TiledGridResource.java
index 73a662e..e21c73e 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/TiledGridResource.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/TiledGridResource.java
@@ -45,15 +45,55 @@ import org.apache.sis.util.ArraysExt;
  */
 public abstract class TiledGridResource extends AbstractGridResource {
     /**
+     * A key in the {@link #rasters} cache of tiles.
+     */
+    static final class CacheKey {
+        /** Index in a row-major array of tiles. */ private final int   indexInTileVector;
+        /** Bands in strictly increasing order.  */ private final int[] selectedBands;
+        /** Subsampling factors at read time.    */ private final int[] subsampling;
+        /** Remainder of subsampling divisions.  */ private final int[] subsamplingOffsets;
+
+        /** Creates a key with given arrays hold be reference (no copy). */
+        CacheKey(final int indexInTileVector, final int[] selectedBands,
+                 final int[] subsampling, final int[] subsamplingOffsets)
+        {
+            this.indexInTileVector  = indexInTileVector;
+            this.selectedBands      = selectedBands;
+            this.subsampling        = subsampling;
+            this.subsamplingOffsets = subsamplingOffsets;
+        }
+
+        /** Returns a hash-code value for this key. */
+        @Override public int hashCode() {
+            return indexInTileVector
+                    +   73 * Arrays.hashCode(selectedBands)
+                    + 1063 * Arrays.hashCode(subsampling)
+                    + 7919 * Arrays.hashCode(subsamplingOffsets);
+        }
+
+        /** Compares this key with the given object for equality. */
+        @Override public boolean equals(final Object obj) {
+            if (obj instanceof CacheKey) {
+                final CacheKey other = (CacheKey) obj;
+                return indexInTileVector == other.indexInTileVector
+                        && Arrays.equals(selectedBands,      other.selectedBands)
+                        && Arrays.equals(subsampling,        other.subsampling)
+                        && Arrays.equals(subsamplingOffsets, other.subsamplingOffsets);
+            }
+            return false;
+        }
+    }
+
+    /**
      * All tiles loaded by any {@link TiledGridCoverage} created from this resource.
-     * Keys are tile indices in a row-major array of tiles.
+     * Keys contains tile indices in a row-major array of tiles.
      * For each value, the {@link WritableRaster#getMinX()} and {@code minY} values
      * can be anything, depending which {@link TiledGridResource} was first to load the tile.
      *
      * @see TiledGridCoverage#rasters
      * @see TiledGridCoverage.AOI#getCachedTile()
      */
-    private final WeakValueHashMap<Integer, WritableRaster> rasters;
+    private final WeakValueHashMap<CacheKey, WritableRaster> rasters;
 
     /**
      * Creates a new resource.
@@ -62,7 +102,7 @@ public abstract class TiledGridResource extends AbstractGridResource {
      */
     protected TiledGridResource(final StoreListeners parent) {
         super(parent);
-        rasters = new WeakValueHashMap<>(Integer.class);
+        rasters = new WeakValueHashMap<>(CacheKey.class);
     }
 
     /**
@@ -187,7 +227,7 @@ public abstract class TiledGridResource extends AbstractGridResource {
          * Cache to use for tiles loaded by the {@link TiledGridCoverage}.
          * It is a reference to {@link TiledGridResource#rasters} if shareable.
          */
-        final WeakValueHashMap<Integer, WritableRaster> cache;
+        final WeakValueHashMap<CacheKey, WritableRaster> cache;
 
         /**
          * Creates parameters for the given domain and range.
@@ -236,14 +276,6 @@ public abstract class TiledGridResource extends AbstractGridResource
{
                 readExtent         = target.getIntersection();
                 subsampling        = target.getSubsampling();
                 subsamplingOffsets = target.getSubsamplingOffsets();
-                if (sharedCache) {
-                    for (final int s : subsampling) {
-                        if (s != 1) {
-                            sharedCache = false;
-                            break;
-                        }
-                    }
-                }
             }
             /*
              * Get the bands selected by user in strictly increasing order of source band
index.
@@ -252,7 +284,6 @@ public abstract class TiledGridResource extends AbstractGridResource {
              */
             int[] selectedBands = null;
             if (!rangeIndices.isIdentity()) {
-                sharedCache = false;
                 bands = Arrays.asList(rangeIndices.select(bands));
                 selectedBands = new int[rangeIndices.getNumBands()];
                 for (int i=0; i<selectedBands.length; i++) {
@@ -274,7 +305,7 @@ public abstract class TiledGridResource extends AbstractGridResource {
              * All `TiledGridCoverage` instances can share the same cache if they read all
tiles fully.
              * If they read only sub-regions or apply subsampling, then they will need their
own cache.
              */
-            cache = sharedCache ? rasters : new WeakValueHashMap<>(Integer.class);
+            cache = sharedCache ? rasters : new WeakValueHashMap<>(CacheKey.class);
         }
 
         /**

Mime
View raw message