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;
}
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);
}
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);
}
}
}
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));
}
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;
}
Aggregations