Skip to content

Commit

Permalink
[Backport 2.x] [Refactor] ImmutableOpenMap to j.u.Map in IndexMetadata (
Browse files Browse the repository at this point in the history
#7306) (#8266)

Refactors usage of ImmutableOpenMap in IndexMetadata to java.util.Map.
This continues the removal of the HPPC dependency in core.

Signed-off-by: Nicholas Walter Knize <[email protected]>
  • Loading branch information
nknize authored Jun 26, 2023
1 parent 3fd36a2 commit 0156c55
Show file tree
Hide file tree
Showing 29 changed files with 195 additions and 623 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- [Refactor] Sets util from server to common lib ([#8230](https://github.com/opensearch-project/OpenSearch/pull/8230))
- [Refactor] Metadata members from ImmutableOpenMap to j.u.Map ([#7165](https://github.com/opensearch-project/OpenSearch/pull/7165))
- [Refactor] more ImmutableOpenMap to jdk Map in cluster package ([#7301](https://github.com/opensearch-project/OpenSearch/pull/7301))
- [Refactor] ImmutableOpenMap to j.u.Map in IndexMetadata ([#7306](https://github.com/opensearch-project/OpenSearch/pull/7306))

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@
import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.common.Nullable;
import org.opensearch.core.ParseField;
import org.opensearch.common.collect.ImmutableOpenMap;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.xcontent.ConstructingObjectParser;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.index.mapper.MapperService;

import java.io.IOException;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -59,9 +60,7 @@ public class IndexTemplateMetadata {
true,
(a, name) -> {
List<Map.Entry<String, AliasMetadata>> alias = (List<Map.Entry<String, AliasMetadata>>) a[5];
ImmutableOpenMap<String, AliasMetadata> aliasMap = new ImmutableOpenMap.Builder<String, AliasMetadata>().putAll(
alias.stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
).build();
final Map<String, AliasMetadata> aliasMap = alias.stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return new IndexTemplateMetadata(
name,
(Integer) a[0],
Expand Down Expand Up @@ -128,7 +127,7 @@ public class IndexTemplateMetadata {

private final MappingMetadata mappings;

private final ImmutableOpenMap<String, AliasMetadata> aliases;
private final Map<String, AliasMetadata> aliases;

public IndexTemplateMetadata(
String name,
Expand All @@ -137,7 +136,7 @@ public IndexTemplateMetadata(
List<String> patterns,
Settings settings,
MappingMetadata mappings,
ImmutableOpenMap<String, AliasMetadata> aliases
final Map<String, AliasMetadata> aliases
) {
if (patterns == null || patterns.isEmpty()) {
throw new IllegalArgumentException("Index patterns must not be null or empty; got " + patterns);
Expand All @@ -148,7 +147,7 @@ public IndexTemplateMetadata(
this.patterns = patterns;
this.settings = settings;
this.mappings = mappings;
this.aliases = aliases;
this.aliases = Collections.unmodifiableMap(aliases);
}

public String name() {
Expand Down Expand Up @@ -176,7 +175,7 @@ public MappingMetadata mappings() {
return this.mappings;
}

public ImmutableOpenMap<String, AliasMetadata> aliases() {
public Map<String, AliasMetadata> aliases() {
return this.aliases;
}

Expand Down Expand Up @@ -217,12 +216,12 @@ public static class Builder {

private MappingMetadata mappings;

private final ImmutableOpenMap.Builder<String, AliasMetadata> aliases;
private final Map<String, AliasMetadata> aliases;

public Builder(String name) {
this.name = name;
mappings = null;
aliases = ImmutableOpenMap.builder();
aliases = new HashMap<>();
}

public Builder(IndexTemplateMetadata indexTemplateMetadata) {
Expand All @@ -233,7 +232,7 @@ public Builder(IndexTemplateMetadata indexTemplateMetadata) {
settings(indexTemplateMetadata.settings());

mappings = indexTemplateMetadata.mappings();
aliases = ImmutableOpenMap.builder(indexTemplateMetadata.aliases());
aliases = new HashMap<>(indexTemplateMetadata.aliases());
}

public Builder order(int order) {
Expand Down Expand Up @@ -277,7 +276,7 @@ public Builder putAlias(AliasMetadata.Builder aliasMetadata) {
}

public IndexTemplateMetadata build() {
return new IndexTemplateMetadata(name, order, version, indexPatterns, settings, mappings, aliases.build());
return new IndexTemplateMetadata(name, order, version, indexPatterns, settings, mappings, aliases);
}

public static IndexTemplateMetadata fromXContent(XContentParser parser, String templateName) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ public void testParsingFromOpenSearchResponse() throws IOException {
assertThat(result.mappings().sourceAsMap(), equalTo(expectedMapping.get("_doc")));

assertThat(result.aliases().size(), equalTo(esIMD.aliases().size()));
List<AliasMetadata> expectedAliases = Arrays.stream(esIMD.aliases().values().toArray(AliasMetadata.class))
List<AliasMetadata> expectedAliases = Arrays.stream(esIMD.aliases().values().toArray(new AliasMetadata[0]))
.sorted(Comparator.comparing(AliasMetadata::alias))
.collect(Collectors.toList());
List<AliasMetadata> actualAliases = Arrays.stream(result.aliases().values().toArray(AliasMetadata.class))
List<AliasMetadata> actualAliases = Arrays.stream(result.aliases().values().toArray(new AliasMetadata[0]))
.sorted(Comparator.comparing(AliasMetadata::alias))
.collect(Collectors.toList());
for (int j = 0; j < result.aliases().size(); j++) {
Expand Down Expand Up @@ -216,7 +216,7 @@ static void toXContent(GetIndexTemplatesResponse response, XContentBuilder build

serverTemplateBuilder.patterns(clientITMD.patterns());

Iterator<AliasMetadata> aliases = clientITMD.aliases().valuesIt();
Iterator<AliasMetadata> aliases = clientITMD.aliases().values().iterator();
aliases.forEachRemaining((a) -> serverTemplateBuilder.putAlias(a));

serverTemplateBuilder.settings(clientITMD.settings());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ public IndexMetadata randomChange(IndexMetadata part) {
break;
case 1:
if (randomBoolean() && part.getAliases().isEmpty() == false) {
builder.removeAlias(randomFrom(part.getAliases().keys().toArray(String.class)));
builder.removeAlias(randomFrom(part.getAliases().keySet().toArray(new String[0])));
} else {
builder.putAlias(AliasMetadata.builder(randomAlphaOfLength(10)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Spliterators;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

Expand Down Expand Up @@ -111,8 +112,8 @@ protected void doClusterManagerOperation(
ImmutableOpenMap<String, Settings> defaultSettings = ImmutableOpenMap.of();
ImmutableOpenMap<String, String> dataStreams = ImmutableOpenMap.<String, String>builder()
.putAll(
StreamSupport.stream(state.metadata().findDataStreams(concreteIndices).spliterator(), false)
.collect(Collectors.toMap(k -> k.key, v -> v.value.getName()))
StreamSupport.stream(Spliterators.spliterator(state.metadata().findDataStreams(concreteIndices).entrySet(), 0), false)
.collect(Collectors.toMap(k -> k.getKey(), v -> v.getValue().getName()))
)
.build();
GetIndexRequest.Feature[] features = request.features();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ private static void enrichIndexAbstraction(
IndexAbstraction.Index index = (IndexAbstraction.Index) ia;

String[] aliasNames = StreamSupport.stream(
Spliterators.spliteratorUnknownSize(index.getWriteIndex().getAliases().keysIt(), 0),
Spliterators.spliteratorUnknownSize(index.getWriteIndex().getAliases().keySet().iterator(), 0),
false
).toArray(String[]::new);
Arrays.sort(aliasNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ static void checkNoDuplicatedAliasInIndexTemplate(
Locale.ROOT,
"Rollover alias [%s] can point to multiple indices, found duplicated alias [%s] in index template [%s]",
rolloverRequestAlias,
template.aliases().keys(),
template.aliases().keySet(),
template.name()
)
);
Expand Down
Loading

0 comments on commit 0156c55

Please sign in to comment.