Gathering detailed insights and metrics for blumea
Gathering detailed insights and metrics for blumea
npm install blumea
Typescript
Module System
Node Version
NPM Version
73.3
Supply Chain
99.4
Quality
75.4
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
2,603
Last Day
2
Last Week
23
Last Month
57
Last Year
306
6 Stars
177 Commits
1 Forks
6 Branches
3 Contributors
Minified
Minified + Gzipped
Latest Version
1.1.2
Package Id
blumea@1.1.2
Unpacked Size
55.92 kB
Size
11.71 kB
File Count
14
NPM Version
8.19.4
Node Version
16.20.2
Publised On
03 Nov 2023
Cumulative downloads
Total Downloads
Last day
100%
2
Compared to previous day
Last week
130%
23
Compared to previous week
Last month
103.6%
57
Compared to previous month
Last year
-76.6%
306
Compared to previous year
3
3
Blumea is a simple and efficient Bloom filter implementation for your applications. It's designed to help you determine whether an element belongs to a set in a space-efficient way.
A Bloom filter is a probabilistic data structure that allows you to test if an element is in a set. It does so by using a bit array and multiple hash functions to check if a given element is likely to be in the set or not. This makes Bloom filters ideal for use cases where space is at a premium, or when you need to test for set membership quickly.
One common example of where Bloom filters are useful is in checking the availability of usernames. Here, the set is the list of all registered usernames, and Bloom filters can be used to determine if a new username is already taken. The downside of Bloom filters is that they are probabilistic in nature and can sometimes give false positives (indicating that an element is in the set when it isn't).
npm i blumea
npm i blumea@latest --save
To start using Blumea in your JavaScript or TypeScript project, you need to first import it. This can be done using either of the following methods:
1 const blumea = require('blumea') 2 3 // import one or more bloom filters 4 const { BloomFilter } = require('blumea')
1 import * as blumea from 'blumea' 2 3 //// import one or more bloom filters 4 import { BloomFilter } from 'blumea' 5
-l
, -log
, or -blumea
.
node application.js -blumea
npm run <app-script> -log
Classical Bloom Filters are an extension of Bloom Filters that use a bit array and hash functions to represent the membership of an element in a set. They are probabilistic data structures used for membership testing with applications in web caching, spell checkers, virus scanners, Content Delivery Networks (CDN) and network routers.
To import the BloomFilter class from the Blumea package into your project, use the following:
1 const { BloomFilter } = require('blumea'); 2 3 /** 4 * Create a Bloom filter instance for your app. 5 * Provide item count and an acceptable false positive rate. 6 */ 7 8 let filter = new BloomFilter(9700, 0.01);
By passing the desired item count and false positive rate as arguments to the BloomFilter constructor, you can create a Bloom filter instance with the appropriate settings for your use case.
insert(element) : To add the element to the bloom filter instance.
find(element): To check for element membership with the false positive rate of the filter.
updateFalsePositiveRate(newFalsePostive): To update the filter instance with a new false positive rate.
updateItemCount(newItemCount): To update the filter instance with a new item count.
Utility Methods:
getHashFunctionCount() or filter.hash_count
getBitArraySize() or filter.size
Note: updateX methods() are experimental. Classical Bloom filters have static false positive rate and item count for a single instance.
Refer Note for more.
Sample Code Snipet:
1 const { BloomFilter } = require('blumea') 2 const { log } = require('log') 3 let filter = new BloomFilter(1024, 0.01) 4 5 6 filter.insert('James Clear') 7 filter.insert('Paulo Coelho') 8 9 log(filter.find('blumea')) // return false. 10 log(filter.find('Paulo Coelho')) // return true. 11 12 log(filter.getHashFunctionCount()) //return the optimal hash func count. 13 14 //Experimental: 15 const fpRate = 0.0; 16 const itemCount = 4096; 17 18 // throws warning & filter updates fp rate to 0.01. 19 filter.updateFalsePositiveRate(fpRate); 20 21 // itemCount updated, instance refreshes other parameters. 22 filter.updateItemCount(itemCount) 23 24 // return the new optimal count. 25 log(filter.getHashFunctionCount())
Counting Bloom Filters are a variant of Bloom Filters that allow adding and removing items efficiently. Rather than simply setting bits to indicate the presence of an item, Counting Bloom Filters keep a count of the number of times an item has been added. This allows the removal of items by decrementing their count, and also reduces the chance of false positives. Counting Bloom Filters are useful when items need to be added and removed from the filter, but at the cost of increased memory usage. Prime use cases include Network traffic management, Web caching and Transactional Fraud detection.
To import the BloomFilter class from the Blumea package into your project, use the following:
1 const { CountingBloomFilter } = require('blumea'); 2 3 /** 4 * Create a Bloom filter instance for your app. 5 * Provide item count and an acceptable false positive rate. 6 */ 7 8 let filter = new CountingBloomFilter(5999, 0.03);
insert(element) : To add the element to the bloom filter instance.
count_bit
array for each element in the filter.find(element): To check for element membership with the false positive rate of the filter.
getCount(element): To check for element membership and extract it's count. It returns the count if the element is found, 0
otherwise.
Utility Methods:
Refer Note for more.
Sample Code Snippet for Counting Bloom:
1 const { CountingBloomFilter } = require('blumea'); 2 const express = require('express'); 3 const app = express(); 4 5 let filter = new CountingBloomFilter(1000, 0.01); 6 7 app.post('/login', async (req, res) => { 8 try { 9 const { username, password } = req.body; 10 // Save NT bandwidth on db queries to validate username. 11 if (!filter.find(username)) { 12 // handle invalid username case 13 } 14 15 //Some Logic to verify credentials. 16 await processLogin({username, password}); 17 18 // save count with bloom filter. 19 filter(username, filter.getCount(username) + 1); 20 21 // Or simply use the insert method. 22 filter.insert(username); 23 24 } catch (error) { 25 // handle error 26 } 27 }) 28 // ...code
A Partitioned Bloom Filter (PBF) is a probabilistic data structure that allows efficient set membership testing. A PBF consists of multiple independent Bloom filters, each of which has a unique hash function. The Bloom filters are divided into partitions, and each partition is allocated a portion of the overall bit array used by the PBF. Each element is hashed by each of the unique hash functions, and the resulting hash values are used to set bits in the corresponding partitions of the Bloom filters.
To import the BloomFilter class from the Blumea package into your project, use the following:
1 const { PartitionedBloomFilter } = require('blumea'); 2 3 /** 4 * Create a Bloom filter instance for your app. 5 * Provide item count and an acceptable false positive rate. 6 */ 7 8 let filter = new PartitionedBloomFilter(5999, 0.03, 2);
insert(element) : This function inserts a new element into the bloom filter by setting the appropriate bits in each partition's bit set based on the hash values of the element.
find(element) : This function checks if a given element is present in the bloom filter by checking the appropriate bits in each partition's bit set based on the hash values of the element. It returns false if any of the bits are zero, indicating that the element is definitely not in the bloom filter, or true if all of the bits are one, indicating that the element may be in the bloom filter.
initializeBitSet() : Initializes the bit set for each partition of the bloom filter.
getPartitionIndex(element) : Calculates and returns the index of the partition that a given element should belong to.
getHashCountPerPartition() : Calculates and returns the number of hash functions to use in each partition of the bloom filter based on the size of each partition and the number of items in each partition.
getSizePerPartition() : Calculates and returns the size of each partition in the bloom filter based on the number of items in each partition and the desired false positive rate.
Utility Methods:
Refer Note for more.
Sample Node app with Partition Bloom Filter:
1 const { PartitionedBloomFilter } = require('blumea') 2 3 // Create a new Partitioned Bloom Filter with 100 items, 0.05 false positive rate, and 4 partitions. 4 const pbf = new PartitionedBloomFilter(100, 0.05, 4) 5 6 // Insert some elements 7 pbf.insert('foo') 8 pbf.insert('bar') 9 10 // Check if an element exists in the filter 11 console.log(pbf.find('foo')) // Returns true 12 console.log(pbf.find('baz')) // Returns false 13 14 // Check the number of hash functions used in each partition 15 console.log(pbf.getHashCountPerPartition())
A cuckoo bloom filter is a variation of a Bloom filter, which is a probabilistic data structure used for efficient set membership testing. Like a Bloom filter, a cuckoo bloom filter represents a set of items as a bit array, but it uses two hash functions and two hash tables instead of one. Cuckoo bloom filters offer better performance than standard Bloom filters for some use cases, but they are more complex to implement and require more memory. They are commonly used in network routers, firewalls, and other applications where fast and efficient set membership testing is required.
To import the BloomFilter class from the Blumea package into your project, use the following:
1 const { CuckooBloomFilter } = require('blumea'); 2 3 /** 4 * Create a Bloom filter instance for your app. 5 * Provide item count and an acceptable false positive rate. 6 */ 7 8 let filter = new CuckooBloomFilter(2999, 0.01);
insert(element) : : This function inserts the element into the filter by hashing it with multiple hash functions and placing it into one of two tables. If both tables are full, it attempts cuckoo hashing by swapping the current element with an existing one. If the element can't be inserted after a certain number of attempts, it returns false. Successful insertion returns true.
find(element) : This function checks if an element exists in the filter by hashing it with the same functions used during insertion and checking the corresponding slots in either table. If the element is found, it returns true. If not, it returns false.
Utility Methods:
Refer Note for more.
Sample Node app with Cuckoo Bloom Filter:
1 const { CuckooBloomFilter } = require('blumea') 2 3 // Create a new Cuckoo Bloom Filter with 100 items, 0.05 false positive rate. 4 const cf = new CuckooBloomFilter(100, 0.05) 5 6 // Insert some elements 7 cf.insert('apple') 8 cf.insert('banana') 9 cf.insert('strawberry') 10 11 // Check if an element exists in the filter 12 console.log(cf.contains('banana')) // Returns true 13 console.log(cf.contains('mango')) // Returns false 14 15 // Check the number of hash functions used in the filer 16 console.log(cf.getHashCount())
Scalable Bloom Filters are designed to be scalable and can handle an arbitrary number of items and adjust size dynamically while using multiple hash functions to generate a set of bits to identify the item's presence. They have real-life applications such as:
To import the BloomFilter class from the Blumea package into your project, use the following:
1 const { ScalableBloomFilter } = require('blumea'); 2 3 /** 4 * Create a Bloom filter instance for your app. 5 * Default values (itemCount: 1000, falsePostiveRate: 0.01) is used. 6 */ 7 8 let filter = new ScalableBloomFilter();
insert(element) : To add the element to the bloom filter instance.
Insertion checks for the available size and would automatically scale the filter by a factor of 2, as and when needed.
find(element): To check for element membership with the false positive rate of the filter.
About Custom Parameters:
1 new ScalableBloomFilter(
2 expectedItems?: number,
3 falsePositiveRate?: number,
4 hashFunctions?: hashFunction []
5 ): ScalableBloomFilter
1 hashingFunction: ((str: string, seed?: number | undefined) => number)
Refer Note for more.
Sample app to add Todos with a Scalable Bloom:
1 2 const { ScalableBloomFilter } = require('blumea'), 3 4 class TODO { 5 6 constructor () { 7 /* 8 * Initialize scalable bloom filter. 9 * Default config is for 1000 items at 0.01 fp rate. 10 * Filter auto-scales by a factor of 2 when needed. 11 */ 12 this.filter = new ScalableBloomFilter(); 13 } 14 15 /* 16 TODO: { 17 _id: uuid(), 18 title: string, 19 deadline: Date, 20 desc: string 21 } 22 */ 23 addTodo (todo) { 24 25 if (filter.find(todo._id)) { 26 // handle case. 27 } else { 28 29 /* 30 * Write todo to DB & update filter. 31 * code to insert to DB goes here. 32 */ 33 filter.insert(todo._id); //auto-scales during insert. 34 } 35 } 36 // add custom implementations 37 } 38
Please note that the false positive rate in a Bloom filter should not exceed 0.999. If this value is exceeded, an exception will be thrown.
The valid range for false positive rates is between 0.01 and 0.999.
MIT License - Github.com/Blumea
No vulnerabilities found.
No security vulnerabilities found.