Java Code Examples for org.iq80.leveldb.DBIterator#peekNext()
The following examples show how to use
org.iq80.leveldb.DBIterator#peekNext() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EzLevelDbTable.java From ezdb with Apache License 2.0 | 4 votes |
public TableIterator<H, R, V> rangeReverse(final H hashKey) { final DBIterator iterator = db.iterator(); final Function<CheckKeysRequest, Boolean> checkKeys = new Function<CheckKeysRequest, Boolean>() { @Override public Boolean apply(CheckKeysRequest input) { return Util.compareKeys(hashKeyComparator, null, input.getKeyBytesFrom(), input.getPeekKey()) == 0; } }; final byte[] keyBytesFrom = Util.combine(hashKeySerde, rangeKeySerde, hashKey, null); TableIterator<H, R, V> emptyIterator = reverseSeekToLast(hashKey, null, null, keyBytesFrom, null, iterator, checkKeys); if (emptyIterator != null) { iterator.close(); return emptyIterator; } return new AutoClosingTableIterator<H, R, V>( new TableIterator<H, R, V>() { private boolean fixFirst = true;; @Override public boolean hasNext() { if (useFixFirst()) { return true; } return iterator.hasPrev() && checkKeys.apply(new CheckKeysRequest( hashKey, null, null, keyBytesFrom, null, iterator.peekPrev())); } private boolean useFixFirst() { if (fixFirst && iterator.hasNext()) { final Entry<byte[], byte[]> peekNext = iterator .peekNext(); if (peekNext != null) { if (checkKeys.apply(new CheckKeysRequest( hashKey, null, null, keyBytesFrom, null, peekNext))) { return true; } else { fixFirst = false; } } } return false; } @Override public TableRow<H, R, V> next() { if (useFixFirst()) { fixFirst = false; return new RawTableRow<H, R, V>( iterator.peekNext(), hashKeySerde, rangeKeySerde, valueSerde); } if(hasNext()) { return new RawTableRow<H, R, V>(iterator.prev(), hashKeySerde, rangeKeySerde, valueSerde); }else { throw new NoSuchElementException(); } } @Override public void remove() { if (useFixFirst()) { throw new UnsupportedOperationException( "Not possible on first result for now..."); } iterator.remove(); } @Override public void close() { try { iterator.close(); } catch (final Exception e) { throw new DbException(e); } } }); }
Example 2
Source File: EzLevelDbTable.java From ezdb with Apache License 2.0 | 4 votes |
public TableIterator<H, R, V> rangeReverse(final H hashKey, final R fromRangeKey) { if (fromRangeKey == null) { return rangeReverse(hashKey); } final DBIterator iterator = db.iterator(); final Function<CheckKeysRequest, Boolean> checkKeys = new Function<CheckKeysRequest, Boolean>() { @Override public Boolean apply(CheckKeysRequest input) { return Util.compareKeys(hashKeyComparator, null, input.getKeyBytesFrom(), input.getPeekKey()) == 0 && (input.getFromRangeKey() == null || Util .compareKeys(hashKeyComparator, rangeKeyComparator, input.getKeyBytesFrom(), input.getPeekKey()) >= 0); } }; final byte[] keyBytesFrom = Util.combine(hashKeySerde, rangeKeySerde, hashKey, fromRangeKey); iterator.seek(keyBytesFrom); if (!iterator.hasNext() || fromRangeKey == null) { byte[] keyBytesFromForSeekLast = Util.combine(hashKeySerde, rangeKeySerde, hashKey, null); TableIterator<H, R, V> emptyIterator = reverseSeekToLast(hashKey, null, null, keyBytesFromForSeekLast, null, iterator, checkKeys); if (emptyIterator != null) { iterator.close(); return emptyIterator; } } return new AutoClosingTableIterator<H, R, V>( new TableIterator<H, R, V>() { private boolean fixFirst = true; @Override public boolean hasNext() { if (useFixFirst()) { return true; } return iterator.hasPrev() && checkKeys.apply(new CheckKeysRequest( hashKey, fromRangeKey, null, keyBytesFrom, null, iterator.peekPrev())); } private boolean useFixFirst() { if (fixFirst && iterator.hasNext()) { final Entry<byte[], byte[]> peekNext = iterator .peekNext(); if (peekNext != null) { if (checkKeys.apply(new CheckKeysRequest( hashKey, fromRangeKey, null, keyBytesFrom, null, peekNext))) { return true; } else { fixFirst = false; } } } return false; } @Override public TableRow<H, R, V> next() { if (useFixFirst()) { fixFirst = false; return new RawTableRow<H, R, V>( iterator.peekNext(), hashKeySerde, rangeKeySerde, valueSerde); } if(hasNext()) { return new RawTableRow<H, R, V>(iterator.prev(), hashKeySerde, rangeKeySerde, valueSerde); }else { throw new NoSuchElementException(); } } @Override public void remove() { if (useFixFirst()) { throw new UnsupportedOperationException( "Not possible on first result for now..."); } iterator.remove(); } @Override public void close() { try { iterator.close(); } catch (final Exception e) { throw new DbException(e); } } }); }