Skip to content

Commit

Permalink
graphaware#158 fix bug when indexing non-finite numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapin committed Feb 17, 2020
1 parent 94f821c commit ac6d007
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.graphaware.module.es.util;

import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.IOException;

/**
* Customizations:
* - connectionManagerShared: true
Expand All @@ -12,6 +18,26 @@
*/
public class CustomJestClientFactory extends JestClientFactory {

/**
* Serialize non-finite doubles (-Infinity, +Infinity, NaN) as strings.
*/
private class NonFiniteAsStringAdapter extends TypeAdapter<Double> {
@Override
public void write(JsonWriter out, Double value) throws IOException {
if (Double.isFinite(value)) {
out.value(value);
} else {
// serialize as "+Infinity", "-Infinity" and "NaN" (string)
out.value(value + "");
}
}

@Override
public Double read(JsonReader in) throws IOException {
return in.nextDouble();
}
}

@Override
protected HttpClientBuilder configureHttpClient(HttpClientBuilder builder) {
return builder
Expand All @@ -21,6 +47,11 @@ protected HttpClientBuilder configureHttpClient(HttpClientBuilder builder) {
@Override
public void setHttpClientConfig(HttpClientConfig httpClientConfig) {
super.setHttpClientConfig(new HttpClientConfig.Builder(httpClientConfig)
.gson(new GsonBuilder()
.serializeSpecialFloatingPointValues()
.registerTypeAdapter(Double.class, new NonFiniteAsStringAdapter())
.create()
)
.readTimeout(20000) // 20s
.connTimeout(10000) // 10s
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ protected void writeSomeStuffWithListToNeo4j() {
node.addLabel(Label.label("LabelWithList2"));
int[] listOfInteger = {1, 2, 3};
node.setProperty("listOfInteger", listOfInteger);
node.setProperty("infinite-number", Double.NEGATIVE_INFINITY);
node.setProperty("nan-number", Double.NaN);
tx.success();
}

Expand Down

0 comments on commit ac6d007

Please sign in to comment.