Skip to content

Commit

Permalink
Fix issues detected by sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
bchapuis committed Nov 5, 2024
1 parent 3de244a commit 30e4e33
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,7 @@
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.apache.lucene.document.*;
import org.locationtech.jts.geom.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -54,123 +44,120 @@ public Document apply(DataRow dataRow) {
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)
if (value == null) {
continue;
DataColumn.Type type = column.type();
}

DataColumn column = columns.get(i);
applyValue(column, doc, value);
}
return doc;
}

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) {
Field[] shapeFields = createShapeFields(columnName, geometry);
for (Field field : shapeFields) {
doc.add(field);
}
doc.add(new StoredField(columnName + "_wkt", geometry.toText()));
private void applyValue(DataColumn column, Document doc, Object value) {
String columnName = column.name();
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, POLYGON, MULTIPOINT, MULTILINESTRING, MULTIPOLYGON, GEOMETRYCOLLECTION, GEOMETRY:
Geometry geometry = (Geometry) value;
if (geometry != null) {
Field[] shapeFields = createShapeFields(columnName, geometry);
for (Field field : shapeFields) {
doc.add(field);
}
break;
case ENVELOPE:
Envelope envelope = (Envelope) value;
String envelopeStr = envelope.toString();
doc.add(new StringField(columnName, envelopeStr, Field.Store.YES));
break;
case INET_ADDRESS:
case INET4_ADDRESS:
case INET6_ADDRESS:
InetAddress addr = (InetAddress) value;
doc.add(new StringField(columnName, addr.getHostAddress(), Field.Store.YES));
break;
case LOCAL_DATE:
LocalDate date = (LocalDate) value;
doc.add(new StringField(columnName, date.toString(), Field.Store.YES));
break;
case LOCAL_TIME:
LocalTime time = (LocalTime) value;
doc.add(new StringField(columnName, time.toString(), Field.Store.YES));
break;
case LOCAL_DATE_TIME:
LocalDateTime dateTime = (LocalDateTime) value;
doc.add(new StringField(columnName, dateTime.toString(), Field.Store.YES));
break;
case NESTED:
Map<String, Object> map = (Map<String, Object>) value;
for (Map.Entry<String, Object> entry : map.entrySet()) {
String nestedKey = columnName + "." + entry.getKey();
Object nestedValue = entry.getValue();
if (nestedValue != null) {
doc.add(new TextField(nestedKey, nestedValue.toString(), Field.Store.YES));
}
doc.add(new StoredField(columnName + "_wkt", geometry.toText()));
}
break;
case ENVELOPE:
Envelope envelope = (Envelope) value;
String envelopeStr = envelope.toString();
doc.add(new StringField(columnName, envelopeStr, Field.Store.YES));
break;
case INET_ADDRESS, INET4_ADDRESS, INET6_ADDRESS:
InetAddress addr = (InetAddress) value;
doc.add(new StringField(columnName, addr.getHostAddress(), Field.Store.YES));
break;
case LOCAL_DATE:
LocalDate date = (LocalDate) value;
doc.add(new StringField(columnName, date.toString(), Field.Store.YES));
break;
case LOCAL_TIME:
LocalTime time = (LocalTime) value;
doc.add(new StringField(columnName, time.toString(), Field.Store.YES));
break;
case LOCAL_DATE_TIME:
LocalDateTime dateTime = (LocalDateTime) value;
doc.add(new StringField(columnName, dateTime.toString(), Field.Store.YES));
break;
case NESTED:
Map<String, Object> map = (Map<String, Object>) value;
for (Map.Entry<String, Object> entry : map.entrySet()) {
String nestedKey = columnName + "." + entry.getKey();
Object nestedValue = entry.getValue();
if (nestedValue != null) {
doc.add(new TextField(nestedKey, nestedValue.toString(), Field.Store.YES));
}
break;
default:
doc.add(new StringField(columnName, value.toString(), Field.Store.YES));
break;
}
} catch (Exception e) {
logger.error("Error processing column '{}' with value '{}': {}", columnName, value,
e.getMessage());
}
break;
default:
doc.add(new StringField(columnName, value.toString(), Field.Store.YES));
break;
}
} catch (Exception e) {
logger.error("Error processing column '{}' with value '{}': {}", columnName, value,
e.getMessage());
}
return doc;
}

private Field[] createShapeFields(String fieldName, Geometry geometry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,4 @@ public Query build() {
builder.add(termsQuery, BooleanClause.Occur.MUST);
return builder.build();
}

/**
* Get the analyzer.
*
* @return the analyzer
*/
private static Analyzer getAnalyzer() {
return GeocoderConstants.ANALYZER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,6 @@
*/
public record DataSchemaImpl(String name, List<DataColumn> columns) implements DataSchema {

/**
* Constructs a schema with the specified name and columns.
*
* @param name the name of the schema
* @param columns the columns of the schema
*/
public DataSchemaImpl {
}

/**
* {@inheritDoc}
*/
@Override
public String name() {
return name;
}

/**
* {@inheritDoc}
*/
@Override
public List<DataColumn> columns() {
return columns;
}

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit 30e4e33

Please sign in to comment.