org.iq80.leveldb.DBIterator Java Examples

The following examples show how to use org.iq80.leveldb.DBIterator. 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: LevelDBUtilTest.java    From julongchain with Apache License 2.0 6 votes vote down vote up
@Test
public void getAll() {

	try {
		// LevelDB db = LevelDBUtil.getDB("/var/julongchain/stateLeveldb");
		LevelDB db =
				LevelDBUtil.getDB(
						"/var/julongchain/production/node/chains/chains/myGroup");
		// byte[] bytes = LevelDBUtil.get(db, "aaa".getBytes(), false);
		// System.out.println(new String(bytes));

		DBIterator iterator = LevelDBUtil.getIterator(db);
		while(iterator.hasNext()){
			Map.Entry<byte[], byte[]> next = iterator.next();
			System.out.println("key:[" + new String(next.getKey()) + "] value:[" + new String(next.getValue()) + "]");
		}

	} catch (LevelDBException e) {
		e.printStackTrace();
	}

}
 
Example #2
Source File: SelectLevelDBData.java    From gsc-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void data(String dataName) {
    Options options = new Options();
    //options.createIfMissing(true);
    DB db = null;
    try {
        System.out.println("Path: " + path + dataName);
        db = factory.open(new File(path + dataName), options);

        logger.info("---------------------------------------------");
        System.out.println();
        DBIterator iterator = db.iterator();
        iterator.seekToFirst();
        int count = 0;
        while (iterator.hasNext()) {
            count++;
            switch (dataName) {
                case "properties":
                    properties(iterator.peekNext().getKey(), iterator.peekNext().getValue());
                    break;
                case "peers":
                    peers(iterator.peekNext().getKey(), iterator.peekNext().getValue());
                    break;
                case "nodes":
                    peers(iterator.peekNext().getKey(), iterator.peekNext().getValue());
                    break;
                default:
                    break;
            }
            iterator.next();
        }
        iterator.close();
        System.out.println(dataName + " Num: " + count);
        System.out.println();
        logger.info("---------------------------------------------");

        db.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #3
Source File: LevelDbUtil.java    From mcg-helper with Apache License 2.0 6 votes vote down vote up
public static void print() throws IOException {
    //iterator,遍历,顺序读
    //读取当前snapshot,快照,读取期间数据的变更,不会反应出来       
    Snapshot snapshot = db.getSnapshot();
    //读选项        
    ReadOptions readOptions = new ReadOptions();
    readOptions.fillCache(false);//遍历中swap出来的数据,不应该保存在memtable中。        
    readOptions.snapshot(snapshot);//默认snapshot为当前。        
    DBIterator iterator = db.iterator(readOptions);
    while (iterator.hasNext()) {
        Map.Entry<byte[],byte[]> item = iterator.next();
        String key = new String(item.getKey(), Constants.CHARSET);
        String value = new String(item.getValue(), Constants.CHARSET);
        logger.debug("key:{},value:{}", key, value);
    }
    iterator.close();
}
 
Example #4
Source File: AccountsListWindow.java    From ethereumj with MIT License 6 votes vote down vote up
private void loadAccounts() {
	new Thread(){
		
		@Override
		public void run(){
			DBIterator i = WorldManager.getInstance().getRepository().getAccountsIterator();
			while(i.hasNext()) {
				DataClass dc = new DataClass();
				dc.address = i.next().getKey();
				
				AccountState state = WorldManager.getInstance().getRepository().getAccountState(dc.address);
				dc.accountState = state;
				
				adapter.addDataPiece(dc);
			}
		}
	}.start();
}
 
Example #5
Source File: LevelDbDataSourceImpl.java    From gsc-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public Map<byte[], byte[]> getPrevious(byte[] key, long limit, int precision) {
    if (limit <= 0 || key.length < precision) {
        return Collections.emptyMap();
    }
    resetDbLock.readLock().lock();
    try (DBIterator iterator = database.iterator()) {
        Map<byte[], byte[]> result = new HashMap<>();
        long i = 0;
        for (iterator.seekToFirst(); iterator.hasNext() && i++ < limit; iterator.next()) {
            Entry<byte[], byte[]> entry = iterator.peekNext();

            if (entry.getKey().length >= precision) {
                if (ByteUtil.less(ByteUtil.parseBytes(key, 0, precision),
                        ByteUtil.parseBytes(entry.getKey(), 0, precision))) {
                    break;
                }
                result.put(entry.getKey(), entry.getValue());
            }
        }
        return result;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        resetDbLock.readLock().unlock();
    }
}
 
Example #6
Source File: LevelDbDataSourceImpl.java    From gsc-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public Set<byte[]> getValuesPrev(byte[] key, long limit) {
    if (limit <= 0) {
        return Sets.newHashSet();
    }
    resetDbLock.readLock().lock();
    try (DBIterator iterator = database.iterator()) {
        Set<byte[]> result = Sets.newHashSet();
        long i = 0;
        byte[] data = getData(key);
        if (Objects.nonNull(data)) {
            result.add(data);
            i++;
        }
        for (iterator.seek(key); iterator.hasPrev() && i++ < limit; iterator.prev()) {
            result.add(iterator.peekPrev().getValue());
        }
        return result;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        resetDbLock.readLock().unlock();
    }
}
 
Example #7
Source File: LevelDbDataSourceImpl.java    From gsc-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public Map<byte[], byte[]> getNext(byte[] key, long limit) {
    if (limit <= 0) {
        return Collections.emptyMap();
    }
    resetDbLock.readLock().lock();
    try (DBIterator iterator = database.iterator()) {
        Map<byte[], byte[]> result = new HashMap<>();
        long i = 0;
        for (iterator.seek(key); iterator.hasNext() && i++ < limit; iterator.next()) {
            Entry<byte[], byte[]> entry = iterator.peekNext();
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        resetDbLock.readLock().unlock();
    }
}
 
Example #8
Source File: LevelDbDataSourceImpl.java    From gsc-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public Set<byte[]> getValuesNext(byte[] key, long limit) {
    if (limit <= 0) {
        return Sets.newHashSet();
    }
    resetDbLock.readLock().lock();
    try (DBIterator iterator = database.iterator()) {
        Set<byte[]> result = Sets.newHashSet();
        long i = 0;
        for (iterator.seek(key); iterator.hasNext() && i++ < limit; iterator.next()) {
            result.add(iterator.peekNext().getValue());
        }
        return result;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        resetDbLock.readLock().unlock();
    }
}
 
Example #9
Source File: LevelDbDataSourceImpl.java    From gsc-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public Set<byte[]> getlatestValues(long limit) {
    if (limit <= 0) {
        return Sets.newHashSet();
    }
    resetDbLock.readLock().lock();
    try (DBIterator iterator = database.iterator()) {
        Set<byte[]> result = Sets.newHashSet();
        long i = 0;
        iterator.seekToLast();
        if (iterator.hasNext()) {
            result.add(iterator.peekNext().getValue());
            i++;
        }
        for (; iterator.hasPrev() && i++ < limit; iterator.prev()) {
            result.add(iterator.peekPrev().getValue());
        }
        return result;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        resetDbLock.readLock().unlock();
    }
}
 
Example #10
Source File: LevelDbDataSourceImpl.java    From gsc-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Deprecated
@Override
public Set<byte[]> allValues() {
    resetDbLock.readLock().lock();
    try (DBIterator iterator = database.iterator()) {
        Set<byte[]> result = Sets.newHashSet();
        for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
            result.add(iterator.peekNext().getValue());
        }
        return result;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        resetDbLock.readLock().unlock();
    }
}
 
Example #11
Source File: LevelDbDataSourceImpl.java    From gsc-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Deprecated
@Override
public Set<byte[]> allKeys() {
    resetDbLock.readLock().lock();
    try (DBIterator iterator = database.iterator()) {
        Set<byte[]> result = Sets.newHashSet();
        for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
            result.add(iterator.peekNext().getKey());
        }
        return result;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        resetDbLock.readLock().unlock();
    }
}
 
Example #12
Source File: WarpDB.java    From warp10-platform with Apache License 2.0 6 votes vote down vote up
@Override
public DBIterator iterator(ReadOptions options) {
  if (null == options) {
    return iterator();
  }
  if (null != options.snapshot()) {
    throw new RuntimeException("Snapshots are unsupported.");
  }
  try {
    mutex.lockInterruptibly();
    pendingOps.incrementAndGet();
  } catch (InterruptedException ie) {
    throw new DBException("Interrupted while acquiring DB mutex.", ie);
  } finally {
    if (mutex.isHeldByCurrentThread()) {
      mutex.unlock();
    }
  }    
  return new WarpIterator(pendingOps, this.db.iterator(options));    
}
 
Example #13
Source File: LevelDBStore.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
@Override
public void iterate(byte[] from, EntryConsumer consumer)
    throws IOException {
  try (DBIterator iter = db.iterator()) {
    if (from != null) {
      iter.seek(from);
    } else {
      iter.seekToFirst();
    }
    while (iter.hasNext()) {
      Entry<byte[], byte[]> current = iter.next();
      if (!consumer.consume(current.getKey(),
          current.getValue())) {
        break;
      }
    }
  }
}
 
Example #14
Source File: LevelDbDataSourceImpl.java    From gsc-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public long getTotal() throws RuntimeException {
    resetDbLock.readLock().lock();
    try (DBIterator iterator = database.iterator()) {
        long total = 0;
        for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
            total++;
        }
        return total;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        resetDbLock.readLock().unlock();
    }
}
 
Example #15
Source File: LevelDb.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
@Benchmark
public void readCrc(final Reader r, final Blackhole bh) throws IOException {
  r.crc.reset();
  try (DBIterator iterator = r.db.iterator()) {
    for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
      final Entry<byte[], byte[]> peeked = iterator.peekNext();
      r.crc.update(peeked.getKey());
      r.crc.update(peeked.getValue());
    }
  }
  bh.consume(r.crc.getValue());
}
 
Example #16
Source File: LevelDb.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
@Benchmark
public void readRev(final Reader r, final Blackhole bh) throws IOException {
  try (DBIterator iterator = r.db.iterator()) {
    for (iterator.seekToLast(); iterator.hasPrev(); iterator.prev()) {
      final Entry<byte[], byte[]> peeked = iterator.peekPrev();
      bh.consume(peeked.getValue());
    }
  }
}
 
Example #17
Source File: LevelDb.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
@Benchmark
public void readSeq(final Reader r, final Blackhole bh) throws IOException {
  try (DBIterator iterator = r.db.iterator()) {
    for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
      final Entry<byte[], byte[]> peeked = iterator.peekNext();
      bh.consume(peeked.getValue());
    }
  }
}
 
Example #18
Source File: LevelDb.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
@Benchmark
public void readXxh64(final Reader r, final Blackhole bh) throws IOException {
  long result = 0;
  try (DBIterator iterator = r.db.iterator()) {
    for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
      final Entry<byte[], byte[]> peeked = iterator.peekNext();
      result += xx_r39().hashBytes(peeked.getKey());
      result += xx_r39().hashBytes(peeked.getValue());
    }
  }
  bh.consume(result);
}
 
Example #19
Source File: DatabaseImpl.java    From ethereumj with MIT License 5 votes vote down vote up
public List<ByteArrayWrapper> dumpKeys() {
	DBIterator iterator = getDb().iterator();
	ArrayList<ByteArrayWrapper> keys = new ArrayList<>();

	while (iterator.hasNext()) {
		ByteArrayWrapper key = new ByteArrayWrapper(iterator.next().getKey());
		keys.add(key);
	}
	Collections.sort((List<ByteArrayWrapper>) keys);
	return keys;
}
 
Example #20
Source File: WarpDB.java    From warp10-platform with Apache License 2.0 5 votes vote down vote up
@Override
public DBIterator iterator() {
  try {
    mutex.lockInterruptibly();
    pendingOps.incrementAndGet();
  } catch (InterruptedException ie) {
    throw new DBException("Interrupted while acquiring DB mutex.", ie);
  } finally {
    if (mutex.isHeldByCurrentThread()) {
      mutex.unlock();
    }
  }
  return new WarpIterator(pendingOps, this.db.iterator());
}
 
Example #21
Source File: LevelDBJobStore.java    From AthenaX with Apache License 2.0 5 votes vote down vote up
@Override
public List<ExtendedJobDefinition> listAll() throws IOException {
  ArrayList<ExtendedJobDefinition> jobs = new ArrayList<>();
  try (DBIterator it = db.iterator()) {
    for (it.seekToFirst(); it.hasNext(); it.next()) {
      Map.Entry<byte[], byte[]> e = it.peekNext();
      UUID uuid = UUID.fromString(new String(e.getKey(), UTF_8));
      JobDefinition def = MAPPER.readValue(e.getValue(), JobDefinition.class);
      jobs.add(new ExtendedJobDefinition().uuid(uuid).definition(def));
    }
  }
  return jobs;
}
 
