import / export BloomFilter not working as expected
imcotton opened this issue · 6 comments
Hi, these functionalities seemed have issue:
bloom-filters/src/bloom-filter.js
Lines 79 to 85 in 9dec78e
BloomFilter.create
should be the correct way to construct a filter since it calculates sizing internally, but forgot to save its_errorRate
for serialization later on
bloom-filters/src/export-import-specs.js
Lines 60 to 74 in 9dec78e
- because
_errorRate
has not been saved, it left asundefined
during exports, then got omitted viaJSON.stringify
, which later leading to throw an exception fromassertFields
byimport
_capacity
has not been set onto the instance nor serialization, but treat assize
to feed intoFilterConstructor
bloom-filters/src/bloom-filter.js
Lines 56 to 62 in 9dec78e
Thank you for sharing this. I will fix this tomorrow.
The latest version from the npm (0.8.0).
The proper way to create a bloom filter is using the current constructor if you want to customize the number of hash functions used or the size of the filter. But if you dont, use the static .create()
function. So I deleted the _errorRate property. The rate is computed/choosed when you construct the filter. Even if you export and import the filter you should have the same error rate between those 2 instances. If you want to add this property to your export json value call the .rate()
method then add it to your object.
I am able to reproduce the issue only by using a serialization step before importing the structure. Otherwise it works correctly.
let exported = filter.saveAsJSON()
// simulate serialization
exported = JSON.stringify(exported)
// simulate deserialization
exported = JSON.parse(exported)
const newFilter = BloomFilter.fromJSON(exported)
- If your a user of JSON.stringify for serialization, dont forget that it removes every undefined property.
Tempory Workaround: Just add _errorRate before importing the filter if you dont want to bump to the new version.
Fix: I removed the _errorRate from the export/import spec. It should work correctly now.