Skip to content

Add support for getting a key set for ImmutableOpenMap #77897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
25c1b9e
Add support for getting a Java stream over ImmutableOpenMap keys
arteam Sep 16, 2021
1c8d236
Remove the cursor variable
arteam Sep 16, 2021
e1e4ec3
Merge branch 'master' into add-support-for-keys-stream-on-immutable-o…
elasticmachine Sep 17, 2021
44c0e15
Merge branch 'master' into add-support-for-keys-stream-on-immutable-o…
elasticmachine Sep 20, 2021
db534c4
Merge branch 'master' into add-support-for-keys-stream-on-immutable-o…
elasticmachine Sep 20, 2021
05b4e03
Merge branch 'master' into add-support-for-keys-stream-on-immutable-o…
elasticmachine Sep 20, 2021
bd0f708
Add tests for ImmutableOpenMapTests#keysStream
arteam Sep 21, 2021
0878240
Add more tests for ImmutableOpenMapTests#keysStream
arteam Sep 21, 2021
0a066bc
Merge branch 'master' into add-support-for-keys-stream-on-immutable-o…
elasticmachine Sep 21, 2021
0a78b57
Merge branch 'master' into add-support-for-keys-stream-on-immutable-o…
elasticmachine Sep 23, 2021
46f927b
Merge branch 'master' into add-support-for-keys-stream-on-immutable-o…
arteam Sep 24, 2021
96508fc
Use a method reference for ImmutableOpenMap::builder
arteam Sep 24, 2021
519e5e4
Fix Gradle integration with Intellij IDEA
arteam Sep 24, 2021
da6aa9d
Provide keySet instead of keyStream
arteam Sep 27, 2021
4bb2e75
Rename keysSet to keySet to be consistent with JUC
arteam Sep 27, 2021
9066ef6
Bring back indexMetadata.getAliases().keysIt().forEachRemaining(allAl…
arteam Sep 27, 2021
0e14f79
Merge branch 'master' into add-support-for-keys-stream-on-immutable-o…
elasticmachine Sep 27, 2021
ec96552
Revert "Bring back indexMetadata.getAliases().keysIt().forEachRemaini…
arteam Sep 27, 2021
b32388c
Bring back indexMetadata.getAliases().keysIt().forEachRemaining(allAl…
arteam Sep 27, 2021
dda4778
Fix testEmptyKeySetWorks check
arteam Sep 27, 2021
97e8677
Inline the keysIt call
arteam Sep 27, 2021
2a8def9
Override contains on AbstractSet
arteam Sep 27, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -80,12 +78,7 @@ protected ToXContent.Params getParams() {
}

protected static <T> void assertMapEquals(ImmutableOpenMap<String, T> expected, Map<String, T> actual) {
Set<String> expectedKeys = new HashSet<>();
Iterator<String> keysIt = expected.keysIt();
while (keysIt.hasNext()) {
expectedKeys.add(keysIt.next());
}

Set<String> expectedKeys = expected.keySet();
assertEquals(expectedKeys, actual.keySet());
for (String key : expectedKeys) {
assertEquals(expected.get(key), actual.get(key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
Expand Down Expand Up @@ -136,8 +135,7 @@ private static void checkSystemIndexAccess(GetAliasesRequest request, SystemIndi

List<String> netNewSystemIndices = new ArrayList<>();
List<String> systemIndicesNames = new ArrayList<>();
for (Iterator<String> it = aliasesMap.keysIt(); it.hasNext(); ) {
String indexName = it.next();
aliasesMap.keySet().forEach(indexName -> {
IndexMetadata index = state.metadata().index(indexName);
if (index != null && index.isSystem()) {
if (systemIndexAccessAllowPredicate.test(index) == false) {
Expand All @@ -148,7 +146,7 @@ private static void checkSystemIndexAccess(GetAliasesRequest request, SystemIndi
}
}
}
}
});
if (systemIndicesNames.isEmpty() == false) {
deprecationLogger.critical(DeprecationCategory.API, "open_system_index_access",
"this request accesses system indices: {}, but in a future major version, direct access to system " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.common.xcontent.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.util.concurrent.CountDown;
import org.elasticsearch.common.xcontent.ParseField;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.tasks.Task;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.RemoteClusterAware;
Expand All @@ -51,9 +51,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.SortedMap;
import java.util.Spliterators;
import java.util.TreeMap;
import java.util.stream.StreamSupport;

public class ResolveIndexAction extends ActionType<ResolveIndexAction.Response> {

Expand Down Expand Up @@ -540,10 +538,7 @@ private static void enrichIndexAbstraction(String indexAbstraction, SortedMap<St
case CONCRETE_INDEX:
IndexAbstraction.Index index = (IndexAbstraction.Index) ia;

String[] aliasNames = StreamSupport.stream(
Spliterators.spliteratorUnknownSize(index.getWriteIndex().getAliases().keysIt(), 0), false)
.toArray(String[]::new);
Arrays.sort(aliasNames);
String[] aliasNames = index.getWriteIndex().getAliases().keySet().stream().sorted().toArray(String[]::new);

List<String> attributes = new ArrayList<>();
attributes.add(index.getWriteIndex().getState() == IndexMetadata.State.OPEN ? "open" : "closed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import com.carrotsearch.hppc.procedures.ObjectObjectProcedure;

import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
Expand Down Expand Up @@ -136,6 +138,29 @@ public void remove() {
};
}

/**
* Returns a {@link Set} view of the keys contained in this map.
*/
public Set<KType> keySet() {
return new AbstractSet<>() {
@Override
public Iterator<KType> iterator() {
return keysIt();
}

@Override
public int size() {
return map.size();
}

@Override
@SuppressWarnings("unchecked")
public boolean contains(Object o) {
return map.containsKey((KType) o);
}
};
}

/**
* @return Returns a container with all values stored in this map.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
import org.apache.lucene.util.automaton.RegExp;
import org.elasticsearch.cluster.metadata.Metadata;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* An "associated index" is an index that is related to or derived from a system
Expand Down Expand Up @@ -107,14 +106,10 @@ static Automaton buildAutomaton(String pattern) {
*/
@Override
public List<String> getMatchingIndices(Metadata metadata) {
ArrayList<String> matchingIndices = new ArrayList<>();
metadata.indices().keysIt().forEachRemaining(indexName -> {
if (matchesIndexPattern(indexName)) {
matchingIndices.add(indexName);
}
});

return Collections.unmodifiableList(matchingIndices);
return metadata.indices().keySet()
.stream()
.filter(this::matchesIndexPattern)
.collect(Collectors.toUnmodifiableList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
import org.elasticsearch.cluster.metadata.DataStream;
import org.elasticsearch.cluster.metadata.Metadata;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static org.elasticsearch.indices.AssociatedIndexDescriptor.buildAutomaton;

Expand Down Expand Up @@ -92,14 +91,7 @@ public String getDataStreamName() {
* @return List of names of backing indices
*/
public List<String> getBackingIndexNames(Metadata metadata) {
ArrayList<String> matchingIndices = new ArrayList<>();
metadata.indices().keysIt().forEachRemaining(indexName -> {
if (this.characterRunAutomaton.run(indexName)) {
matchingIndices.add(indexName);
}
});

return Collections.unmodifiableList(matchingIndices);
return metadata.indices().keySet().stream().filter(this.characterRunAutomaton::run).collect(Collectors.toUnmodifiableList());
}

public String getDescription() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
* A system index descriptor describes one or more system indices. It can match a number of indices using
Expand Down Expand Up @@ -348,14 +349,10 @@ public boolean matchesIndexPattern(String index) {
*/
@Override
public List<String> getMatchingIndices(Metadata metadata) {
ArrayList<String> matchingIndices = new ArrayList<>();
metadata.indices().keysIt().forEachRemaining(indexName -> {
if (matchesIndexPattern(indexName)) {
matchingIndices.add(indexName);
}
});

return Collections.unmodifiableList(matchingIndices);
return metadata.indices().keySet()
.stream()
.filter(this::matchesIndexPattern)
.collect(Collectors.toUnmodifiableList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@

package org.elasticsearch.action.admin.indices.alias.get;

import org.elasticsearch.cluster.metadata.DataStreamTestHelper;
import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.elasticsearch.cluster.metadata.AliasMetadata.Builder;
import org.elasticsearch.cluster.metadata.DataStreamTestHelper;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.test.AbstractWireSerializingTestCase;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
Expand Down Expand Up @@ -53,12 +51,7 @@ private static ImmutableOpenMap<String, List<AliasMetadata>> mutateAliases(Immut
return builder.build();
}

Set<String> indices = new HashSet<>();
Iterator<String> keys = aliases.keysIt();
while (keys.hasNext()) {
indices.add(keys.next());
}

Set<String> indices = aliases.keySet();
List<String> indicesToBeModified = randomSubsetOf(randomIntBetween(1, indices.size()), indices);
ImmutableOpenMap.Builder<String, List<AliasMetadata>> builder = ImmutableOpenMap.builder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.cluster.node;

import com.carrotsearch.randomizedtesting.generators.RandomPicks;

import org.elasticsearch.Version;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.test.ESTestCase;
Expand Down Expand Up @@ -309,30 +310,22 @@ Set<String> matchingNodeIds(DiscoveryNodes nodes) {
}, MASTER_ELIGIBLE(DiscoveryNodeRole.MASTER_ROLE.roleName() + ":true") {
@Override
Set<String> matchingNodeIds(DiscoveryNodes nodes) {
Set<String> ids = new HashSet<>();
nodes.getMasterNodes().keysIt().forEachRemaining(ids::add);
return ids;
return nodes.getMasterNodes().keySet();
}
}, DATA(DiscoveryNodeRole.DATA_ROLE.roleName() + ":true") {
@Override
Set<String> matchingNodeIds(DiscoveryNodes nodes) {
Set<String> ids = new HashSet<>();
nodes.getDataNodes().keysIt().forEachRemaining(ids::add);
return ids;
return nodes.getDataNodes().keySet();
}
}, INGEST(DiscoveryNodeRole.INGEST_ROLE.roleName() + ":true") {
@Override
Set<String> matchingNodeIds(DiscoveryNodes nodes) {
Set<String> ids = new HashSet<>();
nodes.getIngestNodes().keysIt().forEachRemaining(ids::add);
return ids;
return nodes.getIngestNodes().keySet();
}
}, COORDINATING_ONLY(DiscoveryNode.COORDINATING_ONLY + ":true") {
@Override
Set<String> matchingNodeIds(DiscoveryNodes nodes) {
Set<String> ids = new HashSet<>();
nodes.getCoordinatingOnlyNodes().keysIt().forEachRemaining(ids::add);
return ids;
return nodes.getCoordinatingOnlyNodes().keySet();
}
}, CUSTOM_ATTRIBUTE("attr:value") {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.stream.Collectors;

import static org.hamcrest.Matchers.equalTo;
Expand All @@ -45,11 +47,7 @@ public void testSortedStream() {
}

public void testStreamOperationsOnRandomMap() {
ImmutableOpenMap<Long, String> map = Randomness.get().longs(randomIntBetween(1, 1000))
.mapToObj(e -> Tuple.tuple(e, randomAlphaOfLength(8)))
.collect(() -> ImmutableOpenMap.<Long, String>builder(), (builder, t) -> builder.fPut(t.v1(), t.v2()),
ImmutableOpenMap.Builder::putAll)
.build();
ImmutableOpenMap<Long, String> map = randomImmutableOpenMap();

int limit = randomIntBetween(0, map.size());
Map<Long, List<String>> collectedViaStreams = map.stream()
Expand Down Expand Up @@ -79,4 +77,54 @@ public void testStreamOperationsOnRandomMap() {
public void testEmptyStreamWorks() {
assertThat(ImmutableOpenMap.of().stream().count(), equalTo(0L));
}

public void testKeySetStreamOperationsAreSupported() {
assertThat(regionCurrencySymbols.keySet().stream().filter(e -> e.startsWith("U") == false).collect(Collectors.toSet()),
equalTo(Set.of("Japan", "EU", "Korea")));
}

public void testSortedKeysSet() {
assertThat(regionCurrencySymbols.keySet(),
equalTo(Set.of("EU", "Japan", "Korea", "UK", "USA")));
}

public void testStreamOperationsOnRandomMapKeys() {
ImmutableOpenMap<Long, String> map = randomImmutableOpenMap();

int limit = randomIntBetween(0, map.size());
List<Long> collectedViaStream = map.keySet()
.stream()
.filter(e -> e > 0)
.sorted()
.limit(limit)
.collect(Collectors.toList());

SortedSet<Long> positiveNumbers = new TreeSet<>();
for (ObjectObjectCursor<Long, String> cursor : map) {
if (cursor.key > 0) {
positiveNumbers.add(cursor.key);
}
}
int i = 0;
List<Long> collectedIteratively = new ArrayList<>();
for (Long l : positiveNumbers) {
if (i++ >= limit) {
break;
}
collectedIteratively.add(l);
}
assertThat(collectedViaStream, equalTo(collectedIteratively));
}

public void testEmptyKeySetWorks() {
assertThat(ImmutableOpenMap.of().keySet().size(), equalTo(0));
}

private static ImmutableOpenMap<Long, String> randomImmutableOpenMap() {
return Randomness.get().longs(randomIntBetween(1, 1000))
.mapToObj(e -> Tuple.tuple(e, randomAlphaOfLength(8)))
.collect(ImmutableOpenMap::<Long, String>builder, (builder, t) -> builder.fPut(t.v1(), t.v2()),
ImmutableOpenMap.Builder::putAll)
.build();
}
}
Loading