Search in sources :

Example 11 with Row

use of io.cdap.cdap.api.dataset.table.Row 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 MetadataEntity}.
 * @param metadataEntity the {@link MetadataEntity} for which keys are to be removed
 * @param filter the {@link Predicate} that should be satisfied to remove a key
 */
private Change removeMetadata(MetadataEntity metadataEntity, Predicate<String> filter) {
    MDSKey mdsKey = MetadataKey.createValueRowKey(metadataEntity, null);
    byte[] prefix = mdsKey.getKey();
    byte[] stopKey = Bytes.stopKeyForPrefix(prefix);
    Map<String, String> existingMetadata = new HashMap<>();
    Map<String, String> deletedMetadata = new HashMap<>();
    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 = MetadataKey.extractMetadataKey(next.getRow());
            // put all the metadata for this entity as existing
            existingMetadata.put(metadataKey, value);
            if (filter.test(metadataKey)) {
                // if the key matches the key to be deleted delete it and put it in deleted
                indexedTable.delete(new Delete(next.getRow()));
                // store the key to delete its indexes later
                deletedMetadata.put(metadataKey, value);
            }
        }
    }
    // current metadata is existing - deleted
    Map<String, String> currentMetadata = new HashMap<>(existingMetadata);
    // delete all the indexes for all deleted metadata key
    for (String deletedMetadataKey : deletedMetadata.keySet()) {
        deleteIndexes(metadataEntity, deletedMetadataKey);
        currentMetadata.remove(deletedMetadataKey);
    }
    Record changedMetadata = getMetadata(metadataEntity, currentMetadata);
    writeHistory(changedMetadata);
    return new Change(getMetadata(metadataEntity, existingMetadata), changedMetadata);
}
Also used : Delete(io.cdap.cdap.api.dataset.table.Delete) Scanner(io.cdap.cdap.api.dataset.table.Scanner) HashMap(java.util.HashMap) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) Row(io.cdap.cdap.api.dataset.table.Row)

Example 12 with Row

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

the class MetadataDataset method searchByDefaultIndex.

private SearchResults searchByDefaultIndex(SearchRequest request) {
    List<MetadataEntry> results = new LinkedList<>();
    String column = request.isNamespaced() ? DEFAULT_INDEX_COLUMN.getColumn() : DEFAULT_INDEX_COLUMN.getCrossNamespaceColumn();
    for (SearchTerm searchTerm : getSearchTerms(request)) {
        Scanner scanner;
        if (searchTerm.isPrefix()) {
            // if prefixed search get start and stop key
            byte[] startKey = Bytes.toBytes(searchTerm.getTerm());
            @SuppressWarnings("ConstantConditions") byte[] stopKey = Bytes.stopKeyForPrefix(startKey);
            scanner = indexedTable.scanByIndex(Bytes.toBytes(column), startKey, stopKey);
        } else {
            byte[] value = Bytes.toBytes(searchTerm.getTerm());
            scanner = indexedTable.readByIndex(Bytes.toBytes(column), value);
        }
        try {
            Row next;
            while ((next = scanner.next()) != null) {
                Optional<MetadataEntry> metadataEntry = parseRow(next, column, request.getTypes(), request.shouldShowHidden());
                metadataEntry.ifPresent(results::add);
            }
        } finally {
            scanner.close();
        }
    }
    // cursors are currently not supported for default indexes
    return new SearchResults(results, Collections.emptyList());
}
Also used : Scanner(io.cdap.cdap.api.dataset.table.Scanner) Row(io.cdap.cdap.api.dataset.table.Row) LinkedList(java.util.LinkedList)

Example 13 with Row

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

the class MetadataDataset method deleteIndexes.

/**
 * Deletes all indexes associated with a metadata key
 *
 * @param metadataEntity the {@link MetadataEntity} for which keys are to be removed
 * @param metadataKey the key to remove from the metadata of the specified {@link MetadataEntity}
 */
private void deleteIndexes(MetadataEntity metadataEntity, String metadataKey) {
    MDSKey mdsKey = MetadataKey.createIndexRowKey(metadataEntity, metadataKey, null);
    byte[] startKey = mdsKey.getKey();
    byte[] stopKey = Bytes.stopKeyForPrefix(startKey);
    try (Scanner scan = indexedTable.scan(startKey, stopKey)) {
        Row next;
        while ((next = scan.next()) != null) {
            deleteIndexRow(next);
        }
    }
}
Also used : Scanner(io.cdap.cdap.api.dataset.table.Scanner) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) Row(io.cdap.cdap.api.dataset.table.Row)

Example 14 with Row

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

the class MetadataDataset method getSnapshotBeforeTime.

private Record getSnapshotBeforeTime(MetadataEntity metadataEntity, long timeMillis) {
    byte[] scanStartKey = MetadataHistoryKey.getMDSScanStartKey(metadataEntity, timeMillis).getKey();
    byte[] scanEndKey = MetadataHistoryKey.getMDSScanStopKey(metadataEntity).getKey();
    // TODO: add limit to scan, we need only one row
    try (Scanner scanner = indexedTable.scan(scanStartKey, scanEndKey)) {
        Row next = scanner.next();
        if (next != null) {
            return GSON.fromJson(next.getString(HISTORY_COLUMN), Record.class);
        } else {
            return new Record(metadataEntity);
        }
    }
}
Also used : Scanner(io.cdap.cdap.api.dataset.table.Scanner) Row(io.cdap.cdap.api.dataset.table.Row)

Example 15 with Row

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

the class MetadataStoreDataset method exists.

/**
 * Check whether the value is there in the row and default COLUMN
 *
 * @param id the mds key for the row
 * @return a boolean which indicates the value exists or not
 */
public boolean exists(MDSKey id) {
    Row row = table.get(id.getKey());
    if (row.isEmpty()) {
        return false;
    }
    byte[] value = row.get(COLUMN);
    return value != null;
}
Also used : Row(io.cdap.cdap.api.dataset.table.Row)

Aggregations

Row (io.cdap.cdap.api.dataset.table.Row)166 Scanner (io.cdap.cdap.api.dataset.table.Scanner)81 Test (org.junit.Test)50 Table (io.cdap.cdap.api.dataset.table.Table)34 Put (io.cdap.cdap.api.dataset.table.Put)29 ArrayList (java.util.ArrayList)26 TransactionExecutor (org.apache.tephra.TransactionExecutor)26 Get (io.cdap.cdap.api.dataset.table.Get)24 Schema (io.cdap.cdap.api.data.schema.Schema)21 HashMap (java.util.HashMap)19 MDSKey (io.cdap.cdap.data2.dataset2.lib.table.MDSKey)16 Transaction (org.apache.tephra.Transaction)16 TransactionAware (org.apache.tephra.TransactionAware)16 IOException (java.io.IOException)14 Map (java.util.Map)14 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)13 DatasetAdmin (io.cdap.cdap.api.dataset.DatasetAdmin)12 WriteOnly (io.cdap.cdap.api.annotation.WriteOnly)10 DimensionValue (io.cdap.cdap.api.dataset.lib.cube.DimensionValue)10 HBaseTable (io.cdap.cdap.data2.dataset2.lib.table.hbase.HBaseTable)10