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

The following examples show how to use org.iq80.leveldb.DBIterator#next() . 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: 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 5
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 6
Source File: LevelDbStore.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
		Kryo kryo = new Kryo();
		kryo.setReferences(false);
		kryo.setRegistrationRequired(false);
		kryo.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
//		kryo.register(Transaction.class);
//		kryo.register(Xid.class);
//		kryo.register(TransactionType.class);
//		kryo.register(TransactionStatus.class);
//		kryo.register(Participant.class);
		
//		Output output = new Output(new ByteArrayOutputStream());//new Output(new FileOutputStream("file.bin"));
//		
//		DefaultTransaction defaultTransaction = new DefaultTransaction();
//		defaultTransaction.setXid(new DefaultXid("1", "2", "3"));
//		
//		kryo.writeObject(output, defaultTransaction);
//		output.flush();
//		output.close();
		
		Options options = new Options();
		options.createIfMissing(true);
		options.cacheSize(100 * 1048576); // 100MB cache
		options.logger(new org.iq80.leveldb.Logger() {
			public void log(String message) {
				TransactionLog.REPOSITORY.info(message);
			}
		});
		DB db = Iq80DBFactory.factory.open(new File("txtreedb"), options);
		
//		String stats = db.getProperty("leveldb.stats");
//		System.out.println(stats);
		
		DBIterator iterator = null;
		try {
			iterator = db.iterator();
			for(iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
				String key = Iq80DBFactory.asString(iterator.peekNext().getKey());
				Transaction value = (Transaction) SerializationUtil.deserialize(iterator.peekNext().getValue());
				
				System.out.println(key+" = "+value);
			}
		} finally {
			if (iterator != null) {
				iterator.close();
			}
		}

//		String transactionId = "f282205a-e794-4bda-83a2-bb28b8839cad";
//		Input input = new Input(db.get(Iq80DBFactory.bytes(transactionId)));
//		Transaction transaction = (Transaction) kryo.readClassAndObject(input);
//		System.out.println(transaction);
	}
 
Example 7
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 8
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 9
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 10
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);
					}
				}
			});
}