Skip to content

Commit c2e9f51

Browse files
committed
#49 add compatibility with 1.3.4
1 parent 062d889 commit c2e9f51

File tree

4 files changed

+124
-3
lines changed

4 files changed

+124
-3
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -544,11 +544,15 @@ In the case you want to use your own hash functions, you can override the `_seri
544544
```js
545545
const {BaseFilter, BloomFilter} = require('bloom-filters')
546546

547-
// override the method
548-
BaseFilter.prototype._serialize = function (element, seed=undefined) { return Number(1) }
547+
function _serialize(element, seed=undefined) { return Number(1) }
548+
549+
// override the method globaly for all classes
550+
BaseFilter.prototype._serialize = _serialize
549551

550552
// use the library as usual
551553
const bl = BloomFilter.create(2, 0.01)
554+
// override just your structure locally
555+
bl._serialize = _serialize
552556
bl.add('a')
553557
```
554558
See `test/utils-test.js` "_Use different hash functions_" describe close.

src/base-filter.ts

+74
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,78 @@ export default abstract class BaseFilter {
299299
const hash = this._hashAsInt(elem, seed)
300300
return {int: hash, string: numberToHex(hash)}
301301
}
302+
303+
/**
304+
* @deprecated
305+
* Generate a set of distinct indexes on interval [0, size) using the double hashing technique
306+
* This function is the old method called by a lot of filters.
307+
* To work in the current version, replace, the getIndexes function of the filters by this one
308+
* @param element - The element to hash
309+
* @param size - the range on which we can generate an index [0, size) = size
310+
* @param number - The number of indexes desired
311+
* @param seed - The seed used
312+
* @return A array of indexes
313+
* @author Arnaud Grall
314+
*/
315+
protected _deprecated_getDistinctIndices(
316+
element: HashableInput,
317+
size: number,
318+
number: number,
319+
seed?: number
320+
): Array<number> {
321+
if (seed === undefined) {
322+
seed = getDefaultSeed()
323+
}
324+
325+
// old and deprecated double hashing used until version 1.3.9
326+
const _deprecated_double_hashing = (
327+
n: number,
328+
hashA: number,
329+
hashB: number,
330+
size: number
331+
): number => {
332+
return Math.abs((hashA + n * hashB) % size)
333+
}
334+
335+
const _deprecated_getDistinctIndicesBis = (
336+
n: number,
337+
elem: HashableInput,
338+
size: number,
339+
count: number,
340+
indexes: Array<number> = []
341+
): Array<number> => {
342+
if (indexes.length === count) {
343+
return indexes
344+
} else {
345+
const hashes = this._hashTwice(elem, seed! + (size % n))
346+
const ind = _deprecated_double_hashing(
347+
n,
348+
hashes.first,
349+
hashes.second,
350+
size
351+
)
352+
if (indexes.includes(ind)) {
353+
// console.log('generate index: %d for %s', ind, elem)
354+
return _deprecated_getDistinctIndicesBis(
355+
n + 1,
356+
elem,
357+
size,
358+
count,
359+
indexes
360+
)
361+
} else {
362+
// console.log('already found: %d for %s', ind, elem)
363+
indexes.push(ind)
364+
return _deprecated_getDistinctIndicesBis(
365+
n + 1,
366+
elem,
367+
size,
368+
count,
369+
indexes
370+
)
371+
}
372+
}
373+
}
374+
return _deprecated_getDistinctIndicesBis(1, element, size, number)
375+
}
302376
}

test/compatibility-test.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2022 Arnaud Grall
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
'use strict'
26+
27+
require('chai').should()
28+
const {BloomFilter} = require('../dist/api.js')
29+
30+
describe('Compatibility between versions', () => {
31+
it('1.3.4 compatibility (issue #49)', () => {
32+
const bl = BloomFilter.create(2, 0.01)
33+
bl._getIndexes = bl._deprecated_getDistinctIndices
34+
bl.add('alice')
35+
bl.add('bob')
36+
bl.has('alice').should.be.true
37+
bl.has('bob').should.be.true
38+
const exported = bl.saveAsJSON()
39+
console.log(exported)
40+
})
41+
})

test/utils-test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,14 @@ describe('Utils', () => {
170170

171171
describe('Use different hash functions', () => {
172172
it('overriding serialize function by always returning Number(1)', () => {
173-
BaseFilter.prototype._serialize = function (_element, _seed = undefined) { // eslint-disable-line
173+
function _serialize(_element, _seed = undefined) { // eslint-disable-line
174174
return Number(1)
175175
}
176176
const bl = BloomFilter.create(2, 0.01)
177+
bl._serialize = _serialize
177178
bl.add('a')
178179
const bl2 = BloomFilter.create(2, 0.01)
180+
bl2._serialize = _serialize
179181
bl2.add('b')
180182
// 2 bloom filters with a hash functions returning everytime the same thing must be equal
181183
bl.equals(bl2).should.be.true

0 commit comments

Comments
 (0)