Skip to content

Commit

Permalink
feat: allow client instantiation with a Jedis instance (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsbodden authored Nov 8, 2021
1 parent 19db83e commit 0dc2c40
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/main/java/io/rebloom/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,21 @@
public class Client implements Cuckoo, CMS, TDigest, Closeable {

private final Pool<Jedis> pool;
private final Jedis jedis;

/**
* Create a new client to ReBloom
* @param pool Jedis connection pool to be used
*/
public Client(Pool<Jedis> pool){
this.pool = pool;
this.jedis = null;
}

public Client(Jedis jedis) {
this.jedis = jedis;
this.pool = null;
}

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

pool = new JedisPool(conf, host, port, timeout);
jedis = null;
}

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

@Override
public void close(){
this.pool.close();
if (pool != null) {
pool.close();
}
if (jedis != null) {
jedis.close();
}
}

Jedis _conn() {
return pool.getResource();
return jedis != null ? jedis : pool.getResource();
}

/**
Expand Down Expand Up @@ -395,7 +407,7 @@ public List<String> topkList(String key) {
.getMultiBulkReply();
}
}

//
// Count-Min-Sketch Implementation
//
Expand Down
15 changes: 14 additions & 1 deletion src/test/java/io/rebloom/client/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Map;
import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.exceptions.JedisException;
Expand All @@ -14,7 +15,7 @@
* @author Mark Nunberg
*/
public class ClientTest extends TestBase {

@Test
public void createWithPool() {
Client refClient;
Expand All @@ -26,6 +27,18 @@ public void createWithPool() {
assertThrows(JedisException.class, () -> refClient.createFilter("myBloom", 100, 0.001));
}

@Test
public void createWithJedisInstance() {
Client refClient;
Jedis jedis = new Jedis();
try(Client client = new Client(jedis)){
client.createFilter("createBloom", 100, 0.001);
assertTrue(client.delete("createBloom"));
} catch (Exception e) {
fail();
}
}

@Test
public void reserveBasic() {
cl.createFilter("myBloom", 100, 0.001);
Expand Down

0 comments on commit 0dc2c40

Please sign in to comment.