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

The following examples show how to use org.iq80.leveldb.DBIterator#close() . 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: 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 2
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 3
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 4
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 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) {
	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 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) {
	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 7
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 8
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 9
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);
					}
				}
			});
}