-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Add support of getting a Java stream on ImmutableOpenMap #76921
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
arteam
merged 25 commits into
elastic:master
from
arteam:add-stream-to-immutable-openmap
Sep 8, 2021
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
a68f350
Add support of getting a Java stream on ImmutableOpenMap
arteam e55ffe0
Merge branch 'master' into add-stream-to-immutable-openmap
elasticmachine 490a13c
Merge branch 'master' into add-stream-to-immutable-openmap
arteam 10af100
Use fixed spliterator
arteam c581f9d
Make ImmutableOpenMap stream safe
arteam 7023f6c
stream->streamBuilder
arteam 195d207
Create spliterator on the fly by copying cursor to Map.Entry
arteam 658f92f
Add DISTINCT characteristic
arteam b5b82bd
Use Map.Entry instead of internal Entry with public fields
arteam 24587b5
Fix spotless warning
arteam c231712
Merge branch 'master' into add-stream-to-immutable-openmap
elasticmachine e3c6a48
Migrate to Map.Entry::getValue
arteam cc89ef3
Add a test for stream operations on a random map
arteam f2b200d
Fix checkstyle warnings
arteam 729a923
Use predictable Random instead of ThreadLocalRandom
arteam 74ec0b6
Merge branch 'master' into add-stream-to-immutable-openmap
elasticmachine d5217ff
Merge branch 'master' into add-stream-to-immutable-openmap
elasticmachine 3e8276f
Update server/src/test/java/org/elasticsearch/common/collect/Immutabl…
arteam 6fc0c5e
Use safe accumulator and randomize the limit
arteam 73a6a76
Checkstyle
arteam 1c6d1d2
Merge branch 'master' into add-stream-to-immutable-openmap
elasticmachine bcb5b5e
Merge branch 'master' into add-stream-to-immutable-openmap
elasticmachine 8bdd44c
Update server/src/test/java/org/elasticsearch/common/collect/Immutabl…
arteam 899f7b3
Create not only random numbers, but also a random amount of numbers
arteam 879da8b
Merge branch 'master' into add-stream-to-immutable-openmap
elasticmachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
server/src/test/java/org/elasticsearch/common/collect/ImmutableOpenMapTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.common.collect; | ||
|
||
import com.carrotsearch.hppc.cursors.ObjectObjectCursor; | ||
|
||
import org.elasticsearch.common.Randomness; | ||
import org.elasticsearch.core.Tuple; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.TreeMap; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class ImmutableOpenMapTests extends ESTestCase { | ||
|
||
ImmutableOpenMap<String, String> regionCurrencySymbols = ImmutableOpenMap.<String, String>builder() | ||
.fPut("Japan", "¥") | ||
.fPut("USA", "$") | ||
.fPut("EU", "€") | ||
.fPut("UK", "£") | ||
.fPut("Korea", "₩") | ||
.build(); | ||
|
||
public void testStreamOperationsAreSupported() { | ||
assertThat(regionCurrencySymbols.stream().filter(e -> e.getKey().startsWith("U")).map(Map.Entry::getValue) | ||
.collect(Collectors.toSet()), equalTo(Set.of("£", "$"))); | ||
} | ||
|
||
public void testSortedStream() { | ||
assertThat(regionCurrencySymbols.stream().sorted(Map.Entry.comparingByKey()).map(Map.Entry::getValue).collect(Collectors.toList()), | ||
equalTo(List.of("€", "¥", "₩", "£", "$"))); | ||
} | ||
|
||
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(); | ||
|
||
int limit = randomIntBetween(0, map.size()); | ||
Map<Long, List<String>> collectedViaStreams = map.stream() | ||
.filter(e -> e.getKey() > 0) | ||
.sorted(Map.Entry.comparingByKey()) | ||
.limit(limit) | ||
.collect(Collectors.groupingBy(e -> e.getKey() % 2, Collectors.mapping(Map.Entry::getValue, Collectors.toList()))); | ||
|
||
Map<Long, String> sortedMap = new TreeMap<>(); | ||
for (ObjectObjectCursor<Long, String> cursor : map) { | ||
if (cursor.key > 0) { | ||
sortedMap.put(cursor.key, cursor.value); | ||
} | ||
} | ||
int i = 0; | ||
Map<Long, List<String>> collectedIteratively = new HashMap<>(); | ||
for (Map.Entry<Long, String> e : sortedMap.entrySet()) { | ||
if (i++ >= limit) { | ||
break; | ||
} | ||
collectedIteratively.computeIfAbsent(e.getKey() % 2, k -> new ArrayList<>()).add(e.getValue()); | ||
} | ||
|
||
assertThat(collectedViaStreams, equalTo(collectedIteratively)); | ||
} | ||
|
||
public void testEmptyStreamWorks() { | ||
assertThat(ImmutableOpenMap.of().stream().count(), equalTo(0L)); | ||
} | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a randomized test too, generating the map input of varying sizes and verifying that streaming though it works and produces the right results. Perhaps also randomly make it sort and/or parallel.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will add a couple of random tests and check if something gets broken!