@libp2p/utils 5.3.2 → 5.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/filters/bloom-filter.d.ts +34 -0
- package/dist/src/filters/bloom-filter.d.ts.map +1 -0
- package/dist/src/filters/bloom-filter.js +113 -0
- package/dist/src/filters/bloom-filter.js.map +1 -0
- package/dist/src/filters/bucket.d.ts +10 -0
- package/dist/src/filters/bucket.d.ts.map +1 -0
- package/dist/src/filters/bucket.js +53 -0
- package/dist/src/filters/bucket.js.map +1 -0
- package/dist/src/filters/cuckoo-filter.d.ts +41 -0
- package/dist/src/filters/cuckoo-filter.d.ts.map +1 -0
- package/dist/src/filters/cuckoo-filter.js +134 -0
- package/dist/src/filters/cuckoo-filter.js.map +1 -0
- package/dist/src/filters/fingerprint.d.ts +11 -0
- package/dist/src/filters/fingerprint.d.ts.map +1 -0
- package/dist/src/filters/fingerprint.js +34 -0
- package/dist/src/filters/fingerprint.js.map +1 -0
- package/dist/src/filters/hashes.d.ts +8 -0
- package/dist/src/filters/hashes.d.ts.map +1 -0
- package/dist/src/filters/hashes.js +29 -0
- package/dist/src/filters/hashes.js.map +1 -0
- package/dist/src/filters/index.d.ts +9 -0
- package/dist/src/filters/index.d.ts.map +1 -0
- package/dist/src/filters/index.js +4 -0
- package/dist/src/filters/index.js.map +1 -0
- package/dist/src/filters/scalable-cuckoo-filter.d.ts +24 -0
- package/dist/src/filters/scalable-cuckoo-filter.d.ts.map +1 -0
- package/dist/src/filters/scalable-cuckoo-filter.js +87 -0
- package/dist/src/filters/scalable-cuckoo-filter.js.map +1 -0
- package/dist/src/filters/utils.d.ts +2 -0
- package/dist/src/filters/utils.d.ts.map +1 -0
- package/dist/src/filters/utils.js +4 -0
- package/dist/src/filters/utils.js.map +1 -0
- package/package.json +15 -7
- package/src/filters/bloom-filter.ts +142 -0
- package/src/filters/bucket.ts +64 -0
- package/src/filters/cuckoo-filter.ts +197 -0
- package/src/filters/fingerprint.ts +44 -0
- package/src/filters/hashes.ts +38 -0
- package/src/filters/index.ts +9 -0
- package/src/filters/scalable-cuckoo-filter.ts +111 -0
- package/src/filters/utils.ts +3 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
2
|
+
import { CuckooFilter, optimize } from './cuckoo-filter.js';
|
|
3
|
+
import { fnv1a } from './hashes.js';
|
|
4
|
+
import { getRandomInt } from './utils.js';
|
|
5
|
+
export class ScalableCuckooFilter {
|
|
6
|
+
filterSize;
|
|
7
|
+
bucketSize;
|
|
8
|
+
fingerprintSize;
|
|
9
|
+
scale;
|
|
10
|
+
filterSeries;
|
|
11
|
+
hash;
|
|
12
|
+
seed;
|
|
13
|
+
constructor(init) {
|
|
14
|
+
this.bucketSize = init.bucketSize ?? 4;
|
|
15
|
+
this.filterSize = init.filterSize ?? (1 << 18) / this.bucketSize;
|
|
16
|
+
this.fingerprintSize = init.fingerprintSize ?? 2;
|
|
17
|
+
this.scale = init.scale ?? 2;
|
|
18
|
+
this.hash = init.hash ?? fnv1a;
|
|
19
|
+
this.seed = init.seed ?? getRandomInt(0, Math.pow(2, 10));
|
|
20
|
+
this.filterSeries = [
|
|
21
|
+
new CuckooFilter({
|
|
22
|
+
filterSize: this.filterSize,
|
|
23
|
+
bucketSize: this.bucketSize,
|
|
24
|
+
fingerprintSize: this.fingerprintSize,
|
|
25
|
+
hash: this.hash,
|
|
26
|
+
seed: this.seed
|
|
27
|
+
})
|
|
28
|
+
];
|
|
29
|
+
}
|
|
30
|
+
add(item) {
|
|
31
|
+
if (typeof item === 'string') {
|
|
32
|
+
item = uint8ArrayFromString(item);
|
|
33
|
+
}
|
|
34
|
+
if (this.has(item)) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
let current = this.filterSeries.find((cuckoo) => {
|
|
38
|
+
return cuckoo.reliable;
|
|
39
|
+
});
|
|
40
|
+
if (current == null) {
|
|
41
|
+
const curSize = this.filterSize * Math.pow(this.scale, this.filterSeries.length);
|
|
42
|
+
current = new CuckooFilter({
|
|
43
|
+
filterSize: curSize,
|
|
44
|
+
bucketSize: this.bucketSize,
|
|
45
|
+
fingerprintSize: this.fingerprintSize,
|
|
46
|
+
hash: this.hash,
|
|
47
|
+
seed: this.seed
|
|
48
|
+
});
|
|
49
|
+
this.filterSeries.push(current);
|
|
50
|
+
}
|
|
51
|
+
return current.add(item);
|
|
52
|
+
}
|
|
53
|
+
has(item) {
|
|
54
|
+
if (typeof item === 'string') {
|
|
55
|
+
item = uint8ArrayFromString(item);
|
|
56
|
+
}
|
|
57
|
+
for (let i = 0; i < this.filterSeries.length; i++) {
|
|
58
|
+
if (this.filterSeries[i].has(item)) {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
remove(item) {
|
|
65
|
+
if (typeof item === 'string') {
|
|
66
|
+
item = uint8ArrayFromString(item);
|
|
67
|
+
}
|
|
68
|
+
for (let i = 0; i < this.filterSeries.length; i++) {
|
|
69
|
+
if (this.filterSeries[i].remove(item)) {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
get count() {
|
|
76
|
+
return this.filterSeries.reduce((acc, curr) => {
|
|
77
|
+
return acc + curr.count;
|
|
78
|
+
}, 0);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
export function createScalableCuckooFilter(maxItems, errorRate = 0.001, options) {
|
|
82
|
+
return new ScalableCuckooFilter({
|
|
83
|
+
...optimize(maxItems, errorRate),
|
|
84
|
+
...(options ?? {})
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=scalable-cuckoo-filter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scalable-cuckoo-filter.js","sourceRoot":"","sources":["../../../src/filters/scalable-c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AG,CAAC,IAAI,CAAC,EAAE,CAAC;gBACnC,OAAO,IAAI,CAAA;YACb,CAAC;QACH,CAAC;QAED,OAAO,KAAK,CAAA;IACd,CAAC;IAED,MAAM,CAAE,IAAyB;QAC/B,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC7B,IAAI,GAAG,oBAAoB,CAAC,IAAI,CAAC,CAAA;QACnC,CAAC;QAED,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAClD,IAAI,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;gBACtC,OAAO,IAAI,CAAA;YACb,CAAC;QACH,CAAC;QAED,OAAO,KAAK,CAAA;IACd,CAAC;IAED,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE;YAC5C,OAAO,GAAG,GAAG,IAAI,CAAC,KAAK,CAAA;QACzB,CAAC,EAAE,CAAC,CAAC,CAAA;IACP,CAAC;CACF;AAED,MAAM,UAAU,0BAA0B,CAAE,QAAgB,EAAE,YAAoB,KAAK,EAAE,OAAmE;IAC1J,OAAO,IAAI,oBAAoB,CAAC;QAC9B,GAAG,QAAQ,CAAC,QAAQ,EAAE,SAAS,CAAC;QAChC,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC;KACnB,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/filters/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,YAAY,CAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAE9D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/filters/utils.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,YAAY,CAAE,GAAW,EAAE,GAAW;IACpD,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC,GAAG,GAAG,CAAA;AACtD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/utils",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.4.0",
|
|
4
4
|
"description": "Package to aggregate shared logic and dependencies for the libp2p ecosystem",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/utils#readme",
|
|
@@ -60,6 +60,10 @@
|
|
|
60
60
|
"types": "./dist/src/close-source.d.ts",
|
|
61
61
|
"import": "./dist/src/close-source.js"
|
|
62
62
|
},
|
|
63
|
+
"./filters": {
|
|
64
|
+
"types": "./dist/src/filters/index.d.ts",
|
|
65
|
+
"import": "./dist/src/filters/index.js"
|
|
66
|
+
},
|
|
63
67
|
"./ip-port-to-multiaddr": {
|
|
64
68
|
"types": "./dist/src/ip-port-to-multiaddr.d.ts",
|
|
65
69
|
"import": "./dist/src/ip-port-to-multiaddr.js"
|
|
@@ -128,23 +132,28 @@
|
|
|
128
132
|
},
|
|
129
133
|
"dependencies": {
|
|
130
134
|
"@chainsafe/is-ip": "^2.0.2",
|
|
131
|
-
"@libp2p/
|
|
132
|
-
"@libp2p/
|
|
135
|
+
"@libp2p/crypto": "^4.1.1",
|
|
136
|
+
"@libp2p/interface": "^1.3.1",
|
|
137
|
+
"@libp2p/logger": "^4.0.12",
|
|
133
138
|
"@multiformats/multiaddr": "^12.2.1",
|
|
134
139
|
"@multiformats/multiaddr-matcher": "^1.2.0",
|
|
140
|
+
"@sindresorhus/fnv1a": "^3.1.0",
|
|
141
|
+
"@types/murmurhash3js-revisited": "^3.0.3",
|
|
135
142
|
"delay": "^6.0.0",
|
|
136
143
|
"get-iterator": "^2.0.1",
|
|
137
144
|
"is-loopback-addr": "^2.0.2",
|
|
138
145
|
"it-pushable": "^3.2.3",
|
|
139
146
|
"it-stream-types": "^2.0.1",
|
|
147
|
+
"murmurhash3js-revisited": "^3.0.0",
|
|
140
148
|
"netmask": "^2.0.2",
|
|
141
149
|
"p-defer": "^4.0.1",
|
|
142
150
|
"race-event": "^1.2.0",
|
|
143
151
|
"race-signal": "^1.0.2",
|
|
144
|
-
"uint8arraylist": "^2.4.8"
|
|
152
|
+
"uint8arraylist": "^2.4.8",
|
|
153
|
+
"uint8arrays": "^5.0.3"
|
|
145
154
|
},
|
|
146
155
|
"devDependencies": {
|
|
147
|
-
"@libp2p/peer-id-factory": "^4.1.
|
|
156
|
+
"@libp2p/peer-id-factory": "^4.1.1",
|
|
148
157
|
"@types/netmask": "^2.0.5",
|
|
149
158
|
"aegir": "^42.2.5",
|
|
150
159
|
"delay": "^6.0.0",
|
|
@@ -153,8 +162,7 @@
|
|
|
153
162
|
"it-pair": "^2.0.6",
|
|
154
163
|
"it-pipe": "^3.0.1",
|
|
155
164
|
"sinon": "^17.0.1",
|
|
156
|
-
"sinon-ts": "^2.0.0"
|
|
157
|
-
"uint8arrays": "^5.0.3"
|
|
165
|
+
"sinon-ts": "^2.0.0"
|
|
158
166
|
},
|
|
159
167
|
"sideEffects": false
|
|
160
168
|
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// ported from xxbloom - https://github.com/ceejbot/xxbloom/blob/master/LICENSE
|
|
2
|
+
import { randomBytes } from '@libp2p/crypto'
|
|
3
|
+
import mur from 'murmurhash3js-revisited'
|
|
4
|
+
import { Uint8ArrayList } from 'uint8arraylist'
|
|
5
|
+
import { alloc } from 'uint8arrays/alloc'
|
|
6
|
+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
7
|
+
import type { Filter } from './index.js'
|
|
8
|
+
|
|
9
|
+
const LN2_SQUARED = Math.LN2 * Math.LN2
|
|
10
|
+
|
|
11
|
+
export interface BloomFilterOptions {
|
|
12
|
+
seeds?: number[]
|
|
13
|
+
hashes?: number
|
|
14
|
+
bits?: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class BloomFilter implements Filter {
|
|
18
|
+
public readonly seeds: number[]
|
|
19
|
+
public readonly bits: number
|
|
20
|
+
public buffer: Uint8Array
|
|
21
|
+
|
|
22
|
+
constructor (options: BloomFilterOptions = {}) {
|
|
23
|
+
if (options.seeds != null) {
|
|
24
|
+
this.seeds = options.seeds
|
|
25
|
+
} else {
|
|
26
|
+
this.seeds = generateSeeds(options.hashes ?? 8)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
this.bits = options.bits ?? 1024
|
|
30
|
+
this.buffer = alloc(Math.ceil(this.bits / 8))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Add an item to the filter
|
|
35
|
+
*/
|
|
36
|
+
add (item: Uint8Array | string): void {
|
|
37
|
+
if (typeof item === 'string') {
|
|
38
|
+
item = uint8ArrayFromString(item)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
for (let i = 0; i < this.seeds.length; i++) {
|
|
42
|
+
const hash = mur.x86.hash32(item, this.seeds[i])
|
|
43
|
+
const bit = hash % this.bits
|
|
44
|
+
|
|
45
|
+
this.setbit(bit)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Test if the filter has an item. If it returns false it definitely does not
|
|
51
|
+
* have the item. If it returns true, it probably has the item but there's
|
|
52
|
+
* an `errorRate` chance it doesn't.
|
|
53
|
+
*/
|
|
54
|
+
has (item: Uint8Array | string): boolean {
|
|
55
|
+
if (typeof item === 'string') {
|
|
56
|
+
item = uint8ArrayFromString(item)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
for (let i = 0; i < this.seeds.length; i++) {
|
|
60
|
+
const hash = mur.x86.hash32(item, this.seeds[i])
|
|
61
|
+
const bit = hash % this.bits
|
|
62
|
+
|
|
63
|
+
const isSet = this.getbit(bit)
|
|
64
|
+
|
|
65
|
+
if (!isSet) {
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return true
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Reset the filter
|
|
75
|
+
*/
|
|
76
|
+
clear (): void {
|
|
77
|
+
this.buffer.fill(0)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
setbit (bit: number): void {
|
|
81
|
+
let pos = 0
|
|
82
|
+
let shift = bit
|
|
83
|
+
while (shift > 7) {
|
|
84
|
+
pos++
|
|
85
|
+
shift -= 8
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let bitfield = this.buffer[pos]
|
|
89
|
+
bitfield |= (0x1 << shift)
|
|
90
|
+
this.buffer[pos] = bitfield
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
getbit (bit: number): boolean {
|
|
94
|
+
let pos = 0
|
|
95
|
+
let shift = bit
|
|
96
|
+
while (shift > 7) {
|
|
97
|
+
pos++
|
|
98
|
+
shift -= 8
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const bitfield = this.buffer[pos]
|
|
102
|
+
return (bitfield & (0x1 << shift)) !== 0
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Create a `BloomFilter` with the smallest `bits` and `hashes` value for the
|
|
108
|
+
* specified item count and error rate.
|
|
109
|
+
*/
|
|
110
|
+
export function createBloomFilter (itemcount: number, errorRate: number = 0.005): Filter {
|
|
111
|
+
const opts = optimize(itemcount, errorRate)
|
|
112
|
+
return new BloomFilter(opts)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function optimize (itemCount: number, errorRate: number = 0.005): { bits: number, hashes: number } {
|
|
116
|
+
const bits = Math.round(-1 * itemCount * Math.log(errorRate) / LN2_SQUARED)
|
|
117
|
+
const hashes = Math.round((bits / itemCount) * Math.LN2)
|
|
118
|
+
|
|
119
|
+
return { bits, hashes }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function generateSeeds (count: number): number[] {
|
|
123
|
+
let buf: Uint8ArrayList
|
|
124
|
+
let j: number
|
|
125
|
+
const seeds = []
|
|
126
|
+
|
|
127
|
+
for (let i = 0; i < count; i++) {
|
|
128
|
+
buf = new Uint8ArrayList(randomBytes(4))
|
|
129
|
+
seeds[i] = buf.getUint32(0, true)
|
|
130
|
+
|
|
131
|
+
// Make sure we don't end up with two identical seeds,
|
|
132
|
+
// which is unlikely but possible.
|
|
133
|
+
for (j = 0; j < i; j++) {
|
|
134
|
+
if (seeds[i] === seeds[j]) {
|
|
135
|
+
i--
|
|
136
|
+
break
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return seeds
|
|
142
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Fingerprint } from './fingerprint.js'
|
|
2
|
+
import { getRandomInt } from './utils.js'
|
|
3
|
+
|
|
4
|
+
export class Bucket {
|
|
5
|
+
private readonly contents: Array<Fingerprint | null>
|
|
6
|
+
|
|
7
|
+
constructor (size: number) {
|
|
8
|
+
this.contents = new Array(size).fill(null)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
has (fingerprint: Fingerprint): boolean {
|
|
12
|
+
if (!(fingerprint instanceof Fingerprint)) {
|
|
13
|
+
throw new TypeError('Invalid Fingerprint')
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return this.contents.some((fp) => {
|
|
17
|
+
return fingerprint.equals(fp)
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
add (fingerprint: Fingerprint): boolean {
|
|
22
|
+
if (!(fingerprint instanceof Fingerprint)) {
|
|
23
|
+
throw new TypeError('Invalid Fingerprint')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
for (let i = 0; i < this.contents.length; i++) {
|
|
27
|
+
if (this.contents[i] == null) {
|
|
28
|
+
this.contents[i] = fingerprint
|
|
29
|
+
return true
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return true
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
swap (fingerprint: Fingerprint): Fingerprint | null {
|
|
37
|
+
if (!(fingerprint instanceof Fingerprint)) {
|
|
38
|
+
throw new TypeError('Invalid Fingerprint')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const i = getRandomInt(0, this.contents.length - 1)
|
|
42
|
+
const current = this.contents[i]
|
|
43
|
+
this.contents[i] = fingerprint
|
|
44
|
+
|
|
45
|
+
return current
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
remove (fingerprint: Fingerprint): boolean {
|
|
49
|
+
if (!(fingerprint instanceof Fingerprint)) {
|
|
50
|
+
throw new TypeError('Invalid Fingerprint')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const found = this.contents.findIndex((fp) => {
|
|
54
|
+
return fingerprint.equals(fp)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
if (found > -1) {
|
|
58
|
+
this.contents[found] = null
|
|
59
|
+
return true
|
|
60
|
+
} else {
|
|
61
|
+
return false
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
2
|
+
import { Bucket } from './bucket.js'
|
|
3
|
+
import { Fingerprint, MAX_FINGERPRINT_SIZE } from './fingerprint.js'
|
|
4
|
+
import { fnv1a, type Hash } from './hashes.js'
|
|
5
|
+
import { getRandomInt } from './utils.js'
|
|
6
|
+
import type { Filter } from './index.js'
|
|
7
|
+
|
|
8
|
+
const maxCuckooCount = 500
|
|
9
|
+
|
|
10
|
+
export interface CuckooFilterInit {
|
|
11
|
+
/**
|
|
12
|
+
* How many items the filter is expected to contain
|
|
13
|
+
*/
|
|
14
|
+
filterSize: number
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* How many items to put in each bucket
|
|
18
|
+
*/
|
|
19
|
+
bucketSize?: number
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* How many bytes the fingerprint is expected to be
|
|
23
|
+
*/
|
|
24
|
+
fingerprintSize?: number
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A non-cryptographic hash implementation
|
|
28
|
+
*/
|
|
29
|
+
hash?: Hash
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A number used to seed the hash
|
|
33
|
+
*/
|
|
34
|
+
seed?: number
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class CuckooFilter implements Filter {
|
|
38
|
+
private readonly bucketSize: number
|
|
39
|
+
private readonly filterSize: number
|
|
40
|
+
private readonly fingerprintSize: number
|
|
41
|
+
private readonly buckets: Bucket[]
|
|
42
|
+
public count: number
|
|
43
|
+
private readonly hash: Hash
|
|
44
|
+
private readonly seed: number
|
|
45
|
+
|
|
46
|
+
constructor (init: CuckooFilterInit) {
|
|
47
|
+
this.filterSize = init.filterSize
|
|
48
|
+
this.bucketSize = init.bucketSize ?? 4
|
|
49
|
+
this.fingerprintSize = init.fingerprintSize ?? 2
|
|
50
|
+
this.count = 0
|
|
51
|
+
this.buckets = []
|
|
52
|
+
this.hash = init.hash ?? fnv1a
|
|
53
|
+
this.seed = init.seed ?? getRandomInt(0, Math.pow(2, 10))
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
add (item: Uint8Array | string): boolean {
|
|
57
|
+
if (typeof item === 'string') {
|
|
58
|
+
item = uint8ArrayFromString(item)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const fingerprint = new Fingerprint(item, this.hash, this.seed, this.fingerprintSize)
|
|
62
|
+
const j = this.hash.hash(item, this.seed) % this.filterSize
|
|
63
|
+
const k = (j ^ fingerprint.hash()) % this.filterSize
|
|
64
|
+
|
|
65
|
+
if (this.buckets[j] == null) {
|
|
66
|
+
this.buckets[j] = new Bucket(this.bucketSize)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (this.buckets[k] == null) {
|
|
70
|
+
this.buckets[k] = new Bucket(this.bucketSize)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (this.buckets[j].add(fingerprint) || this.buckets[k].add(fingerprint)) {
|
|
74
|
+
this.count++
|
|
75
|
+
return true
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const rand = [j, k]
|
|
79
|
+
let i = rand[getRandomInt(0, rand.length - 1)]
|
|
80
|
+
|
|
81
|
+
if (this.buckets[i] == null) {
|
|
82
|
+
this.buckets[i] = new Bucket(this.bucketSize)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
for (let n = 0; n < maxCuckooCount; n++) {
|
|
86
|
+
const swapped = this.buckets[i].swap(fingerprint)
|
|
87
|
+
|
|
88
|
+
if (swapped == null) {
|
|
89
|
+
continue
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
i = (i ^ swapped.hash()) % this.filterSize
|
|
93
|
+
|
|
94
|
+
if (this.buckets[i] == null) {
|
|
95
|
+
this.buckets[i] = new Bucket(this.bucketSize)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (this.buckets[i].add(swapped)) {
|
|
99
|
+
this.count++
|
|
100
|
+
|
|
101
|
+
return true
|
|
102
|
+
} else {
|
|
103
|
+
continue
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return false
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
has (item: Uint8Array | string): boolean {
|
|
111
|
+
if (typeof item === 'string') {
|
|
112
|
+
item = uint8ArrayFromString(item)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const fingerprint = new Fingerprint(item, this.hash, this.seed, this.fingerprintSize)
|
|
116
|
+
const j = this.hash.hash(item, this.seed) % this.filterSize
|
|
117
|
+
const inJ = this.buckets[j]?.has(fingerprint) ?? false
|
|
118
|
+
|
|
119
|
+
if (inJ) {
|
|
120
|
+
return inJ
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const k = (j ^ fingerprint.hash()) % this.filterSize
|
|
124
|
+
|
|
125
|
+
return this.buckets[k]?.has(fingerprint) ?? false
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
remove (item: Uint8Array | string): boolean {
|
|
129
|
+
if (typeof item === 'string') {
|
|
130
|
+
item = uint8ArrayFromString(item)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const fingerprint = new Fingerprint(item, this.hash, this.seed, this.fingerprintSize)
|
|
134
|
+
const j = this.hash.hash(item, this.seed) % this.filterSize
|
|
135
|
+
const inJ = this.buckets[j]?.remove(fingerprint) ?? false
|
|
136
|
+
|
|
137
|
+
if (inJ) {
|
|
138
|
+
this.count--
|
|
139
|
+
return inJ
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const k = (j ^ fingerprint.hash()) % this.filterSize
|
|
143
|
+
const inK = this.buckets[k]?.remove(fingerprint) ?? false
|
|
144
|
+
|
|
145
|
+
if (inK) {
|
|
146
|
+
this.count--
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return inK
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
get reliable (): boolean {
|
|
153
|
+
return Math.floor(100 * (this.count / this.filterSize)) <= 95
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// max load constants, defined in the cuckoo paper
|
|
158
|
+
const MAX_LOAD = {
|
|
159
|
+
1: 0.5,
|
|
160
|
+
2: 0.84,
|
|
161
|
+
4: 0.95,
|
|
162
|
+
8: 0.98
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function calculateBucketSize (errorRate: number = 0.001): 2 | 4 | 8 {
|
|
166
|
+
if (errorRate > 0.002) {
|
|
167
|
+
return 2
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (errorRate > 0.00001) {
|
|
171
|
+
return 4
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return 8
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function optimize (maxItems: number, errorRate: number = 0.001): CuckooFilterInit {
|
|
178
|
+
// https://www.eecs.harvard.edu/~michaelm/postscripts/cuckoo-conext2014.pdf
|
|
179
|
+
// Section 5.1 Optimal Bucket Size
|
|
180
|
+
const bucketSize = calculateBucketSize(errorRate)
|
|
181
|
+
const load = MAX_LOAD[bucketSize]
|
|
182
|
+
|
|
183
|
+
// https://stackoverflow.com/questions/57555236/how-to-size-a-cuckoo-filter/57617208#57617208
|
|
184
|
+
const filterSize = Math.round(maxItems / load)
|
|
185
|
+
const fingerprintSize = Math.min(Math.ceil(Math.log(filterSize / bucketSize)) + 2, MAX_FINGERPRINT_SIZE)
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
filterSize,
|
|
189
|
+
bucketSize,
|
|
190
|
+
fingerprintSize
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function createCuckooFilter (maxItems: number, errorRate: number = 0.005): Filter {
|
|
195
|
+
const opts = optimize(maxItems, errorRate)
|
|
196
|
+
return new CuckooFilter(opts)
|
|
197
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc'
|
|
2
|
+
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
3
|
+
import type { Hash } from './hashes'
|
|
4
|
+
|
|
5
|
+
export const MAX_FINGERPRINT_SIZE = 64
|
|
6
|
+
|
|
7
|
+
export class Fingerprint {
|
|
8
|
+
private readonly fp: Uint8Array
|
|
9
|
+
private readonly h: Hash
|
|
10
|
+
private readonly seed: number
|
|
11
|
+
|
|
12
|
+
constructor (buf: Uint8Array, hash: Hash, seed: number, fingerprintSize: number = 2) {
|
|
13
|
+
if (fingerprintSize > MAX_FINGERPRINT_SIZE) {
|
|
14
|
+
throw new TypeError('Invalid Fingerprint Size')
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const fnv = hash.hashV(buf, seed)
|
|
18
|
+
const fp = uint8ArrayAlloc(fingerprintSize)
|
|
19
|
+
|
|
20
|
+
for (let i = 0; i < fp.length; i++) {
|
|
21
|
+
fp[i] = fnv[i]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (fp.length === 0) {
|
|
25
|
+
fp[0] = 7
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
this.fp = fp
|
|
29
|
+
this.h = hash
|
|
30
|
+
this.seed = seed
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
hash (): number {
|
|
34
|
+
return this.h.hash(this.fp, this.seed)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
equals (other?: any): boolean {
|
|
38
|
+
if (!(other?.fp instanceof Uint8Array)) {
|
|
39
|
+
return false
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return uint8ArrayEquals(this.fp, other.fp)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import fnv1aHash from '@sindresorhus/fnv1a'
|
|
2
|
+
import mur from 'murmurhash3js-revisited'
|
|
3
|
+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
4
|
+
|
|
5
|
+
export interface Hash {
|
|
6
|
+
hash(input: Uint8Array, seed: number): number
|
|
7
|
+
hashV(input: Uint8Array, seed: number): Uint8Array
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const murmur3: Hash = {
|
|
11
|
+
hash: (input, seed) => {
|
|
12
|
+
return mur.x86.hash32(input, seed)
|
|
13
|
+
},
|
|
14
|
+
hashV: (input, seed) => {
|
|
15
|
+
return numberToBuffer(murmur3.hash(input, seed))
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const fnv1a: Hash = {
|
|
20
|
+
hash: (input) => {
|
|
21
|
+
return Number(fnv1aHash(input, {
|
|
22
|
+
size: 32
|
|
23
|
+
}))
|
|
24
|
+
},
|
|
25
|
+
hashV: (input, seed) => {
|
|
26
|
+
return numberToBuffer(fnv1a.hash(input, seed))
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function numberToBuffer (num: bigint | number): Uint8Array {
|
|
31
|
+
let hex = num.toString(16)
|
|
32
|
+
|
|
33
|
+
if (hex.length % 2 === 1) {
|
|
34
|
+
hex = `0${hex}`
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return uint8ArrayFromString(hex, 'base16')
|
|
38
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { BloomFilter, createBloomFilter, type BloomFilterOptions } from './bloom-filter.js'
|
|
2
|
+
export { CuckooFilter, createCuckooFilter, type CuckooFilterInit } from './cuckoo-filter.js'
|
|
3
|
+
export { ScalableCuckooFilter, createScalableCuckooFilter, type ScalableCuckooFilterInit } from './scalable-cuckoo-filter.js'
|
|
4
|
+
|
|
5
|
+
export interface Filter {
|
|
6
|
+
add(item: Uint8Array | string): void
|
|
7
|
+
has(item: Uint8Array | string): boolean
|
|
8
|
+
remove?(buf: Uint8Array | string): boolean
|
|
9
|
+
}
|