From f6ab4e107870ce2dbed4847f5eb90a5edf314455 Mon Sep 17 00:00:00 2001 From: javanna Date: Thu, 1 Sep 2016 13:55:03 +0200 Subject: [PATCH] ByteSizeValue to implement Writeable rather than Streamable With this we can make ByteSizeValue immutable for real. --- .../common/unit/ByteSizeValue.java | 34 ++++++------------- .../RecoverFilesRecoveryException.java | 2 +- .../common/unit/ByteSizeValueTests.java | 20 +++++++++++ 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java b/core/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java index 32df65850a8..16f07926583 100644 --- a/core/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java +++ b/core/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java @@ -23,20 +23,25 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.io.stream.Streamable; +import org.elasticsearch.common.io.stream.Writeable; import java.io.IOException; import java.util.Locale; import java.util.Objects; -public class ByteSizeValue implements Streamable { +public class ByteSizeValue implements Writeable { - private long size; + private final long size; + private final ByteSizeUnit sizeUnit; - private ByteSizeUnit sizeUnit; - - private ByteSizeValue() { + public ByteSizeValue(StreamInput in) throws IOException { + size = in.readVLong(); + sizeUnit = ByteSizeUnit.BYTES; + } + @Override + public void writeTo(StreamOutput out) throws IOException { + out.writeVLong(bytes()); } public ByteSizeValue(long bytes) { @@ -218,23 +223,6 @@ public class ByteSizeValue implements Streamable { return new ByteSizeValue(bytes, ByteSizeUnit.BYTES); } - public static ByteSizeValue readBytesSizeValue(StreamInput in) throws IOException { - ByteSizeValue sizeValue = new ByteSizeValue(); - sizeValue.readFrom(in); - return sizeValue; - } - - @Override - public void readFrom(StreamInput in) throws IOException { - size = in.readVLong(); - sizeUnit = ByteSizeUnit.BYTES; - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeVLong(bytes()); - } - @Override public boolean equals(Object o) { if (this == o) { diff --git a/core/src/main/java/org/elasticsearch/indices/recovery/RecoverFilesRecoveryException.java b/core/src/main/java/org/elasticsearch/indices/recovery/RecoverFilesRecoveryException.java index 69a55e03c9a..d2e07bd9e4c 100644 --- a/core/src/main/java/org/elasticsearch/indices/recovery/RecoverFilesRecoveryException.java +++ b/core/src/main/java/org/elasticsearch/indices/recovery/RecoverFilesRecoveryException.java @@ -57,7 +57,7 @@ public class RecoverFilesRecoveryException extends ElasticsearchException implem public RecoverFilesRecoveryException(StreamInput in) throws IOException{ super(in); numberOfFiles = in.readInt(); - totalFilesSize = ByteSizeValue.readBytesSizeValue(in); + totalFilesSize = new ByteSizeValue(in); } @Override diff --git a/core/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java b/core/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java index b075e9d56d7..0eada4f2cde 100644 --- a/core/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java +++ b/core/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java @@ -20,9 +20,13 @@ package org.elasticsearch.common.unit; import org.elasticsearch.ElasticsearchParseException; +import org.elasticsearch.common.io.stream.BytesStreamOutput; +import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.test.ESTestCase; import org.hamcrest.MatcherAssert; +import java.io.IOException; + import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; @@ -165,4 +169,20 @@ public class ByteSizeValueTests extends ESTestCase { assertThat(e.getMessage(), containsString("failed to parse setting [test]")); } } + + public void testSerialization() throws IOException { + //negative values cannot be serialized at the moment, we do abs but that is not enough with Long.MIN_VALUE + long l = Long.MIN_VALUE; + while (l == Long.MIN_VALUE) { + l = randomLong(); + } + ByteSizeValue byteSizeValue = new ByteSizeValue(Math.abs(l), randomFrom(ByteSizeUnit.values())); + try (BytesStreamOutput out = new BytesStreamOutput()) { + byteSizeValue.writeTo(out); + try (StreamInput in = out.bytes().streamInput()) { + ByteSizeValue deserializedByteSizeValue = new ByteSizeValue(in); + assertEquals(byteSizeValue, deserializedByteSizeValue); + } + } + } }