Search in sources :

Example 11 with Function

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();
}
Also used : ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) Property(org.elasticsearch.common.settings.Setting.Property) ClusterService(org.elasticsearch.cluster.service.ClusterService) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) Function(java.util.function.Function) SearchService(org.elasticsearch.search.SearchService) GroupShardsIterator(org.elasticsearch.cluster.routing.GroupShardsIterator) Strings(org.elasticsearch.common.Strings) Inject(org.elasticsearch.common.inject.Inject) ArrayList(java.util.ArrayList) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Settings(org.elasticsearch.common.settings.Settings) HandledTransportAction(org.elasticsearch.action.support.HandledTransportAction) Map(java.util.Map) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportService(org.elasticsearch.transport.TransportService) ClusterBlockLevel(org.elasticsearch.cluster.block.ClusterBlockLevel) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) ActionFilters(org.elasticsearch.action.support.ActionFilters) Transport(org.elasticsearch.transport.Transport) Setting(org.elasticsearch.common.settings.Setting) Executor(java.util.concurrent.Executor) Set(java.util.Set) QUERY_THEN_FETCH(org.elasticsearch.action.search.SearchType.QUERY_THEN_FETCH) AliasFilter(org.elasticsearch.search.internal.AliasFilter) List(java.util.List) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Task(org.elasticsearch.tasks.Task) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) AliasFilter(org.elasticsearch.search.internal.AliasFilter) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Set(java.util.Set) Index(org.elasticsearch.index.Index) GroupShardsIterator(org.elasticsearch.cluster.routing.GroupShardsIterator) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes)

Example 12 with Function

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));
    }
}
Also used : ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) Property(org.elasticsearch.common.settings.Setting.Property) ClusterService(org.elasticsearch.cluster.service.ClusterService) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) Function(java.util.function.Function) SearchService(org.elasticsearch.search.SearchService) GroupShardsIterator(org.elasticsearch.cluster.routing.GroupShardsIterator) Strings(org.elasticsearch.common.Strings) Inject(org.elasticsearch.common.inject.Inject) ArrayList(java.util.ArrayList) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Settings(org.elasticsearch.common.settings.Settings) HandledTransportAction(org.elasticsearch.action.support.HandledTransportAction) Map(java.util.Map) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportService(org.elasticsearch.transport.TransportService) ClusterBlockLevel(org.elasticsearch.cluster.block.ClusterBlockLevel) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) ActionFilters(org.elasticsearch.action.support.ActionFilters) Transport(org.elasticsearch.transport.Transport) Setting(org.elasticsearch.common.settings.Setting) Executor(java.util.concurrent.Executor) Set(java.util.Set) QUERY_THEN_FETCH(org.elasticsearch.action.search.SearchType.QUERY_THEN_FETCH) AliasFilter(org.elasticsearch.search.internal.AliasFilter) List(java.util.List) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Task(org.elasticsearch.tasks.Task) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) ClusterState(org.elasticsearch.cluster.ClusterState) Function(java.util.function.Function) ArrayList(java.util.ArrayList) List(java.util.List) Transport(org.elasticsearch.transport.Transport) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with Function

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));
}
Also used : CxxBuckConfig(com.facebook.buck.cxx.CxxBuckConfig) Linker(com.facebook.buck.cxx.Linker) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) CxxLibraryDescription(com.facebook.buck.cxx.CxxLibraryDescription) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) SourcePath(com.facebook.buck.rules.SourcePath) RichStream(com.facebook.buck.util.RichStream) InternalFlavor(com.facebook.buck.model.InternalFlavor) Flavored(com.facebook.buck.model.Flavored) Function(java.util.function.Function) BuildRule(com.facebook.buck.rules.BuildRule) FlavorDomain(com.facebook.buck.model.FlavorDomain) ImmutableList(com.google.common.collect.ImmutableList) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) Map(java.util.Map) NativeLinkable(com.facebook.buck.cxx.NativeLinkable) Predicates(com.google.common.base.Predicates) Suppliers(com.google.common.base.Suppliers) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) Path(java.nio.file.Path) CxxDescriptionEnhancer(com.facebook.buck.cxx.CxxDescriptionEnhancer) MoreCollectors(com.facebook.buck.util.MoreCollectors) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) CxxLinkableEnhancer(com.facebook.buck.cxx.CxxLinkableEnhancer) ImmutableSet(com.google.common.collect.ImmutableSet) FlavorConvertible(com.facebook.buck.model.FlavorConvertible) NativeLinkableInput(com.facebook.buck.cxx.NativeLinkableInput) TargetGraph(com.facebook.buck.rules.TargetGraph) Set(java.util.Set) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) HumanReadableException(com.facebook.buck.util.HumanReadableException) BuildTarget(com.facebook.buck.model.BuildTarget) SuppressFieldNotInitialized(com.facebook.infer.annotation.SuppressFieldNotInitialized) Sets(com.google.common.collect.Sets) LinkerMapMode(com.facebook.buck.cxx.LinkerMapMode) AbstractDescriptionArg(com.facebook.buck.rules.AbstractDescriptionArg) CxxLibrary(com.facebook.buck.cxx.CxxLibrary) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Flavor(com.facebook.buck.model.Flavor) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Pattern(java.util.regex.Pattern) BuildTargets(com.facebook.buck.model.BuildTargets) Description(com.facebook.buck.rules.Description) CxxLibrary(com.facebook.buck.cxx.CxxLibrary) Optional(java.util.Optional) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) LinkerMapMode(com.facebook.buck.cxx.LinkerMapMode) InternalFlavor(com.facebook.buck.model.InternalFlavor) Flavor(com.facebook.buck.model.Flavor) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) BuildTarget(com.facebook.buck.model.BuildTarget) HumanReadableException(com.facebook.buck.util.HumanReadableException) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) BuildRule(com.facebook.buck.rules.BuildRule)

