Skip to content

Commit f6e3025

Browse files
authored
Merge pull request #843 from lonvia/code-cleanup
Code cleanup
2 parents b74daf3 + 13451c8 commit f6e3025

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+274
-326
lines changed

app/es_embedded/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
plugins {
22
id 'com.github.johnrengelman.shadow' version '8.1.1'
33
id 'application'
4+
id 'jacoco'
5+
id 'org.sonarqube' version '5.1.0.4882'
46
}
57

68
apply from: rootProject.file('buildSrc/shared.gradle')

app/es_embedded/src/main/java/de/komoot/photon/Server.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package de.komoot.photon;
22

3-
import de.komoot.photon.DatabaseProperties;
4-
import de.komoot.photon.Importer;
5-
import de.komoot.photon.Updater;
63
import de.komoot.photon.searcher.StructuredSearchHandler;
74
import de.komoot.photon.searcher.ReverseHandler;
85
import de.komoot.photon.searcher.SearchHandler;
@@ -65,7 +62,7 @@ public Server(String mainDirectory) {
6562
try {
6663
setupDirectories(new File(mainDirectory).toURI().toURL());
6764
} catch (Exception e) {
68-
throw new RuntimeException("Can't create directories: " + mainDirectory, e);
65+
throw new UsageException("Can't create directories: " + mainDirectory + ": " + e.getMessage());
6966
}
7067
}
7168

@@ -107,7 +104,7 @@ public Server start(String clusterName, String[] transportAddresses) {
107104
esClient = esNode.client();
108105

109106
} catch (NodeValidationException e) {
110-
throw new RuntimeException("Error while starting elasticsearch server", e);
107+
throw new UsageException("Error while starting elasticsearch server: " + e.getMessage());
111108
}
112109

113110
}
@@ -133,7 +130,7 @@ public void shutdown() {
133130

134131
esClient.close();
135132
} catch (IOException e) {
136-
throw new RuntimeException("Error during elasticsearch server shutdown", e);
133+
LOGGER.info("Error during elasticsearch server shutdown", e);
137134
}
138135
}
139136

