You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/07/31 17:53:48 UTC

[groovy] branch master updated: Support creating hash-table concurrently

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 2bf936a  Support creating hash-table concurrently
2bf936a is described below

commit 2bf936a9baee5280e2ddc2017bdad49b97ac2c76
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sun Aug 1 01:53:19 2021 +0800

    Support creating hash-table concurrently
---
 .../collection/runtime/QueryableCollection.java    | 27 +++++++++++++---------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
index cc5e7eb..6fe8a72 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
@@ -54,6 +54,7 @@ import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import java.util.function.BiFunction;
 import java.util.function.BiPredicate;
+import java.util.function.BinaryOperator;
 import java.util.function.Function;
 import java.util.function.Predicate;
 import java.util.stream.Collector;
@@ -156,17 +157,21 @@ class QueryableCollection<T> implements Queryable<T>, Serializable {
     }
 
     private static <U> Supplier<Map<Integer, List<Candidate<U>>>> createHashTableSupplier(Queryable<? extends U> queryable, Function<? super U, ?> fieldsExtractor2) {
-        return () -> queryable.stream()
-                .map(e -> new Candidate<U>(e, fieldsExtractor2.apply(e)))
-                .collect(
-                        Collectors.toMap(
-                                c -> hash(c.extracted),
-                                Bucket::singletonBucket,
-                                (oldBucket, newBucket) -> {
-                                    oldBucket.addAll(newBucket);
-                                    return oldBucket;
-                                }
-                        ));
+        return () -> {
+            Function<Candidate<U>, Integer> keyMapper = c -> hash(c.extracted);
+            Function<Candidate<U>, List<Candidate<U>>> valueMapper = Bucket::singletonBucket;
+            BinaryOperator<List<Candidate<U>>> mergeFunction = (oldBucket, newBucket) -> {
+                oldBucket.addAll(newBucket);
+                return oldBucket;
+            };
+            Collector<Candidate<U>, ?, ? extends Map<Integer, List<Candidate<U>>>> candidateMapCollector =
+                    isParallel() ? Collectors.toConcurrentMap(keyMapper, valueMapper, mergeFunction)
+                                 : Collectors.toMap(keyMapper, valueMapper, mergeFunction);
+            
+            return queryable.stream()
+                    .map(e -> new Candidate<U>(e, fieldsExtractor2.apply(e)))
+                    .collect(candidateMapCollector);
+        };
     }
 
     private static Integer hash(Object obj) {