com.questdb.iter.JournalIteratorImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of questdb-core Show documentation
Show all versions of questdb-core Show documentation
QuestDB is High Performance Time Series Database
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (C) 2014-2016 Appsicle
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*
******************************************************************************/
package com.questdb.iter;
import com.questdb.Journal;
import com.questdb.ex.JournalException;
import com.questdb.ex.JournalRuntimeException;
import com.questdb.misc.Rows;
import com.questdb.std.AbstractImmutableIterator;
import com.questdb.std.ObjList;
public class JournalIteratorImpl extends AbstractImmutableIterator implements JournalPeekingIterator {
private final ObjList ranges;
private final Journal journal;
private boolean hasNext = true;
private int currentIndex = 0;
private long currentRowID;
private long currentUpperBound;
private int currentPartitionID;
public JournalIteratorImpl(Journal journal, ObjList ranges) {
this.ranges = ranges;
this.journal = journal;
updateVariables();
hasNext = hasNext && currentRowID <= currentUpperBound;
}
@Override
public Journal getJournal() {
return journal;
}
@Override
public boolean hasNext() {
return hasNext;
}
@Override
public T next() {
try {
T result = journal.read(Rows.toRowID(currentPartitionID, currentRowID));
if (currentRowID < currentUpperBound) {
currentRowID++;
} else {
currentIndex++;
updateVariables();
}
return result;
} catch (JournalException e) {
throw new JournalRuntimeException("Error in iterator [%s]", e, this);
}
}
@Override
public boolean isEmpty() {
return ranges == null || ranges.size() == 0;
}
@Override
public T peekFirst() {
JournalIteratorRange w = ranges.get(0);
try {
return journal.read(Rows.toRowID(w.partitionID, w.lo));
} catch (JournalException e) {
throw new JournalRuntimeException("Error in iterator at first element", e);
}
}
@Override
public T peekLast() {
JournalIteratorRange w = ranges.getLast();
try {
return journal.read(Rows.toRowID(w.partitionID, w.hi));
} catch (JournalException e) {
throw new JournalRuntimeException("Error in iterator at last element", e);
}
}
@Override
public String toString() {
return "JournalIteratorImpl{" +
"currentRowID=" + currentRowID +
", currentUpperBound=" + currentUpperBound +
", currentPartitionID=" + currentPartitionID +
", currentIndex=" + currentIndex +
", journal=" + journal +
'}';
}
private void updateVariables() {
if (currentIndex < ranges.size()) {
JournalIteratorRange w = ranges.getQuick(currentIndex);
currentRowID = w.lo;
currentUpperBound = w.hi;
currentPartitionID = w.partitionID;
} else {
hasNext = false;
}
}
}