@@ -260,13 +257,13 @@ public void loadFromDatabase(DatabaseProperties dbProperties) {
260257
Map<String, String> properties = (Map<String, String>) response.getSource().get(BASE_FIELD);
261258

262259
if (properties == null) {
263-
throw new RuntimeException("Found database properties but no '" + BASE_FIELD +"' field. Database corrupt?");
260+
throw new UsageException("Found database properties but no '" + BASE_FIELD +"' field. Database corrupt?");
264261
}
265262

266263
String version = properties.getOrDefault(FIELD_VERSION, "");
267264
if (!DatabaseProperties.DATABASE_VERSION.equals(version)) {
268265
LOGGER.error("Database has incompatible version '{}'. Expected: {}", version, DatabaseProperties.DATABASE_VERSION);
269-
throw new RuntimeException("Incompatible database.");
266+
throw new UsageException("Incompatible database.");
270267
}
271268

272269
String langString = properties.get(FIELD_LANGUAGES);

app/es_embedded/src/main/java/de/komoot/photon/elasticsearch/Importer.java

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package de.komoot.photon.elasticsearch;
22

33
import de.komoot.photon.PhotonDoc;
4-
import de.komoot.photon.Utils;
54
import org.elasticsearch.action.bulk.BulkRequestBuilder;
65
import org.elasticsearch.action.bulk.BulkResponse;
76
import org.elasticsearch.client.Client;

app/es_embedded/src/main/java/de/komoot/photon/elasticsearch/IndexSettings.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package de.komoot.photon.elasticsearch;
22

33
import de.komoot.photon.Utils;
4+
import de.komoot.photon.UsageException;
45
import org.elasticsearch.client.Client;
56
import org.elasticsearch.common.xcontent.XContentType;
67
import org.json.JSONArray;
@@ -96,7 +97,7 @@ public IndexSettings setClassificationTerms(JSONArray terms) {
9697
for (int j = 0; j < jsonTerms.length(); j++) {
9798
String term = jsonTerms.getString(j).toLowerCase().trim();
9899
if (term.indexOf(' ') >= 0) {
99-
throw new RuntimeException("Syntax error in synonym file: only single word classification terms allowed.");
100+
throw new UsageException("Syntax error in synonym file: only single word classification terms allowed.");
100101
}
101102

102103
if (term.length() > 1) {
@@ -122,7 +123,7 @@ private void insertSynonymFilter(String filterName, JSONArray synonyms) {
122123
// Create a filter for the synonyms.
123124
JSONObject filters = (JSONObject) settings.optQuery("/analysis/filter");
124125
if (filters == null) {
125-
throw new RuntimeException("Analyser update: cannot find filter definition");
126+
throw new UsageException("Analyser update: cannot find filter definition");
126127
}
127128
filters.put(filterName, new JSONObject().put("type", "synonym").put("synonyms", synonyms));
128129

@@ -177,7 +178,7 @@ private void insertJsonArrayAfter(String jsonPointer, String field, String posit
177178
JSONObject parent = (JSONObject) settings.optQuery(jsonPointer);
178179
JSONArray array = parent == null ? null : parent.optJSONArray(field);
179180
if (array == null) {
180-
throw new RuntimeException("Analyser update: cannot find JSON array at" + jsonPointer);
181+
throw new UsageException("Analyser update: cannot find JSON array at" + jsonPointer);
181182
}
182183

183184
// We can't just insert items, so build a new array instead.
@@ -192,7 +193,7 @@ private void insertJsonArrayAfter(String jsonPointer, String field, String posit
192193
}
193194

194195
if (!done) {
195-
throw new RuntimeException("Analyser update: cannot find position string " + positionString);
196+
throw new UsageException("Analyser update: cannot find position string " + positionString);
196197
}
197198

198199
parent.put(field, newArray);

app/es_embedded/src/main/java/de/komoot/photon/elasticsearch/Updater.java

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package de.komoot.photon.elasticsearch;
22

33
import de.komoot.photon.PhotonDoc;
4-
import de.komoot.photon.Utils;
54
import org.elasticsearch.action.bulk.BulkRequestBuilder;
65
import org.elasticsearch.action.bulk.BulkResponse;
76
import org.elasticsearch.client.Client;

app/es_embedded/src/test/java/de/komoot/photon/ESBaseTester.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class ESBaseTester {
2121
protected Path dataDirectory;
2222

2323
public static final String TEST_CLUSTER_NAME = "photon-test";
24-
protected static GeometryFactory FACTORY = new GeometryFactory(new PrecisionModel(), 4326);
24+
protected static final GeometryFactory FACTORY = new GeometryFactory(new PrecisionModel(), 4326);
2525

2626
private ElasticTestServer server;
2727

@@ -52,8 +52,8 @@ public void setUpES() throws IOException {
5252
*
5353
* @throws IOException
5454
*/
55-
public void setUpES(Path test_directory, String... languages) throws IOException {
56-
server = new ElasticTestServer(test_directory.toString());
55+
public void setUpES(Path testDirectory, String... languages) throws IOException {
56+
server = new ElasticTestServer(testDirectory.toString());
5757
server.start(TEST_CLUSTER_NAME, new String[]{});
5858
server.recreateIndex(languages, new Date(), false);
5959
refresh();

app/es_embedded/src/test/java/de/komoot/photon/elasticsearch/UpdaterTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ void addNameToDoc() throws IOException {
3737
PhotonResult response = getById(1234);
3838
assertNotNull(response);
3939

40-
Map<String, String> out_names = response.getMap("name");
41-
assertEquals("Foo", out_names.get("default"));
42-
assertEquals("Enfoo", out_names.get("en"));
40+
Map<String, String> outNames = response.getMap("name");
41+
assertEquals("Foo", outNames.get("default"));
42+
assertEquals("Enfoo", outNames.get("en"));
4343
}
4444

4545
@Test
@@ -64,9 +64,9 @@ void removeNameFromDoc() throws IOException {
6464
PhotonResult response = getById(1234);
6565
assertNotNull(response);
6666

67-
Map<String, String> out_names = response.getMap("name");
68-
assertFalse(out_names.containsKey("default"));
69-
assertEquals("Enfoo", out_names.get("en"));
67+
Map<String, String> outNames = response.getMap("name");
68+
assertFalse(outNames.containsKey("default"));
69+
assertEquals("Enfoo", outNames.get("en"));
7070
}
7171

7272
@Test

app/opensearch/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
plugins {
22
id 'com.github.johnrengelman.shadow' version '8.1.1'
33
id 'application'
4+
id 'jacoco'
5+
id 'org.sonarqube' version '5.1.0.4882'
46
}
57

68
apply from: rootProject.file('buildSrc/shared.gradle')

app/opensearch/src/main/java/de/komoot/photon/Server.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class Server {
2323

2424
protected OpenSearchClient client;
2525
private OpenSearchRunner runner = null;
26-
final protected String dataDirectory;
26+
protected final String dataDirectory;
2727

2828
public Server(String mainDirectory) {
2929
dataDirectory = new File(mainDirectory, "photon_data").getAbsolutePath();
@@ -145,13 +145,13 @@ public void loadFromDatabase(DatabaseProperties dbProperties) throws IOException
145145
DBPropertyEntry.class);
146146

147147
if (!dbEntry.found()) {
148-
throw new RuntimeException("Cannot access property record. Database too old?");
148+
throw new UsageException("Cannot access property record. Database too old?");
149149
}
150150

151151
if (!DatabaseProperties.DATABASE_VERSION.equals(dbEntry.source().databaseVersion)) {
152152
LOGGER.error("Database has incompatible version '{}'. Expected: {}",
153153
dbEntry.source().databaseVersion, DatabaseProperties.DATABASE_VERSION);
154-
throw new RuntimeException("Incompatible database.");
154+
throw new UsageException("Incompatible database.");
155155
}
156156

157157
dbProperties.setLanguages(dbEntry.source().languages);

app/opensearch/src/main/java/de/komoot/photon/opensearch/IndexMapping.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.opensearch.client.opensearch.OpenSearchClient;
44
import org.opensearch.client.opensearch._types.mapping.DynamicMapping;
55
import org.opensearch.client.opensearch.indices.PutMappingRequest;
6-
import de.komoot.photon.Constants;
76

87
import java.io.IOException;
98
import java.util.ArrayList;
@@ -28,7 +27,7 @@ public void putMapping(OpenSearchClient client, String indexName) throws IOExcep
2827
}
2928

3029
public IndexMapping addLanguages(String[] languages) {
31-
List<String> name_collectors = new ArrayList<>();
30+
List<String> nameCollectors = new ArrayList<>();
3231
for (var lang: languages) {
3332
mappings.properties("collector." + lang,
3433
b -> b.text(p -> p.index(true)
@@ -55,12 +54,12 @@ public IndexMapping addLanguages(String[] languages) {
5554
.copyTo("collector." + lang, "collector.base")));
5655

5756
//add language-specific collector to default for name
58-
name_collectors.add("name." + lang);
57+
nameCollectors.add("name." + lang);
5958
}
6059

61-
name_collectors.add("collector.default");
62-
name_collectors.add("collector.base");
63-
mappings.properties("name.default", b -> b.text(p -> p.index(false).copyTo(name_collectors)));
60+
nameCollectors.add("collector.default");
61+
nameCollectors.add("collector.base");
62+
mappings.properties("name.default", b -> b.text(p -> p.index(false).copyTo(nameCollectors)));
6463

6564
return this;
6665
}

app/opensearch/src/main/java/de/komoot/photon/opensearch/IndexSettingBuilder.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.komoot.photon.opensearch;
22

3+
import de.komoot.photon.UsageException;
34
import de.komoot.photon.Utils;
45
import org.json.JSONArray;
56
import org.json.JSONObject;
@@ -18,7 +19,7 @@
1819
public class IndexSettingBuilder {
1920
private IndexSettingsAnalysis.Builder settings = new IndexSettingsAnalysis.Builder();
2021
private int numShards = 1;
21-
private Set<String> extra_filters = new HashSet<>();
22+
private Set<String> extraFilters = new HashSet<>();
2223

2324
public IndexSettingBuilder setShards(Integer numShards) {
2425
this.numShards = numShards == null ? 1 : numShards;
@@ -78,7 +79,7 @@ private void setClassificationTerms(JSONArray terms) {
7879
for (int j = 0; j < jsonTerms.length(); j++) {
7980
String term = jsonTerms.getString(j).toLowerCase().trim();
8081
if (term.indexOf(' ') >= 0) {
81-
throw new RuntimeException("Syntax error in synonym file: only single word classification terms allowed.");
82+
throw new UsageException("Syntax error in synonym file: only single word classification terms allowed.");
8283
}
8384

8485
if (term.length() > 1) {
@@ -108,7 +109,7 @@ private void insertSynonymFilter(String filterName, JSONArray synonyms) {
108109
return s;
109110
})));
110111

111-
extra_filters.add(filterName);
112+
extraFilters.add(filterName);
112113
}
113114
}
114115

@@ -141,7 +142,7 @@ private void addDefaultSettings() {
141142
d.charFilter("punctuationgreedy")
142143
.tokenizer("standard")
143144
.filter("lowercase");
144-
for (var filter : extra_filters) {
145+
for (var filter : extraFilters) {
145146
d.filter(filter);
146147
}
147148
d.filter("german_normalization", "asciifolding");
@@ -166,7 +167,7 @@ private void addDefaultSettings() {
166167
f -> f.custom(d -> {
167168
d.tokenizer("whitespace")
168169
.filter("lowercase");
169-
if (extra_filters.contains("classification_synonyms")) {
170+
if (extraFilters.contains("classification_synonyms")) {
170171
d.filter("classification_synonyms");
171172
}
172173

app/opensearch/src/main/java/de/komoot/photon/opensearch/OpenSearchResult.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class OpenSearchResult implements PhotonResult {
1313
private final Map<String, Object> infos;
1414
private final Map<String, Map<String, String>> localeTags;
1515

16-
OpenSearchResult(double extent[], double[] coordinates, Map<String, Object> infos, Map<String, Map<String, String>> localeTags) {
16+
OpenSearchResult(double[] extent, double[] coordinates, Map<String, Object> infos, Map<String, Map<String, String>> localeTags) {
1717
this.extent = extent;
1818
this.coordinates = coordinates;
1919
this.infos = infos;

app/opensearch/src/main/java/de/komoot/photon/opensearch/OpenSearchResultDeserializer.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package de.komoot.photon.opensearch;
22

3-
import com.fasterxml.jackson.core.JacksonException;
43
import com.fasterxml.jackson.core.JsonParser;
54
import com.fasterxml.jackson.databind.DeserializationContext;
65
import com.fasterxml.jackson.databind.JsonNode;
@@ -21,10 +20,9 @@ public OpenSearchResultDeserializer() {
2120
}
2221

2322
@Override
24-
public OpenSearchResult deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
23+
public OpenSearchResult deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
2524
final var node = (ObjectNode) p.getCodec().readTree(p);
2625

27-
final var importanceNode = node.get("importance");
2826
final double[] extent = extractExtent((ObjectNode) node.get("extent"));
2927
final double[] coordinates = extractCoordinate((ObjectNode) node.get("coordinate"));
3028

app/opensearch/src/main/java/de/komoot/photon/opensearch/OpenSearchReverseHandler.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import java.util.List;
1616

1717
public class OpenSearchReverseHandler implements ReverseHandler {
18-
final private OpenSearchClient client;
19-
final private String queryTimeout;
18+
private final OpenSearchClient client;
19+
private final String queryTimeout;
2020

2121
public OpenSearchReverseHandler(OpenSearchClient client, int queryTimeoutSec) {
2222
this.client = client;
@@ -52,7 +52,8 @@ private SearchResponse<OpenSearchResult> search(Query query, int limit, Point lo
5252
s.index(PhotonIndex.NAME)
5353
.searchType(SearchType.QueryThenFetch)
5454
.query(query)
55-
.size(limit);
55+
.size(limit)
56+
.timeout(queryTimeout);
5657

5758
if (location != null) {
5859
s.sort(sq -> sq

app/opensearch/src/main/java/de/komoot/photon/opensearch/OpenSearchSearchHandler.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
import java.util.List;
1414

1515
public class OpenSearchSearchHandler implements SearchHandler {
16-
final private OpenSearchClient client;
17-
final private String[] supportedLanguages;
18-
final private String queryTimeout;
16+
private final OpenSearchClient client;
17+
private final String[] supportedLanguages;
18+
private final String queryTimeout;
1919

2020
public OpenSearchSearchHandler(OpenSearchClient client, String[] supportedLanguages, int queryTimeout) {
2121
this.client = client;

app/opensearch/src/main/java/de/komoot/photon/opensearch/OpenSearchStructuredSearchHandler.java

-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package de.komoot.photon.opensearch;
22

3-
import de.komoot.photon.*;
43
import de.komoot.photon.searcher.StructuredSearchHandler;
54
import de.komoot.photon.searcher.PhotonResult;
65
import de.komoot.photon.query.StructuredPhotonRequest;
7-
import org.opensearch.client.json.jackson.JacksonJsonpGenerator;
86
import org.opensearch.client.opensearch.OpenSearchClient;
97
import org.opensearch.client.opensearch._types.SearchType;
108
import org.opensearch.client.opensearch._types.query_dsl.Query;
119
import org.opensearch.client.opensearch.core.SearchResponse;
12-
import org.opensearch.client.opensearch.core.explain.ExplanationDetail;
13-
import org.opensearch.common.util.iterable.Iterables;
1410

1511
import java.util.ArrayList;
1612
import java.util.List;

app/opensearch/src/main/java/de/komoot/photon/opensearch/OsmTagFilter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private void appendExcludeTerm(Query query) {
7878
excludeTagQueryBuilder.should(query);
7979
}
8080

81-
static private Query makeTermsQuery(String field, String term) {
81+
private static Query makeTermsQuery(String field, String term) {
8282
return TermsQuery.of(q -> q
8383
.field(field)
8484
.terms(t -> t.value(Collections.singletonList(FieldValue.of(term))))).toQuery();

app/opensearch/src/main/java/de/komoot/photon/opensearch/PhotonDocSerializer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import java.util.Set;
1616

1717
public class PhotonDocSerializer extends StdSerializer<PhotonDoc> {
18-
final private String[] languages;
19-
final private String[] extraTags;
18+
private final String[] languages;
19+
private final String[] extraTags;
2020

2121
public PhotonDocSerializer(String[] languages, String[] extraTags) {
2222
super(PhotonDoc.class);

0 commit comments

Comments
 (0)