Search in sources :

Example 6 with Collections

use of java.util.Collections in project elasticsearch by elastic.

the class TermsQueryBuilder method convert.

/**
     * Convert the list in a way that optimizes storage in the case that all
     * elements are either integers or {@link String}s/{@link BytesRef}s. This
     * is useful to help garbage collections for use-cases that involve sending
     * very large terms queries to Elasticsearch. If the list does not only
     * contain integers or {@link String}s, then a list is returned where all
     * {@link String}s have been replaced with {@link BytesRef}s.
     */
static List<?> convert(List<?> list) {
    if (list.isEmpty()) {
        return Collections.emptyList();
    }
    final boolean allNumbers = list.stream().allMatch(o -> o != null && INTEGER_TYPES.contains(o.getClass()));
    if (allNumbers) {
        final long[] elements = list.stream().mapToLong(o -> ((Number) o).longValue()).toArray();
        return new AbstractList<Object>() {

            @Override
            public Object get(int index) {
                return elements[index];
            }

            @Override
            public int size() {
                return elements.length;
            }
        };
    }
    final boolean allStrings = list.stream().allMatch(o -> o != null && STRING_TYPES.contains(o.getClass()));
    if (allStrings) {
        final BytesRefBuilder builder = new BytesRefBuilder();
        try (BytesStreamOutput bytesOut = new BytesStreamOutput()) {
            final int[] endOffsets = new int[list.size()];
            int i = 0;
            for (Object o : list) {
                BytesRef b;
                if (o instanceof BytesRef) {
                    b = (BytesRef) o;
                } else {
                    builder.copyChars(o.toString());
                    b = builder.get();
                }
                bytesOut.writeBytes(b.bytes, b.offset, b.length);
                if (i == 0) {
                    endOffsets[0] = b.length;
                } else {
                    endOffsets[i] = Math.addExact(endOffsets[i - 1], b.length);
                }
                ++i;
            }
            final BytesReference bytes = bytesOut.bytes();
            return new AbstractList<Object>() {

                public Object get(int i) {
                    final int startOffset = i == 0 ? 0 : endOffsets[i - 1];
                    final int endOffset = endOffsets[i];
                    return bytes.slice(startOffset, endOffset - startOffset).toBytesRef();
                }

                public int size() {
                    return endOffsets.length;
                }
            };
        }
    }
    return list.stream().map(o -> o instanceof String ? new BytesRef(o.toString()) : o).collect(Collectors.toList());
}
Also used : Query(org.apache.lucene.search.Query) IntStream(java.util.stream.IntStream) GetResponse(org.elasticsearch.action.get.GetResponse) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) Arrays(java.util.Arrays) ParsingException(org.elasticsearch.common.ParsingException) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) AbstractList(java.util.AbstractList) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Strings(org.elasticsearch.common.Strings) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) XContentMapValues(org.elasticsearch.common.xcontent.support.XContentMapValues) GetRequest(org.elasticsearch.action.get.GetRequest) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) BytesRef(org.apache.lucene.util.BytesRef) Client(org.elasticsearch.client.Client) Set(java.util.Set) TermsLookup(org.elasticsearch.indices.TermsLookup) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) Collectors(java.util.stream.Collectors) XContentParser(org.elasticsearch.common.xcontent.XContentParser) Objects(java.util.Objects) List(java.util.List) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesRefs(org.elasticsearch.common.lucene.BytesRefs) Queries(org.elasticsearch.common.lucene.search.Queries) Collections(java.util.Collections) AbstractList(java.util.AbstractList) BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) BytesRef(org.apache.lucene.util.BytesRef)

Example 7 with Collections

use of java.util.Collections in project spring-framework by spring-projects.

