Java Code Examples for org.iq80.leveldb.DBIterator#remove()

The following examples show how to use org.iq80.leveldb.DBIterator#remove() . 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 vote down vote up
@Override
public TableIterator<H, R, V> range(H hashKey) {
	final DBIterator iterator = db.iterator();
	final byte[] keyBytesFrom = Util.combine(hashKeySerde, rangeKeySerde,
			hashKey, null);
	iterator.seek(keyBytesFrom);
	return new AutoClosingTableIterator<H, R, V>(
			new TableIterator<H, R, V>() {
				@Override
				public boolean hasNext() {
					return iterator.hasNext()
							&& Util.compareKeys(hashKeyComparator, null,
									keyBytesFrom, iterator.peekNext()
											.getKey()) == 0;
				}

				@Override
				public TableRow<H, R, V> next() {
					if(hasNext()) {
						return new RawTableRow<H, R, V>(iterator.next(),
								hashKeySerde, rangeKeySerde, valueSerde);
					}else {
						throw new NoSuchElementException();
					}
				}

				@Override
				public void remove() {
					iterator.remove();
				}

				@Override
				public void close() {
					try {
						iterator.close();
					} catch (Exception e) {
						throw new DbException(e);
					}
				}
			});
}
 
Example 2
Source File: EzLevelDbTable.java    From ezdb with Apache License 2.0 4 votes vote down vote up
@Override
public TableIterator<H, R, V> range(H hashKey, R fromRangeKey) {
	if (fromRangeKey == null) {
		return range(hashKey);
	}
	final DBIterator iterator = db.iterator();
	final byte[] keyBytesFrom = Util.combine(hashKeySerde, rangeKeySerde,
			hashKey, fromRangeKey);
	iterator.seek(keyBytesFrom);
	return new AutoClosingTableIterator<H, R, V>(
			new TableIterator<H, R, V>() {
				@Override
				public boolean hasNext() {
					return iterator.hasNext()
							&& Util.compareKeys(hashKeyComparator, null,
									keyBytesFrom, iterator.peekNext()
											.getKey()) == 0;
				}

				@Override
				public TableRow<H, R, V> next() {
					if(hasNext()) {
						return new RawTableRow<H, R, V>(iterator.next(),
								hashKeySerde, rangeKeySerde, valueSerde);
					}else {
						throw new NoSuchElementException();
					}
				}

				@Override
				public void remove() {
					iterator.remove();
				}

				@Override
				public void close() {
					try {
						iterator.close();
					} catch (Exception e) {
						throw new DbException(e);
					}
				}
			});
}
 
Example 3
Source File: EzLevelDbTable.java    From ezdb with Apache License 2.0 4 votes vote down vote up
@Override
public TableIterator<H, R, V> range(H hashKey, R fromRangeKey, R toRangeKey) {
	if (toRangeKey == null) {
		return range(hashKey, fromRangeKey);
	}
	final DBIterator iterator = db.iterator();
	final byte[] keyBytesFrom = Util.combine(hashKeySerde, rangeKeySerde,
			hashKey, fromRangeKey);
	final byte[] keyBytesTo = Util.combine(hashKeySerde, rangeKeySerde,
			hashKey, toRangeKey);
	iterator.seek(keyBytesFrom);
	return new AutoClosingTableIterator<H, R, V>(
			new TableIterator<H, R, V>() {
				@Override
				public boolean hasNext() {
					return iterator.hasNext()
							&& Util.compareKeys(hashKeyComparator,
									rangeKeyComparator, keyBytesTo,
									iterator.peekNext().getKey()) >= 0;
				}

				@Override
				public TableRow<H, R, V> next() {
					if(hasNext()) {
						return new RawTableRow<H, R, V>(iterator.next(),
								hashKeySerde, rangeKeySerde, valueSerde);
					}else {
						throw new NoSuchElementException();
					}
				}

				@Override
				public void remove() {
					iterator.remove();
				}

				@Override
				public void close() {
					try {
						iterator.close();
					} catch (Exception e) {
						throw new DbException(e);
					}
				}
			});
}
 
Example 4
Source File: EzLevelDbTable.java    From ezdb with Apache License 2.0 4 votes vote down vote up
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 5
Source File: EzLevelDbTable.java    From ezdb with Apache License 2.0 4 votes vote down vote up
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);
					}
				}
			});
}