public abstract class CachedGrid extends Grid
CachedGrid
is a Grid
that uses temporary caching
to reduce the overall memory footprint of gridded data. The cache
uses a similar strategy to how modern operating systems cache
memory pages. A data stream is kept open, and a number of tiles
(rectangular sections of data) are brought into memory as they are
needed. The number of tiles in memory (the cache size) and the
size of each tile may be set by the user. Tiles that are no longer
needed are swapped out to make room for new tiles in the cache. In
order to avoid excessive data I/O, a least-recently-used rule is
used to determine which tile to remove from the cache when the
cache reaches its maximum capacity.
The standard DataVariable.setValue(noaa.coastwatch.util.DataLocation, double)
method is
supported by keeping a dirty flag for each tile. If the tile has been
written to, it is kept in the cache until, upon removal, it is written
to the data stream.
The only methods that subclasses need to implement are readTile(noaa.coastwatch.io.tile.TilingScheme.TilePosition)
to
retrieve tiles from the data source, writeTile(noaa.coastwatch.io.tile.TilingScheme.Tile)
to update the
data with any changes made to tiles, and getDataStream()
to retrieve
the object used to read and write data (only used to check for equality).
Modifier and Type | Field and Description |
---|---|
protected int |
accessMode
The access mode.
|
static int |
DEFAULT_MAX_TILES
Default cache tiles.
|
static int |
DEFAULT_TILE_DIMS
Default tile dimensions.
|
protected static int |
READ_ONLY
Read-only mode.
|
protected static int |
READ_WRITE
Read-write mode.
|
protected TilingScheme |
tiling
The tiling scheme.
|
data, dims, isUnsigned, lookup, unsignedType
Modifier | Constructor and Description |
---|---|
protected |
CachedGrid(Grid grid,
int accessMode)
Constructs a new cached grid with the specified properties.
|
Modifier and Type | Method and Description |
---|---|
void |
flush()
Writes any unwritten tiles to the data stream.
|
java.util.List<TilingScheme.TilePosition> |
getCoveringPositions(int[] start,
int[] count)
Gets a minimal list of tile positions that cover a subset of this
grid.
|
java.lang.Object |
getData()
Gets the variable data array.
|
java.lang.Object |
getData(int[] start,
int[] count)
Gets a subset of grid data values.
|
abstract java.lang.Object |
getDataStream()
Gets the cache data stream as an object.
|
int |
getMaxTiles()
Gets the current maximum number of tiles allowed in the cache.
|
static int[] |
getTileDims(int tileSize,
Grid grid)
Calculates the tile dimensions required to fill the specified
memory.
|
static int |
getTileSize(int[] tileDims,
Grid grid)
Calculates the tile memory size based on dimensions.
|
TilingScheme |
getTilingScheme()
Gets the tiling scheme for this grid if one is available.
|
double |
getValue(int index)
Reads a scaled data value.
|
double |
getValue(int row,
int col)
Reads a scaled data value with no navigation.
|
static void |
main(java.lang.String[] argv)
Tests this class.
|
protected abstract TilingScheme.Tile |
readTile(TilingScheme.TilePosition pos)
Reads the specified tile.
|
protected void |
resetCache()
Resets the tile cache to be empty.
|
void |
setCacheSize(int cacheSize)
Creates a new cache with the specified size.
|
void |
setData(java.lang.Object data)
Sets the variable data array.
|
void |
setData(java.lang.Object subset,
int[] start,
int[] count)
Sets a subset of grid data values.
|
void |
setMaxTiles(int tiles)
Creates a new cache with a maximum number of tiles.
|
void |
setOptimizedCacheSize(int cacheSize)
Creates a new cache with the specified size.
|
void |
setTileDims(int[] tileDims)
Creates a new cache where each tile has the specified dimensions.
|
void |
setTileSize(int tileSize)
Creates a new cache where each tile has the specified size.
|
void |
setValue(int index,
double val)
Writes a scaled data value.
|
void |
setValue(int row,
int col,
double val)
Writes a scaled data value with no navigation.
|
protected abstract void |
writeTile(TilingScheme.Tile tile)
Writes the specified tile.
|
arraycopy, checkSubset, getNavigation, getSubset, getValue, interpolate, navigate, setNavigation, setValue
convertUnits, dispose, format, format, format, getClassBits, getDataClass, getDecimals, getDimensions, getFormat, getLongName, getMissing, getName, getOptimalStride, getRank, getScaling, getStatistics, getStatistics, getStatistics, getStatistics, getUnits, getUnsigned, getValue, getValues, setAccessHint, setFormat, setLongName, setLookup, setMissing, setName, setUnits, setUnsigned, setValue, toString
clone, getMetadataMap
public static final int DEFAULT_MAX_TILES
public static final int DEFAULT_TILE_DIMS
protected static final int READ_ONLY
protected static final int READ_WRITE
protected TilingScheme tiling
protected int accessMode
protected CachedGrid(Grid grid, int accessMode)
grid
- the grid to use for attributes.accessMode
- the access mode, either READ_ONLY
or READ_WRITE
.public TilingScheme getTilingScheme()
Grid
getTilingScheme
in class Grid
public void setOptimizedCacheSize(int cacheSize)
cacheSize
- the cache memory size in bytes.setCacheSize(int)
public abstract java.lang.Object getDataStream()
protected void resetCache()
flush()
prior to resetting the cache. Any tiles whose contents have been
modified but not written will lose those modifications.
Therefore, users of read/write mode tiles should explicitly call
flush()
before this method.public void setCacheSize(int cacheSize)
cacheSize
- the cache memory size in bytes.setMaxTiles(int)
public int getMaxTiles()
public void setMaxTiles(int tiles)
tiles
- the maximum number of tiles.setCacheSize(int)
public void setTileSize(int tileSize)
tileSize
- the tile memory size in bytes.public void setTileDims(int[] tileDims)
tileDims
- the tile dimensions as [rows, columns].public static int getTileSize(int[] tileDims, Grid grid)
tileDims
- the tile dimensions as [rows, columns].grid
- the grid for calculation.public static int[] getTileDims(int tileSize, Grid grid)
tileSize
- the tile memory size in bytes.grid
- the grid for calculation.protected abstract TilingScheme.Tile readTile(TilingScheme.TilePosition pos) throws java.io.IOException
pos
- the tile position to read.java.io.IOException
- if an error occurred reading the tile data.protected abstract void writeTile(TilingScheme.Tile tile) throws java.io.IOException
tile
- the tile to write.java.io.IOException
- if an error occurred writing the tile data.public void flush() throws java.io.IOException
java.io.IOException
- if an error occurred writing the tile data.public void setValue(int index, double val)
DataVariable
setValue
in class DataVariable
index
- the index into the data array.val
- the data value as a double. If the data value is
Double.NaN
and the missing value is non-null, the
missing value is written to the array.DataVariable.setValue(DataLocation,double)
public void setValue(int row, int col, double val)
Grid
setValue
in class Grid
row
- the data location row.col
- the data location column.val
- the data value as a double. If the data value is
Double.NaN
and the missing value is non-null, the
missing value is written to the array.Grid.setValue(DataLocation,double)
public double getValue(int index)
DataVariable
getValue
in class DataVariable
index
- the index into the data array.double
. The
Double.NaN
value is used if the data value is
missing.DataVariable.getValue(DataLocation)
public double getValue(int row, int col)
Grid
getValue
in class Grid
row
- the data location row.col
- the data location column.double
. The
Double.NaN
value is used if the data value is
missing or data coordinate is not valid.Grid.getValue(DataLocation)
public void setData(java.lang.Object data)
DataVariable
setData
in class DataVariable
public java.lang.Object getData()
DataVariable
getData
in class DataVariable
public java.util.List<TilingScheme.TilePosition> getCoveringPositions(int[] start, int[] count)
start
- the subset starting [row, column].count
- the subset dimension [rows, columns].java.lang.IndexOutOfBoundsException
- if the subset falls outside the
grid dimensions.public void setData(java.lang.Object subset, int[] start, int[] count)
Grid
DataVariable.setData(Object)
, but sets only a subset of data
values in the raw, unscaled form.public java.lang.Object getData(int[] start, int[] count)
Grid
Grid.getData(int[], int[])
, but retrieves only a subset of data
values in the raw, unscaled form.getData
in class Grid
start
- the subset starting [row, column].count
- the subset dimension [rows, columns].DataVariable.getData()
public static void main(java.lang.String[] argv) throws java.lang.Exception
argv
- the array of command line parameters.java.lang.Exception