Skip to content

Commit

Permalink
replace custom map method by Map::of (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy2003 authored May 29, 2024
1 parent 12f6d46 commit 93adcb7
Showing 1 changed file with 28 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,6 @@ public static void testCall(GraphDatabaseService db, String call, Consumer<Map<S
testCall(db, call, null, consumer);
}

public static Map<String, Object> map(Object... values) {
Map<String, Object> map = new LinkedHashMap<>();
for (int i = 0; i < values.length; i += 2) {
map.put(values[i].toString(), values[i + 1]);
}
return map;
}

public static void testCall(GraphDatabaseService db, String call, Map<String, Object> params,
Consumer<Map<String, Object>> consumer) {
testCall(db, call, params, consumer, true);
Expand Down Expand Up @@ -159,7 +151,7 @@ public static void testResult(GraphDatabaseService db, String call, Consumer<Res
public static void testResult(GraphDatabaseService db, String call, Map<String, Object> params,
Consumer<Result> resultConsumer) {
try (Transaction tx = db.beginTx()) {
Map<String, Object> p = (params == null) ? map() : params;
Map<String, Object> p = (params == null) ? Map.of() : params;
resultConsumer.accept(tx.execute(call, p));
tx.commit();
}
Expand Down Expand Up @@ -231,7 +223,7 @@ public void old_spatial_model_can_be_upgraded() {
public void add_node_to_non_existing_layer() {
execute("CALL spatial.addPointLayer('some_name')");
Node node = createNode("CREATE (n:Point {latitude:60.1,longitude:15.2}) RETURN n", "n");
testCallFails(db, "CALL spatial.addNode.byId('wrong_name',$nodeId)", map("nodeId", node.getElementId()),
testCallFails(db, "CALL spatial.addNode.byId('wrong_name',$nodeId)", Map.of("nodeId", node.getElementId()),
"No such layer 'wrong_name'");
}

Expand Down Expand Up @@ -380,7 +372,7 @@ public void create_node_and_convert_to_geometry() {
"CREATE (n:Node {geom:'POINT(4.0 5.0)'}) RETURN spatial.decodeGeometry('geom',n) AS geometry",
"geometry");
double distance = (Double) executeObject("RETURN point.distance($geom, point({y: 6.0, x: 4.0})) as distance",
map("geom", geom), "distance");
Map.of("geom", geom), "distance");
MatcherAssert.assertThat("Expected the cartesian distance of 1.0", distance, closeTo(1.0, 0.00001));
}

Expand All @@ -391,7 +383,7 @@ public void create_point_and_pass_as_param() {
"geometry");
double distance = (Double) executeObject(
"WITH spatial.asGeometry($geom) AS geometry RETURN point.distance(geometry, point({latitude: 5.1, longitude: 4.0})) as distance",
map("geom", geom), "distance");
Map.of("geom", geom), "distance");
MatcherAssert.assertThat("Expected the geographic distance of 11132km", distance, closeTo(11132.0, 1.0));
}

Expand Down Expand Up @@ -443,7 +435,7 @@ private Object executeObject(String call, String column) {
private Object executeObject(String call, Map<String, Object> params, String column) {
Object obj;
try (Transaction tx = db.beginTx()) {
Map<String, Object> p = (params == null) ? map() : params;
Map<String, Object> p = (params == null) ? Map.of() : params;
ResourceIterator<Object> values = tx.execute(call, p).columnAs(column);
obj = values.next();
values.close();
Expand Down Expand Up @@ -630,7 +622,7 @@ public void create_a_wkt_layer_using_know_format() {
public void list_layer_names() {
String wkt = "LINESTRING (15.2 60.1, 15.3 60.1)";
execute("CALL spatial.addWKTLayer('geom','wkt')");
execute("CALL spatial.addWKT('geom',$wkt)", map("wkt", wkt));
execute("CALL spatial.addWKT('geom',$wkt)", Map.of("wkt", wkt));

testCall(db, "CALL spatial.layers()", (r) -> {
assertEquals("geom", r.get("name"));
Expand All @@ -654,14 +646,14 @@ public void add_and_remove_multiple_layers() {
for (int i = 0; i < NUM_LAYERS; i++) {
String name = "wktLayer_" + i;
testCallCount(db, "CALL spatial.layers()", null, i);
execute("CALL spatial.addWKTLayer($layerName,'wkt')", map("layerName", name));
execute("CALL spatial.addWKT($layerName,$wkt)", map("wkt", wkt, "layerName", name));
execute("CALL spatial.addWKTLayer($layerName,'wkt')", Map.of("layerName", name));
execute("CALL spatial.addWKT($layerName,$wkt)", Map.of("wkt", wkt, "layerName", name));
testCallCount(db, "CALL spatial.layers()", null, i + 1);
}
for (int i = 0; i < NUM_LAYERS; i++) {
String name = "wktLayer_" + i;
testCallCount(db, "CALL spatial.layers()", null, NUM_LAYERS - i);
execute("CALL spatial.removeLayer($layerName)", map("layerName", name));
execute("CALL spatial.removeLayer($layerName)", Map.of("layerName", name));
testCallCount(db, "CALL spatial.layers()", null, NUM_LAYERS - i - 1);
}
testCallCount(db, "CALL spatial.layers()", null, 0);
Expand Down Expand Up @@ -752,7 +744,7 @@ public void list_layer_types() {
public void find_layer() {
String wkt = "LINESTRING (15.2 60.1, 15.3 60.1)";
execute("CALL spatial.addWKTLayer('geom','wkt')");
execute("CALL spatial.addWKT('geom',$wkt)", map("wkt", wkt));
execute("CALL spatial.addWKT('geom',$wkt)", Map.of("wkt", wkt));

testCall(db, "CALL spatial.layer('geom')",
(r) -> assertEquals("geom", (dump((Node) r.get("node"))).getProperty("layer")));
Expand Down Expand Up @@ -927,9 +919,9 @@ public void add_two_nodes_to_the_spatial_layer() {
});
try (Transaction tx = db.beginTx()) {
Node node = (Node) tx.execute("MATCH (node) WHERE elementId(node) = $nodeId RETURN node",
map("nodeId", node1)).next().get("node");
Map.of("nodeId", node1)).next().get("node");
Result removeResult = tx.execute("CALL spatial.removeNode('geom',$node) YIELD nodeId RETURN nodeId",
map("node", node));
Map.of("node", node));
assertEquals(node1, removeResult.next().get("nodeId"));
removeResult.close();
tx.commit();
Expand All @@ -941,7 +933,7 @@ public void add_two_nodes_to_the_spatial_layer() {
});
try (Transaction tx = db.beginTx()) {
Result removeResult = tx.execute("CALL spatial.removeNode.byId('geom',$nodeId) YIELD nodeId RETURN nodeId",
map("nodeId", node2));
Map.of("nodeId", node2));
assertEquals(node2, removeResult.next().get("nodeId"));
removeResult.close();
tx.commit();
Expand All @@ -960,7 +952,7 @@ public void add_many_nodes_to_the_simple_point_layer_using_addNodes() {
"WITH collect(n) as points\n" +
"CALL spatial.addNodes('simple_poi',points) YIELD count\n" +
"RETURN count";
testCountQuery("addNodes", query, count, "count", map("count", count));
testCountQuery("addNodes", query, count, "count", Map.of("count", count));
testRemoveNodes("simple_poi", count);
}

Expand All @@ -974,7 +966,7 @@ public void add_many_nodes_to_the_simple_point_layer_using_addNode() {
"WITH n\n" +
"CALL spatial.addNode('simple_poi',n) YIELD node\n" +
"RETURN count(node)";
testCountQuery("addNode", query, count, "count(node)", map("count", count));
testCountQuery("addNode", query, count, "count(node)", Map.of("count", count));
testRemoveNode("simple_poi", count);
}

Expand All @@ -989,7 +981,7 @@ public void add_many_nodes_to_the_native_point_layer_using_addNodes() {
"WITH collect(n) as points\n" +
"CALL spatial.addNodes('native_poi',points) YIELD count\n" +
"RETURN count";
testCountQuery("addNodes", query, count, "count", map("count", count));
testCountQuery("addNodes", query, count, "count", Map.of("count", count));
testRemoveNodes("native_poi", count);
}

Expand All @@ -1004,7 +996,7 @@ public void add_many_nodes_to_the_native_point_layer_using_addNode() {
"WITH n\n" +
"CALL spatial.addNode('native_poi',n) YIELD node\n" +
"RETURN count(node)";
testCountQuery("addNode", query, count, "count(node)", map("count", count));
testCountQuery("addNode", query, count, "count(node)", Map.of("count", count));
testRemoveNode("native_poi", count);
}

Expand All @@ -1019,7 +1011,7 @@ private void testRemoveNode(String layer, int count) {
"WITH n\n" +
"CALL spatial.removeNode('" + layer + "',n) YIELD nodeId\n" +
"RETURN count(nodeId)";
testCountQuery("removeNode", remove, count / 2, "count(nodeId)", map("count", count / 2));
testCountQuery("removeNode", remove, count / 2, "count(nodeId)", Map.of("count", count / 2));
// Check that only half remain
testCountQuery("withinDistance",
"CALL spatial.withinDistance('" + layer + "',{lon:15.0,lat:60.0},1000) YIELD node RETURN count(node)",
Expand All @@ -1037,7 +1029,7 @@ private void testRemoveNodes(String layer, int count) {
"WITH collect(n) as points\n" +
"CALL spatial.removeNodes('" + layer + "',points) YIELD count\n" +
"RETURN count";
testCountQuery("removeNodes", remove, count / 2, "count", map("count", count / 2));
testCountQuery("removeNodes", remove, count / 2, "count", Map.of("count", count / 2));
// Check that only half remain
testCountQuery("withinDistance",
"CALL spatial.withinDistance('" + layer + "',{lon:15.0,lat:60.0},1000) YIELD node RETURN count(node)",
Expand Down Expand Up @@ -1146,7 +1138,7 @@ public void import_osm_and_add_geometry() {

@Test
public void import_osm_and_polygons_withinDistance() {
Map<String, Object> params = map("osmFile", "withinDistance.osm", "busShelterID", 2938842290L);
Map<String, Object> params = Map.of("osmFile", "withinDistance.osm", "busShelterID", 2938842290L);
execute("CALL spatial.addLayer('geom','OSM','')");
testCountQuery("importOSMAndPolygonsWithinDistance", "CALL spatial.importOSMToLayer('geom',$osmFile)", 74,
"count", params);
Expand Down Expand Up @@ -1204,18 +1196,17 @@ public void import_osm_and_polygons_withinDistance() {
private void testCountQuery(String name, String query, long count, String column, Map<String, Object> params) {
// warmup
try (Transaction tx = db.beginTx()) {
Result results = tx.execute("EXPLAIN " + query, params == null ? map() : params);
Result results = tx.execute("EXPLAIN " + query, params == null ? Map.of() : params);
results.close();
tx.commit();
}
long start = System.currentTimeMillis();
testResult(db, query, params, res -> {
assertTrue(res.hasNext(), "Expected a single result");
long c = (Long) res.next().get(column);
long c = (Long) res.next().get(column);
assertFalse(res.hasNext(), "Expected a single result");
assertEquals(count, c, "Expected count of " + count + " nodes but got " + c);
}
);
});
System.out.println(name + " query took " + (System.currentTimeMillis() - start) + "ms - " + params);
}

Expand Down Expand Up @@ -1326,15 +1317,15 @@ public void add_a_WKT_geometry_to_a_layer() {
String lineString = "LINESTRING (15.2 60.1, 15.3 60.1)";

execute("CALL spatial.addWKTLayer('geom','wkt')");
testCall(db, "CALL spatial.addWKT('geom',$wkt)", map("wkt", lineString),
testCall(db, "CALL spatial.addWKT('geom',$wkt)", Map.of("wkt", lineString),
r -> assertEquals(lineString, dump(((Node) r.get("node"))).getProperty("wkt")));
}

@Test
public void find_geometries_close_to_a_point_wkt() {
String lineString = "LINESTRING (15.2 60.1, 15.3 60.1)";
execute("CALL spatial.addLayer('geom','WKT','wkt')");
execute("CALL spatial.addWKT('geom',$wkt)", map("wkt", lineString));
execute("CALL spatial.addWKT('geom',$wkt)", Map.of("wkt", lineString));
testCall(db, "CALL spatial.closest('geom',{lon:15.2, lat:60.1}, 1.0)",
r -> assertEquals(lineString, (dump((Node) r.get("node"))).getProperty("wkt")));
}
Expand All @@ -1343,23 +1334,23 @@ public void find_geometries_close_to_a_point_wkt() {
public void find_geometries_close_to_a_point_geohash() {
String lineString = "POINT (15.2 60.1)";
execute("CALL spatial.addLayer('geom','geohash','lon:lat')");
execute("CALL spatial.addWKT('geom',$wkt)", map("wkt", lineString));
execute("CALL spatial.addWKT('geom',$wkt)", Map.of("wkt", lineString));
testCallCount(db, "CALL spatial.closest('geom',{lon:15.2, lat:60.1}, 1.0)", null, 1);
}

@Test
public void find_geometries_close_to_a_point_zorder() {
String lineString = "POINT (15.2 60.1)";
execute("CALL spatial.addLayer('geom','zorder','lon:lat')");
execute("CALL spatial.addWKT('geom',$wkt)", map("wkt", lineString));
execute("CALL spatial.addWKT('geom',$wkt)", Map.of("wkt", lineString));
testCallCount(db, "CALL spatial.closest('geom',{lon:15.2, lat:60.1}, 1.0)", null, 1);
}

@Test
public void find_geometries_close_to_a_point_hilbert() {
String lineString = "POINT (15.2 60.1)";
execute("CALL spatial.addLayer('geom','hilbert','lon:lat')");
execute("CALL spatial.addWKT('geom',$wkt)", map("wkt", lineString));
execute("CALL spatial.addWKT('geom',$wkt)", Map.of("wkt", lineString));
testCallCount(db, "CALL spatial.closest('geom',{lon:15.2, lat:60.1}, 1.0)", null, 1);
}

Expand Down

0 comments on commit 93adcb7

Please sign in to comment.