From 06cb63756c1ec7d478b7d56ba1043cd98cc1f3cd Mon Sep 17 00:00:00 2001 From: Simon Prickett Date: Sat, 15 Jan 2022 16:47:47 +0000 Subject: [PATCH] Adds Bloom Filter example using RedisBloom. (#1835) * Adds Bloom Filter example using RedisBloom. * Update bloom-filter.js Co-authored-by: Leibale Eidelman --- examples/README.md | 27 +++++++------- examples/bloom-filter.js | 79 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 examples/bloom-filter.js diff --git a/examples/README.md b/examples/README.md index 5151edfe1a..cd7e86882d 100644 --- a/examples/README.md +++ b/examples/README.md @@ -2,19 +2,20 @@ This folder contains example scripts showing how to use Node Redis in different scenarios. -| File Name | Description | -|-----------------------------|----------------------------------------------------------------------------------------------------------------| -| `blocking-list-pop.js` | Block until an element is pushed to a list | -| `command-with-modifiers.js` | Define a script that allows to run a command with several modifiers | -| `connect-as-acl-user.js` | Connect to Redis 6 using an ACL user | -| `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys | -| `managing-json.js` | Store, retrieve and manipulate JSON data atomically with [RedisJSON](https://redisjson.io/) | -| `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes | -| `search-json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data | -| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality | -| `stream-producer.js` | Adds entries to a [Redis Stream](https://redis.io/topics/streams-intro) using the `XADD` command | -| `stream-consumer.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) using the blocking `XREAD` command | -| `topk.js` | Use the [RedisBloom](https://redisbloom.io) TopK to track the most frequently seen items. | +| File Name | Description | +|-----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------| +| `blocking-list-pop.js` | Block until an element is pushed to a list | +| `bloom-filter.js` | Space efficient set membership checks with a [Bloom Filter](https://en.wikipedia.org/wiki/Bloom_filter) using [RedisBloom](https://redisbloom.io) | +| `command-with-modifiers.js` | Define a script that allows to run a command with several modifiers | +| `connect-as-acl-user.js` | Connect to Redis 6 using an ACL user | +| `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys | +| `managing-json.js` | Store, retrieve and manipulate JSON data atomically with [RedisJSON](https://redisjson.io/) | +| `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes | +| `search-json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data | +| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality | +| `stream-producer.js` | Adds entries to a [Redis Stream](https://redis.io/topics/streams-intro) using the `XADD` command | +| `stream-consumer.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) using the blocking `XREAD` command | +| `topk.js` | Use the [RedisBloom](https://redisbloom.io) TopK to track the most frequently seen items. | ## Contributing diff --git a/examples/bloom-filter.js b/examples/bloom-filter.js new file mode 100644 index 0000000000..db55c519c3 --- /dev/null +++ b/examples/bloom-filter.js @@ -0,0 +1,79 @@ +// This example demonstrates the use of the Bloom Filter +// in the RedisBloom module (https://redisbloom.io/) + +import { createClient } from 'redis'; + +async function bloomFilter() { + const client = createClient(); + + await client.connect(); + + // Delete any pre-existing Bloom Filter. + await client.del('mybloom'); + + // Reserve a Bloom Filter with configurable error rate and capacity. + // https://oss.redis.com/redisbloom/Bloom_Commands/#bfreserve + try { + await client.bf.reserve('mybloom', 0.01, 1000); + console.log('Reserved Bloom Filter.'); + } catch (e) { + if (e.message.endsWith('item exists')) { + console.log('Bloom Filter already reserved.'); + } else { + console.log('Error, maybe RedisBloom is not installed?:'); + console.log(e); + } + } + + // Add items to Bloom Filter individually with BF.ADD command. + await Promise.all([ + client.bf.add('mybloom', 'leibale'), + client.bf.add('mybloom', 'simon'), + client.bf.add('mybloom', 'guy'), + client.bf.add('mybloom', 'suze'), + client.bf.add('mybloom', 'brian'), + client.bf.add('mybloom', 'steve'), + client.bf.add('mybloom', 'kyle'), + client.bf.add('mybloom', 'josefin'), + client.bf.add('mybloom', 'alex'), + client.bf.add('mybloom', 'nava'), + ]); + + // Add multiple items to Bloom Filter at once with BF.MADD command. + await client.bf.mAdd('mybloom', [ + 'kaitlyn', + 'rachel' + ]); + + console.log('Added members to Bloom Filter.'); + + // Check whether a member exists with the BF.EXISTS command. + const simonExists = await client.bf.exists('mybloom', 'simon'); + console.log(`simon ${simonExists ? 'may' : 'does not'} exist in the Bloom Filter.`); + + // Check whether multiple members exist with the BF.MEXISTS command: + const [ lanceExists, leibaleExists ] = await client.bf.mExists('mybloom', [ + 'lance', + 'leibale' + ]); + + console.log(`lance ${lanceExists ? 'may' : 'does not'} exist in the Bloom Filter.`); + console.log(`leibale ${leibaleExists ? 'may' : 'does not'} exist in the Bloom Filter.`); + + // Get stats for the Bloom Filter with the BF.INFO command: + const info = await client.bf.info('mybloom'); + // info looks like this: + // + // { + // capacity: 1000, + // size: 1531, + // numberOfFilters: 1, + // numberOfInsertedItems: 12, + // expansionRate: 2 + // } + console.log(info); + + await client.quit(); +} + +bloomFilter();