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

io.druid.query.select.SelectResultValueBuilder 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.query.select;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.MinMaxPriorityQueue;
import com.google.common.collect.Queues;
import com.google.common.primitives.Longs;
import com.metamx.common.guava.Comparators;
import io.druid.query.Result;
import org.joda.time.DateTime;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Queue;

/**
 */
public class SelectResultValueBuilder
{
  private static final Comparator comparator = new Comparator()
  {
    @Override
    public int compare(EventHolder o1, EventHolder o2)
    {
      int retVal = Longs.compare(o1.getTimestamp().getMillis(), o2.getTimestamp().getMillis());

      if (retVal == 0) {
        retVal = o1.getSegmentId().compareTo(o2.getSegmentId());
      }

      if (retVal == 0) {
        retVal = Integer.compare(o1.getOffset(), o2.getOffset());
      }

      return retVal;
    }
  };

  private final DateTime timestamp;
  private final PagingSpec pagingSpec;

  private Queue pQueue = null;

  public SelectResultValueBuilder(
      DateTime timestamp,
      PagingSpec pagingSpec,
      boolean descending
  )
  {
    this.timestamp = timestamp;
    this.pagingSpec = pagingSpec;

    instantiatePQueue(pagingSpec.getThreshold(), descending ? Comparators.inverse(comparator) : comparator);
  }

  public void addEntry(
      EventHolder event
  )
  {
    pQueue.add(event);
  }

  public Result build()
  {
    // Pull out top aggregated values
    List values = Lists.newArrayListWithCapacity(pQueue.size());
    Map pagingIdentifiers = Maps.newLinkedHashMap();
    while (!pQueue.isEmpty()) {
      EventHolder event = pQueue.remove();
      pagingIdentifiers.put(event.getSegmentId(), event.getOffset());
      values.add(event);
    }

    if (pagingIdentifiers.isEmpty()) {
      pagingIdentifiers.putAll(pagingSpec.getPagingIdentifiers());
    }

    return new Result(
        timestamp,
        new SelectResultValue(pagingIdentifiers, values)
    );
  }

  private void instantiatePQueue(int threshold, final Comparator comparator)
  {
    this.pQueue = threshold > 0
                  ? MinMaxPriorityQueue.orderedBy(comparator).maximumSize(threshold).create()
                  : Queues.newArrayDeque();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy