Search in sources :

Example 21 with MetadataEntry

use of io.cdap.cdap.data2.metadata.dataset.MetadataEntry in project cdap by caskdata.

the class MetadataDataset method getMetadata.

/**
 * Returns metadata for a given set of entities
 *
 * @param metadataEntitys entities for which metadata is required
 * @return map of entitiyId to set of metadata for that entity
 */
public Set<Record> getMetadata(Set<? extends MetadataEntity> metadataEntitys) {
    if (metadataEntitys.isEmpty()) {
        return Collections.emptySet();
    }
    List<ImmutablePair<byte[], byte[]>> fuzzyKeys = new ArrayList<>(metadataEntitys.size());
    for (MetadataEntity metadataEntity : metadataEntitys) {
        fuzzyKeys.add(getFuzzyKeyFor(metadataEntity));
    }
    // Sort fuzzy keys
    fuzzyKeys.sort(FUZZY_KEY_COMPARATOR);
    // Scan using fuzzy filter. Scan returns one row per property.
    // Group the rows on namespacedId
    Multimap<MetadataEntity, MetadataEntry> metadataMap = HashMultimap.create();
    byte[] start = fuzzyKeys.get(0).getFirst();
    byte[] end = Bytes.stopKeyForPrefix(fuzzyKeys.get(fuzzyKeys.size() - 1).getFirst());
    try (Scanner scan = indexedTable.scan(new Scan(start, end, new FuzzyRowFilter(fuzzyKeys)))) {
        Row next;
        while ((next = scan.next()) != null) {
            MetadataEntry metadataEntry = convertRow(next);
            if (metadataEntry != null) {
                metadataMap.put(metadataEntry.getMetadataEntity(), metadataEntry);
            }
        }
    }
    // Create metadata objects for each entity from grouped rows
    Set<Record> metadataSet = new HashSet<>();
    for (Map.Entry<MetadataEntity, Collection<MetadataEntry>> entry : metadataMap.asMap().entrySet()) {
        Map<String, String> properties = new HashMap<>();
        Set<String> tags = Collections.emptySet();
        for (MetadataEntry metadataEntry : entry.getValue()) {
            if (MetadataConstants.TAGS_KEY.equals(metadataEntry.getKey())) {
                tags = splitTags(metadataEntry.getValue());
            } else {
                properties.put(metadataEntry.getKey(), metadataEntry.getValue());
            }
        }
        metadataSet.add(new Record(entry.getKey(), properties, tags));
    }
    return metadataSet;
}
Also used : MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) Scanner(io.cdap.cdap.api.dataset.table.Scanner) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FuzzyRowFilter(io.cdap.cdap.data2.dataset2.lib.table.FuzzyRowFilter) ImmutablePair(io.cdap.cdap.common.utils.ImmutablePair) Collection(java.util.Collection) Scan(io.cdap.cdap.api.dataset.table.Scan) Row(io.cdap.cdap.api.dataset.table.Row) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 22 with MetadataEntry

use of io.cdap.cdap.data2.metadata.dataset.MetadataEntry in project cdap by caskdata.

the class MetadataDataset method writeValue.

private void writeValue(MetadataEntry entry) {
    String key = entry.getKey();
    MDSKey mdsValueKey = MetadataKey.createValueRowKey(entry.getMetadataEntity(), key);
    Put put = new Put(mdsValueKey.getKey());
    // add the metadata value
    put.add(Bytes.toBytes(VALUE_COLUMN), Bytes.toBytes(entry.getValue()));
    indexedTable.put(put);
}
Also used : MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) Put(io.cdap.cdap.api.dataset.table.Put)

Example 23 with MetadataEntry

use of io.cdap.cdap.data2.metadata.dataset.MetadataEntry in project cdap by caskdata.

the class MetadataDataset method storeIndexes.

/**
 * Store indexes for a {@link MetadataEntry}
 * @param indexers {@link Set<String>} of {@link Indexer indexers} for this {@link MetadataEntry}
 * @param metadataEntry {@link MetadataEntry} for which indexes are to be stored
 */
private void storeIndexes(MetadataEntry metadataEntry, Set<Indexer> indexers) {
    // Delete existing indexes for metadataEntity-key
    deleteIndexes(metadataEntry.getMetadataEntity(), metadataEntry.getKey());
    String namespacePrefix = metadataEntry.getMetadataEntity().getValue(MetadataEntity.NAMESPACE) + MetadataConstants.KEYVALUE_SEPARATOR;
    for (Indexer indexer : indexers) {
        Set<String> indexes = indexer.getIndexes(metadataEntry);
        IndexColumn indexColumn = getIndexColumn(metadataEntry.getKey(), indexer.getSortOrder());
        for (String index : indexes) {
            if (index.isEmpty()) {
                continue;
            }
            // store one value for within namespace search and one for cross namespace search
            String lowercaseIndex = index.toLowerCase();
            MDSKey mdsIndexKey = MetadataKey.createIndexRowKey(metadataEntry.getMetadataEntity(), metadataEntry.getKey(), lowercaseIndex);
            Put put = new Put(mdsIndexKey.getKey());
            put.add(Bytes.toBytes(indexColumn.getCrossNamespaceColumn()), Bytes.toBytes(lowercaseIndex));
            put.add(Bytes.toBytes(indexColumn.getColumn()), Bytes.toBytes(namespacePrefix + lowercaseIndex));
            indexedTable.put(put);
        }
    }
}
Also used : InvertedValueIndexer(io.cdap.cdap.data2.metadata.indexer.InvertedValueIndexer) Indexer(io.cdap.cdap.data2.metadata.indexer.Indexer) SchemaIndexer(io.cdap.cdap.data2.metadata.indexer.SchemaIndexer) DefaultValueIndexer(io.cdap.cdap.data2.metadata.indexer.DefaultValueIndexer) MetadataEntityTypeIndexer(io.cdap.cdap.data2.metadata.indexer.MetadataEntityTypeIndexer) ValueOnlyIndexer(io.cdap.cdap.data2.metadata.indexer.ValueOnlyIndexer) InvertedTimeIndexer(io.cdap.cdap.data2.metadata.indexer.InvertedTimeIndexer) MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) Put(io.cdap.cdap.api.dataset.table.Put)

Example 24 with MetadataEntry

use of io.cdap.cdap.data2.metadata.dataset.MetadataEntry in project cdap by caskdata.

the class MetadataDataset method getMetadata.

/**
 * Return metadata based on target id, and key.
 *
 * @param metadataEntity The id of the target
 * @param key The metadata key to get
 * @return instance of {@link MetadataEntry} for the target type, id, and key
 */
@Nullable
private MetadataEntry getMetadata(MetadataEntity metadataEntity, String key) {
    MDSKey mdsKey = MetadataKey.createValueRowKey(metadataEntity, key);
    Row row = indexedTable.get(mdsKey.getKey());
    if (row.isEmpty()) {
        return null;
    }
    byte[] value = row.get(VALUE_COLUMN);
    if (value == null) {
        // This can happen when all tags are removed one by one. The row still exists, but the value is null.
        return null;
    }
    return new MetadataEntry(metadataEntity, key, Bytes.toString(value));
}
Also used : MDSKey(io.cdap.cdap.data2.dataset2.lib.table.MDSKey) Row(io.cdap.cdap.api.dataset.table.Row) Nullable(javax.annotation.Nullable)

Example 25 with MetadataEntry

use of io.cdap.cdap.data2.metadata.dataset.MetadataEntry in project cdap by caskdata.

the class SearchHelper method getSortedEntities.

private Set<MetadataEntity> getSortedEntities(List<MetadataEntry> results, SortInfo sortInfo) {
    // in this case, the backing storage is expected to return results in the expected order.
    if (SortInfo.SortOrder.WEIGHTED != sortInfo.getSortOrder()) {
        Set<MetadataEntity> entities = new LinkedHashSet<>(results.size());
        for (MetadataEntry metadataEntry : results) {
            entities.add(metadataEntry.getMetadataEntity());
        }
        return entities;
    }
    // if sort order is weighted, score results by weight, and return in descending order of weights
    // Score results
    final Map<MetadataEntity, Integer> weightedResults = new HashMap<>();
    for (MetadataEntry metadataEntry : results) {
        weightedResults.put(metadataEntry.getMetadataEntity(), weightedResults.getOrDefault(metadataEntry.getMetadataEntity(), 0) + 1);
    }
    // Sort the results by score
    List<Map.Entry<MetadataEntity, Integer>> resultList = new ArrayList<>(weightedResults.entrySet());
    resultList.sort(SEARCH_RESULT_DESC_SCORE_COMPARATOR);
    Set<MetadataEntity> result = new LinkedHashSet<>(resultList.size());
    for (Map.Entry<MetadataEntity, Integer> entry : resultList) {
        result.add(entry.getKey());
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) MetadataEntry(io.cdap.cdap.data2.metadata.dataset.MetadataEntry) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MetadataEntry(io.cdap.cdap.data2.metadata.dataset.MetadataEntry) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Aggregations

Test (org.junit.Test)16 MetadataEntry (io.cdap.cdap.data2.metadata.dataset.MetadataEntry)13 MetadataEntry (co.cask.cdap.data2.metadata.dataset.MetadataEntry)7 HashSet (java.util.HashSet)7 ArrayList (java.util.ArrayList)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 DatasetId (io.cdap.cdap.proto.id.DatasetId)4 Schema (co.cask.cdap.api.data.schema.Schema)3 DatasetId (co.cask.cdap.proto.id.DatasetId)3 Schema (io.cdap.cdap.api.data.schema.Schema)3 MetadataEntity (io.cdap.cdap.api.metadata.MetadataEntity)3 MDSKey (io.cdap.cdap.data2.dataset2.lib.table.MDSKey)3 HashMap (java.util.HashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 Map (java.util.Map)3 NamespacedEntityId (co.cask.cdap.proto.id.NamespacedEntityId)2 Put (io.cdap.cdap.api.dataset.table.Put)2 Row (io.cdap.cdap.api.dataset.table.Row)2 Indexer (io.cdap.cdap.data2.metadata.indexer.Indexer)2 InvertedValueIndexer (io.cdap.cdap.data2.metadata.indexer.InvertedValueIndexer)2