Skip to content

Commit

Permalink
getDistinctIndexes: implement #43 suggestions + allow to switch to XX…
Browse files Browse the repository at this point in the history
…H 32/64 if needed
  • Loading branch information
folkvir committed Dec 2, 2021
1 parent b3d6493 commit 1662d80
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 175 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build": "tsc",
"gts": "gts",
"pretest": "yarn build",
"test": "mocha --parallel test/**/*-test.js --trace-deprecation --timeout=30000",
"test": "mocha --parallel test/**/*-test.js --trace-deprecation --timeout=60000",
"doc": "typedoc --out docs/ --emit both --includeVersion src/api.ts",
"lint": "gts lint",
"clean": "gts clean",
Expand Down
5 changes: 2 additions & 3 deletions src/cuckoo/cuckoo-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {
*/
function computeFingerpintLength(size: number, rate: number): number {
const f = Math.ceil(Math.log2(1 / rate) + Math.log2(2 * size))
return Math.ceil(f / 8) // because we use base 16 64-bits hashes
return Math.ceil(f / 8) // because we use 64-bits hashes
}

/**
Expand Down Expand Up @@ -154,7 +154,6 @@ export default class CuckooFilter
): CuckooFilter {
const fl = computeFingerpintLength(bucketSize, errorRate)
const capacity = Math.ceil(size / bucketSize / 0.955)
// const capacity = utils.power2(items)
return new CuckooFilter(capacity, fl, bucketSize, maxKicks)
}

Expand Down Expand Up @@ -370,7 +369,7 @@ export default class CuckooFilter
* @private
*/
_locations(element: HashableInput) {
const hashes = hashIntAndString(element, this.seed, 16)
const hashes = hashIntAndString(element, this.seed)
const hash = hashes.int
if (this._fingerprintLength > hashes.string.length) {
throw new Error(
Expand Down
28 changes: 14 additions & 14 deletions src/iblt/invertible-bloom-lookup-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import BaseFilter from '../base-filter'
import WritableFilter from '../interfaces/writable-filter'
import Cell from './cell'
import {AutoExportable, Field, Parameter} from '../exportable'
import {allInOneHashTwice, allocateArray, getDistinctIndexes} from '../utils'
import {allocateArray, getDistinctIndexes, hashTwiceAsString} from '../utils'
import {optimalFilterSize, optimalHashes} from '../formulas'

/**
Expand Down Expand Up @@ -169,18 +169,18 @@ export default class InvertibleBloomFilter
* @param element - The element to insert
*/
add(element: Buffer): void {
const hashes = allInOneHashTwice(
const hashes = hashTwiceAsString(
JSON.stringify(element.toJSON()),
this.seed
)
const indexes = getDistinctIndexes(
hashes.string.first,
hashes.first,
this._size,
this._hashCount,
this.seed
)
for (let i = 0; i < this._hashCount; ++i) {
this._elements[indexes[i]].add(element, Buffer.from(hashes.string.first))
this._elements[indexes[i]].add(element, Buffer.from(hashes.first))
}
}

Expand All @@ -190,19 +190,19 @@ export default class InvertibleBloomFilter
* @return True if the element has been removed, False otheriwse
*/
remove(element: Buffer): boolean {
const hashes = allInOneHashTwice(
const hashes = hashTwiceAsString(
JSON.stringify(element.toJSON()),
this.seed
)
const indexes = getDistinctIndexes(
hashes.string.first,
this.size,
hashes.first,
this._size,
this._hashCount,
this.seed
)
for (let i = 0; i < this._hashCount; ++i) {
this._elements[indexes[i]] = this._elements[indexes[i]].xorm(
new Cell(Buffer.from(element), Buffer.from(hashes.string.first), 1)
new Cell(Buffer.from(element), Buffer.from(hashes.first), 1)
)
}
return true
Expand All @@ -214,13 +214,13 @@ export default class InvertibleBloomFilter
* @return False if the element is not in the filter, true if "may be" in the filter.
*/
has(element: Buffer): boolean {
const hashes = allInOneHashTwice(
const hashes = hashTwiceAsString(
JSON.stringify(element.toJSON()),
this.seed
)
const indexes = getDistinctIndexes(
hashes.string.first,
this.size,
hashes.first,
this._size,
this._hashCount,
this.seed
)
Expand Down Expand Up @@ -337,16 +337,16 @@ export default class InvertibleBloomFilter
} else {
throw new Error('Please report, not possible')
}
const hashes = allInOneHashTwice(JSON.stringify(id.toJSON()), this.seed)
const hashes = hashTwiceAsString(JSON.stringify(id.toJSON()), this.seed)
const indexes = getDistinctIndexes(
hashes.string.first,
hashes.first,
this._size,
this._hashCount,
this.seed
)
for (let i = 0; i < indexes.length; ++i) {
this._elements[indexes[i]] = this._elements[indexes[i]].xorm(
new Cell(id, Buffer.from(hashes.string.first), c)
new Cell(id, Buffer.from(hashes.first), c)
)
if (this._elements[indexes[i]].isPure()) {
pureList.push(indexes[i])
Expand Down
Loading

0 comments on commit 1662d80

Please sign in to comment.