Issue #924 fix some concurrency issues for GridResource

Former-commit-id: 78491d6318 [formerly e18782687c] [formerly de742a4b8c [formerly 312361f3bacf9ed935ee7cd69be90192af34b3fd]]
Former-commit-id: de742a4b8c
Former-commit-id: 3e9abe8682
This commit is contained in:
Ben Steffensmeier 2012-07-25 17:32:51 -05:00
parent a307242aea
commit bcea00c92b

View file

@ -23,7 +23,6 @@ import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@ -196,6 +195,14 @@ public class GridResource extends
private StyleRule styleRule;
/**
* The great protector of all things related to dataTimes, do not modify or
* iterate over pdosToParse, dataTimes, or tileSet unless you sync on this
* or else...
*
*/
protected Object timeLock = new Object();
/**
* Extends the MemoryBasedTileSet class so that we can have direct access to
* the loadedData
@ -575,6 +582,7 @@ public class GridResource extends
GribRecord[] records = resourceData.getRecords();
GribRecord emptyRecord = new GribRecord();
synchronized (timeLock) {
for (int i = 0; i < records.length; i++) {
if (emptyRecord.equals(records[i])) {
// Don't add empty records
@ -582,6 +590,7 @@ public class GridResource extends
}
pdosToParse.add(records[i]);
}
}
if (resourceData.getNameGenerator() == null) {
resourceData.setNameGenerator(new GridNameGenerator());
@ -650,6 +659,7 @@ public class GridResource extends
mbts = createTileSet(record, lvlSet, levelConverter);
mbts.setMapDescriptor(descriptor);
synchronized (timeLock) {
Set<DataTime> dateSet = tileSet.keySet();
dataTimes.clear();
Iterator<DataTime> dateIterator = dateSet.iterator();
@ -658,6 +668,7 @@ public class GridResource extends
}
Collections.sort(dataTimes);
}
levels = new SingleLevel[lvlSet.size()];
Iterator<SingleLevel> lvlIterator = lvlSet.iterator();
@ -675,12 +686,14 @@ public class GridResource extends
Set<SingleLevel> lvlSet, UnitConverter levelConverter)
throws VizException {
DataTime dataTime = record.getDataTime();
Map<Float, GridMemoryBasedTileSet> tilemap = tileSet.get(dataTime);
Map<Float, GridMemoryBasedTileSet> tilemap = null;
synchronized (timeLock) {
tilemap = tileSet.get(dataTime);
if (tilemap == null) {
tilemap = new HashMap<Float, GridMemoryBasedTileSet>();
tileSet.put(dataTime, tilemap);
}
}
float convertedLevel = (float) levelConverter.convert(record
.getModelInfo().getLevelOneValue());
@ -766,28 +779,28 @@ public class GridResource extends
*/
@Override
protected void disposeInternal() {
synchronized (timeLock) {
for (Map.Entry<DataTime, Map<Float, GridMemoryBasedTileSet>> set : tileSet
.entrySet()) {
for (Map.Entry<Float, GridMemoryBasedTileSet> tile : set.getValue()
.entrySet()) {
for (Map.Entry<Float, GridMemoryBasedTileSet> tile : set
.getValue().entrySet()) {
tile.getValue().dispose();
}
}
}
}
@Override
protected void initInternal(IGraphicsTarget target) throws VizException {
synchronized (timeLock) {
this.target = target;
synchronized (pdosToParse) {
if (pdosToParse.size() > 0) {
for (PluginDataObject pdo : pdosToParse) {
createTile(pdo);
}
pdosToParse.clear();
}
}
boolean combineResources = combineOperation != CombineOperation.NONE;
@ -800,7 +813,8 @@ public class GridResource extends
baseTile.init(target);
if (combineResources && !baseTile.isCombined()) {
Entry<Float, GridMemoryBasedTileSet> tileSetRef = null;
// Need to find the reference in order to update the object when
// Need to find the reference in order to update the object
// when
// it comes back as a new combined object
for (Map.Entry<DataTime, Map<Float, GridMemoryBasedTileSet>> set : tileSet
.entrySet()) {
@ -825,7 +839,8 @@ public class GridResource extends
baseTileEntry.setValue(combinedResourceData);
}
}
DataTime[] primaryDataTimes = descriptor.getTimeMatchingMap().get(this);
DataTime[] primaryDataTimes = descriptor.getTimeMatchingMap().get(
this);
for (int i = 0; i < primaryDataTimes.length; i++) {
Map<Float, GridMemoryBasedTileSet> map = tileSet
.get(primaryDataTimes[i]);
@ -861,20 +876,11 @@ public class GridResource extends
.entrySet()) {
newDataTimes.add(entry.getKey());
}
Collections.sort(newDataTimes, dataTime);
Collections.sort(newDataTimes);
dataTimes = newDataTimes;
}
}
/**
* DataTime Comparator
*/
private static Comparator<DataTime> dataTime = new Comparator<DataTime>() {
@Override
public int compare(DataTime arg0, DataTime arg1) {
return arg0.compareTo(arg1);
}
};
/**
* Combine the given tiles sets
@ -1217,14 +1223,16 @@ public class GridResource extends
@Override
public void setDescriptor(MapDescriptor descriptor) {
this.descriptor = descriptor;
synchronized (timeLock) {
for (Map.Entry<DataTime, Map<Float, GridMemoryBasedTileSet>> set : tileSet
.entrySet()) {
for (Map.Entry<Float, GridMemoryBasedTileSet> tile : set.getValue()
.entrySet()) {
for (Map.Entry<Float, GridMemoryBasedTileSet> tile : set
.getValue().entrySet()) {
tile.getValue().setMapDescriptor(this.descriptor);
}
}
}
}
@Override
public void project(CoordinateReferenceSystem mapData) throws VizException {
@ -1238,6 +1246,7 @@ public class GridResource extends
}
}
if (reproject) {
synchronized (timeLock) {
// If we are reprojecting to screen space, clear all tiles
for (Map.Entry<DataTime, Map<Float, GridMemoryBasedTileSet>> set : tileSet
.entrySet()) {
@ -1255,6 +1264,7 @@ public class GridResource extends
}
pdosToParse.clear();
}
}
} else {
for (GridMemoryBasedTileSet tile : baseTiles.values()) {
tile.reproject();
@ -1453,6 +1463,7 @@ public class GridResource extends
@Override
public void remove(DataTime dataTime) {
synchronized (timeLock) {
Map<Float, GridMemoryBasedTileSet> ts = tileSet.remove(dataTime);
if (ts == null) {
return;
@ -1469,18 +1480,19 @@ public class GridResource extends
}
Collections.sort(dataTimes);
}
}
@Override
public void resourceChanged(ChangeType type, Object object) {
if (type.equals(ChangeType.DATA_UPDATE)) {
PluginDataObject[] pdos = (PluginDataObject[]) object;
synchronized (timeLock) {
for (PluginDataObject pdo : pdos) {
if (pdo != null) {
if (CombineOperation.DIFFERENCE.equals(combineOperation)
if (CombineOperation.DIFFERENCE
.equals(combineOperation)
&& !(pdo instanceof CombinedGribRecord)) {
// Do nothing, timematcher will take care of it.
} else {
@ -1493,6 +1505,7 @@ public class GridResource extends
}
}
}
}
issueRefresh();
}