From 9a800cac608ff4f7f5d95969aa22f2ced0dbf22f Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Thu, 4 May 2017 18:03:05 -0400 Subject: [PATCH] Switch to StreamInput.readEnum and StreamOutput.writeEnum (elastic/x-pack-elasticsearch#1313) Start using StreamInput.readEnum and StreamOutput.writeEnum for enum serialization. Relates to elastic/elasticsearch#24475 Original commit: elastic/x-pack-elasticsearch@67ca57145876d3ca27c23c1a393a76616a067956 --- .../elasticsearch/xpack/ml/datafeed/ChunkingConfig.java | 8 ++------ .../elasticsearch/xpack/ml/datafeed/DatafeedState.java | 8 ++------ .../org/elasticsearch/xpack/ml/job/config/Connective.java | 8 ++------ .../xpack/ml/job/config/DataDescription.java | 8 ++------ .../org/elasticsearch/xpack/ml/job/config/Detector.java | 8 ++------ .../org/elasticsearch/xpack/ml/job/config/JobState.java | 8 ++------ .../org/elasticsearch/xpack/ml/job/config/Operator.java | 8 ++------ .../org/elasticsearch/xpack/ml/job/config/RuleAction.java | 8 ++------ .../xpack/ml/job/config/RuleConditionType.java | 8 ++------ .../ml/job/process/autodetect/state/ModelSizeStats.java | 8 ++------ .../org/elasticsearch/xpack/ml/notifications/Level.java | 8 ++------ 11 files changed, 22 insertions(+), 66 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/datafeed/ChunkingConfig.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/datafeed/ChunkingConfig.java index 3b7ac90ed3d..02a9f416860 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/datafeed/ChunkingConfig.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/datafeed/ChunkingConfig.java @@ -142,16 +142,12 @@ public class ChunkingConfig extends ToXContentToBytes implements Writeable { } public static Mode readFromStream(StreamInput in) throws IOException { - int ordinal = in.readVInt(); - if (ordinal < 0 || ordinal >= values().length) { - throw new IOException("Unknown Mode ordinal [" + ordinal + "]"); - } - return values()[ordinal]; + return in.readEnum(Mode.class); } @Override public void writeTo(StreamOutput out) throws IOException { - out.writeVInt(ordinal()); + out.writeEnum(this); } @Override diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedState.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedState.java index b82bc559505..84f4b2735fe 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedState.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/datafeed/DatafeedState.java @@ -37,11 +37,7 @@ public enum DatafeedState implements Task.Status { } public static DatafeedState fromStream(StreamInput in) throws IOException { - int ordinal = in.readVInt(); - if (ordinal < 0 || ordinal >= values().length) { - throw new IOException("Unknown public enum DatafeedState ordinal [" + ordinal + "]"); - } - return values()[ordinal]; + return in.readEnum(DatafeedState.class); } @Override @@ -51,7 +47,7 @@ public enum DatafeedState implements Task.Status { @Override public void writeTo(StreamOutput out) throws IOException { - out.writeVInt(ordinal()); + out.writeEnum(this); } @Override diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/Connective.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/Connective.java index d99bc72d85b..f0cf4fd6386 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/Connective.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/Connective.java @@ -27,16 +27,12 @@ public enum Connective implements Writeable { } public static Connective readFromStream(StreamInput in) throws IOException { - int ordinal = in.readVInt(); - if (ordinal < 0 || ordinal >= values().length) { - throw new IOException("Unknown Connective ordinal [" + ordinal + "]"); - } - return values()[ordinal]; + return in.readEnum(Connective.class); } @Override public void writeTo(StreamOutput out) throws IOException { - out.writeVInt(ordinal()); + out.writeEnum(this); } @Override diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/DataDescription.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/DataDescription.java index e75b4c684f9..05832c9225f 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/DataDescription.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/DataDescription.java @@ -63,16 +63,12 @@ public class DataDescription extends ToXContentToBytes implements Writeable { } public static DataFormat readFromStream(StreamInput in) throws IOException { - int ordinal = in.readVInt(); - if (ordinal < 0 || ordinal >= values().length) { - throw new IOException("Unknown DataFormat ordinal [" + ordinal + "]"); - } - return values()[ordinal]; + return in.readEnum(DataFormat.class); } @Override public void writeTo(StreamOutput out) throws IOException { - out.writeVInt(ordinal()); + out.writeEnum(this); } @Override diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/Detector.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/Detector.java index 30d24e9decd..3812ff83a99 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/Detector.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/Detector.java @@ -54,16 +54,12 @@ public class Detector extends ToXContentToBytes implements Writeable { } public static ExcludeFrequent readFromStream(StreamInput in) throws IOException { - int ordinal = in.readVInt(); - if (ordinal < 0 || ordinal >= values().length) { - throw new IOException("Unknown ExcludeFrequent ordinal [" + ordinal + "]"); - } - return values()[ordinal]; + return in.readEnum(ExcludeFrequent.class); } @Override public void writeTo(StreamOutput out) throws IOException { - out.writeVInt(ordinal()); + out.writeEnum(this); } @Override diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/JobState.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/JobState.java index cdb8fec7545..2549b0582c1 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/JobState.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/JobState.java @@ -29,16 +29,12 @@ public enum JobState implements ToXContent, Writeable { } public static JobState fromStream(StreamInput in) throws IOException { - int ordinal = in.readVInt(); - if (ordinal < 0 || ordinal >= values().length) { - throw new IOException("Unknown public enum JobState {\n ordinal [" + ordinal + "]"); - } - return values()[ordinal]; + return in.readEnum(JobState.class); } @Override public void writeTo(StreamOutput out) throws IOException { - out.writeVInt(ordinal()); + out.writeEnum(this); } @Override diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/Operator.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/Operator.java index f6b4d72f2f9..a97d28829f3 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/Operator.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/Operator.java @@ -84,16 +84,12 @@ public enum Operator implements Writeable { } public static Operator readFromStream(StreamInput in) throws IOException { - int ordinal = in.readVInt(); - if (ordinal < 0 || ordinal >= values().length) { - throw new IOException("Unknown Operator ordinal [" + ordinal + "]"); - } - return values()[ordinal]; + return in.readEnum(Operator.class); } @Override public void writeTo(StreamOutput out) throws IOException { - out.writeVInt(ordinal()); + out.writeEnum(this); } @Override diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/RuleAction.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/RuleAction.java index 6cf26d2b738..3f47808a3e2 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/RuleAction.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/RuleAction.java @@ -26,16 +26,12 @@ public enum RuleAction implements Writeable { } public static RuleAction readFromStream(StreamInput in) throws IOException { - int ordinal = in.readVInt(); - if (ordinal < 0 || ordinal >= values().length) { - throw new IOException("Unknown RuleAction ordinal [" + ordinal + "]"); - } - return values()[ordinal]; + return in.readEnum(RuleAction.class); } @Override public void writeTo(StreamOutput out) throws IOException { - out.writeVInt(ordinal()); + out.writeEnum(this); } @Override diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/RuleConditionType.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/RuleConditionType.java index b13418d9188..6b2a0e14e20 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/RuleConditionType.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/RuleConditionType.java @@ -31,16 +31,12 @@ public enum RuleConditionType implements Writeable { } public static RuleConditionType readFromStream(StreamInput in) throws IOException { - int ordinal = in.readVInt(); - if (ordinal < 0 || ordinal >= values().length) { - throw new IOException("Unknown RuleConditionType ordinal [" + ordinal + "]"); - } - return values()[ordinal]; + return in.readEnum(RuleConditionType.class); } @Override public void writeTo(StreamOutput out) throws IOException { - out.writeVInt(ordinal()); + out.writeEnum(this); } @Override diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/ModelSizeStats.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/ModelSizeStats.java index 22226bb3c4f..3ab52ad78bb 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/ModelSizeStats.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/process/autodetect/state/ModelSizeStats.java @@ -96,16 +96,12 @@ public class ModelSizeStats extends ToXContentToBytes implements Writeable { } public static MemoryStatus readFromStream(StreamInput in) throws IOException { - int ordinal = in.readVInt(); - if (ordinal < 0 || ordinal >= values().length) { - throw new IOException("Unknown MemoryStatus ordinal [" + ordinal + "]"); - } - return values()[ordinal]; + return in.readEnum(MemoryStatus.class); } @Override public void writeTo(StreamOutput out) throws IOException { - out.writeVInt(ordinal()); + out.writeEnum(this); } @Override diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/notifications/Level.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/notifications/Level.java index a283a253040..3b9f69f1403 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/notifications/Level.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/notifications/Level.java @@ -27,16 +27,12 @@ public enum Level implements Writeable { } public static Level readFromStream(StreamInput in) throws IOException { - int ordinal = in.readVInt(); - if (ordinal < 0 || ordinal >= values().length) { - throw new IOException("Unknown Level ordinal [" + ordinal + "]"); - } - return values()[ordinal]; + return in.readEnum(Level.class); } @Override public void writeTo(StreamOutput out) throws IOException { - out.writeVInt(ordinal()); + out.writeEnum(this); } @Override