
com.lambdaworks.redis.cluster.PipelinedRedisFuture Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lettuce Show documentation
Show all versions of lettuce Show documentation
Advanced and thread-safe Java Redis client for synchronous, asynchronous, and
reactive usage. Supports Cluster, Sentinel, Pipelining, Auto-Reconnect, Codecs
and much more.
The newest version!
/*
* Copyright 2011-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lambdaworks.redis.cluster;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import com.lambdaworks.redis.RedisFuture;
/**
* Pipelining for commands that are executed on multiple cluster nodes. Merges results and emits one composite result.
*
* @author Mark Paluch
*/
class PipelinedRedisFuture extends CompletableFuture implements RedisFuture {
private final CountDownLatch latch = new CountDownLatch(1);
public PipelinedRedisFuture(CompletionStage completionStage, Function converter) {
completionStage.thenAccept(v -> complete(converter.apply(v)))
.exceptionally(throwable -> {
completeExceptionally(throwable);
return null;
});
}
public PipelinedRedisFuture(Map, ? extends RedisFuture>> executions, Function, V> converter) {
CompletableFuture.allOf(executions.values().toArray(new CompletableFuture>[executions.size()]))
.thenRun(() -> complete(converter.apply(this))).exceptionally(throwable -> {
completeExceptionally(throwable);
return null;
});
}
@Override
public boolean complete(V value) {
boolean result = super.complete(value);
latch.countDown();
return result;
}
@Override
public boolean completeExceptionally(Throwable ex) {
boolean value = super.completeExceptionally(ex);
latch.countDown();
return value;
}
@Override
public String getError() {
return null;
}
@Override
public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
return latch.await(timeout, unit);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy