Search in sources :

Example 6 with WriteOnly

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);
}
Also used : TreeSet(java.util.TreeSet) Row(io.cdap.cdap.api.dataset.table.Row) Map(java.util.Map) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap) WriteOnly(io.cdap.cdap.api.annotation.WriteOnly)

Example 7 with WriteOnly

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);
}
Also used : Put(io.cdap.cdap.api.dataset.table.Put) WriteOnly(io.cdap.cdap.api.annotation.WriteOnly)

Example 8 with WriteOnly

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()));
    }
}
Also used : DataSetException(io.cdap.cdap.api.dataset.DataSetException) IOException(java.io.IOException) PartitionDetail(io.cdap.cdap.api.dataset.lib.PartitionDetail) Location(org.apache.twill.filesystem.Location) WriteOnly(io.cdap.cdap.api.annotation.WriteOnly)

Example 9 with WriteOnly

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);
}
Also used : Put(io.cdap.cdap.api.dataset.table.Put) WriteOnly(io.cdap.cdap.api.annotation.WriteOnly)

Example 10 with WriteOnly

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);
    }
}
Also used : DataSetException(io.cdap.cdap.api.dataset.DataSetException) IOException(java.io.IOException) Put(io.cdap.cdap.api.dataset.table.Put) WriteOnly(io.cdap.cdap.api.annotation.WriteOnly)

Aggregations

WriteOnly (io.cdap.cdap.api.annotation.WriteOnly)11 Row (io.cdap.cdap.api.dataset.table.Row)5 Put (io.cdap.cdap.api.dataset.table.Put)4 DataSetException (io.cdap.cdap.api.dataset.DataSetException)3 IOException (java.io.IOException)3 TreeSet (java.util.TreeSet)2 PartitionDetail (io.cdap.cdap.api.dataset.lib.PartitionDetail)1 Map (java.util.Map)1 NavigableMap (java.util.NavigableMap)1 TreeMap (java.util.TreeMap)1 Location (org.apache.twill.filesystem.Location)1