|
| 1 | +// This example demonstrates the use of the Cuckoo Filter |
| 2 | +// in the RedisBloom module (https://redisbloom.io/) |
| 3 | + |
| 4 | +import { createClient } from 'redis'; |
| 5 | + |
| 6 | +async function cuckooFilter() { |
| 7 | + const client = createClient(); |
| 8 | + |
| 9 | + await client.connect(); |
| 10 | + |
| 11 | + // Delete any pre-existing Cuckoo Filter. |
| 12 | + await client.del('mycuckoo'); |
| 13 | + |
| 14 | + // Reserve a Cuckoo Filter with a capacity of 10000 items. |
| 15 | + // https://oss.redis.com/redisbloom/Cuckoo_Commands/#cfreserve |
| 16 | + try { |
| 17 | + await client.cf.reserve('mycuckoo', 10000); |
| 18 | + console.log('Reserved Cuckoo Filter.'); |
| 19 | + } catch (e) { |
| 20 | + console.log('Error, maybe RedisBloom is not installed?:'); |
| 21 | + console.log(e); |
| 22 | + } |
| 23 | + |
| 24 | + // Add items to Cuckoo Filter individually with CF.ADD command. |
| 25 | + https://oss.redis.com/redisbloom/Cuckoo_Commands/#cfadd |
| 26 | + await Promise.all([ |
| 27 | + client.cf.add('mycuckoo', 'leibale'), |
| 28 | + client.cf.add('mycuckoo', 'simon'), |
| 29 | + client.cf.add('mycuckoo', 'guy'), |
| 30 | + client.cf.add('mycuckoo', 'suze'), |
| 31 | + client.cf.add('mycuckoo', 'brian'), |
| 32 | + client.cf.add('mycuckoo', 'steve'), |
| 33 | + client.cf.add('mycuckoo', 'kyle'), |
| 34 | + client.cf.add('mycuckoo', 'josefin'), |
| 35 | + client.cf.add('mycuckoo', 'alex'), |
| 36 | + client.cf.add('mycuckoo', 'nava'), |
| 37 | + ]); |
| 38 | + |
| 39 | + // Add items to the Cuckoo Filter only if they don't exist in it... |
| 40 | + // https://oss.redis.com/redisbloom/Cuckoo_Commands/#cfaddnx |
| 41 | + const nxReply = await Promise.all([ |
| 42 | + client.cf.addNX('mycuckoo', 'kaitlyn'), // New |
| 43 | + client.cf.addNX('mycuckoo', 'rachel'), // New |
| 44 | + client.cf.addNX('mycuckoo', 'brian') // Previously added |
| 45 | + ]); |
| 46 | + |
| 47 | + console.log('Added members to Cuckoo Filter.'); |
| 48 | + console.log('nxReply:'); |
| 49 | + |
| 50 | + // nxReply looks like this: |
| 51 | + // [ |
| 52 | + // true, |
| 53 | + // true, |
| 54 | + // false |
| 55 | + // ] |
| 56 | + console.log(nxReply); |
| 57 | + |
| 58 | + // Check whether a member exists with the CF.EXISTS command. |
| 59 | + const simonExists = await client.bf.exists('mycuckoo', 'simon'); |
| 60 | + console.log(`simon ${simonExists ? 'may' : 'does not'} exist in the Cuckoo Filter.`); |
| 61 | + |
| 62 | + // Get stats for the Cuckoo Filter with the CF.INFO command: |
| 63 | + const info = await client.cf.info('mycuckoo'); |
| 64 | + |
| 65 | + // info looks like this: |
| 66 | + // { |
| 67 | + // size: 16440, |
| 68 | + // numberOfBuckets: 8192, |
| 69 | + // numberOfFilters: 1, |
| 70 | + // numberOfInsertedItems: 12, |
| 71 | + // numberOfDeletedItems: 0, |
| 72 | + // bucketSize: 2, |
| 73 | + // expansionRate: 1, |
| 74 | + // maxIteration: 20 |
| 75 | + // } |
| 76 | + console.log(info); |
| 77 | + |
| 78 | + await client.quit(); |
| 79 | +} |
| 80 | + |
| 81 | +cuckooFilter(); |
0 commit comments