ByteSizeValue to implement Writeable rather than Streamable
With this we can make ByteSizeValue immutable for real.
This commit is contained in:
parent
faa03ad9fa
commit
f6ab4e1078
|
@ -23,20 +23,25 @@ import org.elasticsearch.ElasticsearchParseException;
|
||||||
import org.elasticsearch.common.Strings;
|
import org.elasticsearch.common.Strings;
|
||||||
import org.elasticsearch.common.io.stream.StreamInput;
|
import org.elasticsearch.common.io.stream.StreamInput;
|
||||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
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.io.IOException;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Objects;
|
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;
|
public ByteSizeValue(StreamInput in) throws IOException {
|
||||||
|
size = in.readVLong();
|
||||||
private ByteSizeValue() {
|
sizeUnit = ByteSizeUnit.BYTES;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writeTo(StreamOutput out) throws IOException {
|
||||||
|
out.writeVLong(bytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
public ByteSizeValue(long bytes) {
|
public ByteSizeValue(long bytes) {
|
||||||
|
@ -218,23 +223,6 @@ public class ByteSizeValue implements Streamable {
|
||||||
return new ByteSizeValue(bytes, ByteSizeUnit.BYTES);
|
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
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) {
|
if (this == o) {
|
||||||
|
|
|
@ -57,7 +57,7 @@ public class RecoverFilesRecoveryException extends ElasticsearchException implem
|
||||||
public RecoverFilesRecoveryException(StreamInput in) throws IOException{
|
public RecoverFilesRecoveryException(StreamInput in) throws IOException{
|
||||||
super(in);
|
super(in);
|
||||||
numberOfFiles = in.readInt();
|
numberOfFiles = in.readInt();
|
||||||
totalFilesSize = ByteSizeValue.readBytesSizeValue(in);
|
totalFilesSize = new ByteSizeValue(in);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -20,9 +20,13 @@
|
||||||
package org.elasticsearch.common.unit;
|
package org.elasticsearch.common.unit;
|
||||||
|
|
||||||
import org.elasticsearch.ElasticsearchParseException;
|
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.elasticsearch.test.ESTestCase;
|
||||||
import org.hamcrest.MatcherAssert;
|
import org.hamcrest.MatcherAssert;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
|
@ -165,4 +169,20 @@ public class ByteSizeValueTests extends ESTestCase {
|
||||||
assertThat(e.getMessage(), containsString("failed to parse setting [test]"));
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue