Search in sources :

Example 6 with Result

use of co.cask.cdap.api.dataset.table.Result in project cdap by caskdata.

the class BufferingTable method get.

@ReadOnly
@Override
public Row get(byte[] row, byte[][] columns) {
    ensureTransactionIsStarted();
    reportRead(1);
    try {
        return new Result(row, getRowMap(row, columns));
    } catch (Exception e) {
        LOG.debug("get failed for table: " + getTransactionAwareName() + ", row: " + Bytes.toStringBinary(row), e);
        throw new DataSetException("get failed", e);
    }
}
Also used : DataSetException(co.cask.cdap.api.dataset.DataSetException) IOException(java.io.IOException) DataSetException(co.cask.cdap.api.dataset.DataSetException) Result(co.cask.cdap.api.dataset.table.Result) ReadOnly(co.cask.cdap.api.annotation.ReadOnly)

Example 7 with Result

use of co.cask.cdap.api.dataset.table.Result in project cdap by caskdata.

the class IndexedTable method incrementAndGet.

/**
 * Increments (atomically) the specified row and columns by the specified amounts, and returns the new values.
 * Note that performing this operation on an indexed column will generally have a negative impact on performance,
 * since up to three writes will need to be performed for every increment (one removing the index for the previous,
 * pre-increment value, one adding the index for the incremented value, and one for the increment itself).
 *
 * @see Table#incrementAndGet(byte[], byte[][], long[])
 */
@ReadWrite
@Override
public Row incrementAndGet(byte[] row, byte[][] columns, long[] amounts) {
    if (columns.length != amounts.length) {
        throw new IllegalArgumentException("Size of columns and amounts arguments must match");
    }
    Row existingRow = table.get(row, columns);
    byte[][] updatedValues = new byte[columns.length][];
    NavigableMap<byte[], byte[]> result = new TreeMap<>(Bytes.BYTES_COMPARATOR);
    for (int i = 0; i < columns.length; i++) {
        long existingValue = 0L;
        byte[] existingBytes = existingRow.get(columns[i]);
        if (existingBytes != null) {
            if (existingBytes.length != Bytes.SIZEOF_LONG) {
                throw new NumberFormatException("Attempted to increment a value that is not convertible to long," + " row: " + Bytes.toStringBinary(row) + " column: " + Bytes.toStringBinary(columns[i]));
            }
            existingValue = Bytes.toLong(existingBytes);
            if (indexedColumns.contains(columns[i])) {
                index.delete(createIndexKey(row, columns[i], existingBytes), IDX_COL);
            }
        }
        updatedValues[i] = Bytes.toBytes(existingValue + amounts[i]);
        result.put(columns[i], updatedValues[i]);
        if (indexedColumns.contains(columns[i])) {
            index.put(createIndexKey(row, columns[i], updatedValues[i]), IDX_COL, row);
        }
    }
    table.put(row, columns, updatedValues);
    return new Result(row, result);
}
Also used : Row(co.cask.cdap.api.dataset.table.Row) TreeMap(java.util.TreeMap) Result(co.cask.cdap.api.dataset.table.Result) ReadWrite(co.cask.cdap.api.annotation.ReadWrite)

Aggregations

Result (co.cask.cdap.api.dataset.table.Result)7 DataSetException (co.cask.cdap.api.dataset.DataSetException)5 IOException (java.io.IOException)5 ReadOnly (co.cask.cdap.api.annotation.ReadOnly)4 Row (co.cask.cdap.api.dataset.table.Row)3 ReadWrite (co.cask.cdap.api.annotation.ReadWrite)2 Get (co.cask.cdap.api.dataset.table.Get)2 TreeMap (java.util.TreeMap)2 DatasetAdmin (co.cask.cdap.api.dataset.DatasetAdmin)1 Table (co.cask.cdap.api.dataset.table.Table)1 HBaseTable (co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable)1 Map (java.util.Map)1 NavigableMap (java.util.NavigableMap)1 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)1 Transaction (org.apache.tephra.Transaction)1 TransactionAware (org.apache.tephra.TransactionAware)1 Test (org.junit.Test)1