Search in sources :

Example 21 with ReadOnly

use of io.cdap.cdap.api.annotation.ReadOnly in project cdap by caskdata.

the class IndexedObjectStore method readAllByIndex.

/**
 * Read all the objects from the objectStore for a given index. Returns all the objects that match the secondaryKey.
 * Returns an empty list if no values are found. Never returns null.
 *
 * @param secondaryKey for the lookup.
 * @return List of Objects matching the secondaryKey.
 */
@ReadOnly
public List<T> readAllByIndex(byte[] secondaryKey) {
    List<T> resultList = new ArrayList<>();
    // Lookup the secondaryKey and get all the keys in primary
    // Each row with secondaryKey as rowKey contains column named as the primary key
    // of every object that can be looked up using the secondaryKey
    Row row = index.get(secondaryKey);
    // if the index has no match, return nothing
    if (!row.isEmpty()) {
        resultList = row.getColumns().keySet().stream().map(objectStore::read).collect(Collectors.toList());
    }
    return Collections.unmodifiableList(resultList);
}
Also used : ArrayList(java.util.ArrayList) Row(io.cdap.cdap.api.dataset.table.Row) ReadOnly(io.cdap.cdap.api.annotation.ReadOnly)

Example 22 with ReadOnly

use of io.cdap.cdap.api.annotation.ReadOnly in project cdap by cdapio.

the class IndexedTable method scanByIndex.

/**
 * Reads table rows within the given secondary index key range. If no rows are indexed, falling within the given
 * range, then a {@link io.cdap.cdap.api.dataset.table.Scanner} with no results will be returned.
 *
 * @param column the column to use for the index lookup
 * @param startValue the inclusive start of the range for which rows must fall within to be returned in the scan.
 *                   {@code null} means start from first row of the table
 * @param endValue the exclusive end of the range for which rows must fall within to be returned in the scan
 *                 {@code null} means end with the last row of the table
 * @return a Scanner returning rows from the data table, whose stored value for the given column is within the the
 *         given range.
 * @throws java.lang.IllegalArgumentException if the given column is not configured for indexing.
 */
@ReadOnly
public Scanner scanByIndex(byte[] column, @Nullable byte[] startValue, @Nullable byte[] endValue) {
    assertIndexedColumn(column);
    // KEY_DELIMITER is not used at the end of the rowKeys, because they are used for a range scan,
    // instead of a fixed-match lookup
    byte[] startRow = startValue == null ? Bytes.concat(keyPrefix, column, KEY_DELIMITER) : Bytes.concat(keyPrefix, column, KEY_DELIMITER, startValue);
    byte[] stopRow = endValue == null ? Bytes.stopKeyForPrefix(Bytes.concat(keyPrefix, column, KEY_DELIMITER)) : Bytes.concat(keyPrefix, column, KEY_DELIMITER, endValue);
    Scanner indexScan = index.scan(startRow, stopRow);
    return new IndexRangeScanner(indexScan, column, startValue, endValue);
}
Also used : Scanner(io.cdap.cdap.api.dataset.table.Scanner) RecordScanner(io.cdap.cdap.api.data.batch.RecordScanner) ReadOnly(io.cdap.cdap.api.annotation.ReadOnly)

Example 23 with ReadOnly

use of io.cdap.cdap.api.annotation.ReadOnly in project cdap by cdapio.

the class PartitionedFileSetDataset method assertNotExists.

// Throws PartitionAlreadyExistsException if the partition key already exists.
// Otherwise, returns the rowkey corresponding to the PartitionKey.
@ReadOnly
byte[] assertNotExists(PartitionKey key, boolean supportNonTransactional) {
    byte[] rowKey = generateRowKey(key, partitioning);
    if (tx == null && supportNonTransactional) {
        if (LOG.isWarnEnabled()) {
            StringBuilder sb = new StringBuilder();
            for (StackTraceElement stackTraceElement : Thread.currentThread().getStackTrace()) {
                sb.append("\n\tat ").append(stackTraceElement.toString());
            }
            SAMPLING_LOG.warn("Operation should be performed within a transaction. " + "This operation may require a transaction in the future. {}", sb);
        }
        // to handle backwards compatibility (user might have called PartitionedFileSet#getPartitionOutput outside
        // of a transaction), we can't check partition existence via the partitionsTable. As an fallback approach,
        // check the filesystem.
        Location partitionLocation = files.getLocation(getOutputPath(key));
        if (exists(partitionLocation)) {
            throw new DataSetException(String.format("Location %s for partition key %s already exists: ", partitionLocation, key));
        }
    } else {
        Row row = partitionsTable.get(rowKey);
        if (!row.isEmpty()) {
            throw new PartitionAlreadyExistsException(getName(), key);
        }
    }
    return rowKey;
}
Also used : DataSetException(io.cdap.cdap.api.dataset.DataSetException) Row(io.cdap.cdap.api.dataset.table.Row) PartitionAlreadyExistsException(io.cdap.cdap.api.dataset.lib.PartitionAlreadyExistsException) Location(org.apache.twill.filesystem.Location) ReadOnly(io.cdap.cdap.api.annotation.ReadOnly)

Example 24 with ReadOnly

use of io.cdap.cdap.api.annotation.ReadOnly in project cdap by cdapio.

the class BufferingTable method scan.

@ReadOnly
@Override
public Scanner scan(Scan scan) {
    ensureTransactionIsStarted();
    NavigableMap<byte[], NavigableMap<byte[], Update>> bufferMap = scanBuffer(scan);
    try {
        return new BufferingScanner(bufferMap, scanPersisted(scan));
    } catch (Exception e) {
        LOG.debug("scan failed for table: " + getTransactionAwareName() + ", scan: " + scan.toString(), e);
        throw new DataSetException("scan failed", e);
    }
}
Also used : NavigableMap(java.util.NavigableMap) DataSetException(io.cdap.cdap.api.dataset.DataSetException) DataSetException(io.cdap.cdap.api.dataset.DataSetException) IOException(java.io.IOException) ReadOnly(io.cdap.cdap.api.annotation.ReadOnly)

Example 25 with ReadOnly

use of io.cdap.cdap.api.annotation.ReadOnly in project cdap by cdapio.

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(io.cdap.cdap.api.dataset.DataSetException) DataSetException(io.cdap.cdap.api.dataset.DataSetException) IOException(java.io.IOException) Result(io.cdap.cdap.api.dataset.table.Result) ReadOnly(io.cdap.cdap.api.annotation.ReadOnly)

Aggregations

ReadOnly (io.cdap.cdap.api.annotation.ReadOnly)26 DataSetException (io.cdap.cdap.api.dataset.DataSetException)14 IOException (java.io.IOException)10 Result (io.cdap.cdap.api.dataset.table.Result)8 Row (io.cdap.cdap.api.dataset.table.Row)8 Scanner (io.cdap.cdap.api.dataset.table.Scanner)6 RecordScanner (io.cdap.cdap.api.data.batch.RecordScanner)4 NavigableMap (java.util.NavigableMap)4 PartitionAlreadyExistsException (io.cdap.cdap.api.dataset.lib.PartitionAlreadyExistsException)2 Get (io.cdap.cdap.api.dataset.table.Get)2 FuzzyRowFilter (io.cdap.cdap.data2.dataset2.lib.table.FuzzyRowFilter)2 ScanBuilder (io.cdap.cdap.data2.util.hbase.ScanBuilder)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)2 Get (org.apache.hadoop.hbase.client.Get)2 Result (org.apache.hadoop.hbase.client.Result)2 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)2 Location (org.apache.twill.filesystem.Location)2