Search in sources :

Example 61 with Scanner

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

the class MetadataDataset method getMetadata.

/**
 * Retrieves the metadata for the specified {@link NamespacedEntityId}.
 *
 * @param targetId the specified {@link NamespacedEntityId}
 * @return a Map representing the metadata for the specified {@link NamespacedEntityId}
 */
private Map<String, String> getMetadata(NamespacedEntityId targetId) {
    String targetType = EntityIdKeyHelper.getTargetType(targetId);
    MDSKey mdsKey = MdsKey.getMDSValueKey(targetId, null);
    byte[] startKey = mdsKey.getKey();
    byte[] stopKey = Bytes.stopKeyForPrefix(startKey);
    Map<String, String> metadata = new HashMap<>();
    try (Scanner scan = indexedTable.scan(startKey, stopKey)) {
        Row next;
        while ((next = scan.next()) != null) {
            String key = MdsKey.getMetadataKey(targetType, next.getRow());
            byte[] value = next.get(VALUE_COLUMN);
            if (key == null || value == null) {
                continue;
            }
            metadata.put(key, Bytes.toString(value));
        }
        return metadata;
    }
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) HashMap(java.util.HashMap) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) Row(co.cask.cdap.api.dataset.table.Row)

Example 62 with Scanner

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

the class MetadataDataset method removeMetadata.

/**
 * Removes all keys that satisfy a given predicate from the metadata of the specified {@link NamespacedEntityId}.
 *
 * @param targetId the {@link NamespacedEntityId} for which keys are to be removed
 * @param filter the {@link Predicate} that should be satisfied to remove a key
 */
private void removeMetadata(NamespacedEntityId targetId, Predicate<String> filter) {
    String targetType = EntityIdKeyHelper.getTargetType(targetId);
    MDSKey mdsKey = MdsKey.getMDSValueKey(targetId, null);
    byte[] prefix = mdsKey.getKey();
    byte[] stopKey = Bytes.stopKeyForPrefix(prefix);
    List<String> deletedMetadataKeys = new LinkedList<>();
    try (Scanner scan = indexedTable.scan(prefix, stopKey)) {
        Row next;
        while ((next = scan.next()) != null) {
            String value = next.getString(VALUE_COLUMN);
            if (value == null) {
                continue;
            }
            String metadataKey = MdsKey.getMetadataKey(targetType, next.getRow());
            if (filter.apply(metadataKey)) {
                indexedTable.delete(new Delete(next.getRow()));
                // store the key to delete its indexes later
                deletedMetadataKeys.add(metadataKey);
            }
        }
    }
    // delete all the indexes for all deleted metadata key
    for (String deletedMetadataKey : deletedMetadataKeys) {
        deleteIndexes(targetId, deletedMetadataKey);
    }
    writeHistory(targetId);
}
Also used : Delete(co.cask.cdap.api.dataset.table.Delete) Scanner(co.cask.cdap.api.dataset.table.Scanner) MDSKey(co.cask.cdap.data2.dataset2.lib.table.MDSKey) Row(co.cask.cdap.api.dataset.table.Row) LinkedList(java.util.LinkedList)

Example 63 with Scanner

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

the class LevelDBTable method scanPersisted.

@ReadOnly
@Override
protected Scanner scanPersisted(Scan scan) throws Exception {
    FuzzyRowFilter filter = null;
    if (scan.getFilter() != null) {
        // todo: currently we support only FuzzyRowFilter as an experimental feature
        if (scan.getFilter() instanceof FuzzyRowFilter) {
            filter = (FuzzyRowFilter) scan.getFilter();
        } else {
            throw new DataSetException("Unknown filter type: " + scan.getFilter());
        }
    }
    final Scanner scanner = core.scan(scan.getStartRow(), scan.getStopRow(), filter, null, tx);
    return new Scanner() {

        @Nullable
        @Override
        public Row next() {
            return LevelDBTable.this.next(scanner);
        }

        @Override
        public void close() {
            scanner.close();
        }
    };
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) DataSetException(co.cask.cdap.api.dataset.DataSetException) FuzzyRowFilter(co.cask.cdap.data2.dataset2.lib.table.FuzzyRowFilter) ReadOnly(co.cask.cdap.api.annotation.ReadOnly)

Example 64 with Scanner

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

the class LineageDataset method getEntitiesForRun.

/**
 * @return a set of entities (program and data it accesses) associated with a program run.
 */
public Set<NamespacedEntityId> getEntitiesForRun(ProgramRunId run) {
    ImmutableSet.Builder<NamespacedEntityId> recordBuilder = ImmutableSet.builder();
    byte[] startKey = getRunScanStartKey(run);
    try (Scanner scanner = accessRegistryTable.scan(startKey, Bytes.stopKeyForPrefix(startKey))) {
        Row row;
        while ((row = scanner.next()) != null) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Got row key = {}", Bytes.toString(row.getRow()));
            }
            RowKey rowKey = parseRow(row);
            if (run.getEntityName().equals(rowKey.getRunId().getId())) {
                recordBuilder.add(rowKey.getProgram());
                recordBuilder.add(rowKey.getData());
            }
        }
    }
    return recordBuilder.build();
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) NamespacedEntityId(co.cask.cdap.proto.id.NamespacedEntityId) ImmutableSet(com.google.common.collect.ImmutableSet) Row(co.cask.cdap.api.dataset.table.Row)

Example 65 with Scanner

use of co.cask.cdap.api.dataset.table.Scanner 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.test(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)

Aggregations

Scanner (co.cask.cdap.api.dataset.table.Scanner)78 Row (co.cask.cdap.api.dataset.table.Row)67 Scan (co.cask.cdap.api.dataset.table.Scan)14 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)13 Table (co.cask.cdap.api.dataset.table.Table)12 Map (java.util.Map)11 DatasetId (co.cask.cdap.proto.id.DatasetId)8 TransactionExecutor (org.apache.tephra.TransactionExecutor)8 MDSKey (co.cask.cdap.data2.dataset2.lib.table.MDSKey)6 QueueEntryRow (co.cask.cdap.data2.transaction.queue.QueueEntryRow)6 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 Put (co.cask.cdap.api.dataset.table.Put)5 ImmutableMap (com.google.common.collect.ImmutableMap)5 SortedMap (java.util.SortedMap)5 DatasetProperties (co.cask.cdap.api.dataset.DatasetProperties)4 Get (co.cask.cdap.api.dataset.table.Get)4 FuzzyRowFilter (co.cask.cdap.data2.dataset2.lib.table.FuzzyRowFilter)4 ProgramSchedule (co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule)4