Search in sources :

Example 31 with Put

use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.

the class IndexedTable method put.

@WriteOnly
@Override
public void put(byte[] row, byte[] column, byte[] value) {
    Put put = new Put(row);
    put.add(column, value);
    put(put);
}
Also used : Put(io.cdap.cdap.api.dataset.table.Put) WriteOnly(io.cdap.cdap.api.annotation.WriteOnly)

Example 32 with Put

use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.

the class IndexedTable method compareAndSwap.

/**
 * Perform a swap operation by primary key.
 * Parameters are as if they were on a non-indexed table.
 * Note that if the swap is on the secondary key column,
 * then the index must be updated; otherwise, this is a
 * pass-through to the underlying table.
 */
@ReadWrite
@Override
public boolean compareAndSwap(byte[] row, byte[] column, byte[] expected, byte[] newValue) {
    // is the same as the new value, then the index is not affected either.
    if (!indexedColumns.contains(column) || Arrays.equals(expected, newValue)) {
        return table.compareAndSwap(row, column, expected, newValue);
    }
    // the swap is on the index column. it will only succeed if the current
    // value matches the expected value of the swap. if that value is not null,
    // then we must remove the row key from the index for that value.
    Delete idxDelete = null;
    if (expected != null) {
        idxDelete = new Delete(createIndexKey(row, column, expected), IDX_COL);
    }
    // if the new value is not null, then we must add the rowkey to the index
    // for that value.
    Put idxPut = null;
    if (newValue != null) {
        idxPut = new Put(createIndexKey(row, column, newValue), IDX_COL, row);
    }
    // apply all operations to both tables
    boolean success = table.compareAndSwap(row, column, expected, newValue);
    if (!success) {
        // do nothing: no changes
        return false;
    }
    if (idxDelete != null) {
        index.delete(idxDelete);
    }
    if (idxPut != null) {
        index.put(idxPut);
    }
    return true;
}
Also used : Delete(io.cdap.cdap.api.dataset.table.Delete) Put(io.cdap.cdap.api.dataset.table.Put) ReadWrite(io.cdap.cdap.api.annotation.ReadWrite)

Example 33 with Put

use of io.cdap.cdap.api.dataset.table.Put 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 34 with Put

use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.

the class MockAction method run.

@Override
public void run(ActionContext context) throws Exception {
    context.execute(new TxRunnable() {

        @Override
        public void run(DatasetContext context) throws Exception {
            Table table = context.getDataset(config.tableName);
            Put put = new Put(config.rowKey);
            put.add(config.columnKey, config.value);
            table.put(put);
        }
    });
    // Set the same value in the arguments as well.
    context.getArguments().set(config.rowKey + config.columnKey, config.value);
    if (config.argumentKey != null && config.argumentValue != null) {
        if (!context.getArguments().get(config.argumentKey).equals(config.argumentValue)) {
            throw new IllegalStateException(String.format("Expected %s to be present in the argument map with value %s.", config.argumentKey, config.argumentValue));
        }
    }
}
Also used : Table(io.cdap.cdap.api.dataset.table.Table) TxRunnable(io.cdap.cdap.api.TxRunnable) DatasetContext(io.cdap.cdap.api.data.DatasetContext) Put(io.cdap.cdap.api.dataset.table.Put)

Example 35 with Put

use of io.cdap.cdap.api.dataset.table.Put in project cdap by caskdata.

the class MetricsTableOnTable method putBytes.

@Override
public void putBytes(SortedMap<byte[], ? extends SortedMap<byte[], byte[]>> updates) {
    for (Map.Entry<byte[], ? extends SortedMap<byte[], byte[]>> rowUpdate : updates.entrySet()) {
        Put put = new Put(rowUpdate.getKey());
        for (Map.Entry<byte[], byte[]> columnUpdate : rowUpdate.getValue().entrySet()) {
            put.add(columnUpdate.getKey(), columnUpdate.getValue());
        }
        table.put(put);
    }
}
Also used : Map(java.util.Map) NavigableMap(java.util.NavigableMap) SortedMap(java.util.SortedMap) Put(io.cdap.cdap.api.dataset.table.Put)

Aggregations

Put (io.cdap.cdap.api.dataset.table.Put)53 Test (org.junit.Test)24 Table (io.cdap.cdap.api.dataset.table.Table)23 Get (io.cdap.cdap.api.dataset.table.Get)13 Row (io.cdap.cdap.api.dataset.table.Row)13 Transaction (org.apache.tephra.Transaction)13 TransactionAware (org.apache.tephra.TransactionAware)13 Schema (io.cdap.cdap.api.data.schema.Schema)10 TransactionExecutor (org.apache.tephra.TransactionExecutor)10 DatasetAdmin (io.cdap.cdap.api.dataset.DatasetAdmin)7 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)6 IOException (java.io.IOException)6 HBaseTable (io.cdap.cdap.data2.dataset2.lib.table.hbase.HBaseTable)5 DatasetId (io.cdap.cdap.proto.id.DatasetId)5 WriteOnly (io.cdap.cdap.api.annotation.WriteOnly)4 DataSetException (io.cdap.cdap.api.dataset.DataSetException)4 Scanner (io.cdap.cdap.api.dataset.table.Scanner)4 MDSKey (io.cdap.cdap.data2.dataset2.lib.table.MDSKey)4 ReflectionPutWriter (io.cdap.cdap.internal.io.ReflectionPutWriter)4 Map (java.util.Map)4