From 8bc6297d36e9e2a8170acf813012618c3032fa86 Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Thu, 14 Nov 2019 14:39:00 -0800 Subject: [PATCH 1/3] correct licensing and incorporation of FastMath this resolves incorrectly licensed code in #49009. ESSloppyMath is removed in favor of preserving as much of the original FastMath as possible. Since no additional methods are introduced in ESSloppyMath, this abstraction is removed. --- .../util/{ESSloppyMath.java => FastMath.java} | 34 +++++++++---------- ...loppyMathTests.java => FastMathTests.java} | 33 ++++++++++-------- .../bucket/geogrid/GeoTileUtils.java | 4 +-- 3 files changed, 37 insertions(+), 34 deletions(-) rename libs/core/src/main/java/org/elasticsearch/common/util/{ESSloppyMath.java => FastMath.java} (91%) rename libs/core/src/test/java/org/elasticsearch/common/util/{ESSloppyMathTests.java => FastMathTests.java} (55%) diff --git a/libs/core/src/main/java/org/elasticsearch/common/util/ESSloppyMath.java b/libs/core/src/main/java/org/elasticsearch/common/util/FastMath.java similarity index 91% rename from libs/core/src/main/java/org/elasticsearch/common/util/ESSloppyMath.java rename to libs/core/src/main/java/org/elasticsearch/common/util/FastMath.java index 9c36b3f3e17a5..c18bc4ef485b2 100644 --- a/libs/core/src/main/java/org/elasticsearch/common/util/ESSloppyMath.java +++ b/libs/core/src/main/java/org/elasticsearch/common/util/FastMath.java @@ -1,10 +1,9 @@ /* - * 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 + * Copyright 2012 Jeff Hain + * + * Licensed 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 * @@ -14,16 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.elasticsearch.common.util; /* * ============================================================================= + * Notice of fdlibm package this program is partially derived from: * - * sinh and atan ported from - * https://github.com/yannrichet/jmathplot/blob/f25426e0ab0e68647ad2b75f577c7be050ecac86/src/main/java/org/math/plot/utils/FastMath.java - * (Apache 2.0) Copyright 2012 Jeff Hain, which is partially derived from fdlibm package - * - * ============================================================================= * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunSoft, a Sun Microsystems, Inc. business. @@ -33,13 +27,18 @@ * ============================================================================= */ - -/** - * Similar to Lucene's SloppyMath, but for additional math functions. +/* + * This code sourced from: + * https://github.com/yannrichet/jmathplot/blob/f25426e0ab0e68647ad2b75f577c7be050ecac86/src/main/java/org/math/plot/utils/FastMath.java */ -public class ESSloppyMath { - private ESSloppyMath() {} +package org.elasticsearch.common.util; + +public final class FastMath { + + private FastMath() { + + } //-------------------------------------------------------------------------- // RE-USABLE CONSTANTS @@ -177,4 +176,5 @@ public static double atan(double value) { } } } + } diff --git a/libs/core/src/test/java/org/elasticsearch/common/util/ESSloppyMathTests.java b/libs/core/src/test/java/org/elasticsearch/common/util/FastMathTests.java similarity index 55% rename from libs/core/src/test/java/org/elasticsearch/common/util/ESSloppyMathTests.java rename to libs/core/src/test/java/org/elasticsearch/common/util/FastMathTests.java index 35afbe77ff882..f1250164ecd3a 100644 --- a/libs/core/src/test/java/org/elasticsearch/common/util/ESSloppyMathTests.java +++ b/libs/core/src/test/java/org/elasticsearch/common/util/FastMathTests.java @@ -1,27 +1,30 @@ /* - * 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 + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch 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 + * 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. + * 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.elasticsearch.common.util; import org.elasticsearch.test.ESTestCase; -import static org.elasticsearch.common.util.ESSloppyMath.atan; -import static org.elasticsearch.common.util.ESSloppyMath.sinh; +import static org.elasticsearch.common.util.FastMath.atan; +import static org.elasticsearch.common.util.FastMath.sinh; -public class ESSloppyMathTests extends ESTestCase { +public class FastMathTests extends ESTestCase { // accuracy for atan(x) static double ATAN_DELTA = 1E-15; diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoTileUtils.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoTileUtils.java index 19381e87fedb6..cf23628023592 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoTileUtils.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoTileUtils.java @@ -20,7 +20,7 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.geo.GeoPoint; -import org.elasticsearch.common.util.ESSloppyMath; +import org.elasticsearch.common.util.FastMath; import org.elasticsearch.common.xcontent.ObjectParser.ValueType; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.support.XContentMapValues; @@ -211,7 +211,7 @@ private static int validateZXY(int zoom, int xTile, int yTile) { private static GeoPoint zxyToGeoPoint(int zoom, int xTile, int yTile) { final int tiles = validateZXY(zoom, xTile, yTile); final double n = Math.PI - (2.0 * Math.PI * (yTile + 0.5)) / tiles; - final double lat = Math.toDegrees(ESSloppyMath.atan(ESSloppyMath.sinh(n))); + final double lat = Math.toDegrees(FastMath.atan(FastMath.sinh(n))); final double lon = ((xTile + 0.5) / tiles * 360.0) - 180; return new GeoPoint(lat, lon); } From ff43fd9051042efc23add9e0265f736539e865e3 Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Tue, 19 Nov 2019 18:11:16 -0800 Subject: [PATCH 2/3] wrap FastMath with ESSloppyMath --- .../common/util/ESSloppyMath.java | 36 +++++++++++++++++++ .../elasticsearch/common/util/FastMath.java | 7 ++-- ...tMathTests.java => ESSloppyMathTests.java} | 6 ++-- .../bucket/geogrid/GeoTileUtils.java | 4 +-- 4 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 libs/core/src/main/java/org/elasticsearch/common/util/ESSloppyMath.java rename libs/core/src/test/java/org/elasticsearch/common/util/{FastMathTests.java => ESSloppyMathTests.java} (91%) diff --git a/libs/core/src/main/java/org/elasticsearch/common/util/ESSloppyMath.java b/libs/core/src/main/java/org/elasticsearch/common/util/ESSloppyMath.java new file mode 100644 index 0000000000000..d123b89e8c7b8 --- /dev/null +++ b/libs/core/src/main/java/org/elasticsearch/common/util/ESSloppyMath.java @@ -0,0 +1,36 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch 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.elasticsearch.common.util; + +/** + * Similar to Lucene's SloppyMath, but for additional math functions. + */ +public class ESSloppyMath { + + private ESSloppyMath() {} + + public static double sinh(double value) { + return FastMath.sinh(value); + } + + public static double atan(double value) { + return FastMath.atan(value); + } +} diff --git a/libs/core/src/main/java/org/elasticsearch/common/util/FastMath.java b/libs/core/src/main/java/org/elasticsearch/common/util/FastMath.java index c18bc4ef485b2..326e3179443f3 100644 --- a/libs/core/src/main/java/org/elasticsearch/common/util/FastMath.java +++ b/libs/core/src/main/java/org/elasticsearch/common/util/FastMath.java @@ -34,11 +34,12 @@ package org.elasticsearch.common.util; +/** + * Additions or modifications to this class should only come from the original org.math.plot.utils.FastMath source + */ public final class FastMath { - private FastMath() { - - } + private FastMath() { } //-------------------------------------------------------------------------- // RE-USABLE CONSTANTS diff --git a/libs/core/src/test/java/org/elasticsearch/common/util/FastMathTests.java b/libs/core/src/test/java/org/elasticsearch/common/util/ESSloppyMathTests.java similarity index 91% rename from libs/core/src/test/java/org/elasticsearch/common/util/FastMathTests.java rename to libs/core/src/test/java/org/elasticsearch/common/util/ESSloppyMathTests.java index f1250164ecd3a..24ac1e0176392 100644 --- a/libs/core/src/test/java/org/elasticsearch/common/util/FastMathTests.java +++ b/libs/core/src/test/java/org/elasticsearch/common/util/ESSloppyMathTests.java @@ -21,10 +21,10 @@ import org.elasticsearch.test.ESTestCase; -import static org.elasticsearch.common.util.FastMath.atan; -import static org.elasticsearch.common.util.FastMath.sinh; +import static org.elasticsearch.common.util.ESSloppyMath.atan; +import static org.elasticsearch.common.util.ESSloppyMath.sinh; -public class FastMathTests extends ESTestCase { +public class ESSloppyMathTests extends ESTestCase { // accuracy for atan(x) static double ATAN_DELTA = 1E-15; diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoTileUtils.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoTileUtils.java index cf23628023592..19381e87fedb6 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoTileUtils.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoTileUtils.java @@ -20,7 +20,7 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.geo.GeoPoint; -import org.elasticsearch.common.util.FastMath; +import org.elasticsearch.common.util.ESSloppyMath; import org.elasticsearch.common.xcontent.ObjectParser.ValueType; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.support.XContentMapValues; @@ -211,7 +211,7 @@ private static int validateZXY(int zoom, int xTile, int yTile) { private static GeoPoint zxyToGeoPoint(int zoom, int xTile, int yTile) { final int tiles = validateZXY(zoom, xTile, yTile); final double n = Math.PI - (2.0 * Math.PI * (yTile + 0.5)) / tiles; - final double lat = Math.toDegrees(FastMath.atan(FastMath.sinh(n))); + final double lat = Math.toDegrees(ESSloppyMath.atan(ESSloppyMath.sinh(n))); final double lon = ((xTile + 0.5) / tiles * 360.0) - 180; return new GeoPoint(lat, lon); } From c02a27e0b91c2a1a2d3406410b083e76ec191e97 Mon Sep 17 00:00:00 2001 From: Tal Levy Date: Wed, 20 Nov 2019 08:08:37 -0800 Subject: [PATCH 3/3] package private FastMath --- .../src/main/java/org/elasticsearch/common/util/FastMath.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/core/src/main/java/org/elasticsearch/common/util/FastMath.java b/libs/core/src/main/java/org/elasticsearch/common/util/FastMath.java index 326e3179443f3..6446e706442ee 100644 --- a/libs/core/src/main/java/org/elasticsearch/common/util/FastMath.java +++ b/libs/core/src/main/java/org/elasticsearch/common/util/FastMath.java @@ -37,7 +37,7 @@ /** * Additions or modifications to this class should only come from the original org.math.plot.utils.FastMath source */ -public final class FastMath { +final class FastMath { private FastMath() { }