From 14097359a469bc8d9313dd53dc3717d1838609ca Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Thu, 12 Apr 2018 10:24:58 -0600 Subject: [PATCH] Move TimeValue into elasticsearch-core project (#29486) This commit moves the `TimeValue` class into the elasticsearch-core project. This allows us to use this class in many of our other projects without relying on the entire `server` jar. Relates to #28504 --- .../elasticsearch/common/unit/TimeValue.java | 10 +------ .../common/unit/TimeValueTests.java | 30 ------------------- .../XContentElasticsearchExtension.java | 1 + .../common/io/stream/BytesStreamsTests.java | 28 +++++++++++++++++ 4 files changed, 30 insertions(+), 39 deletions(-) rename {server => libs/elasticsearch-core}/src/main/java/org/elasticsearch/common/unit/TimeValue.java (96%) rename {server => libs/elasticsearch-core}/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java (88%) diff --git a/server/src/main/java/org/elasticsearch/common/unit/TimeValue.java b/libs/elasticsearch-core/src/main/java/org/elasticsearch/common/unit/TimeValue.java similarity index 96% rename from server/src/main/java/org/elasticsearch/common/unit/TimeValue.java rename to libs/elasticsearch-core/src/main/java/org/elasticsearch/common/unit/TimeValue.java index 8fa2c94173a..56cdc09b34e 100644 --- a/server/src/main/java/org/elasticsearch/common/unit/TimeValue.java +++ b/libs/elasticsearch-core/src/main/java/org/elasticsearch/common/unit/TimeValue.java @@ -19,15 +19,12 @@ package org.elasticsearch.common.unit; -import org.elasticsearch.common.xcontent.ToXContentFragment; -import org.elasticsearch.common.xcontent.XContentBuilder; - import java.io.IOException; import java.util.Locale; import java.util.Objects; import java.util.concurrent.TimeUnit; -public class TimeValue implements Comparable, ToXContentFragment { +public class TimeValue implements Comparable { /** How many nano-seconds in one milli-second */ public static final long NSEC_PER_MSEC = TimeUnit.NANOSECONDS.convert(1, TimeUnit.MILLISECONDS); @@ -352,9 +349,4 @@ public class TimeValue implements Comparable, ToXContentFragment { double otherValue = ((double) timeValue.duration) * timeValue.timeUnit.toNanos(1); return Double.compare(thisValue, otherValue); } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.value(toString()); - } } diff --git a/server/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java b/libs/elasticsearch-core/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java similarity index 88% rename from server/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java rename to libs/elasticsearch-core/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java index a94e6581ea3..af6b89be5ff 100644 --- a/server/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java +++ b/libs/elasticsearch-core/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java @@ -19,15 +19,10 @@ package org.elasticsearch.common.unit; -import org.elasticsearch.common.io.stream.BytesStreamOutput; -import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.test.ESTestCase; -import java.io.IOException; import java.util.concurrent.TimeUnit; -import static org.elasticsearch.common.unit.TimeValue.timeValueNanos; -import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.Matchers.containsString; @@ -154,31 +149,6 @@ public class TimeValueTests extends ESTestCase { return randomFrom("nanos", "micros", "ms", "s", "m", "h", "d"); } - private void assertEqualityAfterSerialize(TimeValue value, int expectedSize) throws IOException { - BytesStreamOutput out = new BytesStreamOutput(); - out.writeTimeValue(value); - assertEquals(expectedSize, out.size()); - - StreamInput in = out.bytes().streamInput(); - TimeValue inValue = in.readTimeValue(); - - assertThat(inValue, equalTo(value)); - assertThat(inValue.duration(), equalTo(value.duration())); - assertThat(inValue.timeUnit(), equalTo(value.timeUnit())); - } - - public void testSerialize() throws Exception { - assertEqualityAfterSerialize(new TimeValue(100, TimeUnit.DAYS), 3); - assertEqualityAfterSerialize(timeValueNanos(-1), 2); - assertEqualityAfterSerialize(timeValueNanos(1), 2); - assertEqualityAfterSerialize(timeValueSeconds(30), 2); - - final TimeValue timeValue = new TimeValue(randomIntBetween(0, 1024), randomFrom(TimeUnit.values())); - BytesStreamOutput out = new BytesStreamOutput(); - out.writeZLong(timeValue.duration()); - assertEqualityAfterSerialize(timeValue, 1 + out.bytes().length()); - } - public void testFailOnUnknownUnits() { try { TimeValue.parseTimeValue("23tw", null, "test"); diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/XContentElasticsearchExtension.java b/server/src/main/java/org/elasticsearch/common/xcontent/XContentElasticsearchExtension.java index 42089d23923..38abe90ad46 100644 --- a/server/src/main/java/org/elasticsearch/common/xcontent/XContentElasticsearchExtension.java +++ b/server/src/main/java/org/elasticsearch/common/xcontent/XContentElasticsearchExtension.java @@ -61,6 +61,7 @@ public class XContentElasticsearchExtension implements XContentBuilderExtension writers.put(FixedDateTimeZone.class, (b, v) -> b.value(Objects.toString(v))); writers.put(MutableDateTime.class, XContentBuilder::timeValue); writers.put(DateTime.class, XContentBuilder::timeValue); + writers.put(TimeValue.class, (b, v) -> b.value(v.toString())); writers.put(BytesReference.class, (b, v) -> { if (v == null) { diff --git a/server/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java b/server/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java index 27656e9bc09..cee6c2e4cb8 100644 --- a/server/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java +++ b/server/src/test/java/org/elasticsearch/common/io/stream/BytesStreamsTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.lucene.BytesRefs; +import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.test.ESTestCase; import org.joda.time.DateTimeZone; @@ -41,6 +42,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.TreeMap; +import java.util.concurrent.TimeUnit; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -812,4 +814,30 @@ public class BytesStreamsTests extends ESTestCase { } assertEquals(0, input.available()); } + + private void assertEqualityAfterSerialize(TimeValue value, int expectedSize) throws IOException { + BytesStreamOutput out = new BytesStreamOutput(); + out.writeTimeValue(value); + assertEquals(expectedSize, out.size()); + + StreamInput in = out.bytes().streamInput(); + TimeValue inValue = in.readTimeValue(); + + assertThat(inValue, equalTo(value)); + assertThat(inValue.duration(), equalTo(value.duration())); + assertThat(inValue.timeUnit(), equalTo(value.timeUnit())); + } + + public void testTimeValueSerialize() throws Exception { + assertEqualityAfterSerialize(new TimeValue(100, TimeUnit.DAYS), 3); + assertEqualityAfterSerialize(TimeValue.timeValueNanos(-1), 2); + assertEqualityAfterSerialize(TimeValue.timeValueNanos(1), 2); + assertEqualityAfterSerialize(TimeValue.timeValueSeconds(30), 2); + + final TimeValue timeValue = new TimeValue(randomIntBetween(0, 1024), randomFrom(TimeUnit.values())); + BytesStreamOutput out = new BytesStreamOutput(); + out.writeZLong(timeValue.duration()); + assertEqualityAfterSerialize(timeValue, 1 + out.bytes().length()); + } + }