Search in sources :

Example 1 with Result

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

the class TableTest method testEmptyGet.

@Test
public void testEmptyGet() throws Exception {
    DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
    admin.create();
    try {
        Transaction tx = txClient.startShort();
        Table myTable = getTable(CONTEXT1, MY_TABLE);
        ((TransactionAware) myTable).startTx(tx);
        myTable.put(R1, C1, V1);
        myTable.put(R1, C2, V2);
        // to be used for validation later
        TreeMap<byte[], byte[]> expectedColumns = new TreeMap<>(Bytes.BYTES_COMPARATOR);
        expectedColumns.put(C1, V1);
        expectedColumns.put(C2, V2);
        Result expectedResult = new Result(R1, expectedColumns);
        Result emptyResult = new Result(R1, ImmutableMap.<byte[], byte[]>of());
        ((TransactionAware) myTable).commitTx();
        txClient.commitOrThrow(tx);
        // start another transaction, so that the buffering table doesn't cache the values; the underlying Table
        // implementations are tested this way.
        tx = txClient.startShort();
        ((TransactionAware) myTable).startTx(tx);
        Row row = myTable.get(R1, new byte[][] { C1, C2 });
        assertEquals(expectedResult, row);
        // passing in empty columns returns empty result
        row = myTable.get(R1, new byte[][] {});
        assertEquals(emptyResult, row);
        // test all the Get constructors and their behavior
        // constructors specifying only rowkey retrieve all columns
        Get get = new Get(R1);
        Assert.assertNull(get.getColumns());
        assertEquals(expectedResult, myTable.get(get));
        get = new Get(Bytes.toString(R1));
        Assert.assertNull(get.getColumns());
        assertEquals(expectedResult, myTable.get(get));
        get.add(C1);
        get.add(Bytes.toString(C2));
        assertEquals(expectedResult, myTable.get(get));
        // constructor specifying columns, but with an empty array/collection retrieve 0 columns
        get = new Get(R1, new byte[][] {});
        Assert.assertNotNull(get.getColumns());
        assertEquals(emptyResult, myTable.get(get));
        get = new Get(R1, ImmutableList.<byte[]>of());
        Assert.assertNotNull(get.getColumns());
        assertEquals(emptyResult, myTable.get(get));
        get = new Get(Bytes.toString(R1), new String[] {});
        Assert.assertNotNull(get.getColumns());
        assertEquals(emptyResult, myTable.get(get));
        get = new Get(Bytes.toString(R1), ImmutableList.<String>of());
        Assert.assertNotNull(get.getColumns());
        assertEquals(emptyResult, myTable.get(get));
        row = myTable.get(R1, new byte[][] {});
        assertEquals(emptyResult, row);
        txClient.abort(tx);
    } finally {
        admin.drop();
    }
}
Also used : Table(co.cask.cdap.api.dataset.table.Table) HBaseTable(co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable) Transaction(org.apache.tephra.Transaction) TransactionAware(org.apache.tephra.TransactionAware) Get(co.cask.cdap.api.dataset.table.Get) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) Row(co.cask.cdap.api.dataset.table.Row) TreeMap(java.util.TreeMap) Result(co.cask.cdap.api.dataset.table.Result) Test(org.junit.Test)

Example 2 with Result

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

the class BufferingTable method internalIncrementAndGet.

@ReadWrite
protected Row internalIncrementAndGet(byte[] row, byte[][] columns, long[] amounts) {
    // Logic:
    // * fetching current values
    // * updating values
    // * updating in-memory store
    // * returning updated values as result
    // NOTE: there is more efficient way to do it, but for now we want more simple implementation, not over-optimizing
    Map<byte[], byte[]> rowMap;
    try {
        rowMap = getRowMap(row, columns);
        reportRead(1);
    } catch (Exception e) {
        LOG.debug("incrementAndGet failed for table: " + getTransactionAwareName() + ", row: " + Bytes.toStringBinary(row), e);
        throw new DataSetException("incrementAndGet failed", e);
    }
    byte[][] updatedValues = new byte[columns.length][];
    NavigableMap<byte[], byte[]> result = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    for (int i = 0; i < columns.length; i++) {
        byte[] column = columns[i];
        byte[] val = rowMap.get(column);
        // converting to long
        long longVal;
        if (val == null) {
            longVal = 0L;
        } else {
            if (val.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(column));
            }
            longVal = Bytes.toLong(val);
        }
        longVal += amounts[i];
        updatedValues[i] = Bytes.toBytes(longVal);
        result.put(column, updatedValues[i]);
    }
    putInternal(row, columns, updatedValues);
    reportWrite(1, getSize(row) + getSize(columns) + getSize(amounts));
    return new Result(row, result);
}
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) ReadWrite(co.cask.cdap.api.annotation.ReadWrite)

Example 3 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[] startColumn, byte[] stopColumn, int limit) {
    ensureTransactionIsStarted();
    reportRead(1);
    // checking if the row was deleted inside this tx
    NavigableMap<byte[], Update> buffCols = buff.get(row);
    // potential improvement: do not fetch columns available in in-mem buffer (we know them at this point)
    try {
        Map<byte[], byte[]> persistedCols = getPersisted(row, startColumn, stopColumn, limit);
        // adding server cols, and then overriding with buffered values
        NavigableMap<byte[], byte[]> result = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
        if (persistedCols != null) {
            result.putAll(persistedCols);
        }
        if (buffCols != null) {
            buffCols = getRange(buffCols, startColumn, stopColumn, limit);
            // null valued columns in in-memory buffer are deletes, so we need to delete them from the result list
            mergeToPersisted(result, buffCols, null);
        }
        // applying limit
        return new Result(row, head(result, limit));
    } 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 4 with Result

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

the class BufferingTable method get.

/**
 * NOTE: Depending on the use-case, calling this method may be much less
 *       efficient than calling same method with columns as parameters because it may always require round trip to
 *       persistent store
 */
@ReadOnly
@Override
public Row get(byte[] row) {
    ensureTransactionIsStarted();
    reportRead(1);
    try {
        return new Result(row, getRowMap(row));
    } 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 5 with Result

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

the class BufferingTable method get.

@ReadOnly
@Override
public List<Row> get(List<Get> gets) {
    ensureTransactionIsStarted();
    try {
        // get persisted, then overwrite with whats buffered
        List<Map<byte[], byte[]>> persistedRows = getPersisted(gets);
        // gets and rows lists are always of the same size
        Preconditions.checkArgument(gets.size() == persistedRows.size(), "Invalid number of rows fetched when performing multi-get. There must be one row for each get.");
        List<Row> result = Lists.newArrayListWithCapacity(persistedRows.size());
        Iterator<Map<byte[], byte[]>> persistedRowsIter = persistedRows.iterator();
        Iterator<Get> getIter = gets.iterator();
        while (persistedRowsIter.hasNext() && getIter.hasNext()) {
            Get get = getIter.next();
            Map<byte[], byte[]> persistedRow = persistedRowsIter.next();
            // navigable copy of the persisted data. Implementation may return immutable or unmodifiable maps,
            // so we make a copy here.
            NavigableMap<byte[], byte[]> rowColumns = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
            rowColumns.putAll(persistedRow);
            byte[] row = get.getRow();
            NavigableMap<byte[], Update> buffCols = buff.get(row);
            // merge what was in the buffer and what was persisted
            if (buffCols != null) {
                List<byte[]> getColumns = get.getColumns();
                byte[][] columns = getColumns == null ? null : getColumns.toArray(new byte[getColumns.size()][]);
                mergeToPersisted(rowColumns, buffCols, columns);
            }
            result.add(new Result(row, unwrapDeletes(rowColumns)));
        }
        return result;
    } catch (Exception e) {
        LOG.debug("multi-get failed for table: " + getTransactionAwareName(), e);
        throw new DataSetException("multi-get failed", e);
    }
}
Also used : IOException(java.io.IOException) DataSetException(co.cask.cdap.api.dataset.DataSetException) Result(co.cask.cdap.api.dataset.table.Result) DataSetException(co.cask.cdap.api.dataset.DataSetException) Get(co.cask.cdap.api.dataset.table.Get) Row(co.cask.cdap.api.dataset.table.Row) Map(java.util.Map) NavigableMap(java.util.NavigableMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) ReadOnly(co.cask.cdap.api.annotation.ReadOnly)

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