simplification of sharding
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.io.IOException;
|
||||
import redis.clients.util.RedisInputStream;
|
||||
import redis.clients.util.RedisOutputStream;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import redis.clients.util.RedisInputStream;
|
||||
import redis.clients.util.RedisOutputStream;
|
||||
|
||||
public class Connection {
|
||||
private String host;
|
||||
private int port = Protocol.DEFAULT_PORT;
|
||||
@@ -50,13 +50,13 @@ public class Connection {
|
||||
}
|
||||
|
||||
protected Connection sendCommand(String name, String... args) {
|
||||
try {
|
||||
connect();
|
||||
} catch (UnknownHostException e) {
|
||||
throw new JedisException("Could not connect to redis-server", e);
|
||||
} catch (IOException e) {
|
||||
throw new JedisException("Could not connect to redis-server", e);
|
||||
}
|
||||
try {
|
||||
connect();
|
||||
} catch (UnknownHostException e) {
|
||||
throw new JedisException("Could not connect to redis-server", e);
|
||||
} catch (IOException e) {
|
||||
throw new JedisException("Could not connect to redis-server", e);
|
||||
}
|
||||
protocol.sendCommand(outputStream, name, args);
|
||||
pipelinedCommands++;
|
||||
return this;
|
||||
@@ -126,9 +126,9 @@ public class Connection {
|
||||
return (String) protocol.read(inputStream);
|
||||
}
|
||||
|
||||
public Integer getIntegerReply() {
|
||||
public int getIntegerReply() {
|
||||
pipelinedCommands--;
|
||||
return (Integer) protocol.read(inputStream);
|
||||
return ((Integer) protocol.read(inputStream)).intValue();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@@ -29,7 +29,7 @@ public class Jedis {
|
||||
client.setTimeout(timeout);
|
||||
}
|
||||
|
||||
public Jedis(ShardInfo shardInfo) {
|
||||
public Jedis(JedisShardInfo shardInfo) {
|
||||
client = new Client(shardInfo.getHost(), shardInfo.getPort());
|
||||
client.setTimeout(shardInfo.getTimeout());
|
||||
if (shardInfo.getPassword() != null) {
|
||||
|
||||
@@ -32,7 +32,7 @@ public class JedisPool extends FixedResourcePool<Jedis> {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public JedisPool(ShardInfo shardInfo) {
|
||||
public JedisPool(JedisShardInfo shardInfo) {
|
||||
this.host = shardInfo.getHost();
|
||||
this.port = shardInfo.getPort();
|
||||
this.timeout = shardInfo.getTimeout();
|
||||
@@ -80,4 +80,4 @@ public class JedisPool extends FixedResourcePool<Jedis> {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
80
src/main/java/redis/clients/jedis/JedisShardInfo.java
Normal file
80
src/main/java/redis/clients/jedis/JedisShardInfo.java
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2009-2010 MBTE Sweden AB.
|
||||
*
|
||||
* 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 redis.clients.jedis;
|
||||
|
||||
import redis.clients.util.ShardInfo;
|
||||
import redis.clients.util.Sharded;
|
||||
|
||||
public class JedisShardInfo extends ShardInfo<Jedis> {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "JedisShardInfo [host=" + host + ", port=" + port + ", weight="
|
||||
+ getWeight() + "]";
|
||||
}
|
||||
|
||||
private int timeout;
|
||||
private String host;
|
||||
private int port;
|
||||
private String password = null;
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public JedisShardInfo(String host) {
|
||||
this(host, Protocol.DEFAULT_PORT);
|
||||
}
|
||||
|
||||
public JedisShardInfo(String host, int port) {
|
||||
this(host, port, 2000);
|
||||
}
|
||||
|
||||
public JedisShardInfo(String host, int port, int timeout) {
|
||||
this(host, port, timeout, Sharded.DEFAULT_WEIGHT);
|
||||
}
|
||||
|
||||
public JedisShardInfo(String host, int port, int timeout, int weight) {
|
||||
super(weight);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String auth) {
|
||||
this.password = auth;
|
||||
}
|
||||
|
||||
public int getTimeout() {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
public void setTimeout(int timeout) {
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Jedis createResource() {
|
||||
return new Jedis(this);
|
||||
}
|
||||
}
|
||||
@@ -10,12 +10,12 @@ import redis.clients.util.Hashing;
|
||||
import redis.clients.util.ShardInfo;
|
||||
import redis.clients.util.Sharded;
|
||||
|
||||
public class ShardedJedis extends Sharded<Jedis> {
|
||||
public ShardedJedis(List<ShardInfo> shards) {
|
||||
public class ShardedJedis extends Sharded<Jedis,JedisShardInfo> {
|
||||
public ShardedJedis(List<JedisShardInfo> shards) {
|
||||
super(shards);
|
||||
}
|
||||
|
||||
public ShardedJedis(List<ShardInfo> shards, Hashing algo) {
|
||||
public ShardedJedis(List<JedisShardInfo> shards, Hashing algo) {
|
||||
super(shards, algo);
|
||||
}
|
||||
|
||||
@@ -347,12 +347,12 @@ public class ShardedJedis extends Sharded<Jedis> {
|
||||
}
|
||||
|
||||
public void disconnect() throws IOException {
|
||||
for (Jedis jedis : getAllShards()) {
|
||||
jedis.disconnect();
|
||||
for (JedisShardInfo jedis : getAllShards()) {
|
||||
jedis.getResource().disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
protected Jedis create(ShardInfo shard) {
|
||||
protected Jedis create(JedisShardInfo shard) {
|
||||
return new Jedis(shard);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user