LevelDBKeyIterator extends LevelDBIteratorBase<byte[]> { public byte[] next() { levelDB.checkDatabaseOpen(); checkIteratorOpen(); PointerByReference resultLengthPointer = new PointerByReference(); PointerByReference resultPointer = LevelDBNative.leveldb_iter_key(iterator, resultLengthPointer); long resultLength; if (Native.POINTER_SIZE == 8) { resultLength = resultLengthPointer.getPointer().getLong(0); } else { resultLength = resultLengthPointer.getPointer().getInt(0); } byte[] key = resultPointer.getPointer().getByteArray(0, (int) resultLength); LevelDBNative.leveldb_iter_next(iterator); return key; } LevelDBKeyIterator(LevelDB levelDB, LevelDBReadOptions readOptions); byte[] next();  }
@Test public void iterate_over_keys() { 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(LevelDBKeyIterator iterator = new LevelDBKeyIterator(levelDB, readOptions)) { int loop = 0; while (iterator.hasNext()) { byte[] currentKey = iterator.next(); Assert.assertArrayEquals(key, currentKey); loop += 1; } Assert.assertEquals(1, loop); } } } } }
@Test public void seek_to_first() { try(LevelDBOptions options = new LevelDBOptions()) { options.setCreateIfMissing(true); try(LevelDB levelDB = new LevelDB(testFolder.getRoot().getAbsolutePath(), options)) { try(LevelDBWriteOptions writeOptions = new LevelDBWriteOptions()) { for (int i = 0; i < 10; i++) { byte[] bytes = {(byte) i}; levelDB.put(bytes, bytes, writeOptions); } } try(LevelDBReadOptions readOptions = new LevelDBReadOptions()) { try(LevelDBKeyIterator iterator = new LevelDBKeyIterator(levelDB, readOptions)) { while (iterator.hasNext()) { byte[] currentKey = iterator.next(); if (Arrays.equals(new byte[]{5}, currentKey)) { break; } } iterator.seekToFirst(); byte[] currentKey = iterator.next(); Assert.assertArrayEquals(new byte[]{0}, currentKey); } } } } }
@Test public void seek_to_last() { try(LevelDBOptions options = new LevelDBOptions()) { options.setCreateIfMissing(true); try(LevelDB levelDB = new LevelDB(testFolder.getRoot().getAbsolutePath(), options)) { try(LevelDBWriteOptions writeOptions = new LevelDBWriteOptions()) { for (int i = 0; i < 10; i++) { byte[] bytes = {(byte) i}; levelDB.put(bytes, bytes, writeOptions); } } try(LevelDBReadOptions readOptions = new LevelDBReadOptions()) { try(LevelDBKeyIterator iterator = new LevelDBKeyIterator(levelDB, readOptions)) { iterator.seekToLast(); iterator.next(); Assert.assertFalse(iterator.hasNext()); } } } } }
@Test public void seek_to_key() { try(LevelDBOptions options = new LevelDBOptions()) { options.setCreateIfMissing(true); try(LevelDB levelDB = new LevelDB(testFolder.getRoot().getAbsolutePath(), options)) { try(LevelDBWriteOptions writeOptions = new LevelDBWriteOptions()) { for (int i = 0; i < 10; i++) { byte[] bytes = {(byte) i}; levelDB.put(bytes, bytes, writeOptions); } } try(LevelDBReadOptions readOptions = new LevelDBReadOptions()) { try(LevelDBKeyIterator iterator = new LevelDBKeyIterator(levelDB, readOptions)) { iterator.seekToKey(new byte[]{5}); Assert.assertTrue(iterator.hasNext()); Assert.assertArrayEquals(new byte[]{5}, iterator.next()); } } } } }