瀏覽代碼

新增SimpleQuery是否并行的重载,默认串行流以节省内存开销

achao 3 年之前
父節點
當前提交
ce637f06dc

+ 117 - 66
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/toolkit/SimpleQuery.java

@@ -1,6 +1,7 @@
 package com.baomidou.mybatisplus.extension.toolkit;
 package com.baomidou.mybatisplus.extension.toolkit;
 
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
 import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
 import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
 import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
 
 
@@ -22,20 +23,6 @@ public class SimpleQuery {
         /* Do not new me! */
         /* Do not new me! */
     }
     }
 
 
-    /**
-     * 是否并行流
-     */
-    public static boolean isParallel = true;
-
-    /**
-     * 切换是串行流还是并行流
-     *
-     * @param isParallel 是否并行流 true为并行流 false为串行流
-     */
-    public static void setIsParallel(boolean isParallel) {
-        SimpleQuery.isParallel = isParallel;
-    }
-
     /**
     /**
      * 通过lambda获取Class
      * 通过lambda获取Class
      *
      *
@@ -49,14 +36,7 @@ public class SimpleQuery {
     }
     }
 
 
     /**
     /**
-     * 传入Wrappers和key,从数据库中根据条件查询出对应的列表,封装成Map
-     *
-     * @param wrapper   条件构造器
-     * @param sFunction key
-     * @param peeks     封装成map时可能需要的后续操作,不需要可以不传
-     * @param <E>       实体类型
-     * @param <A>       实体中的属性类型
-     * @return Map<实体中的属性, 实体>
+     * ignore
      */
      */
     @SafeVarargs
     @SafeVarargs
     public static <E, A> Map<A, E> keyMap(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, Consumer<E>... peeks) {
     public static <E, A> Map<A, E> keyMap(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, Consumer<E>... peeks) {
@@ -66,16 +46,23 @@ public class SimpleQuery {
     /**
     /**
      * 传入Wrappers和key,从数据库中根据条件查询出对应的列表,封装成Map
      * 传入Wrappers和key,从数据库中根据条件查询出对应的列表,封装成Map
      *
      *
-     * @param wrapper   条件构造器
-     * @param keyFunc   key
-     * @param valueFunc value
-     * @param peeks     封装成map时可能需要的后续操作,不需要可以不传
-     * @param <E>       实体类型
-     * @param <A>       实体中的属性类型
-     * @param <P>       实体中的属性类型
+     * @param wrapper    条件构造器
+     * @param sFunction  key
+     * @param isParallel 是否并行流
+     * @param peeks      封装成map时可能需要的后续操作,不需要可以不传
+     * @param <E>        实体类型
+     * @param <A>        实体中的属性类型
      * @return Map<实体中的属性, 实体>
      * @return Map<实体中的属性, 实体>
      */
      */
     @SafeVarargs
     @SafeVarargs
+    public static <E, A> Map<A, E> keyMap(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, boolean isParallel, Consumer<E>... peeks) {
+        return list2Map(SqlHelper.getMapper(getType(sFunction)).selectList(wrapper), sFunction, Function.identity(), isParallel, peeks);
+    }
+
+    /**
+     * ignore
+     */
+    @SafeVarargs
     public static <E, A, P> Map<A, P> map(LambdaQueryWrapper<E> wrapper, SFunction<E, A> keyFunc, SFunction<E, P> valueFunc, Consumer<E>... peeks) {
     public static <E, A, P> Map<A, P> map(LambdaQueryWrapper<E> wrapper, SFunction<E, A> keyFunc, SFunction<E, P> valueFunc, Consumer<E>... peeks) {
         return list2Map(SqlHelper.getMapper(getType(keyFunc)).selectList(wrapper), keyFunc, valueFunc, peeks);
         return list2Map(SqlHelper.getMapper(getType(keyFunc)).selectList(wrapper), keyFunc, valueFunc, peeks);
     }
     }
@@ -83,62 +70,113 @@ public class SimpleQuery {
     /**
     /**
      * 传入Wrappers和key,从数据库中根据条件查询出对应的列表,封装成Map
      * 传入Wrappers和key,从数据库中根据条件查询出对应的列表,封装成Map
      *
      *
-     * @param wrapper   条件构造器
-     * @param sFunction 分组依据
-     * @param peeks     后续操作
-     * @param <E>       实体类型
-     * @param <A>       实体中的属性类型
-     * @return Map<实体中的属性, List < 实体>>
+     * @param wrapper    条件构造器
+     * @param keyFunc    key
+     * @param valueFunc  value
+     * @param isParallel 是否并行流
+     * @param peeks      封装成map时可能需要的后续操作,不需要可以不传
+     * @param <E>        实体类型
+     * @param <A>        实体中的属性类型
+     * @param <P>        实体中的属性类型
+     * @return Map<实体中的属性, 实体>
+     */
+    @SafeVarargs
+    public static <E, A, P> Map<A, P> map(LambdaQueryWrapper<E> wrapper, SFunction<E, A> keyFunc, SFunction<E, P> valueFunc, boolean isParallel, Consumer<E>... peeks) {
+        return list2Map(SqlHelper.getMapper(getType(keyFunc)).selectList(wrapper), keyFunc, valueFunc, isParallel, peeks);
+    }
+
+    /**
+     * ignore
      */
      */
     @SafeVarargs
     @SafeVarargs
     public static <E, A> Map<A, List<E>> group(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, Consumer<E>... peeks) {
     public static <E, A> Map<A, List<E>> group(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, Consumer<E>... peeks) {
         return listGroupBy(SqlHelper.getMapper(getType(sFunction)).selectList(wrapper), sFunction, peeks);
         return listGroupBy(SqlHelper.getMapper(getType(sFunction)).selectList(wrapper), sFunction, peeks);
     }
     }
 
 
+    /**
+     * 传入Wrappers和key,从数据库中根据条件查询出对应的列表,封装成Map
+     *
+     * @param wrapper    条件构造器
+     * @param sFunction  分组依据
+     * @param isParallel 是否并行流
+     * @param peeks      后续操作
+     * @param <E>        实体类型
+     * @param <A>        实体中的属性类型
+     * @return Map<实体中的属性, List < 实体>>
+     */
+    @SafeVarargs
+    public static <E, A> Map<A, List<E>> group(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, boolean isParallel, Consumer<E>... peeks) {
+        return listGroupBy(SqlHelper.getMapper(getType(sFunction)).selectList(wrapper), sFunction, isParallel, peeks);
+    }
+
+    /**
+     * ignore
+     */
+    @SafeVarargs
+    public static <E, A> List<A> list(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, Consumer<E>... peeks) {
+        return list2List(SqlHelper.getMapper(getType(sFunction)).selectList(wrapper), sFunction, peeks);
+    }
+
     /**
     /**
      * 传入wrappers和需要的某一列,从数据中根据条件查询出对应的列,转换成list
      * 传入wrappers和需要的某一列,从数据中根据条件查询出对应的列,转换成list
      *
      *
-     * @param wrapper   条件构造器
-     * @param sFunction 需要的列
-     * @param peeks     后续操作
+     * @param wrapper    条件构造器
+     * @param sFunction  需要的列
+     * @param isParallel 是否并行流
+     * @param peeks      后续操作
      * @return java.util.List<A>
      * @return java.util.List<A>
-     * @author <achao1441470436@gmail.com>
      * @since 2021/11/9 17:59
      * @since 2021/11/9 17:59
      */
      */
     @SafeVarargs
     @SafeVarargs
-    public static <E, A> List<A> list(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, Consumer<E>... peeks) {
-        return list2List(SqlHelper.getMapper(getType(sFunction)).selectList(wrapper), sFunction, peeks);
+    public static <E, A> List<A> list(LambdaQueryWrapper<E> wrapper, SFunction<E, A> sFunction, boolean isParallel, Consumer<E>... peeks) {
+        return list2List(SqlHelper.getMapper(getType(sFunction)).selectList(wrapper), sFunction, isParallel, peeks);
     }
     }
 
 
+    /**
+     * ignore
+     */
+    @SafeVarargs
+    public static <A, E> List<A> list2List(List<E> list, SFunction<E, A> sFunction, Consumer<E>... peeks) {
+        return list2List(list, sFunction, false, peeks);
+    }
 
 
     /**
     /**
      * 对list进行map、peek操作
      * 对list进行map、peek操作
      *
      *
-     * @param list      数据
-     * @param sFunction 需要的列
-     * @param peeks     后续操作
+     * @param list       数据
+     * @param sFunction  需要的列
+     * @param isParallel 是否并行流
+     * @param peeks      后续操作
      * @return java.util.List<A>
      * @return java.util.List<A>
-     * @author <achao1441470436@gmail.com>
      * @since 2021/11/9 18:01
      * @since 2021/11/9 18:01
      */
      */
     @SafeVarargs
     @SafeVarargs
-    public static <A, E> List<A> list2List(List<E> list, SFunction<E, A> sFunction, Consumer<E>... peeks) {
-        return Stream.of(peeks).reduce(list.parallelStream(), Stream::peek, Stream::concat).map(sFunction).collect(Collectors.toList());
+    public static <A, E> List<A> list2List(List<E> list, SFunction<E, A> sFunction, boolean isParallel, Consumer<E>... peeks) {
+        return peekStream(list, isParallel, peeks).map(sFunction).collect(Collectors.toList());
+    }
+
+    /**
+     * ignore
+     */
+    @SafeVarargs
+    public static <A, E> Map<A, List<E>> listGroupBy(List<E> list, SFunction<E, A> sFunction, Consumer<E>... peeks) {
+        return listGroupBy(list, sFunction, false, peeks);
     }
     }
 
 
     /**
     /**
      * 对list进行groupBy操作
      * 对list进行groupBy操作
      *
      *
-     * @param list      数据
-     * @param sFunction 分组的key,依据
-     * @param peeks     封装成map时可能需要的后续操作,不需要可以不传
-     * @param <E>       实体类型
-     * @param <A>       实体中的属性类型
+     * @param list       数据
+     * @param sFunction  分组的key,依据
+     * @param isParallel 是否并行流
+     * @param peeks      封装成map时可能需要的后续操作,不需要可以不传
+     * @param <E>        实体类型
+     * @param <A>        实体中的属性类型
      * @return Map<实体中的属性, List < 实体>>
      * @return Map<实体中的属性, List < 实体>>
      */
      */
     @SafeVarargs
     @SafeVarargs
-    public static <A, E> Map<A, List<E>> listGroupBy(List<E> list, SFunction<E, A> sFunction, Consumer<E>... peeks) {
-        return peekStream(list, peeks).collect(HashMap::new, (m, v) -> {
+    public static <A, E> Map<A, List<E>> listGroupBy(List<E> list, SFunction<E, A> sFunction, boolean isParallel, Consumer<E>... peeks) {
+        return peekStream(list, isParallel, peeks).collect(HashMap::new, (m, v) -> {
             A key = Optional.ofNullable(v).map(sFunction).orElse(null);
             A key = Optional.ofNullable(v).map(sFunction).orElse(null);
             List<E> values = m.getOrDefault(key, new ArrayList<>(list.size()));
             List<E> values = m.getOrDefault(key, new ArrayList<>(list.size()));
             values.add(v);
             values.add(v);
@@ -151,32 +189,45 @@ public class SimpleQuery {
     }
     }
 
 
 
 
+    /**
+     * ignore
+     */
+    @SafeVarargs
+    public static <E, A, P> Map<A, P> list2Map(List<E> list, SFunction<E, A> keyFunc, Function<E, P> valueFunc, Consumer<E>... peeks) {
+        return list2Map(list, keyFunc, valueFunc, false, peeks);
+    }
+
     /**
     /**
      * list转换为map
      * list转换为map
      *
      *
-     * @param <E>     实体类型
-     * @param <A>     实体中的属性类型
-     * @param <P>     实体中的属性类型
-     * @param list    数据
-     * @param keyFunc key
-     * @param peeks   封装成map时可能需要的后续操作,不需要可以不传
+     * @param <E>        实体类型
+     * @param <A>        实体中的属性类型
+     * @param <P>        实体中的属性类型
+     * @param list       数据
+     * @param keyFunc    key
+     * @param isParallel 是否并行流
+     * @param peeks      封装成map时可能需要的后续操作,不需要可以不传
      * @return Map<实体中的属性, 实体>
      * @return Map<实体中的属性, 实体>
      */
      */
     @SafeVarargs
     @SafeVarargs
-    public static <E, A, P> Map<A, P> list2Map(List<E> list, SFunction<E, A> keyFunc, Function<E, P> valueFunc, Consumer<E>... peeks) {
-        return peekStream(list, peeks).collect(HashMap::new, (m, v) -> m.put(keyFunc.apply(v), valueFunc.apply(v)), HashMap::putAll);
+    public static <E, A, P> Map<A, P> list2Map(List<E> list, SFunction<E, A> keyFunc, Function<E, P> valueFunc, boolean isParallel, Consumer<E>... peeks) {
+        return peekStream(list, isParallel, peeks).collect(HashMap::new, (m, v) -> m.put(keyFunc.apply(v), valueFunc.apply(v)), HashMap::putAll);
     }
     }
 
 
     /**
     /**
      * 将list转为Stream流,然后再叠加peek操作
      * 将list转为Stream流,然后再叠加peek操作
      *
      *
-     * @param list  数据
-     * @param peeks 叠加的peek操作
-     * @param <E>   数据元素类型
+     * @param list       数据
+     * @param isParallel 是否并行流
+     * @param peeks      叠加的peek操作
+     * @param <E>        数据元素类型
      * @return 转换后的流
      * @return 转换后的流
      */
      */
     @SafeVarargs
     @SafeVarargs
-    public static <E> Stream<E> peekStream(List<E> list, Consumer<E>... peeks) {
+    public static <E> Stream<E> peekStream(List<E> list, boolean isParallel, Consumer<E>... peeks) {
+        if (CollectionUtils.isEmpty(list)) {
+            return Stream.empty();
+        }
         return Stream.of(peeks).reduce(StreamSupport.stream(list.spliterator(), isParallel), Stream::peek, Stream::concat);
         return Stream.of(peeks).reduce(StreamSupport.stream(list.spliterator(), isParallel), Stream::peek, Stream::concat);
     }
     }