Search in sources :

Example 66 with Row

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

the class PartitionedFileSetDataset method scannerToPartitions.

/**
   * While applying a partition filter and a limit, parse partitions from the rows of a scanner and add them to a list.
   * Note that multiple partitions can have the same transaction write pointer. For each set of partitions with the same
   * write pointer, we either add the entire set or exclude the entire set. The limit is applied after adding each such
   * set of partitions to the list.
   *
   * @param scanner the scanner on the partitions table from which to read partitions
   * @param partitions list to add the qualifying partitions to
   * @param limit limit, which once reached, partitions committed by other transactions will not be added.
   *              The limit is checked after adding consuming all partitions of a transaction, so
   *              the total number of consumed partitions may be greater than this limit.
   * @param predicate predicate to apply before adding to the partitions list
   * @return Transaction ID of the partition that we reached in the scanner, but did not add to the list. This value
   *         can be useful in future scans.
   */
@Nullable
private Long scannerToPartitions(Scanner scanner, List<PartitionDetail> partitions, int limit, Predicate<PartitionDetail> predicate) {
    Long prevTxId = null;
    Row row;
    while ((row = scanner.next()) != null) {
        PartitionKey key = parseRowKey(row.getRow(), partitioning);
        String relativePath = Bytes.toString(row.get(RELATIVE_PATH));
        Long txId = Bytes.toLong(row.get(WRITE_PTR_COL));
        // by a transaction or none, since we keep our marker based upon transaction id.
        if (prevTxId != null && !prevTxId.equals(txId)) {
            if (partitions.size() >= limit) {
                return txId;
            }
        }
        prevTxId = txId;
        BasicPartitionDetail partitionDetail = new BasicPartitionDetail(PartitionedFileSetDataset.this, relativePath, key, metadataFromRow(row));
        if (!predicate.apply(partitionDetail)) {
            continue;
        }
        partitions.add(partitionDetail);
    }
    return null;
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) PartitionKey(co.cask.cdap.api.dataset.lib.PartitionKey) Row(co.cask.cdap.api.dataset.table.Row) Nullable(javax.annotation.Nullable)

Example 67 with Row

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

the class MetadataStoreDataset method deleteAll.

public void deleteAll(MDSKey id, @Nullable Predicate<MDSKey> filter) {
    byte[] prefix = id.getKey();
    byte[] stopKey = Bytes.stopKeyForPrefix(prefix);
    try {
        try (Scanner scan = table.scan(prefix, stopKey)) {
            Row next;
            while ((next = scan.next()) != null) {
                String columnValue = next.getString(COLUMN);
                if (columnValue == null) {
                    continue;
                }
                MDSKey key = new MDSKey(next.getRow());
                if (filter != null && !filter.apply(key)) {
                    continue;
                }
                table.delete(new Delete(next.getRow()).add(COLUMN));
            }
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Delete(co.cask.cdap.api.dataset.table.Delete) Scanner(co.cask.cdap.api.dataset.table.Scanner) Row(co.cask.cdap.api.dataset.table.Row)

Example 68 with Row

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

the class MetadataStoreDataset method listKV.

private <T> Map<MDSKey, T> listKV(Scan runScan, Type typeOfT, int limit, @Nullable Predicate<MDSKey> keyFilter, @Nullable Predicate<T> valueFilter) {
    try {
        Map<MDSKey, T> map = Maps.newLinkedHashMap();
        try (Scanner scan = table.scan(runScan)) {
            Row next;
            while ((limit > 0) && (next = scan.next()) != null) {
                MDSKey key = new MDSKey(next.getRow());
                byte[] columnValue = next.get(COLUMN);
                if (columnValue == null) {
                    continue;
                }
                T value = deserialize(columnValue, typeOfT);
                // Key Filter doesn't pass
                if (keyFilter != null && !keyFilter.apply(key)) {
                    continue;
                }
                // If Value Filter doesn't pass
                if (valueFilter != null && !valueFilter.apply(value)) {
                    continue;
                }
                map.put(key, value);
                limit--;
            }
            return map;
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) Row(co.cask.cdap.api.dataset.table.Row)

Example 69 with Row

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

the class MetadataStoreDataset method exists.

public boolean exists(MDSKey id) {
    Row row = table.get(id.getKey());
    if (row.isEmpty()) {
        return false;
    }
    byte[] value = row.get(COLUMN);
    if (value == null) {
        return false;
    }
    return true;
}
Also used : Row(co.cask.cdap.api.dataset.table.Row)

Example 70 with Row

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

the class MetadataStoreDataset method listCombinedFilterKV.

private <T> Map<MDSKey, T> listCombinedFilterKV(Scan runScan, Type typeOfT, int limit, @Nullable Predicate<KeyValue<T>> combinedFilter) {
    try {
        Map<MDSKey, T> map = Maps.newLinkedHashMap();
        try (Scanner scan = table.scan(runScan)) {
            Row next;
            while ((limit > 0) && (next = scan.next()) != null) {
                MDSKey key = new MDSKey(next.getRow());
                byte[] columnValue = next.get(COLUMN);
                if (columnValue == null) {
                    continue;
                }
                T value = deserialize(columnValue, typeOfT);
                KeyValue<T> kv = new KeyValue<>(key, value);
                // Combined Filter doesn't pass
                if (combinedFilter != null && !combinedFilter.apply(kv)) {
                    continue;
                }
                map.put(kv.getKey(), kv.getValue());
                limit--;
            }
            return map;
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) Row(co.cask.cdap.api.dataset.table.Row)

Aggregations

Row (co.cask.cdap.api.dataset.table.Row)111 Scanner (co.cask.cdap.api.dataset.table.Scanner)60 Test (org.junit.Test)23 Table (co.cask.cdap.api.dataset.table.Table)20 Get (co.cask.cdap.api.dataset.table.Get)16 ArrayList (java.util.ArrayList)16 TransactionExecutor (org.apache.tephra.TransactionExecutor)16 Map (java.util.Map)15 Put (co.cask.cdap.api.dataset.table.Put)14 HashMap (java.util.HashMap)10 Scan (co.cask.cdap.api.dataset.table.Scan)9 TransactionAware (org.apache.tephra.TransactionAware)9 MDSKey (co.cask.cdap.data2.dataset2.lib.table.MDSKey)8 QueueEntryRow (co.cask.cdap.data2.transaction.queue.QueueEntryRow)8 DatasetId (co.cask.cdap.proto.id.DatasetId)8 IOException (java.io.IOException)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 Transaction (org.apache.tephra.Transaction)7 WriteOnly (co.cask.cdap.api.annotation.WriteOnly)6 Schema (co.cask.cdap.api.data.schema.Schema)6