Search in sources :

Example 96 with Function

use of java.util.function.Function in project cassandra by apache.

the class AbstractRow method toString.

public String toString(TableMetadata metadata, boolean includeClusterKeys, boolean fullDetails) {
    StringBuilder sb = new StringBuilder();
    sb.append("Row");
    if (fullDetails) {
        sb.append("[info=").append(primaryKeyLivenessInfo());
        if (!deletion().isLive())
            sb.append(" del=").append(deletion());
        sb.append(" ]");
    }
    sb.append(": ");
    if (includeClusterKeys)
        sb.append(clustering().toString(metadata));
    else
        sb.append(clustering().toCQLString(metadata));
    sb.append(" | ");
    boolean isFirst = true;
    for (ColumnData cd : this) {
        if (isFirst)
            isFirst = false;
        else
            sb.append(", ");
        if (fullDetails) {
            if (cd.column().isSimple()) {
                sb.append(cd);
            } else {
                ComplexColumnData complexData = (ComplexColumnData) cd;
                if (!complexData.complexDeletion().isLive())
                    sb.append("del(").append(cd.column().name).append(")=").append(complexData.complexDeletion());
                for (Cell cell : complexData) sb.append(", ").append(cell);
            }
        } else {
            if (cd.column().isSimple()) {
                Cell cell = (Cell) cd;
                sb.append(cell.column().name).append('=');
                if (cell.isTombstone())
                    sb.append("<tombstone>");
                else
                    sb.append(cell.column().type.getString(cell.value()));
            } else {
                sb.append(cd.column().name).append('=');
                ComplexColumnData complexData = (ComplexColumnData) cd;
                Function<Cell, String> transform = null;
                if (cd.column().type.isCollection()) {
                    CollectionType ct = (CollectionType) cd.column().type;
                    transform = cell -> String.format("%s -> %s", ct.nameComparator().getString(cell.path().get(0)), ct.valueComparator().getString(cell.value()));
                } else if (cd.column().type.isUDT()) {
                    UserType ut = (UserType) cd.column().type;
                    transform = cell -> {
                        Short fId = ut.nameComparator().getSerializer().deserialize(cell.path().get(0));
                        return String.format("%s -> %s", ut.fieldNameAsString(fId), ut.fieldType(fId).getString(cell.value()));
                    };
                } else {
                    transform = cell -> "";
                }
                sb.append(StreamSupport.stream(complexData.spliterator(), false).map(transform).collect(Collectors.joining(", ", "{", "}")));
            }
        }
    }
    return sb.toString();
}
Also used : Iterables(com.google.common.collect.Iterables) FBUtilities(org.apache.cassandra.utils.FBUtilities) MessageDigest(java.security.MessageDigest) org.apache.cassandra.db(org.apache.cassandra.db) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) Objects(java.util.Objects) AbstractCollection(java.util.AbstractCollection) TableMetadata(org.apache.cassandra.schema.TableMetadata) StreamSupport(java.util.stream.StreamSupport) CollectionType(org.apache.cassandra.db.marshal.CollectionType) UserType(org.apache.cassandra.db.marshal.UserType) MarshalException(org.apache.cassandra.serializers.MarshalException) CollectionType(org.apache.cassandra.db.marshal.CollectionType) UserType(org.apache.cassandra.db.marshal.UserType)

Example 97 with Function

use of java.util.function.Function in project elasticsearch by elastic.

the class RemoteClusterService method processRemoteShards.

