Search in sources :

Example 1 with SnapshotIterator

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;
        }
    };
}
Also used : Entry(java.util.Map.Entry) SnapshotIterator(org.apache.geode.cache.snapshot.SnapshotIterator) SnapshotRecord(org.apache.geode.internal.cache.snapshot.SnapshotPacket.SnapshotRecord) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

Entry (java.util.Map.Entry)1 NoSuchElementException (java.util.NoSuchElementException)1 SnapshotIterator (org.apache.geode.cache.snapshot.SnapshotIterator)1 SnapshotRecord (org.apache.geode.internal.cache.snapshot.SnapshotPacket.SnapshotRecord)1