use of org.iq80.leveldb.DBIterator in project cdap by caskdata.
the class LevelDBTableCore method deleteRows.
/**
* Delete a list of rows from the table entirely, disregarding transactions.
* @param toDelete the row keys to delete
*/
public void deleteRows(Collection<byte[]> toDelete) throws IOException {
if (toDelete.isEmpty()) {
return;
}
// find first row to delete and first entry in the DB to examine
Iterator<byte[]> rows = toDelete.iterator();
byte[] currentRow = rows.next();
byte[] startKey = createStartKey(currentRow);
DB db = getDB();
WriteBatch batch = db.createWriteBatch();
try (DBIterator iterator = db.iterator()) {
iterator.seek(startKey);
if (!iterator.hasNext()) {
// nothing in the db to delete
return;
}
Map.Entry<byte[], byte[]> entry = iterator.next();
// iterate over the database and the rows to delete, collecting (raw) keys to delete
while (entry != null && currentRow != null) {
KeyValue kv = KeyValue.fromKey(entry.getKey());
int comp = Bytes.compareTo(kv.getRow(), currentRow);
if (comp == 0) {
// same row -> delete
batch.delete(entry.getKey());
entry = iterator.hasNext() ? iterator.next() : null;
} else if (comp > 0) {
// read past current row -> move to next row
currentRow = rows.hasNext() ? rows.next() : null;
} else if (comp < 0) {
// iterator must seek to current row
iterator.seek(createStartKey(currentRow));
entry = iterator.hasNext() ? iterator.next() : null;
}
}
}
// delete all the entries that were found
db.write(batch, getWriteOptions());
}
use of org.iq80.leveldb.DBIterator in project cdap by caskdata.
the class LevelDBTableCore method getRow.
/**
* if columns are not null, then limit param is ignored and limit is columns.length
*/
public NavigableMap<byte[], byte[]> getRow(byte[] row, @Nullable byte[][] columns, byte[] startCol, byte[] stopCol, int limit, Transaction tx) throws IOException {
if (columns != null) {
if (columns.length == 0) {
return EMPTY_ROW_MAP;
}
columns = Arrays.copyOf(columns, columns.length);
Arrays.sort(columns, Bytes.BYTES_COMPARATOR);
limit = columns.length;
}
byte[] startKey = createStartKey(row, columns == null ? startCol : columns[0]);
byte[] endKey = createEndKey(row, columns == null ? stopCol : upperBound(columns[columns.length - 1]));
try (DBIterator iterator = getDB().iterator()) {
iterator.seek(startKey);
return getRow(iterator, endKey, tx, false, columns, limit).getSecond();
}
}
Aggregations