the class TomcatRequestUpgradeStrategy method upgrade.

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, Optional<String> subProtocol) {
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    HttpServletRequest servletRequest = getHttpServletRequest(request);
    HttpServletResponse servletResponse = getHttpServletResponse(response);
    Endpoint endpoint = new StandardWebSocketHandlerAdapter(handler, session -> {
        HandshakeInfo info = getHandshakeInfo(exchange, subProtocol);
        DataBufferFactory factory = response.bufferFactory();
        return new StandardWebSocketSession(session, info, factory);
    });
    String requestURI = servletRequest.getRequestURI();
    DefaultServerEndpointConfig config = new DefaultServerEndpointConfig(requestURI, endpoint);
    config.setSubprotocols(subProtocol.map(Collections::singletonList).orElse(Collections.emptyList()));
    try {
        WsServerContainer container = getContainer(servletRequest);
        container.doUpgrade(servletRequest, servletResponse, config, Collections.emptyMap());
    } catch (ServletException | IOException ex) {
        return Mono.error(ex);
    }
    return Mono.empty();
}
Also used : WsServerContainer(org.apache.tomcat.websocket.server.WsServerContainer) StandardWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.StandardWebSocketHandlerAdapter) ServletServerHttpRequest(org.springframework.http.server.reactive.ServletServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.reactive.ServletServerHttpResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Endpoint(javax.websocket.Endpoint) StandardWebSocketSession(org.springframework.web.reactive.socket.adapter.StandardWebSocketSession) Collections(java.util.Collections) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 8 with Collections

use of java.util.Collections in project torodb by torodb.

the class RecoveryService method cloneDatabases.

private void cloneDatabases(@Nonnull MongoClient remoteClient) throws CloningException, MongoException, UserException {
    enableDataImportMode();
    try {
        Stream<String> dbNames;
        try (MongoConnection remoteConnection = remoteClient.openConnection()) {
            RemoteCommandResponse<ListDatabasesReply> remoteResponse = remoteConnection.execute(ListDatabasesCommand.INSTANCE, "admin", true, Empty.getInstance());
            if (!remoteResponse.isOk()) {
                throw remoteResponse.asMongoException();
            }
            dbNames = remoteResponse.getCommandReply().get().getDatabases().stream().map(db -> db.getName());
        }
        dbNames.filter(this::isReplicable).forEach(databaseName -> {
            MyWritePermissionSupplier writePermissionSupplier = new MyWritePermissionSupplier(databaseName);
            CloneOptions options = new CloneOptions(true, true, true, false, databaseName, Collections.<String>emptySet(), writePermissionSupplier, (colName) -> replFilters.getCollectionPredicate().test(databaseName, colName), (collection, indexName, unique, keys) -> replFilters.getIndexPredicate().test(databaseName, collection, indexName, unique, keys));
            try {
                cloner.cloneDatabase(databaseName, remoteClient, server, options);
            } catch (MongoException ex) {
                throw new CloningException(ex);
            }
        });
    } finally {
        disableDataImportMode();
    }
}
Also used : RunnableTorodbService(com.torodb.core.services.RunnableTorodbService) ReadOplogTransaction(com.torodb.mongodb.repl.OplogManager.ReadOplogTransaction) MongoConnection(com.eightkdata.mongowp.client.core.MongoConnection) RollbackException(com.torodb.core.transaction.RollbackException) Assisted(com.google.inject.assistedinject.Assisted) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) MongoDbRepl(com.torodb.mongodb.repl.guice.MongoDbRepl) MongoClient(com.eightkdata.mongowp.client.core.MongoClient) Empty(com.eightkdata.mongowp.server.api.tools.Empty) RollbackReplicationException(com.torodb.mongodb.repl.oplogreplier.RollbackReplicationException) MongoException(com.eightkdata.mongowp.exceptions.MongoException) OplogOperationUnsupported(com.eightkdata.mongowp.exceptions.OplogOperationUnsupported) ThreadFactory(java.util.concurrent.ThreadFactory) OplogFetcher(com.torodb.mongodb.repl.oplogreplier.fetcher.OplogFetcher) CancellationException(java.util.concurrent.CancellationException) CloningException(com.torodb.mongodb.utils.DbCloner.CloningException) ListDatabasesCommand(com.torodb.mongodb.commands.signatures.diagnostic.ListDatabasesCommand) OplogApplier(com.torodb.mongodb.repl.oplogreplier.OplogApplier) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) StopReplicationException(com.torodb.mongodb.repl.oplogreplier.StopReplicationException) OpTime(com.eightkdata.mongowp.OpTime) OplogStartMissingException(com.eightkdata.mongowp.exceptions.OplogStartMissingException) Supplier(com.google.common.base.Supplier) MongodServer(com.torodb.mongodb.core.MongodServer) UnreachableMongoServerException(com.eightkdata.mongowp.client.core.UnreachableMongoServerException) Inject(javax.inject.Inject) TorodConnection(com.torodb.torod.TorodConnection) NoSyncSourceFoundException(com.torodb.mongodb.repl.exceptions.NoSyncSourceFoundException) ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) MongoClientFactory(com.eightkdata.mongowp.client.core.MongoClientFactory) LimitedOplogFetcher(com.torodb.mongodb.repl.oplogreplier.fetcher.LimitedOplogFetcher) RemoteCommandResponse(com.eightkdata.mongowp.client.core.MongoConnection.RemoteCommandResponse) OplogManagerPersistException(com.torodb.mongodb.repl.OplogManager.OplogManagerPersistException) DbCloner(com.torodb.mongodb.utils.DbCloner) Supervisor(com.torodb.core.supervision.Supervisor) Nonnull(javax.annotation.Nonnull) CloneOptions(com.torodb.mongodb.utils.DbCloner.CloneOptions) SharedWriteTorodTransaction(com.torodb.torod.SharedWriteTorodTransaction) TorodbRunnableService(com.torodb.core.annotations.TorodbRunnableService) SupervisorDecision(com.torodb.core.supervision.SupervisorDecision) MongoCursor(com.eightkdata.mongowp.server.api.pojos.MongoCursor) UnexpectedOplogApplierException(com.torodb.mongodb.repl.oplogreplier.OplogApplier.UnexpectedOplogApplierException) UserException(com.torodb.core.exceptions.user.UserException) WriteOplogTransaction(com.torodb.mongodb.repl.OplogManager.WriteOplogTransaction) HostAndPort(com.google.common.net.HostAndPort) Status(com.eightkdata.mongowp.Status) TorodServer(com.torodb.torod.TorodServer) ListDatabasesReply(com.torodb.mongodb.commands.signatures.diagnostic.ListDatabasesCommand.ListDatabasesReply) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) CloningException(com.torodb.mongodb.utils.DbCloner.CloningException) MongoException(com.eightkdata.mongowp.exceptions.MongoException) ListDatabasesReply(com.torodb.mongodb.commands.signatures.diagnostic.ListDatabasesCommand.ListDatabasesReply) MongoConnection(com.eightkdata.mongowp.client.core.MongoConnection) CloneOptions(com.torodb.mongodb.utils.DbCloner.CloneOptions)

Example 9 with Collections

use of java.util.Collections in project wildfly by wildfly.

the class ChannelCommandDispatcher method submitOnCluster.

@Override
public <R> Map<Node, Future<R>> submitOnCluster(Command<R, ? super C> command, Node... excludedNodes) throws CommandDispatcherException {
    Map<Node, Future<R>> results = new ConcurrentHashMap<>();
    FutureListener<RspList<R>> listener = future -> {
        try {
            future.get().keySet().stream().map(address -> this.factory.createNode(address)).forEach(node -> results.remove(node));
        } catch (CancellationException e) {
        } catch (ExecutionException e) {
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    };
    Message message = this.createMessage(command);
    RequestOptions options = this.createRequestOptions(excludedNodes);
    try {
        Future<? extends Map<Address, Rsp<R>>> futureResponses = this.dispatcher.castMessageWithFuture(null, message, options, listener);
        Set<Node> excluded = (excludedNodes != null) ? new HashSet<>(Arrays.asList(excludedNodes)) : Collections.<Node>emptySet();
        for (Address address : this.dispatcher.getChannel().getView().getMembers()) {
            Node node = this.factory.createNode(address);
            if (!excluded.contains(node)) {
                Future<R> future = new Future<R>() {

                    @Override
                    public boolean cancel(boolean mayInterruptIfRunning) {
                        return futureResponses.cancel(mayInterruptIfRunning);
                    }

                    @Override
                    public R get() throws InterruptedException, ExecutionException {
                        Map<Address, Rsp<R>> responses = futureResponses.get();
                        Rsp<R> response = responses.get(address);
                        if (response == null) {
                            throw new CancellationException();
                        }
                        return createCommandResponse(response).get();
                    }

                    @Override
                    public R get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
                        Map<Address, Rsp<R>> responses = futureResponses.get(timeout, unit);
                        Rsp<R> response = responses.get(address);
                        if (response == null) {
                            throw new CancellationException();
                        }
                        return createCommandResponse(response).get();
                    }

                    @Override
                    public boolean isCancelled() {
                        return futureResponses.isCancelled();
                    }

                    @Override
                    public boolean isDone() {
                        return futureResponses.isDone();
                    }
                };
                results.put(node, future);
            }
        }
        return results;
    } catch (Exception e) {
        throw new CommandDispatcherException(e);
    }
}
Also used : Arrays(java.util.Arrays) Rsp(org.jgroups.util.Rsp) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) FutureListener(org.jgroups.util.FutureListener) HashSet(java.util.HashSet) CommandDispatcher(org.wildfly.clustering.dispatcher.CommandDispatcher) Command(org.wildfly.clustering.dispatcher.Command) Future(java.util.concurrent.Future) RspFilter(org.jgroups.blocks.RspFilter) RequestOptions(org.jgroups.blocks.RequestOptions) Map(java.util.Map) RspList(org.jgroups.util.RspList) Address(org.jgroups.Address) NodeFactory(org.wildfly.clustering.group.NodeFactory) CancellationException(java.util.concurrent.CancellationException) ResponseMode(org.jgroups.blocks.ResponseMode) CommandResponse(org.wildfly.clustering.dispatcher.CommandResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Addressable(org.wildfly.clustering.server.Addressable) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Message(org.jgroups.Message) Node(org.wildfly.clustering.group.Node) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) Collections(java.util.Collections) MessageDispatcher(org.jgroups.blocks.MessageDispatcher) Message(org.jgroups.Message) Address(org.jgroups.Address) RequestOptions(org.jgroups.blocks.RequestOptions) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) Node(org.wildfly.clustering.group.Node) RspList(org.jgroups.util.RspList) Rsp(org.jgroups.util.Rsp) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) CancellationException(java.util.concurrent.CancellationException) Future(java.util.concurrent.Future) TimeUnit(java.util.concurrent.TimeUnit) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with Collections

use of java.util.Collections in project L42 by ElvisResearchGroup.

the class Translator method useFrom.

//this should take a class, strip out nested and 'from' it so that it is as at top level
static ClassB useFrom(ClassB ct, Path p) {
    ArrayList<Member> ms = new ArrayList<Member>();
    for (Member m : ct.getMs()) {
        m.match(nc -> null, mi -> {
            throw Assertions.codeNotReachable();
        }, mt -> ms.add(From.from(mt, p)));
    }
    //for(PathMwt pmwt:ct.getStage().getInherited()){
    for (PathMwt pmwt : Collections.<PathMwt>emptyList()) {
        if (Functions.getIfInDom(ms, pmwt.getMwt().getMs()).isPresent()) {
            continue;
        }
        ms.add(From.from(pmwt.getMwt(), p));
    }
    List<Path> sup = tools.Map.of(ti -> (Path) From.fromP(ti.getPath(), p), ct.getSupertypes());
    //tools.Map.of(pi->(Path)From.fromP(pi,p),ct.getStage().getInheritedPaths());
    List<Path> supAll = sup;
    ClassB res = ct.withMs(ms).withSupertypes(tools.Map.of(pi -> pi.toImmNT(), sup));
    return res;
}
Also used : Path(ast.Ast.Path) From(coreVisitors.From) Stage(ast.Ast.Stage) Arrays(java.util.Arrays) Program(programReduction.Program) Ast(ast.Ast) Configuration(facade.Configuration) Type(ast.Ast.Type) HashMap(java.util.HashMap) Assertions(tools.Assertions) ArrayList(java.util.ArrayList) CompilationError(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.CompilationError) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) ClassB(ast.ExpCore.ClassB) InterfaceHeader(ast.Ast.InterfaceHeader) Map(java.util.Map) Facade(reduction.Facade) PathMwt(ast.Util.PathMwt) MapClassLoader(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.MapClassLoader) Method(java.lang.reflect.Method) IsCompiled(coreVisitors.IsCompiled) NestedClass(ast.ExpCore.ClassB.NestedClass) Files(java.nio.file.Files) Path(ast.Ast.Path) Doc(ast.Ast.Doc) Phase(ast.ExpCore.ClassB.Phase) IOException(java.io.IOException) ExpCore(ast.ExpCore) Member(ast.ExpCore.ClassB.Member) Functions(auxiliaryGrammar.Functions) InvocationTargetException(java.lang.reflect.InvocationTargetException) Util(ast.Util) Consumer(java.util.function.Consumer) List(java.util.List) EmptyProgram(programReduction.Program.EmptyProgram) Paths(java.nio.file.Paths) Optional(java.util.Optional) SourceFile(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler.SourceFile) Timer(profiling.Timer) InMemoryJavaCompiler(platformSpecific.inMemoryCompiler.InMemoryJavaCompiler) Collections(java.util.Collections) L42(facade.L42) ArrayList(java.util.ArrayList) Member(ast.ExpCore.ClassB.Member) PathMwt(ast.Util.PathMwt) ClassB(ast.ExpCore.ClassB)

Aggregations

Collections (java.util.Collections)12 List (java.util.List)9 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)5 Arrays (java.util.Arrays)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Set (java.util.Set)4 ListMultimap (com.google.common.collect.ListMultimap)2 Lists (com.google.common.collect.Lists)2 MultimapBuilder (com.google.common.collect.MultimapBuilder)2 Collection (java.util.Collection)2 Optional (java.util.Optional)2 CancellationException (java.util.concurrent.CancellationException)2 Consumer (java.util.function.Consumer)2 Collectors (java.util.stream.Collectors)2 SuppressLint (android.annotation.SuppressLint)1 Bundle (android.os.Bundle)1 NonNull (android.support.annotation.NonNull)1