Skip to content

Commit 0dc2c40

Browse files
authored
feat: allow client instantiation with a Jedis instance (#57)
1 parent 19db83e commit 0dc2c40

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/main/java/io/rebloom/client/Client.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,21 @@
3737
public class Client implements Cuckoo, CMS, TDigest, Closeable {
3838

3939
private final Pool<Jedis> pool;
40+
private final Jedis jedis;
4041

4142
/**
4243
* Create a new client to ReBloom
4344
* @param pool Jedis connection pool to be used
4445
*/
4546
public Client(Pool<Jedis> pool){
4647
this.pool = pool;
48+
this.jedis = null;
4749
}
4850

51+
public Client(Jedis jedis) {
52+
this.jedis = jedis;
53+
this.pool = null;
54+
}
4955

5056
/**
5157
* Create a new client to ReBloom
@@ -67,6 +73,7 @@ public Client(String host, int port, int timeout, int poolSize) {
6773
conf.setFairness(true);
6874

6975
pool = new JedisPool(conf, host, port, timeout);
76+
jedis = null;
7077
}
7178

7279
/**
@@ -80,11 +87,16 @@ public Client(String host, int port) {
8087

8188
@Override
8289
public void close(){
83-
this.pool.close();
90+
if (pool != null) {
91+
pool.close();
92+
}
93+
if (jedis != null) {
94+
jedis.close();
95+
}
8496
}
8597

8698
Jedis _conn() {
87-
return pool.getResource();
99+
return jedis != null ? jedis : pool.getResource();
88100
}
89101

90102
/**
@@ -395,7 +407,7 @@ public List<String> topkList(String key) {
395407
.getMultiBulkReply();
396408
}
397409
}
398-
410+
399411
//
400412
// Count-Min-Sketch Implementation
401413
//

src/test/java/io/rebloom/client/ClientTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Map;
77
import org.junit.Test;
88

9+
import redis.clients.jedis.Jedis;
910
import redis.clients.jedis.JedisPool;
1011
import redis.clients.jedis.exceptions.JedisDataException;
1112
import redis.clients.jedis.exceptions.JedisException;
@@ -14,7 +15,7 @@
1415
* @author Mark Nunberg
1516
*/
1617
public class ClientTest extends TestBase {
17-
18+
1819
@Test
1920
public void createWithPool() {
2021
Client refClient;
@@ -26,6 +27,18 @@ public void createWithPool() {
2627
assertThrows(JedisException.class, () -> refClient.createFilter("myBloom", 100, 0.001));
2728
}
2829

30+
@Test
31+
public void createWithJedisInstance() {
32+
Client refClient;
33+
Jedis jedis = new Jedis();
34+
try(Client client = new Client(jedis)){
35+
client.createFilter("createBloom", 100, 0.001);
36+
assertTrue(client.delete("createBloom"));
37+
} catch (Exception e) {
38+
fail();
39+
}
40+
}
41+
2942
@Test
3043
public void reserveBasic() {
3144
cl.createFilter("myBloom", 100, 0.001);

0 commit comments

Comments
 (0)