use of java.util.function.Function in project elasticsearch by elastic.
the class TransportSearchAction method executeSearch.
private void executeSearch(SearchTask task, long startTimeInMillis, SearchRequest searchRequest, String[] localIndices, List<ShardIterator> remoteShardIterators, Function<String, Transport.Connection> remoteConnections, ClusterState clusterState, Map<String, AliasFilter> remoteAliasMap, ActionListener<SearchResponse> listener) {
clusterState.blocks().globalBlockedRaiseException(ClusterBlockLevel.READ);
// TODO: I think startTime() should become part of ActionRequest and that should be used both for index name
// date math expressions and $now in scripts. This way all apis will deal with now in the same way instead
// of just for the _search api
final Index[] indices;
if (localIndices.length == 0 && remoteShardIterators.size() > 0) {
// don't search on _all if only remote indices were specified
indices = Index.EMPTY_ARRAY;
} else {
indices = indexNameExpressionResolver.concreteIndices(clusterState, searchRequest.indicesOptions(), startTimeInMillis, localIndices);
}
Map<String, AliasFilter> aliasFilter = buildPerIndexAliasFilter(searchRequest, clusterState, indices, remoteAliasMap);
Map<String, Set<String>> routingMap = indexNameExpressionResolver.resolveSearchRouting(clusterState, searchRequest.routing(), searchRequest.indices());
String[] concreteIndices = new String[indices.length];
for (int i = 0; i < indices.length; i++) {
concreteIndices[i] = indices[i].getName();
}
GroupShardsIterator localShardsIterator = clusterService.operationRouting().searchShards(clusterState, concreteIndices, routingMap, searchRequest.preference());
GroupShardsIterator shardIterators = mergeShardsIterators(localShardsIterator, remoteShardIterators);
failIfOverShardCountLimit(clusterService, shardIterators.size());
Map<String, Float> concreteIndexBoosts = resolveIndexBoosts(searchRequest, clusterState);
// optimize search type for cases where there is only one shard group to search on
if (shardIterators.size() == 1) {
// if we only have one group, then we always want Q_A_F, no need for DFS, and no need to do THEN since we hit one shard
searchRequest.searchType(QUERY_THEN_FETCH);
}
if (searchRequest.isSuggestOnly()) {
// disable request cache if we have only suggest
searchRequest.requestCache(false);
switch(searchRequest.searchType()) {
case DFS_QUERY_THEN_FETCH:
// convert to Q_T_F if we have only suggest
searchRequest.searchType(QUERY_THEN_FETCH);
break;
}
}
final DiscoveryNodes nodes = clusterState.nodes();
Function<String, Transport.Connection> connectionLookup = (nodeId) -> {
final DiscoveryNode discoveryNode = nodes.get(nodeId);
final Transport.Connection connection;
if (discoveryNode != null) {
connection = searchTransportService.getConnection(discoveryNode);
} else {
connection = remoteConnections.apply(nodeId);
}
if (connection == null) {
throw new IllegalStateException("no node found for id: " + nodeId);
}
return connection;
};
searchAsyncAction(task, searchRequest, shardIterators, startTimeInMillis, connectionLookup, clusterState.version(), Collections.unmodifiableMap(aliasFilter), concreteIndexBoosts, listener).start();
}
use of java.util.function.Function in project elasticsearch by elastic.
the class TransportSearchAction method doExecute.
@Override
protected void doExecute(Task task, SearchRequest searchRequest, ActionListener<SearchResponse> listener) {
// pure paranoia if time goes backwards we are at least positive
final long startTimeInMillis = Math.max(0, System.currentTimeMillis());
final String[] localIndices;
final Map<String, List<String>> remoteClusterIndices;
final ClusterState clusterState = clusterService.state();
if (remoteClusterService.isCrossClusterSearchEnabled()) {
remoteClusterIndices = // empty string is not allowed
remoteClusterService.groupClusterIndices(// empty string is not allowed
searchRequest.indices(), idx -> indexNameExpressionResolver.hasIndexOrAlias(idx, clusterState));
List<String> remove = remoteClusterIndices.remove(RemoteClusterService.LOCAL_CLUSTER_GROUP_KEY);
localIndices = remove == null ? Strings.EMPTY_ARRAY : remove.toArray(new String[remove.size()]);
} else {
remoteClusterIndices = Collections.emptyMap();
localIndices = searchRequest.indices();
}
if (remoteClusterIndices.isEmpty()) {
executeSearch((SearchTask) task, startTimeInMillis, searchRequest, localIndices, Collections.emptyList(), (nodeId) -> null, clusterState, Collections.emptyMap(), listener);
} else {
remoteClusterService.collectSearchShards(searchRequest, remoteClusterIndices, ActionListener.wrap((searchShardsResponses) -> {
List<ShardIterator> remoteShardIterators = new ArrayList<>();
Map<String, AliasFilter> remoteAliasFilters = new HashMap<>();
Function<String, Transport.Connection> connectionFunction = remoteClusterService.processRemoteShards(searchShardsResponses, remoteShardIterators, remoteAliasFilters);
executeSearch((SearchTask) task, startTimeInMillis, searchRequest, localIndices, remoteShardIterators, connectionFunction, clusterState, remoteAliasFilters, listener);
}, listener::onFailure));
}
}
use of java.util.function.Function in project buck by facebook.
the class SwiftLibraryDescription method createBuildRule.
@Override
public <A extends SwiftLibraryDescription.Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params, final BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
Optional<LinkerMapMode> flavoredLinkerMapMode = LinkerMapMode.FLAVOR_DOMAIN.getValue(params.getBuildTarget());
params = LinkerMapMode.removeLinkerMapModeFlavorInParams(params, flavoredLinkerMapMode);
final BuildTarget buildTarget = params.getBuildTarget();
// See if we're building a particular "type" and "platform" of this library, and if so, extract
// them from the flavors attached to the build target.
Optional<Map.Entry<Flavor, CxxPlatform>> platform = cxxPlatformFlavorDomain.getFlavorAndValue(buildTarget);
final ImmutableSortedSet<Flavor> buildFlavors = buildTarget.getFlavors();
ImmutableSortedSet<BuildRule> filteredExtraDeps = params.getExtraDeps().get().stream().filter(input -> !input.getBuildTarget().getUnflavoredBuildTarget().equals(buildTarget.getUnflavoredBuildTarget())).collect(MoreCollectors.toImmutableSortedSet());
params = params.copyReplacingExtraDeps(Suppliers.ofInstance(filteredExtraDeps));
if (!buildFlavors.contains(SWIFT_COMPANION_FLAVOR) && platform.isPresent()) {
final CxxPlatform cxxPlatform = platform.get().getValue();
Optional<SwiftPlatform> swiftPlatform = swiftPlatformFlavorDomain.getValue(buildTarget);
if (!swiftPlatform.isPresent()) {
throw new HumanReadableException("Platform %s is missing swift compiler", cxxPlatform);
}
// See if we're building a particular "type" and "platform" of this library, and if so,
// extract them from the flavors attached to the build target.
Optional<Map.Entry<Flavor, Type>> type = LIBRARY_TYPE.getFlavorAndValue(buildTarget);
if (!buildFlavors.contains(SWIFT_COMPILE_FLAVOR) && type.isPresent()) {
Set<Flavor> flavors = Sets.newHashSet(params.getBuildTarget().getFlavors());
flavors.remove(type.get().getKey());
BuildTarget target = BuildTarget.builder(params.getBuildTarget().getUnflavoredBuildTarget()).addAllFlavors(flavors).build();
if (flavoredLinkerMapMode.isPresent()) {
target = target.withAppendedFlavors(flavoredLinkerMapMode.get().getFlavor());
}
BuildRuleParams typeParams = params.withBuildTarget(target);
switch(type.get().getValue()) {
case SHARED:
return createSharedLibraryBuildRule(typeParams, resolver, target, swiftPlatform.get(), cxxPlatform, args.soname, flavoredLinkerMapMode);
case STATIC:
case MACH_O_BUNDLE:
}
throw new RuntimeException("unhandled library build type");
}
// All swift-compile rules of swift-lib deps are required since we need their swiftmodules
// during compilation.
final Function<BuildRule, BuildRule> requireSwiftCompile = input -> {
try {
Preconditions.checkArgument(input instanceof SwiftLibrary);
return ((SwiftLibrary) input).requireSwiftCompileRule(cxxPlatform.getFlavor());
} catch (NoSuchBuildTargetException e) {
throw new HumanReadableException(e, "Could not find SwiftCompile with target %s", buildTarget);
}
};
params = params.copyAppendingExtraDeps(params.getDeps().stream().filter(SwiftLibrary.class::isInstance).map(requireSwiftCompile).collect(MoreCollectors.toImmutableSet()));
params = params.copyAppendingExtraDeps(params.getDeps().stream().filter(CxxLibrary.class::isInstance).map(input -> {
BuildTarget companionTarget = input.getBuildTarget().withAppendedFlavors(SWIFT_COMPANION_FLAVOR);
return resolver.getRuleOptional(companionTarget).map(requireSwiftCompile);
}).filter(Optional::isPresent).map(Optional::get).collect(MoreCollectors.toImmutableSortedSet()));
return new SwiftCompile(cxxPlatform, swiftBuckConfig, params, swiftPlatform.get().getSwift(), args.frameworks, args.moduleName.orElse(buildTarget.getShortName()), BuildTargets.getGenPath(params.getProjectFilesystem(), buildTarget, "%s"), args.srcs, args.compilerFlags, args.enableObjcInterop, args.bridgingHeader);
}
// Otherwise, we return the generic placeholder of this library.
params = LinkerMapMode.restoreLinkerMapModeFlavorInParams(params, flavoredLinkerMapMode);
return new SwiftLibrary(params, resolver, ImmutableSet.of(), swiftPlatformFlavorDomain, args.frameworks, args.libraries, args.supportedPlatformsRegex, args.preferredLinkage.orElse(NativeLinkable.Linkage.ANY));
}
use of java.util.function.Function in project elasticsearch by elastic.
the class AbstractFilteringJsonGeneratorTestCase method testEmptyObject.
public void testEmptyObject() throws IOException {
final Function<XContentBuilder, XContentBuilder> build = builder -> {
try {
return builder.startObject().startObject("foo").endObject().endObject();
} catch (IOException e) {
throw new RuntimeException(e);
}
};
XContentBuilder expected = build.apply(newXContentBuilder());
assertXContentBuilder(expected, build.apply(newXContentBuilderWithIncludes("foo")));
assertXContentBuilder(expected, build.apply(newXContentBuilderWithExcludes("bar")));
assertXContentBuilder(expected, build.apply(newXContentBuilder(singleton("f*"), singleton("baz"))));
expected = newXContentBuilder().startObject().endObject();
assertXContentBuilder(expected, build.apply(newXContentBuilderWithExcludes("foo")));
assertXContentBuilder(expected, build.apply(newXContentBuilderWithIncludes("bar")));
assertXContentBuilder(expected, build.apply(newXContentBuilder(singleton("f*"), singleton("foo"))));
}
use of java.util.function.Function in project elasticsearch by elastic.
the class AbstractFilteringJsonGeneratorTestCase method testSingleFieldObject.
public void testSingleFieldObject() throws IOException {
final Function<XContentBuilder, XContentBuilder> build = builder -> {
try {
return builder.startObject().startObject("foo").field("bar", "test").endObject().endObject();
} catch (IOException e) {
throw new RuntimeException(e);
}
};
XContentBuilder expected = build.apply(newXContentBuilder());
assertXContentBuilder(expected, build.apply(newXContentBuilderWithIncludes("foo.bar")));
assertXContentBuilder(expected, build.apply(newXContentBuilderWithExcludes("foo.baz")));
assertXContentBuilder(expected, build.apply(newXContentBuilder(singleton("foo"), singleton("foo.baz"))));
expected = newXContentBuilder().startObject().endObject();
assertXContentBuilder(expected, build.apply(newXContentBuilderWithExcludes("foo.bar")));
assertXContentBuilder(expected, build.apply(newXContentBuilder(singleton("foo"), singleton("foo.b*"))));
}
Aggregations