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/04/22 00:26:24 UTC

[groovy] branch master updated: Trivial refactoring: move factory method of `WindowImpl`

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 0ae4fd1  Trivial refactoring: move factory method of `WindowImpl`
0ae4fd1 is described below

commit 0ae4fd145b3253cbaa0dac6b4413c1c766f7fcda
Author: Daniel Sun <su...@apache.org>
AuthorDate: Thu Apr 22 08:25:54 2021 +0800

    Trivial refactoring: move factory method of `WindowImpl`
---
 .../collection/runtime/QueryableCollection.java    |  2 +-
 .../ginq/provider/collection/runtime/Window.java   | 47 +++++++++++++++++++++-
 .../collection/runtime/WindowDefinition.java       |  2 +-
 .../provider/collection/runtime/WindowImpl.java    | 35 ++--------------
 4 files changed, 51 insertions(+), 35 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 cf1e4fd..e13e106 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
@@ -602,7 +602,7 @@ class QueryableCollection<T> implements Queryable<T>, Serializable {
                 sortedPartitionId -> Partition.newInstance(partition.orderBy(composeOrders(windowDefinition)).toList())
         );
         
-        return WindowImpl.newInstance(currentRecord, sortedPartition, windowDefinition);
+        return Window.newInstance(currentRecord, sortedPartition, windowDefinition);
     }
 
     private static class PartitionCacheKey {
diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/Window.java b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/Window.java
index 5487098..dfcd00f 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/Window.java
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/Window.java
@@ -18,16 +18,61 @@
  */
 package org.apache.groovy.ginq.provider.collection.runtime;
 
+import groovy.lang.Tuple2;
+
 import java.math.BigDecimal;
+import java.util.Collections;
+import java.util.List;
 import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import static java.util.Collections.binarySearch;
+import static java.util.Comparator.comparing;
+import static org.apache.groovy.ginq.provider.collection.runtime.Queryable.from;
 
 /**
  * Represents window which stores elements used by window functions
  *
- * @param <T> the type of {@link Queryable} element
+ * @param <T> the type of {@link Window} element
  * @since 4.0.0
  */
 public interface Window<T> extends Queryable<T> {
+    /**
+     * Factory method to create {@link Window} instance
+     *
+     * @param currentRecord current record
+     * @param partition the partition where the window is constructed
+     * @param windowDefinition window definition
+     * @param <T> the type of {@link Window} element
+     * @param <U> the type of field to sort
+     * @return the {@link Window} instance
+     * @since 4.0.0
+     */
+    static <T, U extends Comparable<? super U>> Window<T> newInstance(Tuple2<T, Long> currentRecord, Partition<Tuple2<T, Long>> partition, WindowDefinition<T, U> windowDefinition) {
+        final List<Order<? super T, ? extends U>> orderList = windowDefinition.orderBy();
+        Order<? super T, ? extends U> order;
+        if (null != orderList && 1 == orderList.size()) {
+            order = orderList.get(0);
+        } else {
+            order = null;
+        }
+
+        List<Tuple2<T, Long>> listWithIndex = partition.toList();
+
+        int tmpIndex = null == orderList || orderList.isEmpty()
+                ? binarySearch(listWithIndex, currentRecord, comparing(Tuple2::getV2))
+                : binarySearch(listWithIndex, currentRecord, WindowImpl.makeComparator(WindowImpl.composeOrders(orderList)).thenComparing(Tuple2::getV2));
+        int index = tmpIndex >= 0 ? tmpIndex : -tmpIndex - 1;
+        U value = null == order ? null : order.getKeyExtractor().apply(currentRecord.getV1());
+
+        RowBound validRowBound = WindowImpl.getValidRowBound(windowDefinition, index, value, listWithIndex);
+        List<T> list = null == validRowBound ? Collections.emptyList()
+                : from(listWithIndex.stream().map(Tuple2::getV1).collect(Collectors.toList()))
+                .limit(validRowBound.getLower(), validRowBound.getUpper() - validRowBound.getLower() + 1)
+                .toList();
+
+        return new WindowImpl<>(currentRecord, index, value, list, order);
+    }
 
     /**
      * Returns row number in the window, similar to SQL's {@code row_number()}
diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowDefinition.java b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowDefinition.java
index 8f4ae99..f56cb14 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowDefinition.java
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowDefinition.java
@@ -25,7 +25,7 @@ import java.util.function.Function;
 /**
  * Represents window definition, which will define the result set to construct the window
  *
- * @param <T> the type of {@link Queryable} element
+ * @param <T> the type of {@link Window} element
  * @param <U> the type of field to sort
  * @since 4.0.0
  */
diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowImpl.java b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowImpl.java
index 26d256e..480c625 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowImpl.java
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/WindowImpl.java
@@ -23,15 +23,12 @@ import org.apache.groovy.util.ReversedList;
 
 import java.math.BigDecimal;
 import java.math.RoundingMode;
-import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 
 import static java.util.Collections.binarySearch;
-import static java.util.Comparator.comparing;
-import static org.apache.groovy.ginq.provider.collection.runtime.Queryable.from;
 import static org.codehaus.groovy.runtime.dgmimpl.NumberNumberPlus.plus;
 import static org.codehaus.groovy.runtime.typehandling.NumberMath.toBigDecimal;
 
@@ -44,33 +41,7 @@ import static org.codehaus.groovy.runtime.typehandling.NumberMath.toBigDecimal;
  */
 class WindowImpl<T, U extends Comparable<? super U>> extends QueryableCollection<T> implements Window<T> {
 
-    static <T, U extends Comparable<? super U>> Window<T> newInstance(Tuple2<T, Long> currentRecord, Partition<Tuple2<T, Long>> partition, WindowDefinition<T, U> windowDefinition) {
-        final List<Order<? super T, ? extends U>> orderList = windowDefinition.orderBy();
-        Order<? super T, ? extends U> order;
-        if (null != orderList && 1 == orderList.size()) {
-            order = orderList.get(0);
-        } else {
-            order = null;
-        }
-
-        List<Tuple2<T, Long>> listWithIndex = partition.toList();
-
-        int tmpIndex = null == orderList || orderList.isEmpty()
-                ? binarySearch(listWithIndex, currentRecord, comparing(Tuple2::getV2))
-                : binarySearch(listWithIndex, currentRecord, makeComparator(composeOrders(orderList)).thenComparing(Tuple2::getV2));
-        int index = tmpIndex >= 0 ? tmpIndex : -tmpIndex - 1;
-        U value = null == order ? null : order.getKeyExtractor().apply(currentRecord.getV1());
-
-        RowBound validRowBound = getValidRowBound(windowDefinition, index, value, listWithIndex);
-        List<T> list = null == validRowBound ? Collections.emptyList()
-                                  : from(listWithIndex.stream().map(Tuple2::getV1).collect(Collectors.toList()))
-                                      .limit(validRowBound.getLower(), validRowBound.getUpper() - validRowBound.getLower() + 1)
-                                      .toList();
-
-        return new WindowImpl<>(currentRecord, index, value, list, order);
-    }
-
-    private WindowImpl(Tuple2<T, Long> currentRecord, int index, U value, List<T> list, Order<? super T, ? extends U> order) {
+    WindowImpl(Tuple2<T, Long> currentRecord, int index, U value, List<T> list, Order<? super T, ? extends U> order) {
         super(list);
         this.currentRecord = currentRecord;
         this.order = order;
@@ -210,7 +181,7 @@ class WindowImpl<T, U extends Comparable<? super U>> extends QueryableCollection
         return null == upper || Long.MAX_VALUE == upper ? size - 1 : index + upper;
     }
 
-    private static <T, U extends Comparable<? super U>> RowBound getValidRowBound(WindowDefinition<T, U> windowDefinition, int index, U value, List<Tuple2<T, Long>> listWithIndex) {
+    static <T, U extends Comparable<? super U>> RowBound getValidRowBound(WindowDefinition<T, U> windowDefinition, int index, U value, List<Tuple2<T, Long>> listWithIndex) {
         int size = listWithIndex.size();
         long firstIndex = 0;
         long lastIndex = size - 1;
@@ -298,7 +269,7 @@ class WindowImpl<T, U extends Comparable<? super U>> extends QueryableCollection
         return valueIndex;
     }
 
-    private static <T, U extends Comparable<? super U>> List<Order<Tuple2<T, Long>, U>> composeOrders(List<Queryable.Order<? super T, ? extends U>> orderList) {
+    static <T, U extends Comparable<? super U>> List<Order<Tuple2<T, Long>, U>> composeOrders(List<Queryable.Order<? super T, ? extends U>> orderList) {
         return orderList.stream()
                 .map(order -> new Order<Tuple2<T, Long>, U>(t -> order.getKeyExtractor().apply(t.getV1()), order.isAsc(), order.isNullsLast()))
                 .collect(Collectors.toList());