All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.druid.java.util.common.guava.MergeSequence Maven / Gradle / Ivy

There is a newer version: 0.12.3
Show newest version
/*
 * Licensed to Metamarkets Group Inc. (Metamarkets) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Metamarkets licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package io.druid.java.util.common.guava;

import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Ordering;
import com.google.common.io.Closer;

import java.io.IOException;
import java.util.PriorityQueue;

/**
 */
public class MergeSequence extends YieldingSequenceBase
{
  private final Ordering ordering;
  private final Sequence> baseSequences;

  public MergeSequence(
      Ordering ordering,
      Sequence> baseSequences
  )
  {
    this.ordering = ordering;
    this.baseSequences = baseSequences;
  }

  @Override
  public  Yielder toYielder(OutType initValue, YieldingAccumulator accumulator)
  {
    PriorityQueue> pQueue = new PriorityQueue<>(
        32,
        ordering.onResultOf(
            new Function, T>()
            {
              @Override
              public T apply(Yielder input)
              {
                return input.get();
              }
            }
        )
    );

    pQueue = baseSequences.accumulate(
        pQueue,
        new Accumulator>, Sequence>()
        {
          @Override
          public PriorityQueue> accumulate(PriorityQueue> queue, Sequence in)
          {
            final Yielder yielder = in.toYielder(
                null,
                new YieldingAccumulator()
                {
                  @Override
                  public T accumulate(T accumulated, T in)
                  {
                    yield();
                    return in;
                  }
                }
            );

            if (!yielder.isDone()) {
              queue.add(yielder);
            } else {
              try {
                yielder.close();
              }
              catch (IOException e) {
                throw Throwables.propagate(e);
              }
            }

            return queue;
          }
        }
    );

    return makeYielder(pQueue, initValue, accumulator);
  }

  private  Yielder makeYielder(
      final PriorityQueue> pQueue,
      OutType initVal,
      final YieldingAccumulator accumulator
  )
  {
    OutType retVal = initVal;
    while (!accumulator.yielded() && !pQueue.isEmpty()) {
      Yielder yielder = pQueue.remove();
      retVal = accumulator.accumulate(retVal, yielder.get());
      yielder = yielder.next(null);
      if (yielder.isDone()) {
        try {
          yielder.close();
        }
        catch (IOException e) {
          throw Throwables.propagate(e);
        }
      } else {
        pQueue.add(yielder);
      }
    }

    if (pQueue.isEmpty() && !accumulator.yielded()) {
      return Yielders.done(retVal, null);
    }

    final OutType yieldVal = retVal;
    return new Yielder()
    {
      @Override
      public OutType get()
      {
        return yieldVal;
      }

      @Override
      public Yielder next(OutType initValue)
      {
        accumulator.reset();
        return makeYielder(pQueue, initValue, accumulator);
      }

      @Override
      public boolean isDone()
      {
        return false;
      }

      @Override
      public void close() throws IOException
      {
        Closer closer = Closer.create();
        while (!pQueue.isEmpty()) {
          closer.register(pQueue.remove());
        }
        closer.close();
      }
    };
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy