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

org.kairosdb.util.TournamentTree Maven / Gradle / Ivy

Go to download

KairosDB is a fast distributed scalable time series abstraction on top of Cassandra.

There is a newer version: 1.2.28
Show newest version
/*
 * Copyright 2016 KairosDB Authors
 *
 *    Licensed 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 org.kairosdb.util;



import org.kairosdb.core.datastore.Order;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class TournamentTree
	{
	private static class TreeValue
		{
		private int m_iteratorNum;
		private T m_value;
		private Iterator m_iterator;

		public TreeValue(Iterator iterator, T value, int iteratorNum)
			{
			m_iterator = iterator;
			m_value = value;
			m_iteratorNum = iteratorNum;
			}

		public int getIteratorNum() { return (m_iteratorNum); }
		public void setValue(T value) { m_value = value; }
		public T getValue() { return (m_value); }
		public Iterator getIterator() { return (m_iterator); }
		}

	private class TreeComparator implements Comparator>
		{
		public int compare(TreeValue tv1, TreeValue tv2)
			{
			int resp = m_comparator.compare(tv1.getValue(), tv2.getValue());

			if (resp == 0)
				return (tv1.getIteratorNum() - tv2.getIteratorNum());
			else
				return (resp);
			}
		}

	//===========================================================================
	private TreeSet> m_treeSet;
	private Comparator m_comparator;
	private int m_iteratorIndex = 0;
	private Order m_order;

	public TournamentTree(Comparator comparator, Order order)
	{
		m_comparator = comparator;
		m_treeSet = new TreeSet>(new TreeComparator());
		m_order = order;
	}

	//---------------------------------------------------------------------------
	public void addIterator(Iterator iterator)
	{
		if (iterator.hasNext())
			m_treeSet.add(new TreeValue(iterator, iterator.next(), m_iteratorIndex ++));
	}

	//---------------------------------------------------------------------------
	public boolean hasNext()
	{
		return !m_treeSet.isEmpty();
	}

	//---------------------------------------------------------------------------
	public T nextElement()
	{
		TreeValue value;
		if (m_order == Order.ASC)
			value = m_treeSet.pollFirst();
		else
			value = m_treeSet.pollLast();

		if (value == null)
			return (null);

		T ret = value.getValue();

		if (value.getIterator().hasNext())
		{
			value.setValue(value.getIterator().next());
			m_treeSet.add(value);
		}

		return (ret);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy