public byte[] key() { return iterator.key(); }
private static int getStartValue(RocksDB db, ColumnFamilyHandle handler) { int startValue = 0; SortedMap<Integer, Integer> values = new TreeMap<Integer, Integer>(); RocksIterator itr = db.newIterator(handler); itr.seekToFirst(); while(itr.isValid()) { Integer key = Integer.valueOf(new String(itr.key())); Integer value = Integer.valueOf(new String(itr.value())); values.put(key, value); itr.next(); } LOG.info("Load previous db: size={}, values={}", values.size(), values); if (!values.isEmpty()) { Integer key = values.lastKey(); startValue = values.get(key); startValue++; } LOG.info("Start value={}", startValue); return startValue; }
@Override public Set<EdgeEntry> allEdges() { Set<EdgeEntry> edges = new HashSet<>(); RocksIterator iter = mDb.newIterator(mEdgesColumn); iter.seekToFirst(); while (iter.isValid()) { long parentId = RocksUtils.readLong(iter.key(), 0); String childName = new String(iter.key(), Longs.BYTES, iter.key().length - Longs.BYTES); long childId = Longs.fromByteArray(iter.value()); edges.add(new EdgeEntry(parentId, childName, childId)); iter.next(); } return edges; }
@Override public Set<byte[]> keys() throws RuntimeException { resetDbLock.readLock().lock(); try { if (logger.isTraceEnabled()) logger.trace("~> RocksDbDataSource.keys(): " + name); try (RocksIterator iterator = db.newIterator()) { Set<byte[]> result = new HashSet<>(); for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) { result.add(iterator.key()); } if (logger.isTraceEnabled()) logger.trace("<~ RocksDbDataSource.keys(): " + name + ", " + result.size()); return result; } catch (Exception e) { logger.error("Error iterating db '{}'", name, e); hintOnTooManyOpenFiles(e); throw new RuntimeException(e); } } finally { resetDbLock.readLock().unlock(); } }
@Override public Set<MutableInode<?>> allInodes() { Set<MutableInode<?>> inodes = new HashSet<>(); RocksIterator iter = mDb.newIterator(mInodesColumn); iter.seekToFirst(); while (iter.isValid()) { inodes.add(getMutable(Longs.fromByteArray(iter.key())).get()); iter.next(); } return inodes; }
@Override public Collection<K> listKeys() { Collection<K> keys = new ArrayList<>(); RocksIterator itr = rocksDb.newIterator(columnFamily); itr.seekToFirst(); while (itr.isValid()) { keys.add((K) serializer.deserialize(itr.key())); itr.next(); } return keys; }
@Override public String[] next() { counter++; if (counter % 100000 == 0) { logger.info("scanned {} rows from rocksDB", counter); } String[] result = rowEncoder.decode(new KV(rocksIterator.key(), rocksIterator.value())); rocksIterator.next(); return result; }
@Override public Block next() { try { return new Block(Longs.fromByteArray(iter.key()), BlockMeta.parseFrom(iter.value())); } catch (Exception e) { throw new RuntimeException(e); } finally { iter.next(); } } };
@Override public ByteArray next() { if(!this.innerIterator.isValid()) { throw new NoSuchElementException("Iterate to end"); } byte[] keyEntry = this.innerIterator.key(); this.innerIterator.next(); if (RocksDbStorageEngine.this.isPartitionScanSupported()) { keyEntry = StoreBinaryFormat.extractKey(keyEntry); } return new ByteArray(keyEntry); }
@Override public <T> List<Pair<String, T>> fetchTop(int size, Type type) throws FailStoreException { RocksIterator iterator = null; try { List<Pair<String, T>> list = new ArrayList<Pair<String, T>>(size); iterator = db.newIterator(); for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) { iterator.status(); String key = new String(iterator.key(), "UTF-8"); T value = JSON.parse(new String(iterator.value(), "UTF-8"), type); Pair<String, T> pair = new Pair<String, T>(key, value); list.add(pair); if (list.size() >= size) { break; } } return list; } catch (Exception e) { throw new FailStoreException(e); } finally { if (iterator != null) { iterator.dispose(); } } }
private boolean fetchnextKey() { if(this.innerIterator.isValid()) { byte[] keyEntry = this.innerIterator.key(); // Check if the next key returned starts with the expected // prefix if(StoreBinaryFormat.extractPartition(keyEntry) != partitionId) { return false; } this.innerIterator.next(); cache = new ByteArray(StoreBinaryFormat.extractKey(keyEntry)); return true; } return false; }
@Override public <T> List<Pair<String, T>> fetchTop(int size, Type type) throws FailStoreException { RocksIterator iterator = null; try { List<Pair<String, T>> list = new ArrayList<Pair<String, T>>(size); iterator = db.newIterator(); for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) { iterator.status(); String key = new String(iterator.key(), "UTF-8"); T value = JSON.parse(new String(iterator.value(), "UTF-8"), type); Pair<String, T> pair = new Pair<String, T>(key, value); list.add(pair); if (list.size() >= size) { break; } } return list; } catch (Exception e) { throw new FailStoreException(e); } finally { if (iterator != null) { iterator.dispose(); } } }
protected boolean makeMore() { if(innerIterator.isValid()) { byte[] keyEntry = innerIterator.key(); byte[] valueEntry = innerIterator.value(); innerIterator.next(); if (RocksDbStorageEngine.this.isPartitionScanSupported()) { keyEntry = StoreBinaryFormat.extractKey(keyEntry); } ByteArray key = new ByteArray(keyEntry); for(Versioned<byte[]> val: StoreBinaryFormat.fromByteArray(valueEntry)) { cache.add(new Pair<ByteArray, Versioned<byte[]>>(key, val)); } return true; } return false; }
protected boolean makeMore() { if(innerIterator.isValid()) { byte[] keyEntry = innerIterator.key(); // Check if the next key returned starts with the expected // prefix if(StoreBinaryFormat.extractPartition(keyEntry) != partitionId) { return false; } byte[] valueEntry = innerIterator.value(); innerIterator.next(); ByteArray key = new ByteArray(StoreBinaryFormat.extractKey(keyEntry)); for(Versioned<byte[]> val: StoreBinaryFormat.fromByteArray(valueEntry)) { cache.add(new Pair<ByteArray, Versioned<byte[]>>(key, val)); } return true; } return false; }
void scanRange(RocksDbKey start, RocksDbKey end, RocksDbScanCallback fn) { try (ReadOptions ro = new ReadOptions()) { ro.setTotalOrderSeek(true); RocksIterator iterator = db.newIterator(ro); for (iterator.seek(start.getRaw()); iterator.isValid(); iterator.next()) { RocksDbKey key = new RocksDbKey(iterator.key()); if (key.compareTo(end) >= 0) { // past limit, quit return; } RocksDbValue val = new RocksDbValue(iterator.value()); if (!fn.cb(key, val)) { // if cb returns false, we are done with this section of rows return; } } } }
RocksDbKey key = new RocksDbKey(iterator.key());
@Override public BackendColumn next() { if (!this.matched) { if (!this.hasNext()) { throw new NoSuchElementException(); } } BackendColumn col = BackendColumn.of(this.itor.key(), this.itor.value()); this.itor.next(); this.matched = false; return col; }
/** * Just for debug */ @SuppressWarnings("unused") private void dump() { this.seek(); System.out.println(">>>> scan from " + this.table + ": " + (this.keyBegin == null ? "*" : StringEncoding.format(this.keyBegin)) + (this.itor.isValid() ? "" : " - No data")); for (; this.itor.isValid(); this.itor.next()) { System.out.println(String.format("%s=%s", StringEncoding.format(this.itor.key()), StringEncoding.format(this.itor.value()))); } }
private void seek() { if (this.keyBegin == null) { // Seek to the first if no `keyBegin` this.itor.seekToFirst(); } else { /* * Seek to `keyBegin`: * if set SCAN_GT_BEGIN/SCAN_GTE_BEGIN (key > / >= 'xx') * or if set SCAN_PREFIX_WITH_BEGIN (key prefix with 'xx') */ this.itor.seek(this.keyBegin); // Skip `keyBegin` if set SCAN_GT_BEGIN (key > 'xx') if (this.match(Session.SCAN_GT_BEGIN) && !this.match(Session.SCAN_GTE_BEGIN)) { while (this.itor.isValid() && Bytes.equals(this.itor.key(), this.keyBegin)) { this.itor.next(); } } } }