Function<String, Transport.Connection> processRemoteShards(Map<String, ClusterSearchShardsResponse> searchShardsResponses, List<ShardIterator> remoteShardIterators, Map<String, AliasFilter> aliasFilterMap) {
    Map<String, Supplier<Transport.Connection>> nodeToCluster = new HashMap<>();
    for (Map.Entry<String, ClusterSearchShardsResponse> entry : searchShardsResponses.entrySet()) {
        String clusterName = entry.getKey();
        ClusterSearchShardsResponse searchShardsResponse = entry.getValue();
        for (DiscoveryNode remoteNode : searchShardsResponse.getNodes()) {
            nodeToCluster.put(remoteNode.getId(), () -> getConnection(remoteNode, clusterName));
        }
        Map<String, AliasFilter> indicesAndFilters = searchShardsResponse.getIndicesAndFilters();
        for (ClusterSearchShardsGroup clusterSearchShardsGroup : searchShardsResponse.getGroups()) {
            //add the cluster name to the remote index names for indices disambiguation
            //this ends up in the hits returned with the search response
            ShardId shardId = clusterSearchShardsGroup.getShardId();
            Index remoteIndex = shardId.getIndex();
            Index index = new Index(clusterName + REMOTE_CLUSTER_INDEX_SEPARATOR + remoteIndex.getName(), remoteIndex.getUUID());
            ShardIterator shardIterator = new PlainShardIterator(new ShardId(index, shardId.getId()), Arrays.asList(clusterSearchShardsGroup.getShards()));
            remoteShardIterators.add(shardIterator);
            AliasFilter aliasFilter;
            if (indicesAndFilters == null) {
                aliasFilter = new AliasFilter(null, Strings.EMPTY_ARRAY);
            } else {
                aliasFilter = indicesAndFilters.get(shardId.getIndexName());
                assert aliasFilter != null;
            }
            // here we have to map the filters to the UUID since from now on we use the uuid for the lookup
            aliasFilterMap.put(remoteIndex.getUUID(), aliasFilter);
        }
    }
    return (nodeId) -> {
        Supplier<Transport.Connection> supplier = nodeToCluster.get(nodeId);
        if (supplier == null) {
            throw new IllegalArgumentException("unknown remote node: " + nodeId);
        }
        return supplier.get();
    };
}
Also used : ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) ShardId(org.elasticsearch.index.shard.ShardId) Arrays(java.util.Arrays) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) TimeoutException(java.util.concurrent.TimeoutException) PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Strings(org.elasticsearch.common.Strings) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Settings(org.elasticsearch.common.settings.Settings) TimeValue(org.elasticsearch.common.unit.TimeValue) Map(java.util.Map) CountDown(org.elasticsearch.common.util.concurrent.CountDown) TransportService(org.elasticsearch.transport.TransportService) ClusterSearchShardsGroup(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup) Transport(org.elasticsearch.transport.Transport) AbstractComponent(org.elasticsearch.common.component.AbstractComponent) Setting(org.elasticsearch.common.settings.Setting) Predicate(java.util.function.Predicate) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOUtils(org.apache.lucene.util.IOUtils) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) TimeUnit(java.util.concurrent.TimeUnit) AliasFilter(org.elasticsearch.search.internal.AliasFilter) List(java.util.List) Version(org.elasticsearch.Version) Stream(java.util.stream.Stream) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Supplier(org.apache.logging.log4j.util.Supplier) Closeable(java.io.Closeable) TransportException(org.elasticsearch.transport.TransportException) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) AliasFilter(org.elasticsearch.search.internal.AliasFilter) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) Index(org.elasticsearch.index.Index) ClusterSearchShardsGroup(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup) ShardId(org.elasticsearch.index.shard.ShardId) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) Supplier(org.apache.logging.log4j.util.Supplier) Transport(org.elasticsearch.transport.Transport) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 98 with Function

use of java.util.function.Function in project vert.x by eclipse.

the class MetricsContextTest method testNetServer.

private void testNetServer(Function<Vertx, Context> contextFactory, BiConsumer<Thread, Context> checker) throws Exception {
    AtomicReference<Thread> expectedThread = new AtomicReference<>();
    AtomicReference<Context> expectedContext = new AtomicReference<>();
    AtomicBoolean socketConnectedCalled = new AtomicBoolean();
    AtomicBoolean socketDisconnectedCalled = new AtomicBoolean();
    AtomicBoolean bytesReadCalled = new AtomicBoolean();
    AtomicBoolean bytesWrittenCalled = new AtomicBoolean();
    AtomicBoolean closeCalled = new AtomicBoolean();
    VertxMetricsFactory factory = (vertx, options) -> new DummyVertxMetrics() {

        @Override
        public TCPMetrics createMetrics(SocketAddress localAddress, NetServerOptions options) {
            return new DummyTCPMetrics() {

                @Override
                public Void connected(SocketAddress remoteAddress, String remoteName) {
                    socketConnectedCalled.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                    return null;
                }

                @Override
                public void disconnected(Void socketMetric, SocketAddress remoteAddress) {
                    socketDisconnectedCalled.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                }

                @Override
                public void bytesRead(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
                    bytesReadCalled.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                }

                @Override
                public void bytesWritten(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
                    bytesWrittenCalled.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                }

                @Override
                public boolean isEnabled() {
                    return true;
                }

                @Override
                public void close() {
                    closeCalled.set(true);
                }
            };
        }
    };
    CountDownLatch latch = new CountDownLatch(1);
    Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
    Context ctx = contextFactory.apply(vertx);
    ctx.runOnContext(v1 -> {
        NetServer server = vertx.createNetServer().connectHandler(so -> {
            so.handler(buf -> {
                so.write("bye");
            });
        });
        server.listen(1234, "localhost", onSuccess(s -> {
            expectedThread.set(Thread.currentThread());
            expectedContext.set(Vertx.currentContext());
            checker.accept(expectedThread.get(), expectedContext.get());
            latch.countDown();
        }));
    });
    awaitLatch(latch);
    NetClient client = vertx.createNetClient();
    client.connect(1234, "localhost", onSuccess(so -> {
        so.handler(buf -> {
            so.closeHandler(v -> {
                executeInVanillaThread(() -> {
                    vertx.close(v4 -> {
                        assertTrue(bytesReadCalled.get());
                        assertTrue(bytesWrittenCalled.get());
                        assertTrue(socketConnectedCalled.get());
                        assertTrue(socketDisconnectedCalled.get());
                        assertTrue(closeCalled.get());
                        testComplete();
                    });
                });
            });
            so.close();
        });
        so.write("hello");
    }));
    await();
}
Also used : io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) io.vertx.core.net(io.vertx.core.net) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) io.vertx.core.http(io.vertx.core.http) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) Ignore(org.junit.Ignore) BiConsumer(java.util.function.BiConsumer) io.vertx.core.spi.metrics(io.vertx.core.spi.metrics) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) CountDownLatch(java.util.concurrent.CountDownLatch) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 99 with Function

use of java.util.function.Function in project vert.x by eclipse.

the class MetricsContextTest method testNetClient.

private void testNetClient(Function<Vertx, Context> contextFactory, BiConsumer<Thread, Context> checker) throws Exception {
    AtomicReference<Thread> expectedThread = new AtomicReference<>();
    AtomicReference<Context> expectedContext = new AtomicReference<>();
    AtomicBoolean socketConnectedCalled = new AtomicBoolean();
    AtomicBoolean socketDisconnectedCalled = new AtomicBoolean();
    AtomicBoolean bytesReadCalled = new AtomicBoolean();
    AtomicBoolean bytesWrittenCalled = new AtomicBoolean();
    AtomicBoolean closeCalled = new AtomicBoolean();
    VertxMetricsFactory factory = (vertx, options) -> new DummyVertxMetrics() {

        @Override
        public TCPMetrics createMetrics(NetClient client, NetClientOptions options) {
            return new DummyTCPMetrics() {

                @Override
                public Void connected(SocketAddress remoteAddress, String remoteName) {
                    socketConnectedCalled.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                    return null;
                }

                @Override
                public void disconnected(Void socketMetric, SocketAddress remoteAddress) {
                    socketDisconnectedCalled.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                }

                @Override
                public void bytesRead(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
                    bytesReadCalled.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                }

                @Override
                public void bytesWritten(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
                    bytesWrittenCalled.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                }

                @Override
                public boolean isEnabled() {
                    return true;
                }

                @Override
                public void close() {
                    closeCalled.set(true);
                }
            };
        }
    };
    CountDownLatch latch = new CountDownLatch(1);
    Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
    Context ctx = contextFactory.apply(vertx);
    NetServer server = vertx.createNetServer().connectHandler(so -> {
        so.handler(buf -> {
            so.write("bye");
        });
    });
    server.listen(1234, "localhost", onSuccess(s -> {
        latch.countDown();
    }));
    awaitLatch(latch);
    ctx.runOnContext(v1 -> {
        NetClient client = vertx.createNetClient();
        expectedThread.set(Thread.currentThread());
        expectedContext.set(Vertx.currentContext());
        client.connect(1234, "localhost", onSuccess(so -> {
            so.handler(buf -> {
                so.closeHandler(v -> {
                    assertTrue(bytesReadCalled.get());
                    assertTrue(bytesWrittenCalled.get());
                    assertTrue(socketConnectedCalled.get());
                    assertTrue(socketDisconnectedCalled.get());
                    executeInVanillaThread(() -> {
                        client.close();
                        vertx.close(v4 -> {
                            assertTrue(closeCalled.get());
                            testComplete();
                        });
                    });
                });
                so.close();
            });
            so.write("hello");
        }));
    });
    await();
}
Also used : io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) io.vertx.core.net(io.vertx.core.net) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) io.vertx.core.http(io.vertx.core.http) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) Ignore(org.junit.Ignore) BiConsumer(java.util.function.BiConsumer) io.vertx.core.spi.metrics(io.vertx.core.spi.metrics) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) CountDownLatch(java.util.concurrent.CountDownLatch) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 100 with Function

use of java.util.function.Function in project vert.x by eclipse.

the class MetricsContextTest method testDatagram.

private void testDatagram(Function<Vertx, Context> contextFactory, BiConsumer<Thread, Context> checker) {
    AtomicReference<Thread> expectedThread = new AtomicReference<>();
    AtomicReference<Context> expectedContext = new AtomicReference<>();
    AtomicBoolean listening = new AtomicBoolean();
    AtomicBoolean bytesReadCalled = new AtomicBoolean();
    AtomicBoolean bytesWrittenCalled = new AtomicBoolean();
    AtomicBoolean closeCalled = new AtomicBoolean();
    VertxMetricsFactory factory = (vertx, options) -> new DummyVertxMetrics() {

        @Override
        public DatagramSocketMetrics createMetrics(DatagramSocket socket, DatagramSocketOptions options) {
            return new DummyDatagramMetrics() {

                @Override
                public void listening(String localName, SocketAddress localAddress) {
                    listening.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                }

                @Override
                public void bytesRead(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
                    bytesReadCalled.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                }

                @Override
                public void bytesWritten(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
                    bytesWrittenCalled.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                }

                @Override
                public void close() {
                    closeCalled.set(true);
                }

                @Override
                public boolean isEnabled() {
                    return true;
                }
            };
        }
    };
    Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
    Context ctx = contextFactory.apply(vertx);
    ctx.runOnContext(v1 -> {
        expectedThread.set(Thread.currentThread());
        expectedContext.set(Vertx.currentContext());
        DatagramSocket socket = vertx.createDatagramSocket();
        socket.listen(1234, "localhost", ar1 -> {
            assertTrue(ar1.succeeded());
            checker.accept(expectedThread.get(), expectedContext.get());
            socket.handler(packet -> {
                assertTrue(listening.get());
                assertTrue(bytesReadCalled.get());
                assertTrue(bytesWrittenCalled.get());
                executeInVanillaThread(() -> {
                    socket.close(ar2 -> {
                        assertTrue(closeCalled.get());
                        assertTrue(ar2.succeeded());
                        testComplete();
                    });
                });
            });
            socket.send(Buffer.buffer("msg"), 1234, "localhost", ar2 -> {
                assertTrue(ar2.succeeded());
            });
        });
    });
    await();
}
Also used : io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) io.vertx.core.net(io.vertx.core.net) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) io.vertx.core.http(io.vertx.core.http) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) Ignore(org.junit.Ignore) BiConsumer(java.util.function.BiConsumer) io.vertx.core.spi.metrics(io.vertx.core.spi.metrics) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DatagramSocket(io.vertx.core.datagram.DatagramSocket)

Aggregations

Function (java.util.function.Function)176 List (java.util.List)75 ArrayList (java.util.ArrayList)55 Map (java.util.Map)51 Test (org.junit.Test)47 IOException (java.io.IOException)44 HashMap (java.util.HashMap)37 Set (java.util.Set)36 Collectors (java.util.stream.Collectors)33 Arrays (java.util.Arrays)30 Collections (java.util.Collections)26 Collection (java.util.Collection)25 File (java.io.File)20 HashSet (java.util.HashSet)19 Supplier (java.util.function.Supplier)19 BiFunction (java.util.function.BiFunction)18 Consumer (java.util.function.Consumer)16 Test (org.testng.annotations.Test)16 Stream (java.util.stream.Stream)14 Assert.assertEquals (org.junit.Assert.assertEquals)13