github-advanced-security[bot] commented on code in PR #902: URL: https://github.com/apache/incubator-baremaps/pull/902#discussion_r1829635251
########## baremaps-core/src/main/java/org/apache/baremaps/geocoder/DataRowMapper.java: ########## @@ -0,0 +1,253 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.baremaps.geocoder; + +import java.net.InetAddress; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import org.apache.baremaps.data.storage.DataColumn; +import org.apache.baremaps.data.storage.DataRow; +import org.apache.baremaps.data.storage.DataSchema; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.DoublePoint; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.FloatPoint; +import org.apache.lucene.document.IntPoint; +import org.apache.lucene.document.LatLonPoint; +import org.apache.lucene.document.LatLonShape; +import org.apache.lucene.document.LongPoint; +import org.apache.lucene.document.StoredField; +import org.apache.lucene.document.StringField; +import org.apache.lucene.document.TextField; +import org.locationtech.jts.geom.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DataRowMapper implements Function<DataRow, Document> { + + private static final Logger logger = LoggerFactory.getLogger(DataRowMapper.class); + + @Override + public Document apply(DataRow dataRow) { + Document doc = new Document(); + DataSchema schema = dataRow.schema(); + List<DataColumn> columns = schema.columns(); + for (int i = 0; i < columns.size(); i++) { + DataColumn column = columns.get(i); + String columnName = column.name(); + Object value = dataRow.get(i); + if (value == null) + continue; + DataColumn.Type type = column.type(); + + try { + switch (type) { + case BINARY: + doc.add(new StoredField(columnName, (byte[]) value)); + break; + case BYTE: + doc.add(new IntPoint(columnName, ((Byte) value).intValue())); + doc.add(new StoredField(columnName, ((Byte) value).intValue())); + break; + case BOOLEAN: + doc.add(new StringField(columnName, value.toString(), Field.Store.YES)); + break; + case SHORT: + doc.add(new IntPoint(columnName, ((Short) value).intValue())); + doc.add(new StoredField(columnName, ((Short) value).intValue())); + break; + case INTEGER: + doc.add(new IntPoint(columnName, (Integer) value)); + doc.add(new StoredField(columnName, (Integer) value)); + break; + case LONG: + doc.add(new LongPoint(columnName, (Long) value)); + doc.add(new StoredField(columnName, (Long) value)); + break; + case FLOAT: + doc.add(new FloatPoint(columnName, (Float) value)); + doc.add(new StoredField(columnName, (Float) value)); + break; + case DOUBLE: + doc.add(new DoublePoint(columnName, (Double) value)); + doc.add(new StoredField(columnName, (Double) value)); + break; + case STRING: + doc.add(new TextField(columnName, (String) value, Field.Store.YES)); + break; + case COORDINATE: + Coordinate coord = (Coordinate) value; + double lat = coord.getY(); + double lon = coord.getX(); + doc.add(new LatLonPoint(columnName, lat, lon)); + doc.add(new StoredField(columnName + "_lat", lat)); + doc.add(new StoredField(columnName + "_lon", lon)); + break; + case POINT: + Point point = (Point) value; + double pointLat = point.getY(); + double pointLon = point.getX(); + doc.add(new LatLonPoint(columnName, pointLat, pointLon)); + doc.add(new StoredField(columnName + "_lat", pointLat)); + doc.add(new StoredField(columnName + "_lon", pointLon)); + break; + case LINESTRING: + case POLYGON: + case MULTIPOINT: + case MULTILINESTRING: + case MULTIPOLYGON: + case GEOMETRYCOLLECTION: + case GEOMETRY: + Geometry geometry = (Geometry) value; + if (geometry != null) { Review Comment: ## Useless null check This check is useless. [geometry](1) cannot be null at this check, since it is guarded by [... == ...](2). [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/1614) ########## baremaps-core/src/test/java/org/apache/baremaps/geocoder/DataTableIndexTest.java: ########## @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.baremaps.geocoder; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; +import org.apache.baremaps.storage.geoparquet.GeoParquetDataTable; +import org.apache.baremaps.testing.TestFiles; +import org.apache.baremaps.utils.FileUtils; +import org.apache.lucene.document.Document; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.SearcherFactory; +import org.apache.lucene.search.SearcherManager; +import org.apache.lucene.store.FSDirectory; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + + +public class DataTableIndexTest { + + private static Path directory; + private static IndexSearcher searcher; + + @BeforeAll + public static void beforeAll() throws Exception { + // Init the geocoder service + directory = Files.createTempDirectory(Paths.get("."), "geocoder_"); + + // Create the geonames index + var dir = FSDirectory.open(directory); + var data = TestFiles.resolve("baremaps-testing/data/samples/example.parquet"); + var config = new IndexWriterConfig(GeocoderConstants.ANALYZER); + try (var indexWriter = new IndexWriter(dir, config)) { + indexWriter.deleteAll(); + var documents = new GeoParquetDataTable(data.toUri()) + .stream() + .map(new DataRowMapper()); + indexWriter.addDocuments((Iterable<Document>) documents::iterator); + } + + var searcherManager = new SearcherManager(dir, new SearcherFactory()); + searcher = searcherManager.acquire(); + } + + @AfterAll + public static void afterAll() throws IOException { + FileUtils.deleteRecursively(directory); + } + + @Test + void testQueryNoHits() throws Exception { + var geonamesQuery = new DataTableQueryBuilder() + .column("continent", 1.0f) + .query("test") + .build(); + var topDocs = searcher.search(geonamesQuery, 1); + assertEquals(0, topDocs.totalHits.value); + } + + @Test + void testQuery() throws Exception { + var geonamesQuery = new DataTableQueryBuilder() + .column("continent", 1.0f) + .query("oceania") + .build(); + var topDocs = searcher.search(geonamesQuery, 1); + var doc = searcher.doc(Arrays.stream(topDocs.scoreDocs).findFirst().get().doc); Review Comment: ## Deprecated method or constructor invocation Invoking [IndexSearcher.doc](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/1605) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
