Skip to content

Commit

Permalink
Test module commands at 6479 port and test JSON commands
Browse files Browse the repository at this point in the history
Also, fix JSON commands
  • Loading branch information
sazzad16 committed Nov 9, 2021
1 parent b53c87e commit 652a782
Show file tree
Hide file tree
Showing 42 changed files with 938 additions and 78 deletions.
5 changes: 3 additions & 2 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,8 @@ public LCSMatchResult build(Object data) {
public static final Builder<Class<?>> REDIS_JSON_TYPE = new Builder<Class<?>>() {
@Override
public Class<?> build(Object data) {
switch ((String) data) {
String str = STRING.build(data);
switch ((String) str) {
case "null":
return null;
case "boolean":
Expand All @@ -1240,7 +1241,7 @@ public Class<?> build(Object data) {
case "array":
return List.class;
default:
throw new RuntimeException((String) data);
throw new RuntimeException(str);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/redis/clients/jedis/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Connection(final JedisSocketFactory socketFactory, JedisClientConfig clie
this(socketFactory, clientConfig, null);
}

public Connection(final JedisSocketFactory socketFactory, JedisClientConfig clientConfig, Pool<Connection> pool) {
Connection(final JedisSocketFactory socketFactory, JedisClientConfig clientConfig, Pool<Connection> pool) {
this.socketFactory = socketFactory;
this.soTimeout = clientConfig.getSocketTimeoutMillis();
this.infiniteSoTimeout = clientConfig.getBlockingSocketTimeoutMillis();
Expand Down Expand Up @@ -430,12 +430,13 @@ private String clientSetname(final String name) {
return getStatusCodeReply();
}

public void ping() {
public boolean ping() {
sendCommand(Protocol.Command.PING);
String status = getStatusCodeReply();
if (!"PONG".equals(status)) {
throw new JedisException(status);
}
return true;
}

}
129 changes: 129 additions & 0 deletions src/main/java/redis/clients/jedis/ConnectionFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package redis.clients.jedis;


import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.exceptions.JedisException;
import redis.clients.jedis.util.Pool;

/**
* PoolableObjectFactory custom impl.
*/
public class ConnectionFactory implements PooledObjectFactory<Connection> {

private static final Logger logger = LoggerFactory.getLogger(ConnectionFactory.class);

private final JedisSocketFactory jedisSocketFactory;

private final JedisClientConfig clientConfig;

private volatile Pool<Connection> pool;

public ConnectionFactory(final HostAndPort hostAndPort) {
this.clientConfig = DefaultJedisClientConfig.builder().build();
this.jedisSocketFactory = new DefaultJedisSocketFactory(hostAndPort);
}

public ConnectionFactory(final HostAndPort hostAndPort, final JedisClientConfig clientConfig) {
this.clientConfig = DefaultJedisClientConfig.copyConfig(clientConfig);
this.jedisSocketFactory = new DefaultJedisSocketFactory(hostAndPort, this.clientConfig);
}

public ConnectionFactory(final JedisSocketFactory jedisSocketFactory, final JedisClientConfig clientConfig) {
this.clientConfig = DefaultJedisClientConfig.copyConfig(clientConfig);
this.jedisSocketFactory = jedisSocketFactory;
}

public void setPool(Pool<Connection> pool) {
this.pool = pool;
}

public void setPassword(final String password) {
this.clientConfig.updatePassword(password);
}

@Override
public void activateObject(PooledObject<Connection> pooledConnection) throws Exception {
// final Connection jedis = pooledConnection.getObject();
// if (jedis.getDB() != clientConfig.getDatabase()) {
// jedis.select(clientConfig.getDatabase());
// }
}

@Override
public void destroyObject(PooledObject<Connection> pooledConnection) throws Exception {
final Connection jedis = pooledConnection.getObject();
if (jedis.isConnected()) {
try {
// need a proper test, probably with mock
if (!jedis.isBroken()) {
jedis.quit();
}
} catch (RuntimeException e) {
logger.warn("Error while QUIT", e);
}
try {
jedis.close();
} catch (RuntimeException e) {
logger.warn("Error while close", e);
}
}
}

@Override
public PooledObject<Connection> makeObject() throws Exception {
if (pool == null) {
throw new IllegalStateException("Pool is not set.");
}
Connection jedis = null;
try {
jedis = new Connection(jedisSocketFactory, clientConfig, pool);
jedis.connect();
return new DefaultPooledObject<>(jedis);
} catch (JedisException je) {
if (jedis != null) {
try {
jedis.quit();
} catch (RuntimeException e) {
logger.warn("Error while QUIT", e);
}
try {
jedis.close();
} catch (RuntimeException e) {
logger.warn("Error while close", e);
}
}
throw je;
}
}

@Override
public void passivateObject(PooledObject<Connection> pooledConnection) throws Exception {
// TODO maybe should select db 0? Not sure right now.
}

@Override
public boolean validateObject(PooledObject<Connection> pooledConnection) {
final Connection jedis = pooledConnection.getObject();
try {
// String host = jedisSocketFactory.getHost();
// int port = jedisSocketFactory.getPort();
//
//
// String connectionHost = jedis.getClient().getHost();
// int connectionPort = jedis.getClient().getPort();
//
// return host.equals(connectionHost)
// && port == connectionPort && jedis.isConnected()
// && jedis.ping().equals("PONG");
return jedis.isConnected() && jedis.ping();
} catch (final Exception e) {
logger.error("Error while validating pooled Connection object.", e);
return false;
}
}
}
28 changes: 20 additions & 8 deletions src/main/java/redis/clients/jedis/RedisCommandObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -2471,6 +2471,10 @@ public final CommandObject<String> jsonSet(String key, Path path, Object object,
return new CommandObject<>(commandArguments(JsonCommand.SET).key(key).add(path).add(GSON.toJson(object)).addParams(params), BuilderFactory.STRING);
}

public final CommandObject<Object> jsonGet(String key) {
return new CommandObject<>(commandArguments(JsonCommand.GET).key(key), new GsonObjectBuilder<>(Object.class));
}

public final <T> CommandObject<T> jsonGet(String key, Class<T> clazz) {
return new CommandObject<>(commandArguments(JsonCommand.GET).key(key), new GsonObjectBuilder<>(clazz));
}
Expand All @@ -2480,7 +2484,7 @@ public final <T> CommandObject<T> jsonGet(String key, Class<T> clazz, Path... pa
}

public final <T> CommandObject<List<T>> jsonMGet(Class<T> clazz, String... keys) {
return new CommandObject<>(commandArguments(JsonCommand.MGET).keys((Object[]) keys), new GsonObjectListBuilder<>(clazz));
return jsonMGet(Path.ROOT_PATH, clazz, keys);
}

public final <T> CommandObject<List<T>> jsonMGet(Path path, Class<T> clazz, String... keys) {
Expand All @@ -2504,23 +2508,31 @@ public final CommandObject<String> jsonToggle(String key, Path path) {
}

public final CommandObject<Class<?>> jsonType(String key) {
return jsonType(key, Path.ROOT_PATH);
return new CommandObject<>(commandArguments(JsonCommand.TYPE).key(key), BuilderFactory.REDIS_JSON_TYPE);
}

public final CommandObject<Class<?>> jsonType(String key, Path path) {
return new CommandObject<>(commandArguments(JsonCommand.TYPE).key(key).add(path), BuilderFactory.REDIS_JSON_TYPE);
}

public final CommandObject<Long> jsonStrAppend(String key, Path path, Object... objects) {
return new CommandObject<>(commandArguments(JsonCommand.STRAPPEND).key(key).add(path).add(objects), BuilderFactory.LONG);
CommandArguments args = commandArguments(JsonCommand.STRAPPEND).key(key).add(path);
for (Object object : objects) {
args.add(GSON.toJson(object));
}
return new CommandObject<>(args, BuilderFactory.LONG);
}

public final CommandObject<Long> jsonStrLen(String key, Path path) {
return new CommandObject<>(commandArguments(JsonCommand.STRLEN).key(key).add(path), BuilderFactory.LONG);
}

public final CommandObject<Long> jsonArrAppend(String key, Path path, Object... objects) {
return new CommandObject<>(commandArguments(JsonCommand.ARRAPPEND).key(key).add(path).add(objects), BuilderFactory.LONG);
CommandArguments args = commandArguments(JsonCommand.ARRAPPEND).key(key).add(path);
for (Object object : objects) {
args.add(GSON.toJson(object));
}
return new CommandObject<>(args, BuilderFactory.LONG);
}

public final CommandObject<Long> jsonArrIndex(String key, Path path, Object scalar) {
Expand All @@ -2543,16 +2555,16 @@ public final CommandObject<Long> jsonArrTrim(String key, Path path, Long start,
return new CommandObject<>(commandArguments(JsonCommand.ARRTRIM).key(key).add(path).add(start).add(stop), BuilderFactory.LONG);
}

public final <T> CommandObject<T> jsonArrPop(String key, Class<T> clazz, Path path, Long index) {
return new CommandObject<>(commandArguments(JsonCommand.ARRPOP).key(key).add(path).add(index), new GsonObjectBuilder<>(clazz));
public final <T> CommandObject<T> jsonArrPop(String key, Class<T> clazz) {
return new CommandObject<>(commandArguments(JsonCommand.ARRPOP).key(key), new GsonObjectBuilder<>(clazz));
}

public final <T> CommandObject<T> jsonArrPop(String key, Class<T> clazz, Path path) {
return new CommandObject<>(commandArguments(JsonCommand.ARRPOP).key(key).add(path), new GsonObjectBuilder<>(clazz));
}

public final <T> CommandObject<T> jsonArrPop(String key, Class<T> clazz) {
return jsonArrPop(key, clazz, Path.ROOT_PATH);
public final <T> CommandObject<T> jsonArrPop(String key, Class<T> clazz, Path path, long index) {
return new CommandObject<>(commandArguments(JsonCommand.ARRPOP).key(key).add(path).add(index), new GsonObjectBuilder<>(clazz));
}
// RedisJSON commands

Expand Down
7 changes: 6 additions & 1 deletion src/main/java/redis/clients/jedis/UnifiedJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -2816,6 +2816,11 @@ public String jsonSet(String key, Path path, Object object, JsonSetParams params
return executeCommand(commandObjects.jsonSet(key, path, object, params));
}

@Override
public Object jsonGet(String key) {
return executeCommand(commandObjects.jsonGet(key));
}

@Override
public <T> T jsonGet(String key, Class<T> clazz) {
return executeCommand(commandObjects.jsonGet(key, clazz));
Expand Down Expand Up @@ -2847,7 +2852,7 @@ public Long jsonDel(String key, Path path) {
}

@Override
public Long jsonClear(String key, Path path) {
public long jsonClear(String key, Path path) {
return executeCommand(commandObjects.jsonClear(key, path));
}

Expand Down
49 changes: 27 additions & 22 deletions src/main/java/redis/clients/jedis/json/JsonSetParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,37 @@
import redis.clients.jedis.CommandArguments;
import redis.clients.jedis.params.IParams;


public class JsonSetParams implements IParams {
private boolean nx = false;
private boolean xx = false;

public JsonSetParams() { }
private boolean nx = false;
private boolean xx = false;

public JsonSetParams setNX() {
this.nx = true;
this.xx = false;
return this;
}
public JsonSetParams() {
}

public JsonSetParams setXX() {
this.nx = false;
this.xx = true;
return this;
}
public static JsonSetParams jsonSetParams() {
return new JsonSetParams();
}

@Override
public void addParams(CommandArguments args) {
if (this.nx) {
args.add("NX");
}
if (this.xx) {
args.add("XX");
}
public JsonSetParams nx() {
this.nx = true;
this.xx = false;
return this;
}

public JsonSetParams xx() {
this.nx = false;
this.xx = true;
return this;
}

@Override
public void addParams(CommandArguments args) {
if (nx) {
args.add("NX");
}
if (xx) {
args.add("XX");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface RedisJsonCommands {

String jsonSet(String key, Path path, Object object, JsonSetParams params);

Object jsonGet(String key);
//<T> T jsonGet(String key);

//<T> T jsonGet(String key, Path... paths);
Expand All @@ -28,7 +29,7 @@ public interface RedisJsonCommands {

Long jsonDel(String key, Path path);

Long jsonClear(String key, Path path);
long jsonClear(String key, Path path);

String jsonToggle(String key, Path path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public Connection getConnection(CommandArguments args) {
return slot >= 0 ? getConnectionFromSlot(slot) : getConnection();
}

@Override
public Connection getConnection() {
// In antirez's redis-rb-cluster implementation, getRandomConnection always
// return valid connection (able to ping-pong) or exception if all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@

public interface JedisConnectionProvider extends AutoCloseable {

Connection getConnection();

Connection getConnection(CommandArguments args);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public final void setConnection(Connection connection) {
public void close() {
}

@Override
public final Connection getConnection() {
return connection;
}

@Override
public final Connection getConnection(CommandArguments args) {
return connection;
Expand Down
Loading

0 comments on commit 652a782

Please sign in to comment.