use of io.cdap.cdap.api.annotation.WriteOnly in project cdap by caskdata.
the class IndexedTable method put.
/**
* Writes a put to the data table. If any of the columns in the {@link Put} are configured to be indexed, the
* appropriate indexes will be updated with the indexed values referencing the data table row.
*
* @param put The put operation to store
*/
@WriteOnly
@Override
public void put(Put put) {
// if different value exists, remove current index ref
// add a new index ref unless same value already exists
byte[] dataRow = put.getRow();
// find which values need to be indexed
Map<byte[], byte[]> putColumns = put.getValues();
Set<byte[]> colsToIndex = new TreeSet<>(Bytes.BYTES_COMPARATOR);
for (Map.Entry<byte[], byte[]> putEntry : putColumns.entrySet()) {
if (indexedColumns.contains(putEntry.getKey())) {
colsToIndex.add(putEntry.getKey());
}
}
if (!colsToIndex.isEmpty()) {
// first read the existing indexed values to find which have changed and need to be updated
Row existingRow = table.get(dataRow, colsToIndex.toArray(new byte[colsToIndex.size()][]));
for (Map.Entry<byte[], byte[]> entry : existingRow.getColumns().entrySet()) {
if (!Arrays.equals(entry.getValue(), putColumns.get(entry.getKey()))) {
index.delete(createIndexKey(dataRow, entry.getKey(), entry.getValue()), IDX_COL);
} else {
// value already indexed
colsToIndex.remove(entry.getKey());
}
}
// add new index entries for all values that have changed or did not exist
for (byte[] col : colsToIndex) {
index.put(createIndexKey(dataRow, col, putColumns.get(col)), IDX_COL, dataRow);
}
}
// store the data row
table.put(put);
}
use of io.cdap.cdap.api.annotation.WriteOnly in project cdap by caskdata.
the class IndexedTable method put.
@WriteOnly
@Override
public void put(byte[] row, byte[][] columns, byte[][] values) {
Put put = new Put(row);
for (int i = 0; i < columns.length; i++) {
put.add(columns[i], values[i]);
}
put(put);
}
use of io.cdap.cdap.api.annotation.WriteOnly in project cdap by caskdata.
the class PartitionedFileSetDataset method dropPartition.
@WriteOnly
@Override
public void dropPartition(PartitionKey key) {
byte[] rowKey = generateRowKey(key, partitioning);
PartitionDetail partition = getPartition(key);
if (partition == null) {
// silently ignore non-existing partitions
return;
}
// TODO: make DDL operations transactional [CDAP-1393]
dropPartitionFromExplore(key);
partitionsTable.delete(rowKey);
if (!isExternal) {
Location partitionLocation = partition.getLocation();
try {
if (partitionLocation.exists()) {
Location dstLocation = getQuarantineLocation().append(partition.getRelativePath());
Location dstParent = Locations.getParent(dstLocation);
// shouldn't be null, since dstLocation was created by appending to a location, so it must have a parent
Preconditions.checkNotNull(dstParent);
// before moving into quarantine, we need to ensure that parent location exists
if (!dstParent.exists()) {
if (!dstParent.mkdirs()) {
throw new DataSetException(String.format("Failed to create parent directory %s", dstParent));
}
}
partitionLocation.renameTo(dstLocation);
}
} catch (IOException ioe) {
throw new DataSetException(String.format("Failed to move location %s into quarantine", partitionLocation), ioe);
}
operationsInThisTx.add(new DropPartitionOperation(key, partition.getRelativePath()));
}
}
use of io.cdap.cdap.api.annotation.WriteOnly in project cdap by caskdata.
the class AbstractTable method write.
@WriteOnly
@Override
public void write(StructuredRecord structuredRecord) throws IOException {
if (recordPutTransformer == null) {
throw new IllegalStateException(String.format("Table must have both '%s' and '%s' properties set in " + "order to be used as a RecordWritable.", Table.PROPERTY_SCHEMA, Table.PROPERTY_SCHEMA_ROW_FIELD));
}
Put put = recordPutTransformer.toPut(structuredRecord);
put(put);
}
use of io.cdap.cdap.api.annotation.WriteOnly in project cdap by caskdata.
the class ObjectMappedTableDataset method write.
@WriteOnly
@Override
public void write(byte[] key, T object) {
Put put = new Put(key);
try {
putWriter.write(object, put);
table.put(put);
} catch (IOException e) {
// should never happen
throw new DataSetException("Failed to encode object to be written: " + e.getMessage(), e);
}
}
Aggregations