Skip to content

Commit

Permalink
Merge pull request #10 from n-lagomarsini/concurrentmap
Browse files Browse the repository at this point in the history
[JAIEXT-23] Creation of a new Concurrent TileCache with an inner MultiMap
  • Loading branch information
Simone Giannecchini committed May 30, 2014
2 parents ff48e95 + 5df00a5 commit 68f981a
Show file tree
Hide file tree
Showing 7 changed files with 724 additions and 101 deletions.
7 changes: 6 additions & 1 deletion concurrent-tile-cache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-coverage</artifactId>
<version>10-SNAPSHOT</version>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.concurrentlinkedhashmap</groupId>
Expand All @@ -27,6 +27,11 @@
<artifactId>high-scale-lib</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package it.geosolutions.concurrent;



import it.geosolutions.concurrent.ConcurrentTileCache.Actions;

import java.awt.image.DataBuffer;
Expand All @@ -15,117 +13,155 @@
import javax.media.jai.remote.SerializableRenderedImage;

/**
* This class is used by ConcurrentTileCache to create an object that includes all the
* information associated with a tile, and is put into the cache.
* This class is used by ConcurrentTileCache to create an object that includes all the information associated with a tile, and is put into the cache.
*/
public final class CachedTileImpl implements CachedTile {

final Raster tile; // the tile
final Raster tile; // the tile

final WeakReference owner; // the RenderedImage of this tile
final WeakReference owner; // the RenderedImage of this tile

final int tileX; // tile X index
final int tileX; // tile X index

final int tileY; // tile Y index
final int tileY; // tile Y index

final Object tileCacheMetric; // Metric for weighting tile computation cost
final Object tileCacheMetric; // Metric for weighting tile computation cost

private long timeStamp; // the last time this tile is accessed (if diagnosticEnable==false it is set only at the creation time)
private long timeStamp; // the last time this tile is accessed (if diagnosticEnable==false it is set only at the creation time)

final Object key; // the key used to hash this tile
final Object key; // the key used to hash this tile

private final Object imageKey; // Key of the associated image

final long tileSize; // the memory of this tile in bytes
final long tileSize; // the memory of this tile in bytes

private Actions action; // every action done by the tile cache
private Actions action; // every action done by the tile cache

/**
* Constructor that takes a tile cache metric
*
* @since 1.1
*/
public CachedTileImpl(RenderedImage owner, int tileX, int tileY, Raster tile,
Object tileCacheMetric) {
/**
* Constructor that takes a tile cache metric
*
* @since 1.1
*/
public CachedTileImpl(RenderedImage owner, int tileX, int tileY, Raster tile,
Object tileCacheMetric) {

this.owner = new WeakReference(owner);
this.tile = tile;
this.tileX = tileX;
this.tileY = tileY;
this.owner = new WeakReference(owner);
this.tile = tile;
this.tileX = tileX;
this.tileY = tileY;

this.tileCacheMetric = tileCacheMetric; // may be null
this.tileCacheMetric = tileCacheMetric; // may be null

key = hashKey(owner, tileX, tileY);
key = hashKey(owner, tileX, tileY);

imageKey = hashKey(owner);

DataBuffer db = tile.getDataBuffer();
tileSize = db.getDataTypeSize(db.getDataType()) / 8L * db.getSize()
* db.getNumBanks();
updateTileTimeStamp();
DataBuffer db = tile.getDataBuffer();
tileSize = db.getDataTypeSize(db.getDataType()) / 8L * db.getSize() * db.getNumBanks();
updateTileTimeStamp();

}
}

/**
* Returns the hash table "key" as a <code>Object</code> for this tile.
*/
public static Object hashKey(RenderedImage owner, int tileX, int tileY) {
long idx = tileY * (long) owner.getNumXTiles() + tileX;

BigInteger imageID = null;
if (owner instanceof PlanarImage)
imageID = (BigInteger) ((PlanarImage) owner).getImageID();
else if (owner instanceof SerializableRenderedImage)
imageID = (BigInteger) ((SerializableRenderedImage) owner).getImageID();

if (imageID != null) {
byte[] buf = imageID.toByteArray();
int length = buf.length;
byte[] buf1 = new byte[length + 8];
System.arraycopy(buf, 0, buf1, 0, length);
for (int i = 7, j = 0; i >= 0; i--, j += 8)
buf1[length++] = (byte) (idx >> j);
return new BigInteger(buf1);
/**
* Returns the key associated to the tile.
* @return
*/
public Object getKey() {
return key;
}

idx = idx & 0x00000000ffffffffL;
return Long.valueOf((((long) owner.hashCode() << 32) | idx));
}
/**
* Returns the key associate to the tile owner
* @return
*/
public Object getImageKey() {
return imageKey;
}

/**
* Returns the hash table "key" as a <code>Object</code> for this tile.
*/
public static Object hashKey(RenderedImage owner, int tileX, int tileY) {
long idx = tileY * (long) owner.getNumXTiles() + tileX;

BigInteger imageID = null;
if (owner instanceof PlanarImage)
imageID = (BigInteger) ((PlanarImage) owner).getImageID();
else if (owner instanceof SerializableRenderedImage)
imageID = (BigInteger) ((SerializableRenderedImage) owner).getImageID();

if (imageID != null) {
byte[] buf = imageID.toByteArray();
int length = buf.length;
byte[] buf1 = new byte[length + 8];
System.arraycopy(buf, 0, buf1, 0, length);
for (int i = 7, j = 0; i >= 0; i--, j += 8)
buf1[length++] = (byte) (idx >> j);
return new BigInteger(buf1);
}

idx = idx & 0x00000000ffffffffL;
return Long.valueOf((((long) owner.hashCode() << 32) | idx));
}

/** Returns the value of the cached tile. */
public Raster getTile() {
return tile;
}
/**
* Returns the hash table "key" as a <code>Object</code> for this image.
*/
public static Object hashKey(RenderedImage owner) {

/** Returns the owner of the cached tile. */
public RenderedImage getOwner() {
return (RenderedImage) owner.get();
}
BigInteger imageID = null;
if (owner instanceof PlanarImage)
imageID = (BigInteger) ((PlanarImage) owner).getImageID();
else if (owner instanceof SerializableRenderedImage)
imageID = (BigInteger) ((SerializableRenderedImage) owner).getImageID();

/** Returns the current time stamp */
public long getTileTimeStamp() {
return timeStamp;
}
if (imageID != null) {
byte[] buf = imageID.toByteArray();
return new BigInteger(buf);
}

/** Returns the tileCacheMetric object */
public Object getTileCacheMetric() {
return tileCacheMetric;
}
return owner.hashCode();
}

/** Returns the tile memory size */
public long getTileSize() {
return tileSize;
}
/** Returns the value of the cached tile. */
public Raster getTile() {
return tile;
}

/**
* Returns information about the status of the tile
*/
public int getAction() {
return action.valueAction();
}
/** Sets the status of the tile*/
public void setAction(Actions newAction) {
action = newAction;
}
/** Returns the owner of the cached tile. */
public RenderedImage getOwner() {
return (RenderedImage) owner.get();
}

/** Sets the timestamp to the new current value */
public void updateTileTimeStamp() {
timeStamp = System.currentTimeMillis();
}
/** Returns the current time stamp */
public long getTileTimeStamp() {
return timeStamp;
}

/** Returns the tileCacheMetric object */
public Object getTileCacheMetric() {
return tileCacheMetric;
}

/** Returns the tile memory size */
public long getTileSize() {
return tileSize;
}

/**
* Returns information about the status of the tile
*/
public int getAction() {
return action.valueAction();
}

/** Sets the status of the tile */
public void setAction(Actions newAction) {
action = newAction;
}

/** Sets the timestamp to the new current value */
public void updateTileTimeStamp() {
timeStamp = System.currentTimeMillis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Comparator;
import java.util.Iterator;
import java.util.Observable;
import java.util.Set;
import java.util.Vector;

import javax.media.jai.TileCache;
Expand Down Expand Up @@ -288,13 +289,17 @@ public void removeTiles(RenderedImage owner) {
}
}
} else {
synchronized (this) {
Iterable keys = (Iterable) cacheObject.asMap().keySet().iterator();
cacheObject.invalidateAll(keys);
}

int minTx = owner.getMinTileX();
int minTy = owner.getMinTileY();
int maxTx = minTx + owner.getNumXTiles();
int maxTy = minTy + owner.getNumYTiles();

for (int y = minTy; y < maxTy; y++) {
for (int x = minTx; x < maxTx; x++) {
remove(owner, x, y);
}
}
}

}

/**
Expand Down
Loading

0 comments on commit 68f981a

Please sign in to comment.