-
-
Save sankarcheppali/989d861ff0512a07e53d6dc43653263e to your computer and use it in GitHub Desktop.
Using redis bloom filter with jedis driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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