Make DataStream instances explicitly immutable (#58688) (#58839)

This commit is contained in:
Dan Hermann 2020-07-01 11:14:01 -05:00 committed by GitHub
parent 860c94ca60
commit 98a62a6b2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 4 deletions

View File

@ -56,7 +56,7 @@ public final class DataStream extends AbstractDiffable<DataStream> implements To
public DataStream(String name, TimestampField timeStampField, List<Index> indices, long generation) { public DataStream(String name, TimestampField timeStampField, List<Index> indices, long generation) {
this.name = name; this.name = name;
this.timeStampField = timeStampField; this.timeStampField = timeStampField;
this.indices = indices; this.indices = Collections.unmodifiableList(indices);
this.generation = generation; this.generation = generation;
assert indices.size() > 0; assert indices.size() > 0;
assert indices.get(indices.size() - 1).getName().equals(getDefaultBackingIndexName(name, generation)); assert indices.get(indices.size() - 1).getName().equals(getDefaultBackingIndexName(name, generation));
@ -236,12 +236,11 @@ public final class DataStream extends AbstractDiffable<DataStream> implements To
"invalid type defined for mapping of timestamp_field"; "invalid type defined for mapping of timestamp_field";
this.name = name; this.name = name;
this.fieldMapping = fieldMapping; this.fieldMapping = Collections.unmodifiableMap(fieldMapping);
} }
public TimestampField(StreamInput in) throws IOException { public TimestampField(StreamInput in) throws IOException {
this.name = in.readString(); this(in.readString(), in.readMap());
this.fieldMapping = in.readMap();
} }
/** /**

View File

@ -181,4 +181,10 @@ public class DataStreamTests extends AbstractSerializingTestCase<DataStream> {
Map.of("properties", Map.of("@timestamp", Map.of("type", "date", "meta", Map.of("x", "y"))))))))); Map.of("properties", Map.of("@timestamp", Map.of("type", "date", "meta", Map.of("x", "y")))))))));
assertThat(mappings, equalTo(expectedMapping)); assertThat(mappings, equalTo(expectedMapping));
} }
public void testDataStreamsAreImmutable() {
DataStream ds = randomInstance();
expectThrows(UnsupportedOperationException.class, () -> ds.getIndices().clear());
expectThrows(UnsupportedOperationException.class, () -> ds.getTimeStampField().getFieldMapping().clear());
}
} }