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();
}
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();
};
}
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();
}
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();
}
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();
}
Aggregations