Ejemplo n.º 1
0
  private Page extractOutput() {
    // INVARIANT: pagesIndex contains the full grouped & sorted data for one or more partitions

    // Iterate through the positions sequentially until we have one full page
    while (!pageBuilder.isFull()) {
      if (partition == null || !partition.hasNext()) {
        int partitionStart = partition == null ? 0 : partition.getPartitionEnd();

        if (partitionStart >= pagesIndex.getPositionCount()) {
          // Finished all of the partitions in the current pagesIndex
          partition = null;
          pagesIndex.clear();

          // Try to extract more partitions from the pendingInput
          if (pendingInput != null && processPendingInput()) {
            partitionStart = 0;
          } else if (state == State.FINISHING) {
            state = State.FINISHED;
            // Output the remaining page if we have anything buffered
            if (!pageBuilder.isEmpty()) {
              Page page = pageBuilder.build();
              pageBuilder.reset();
              return page;
            }
            return null;
          } else {
            state = State.NEEDS_INPUT;
            return null;
          }
        }

        int partitionEnd = findGroupEnd(pagesIndex, unGroupedPartitionHashStrategy, partitionStart);
        partition =
            new WindowPartition(
                pagesIndex,
                partitionStart,
                partitionEnd,
                outputChannels,
                windowFunctions,
                frameInfo,
                peerGroupHashStrategy);
      }

      partition.processNextRow(pageBuilder);
    }

    Page page = pageBuilder.build();
    pageBuilder.reset();
    return page;
  }
    public ListenableFuture<?> flush(boolean force) {
      // add all full pages to output buffer
      List<ListenableFuture<?>> blockedFutures = new ArrayList<>();
      for (int partition = 0; partition < pageBuilders.size(); partition++) {
        PageBuilder partitionPageBuilder = pageBuilders.get(partition);
        if (!partitionPageBuilder.isEmpty() && (force || partitionPageBuilder.isFull())) {
          Page pagePartition = partitionPageBuilder.build();
          partitionPageBuilder.reset();

          blockedFutures.add(outputBuffer.enqueue(partition, pagePartition));
          pagesAdded.incrementAndGet();
          rowsAdded.addAndGet(pagePartition.getPositionCount());
        }
      }
      ListenableFuture<?> future = Futures.allAsList(blockedFutures);
      if (future.isDone()) {
        return NOT_BLOCKED;
      }
      return future;
    }