Remove Writeable#readFrom

It is always better to call a static read method or a constructor that
takes StreamInput.

Relates to #17085
This commit is contained in:
Nik Everett 2016-04-21 09:58:46 -04:00
parent 476d57150a
commit 9f4cb3de9f
15 changed files with 151 additions and 205 deletions

View File

@ -47,6 +47,16 @@ public final class TaskOperationFailure implements Writeable<TaskOperationFailur
private final RestStatus status;
public TaskOperationFailure(String nodeId, long taskId, Throwable t) {
this.nodeId = nodeId;
this.taskId = taskId;
this.reason = t;
status = ExceptionsHelper.status(t);
}
/**
* Read from a stream.
*/
public TaskOperationFailure(StreamInput in) throws IOException {
nodeId = in.readString();
taskId = in.readLong();
@ -54,11 +64,12 @@ public final class TaskOperationFailure implements Writeable<TaskOperationFailur
status = RestStatus.readFrom(in);
}
public TaskOperationFailure(String nodeId, long taskId, Throwable t) {
this.nodeId = nodeId;
this.taskId = taskId;
this.reason = t;
status = ExceptionsHelper.status(t);
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(nodeId);
out.writeLong(taskId);
out.writeThrowable(reason);
RestStatus.writeTo(out, status);
}
public String getNodeId() {
@ -81,19 +92,6 @@ public final class TaskOperationFailure implements Writeable<TaskOperationFailur
return reason;
}
@Override
public TaskOperationFailure readFrom(StreamInput in) throws IOException {
return new TaskOperationFailure(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(nodeId);
out.writeLong(taskId);
out.writeThrowable(reason);
RestStatus.writeTo(out, status);
}
@Override
public String toString() {
return "[" + nodeId + "][" + taskId + "] failed, reason [" + getReason() + "]";

View File

@ -50,6 +50,18 @@ public final class ClusterAllocationExplanation implements ToXContent, Writeable
private final UnassignedInfo unassignedInfo;
private final long remainingDelayNanos;
public ClusterAllocationExplanation(ShardId shard, boolean primary, @Nullable String assignedNodeId,
UnassignedInfo unassignedInfo, Map<DiscoveryNode, Decision> nodeToDecision,
Map<DiscoveryNode, Float> nodeWeights, long remainingDelayNanos) {
this.shard = shard;
this.primary = primary;
this.assignedNodeId = assignedNodeId;
this.unassignedInfo = unassignedInfo;
this.nodeToDecision = nodeToDecision == null ? Collections.emptyMap() : nodeToDecision;
this.nodeWeights = nodeWeights == null ? Collections.emptyMap() : nodeWeights;
this.remainingDelayNanos = remainingDelayNanos;
}
public ClusterAllocationExplanation(StreamInput in) throws IOException {
this.shard = ShardId.readShardId(in);
this.primary = in.readBoolean();
@ -78,17 +90,28 @@ public final class ClusterAllocationExplanation implements ToXContent, Writeable
remainingDelayNanos = in.readVLong();
}
public ClusterAllocationExplanation(ShardId shard, boolean primary, @Nullable String assignedNodeId,
UnassignedInfo unassignedInfo, Map<DiscoveryNode, Decision> nodeToDecision,
Map<DiscoveryNode, Float> nodeWeights, long remainingDelayNanos) {
this.shard = shard;
this.primary = primary;
this.assignedNodeId = assignedNodeId;
this.unassignedInfo = unassignedInfo;
this.nodeToDecision = nodeToDecision == null ? Collections.emptyMap() : nodeToDecision;
this.nodeWeights = nodeWeights == null ? Collections.emptyMap() : nodeWeights;
this.remainingDelayNanos = remainingDelayNanos;
@Override
public void writeTo(StreamOutput out) throws IOException {
this.getShard().writeTo(out);
out.writeBoolean(this.isPrimary());
out.writeOptionalString(this.getAssignedNodeId());
out.writeOptionalWriteable(this.getUnassignedInfo());
Map<DiscoveryNode, Decision> ntd = this.getNodeDecisions();
out.writeVInt(ntd.size());
for (Map.Entry<DiscoveryNode, Decision> entry : ntd.entrySet()) {
entry.getKey().writeTo(out);
Decision.writeTo(entry.getValue(), out);
}
Map<DiscoveryNode, Float> ntw = this.getNodeWeights();
out.writeVInt(ntw.size());
for (Map.Entry<DiscoveryNode, Float> entry : ntw.entrySet()) {
entry.getKey().writeTo(out);
out.writeFloat(entry.getValue());
}
out.writeVLong(remainingDelayNanos);
}
public ShardId getShard() {
return this.shard;
@ -183,31 +206,4 @@ public final class ClusterAllocationExplanation implements ToXContent, Writeable
builder.endObject(); // end wrapping object
return builder;
}
@Override
public ClusterAllocationExplanation readFrom(StreamInput in) throws IOException {
return new ClusterAllocationExplanation(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
this.getShard().writeTo(out);
out.writeBoolean(this.isPrimary());
out.writeOptionalString(this.getAssignedNodeId());
out.writeOptionalWriteable(this.getUnassignedInfo());
Map<DiscoveryNode, Decision> ntd = this.getNodeDecisions();
out.writeVInt(ntd.size());
for (Map.Entry<DiscoveryNode, Decision> entry : ntd.entrySet()) {
entry.getKey().writeTo(out);
Decision.writeTo(entry.getValue(), out);
}
Map<DiscoveryNode, Float> ntw = this.getNodeWeights();
out.writeVInt(ntw.size());
for (Map.Entry<DiscoveryNode, Float> entry : ntw.entrySet()) {
entry.getKey().writeTo(out);
out.writeFloat(entry.getValue());
}
out.writeVLong(remainingDelayNanos);
}
}

View File

@ -75,6 +75,9 @@ public class TaskInfo implements Writeable<TaskInfo>, ToXContent {
this.parentTaskId = parentTaskId;
}
/**
* Read from a stream.
*/
public TaskInfo(StreamInput in) throws IOException {
node = new DiscoveryNode(in);
taskId = new TaskId(node.getId(), in.readLong());
@ -88,6 +91,20 @@ public class TaskInfo implements Writeable<TaskInfo>, ToXContent {
parentTaskId = TaskId.readFromStream(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
node.writeTo(out);
out.writeLong(taskId.getId());
out.writeString(type);
out.writeString(action);
out.writeOptionalString(description);
out.writeOptionalNamedWriteable(status);
out.writeLong(startTime);
out.writeLong(runningTimeNanos);
out.writeBoolean(cancellable);
parentTaskId.writeTo(out);
}
public TaskId getTaskId() {
return taskId;
}
@ -148,25 +165,6 @@ public class TaskInfo implements Writeable<TaskInfo>, ToXContent {
return parentTaskId;
}
@Override
public TaskInfo readFrom(StreamInput in) throws IOException {
return new TaskInfo(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
node.writeTo(out);
out.writeLong(taskId.getId());
out.writeString(type);
out.writeString(action);
out.writeOptionalString(description);
out.writeOptionalNamedWriteable(status);
out.writeLong(startTime);
out.writeLong(runningTimeNanos);
out.writeBoolean(cancellable);
parentTaskId.writeTo(out);
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field("node", node.getId());

View File

@ -46,22 +46,16 @@ final class WriteableIngestDocument implements Writeable<WriteableIngestDocument
this.ingestDocument = new IngestDocument(sourceAndMetadata, ingestMetadata);
}
IngestDocument getIngestDocument() {
return ingestDocument;
}
@Override
public WriteableIngestDocument readFrom(StreamInput in) throws IOException {
return new WriteableIngestDocument(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeMap(ingestDocument.getSourceAndMetadata());
out.writeGenericValue(ingestDocument.getIngestMetadata());
}
IngestDocument getIngestDocument() {
return ingestDocument;
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("doc");

View File

@ -88,34 +88,6 @@ public class DiscoveryNode implements Writeable<DiscoveryNode>, ToXContent {
private final Version version;
private final Set<Role> roles;
/**
* Creates a new {@link DiscoveryNode} by reading from the stream provided as argument
* @param in the stream
* @throws IOException if there is an error while reading from the stream
*/
public DiscoveryNode(StreamInput in) throws IOException {
this.nodeName = in.readString().intern();
this.nodeId = in.readString().intern();
this.hostName = in.readString().intern();
this.hostAddress = in.readString().intern();
this.address = TransportAddressSerializers.addressFromStream(in);
int size = in.readVInt();
this.attributes = new HashMap<>(size);
for (int i = 0; i < size; i++) {
this.attributes.put(in.readString(), in.readString());
}
int rolesSize = in.readVInt();
this.roles = EnumSet.noneOf(Role.class);
for (int i = 0; i < rolesSize; i++) {
int ordinal = in.readVInt();
if (ordinal < 0 || ordinal >= Role.values().length) {
throw new IOException("Unknown Role ordinal [" + ordinal + "]");
}
this.roles.add(Role.values()[ordinal]);
}
this.version = Version.readVersion(in);
}
/**
* Creates a new {@link DiscoveryNode}
* <p>
@ -204,6 +176,53 @@ public class DiscoveryNode implements Writeable<DiscoveryNode>, ToXContent {
this.roles = Collections.unmodifiableSet(rolesSet);
}
/**
* Creates a new {@link DiscoveryNode} by reading from the stream provided as argument
* @param in the stream
* @throws IOException if there is an error while reading from the stream
*/
public DiscoveryNode(StreamInput in) throws IOException {
this.nodeName = in.readString().intern();
this.nodeId = in.readString().intern();
this.hostName = in.readString().intern();
this.hostAddress = in.readString().intern();
this.address = TransportAddressSerializers.addressFromStream(in);
int size = in.readVInt();
this.attributes = new HashMap<>(size);
for (int i = 0; i < size; i++) {
this.attributes.put(in.readString(), in.readString());
}
int rolesSize = in.readVInt();
this.roles = EnumSet.noneOf(Role.class);
for (int i = 0; i < rolesSize; i++) {
int ordinal = in.readVInt();
if (ordinal < 0 || ordinal >= Role.values().length) {
throw new IOException("Unknown Role ordinal [" + ordinal + "]");
}
this.roles.add(Role.values()[ordinal]);
}
this.version = Version.readVersion(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(nodeName);
out.writeString(nodeId);
out.writeString(hostName);
out.writeString(hostAddress);
addressToStream(out, address);
out.writeVInt(attributes.size());
for (Map.Entry<String, String> entry : attributes.entrySet()) {
out.writeString(entry.getKey());
out.writeString(entry.getValue());
}
out.writeVInt(roles.size());
for (Role role : roles) {
out.writeVInt(role.ordinal());
}
Version.writeVersion(version, out);
}
/**
* The address that the node can be communicated with.
*/
@ -273,30 +292,6 @@ public class DiscoveryNode implements Writeable<DiscoveryNode>, ToXContent {
return this.hostAddress;
}
@Override
public DiscoveryNode readFrom(StreamInput in) throws IOException {
return new DiscoveryNode(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(nodeName);
out.writeString(nodeId);
out.writeString(hostName);
out.writeString(hostAddress);
addressToStream(out, address);
out.writeVInt(attributes.size());
for (Map.Entry<String, String> entry : attributes.entrySet()) {
out.writeString(entry.getKey());
out.writeString(entry.getValue());
}
out.writeVInt(roles.size());
for (Role role : roles) {
out.writeVInt(role.ordinal());
}
Version.writeVersion(version, out);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof DiscoveryNode)) {

View File

@ -126,9 +126,7 @@ public enum GeoDistance implements Writeable<GeoDistance> {
}
};
/** Returns a GeoDistance object as read from the StreamInput. */
@Override
public GeoDistance readFrom(StreamInput in) throws IOException {
public static GeoDistance readFromStream(StreamInput in) throws IOException {
int ord = in.readVInt();
if (ord < 0 || ord >= values().length) {
throw new IOException("Unknown GeoDistance ordinal [" + ord + "]");
@ -136,10 +134,6 @@ public enum GeoDistance implements Writeable<GeoDistance> {
return GeoDistance.values()[ord];
}
public static GeoDistance readFromStream(StreamInput in) throws IOException {
return DEFAULT.readFrom(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(this.ordinal());

View File

@ -43,8 +43,7 @@ public enum ShapeRelation implements Writeable<ShapeRelation>{
this.relationName = relationName;
}
@Override
public ShapeRelation readFrom(StreamInput in) throws IOException {
public static ShapeRelation readFromStream(StreamInput in) throws IOException {
int ordinal = in.readVInt();
if (ordinal < 0 || ordinal >= values().length) {
throw new IOException("Unknown ShapeRelation ordinal [" + ordinal + "]");

View File

@ -42,8 +42,7 @@ public enum SpatialStrategy implements Writeable<SpatialStrategy> {
return strategyName;
}
@Override
public SpatialStrategy readFrom(StreamInput in) throws IOException {
public static SpatialStrategy readFromStream(StreamInput in) throws IOException {
int ordinal = in.readVInt();
if (ordinal < 0 || ordinal >= values().length) {
throw new IOException("Unknown SpatialStrategy ordinal [" + ordinal + "]");

View File

@ -38,16 +38,6 @@ public interface Writeable<T> { // TODO remove <T>
*/
void writeTo(StreamOutput out) throws IOException;
/**
* Read this object from a stream. Use a {@link Writeable.Reader} instead. This lives on for backwards compatibility but should be
* removed before 5.0.0GA. It is not deprecated because Diffable extends this interface and it shouldn't be deprecated there.
*/
default T readFrom(StreamInput in) throws IOException {
// NORELEASE remove before 5.0.0GA
throw new UnsupportedOperationException(
"Prefer calling a constructor or static method that takes a StreamInput to calling readFrom.");
}
/**
* Reference to a method that can read some object from a stream. By convention this is a constructor that takes
* {@linkplain StreamInput} as an argument for most classes and a static method for things like enums. Returning null from one of these

View File

@ -154,8 +154,8 @@ public class GeoShapeQueryBuilder extends AbstractQueryBuilder<GeoShapeQueryBuil
indexedShapeIndex = in.readOptionalString();
indexedShapePath = in.readOptionalString();
}
relation = ShapeRelation.DISJOINT.readFrom(in);
strategy = in.readOptionalWriteable(SpatialStrategy.RECURSIVE::readFrom);
relation = ShapeRelation.readFromStream(in);
strategy = in.readOptionalWriteable(SpatialStrategy::readFromStream);
ignoreUnmapped = in.readBoolean();
}

View File

@ -730,8 +730,6 @@ public class Store extends AbstractIndexShardComponent implements Closeable, Ref
* @see StoreFileMetaData
*/
public final static class MetadataSnapshot implements Iterable<StoreFileMetaData>, Writeable<MetadataSnapshot> {
private static final ESLogger logger = Loggers.getLogger(MetadataSnapshot.class);
private final Map<String, StoreFileMetaData> metadata;
public static final MetadataSnapshot EMPTY = new MetadataSnapshot();
@ -760,6 +758,9 @@ public class Store extends AbstractIndexShardComponent implements Closeable, Ref
assert metadata.isEmpty() || numSegmentFiles() == 1 : "numSegmentFiles: " + numSegmentFiles();
}
/**
* Read from a stream.
*/
public MetadataSnapshot(StreamInput in) throws IOException {
final int size = in.readVInt();
Map<String, StoreFileMetaData> metadata = new HashMap<>();
@ -779,6 +780,20 @@ public class Store extends AbstractIndexShardComponent implements Closeable, Ref
assert metadata.isEmpty() || numSegmentFiles() == 1 : "numSegmentFiles: " + numSegmentFiles();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(this.metadata.size());
for (StoreFileMetaData meta : this) {
meta.writeTo(out);
}
out.writeVInt(commitUserData.size());
for (Map.Entry<String, String> entry : commitUserData.entrySet()) {
out.writeString(entry.getKey());
out.writeString(entry.getValue());
}
out.writeLong(numDocs);
}
/**
* Returns the number of documents in this store snapshot
*/
@ -1020,20 +1035,6 @@ public class Store extends AbstractIndexShardComponent implements Closeable, Ref
return metadata.size();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(this.metadata.size());
for (StoreFileMetaData meta : this) {
meta.writeTo(out);
}
out.writeVInt(commitUserData.size());
for (Map.Entry<String, String> entry : commitUserData.entrySet()) {
out.writeString(entry.getKey());
out.writeString(entry.getValue());
}
out.writeLong(numDocs);
}
public Map<String, String> getCommitUserData() {
return commitUserData;
}
@ -1076,11 +1077,6 @@ public class Store extends AbstractIndexShardComponent implements Closeable, Ref
public String getSyncId() {
return commitUserData.get(Engine.SYNC_COMMIT_ID);
}
@Override
public MetadataSnapshot readFrom(StreamInput in) throws IOException {
return new MetadataSnapshot(in);
}
}
/**

View File

@ -329,17 +329,10 @@ public class TestTaskPlugin extends Plugin {
}
public UnblockTestTaskResponse(StreamInput in) {
}
@Override
public void writeTo(StreamOutput out) throws IOException {
}
@Override
public UnblockTestTaskResponse readFrom(StreamInput in) throws IOException {
return new UnblockTestTaskResponse(in);
}
}

View File

@ -195,6 +195,11 @@ public class TransportTasksActionTests extends TaskManagerTestCase {
status = in.readString();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(status);
}
public TestTaskResponse(String status) {
this.status = status;
}
@ -202,19 +207,8 @@ public class TransportTasksActionTests extends TaskManagerTestCase {
public String getStatus() {
return status;
}
@Override
public TestTaskResponse readFrom(StreamInput in) throws IOException {
return new TestTaskResponse(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(status);
}
}
static class TestTasksRequest extends BaseTasksRequest<TestTasksRequest> {
}

View File

@ -63,19 +63,19 @@ public class ShapeRelationTests extends ESTestCase {
try (BytesStreamOutput out = new BytesStreamOutput()) {
out.writeVInt(0);
try (StreamInput in = StreamInput.wrap(out.bytes())) {
assertThat(ShapeRelation.DISJOINT.readFrom(in), equalTo(ShapeRelation.INTERSECTS));
assertThat(ShapeRelation.readFromStream(in), equalTo(ShapeRelation.INTERSECTS));
}
}
try (BytesStreamOutput out = new BytesStreamOutput()) {
out.writeVInt(1);
try (StreamInput in = StreamInput.wrap(out.bytes())) {
assertThat(ShapeRelation.DISJOINT.readFrom(in), equalTo(ShapeRelation.DISJOINT));
assertThat(ShapeRelation.readFromStream(in), equalTo(ShapeRelation.DISJOINT));
}
}
try (BytesStreamOutput out = new BytesStreamOutput()) {
out.writeVInt(2);
try (StreamInput in = StreamInput.wrap(out.bytes())) {
assertThat(ShapeRelation.DISJOINT.readFrom(in), equalTo(ShapeRelation.WITHIN));
assertThat(ShapeRelation.readFromStream(in), equalTo(ShapeRelation.WITHIN));
}
}
}
@ -84,7 +84,7 @@ public class ShapeRelationTests extends ESTestCase {
try (BytesStreamOutput out = new BytesStreamOutput()) {
out.writeVInt(randomIntBetween(3, Integer.MAX_VALUE));
try (StreamInput in = StreamInput.wrap(out.bytes())) {
ShapeRelation.DISJOINT.readFrom(in);
ShapeRelation.readFromStream(in);
fail("Expected IOException");
} catch(IOException e) {
assertThat(e.getMessage(), containsString("Unknown ShapeRelation ordinal ["));

View File

@ -55,13 +55,13 @@ public class SpatialStrategyTests extends ESTestCase {
try (BytesStreamOutput out = new BytesStreamOutput()) {
out.writeVInt(0);
try (StreamInput in = StreamInput.wrap(out.bytes())) {
assertThat(SpatialStrategy.TERM.readFrom(in), equalTo(SpatialStrategy.TERM));
assertThat(SpatialStrategy.readFromStream(in), equalTo(SpatialStrategy.TERM));
}
}
try (BytesStreamOutput out = new BytesStreamOutput()) {
out.writeVInt(1);
try (StreamInput in = StreamInput.wrap(out.bytes())) {
assertThat(SpatialStrategy.TERM.readFrom(in), equalTo(SpatialStrategy.RECURSIVE));
assertThat(SpatialStrategy.readFromStream(in), equalTo(SpatialStrategy.RECURSIVE));
}
}
}
@ -70,7 +70,7 @@ public class SpatialStrategyTests extends ESTestCase {
try (BytesStreamOutput out = new BytesStreamOutput()) {
out.writeVInt(randomIntBetween(2, Integer.MAX_VALUE));
try (StreamInput in = StreamInput.wrap(out.bytes())) {
SpatialStrategy.TERM.readFrom(in);
SpatialStrategy.readFromStream(in);
fail("Expected IOException");
} catch(IOException e) {
assertThat(e.getMessage(), containsString("Unknown SpatialStrategy ordinal ["));