ByteSizeValue to implement Writeable rather than Streamable

With this we can make ByteSizeValue immutable for real.
This commit is contained in:
javanna 2016-09-01 13:55:03 +02:00 committed by Luca Cavanna
parent faa03ad9fa
commit f6ab4e1078
3 changed files with 32 additions and 24 deletions

View File

@ -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) {

View File

@ -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

View File

@ -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);
}
}
}
}