Skip to content

Commit

Permalink
http request builder HEAD, closeable executors, HashMap.newHashMap co…
Browse files Browse the repository at this point in the history
…nstructor
  • Loading branch information
msbarry committed Oct 27, 2023
1 parent 6f0ac9f commit dccbf78
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public static List<Feature> decode(byte[] encoded) {

for (VectorTileProto.Tile.Feature feature : layer.getFeaturesList()) {
int tagsCount = feature.getTagsCount();
Map<String, Object> attrs = new HashMap<>(tagsCount / 2);
Map<String, Object> attrs = HashMap.newHashMap(tagsCount / 2);
int tagIdx = 0;
while (tagIdx < feature.getTagsCount()) {
String key = keys.get(feature.getTags(tagIdx++));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ private VectorTile.Feature decodeVectorTileFeature(SortableFeature entry) {
GeometryType geomType = decodeGeomType(geomTypeAndScale);
int scale = decodeScale(geomTypeAndScale);
int mapSize = unpacker.unpackMapHeader();
Map<String, Object> attrs = new HashMap<>(mapSize);
Map<String, Object> attrs = HashMap.newHashMap(mapSize);
for (int i = 0; i < mapSize; i++) {
String key = commonValueStrings.decode(unpacker.unpackInt());
Value v = unpacker.unpackValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void readFeatures(Consumer<SimpleFeature> next) throws Exception {
Geometry latLonGeom = (transform.isIdentity()) ? featureGeom : JTS.transform(featureGeom, transform);

FeatureColumns columns = feature.getColumns();
SimpleFeature geom = SimpleFeature.create(latLonGeom, new HashMap<>(columns.columnCount()),
SimpleFeature geom = SimpleFeature.create(latLonGeom, HashMap.newHashMap(columns.columnCount()),
sourceName, featureName, ++id);

for (int i = 0; i < columns.columnCount(); ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void readFeatures(Consumer<SimpleFeature> next) throws Exception {

// create the feature and pass to next stage
Geometry latLonGeometry = GeoUtils.WKB_READER.read(geometry);
SimpleFeature readerGeometry = SimpleFeature.create(latLonGeometry, new HashMap<>(column.length - 1),
SimpleFeature readerGeometry = SimpleFeature.create(latLonGeometry, HashMap.newHashMap(column.length - 1),
sourceName, table, ++id);
for (int c = 0; c < column.length; c++) {
if (c != geometryColumn) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void readFeatures(Consumer<SimpleFeature> next) throws TransformException
latLonGeometry = JTS.transform(source, transformToLatLon);
}
if (latLonGeometry != null) {
SimpleFeature geom = SimpleFeature.create(latLonGeometry, new HashMap<>(attributeNames.length),
SimpleFeature geom = SimpleFeature.create(latLonGeometry, HashMap.newHashMap(attributeNames.length),
sourceName, layer, ++id);
for (int i = 1; i < attributeNames.length; i++) {
geom.setTag(attributeNames[i], feature.getAttribute(i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public Iterator<OsmElement> iterator() {

private Map<String, Object> buildTags(int num, IntUnaryOperator key, IntUnaryOperator value) {
if (num > 0) {
Map<String, Object> tags = new HashMap<>(num);
Map<String, Object> tags = HashMap.newHashMap(num);
for (int i = 0; i < num; i++) {
String k = fieldDecoder.decodeString(key.applyAsInt(i));
String v = fieldDecoder.decodeString(value.applyAsInt(i));
Expand Down Expand Up @@ -366,7 +366,7 @@ public OsmElement.Node next() {

if (tags == null) {
// divide by 2 as key&value, multiply by 2 because of the better approximation
tags = new HashMap<>(Math.max(3, 2 * (nodes.getKeysValsCount() / 2) / nodes.getKeysValsCount()));
tags = HashMap.newHashMap(Math.max(3, 2 * (nodes.getKeysValsCount() / 2) / nodes.getKeysValsCount()));
}

tags.put(fieldDecoder.decodeString(keyIndex), fieldDecoder.decodeString(valueIndex));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public Map<String, Long> dataErrors() {

@Override
public void close() {
executor.shutdown();
executor.close();
push();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private CompletableFuture<ResourceMetadata> httpHeadFollowRedirects(String url,

CompletableFuture<ResourceMetadata> httpHead(String url) {
return client
.sendAsync(newHttpRequest(url).method("HEAD", HttpRequest.BodyPublishers.noBody()).build(),
.sendAsync(newHttpRequest(url).HEAD().build(),
responseInfo -> {
int status = responseInfo.statusCode();
Optional<String> location = Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,33 @@ public Worker(String prefix, Stats stats, int threads, RunnableThatThrows task)
public Worker(String prefix, Stats stats, int threads, IntConsumerThatThrows task) {
this.prefix = prefix;
stats.gauge(prefix + "_threads", threads);
var es = Executors.newFixedThreadPool(threads, new NamedThreadFactory(prefix));
String parentStage = LogUtil.getStage();
List<CompletableFuture<?>> results = new ArrayList<>();
for (int i = 0; i < threads; i++) {
final int threadId = i;
results.add(CompletableFuture.runAsync(() -> {
LogUtil.setStage(parentStage, prefix);
String id = Thread.currentThread().getName();
LOGGER.trace("Starting worker");
try {
long start = System.nanoTime();
task.accept(threadId);
stats.timers().finishedWorker(prefix, Duration.ofNanos(System.nanoTime() - start));
} catch (Throwable e) {
System.err.println("Worker " + id + " died");
// when one worker dies it may close resources causing others to die as well, so only log the first
if (firstWorkerDied.compareAndSet(false, true)) {
e.printStackTrace(); // NOSONAR
try (var es = Executors.newFixedThreadPool(threads, new NamedThreadFactory(prefix))) {
String parentStage = LogUtil.getStage();
List<CompletableFuture<?>> results = new ArrayList<>();
for (int i = 0; i < threads; i++) {
final int threadId = i;
results.add(CompletableFuture.runAsync(() -> {
LogUtil.setStage(parentStage, prefix);
String id = Thread.currentThread().getName();
LOGGER.trace("Starting worker");
try {
long start = System.nanoTime();
task.accept(threadId);
stats.timers().finishedWorker(prefix, Duration.ofNanos(System.nanoTime() - start));
} catch (Throwable e) {
System.err.println("Worker " + id + " died");
// when one worker dies it may close resources causing others to die as well, so only log the first
if (firstWorkerDied.compareAndSet(false, true)) {
e.printStackTrace(); // NOSONAR
}
throwFatalException(e);
} finally {
LOGGER.trace("Finished worker");
}
throwFatalException(e);
} finally {
LOGGER.trace("Finished worker");
}
}, es));
}, es));
}
done = joinFutures(results);
}
es.shutdown();
done = joinFutures(results);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class FeatureRendererTest {
private FeatureCollector collector(Geometry worldGeom) {
var latLonGeom = GeoUtils.worldToLatLonCoords(worldGeom);
return new FeatureCollector.Factory(config, stats)
.get(SimpleFeature.create(latLonGeom, new HashMap<>(0), null, null,
.get(SimpleFeature.create(latLonGeom, HashMap.newHashMap(0), null, null,
1));
}

Expand Down

0 comments on commit dccbf78

Please sign in to comment.