use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.
the class MetadataStoreDataset method scan.
/**
* Run a scan on MDS for default COLUMN
*
* @param startId scan start key
* @param stopId scan stop key
* @param typeOfT type of value
* @param function function to process each element returned from scan.
* If function.apply returns false then the scan is stopped.
* Also, function.apply should not return null.
* @param <T> type of value
*/
public <T> void scan(MDSKey startId, @Nullable MDSKey stopId, Type typeOfT, Function<KeyValue<T>, Boolean> function) {
byte[] startKey = startId.getKey();
byte[] stopKey = stopId == null ? Bytes.stopKeyForPrefix(startKey) : stopId.getKey();
try (Scanner scan = table.scan(startKey, stopKey)) {
Row next;
while ((next = scan.next()) != null) {
byte[] columnValue = next.get(COLUMN);
if (columnValue == null) {
continue;
}
MDSKey key = new MDSKey(next.getRow());
T value = deserialize(key, columnValue, typeOfT);
// noinspection ConstantConditions
if (!function.apply(new KeyValue<>(key, value))) {
break;
}
}
}
}
use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.
the class MetadataStoreDataset method exists.
/**
* Check whether the value is there in the row and default COLUMN
*
* @param id the mds key for the row
* @return a boolean which indicates the value exists or not
*/
public boolean exists(MDSKey id) {
Row row = table.get(id.getKey());
if (row.isEmpty()) {
return false;
}
byte[] value = row.get(COLUMN);
return value != null;
}
use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.
the class MetadataStoreDataset method getFirst.
/**
* Get the first element in the row and default COLUMN, of type T
*
* @param id the mds key for the row
* @param typeOfT the type of the result
* @return the deserialized value of the result, null if not exist
*/
@Nullable
public <T> T getFirst(MDSKey id, Type typeOfT) {
try {
try (Scanner scan = table.scan(id.getKey(), Bytes.stopKeyForPrefix(id.getKey()))) {
Row row = scan.next();
if (row == null || row.isEmpty()) {
return null;
}
byte[] value = row.get(COLUMN);
if (value == null) {
return null;
}
return deserialize(id, value, typeOfT);
}
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.
the class MetadataStoreDataset method getKV.
/**
* Get all non-null values with the given ids for default COLUMN in a map
*
* @param ids set of the mds keys
* @return a map of the deserialized value of the result
*/
protected Map<MDSKey, byte[]> getKV(Set<MDSKey> ids) {
Map<MDSKey, byte[]> resultMap = new HashMap<>();
List<Get> getList = new ArrayList<>();
for (MDSKey id : ids) {
getList.add(new Get(id.getKey()));
}
List<Row> rowList = table.get(getList);
for (Row row : rowList) {
if (row.isEmpty()) {
continue;
}
byte[] value = row.get(COLUMN);
if (value == null) {
continue;
}
MDSKey key = new MDSKey(row.getRow());
resultMap.put(key, value);
}
return resultMap;
}
use of io.cdap.cdap.api.dataset.table.Row in project cdap by cdapio.
the class LevelDBTableCore method deleteRange.
public void deleteRange(byte[] startRow, byte[] stopRow, @Nullable FuzzyRowFilter filter, @Nullable byte[][] columns) throws IOException {
if (columns != null) {
if (columns.length == 0) {
return;
}
columns = Arrays.copyOf(columns, columns.length);
Arrays.sort(columns, Bytes.BYTES_COMPARATOR);
}
DB db = getDB();
DBIterator iterator = db.iterator();
seekToStart(iterator, startRow);
byte[] endKey = stopRow == null ? null : createStartKey(stopRow);
DBIterator deleteIterator = db.iterator();
seekToStart(deleteIterator, startRow);
// todo make configurable
final int deletesPerRound = 1024;
try (Scanner scanner = new LevelDBScanner(iterator, endKey, filter, columns, null)) {
Row rowValues;
WriteBatch batch = db.createWriteBatch();
int deletesInBatch = 0;
// go through all matching cells and delete them in batches.
while ((rowValues = scanner.next()) != null) {
byte[] row = rowValues.getRow();
for (byte[] column : rowValues.getColumns().keySet()) {
addToDeleteBatch(batch, deleteIterator, row, column);
deletesInBatch++;
// perform the deletes when we have built up a batch.
if (deletesInBatch >= deletesPerRound) {
// delete all the entries that were found
db.write(batch, getWriteOptions());
batch = db.createWriteBatch();
deletesInBatch = 0;
}
}
}
// perform any outstanding deletes
if (deletesInBatch > 0) {
db.write(batch, getWriteOptions());
}
} finally {
deleteIterator.close();
}
}
Aggregations