redis.clients.jedis.mcf.MultiClusterTransaction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jedis_preview Show documentation
Show all versions of jedis_preview Show documentation
Jedis is a blazingly small and sane Redis java client.
The newest version!
package redis.clients.jedis.mcf;
import static redis.clients.jedis.Protocol.Command.DISCARD;
import static redis.clients.jedis.Protocol.Command.EXEC;
import static redis.clients.jedis.Protocol.Command.MULTI;
import static redis.clients.jedis.Protocol.Command.UNWATCH;
import static redis.clients.jedis.Protocol.Command.WATCH;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicInteger;
import redis.clients.jedis.*;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.graph.ResultSet;
import redis.clients.jedis.providers.MultiClusterPooledConnectionProvider;
import redis.clients.jedis.util.KeyValue;
/**
* This is high memory dependent solution as all the appending commands will be hold in memory.
*/
public class MultiClusterTransaction extends TransactionBase {
private static final Builder> NO_OP_BUILDER = BuilderFactory.RAW_OBJECT;
private final CircuitBreakerFailoverConnectionProvider failoverProvider;
private final AtomicInteger extraCommandCount = new AtomicInteger();
private final Queue>> commands = new LinkedList<>();
private boolean inWatch = false;
private boolean inMulti = false;
/**
* A MULTI command will be added to be sent to server. WATCH/UNWATCH/MULTI commands must not be
* called with this object.
* @param provider
*/
public MultiClusterTransaction(MultiClusterPooledConnectionProvider provider) {
this(provider, true);
}
/**
* A user wanting to WATCH/UNWATCH keys followed by a call to MULTI ({@link #multi()}) it should
* be {@code doMulti=false}.
*
* @param provider
* @param doMulti {@code false} should be set to enable manual WATCH, UNWATCH and MULTI
*/
public MultiClusterTransaction(MultiClusterPooledConnectionProvider provider, boolean doMulti) {
this.failoverProvider = new CircuitBreakerFailoverConnectionProvider(provider);
try (Connection connection = failoverProvider.getConnection()) {
RedisProtocol proto = connection.getRedisProtocol();
if (proto != null) this.commandObjects.setProtocol(proto);
}
if (doMulti) multi();
}
@Override
public final void multi() {
appendCommand(new CommandObject<>(new CommandArguments(MULTI), NO_OP_BUILDER));
extraCommandCount.incrementAndGet();
inMulti = true;
}
/**
* @param keys
* @return {@code null}
*/
@Override
public final String watch(String... keys) {
appendCommand(new CommandObject<>(new CommandArguments(WATCH).addObjects((Object[]) keys), NO_OP_BUILDER));
extraCommandCount.incrementAndGet();
inWatch = true;
return null;
}
/**
* @param keys
* @return {@code null}
*/
@Override
public final String watch(byte[]... keys) {
appendCommand(new CommandObject<>(new CommandArguments(WATCH).addObjects((Object[]) keys), NO_OP_BUILDER));
extraCommandCount.incrementAndGet();
inWatch = true;
return null;
}
/**
* @return {@code null}
*/
@Override
public final String unwatch() {
appendCommand(new CommandObject<>(new CommandArguments(UNWATCH), NO_OP_BUILDER));
extraCommandCount.incrementAndGet();
inWatch = false;
return null;
}
@Override
protected final Response appendCommand(CommandObject commandObject) {
CommandArguments args = commandObject.getArguments();
Response response = new Response<>(commandObject.getBuilder());
commands.add(KeyValue.of(args, response));
return response;
}
@Override
public void close() {
clear();
}
private void clear() {
if (inMulti) {
discard();
} else if (inWatch) {
unwatch();
}
}
@Override
public final List