Example 14 with Function

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"))));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) Collections.emptySet(java.util.Collections.emptySet) XContentType(org.elasticsearch.common.xcontent.XContentType) XContent(org.elasticsearch.common.xcontent.XContent) Set(java.util.Set) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) BytesReference(org.elasticsearch.common.bytes.BytesReference) Function(java.util.function.Function) Sets(org.elasticsearch.common.util.set.Sets) XContentParser(org.elasticsearch.common.xcontent.XContentParser) Collections.singleton(java.util.Collections.singleton) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.nullValue(org.hamcrest.Matchers.nullValue) ESTestCase(org.elasticsearch.test.ESTestCase) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 15 with Function

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*"))));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) Collections.emptySet(java.util.Collections.emptySet) XContentType(org.elasticsearch.common.xcontent.XContentType) XContent(org.elasticsearch.common.xcontent.XContent) Set(java.util.Set) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) BytesReference(org.elasticsearch.common.bytes.BytesReference) Function(java.util.function.Function) Sets(org.elasticsearch.common.util.set.Sets) XContentParser(org.elasticsearch.common.xcontent.XContentParser) Collections.singleton(java.util.Collections.singleton) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.nullValue(org.hamcrest.Matchers.nullValue) ESTestCase(org.elasticsearch.test.ESTestCase) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

Function (java.util.function.Function)1261 List (java.util.List)606 Map (java.util.Map)447 ArrayList (java.util.ArrayList)416 Test (org.junit.Test)358 Collectors (java.util.stream.Collectors)324 HashMap (java.util.HashMap)287 Collections (java.util.Collections)284 Arrays (java.util.Arrays)271 Set (java.util.Set)255 IOException (java.io.IOException)252 Collection (java.util.Collection)192 HashSet (java.util.HashSet)191 TimeUnit (java.util.concurrent.TimeUnit)174 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)158 Optional (java.util.Optional)157 Assert (org.junit.Assert)137 Consumer (java.util.function.Consumer)134 Supplier (java.util.function.Supplier)126 CompletableFuture (java.util.concurrent.CompletableFuture)121