in src/main/java/com/spotify/sparkey/SparkeyLogIterator.java [66:164]
public Iterator<SparkeyReader.Entry> iterator() {
try {
final BlockPositionedInputStream stream;
{
final InputStream stream2 = new BufferedInputStream(new FileInputStream(logFile), 128 * 1024);
Sparkey.incrOpenFiles();
stream2.skip(start);
stream = header.getCompressionTypeBackend().createBlockInput(stream2, header.getCompressionBlockSize(), start);
}
return new Iterator<SparkeyReader.Entry>() {
private long prevPos;
private long pos = start;
private final byte[] keyBuf = new byte[(int) header.getMaxKeyLen()];
private final Entry entry = new Entry(stream, keyBuf);
private boolean ready = false;
private int entryIndex;
private boolean closed = false;
public boolean hasNext() {
if (ready) {
return true;
}
try {
entry.stream.skipRemaining();
pos = stream.getBlockPosition();
if (pos >= end) {
closeStream();
return false;
}
if (pos == prevPos) {
entryIndex++;
} else {
entryIndex = 0;
}
prevPos = pos;
entry.position = pos;
entry.entryIndex = entryIndex;
entry.type = null;
entry.keyLen = 0;
entry.valueLen = 0;
int first;
try {
first = Util.readUnsignedVLQInt(stream);
} catch (EOFException e) {
closeStream();
return false;
}
int second = Util.readUnsignedVLQInt(stream);
long remaining;
if (first == 0) {
entry.type = SparkeyReader.Type.DELETE;
entry.keyLen = second;
entry.valueLen = 0;
remaining = 0;
} else {
entry.type = SparkeyReader.Type.PUT;
entry.keyLen = first - 1;
entry.valueLen = second;
remaining = entry.valueLen;
}
stream.read(keyBuf, 0, entry.keyLen);
entry.stream.setRemaining(remaining);
ready = true;
return true;
} catch (IOException e) {
closeStream();
throw new RuntimeException(e);
}
}
private void closeStream() {
if (closed) {
return;
}
closed = true;
Sparkey.decrOpenFiles();
Util.nonThrowingClose(stream);
}
public Entry next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
ready = false;
return entry;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
} catch (IOException e) {
throw new RuntimeException(e);
}
}