LevelDBKeyValueIterator extends LevelDBIteratorBase<KeyValuePair> { public KeyValuePair next() { levelDB.checkDatabaseOpen(); checkIteratorOpen(); PointerByReference resultPointer; PointerByReference resultLengthPointer = new PointerByReference(); long resultLength; resultPointer = LevelDBNative.leveldb_iter_key(iterator, resultLengthPointer); if (Native.POINTER_SIZE == 8) { resultLength = resultLengthPointer.getPointer().getLong(0); } else { resultLength = resultLengthPointer.getPointer().getInt(0); } byte[] key = resultPointer.getPointer().getByteArray(0, (int) resultLength); resultPointer = LevelDBNative.leveldb_iter_value(iterator, resultLengthPointer); if (Native.POINTER_SIZE == 8) { resultLength = resultLengthPointer.getPointer().getLong(0); } else { resultLength = resultLengthPointer.getPointer().getInt(0); } byte[] value = resultPointer.getPointer().getByteArray(0, (int) resultLength); LevelDBNative.leveldb_iter_next(iterator); return new KeyValuePair(key, value); } LevelDBKeyValueIterator(LevelDB levelDB, LevelDBReadOptions readOptions); KeyValuePair next();  }
@Test public void iterate_over_keys_and_values() { try(LevelDBOptions options = new LevelDBOptions()) { options.setCreateIfMissing(true); try(LevelDB levelDB = new LevelDB(testFolder.getRoot().getAbsolutePath(), options)) { byte[] key = new byte[] {42}; byte[] value = new byte[] {43}; try(LevelDBWriteOptions writeOptions = new LevelDBWriteOptions()) { levelDB.put(key, value, writeOptions); } try(LevelDBReadOptions readOptions = new LevelDBReadOptions()) { try(LevelDBKeyValueIterator iterator = new LevelDBKeyValueIterator(levelDB, readOptions)) { int loop = 0; while (iterator.hasNext()) { KeyValuePair pair = iterator.next(); Assert.assertArrayEquals(key, pair.getKey()); Assert.assertArrayEquals(value, pair.getValue()); loop += 1; } Assert.assertEquals(1, loop); } } } } }