Skip to content

Instantly share code, notes, and snippets.

@sankarcheppali
Created May 16, 2023 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sankarcheppali/989d861ff0512a07e53d6dc43653263e to your computer and use it in GitHub Desktop.
Save sankarcheppali/989d861ff0512a07e53d6dc43653263e to your computer and use it in GitHub Desktop.
Using redis bloom filter with jedis driver
package net.icircuit.bf.redis;
import redis.clients.jedis.JedisPooled;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;
import static java.util.stream.Collectors.toSet;
public class JedisBloomFilter {
JedisPooled pool = new JedisPooled("localhost", 6379);
/**
* returns entries that were not present previously in the bloom filter and were added
*/
public Set<String> put(String filterName, Set<String> entries) {
final String[] entriesArr = entries.toArray(new String[0]);
final List<Boolean> result = pool.bfMAdd(filterName, entriesArr);
return IntStream.range(0, result.size())
.filter(result::get)
.mapToObj(index -> entriesArr[index])
.collect(toSet());
}
/**
* returns entries that were present in the bloom filter
*/
public Set<String> get(String filterName, Set<String> entries) {
final String[] entriesArr = entries.toArray(new String[0]);
final List<Boolean> result = pool.bfMExists(filterName, entriesArr);
return IntStream.range(0, result.size())
.filter(result::get)
.mapToObj(index -> entriesArr[index])
.collect(toSet());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment