org.iq80.leveldb.util.Level0Iterator Maven / Gradle / Ivy
package org.iq80.leveldb.util;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.Iterables;
import com.google.common.primitives.Ints;
import org.iq80.leveldb.impl.FileMetaData;
import org.iq80.leveldb.impl.InternalKey;
import org.iq80.leveldb.impl.SeekingIterator;
import org.iq80.leveldb.impl.TableCache;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
public final class Level0Iterator extends AbstractSeekingIterator implements InternalIterator
{
private final List inputs;
private final PriorityQueue priorityQueue;
private final Comparator comparator;
public Level0Iterator(TableCache tableCache, List files, Comparator comparator)
{
Builder builder = ImmutableList.builder();
for (FileMetaData file : files) {
builder.add(tableCache.newIterator(file));
}
this.inputs = builder.build();
this.comparator = comparator;
this.priorityQueue = new PriorityQueue(Iterables.size(inputs) + 1);
resetPriorityQueue(comparator);
}
public Level0Iterator(List inputs, Comparator comparator)
{
this.inputs = inputs;
this.comparator = comparator;
this.priorityQueue = new PriorityQueue(Iterables.size(inputs));
resetPriorityQueue(comparator);
}
@Override
protected void seekToFirstInternal()
{
for (InternalTableIterator input : inputs) {
input.seekToFirst();
}
resetPriorityQueue(comparator);
}
@Override
protected void seekInternal(InternalKey targetKey)
{
for (InternalTableIterator input : inputs) {
input.seek(targetKey);
}
resetPriorityQueue(comparator);
}
private void resetPriorityQueue(Comparator comparator)
{
int i = 0;
for (InternalTableIterator input : inputs) {
if (input.hasNext()) {
priorityQueue.add(new ComparableIterator(input, comparator, i++, input.next()));
}
}
}
@Override
protected Entry getNextElement()
{
Entry result = null;
ComparableIterator nextIterator = priorityQueue.poll();
if (nextIterator != null) {
result = nextIterator.next();
if (nextIterator.hasNext()) {
priorityQueue.add(nextIterator);
}
}
return result;
}
@Override
public String toString()
{
final StringBuilder sb = new StringBuilder();
sb.append("MergingIterator");
sb.append("{inputs=").append(Iterables.toString(inputs));
sb.append(", comparator=").append(comparator);
sb.append('}');
return sb.toString();
}
private static class ComparableIterator implements Iterator>, Comparable {
private final SeekingIterator iterator;
private final Comparator comparator;
private final int ordinal;
private Entry nextElement;
private ComparableIterator(SeekingIterator iterator, Comparator comparator, int ordinal, Entry nextElement)
{
this.iterator = iterator;
this.comparator = comparator;
this.ordinal = ordinal;
this.nextElement = nextElement;
}
@Override
public boolean hasNext()
{
return nextElement != null;
}
public Entry next()
{
if (nextElement == null) {
throw new NoSuchElementException();
}
Entry result = nextElement;
if (iterator.hasNext()) {
nextElement = iterator.next();
} else {
nextElement = null;
}
return result;
}
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ComparableIterator comparableIterator = (ComparableIterator) o;
if (ordinal != comparableIterator.ordinal) {
return false;
}
if (nextElement != null ? !nextElement.equals(comparableIterator.nextElement) : comparableIterator.nextElement != null) {
return false;
}
return true;
}
@Override
public int hashCode()
{
int result = ordinal;
result = 31 * result + (nextElement != null ? nextElement.hashCode() : 0);
return result;
}
@Override
public int compareTo(ComparableIterator that)
{
int result = comparator.compare(this.nextElement.getKey(), that.nextElement.getKey());
if (result == 0) {
result = Ints.compare(this.ordinal, that.ordinal);
}
return result;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy