use of com.couchbase.client.core.deps.com.fasterxml.jackson.databind.node.ArrayNode in project couchbase-jvm-clients by couchbase.
the class ProjectionsApplier method insertRecursive.
/**
* Will follow `path`, constructing JSON as it does into `out`, and inserting `content` at the leaf
*
* @param out must be either a ObjectNode or ArrayNode, and the JSON will be constructed into it
*/
private static void insertRecursive(final ContainerNode<?> out, final List<PathElement> path, final JsonNode content) {
if (path.isEmpty()) {
// Recursion is done
return;
}
if (path.size() == 1) {
// At leaf of path
PathElement leaf = path.get(0);
if (leaf instanceof PathArray) {
PathArray v = (PathArray) leaf;
ArrayNode toInsert = out.arrayNode().add(content);
if (out.isObject()) {
((ObjectNode) out).set(v.str(), toInsert);
} else {
((ArrayNode) out).add(toInsert);
}
} else {
PathObjectOrField v = (PathObjectOrField) leaf;
if (out.isObject()) {
((ObjectNode) out).set(v.str(), content);
} else {
ObjectNode toInsert = out.objectNode();
toInsert.set(v.str(), content);
((ArrayNode) out).add(toInsert);
}
}
} else {
// Non-leaf
PathElement next = path.get(0);
List<PathElement> remaining = path.subList(1, path.size());
if (next instanceof PathArray) {
PathArray v = (PathArray) next;
ArrayNode toInsert = out.arrayNode();
if (out.isObject()) {
((ObjectNode) out).set(v.str(), toInsert);
insertRecursive(toInsert, remaining, content);
} else {
((ArrayNode) out).add(toInsert);
insertRecursive(out, remaining, content);
}
} else {
PathObjectOrField v = (PathObjectOrField) next;
if (out.isObject()) {
ObjectNode createIn = (ObjectNode) out.get(v.str());
if (createIn == null) {
createIn = out.objectNode();
}
((ObjectNode) out).set(v.str(), createIn);
insertRecursive(createIn, remaining, content);
} else {
ObjectNode toCreate = out.objectNode();
ObjectNode nextToCreate = out.objectNode();
toCreate.set(v.str(), nextToCreate);
((ArrayNode) out).add(toCreate);
insertRecursive(nextToCreate, remaining, content);
}
}
}
}
use of com.couchbase.client.core.deps.com.fasterxml.jackson.databind.node.ArrayNode in project couchbase-jvm-clients by couchbase.
the class WaitUntilReadyHelper method waitUntilReady.
@Stability.Internal
public static CompletableFuture<Void> waitUntilReady(final Core core, final Set<ServiceType> serviceTypes, final Duration timeout, final ClusterState desiredState, final Optional<String> bucketName) {
final WaitUntilReadyState state = new WaitUntilReadyState();
state.transition(WaitUntilReadyStage.CONFIG_LOAD);
return Flux.interval(Duration.ofMillis(10), core.context().environment().scheduler()).onBackpressureDrop().filter(i -> !(core.configurationProvider().bucketConfigLoadInProgress() || core.configurationProvider().globalConfigLoadInProgress() || (bucketName.isPresent() && core.configurationProvider().collectionRefreshInProgress()) || (bucketName.isPresent() && core.clusterConfig().bucketConfig(bucketName.get()) == null))).filter(i -> {
// created.
if (bucketName.isPresent()) {
state.transition(WaitUntilReadyStage.BUCKET_CONFIG_READY);
BucketConfig bucketConfig = core.clusterConfig().bucketConfig(bucketName.get());
long extNodes = bucketConfig.portInfos().stream().filter(p -> p.ports().containsKey(ServiceType.KV)).count();
long visibleNodes = bucketConfig.nodes().stream().filter(n -> n.services().containsKey(ServiceType.KV)).count();
return extNodes > 0 && extNodes == visibleNodes;
} else {
return true;
}
}).flatMap(i -> {
if (bucketName.isPresent()) {
state.transition(WaitUntilReadyStage.BUCKET_NODES_HEALTHY);
// To avoid tmpfails on the bucket, we double check that all nodes from the nodes list are
// in a healthy status - but for this we need to actually fetch the verbose config, since
// the terse one doesn't have that status in it.
GenericManagerRequest request = new GenericManagerRequest(core.context(), () -> new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/pools/default/buckets/" + bucketName.get()), true, null);
core.send(request);
return Reactor.wrap(request, request.response(), true).filter(response -> {
if (response.status() != ResponseStatus.SUCCESS) {
return false;
}
ObjectNode root = (ObjectNode) Mapper.decodeIntoTree(response.content());
ArrayNode nodes = (ArrayNode) root.get("nodes");
long healthy = StreamSupport.stream(nodes.spliterator(), false).filter(node -> node.get("status").asText().equals("healthy")).count();
return nodes.size() == healthy;
}).map(ignored -> i);
} else {
return Flux.just(i);
}
}).take(1).flatMap(aLong -> {
// to being ready at this point. Bucket level wait until ready is the way to go there.
if (!bucketName.isPresent() && !core.clusterConfig().hasClusterOrBucketConfig()) {
state.transition(WaitUntilReadyStage.COMPLETE);
WaitUntilReadyContext waitUntilReadyContext = new WaitUntilReadyContext(servicesToCheck(core, serviceTypes, bucketName), timeout, desiredState, bucketName, core.diagnostics().collect(Collectors.groupingBy(EndpointDiagnostics::type)), state);
core.context().environment().eventBus().publish(new WaitUntilReadyCompletedEvent(waitUntilReadyContext, WaitUntilReadyCompletedEvent.Reason.CLUSTER_LEVEL_NOT_SUPPORTED));
return Flux.empty();
}
state.transition(WaitUntilReadyStage.PING);
final Flux<ClusterState> diagnostics = Flux.interval(Duration.ofMillis(10), core.context().environment().scheduler()).onBackpressureDrop().map(i -> diagnosticsCurrentState(core)).takeUntil(s -> s == desiredState);
return Flux.concat(ping(core, servicesToCheck(core, serviceTypes, bucketName), timeout, bucketName), diagnostics);
}).then().timeout(timeout, Mono.defer(() -> {
WaitUntilReadyContext waitUntilReadyContext = new WaitUntilReadyContext(servicesToCheck(core, serviceTypes, bucketName), timeout, desiredState, bucketName, core.diagnostics().collect(Collectors.groupingBy(EndpointDiagnostics::type)), state);
CancellationErrorContext errorContext = new CancellationErrorContext(waitUntilReadyContext);
return Mono.error(new UnambiguousTimeoutException("WaitUntilReady timed out", errorContext));
}), core.context().environment().scheduler()).doOnSuccess(unused -> {
state.transition(WaitUntilReadyStage.COMPLETE);
WaitUntilReadyContext waitUntilReadyContext = new WaitUntilReadyContext(servicesToCheck(core, serviceTypes, bucketName), timeout, desiredState, bucketName, core.diagnostics().collect(Collectors.groupingBy(EndpointDiagnostics::type)), state);
core.context().environment().eventBus().publish(new WaitUntilReadyCompletedEvent(waitUntilReadyContext, WaitUntilReadyCompletedEvent.Reason.SUCCESS));
}).toFuture();
}
Aggregations