use of io.cdap.cdap.common.utils.ImmutablePair in project cdap by caskdata.
the class DatasetMetadataStorage method search.
@Override
public SearchResponse search(SearchRequest request) {
Cursor cursor = request.getCursor() != null && !request.getCursor().isEmpty() ? Cursor.fromString(request.getCursor()) : null;
Set<String> namespaces = cursor == null ? request.getNamespaces() : cursor.getNamespaces();
ImmutablePair<NamespaceId, Set<EntityScope>> namespaceAndScopes = determineNamespaceAndScopes(namespaces);
CursorAndOffsetInfo cursorOffsetAndLimits = determineCursorOffsetAndLimits(request, cursor);
String query = cursor != null ? cursor.getQuery() : request.getQuery() == null || request.getQuery().isEmpty() ? "*" : request.getQuery();
Set<String> types = cursor != null ? cursor.getTypes() : request.getTypes();
types = types == null ? Collections.emptySet() : types;
Sorting sorting = cursor == null ? request.getSorting() : cursor.getSorting() == null ? null : Sorting.of(cursor.getSorting());
SortInfo sortInfo = sorting == null ? SortInfo.DEFAULT : new SortInfo(sorting.getKey(), SortInfo.SortOrder.valueOf(sorting.getOrder().name()));
boolean showHidden = cursor != null ? cursor.isShowHidden() : request.isShowHidden();
MetadataScope scope = cursor != null ? cursor.getScope() : request.getScope();
MetadataSearchResponse response = search(new io.cdap.cdap.data2.metadata.dataset.SearchRequest(namespaceAndScopes.getFirst(), query, types, sortInfo, cursorOffsetAndLimits.getOffsetToRequest(), cursorOffsetAndLimits.getLimitToRequest(), request.isCursorRequested() ? 1 : 0, cursorOffsetAndLimits.getCursor(), showHidden, namespaceAndScopes.getSecond()), scope);
// translate results back and limit them to at most what was requested (see above where we add 1)
int limitToRespond = cursorOffsetAndLimits.getLimitToRespond();
int offsetToRespond = cursorOffsetAndLimits.getOffsetToRespond();
List<MetadataRecord> results = response.getResults().stream().limit(limitToRespond).map(record -> {
Metadata metadata = Metadata.EMPTY;
for (Map.Entry<MetadataScope, io.cdap.cdap.api.metadata.Metadata> entry : record.getMetadata().entrySet()) {
Metadata toAdd = new Metadata(entry.getKey(), entry.getValue().getTags(), entry.getValue().getProperties());
metadata = mergeDisjointMetadata(metadata, toAdd);
}
return new MetadataRecord(record.getMetadataEntity(), metadata);
}).collect(Collectors.toList());
Cursor newCursor = null;
if (response.getCursors() != null && !response.getCursors().isEmpty()) {
String actualCursor = response.getCursors().get(0);
if (cursor != null) {
// the new cursor's offset is the previous cursor's offset plus the number of results
newCursor = new Cursor(cursor, cursor.getOffset() + results.size(), actualCursor);
} else {
newCursor = new Cursor(offsetToRespond + results.size(), limitToRespond, showHidden, scope, namespaces, types, sorting == null ? null : sorting.toString(), actualCursor, query);
}
}
// adjust the total results by the difference of requested offset and the true offset that we respond back
int totalResults = offsetToRespond - cursorOffsetAndLimits.getOffsetToRequest() + response.getTotal();
return new SearchResponse(request, newCursor == null ? null : newCursor.toString(), offsetToRespond, limitToRespond, totalResults, results);
}
use of io.cdap.cdap.common.utils.ImmutablePair in project cdap by caskdata.
the class ArtifactStore method addPluginsInRangeToMap.
private void addPluginsInRangeToMap(final NamespaceId namespace, List<Id.Artifact> parentArtifacts, Iterator<StructuredRow> iterator, SortedMap<ArtifactDescriptor, PluginClass> plugins, @Nullable Predicate<io.cdap.cdap.proto.id.ArtifactId> range, int limit) {
// if predicate is null,
// filter out plugins whose artifacts are not in the system namespace and not in this namespace
range = range != null ? range : input -> NamespaceId.SYSTEM.equals(input.getParent()) || input.getParent().equals(namespace);
while (iterator.hasNext()) {
StructuredRow row = iterator.next();
ImmutablePair<ArtifactDescriptor, PluginData> pluginPair = getPlugin(row, range);
if (pluginPair == null) {
continue;
}
PluginData pluginData = pluginPair.getSecond();
// filter out plugins that don't extend this version of the parent artifact
for (Id.Artifact parentArtifactId : parentArtifacts) {
if (pluginData.isUsableBy(parentArtifactId.toEntityId()) && isAllowed(pluginData.pluginClass)) {
plugins.put(pluginPair.getFirst(), pluginData.pluginClass);
break;
}
}
if (limit < plugins.size()) {
plugins.remove(plugins.lastKey());
}
}
}
use of io.cdap.cdap.common.utils.ImmutablePair in project cdap by caskdata.
the class HBaseTable method setFilterIfNeeded.
private void setFilterIfNeeded(ScanBuilder scan, @Nullable Filter filter) {
if (filter == null) {
return;
}
if (filter instanceof FuzzyRowFilter) {
FuzzyRowFilter fuzzyRowFilter = (FuzzyRowFilter) filter;
List<Pair<byte[], byte[]>> fuzzyPairs = Lists.newArrayListWithExpectedSize(fuzzyRowFilter.getFuzzyKeysData().size());
for (ImmutablePair<byte[], byte[]> pair : fuzzyRowFilter.getFuzzyKeysData()) {
fuzzyPairs.add(Pair.newPair(pair.getFirst(), pair.getSecond()));
}
scan.setFilter(new org.apache.hadoop.hbase.filter.FuzzyRowFilter(fuzzyPairs));
} else {
throw new IllegalArgumentException("Unsupported filter: " + filter);
}
}
use of io.cdap.cdap.common.utils.ImmutablePair in project cdap by caskdata.
the class MetadataDataset method getFuzzyKeyFor.
private ImmutablePair<byte[], byte[]> getFuzzyKeyFor(MetadataEntity metadataEntity) {
// We need to create fuzzy pairs to match the first part of the key containing metadataEntity
MDSKey mdsKey = MetadataKey.createValueRowKey(metadataEntity, null);
byte[] keyBytes = mdsKey.getKey();
// byte array is automatically initialized to 0, which implies fixed match in fuzzy info
// the row key after metadataEntity doesn't need to be a match.
// Workaround for HBASE-15676, need to have at least one 1 in the fuzzy filter
byte[] infoBytes = new byte[keyBytes.length + 1];
infoBytes[infoBytes.length - 1] = 1;
// the key array size and mask array size has to be equal so increase the size by 1
return new ImmutablePair<>(Bytes.concat(keyBytes, new byte[1]), infoBytes);
}
use of io.cdap.cdap.common.utils.ImmutablePair in project cdap by caskdata.
the class AppFabricTestBase method getAppDetails.
/**
* Gets a list of {@link BatchApplicationDetail} from the give set of application version
*
* @param namespace the namespace to read from
* @param appVersions list of appId and version pair.
*/
protected List<BatchApplicationDetail> getAppDetails(String namespace, Collection<ImmutablePair<String, String>> appVersions) throws Exception {
List<Map<String, String>> request = appVersions.stream().map(e -> (e.getSecond() == null) ? Collections.singletonMap("appId", e.getFirst()) : ImmutableMap.of("appId", e.getFirst(), "version", e.getSecond())).collect(Collectors.toList());
HttpResponse response = doPost(getVersionedAPIPath("appdetail", Constants.Gateway.API_VERSION_3_TOKEN, namespace), GSON.toJson(request));
assertResponseCode(200, response);
Assert.assertEquals("application/json", getFirstHeaderValue(response, HttpHeaderNames.CONTENT_TYPE.toString()));
return readResponse(response, new TypeToken<List<BatchApplicationDetail>>() {
}.getType());
}
Aggregations