use of io.cdap.cdap.api.dataset.table.Scan in project cdap by caskdata.
the class MetadataStoreDataset method listKV.
/**
* Returns mapping of all that match the given keySet provided they pass the combinedFilter predicate
* for default COLUMN
*
* @param keySet row key set
* @param typeOfT the type of the result
* @param limit limit number of result
* @param combinedFilter filter for key
* @return map of row key to result
*/
public <T> Map<MDSKey, T> listKV(Set<MDSKey> keySet, Type typeOfT, int limit, @Nullable Predicate<KeyValue<T>> combinedFilter) {
// Sort fuzzy keys
List<MDSKey> sortedKeys = Lists.newArrayList(keySet);
Collections.sort(sortedKeys);
// Scan using fuzzy filter
byte[] startKey = sortedKeys.get(0).getKey();
byte[] stopKey = Bytes.stopKeyForPrefix(sortedKeys.get(sortedKeys.size() - 1).getKey());
List<ImmutablePair<byte[], byte[]>> fuzzyKeys = new ArrayList<>();
for (MDSKey key : sortedKeys) {
fuzzyKeys.add(getFuzzyKeyFor(key));
}
Scan scan = new Scan(startKey, stopKey, new FuzzyRowFilter(fuzzyKeys));
return listCombinedFilterKV(scan, typeOfT, limit, combinedFilter);
}
use of io.cdap.cdap.api.dataset.table.Scan in project cdap by caskdata.
the class MetadataStoreDataset method listKV.
/**
* Return mapping of all that has the prefix of the given mds key and the value with it.
*
* @param startId prefix row key.
* @param limit limit number of result.
* @return map of row key to result.
*/
public Map<MDSKey, byte[]> listKV(MDSKey startId, int limit) {
byte[] startKey = startId.getKey();
byte[] stopKey = Bytes.stopKeyForPrefix(startKey);
Scan scan = new Scan(startKey, stopKey);
Map<MDSKey, byte[]> map = new LinkedHashMap<>();
try (Scanner scanner = table.scan(scan)) {
Row next;
while ((limit > 0) && (next = scanner.next()) != null) {
MDSKey key = new MDSKey(next.getRow());
byte[] value = next.get(COLUMN);
if (value == null) {
continue;
}
map.put(key, value);
limit--;
}
}
return map;
}
use of io.cdap.cdap.api.dataset.table.Scan 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;
}
Aggregations