Example #22
Source File: LevelDBEntityStoreMixin.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
public Stream<Reader> entityStates()
{
    DBIterator iterator = db.iterator();
    iterator.seekToFirst();
    return StreamSupport.stream(
        new Spliterators.AbstractSpliterator<Reader>( Long.MAX_VALUE, Spliterator.ORDERED )
        {
            @Override
            public boolean tryAdvance( final Consumer<? super Reader> action )
            {
                if( !iterator.hasNext() )
                {
                    return false;
                }
                action.accept( new StringReader( new String( iterator.next().getValue(), charset ) ) );
                return true;
            }
        },
        false
    ).onClose(
        () ->
        {
            try
            {
                iterator.close();
            }
            catch( IOException ex )
            {
                throw new EntityStoreException( "Unable to close DB iterator" );
            }
        }
    );
}
 
Example #23
Source File: DatabaseImpl.java    From ethereumj with MIT License 5 votes vote down vote up
public DatabaseImpl(String name) {
    	// Initialize Database
        this.name = name;
		Options options = new Options();
		options.createIfMissing(true);
		options.compressionType(CompressionType.NONE);
		try {
			logger.debug("Opening database");
            File dbLocation = new File(System.getProperty("user.dir") + "/" +
                                       SystemProperties.CONFIG.databaseDir() + "/");
            File fileLocation = new File(dbLocation, name);

			if(SystemProperties.CONFIG.databaseReset()) {
				destroyDB(fileLocation);
			}

			logger.debug("Initializing new or existing database: '{}'", name);
			db = factory.open(fileLocation, options);
//			logger.debug("Showing database stats");
//			String stats = DATABASE.getProperty("leveldb.stats");
//			logger.debug(stats);

            if (logger.isTraceEnabled()){

                logger.trace("Dump for: {}", fileLocation.toString());
                DBIterator iter =  db.iterator();

                while(iter.hasNext()){
                    byte[] key   = iter.peekNext().getKey();
                    byte[] value = iter.peekNext().getValue();

                    logger.trace("key={}, value={}", Hex.toHexString(key), Hex.toHexString(value));
                    iter.next();
                }
            }
		} catch (IOException ioe) {
			logger.error(ioe.getMessage(), ioe);
			throw new RuntimeException("Can't initialize database");
		}		
	}
 
Example #24
Source File: HistoryLevelDBQueryExecutor.java    From julongchain with Apache License 2.0 5 votes vote down vote up
@Override
 public IResultsIterator getHistoryForKey(String namespace, String key) throws LedgerException{
     if(!LedgerConfig.isHistoryDBEnabled()){
     	String msg = "History db is not available";
log.debug(msg);
         throw new LedgerException(msg);
     }
     byte[] compositeStartKey = HistoryDBHelper.constructPartialCompositeHistoryKey(namespace, key, false);
     DBIterator iterator = (DBIterator) historyDB.getProvider().getIterator((compositeStartKey));

  return new HistoryScanner(compositeStartKey, namespace, key, iterator, blockStore, ledgerID);
 }
 
Example #25
Source File: LevelDBStore.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if the DB is empty.
 *
 * @return boolean
 * @throws IOException
 */
@Override
public boolean isEmpty() throws IOException {
  try (DBIterator iter = db.iterator()) {
    iter.seekToFirst();
    boolean hasNext = !iter.hasNext();
    return hasNext;
  }
}
 
Example #26
Source File: LevelDB.java    From aion with MIT License 5 votes vote down vote up
@Override
public boolean isEmpty() {
    check();

    try (DBIterator itr = db.iterator()) {
        itr.seekToFirst();

        // check if there is at least one item
        return !itr.hasNext();
    } catch (Exception e) {
        LOG.error("Unable to extract information from database " + this.toString() + ".", e);
    }

    return true;
}
 
Example #27
Source File: LevelDB.java    From aion with MIT License 5 votes vote down vote up
/**
 * @implNote Building two wrappers for the same {@link DBIterator} will lead to inconsistent
 *     behavior.
 */
LevelDBIteratorWrapper(final ReadOptions readOptions, final DBIterator iterator, final Logger log) {
    this.readOptions = readOptions;
    this.iterator = iterator;
    iterator.seekToFirst();
    closed = false;
    this.LOG = log;
}
 
Example #28
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 #29
Source File: HistoryScanner.java    From julongchain with Apache License 2.0 5 votes vote down vote up
public HistoryScanner(byte[] compositePartialKey,
                      String nameSpace,
                      String key,
                      DBIterator dbItr,
                      IBlockStore blockStore,
                      String ledgerID){
    this.compositePartialKey = compositePartialKey;
    this.nameSpace = nameSpace;
    this.key = key;
    this.dbItr = dbItr;
    this.blockStore = blockStore;
    this.ledgerID = ledgerID;
}
 
Example #30
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;
}