Skip to content

Commit

Permalink
Dry up some spots around map reading from StreamInput (#96853)
Browse files Browse the repository at this point in the history
Mostly the keys we read are strings, lets add an overload for that
to save some code and maybe help the compiler make better decisions.
Also readMapOfLists an be way simplified, no point in duplicating
the map reading code here just to save one capturing lambda, there's
not hot code that benefits from this.
  • Loading branch information
original-brownbear authored Jun 15, 2023
1 parent 01731ae commit 795e07c
Show file tree
Hide file tree
Showing 125 changed files with 167 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public BucketSelectorPipelineAggregationBuilder(String name, Map<String, String>
*/
public BucketSelectorPipelineAggregationBuilder(StreamInput in) throws IOException {
super(in, NAME);
bucketsPathsMap = in.readMap(StreamInput::readString, StreamInput::readString);
bucketsPathsMap = in.readMap(StreamInput::readString);
script = new Script(in);
gapPolicy = GapPolicy.readFrom(in);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static class Response extends ActionResponse implements ToXContentObject

Response(StreamInput in) throws IOException {
super(in);
grokPatterns = in.readMap(StreamInput::readString, StreamInput::readString);
grokPatterns = in.readMap(StreamInput::readString);
}

public Map<String, String> getGrokPatterns() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ public static GeoIpTaskState fromXContent(XContentParser parser) throws IOExcept
}

GeoIpTaskState(StreamInput input) throws IOException {
databases = input.readImmutableMap(StreamInput::readString, in -> {
long lastUpdate = in.readLong();
return new Metadata(lastUpdate, in.readVInt(), in.readVInt(), in.readString(), in.readLong());
});
databases = input.readImmutableMap(in -> new Metadata(in.readLong(), in.readVInt(), in.readVInt(), in.readString(), in.readLong()));
}

public GeoIpTaskState put(String name, Metadata metadata) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ public ElasticsearchException(String msg, Throwable cause, Object... args) {
public ElasticsearchException(StreamInput in) throws IOException {
super(in.readOptionalString(), in.readException());
readStackTrace(this, in);
headers.putAll(in.readMapOfLists(StreamInput::readString, StreamInput::readString));
metadata.putAll(in.readMapOfLists(StreamInput::readString, StreamInput::readString));
headers.putAll(in.readMapOfLists(StreamInput::readString));
metadata.putAll(in.readMapOfLists(StreamInput::readString));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static DesiredBalanceResponse from(StreamInput in) throws IOException {
in.getTransportVersion().onOrAfter(CLUSTER_BALANCE_STATS_VERSION)
? ClusterBalanceStats.readFrom(in)
: ClusterBalanceStats.EMPTY,
in.readImmutableMap(StreamInput::readString, v -> v.readImmutableMap(StreamInput::readVInt, DesiredShards::from)),
in.readImmutableMap(v -> v.readImmutableMap(StreamInput::readVInt, DesiredShards::from)),
in.getTransportVersion().onOrAfter(CLUSTER_INFO_VERSION) ? new ClusterInfo(in) : ClusterInfo.EMPTY
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ClusterSearchShardsResponse(StreamInput in) throws IOException {
super(in);
groups = in.readArray(ClusterSearchShardsGroup::new, ClusterSearchShardsGroup[]::new);
nodes = in.readArray(DiscoveryNode::new, DiscoveryNode[]::new);
indicesAndFilters = in.readMap(StreamInput::readString, AliasFilter::readFrom);
indicesAndFilters = in.readMap(AliasFilter::readFrom);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public GetSnapshotsResponse(
public GetSnapshotsResponse(StreamInput in) throws IOException {
this.snapshots = in.readImmutableList(SnapshotInfo::readFrom);
if (in.getTransportVersion().onOrAfter(GetSnapshotsRequest.MULTIPLE_REPOSITORIES_SUPPORT_ADDED)) {
final Map<String, ElasticsearchException> failedResponses = in.readMap(StreamInput::readString, StreamInput::readException);
final Map<String, ElasticsearchException> failedResponses = in.readMap(StreamInput::readException);
this.failures = Collections.unmodifiableMap(failedResponses);
this.next = in.readOptionalString();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class GetShardSnapshotResponse extends ActionResponse {
GetShardSnapshotResponse(StreamInput in) throws IOException {
super(in);
this.latestShardSnapshot = in.readOptionalWriteable(ShardSnapshotInfo::new);
this.repositoryFailures = in.readMap(StreamInput::readString, RepositoryException::new);
this.repositoryFailures = in.readMap(RepositoryException::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public SearchUsageStats(Map<String, Long> queries, Map<String, Long> sections, l
}

public SearchUsageStats(StreamInput in) throws IOException {
this.queries = in.readMap(StreamInput::readString, StreamInput::readLong);
this.sections = in.readMap(StreamInput::readString, StreamInput::readLong);
this.queries = in.readMap(StreamInput::readLong);
this.sections = in.readMap(StreamInput::readLong);
this.totalSearchCount = in.readVLong();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public GetAliasesResponse(Map<String, List<AliasMetadata>> aliases, Map<String,
public GetAliasesResponse(StreamInput in) throws IOException {
super(in);
aliases = in.readImmutableOpenMap(StreamInput::readString, i -> i.readList(AliasMetadata::new));
dataStreamAliases = in.readMap(StreamInput::readString, in1 -> in1.readList(DataStreamAlias::new));
dataStreamAliases = in.readMap(in1 -> in1.readList(DataStreamAlias::new));
}

public Map<String, List<AliasMetadata>> getAliases() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class ReloadAnalyzersResponse extends BroadcastResponse {

public ReloadAnalyzersResponse(StreamInput in) throws IOException {
super(in);
this.reloadDetails = in.readMap(StreamInput::readString, ReloadDetails::new);
this.reloadDetails = in.readMap(ReloadDetails::new);
}

public ReloadAnalyzersResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class AnalyzeIndexDiskUsageResponse extends BroadcastResponse {

AnalyzeIndexDiskUsageResponse(StreamInput in) throws IOException {
super(in);
stats = in.readMap(StreamInput::readString, IndexDiskUsageStats::new);
stats = in.readMap(IndexDiskUsageStats::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public IndexDiskUsageStats(long indexSizeInBytes) {
}

public IndexDiskUsageStats(StreamInput in) throws IOException {
this.fields = new HashMap<>(in.readMap(StreamInput::readString, PerFieldDiskUsage::new));
this.fields = new HashMap<>(in.readMap(PerFieldDiskUsage::new));
this.indexSizeInBytes = in.readVLong();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class GetFieldMappingsResponse extends ActionResponse implements ToXConte

GetFieldMappingsResponse(StreamInput in) throws IOException {
super(in);
mappings = in.readImmutableMap(StreamInput::readString, mapIn -> {
mappings = in.readImmutableMap(mapIn -> {
if (mapIn.getTransportVersion().before(TransportVersion.V_8_0_0)) {
int typesSize = mapIn.readVInt();
assert typesSize == 1 || typesSize == 0 : "Expected 0 or 1 types but got " + typesSize;
Expand All @@ -60,10 +60,7 @@ public class GetFieldMappingsResponse extends ActionResponse implements ToXConte
}
mapIn.readString(); // type
}
return mapIn.readImmutableMap(
StreamInput::readString,
inpt -> new FieldMappingMetadata(inpt.readString(), inpt.readBytesReference())
);
return mapIn.readImmutableMap(inpt -> new FieldMappingMetadata(inpt.readString(), inpt.readBytesReference()));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public GetMappingsResponse(Map<String, MappingMetadata> mappings) {

GetMappingsResponse(StreamInput in) throws IOException {
super(in);
mappings = in.readImmutableMap(StreamInput::readString, in.getTransportVersion().before(TransportVersion.V_8_0_0) ? i -> {
mappings = in.readImmutableMap(in.getTransportVersion().before(TransportVersion.V_8_0_0) ? i -> {
int mappingCount = i.readVInt();
assert mappingCount == 1 || mappingCount == 0 : "Expected 0 or 1 mappings but got " + mappingCount;
if (mappingCount == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class RecoveryResponse extends BaseBroadcastResponse implements ChunkedTo

public RecoveryResponse(StreamInput in) throws IOException {
super(in);
shardRecoveryStates = in.readMapOfLists(StreamInput::readString, RecoveryState::readRecoveryState);
shardRecoveryStates = in.readMapOfLists(RecoveryState::readRecoveryState);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public GetSettingsResponse(Map<String, Settings> indexToSettings, Map<String, Se

public GetSettingsResponse(StreamInput in) throws IOException {
super(in);
indexToSettings = in.readImmutableMap(StreamInput::readString, Settings::readSettingsFromStream);
indexToDefaultSettings = in.readImmutableMap(StreamInput::readString, Settings::readSettingsFromStream);
indexToSettings = in.readImmutableMap(Settings::readSettingsFromStream);
indexToDefaultSettings = in.readImmutableMap(Settings::readSettingsFromStream);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,7 @@ public IndicesShardStoresResponse(Map<String, Map<Integer, List<StoreStatus>>> s

public IndicesShardStoresResponse(StreamInput in) throws IOException {
super(in);
storeStatuses = in.readImmutableMap(
StreamInput::readString,
i -> i.readImmutableMap(StreamInput::readInt, j -> j.readImmutableList(StoreStatus::new))
);
storeStatuses = in.readImmutableMap(i -> i.readImmutableMap(StreamInput::readInt, j -> j.readImmutableList(StoreStatus::new)));
failures = in.readImmutableList(Failure::readFailure);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class FieldUsageStatsResponse extends ChunkedBroadcastResponse {

FieldUsageStatsResponse(StreamInput in) throws IOException {
super(in);
stats = in.readMap(StreamInput::readString, i -> i.readList(FieldUsageShardResponse::new));
stats = in.readMap(i -> i.readList(FieldUsageShardResponse::new));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public class IndicesStatsResponse extends ChunkedBroadcastResponse {
super(in);
shards = in.readArray(ShardStats::new, ShardStats[]::new);
if (in.getTransportVersion().onOrAfter(TransportVersion.V_8_1_0)) {
indexHealthMap = in.readMap(StreamInput::readString, ClusterHealthStatus::readFrom);
indexStateMap = in.readMap(StreamInput::readString, IndexMetadata.State::readFrom);
indexHealthMap = in.readMap(ClusterHealthStatus::readFrom);
indexStateMap = in.readMap(IndexMetadata.State::readFrom);
} else {
indexHealthMap = Map.of();
indexStateMap = Map.of();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static class Response extends ActionResponse implements ToXContentObject

public Response(StreamInput in) throws IOException {
super(in);
componentTemplates = in.readMap(StreamInput::readString, ComponentTemplate::new);
componentTemplates = in.readMap(ComponentTemplate::new);
if (in.getTransportVersion().onOrAfter(TransportVersion.V_8_500_007)) {
rolloverConfiguration = in.readOptionalWriteable(RolloverConfiguration::new);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static class Response extends ActionResponse implements ToXContentObject

public Response(StreamInput in) throws IOException {
super(in);
indexTemplates = in.readMap(StreamInput::readString, ComposableIndexTemplate::new);
indexTemplates = in.readMap(ComposableIndexTemplate::new);
if (in.getTransportVersion().onOrAfter(TransportVersion.V_8_500_007)) {
rolloverConfiguration = in.readOptionalWriteable(RolloverConfiguration::new);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public FieldCapabilities(
this.nonDimensionIndices = null;
this.metricConflictsIndices = null;
}
meta = in.readMap(StreamInput::readString, i -> i.readSet(StreamInput::readString));
meta = in.readMap(i -> i.readSet(StreamInput::readString));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class FieldCapabilitiesIndexResponse implements Writeable {

FieldCapabilitiesIndexResponse(StreamInput in) throws IOException {
this.indexName = in.readString();
this.responseMap = in.readMap(StreamInput::readString, IndexFieldCapabilities::new);
this.responseMap = in.readMap(IndexFieldCapabilities::new);
this.canMatch = in.readBoolean();
this.originVersion = in.getTransportVersion();
if (in.getTransportVersion().onOrAfter(MAPPING_HASH_VERSION)) {
Expand All @@ -71,7 +71,7 @@ private record GroupByMappingHash(List<String> indices, String indexMappingHash,
implements
Writeable {
GroupByMappingHash(StreamInput in) throws IOException {
this(in.readStringList(), in.readString(), in.readMap(StreamInput::readString, IndexFieldCapabilities::new));
this(in.readStringList(), in.readString(), in.readMap(IndexFieldCapabilities::new));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private FieldCapabilitiesResponse(
public FieldCapabilitiesResponse(StreamInput in) throws IOException {
super(in);
indices = in.readStringArray();
this.responseMap = in.readMap(StreamInput::readString, FieldCapabilitiesResponse::readField);
this.responseMap = in.readMap(FieldCapabilitiesResponse::readField);
this.indexResponses = FieldCapabilitiesIndexResponse.readList(in);
this.failures = in.readList(FieldCapabilitiesFailure::new);
}
Expand Down Expand Up @@ -136,7 +136,7 @@ public boolean isMetadataField(String field) {
}

private static Map<String, FieldCapabilities> readField(StreamInput in) throws IOException {
return in.readMap(StreamInput::readString, FieldCapabilities::new);
return in.readMap(FieldCapabilities::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class IndexFieldCapabilities implements Writeable {
this.isDimension = false;
this.metricType = null;
}
this.meta = in.readMap(StreamInput::readString, StreamInput::readString);
this.meta = in.readMap(StreamInput::readString);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public IndexRequest(@Nullable ShardId shardId, StreamInput in) throws IOExceptio
requireAlias = false;
}
if (in.getTransportVersion().onOrAfter(TransportVersion.V_7_13_0)) {
dynamicTemplates = in.readMap(StreamInput::readString, StreamInput::readString);
dynamicTemplates = in.readMap(StreamInput::readString);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static SearchContextId decode(NamedWriteableRegistry namedWriteableRegist
final TransportVersion version = TransportVersion.readVersion(in);
in.setTransportVersion(version);
final Map<ShardId, SearchContextIdForNode> shards = in.readMap(ShardId::new, SearchContextIdForNode::new);
final Map<String, AliasFilter> aliasFilters = in.readMap(StreamInput::readString, AliasFilter::readFrom);
final Map<String, AliasFilter> aliasFilters = in.readMap(AliasFilter::readFrom);
if (in.available() > 0) {
throw new IllegalArgumentException("Not all bytes were read");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public SearchRequest(StreamInput in) throws IOException {
minCompatibleShardNode = null;
}
if (in.getTransportVersion().onOrAfter(TransportVersion.V_7_16_0)) {
waitForCheckpoints = in.readMap(StreamInput::readString, StreamInput::readLongArray);
waitForCheckpoints = in.readMap(StreamInput::readLongArray);
waitForCheckpointsTimeout = in.readTimeValue();
}
if (in.getTransportVersion().onOrAfter(TransportVersion.V_8_4_0)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public SearchShardsResponse(StreamInput in) throws IOException {
super(in);
this.groups = in.readList(SearchShardsGroup::new);
this.nodes = in.readList(DiscoveryNode::new);
this.aliasFilters = in.readMap(StreamInput::readString, AliasFilter::readFrom);
this.aliasFilters = in.readMap(AliasFilter::readFrom);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public TermVectorsFields(BytesReference headerRef, BytesReference termVectors) t
hasTermStatistic = header.readBoolean();
hasFieldStatistic = header.readBoolean();
hasScores = header.readBoolean();
fieldMap = header.readMap(StreamInput::readString, StreamInput::readVLong);
fieldMap = header.readMap(StreamInput::readVLong);
}
// reference to the term vector data
this.termVectors = termVectors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public ClusterInfo(
}

public ClusterInfo(StreamInput in) throws IOException {
this.leastAvailableSpaceUsage = in.readImmutableMap(StreamInput::readString, DiskUsage::new);
this.mostAvailableSpaceUsage = in.readImmutableMap(StreamInput::readString, DiskUsage::new);
this.shardSizes = in.readImmutableMap(StreamInput::readString, StreamInput::readLong);
this.leastAvailableSpaceUsage = in.readImmutableMap(DiskUsage::new);
this.mostAvailableSpaceUsage = in.readImmutableMap(DiskUsage::new);
this.shardSizes = in.readImmutableMap(StreamInput::readLong);
this.shardDataSetSizes = in.getTransportVersion().onOrAfter(DATA_SET_SIZE_SIZE_VERSION)
? in.readImmutableMap(ShardId::new, StreamInput::readLong)
: Map.of();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ public static ClusterState readFrom(StreamInput in, DiscoveryNode localNode) thr
builder.routingTable = RoutingTable.readFrom(in);
builder.nodes = DiscoveryNodes.readFrom(in, localNode);
if (in.getTransportVersion().onOrAfter(TransportVersion.V_8_8_0)) {
builder.transportVersions(in.readMap(StreamInput::readString, TransportVersion::readVersion));
builder.transportVersions(in.readMap(TransportVersion::readVersion));
} else {
// this clusterstate is from a pre-8.8.0 node
// infer the versions from discoverynodes for now
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public ClusterFormationState(StreamInput in) throws IOException {
this(
in.readStringList(),
new DiscoveryNode(in),
in.readMap(StreamInput::readString, DiscoveryNode::new),
in.readMap(DiscoveryNode::new),
in.readLong(),
in.readLong(),
new VotingConfiguration(in),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ private static List<DiscoveryNode> readRecentMasters(StreamInput in) throws IOEx

private static Map<String, String> readClusterFormationStates(StreamInput in) throws IOException {
if (in.readBoolean()) {
return in.readMap(StreamInput::readString, StreamInput::readString);
return in.readMap(StreamInput::readString);
} else {
return Map.of();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public ComponentTemplateMetadata(Map<String, ComponentTemplate> componentTemplat
}

public ComponentTemplateMetadata(StreamInput in) throws IOException {
this.componentTemplates = in.readMap(StreamInput::readString, ComponentTemplate::new);
this.componentTemplates = in.readMap(ComponentTemplate::new);
}

public Map<String, ComponentTemplate> componentTemplates() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public ComposableIndexTemplateMetadata(Map<String, ComposableIndexTemplate> temp
}

public ComposableIndexTemplateMetadata(StreamInput in) throws IOException {
this.indexTemplates = in.readMap(StreamInput::readString, ComposableIndexTemplate::new);
this.indexTemplates = in.readMap(ComposableIndexTemplate::new);
}

public static ComposableIndexTemplateMetadata fromXContent(XContentParser parser) throws IOException {
Expand Down
Loading

0 comments on commit 795e07c

Please sign in to comment.