org.kairosdb.util.TournamentTree Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kairosdb Show documentation
Show all versions of kairosdb Show documentation
KairosDB is a fast distributed scalable time series abstraction on top of Cassandra.
/*
* 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 final TreeSet> m_treeSet;
private final Comparator m_comparator;
private int m_iteratorIndex = 0;
private final Order m_order;
public TournamentTree(final Comparator comparator, final Order order) {
m_comparator = comparator;
m_treeSet = new TreeSet>(new TreeComparator());
m_order = order;
}
//---------------------------------------------------------------------------
public void addIterator(final 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() {
final TreeValue value;
if (m_order == Order.ASC)
value = m_treeSet.pollFirst();
else
value = m_treeSet.pollLast();
if (value == null)
return (null);
final T ret = value.getValue();
if (value.getIterator().hasNext()) {
value.setValue(value.getIterator().next());
m_treeSet.add(value);
}
return (ret);
}
private static class TreeValue {
private final int m_iteratorNum;
private T m_value;
private final Iterator m_iterator;
public TreeValue(final Iterator iterator, final T value, final int iteratorNum) {
m_iterator = iterator;
m_value = value;
m_iteratorNum = iteratorNum;
}
public int getIteratorNum() {
return (m_iteratorNum);
}
public T getValue() {
return (m_value);
}
public void setValue(final T value) {
m_value = value;
}
public Iterator getIterator() {
return (m_iterator);
}
}
private class TreeComparator implements Comparator> {
public int compare(final TreeValue tv1, final TreeValue tv2) {
final int resp = m_comparator.compare(tv1.getValue(), tv2.getValue());
if (resp == 0)
return (tv1.getIteratorNum() - tv2.getIteratorNum());
else
return (resp);
}
}
}