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

The following examples show how to use org.iq80.leveldb.DBIterator#seek() . 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: LevelDBHandler.java    From julongchain with Apache License 2.0 5 votes vote down vote up
/**
 * 根据给出的开始、结束Key遍历
 */
@Override
public Iterator<Map.Entry<byte[], byte[]>> getIterator(byte[] startKey) throws LevelDBException {
    LevelDB db = LevelDBUtil.getDB(dbName);
    DBIterator dbItr = LevelDBUtil.getIterator(db);
    if(startKey != null){
        dbItr.seek(startKey);
    }
    return dbItr;
}
 
Example 2
Source File: EzLevelDbTable.java    From ezdb with Apache License 2.0 5 votes vote down vote up
private TableIterator<H, R, V> reverseSeekToLast(H hashKey, R fromRangeKey,
		R toRangeKey, final byte[] keyBytesFrom, byte[] keyBytesTo,
		final DBIterator iterator,
		Function<CheckKeysRequest, Boolean> checkKeys) {
	iterator.seek(keyBytesFrom);
	Entry<byte[], byte[]> last = null;
	while (iterator.hasNext()
			&& checkKeys.apply(new CheckKeysRequest(hashKey, fromRangeKey,
					toRangeKey, keyBytesFrom, keyBytesTo, iterator
							.peekNext()))) {
		last = iterator.next();
	}
	// if there is no last one, there is nothing at all in the table
	if (last == null) {
		return new TableIterator<H, R, V>() {

			@Override
			public boolean hasNext() {
				return false;
			}

			@Override
			public TableRow<H, R, V> next() {
				throw new NoSuchElementException();
			}

			@Override
			public void remove() {
				throw new NoSuchElementException();
			}

			@Override
			public void close() {
			}
		};
	}
	// since last has been found, seek again for that one
	iterator.seek(last.getKey());
	return null;
}
 
Example 3
Source File: StandaloneStoreClient.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
@Override
public long delete(WriteToken token, Metadata metadata, long start, long end) throws IOException {
  
  //
  // Regen classId/labelsId
  //
  
  // 128BITS
  metadata.setLabelsId(GTSHelper.labelsId(this.keystore.getKey(KeyStore.SIPHASH_LABELS), metadata.getLabels()));
  metadata.setClassId(GTSHelper.classId(this.keystore.getKey(KeyStore.SIPHASH_CLASS), metadata.getName()));

  //
  // Retrieve an iterator
  //
  
  DBIterator iterator = this.db.iterator();
  //
  // Seek the most recent key
  //
  
  // 128BITS
  byte[] bend = new byte[Constants.HBASE_RAW_DATA_KEY_PREFIX.length + 8 + 8 + 8];
  ByteBuffer bb = ByteBuffer.wrap(bend).order(ByteOrder.BIG_ENDIAN);
  bb.put(Constants.HBASE_RAW_DATA_KEY_PREFIX);
  bb.putLong(metadata.getClassId());
  bb.putLong(metadata.getLabelsId());
  bb.putLong(Long.MAX_VALUE - end);

  iterator.seek(bend);
  
  byte[] bstart = new byte[bend.length];
  bb = ByteBuffer.wrap(bstart).order(ByteOrder.BIG_ENDIAN);
  bb.put(Constants.HBASE_RAW_DATA_KEY_PREFIX);
  bb.putLong(metadata.getClassId());
  bb.putLong(metadata.getLabelsId());
  bb.putLong(Long.MAX_VALUE - start);
  
  //
  // Scan the iterator, deleting keys if they are between start and end
  //
  
  long count = 0L;
  
  WriteBatch batch = this.db.createWriteBatch();
  int batchsize = 0;
  
  WriteOptions options = new WriteOptions().sync(1.0 == syncrate);
              
  while (iterator.hasNext()) {
    Entry<byte[],byte[]> entry = iterator.next();

    if (Bytes.compareTo(entry.getKey(), bend) >= 0 && Bytes.compareTo(entry.getKey(), bstart) <= 0) {
      batch.delete(entry.getKey());
      batchsize++;
      
      if (MAX_DELETE_BATCHSIZE <= batchsize) {
        if (syncwrites) {
          options = new WriteOptions().sync(Math.random() < syncrate);
        }
        this.db.write(batch, options);
        batch.close();
        batch = this.db.createWriteBatch();
        batchsize = 0;
      }
      //this.db.delete(entry.getKey());
      count++;
    } else {
      break;
    }
  }
  
  if (batchsize > 0) {
    if (syncwrites) {
      options = new WriteOptions().sync(Math.random() < syncrate);
    }
    this.db.write(batch, options);
  }

  iterator.close();
  batch.close();
  
  return count;
}
 
Example 4
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 5
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 6
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 7
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);
					}
				}
			});
}