use of org.apache.geode.cache.snapshot.SnapshotIterator in project geode by apache.
the class GFSnapshot method read.
/**
* Reads a snapshot file.
*
* @param <K> the key type
* @param <V> the value type
* @param snapshot the snapshot file
* @return the snapshot iterator
*
* @throws IOException error reading the snapshot file
* @throws ClassNotFoundException unable to deserialize entry
*/
public static <K, V> SnapshotIterator<K, V> read(final File snapshot) throws IOException, ClassNotFoundException {
return new SnapshotIterator<K, V>() {
GFSnapshotImporter in = new GFSnapshotImporter(snapshot);
private boolean foundNext;
private Entry<K, V> next;
@Override
public boolean hasNext() throws IOException, ClassNotFoundException {
if (!foundNext) {
return moveNext();
}
return true;
}
@Override
public Entry<K, V> next() throws IOException, ClassNotFoundException {
if (!foundNext && !moveNext()) {
throw new NoSuchElementException();
}
Entry<K, V> result = next;
foundNext = false;
next = null;
return result;
}
@Override
public void close() throws IOException {
in.close();
}
private boolean moveNext() throws IOException, ClassNotFoundException {
SnapshotRecord record;
while ((record = in.readSnapshotRecord()) != null) {
foundNext = true;
final K key = record.getKeyObject();
final V value = record.getValueObject();
next = new Entry<K, V>() {
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
throw new UnsupportedOperationException();
}
};
return true;
}
close();
return false;
}
};
}
Aggregations