Comparing version 0.2.2 to 0.2.3
33
bloem.js
"use strict" | ||
var BitBuffer = require('bitbuffer').BitBuffer | ||
var VNF = require('fnv').FNV | ||
var FNV = require('fnv').FNV | ||
@@ -22,6 +22,6 @@ function calculateSize(capacity, error_rate) { | ||
function fnv(seed, data) { | ||
var h = new VNF() | ||
var h = new FNV() | ||
h.update(seed) | ||
h.update(data) | ||
return h.value() | ||
return h.value() >>> 0 | ||
} | ||
@@ -59,2 +59,9 @@ var h1 = fnv(Buffer("S"), key) | ||
Bloem.destringify = function(data) { | ||
var filter = new Bloem(data.size, data.slices) | ||
filter.bitfield.buffer = new Buffer(data.bitfield.buffer) | ||
return filter | ||
} | ||
function SafeBloem(capacity, error_rate) { | ||
@@ -83,2 +90,9 @@ var size = calculateSize(capacity, error_rate) | ||
SafeBloem.destringify = function(data) { | ||
var filter = new SafeBloem(data.capacity, data.error_rate) | ||
filter.count = data.count | ||
filter.filter.bitfield.buffer = new Buffer(data.filter.bitfield.buffer) | ||
return filter | ||
} | ||
function ScalingBloem(error_rate, options) { | ||
@@ -113,2 +127,15 @@ var options = options || {} | ||
ScalingBloem.destringify = function(data) { | ||
var filter = new ScalingBloem(data.error_rate, { | ||
'ratio': data.ratio, | ||
'scaling': data.scaling, | ||
'initial_capacity': data.initial_capacity | ||
}) | ||
filter.filters = [] | ||
for(var i = 0; i < data.filters.length; i++) { | ||
filter.filters.push(SafeBloem.destringify(data.filters[i])) | ||
} | ||
return filter | ||
} | ||
exports.Bloem = Bloem | ||
@@ -115,0 +142,0 @@ exports.SafeBloem = SafeBloem |
{ | ||
"name": "bloem", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"author": "Sebastian Wiedenroth <wiedi@frubar.net>", | ||
@@ -5,0 +5,0 @@ "description": "Bloom Filter using the FNV hash function", |
66
test.js
@@ -7,3 +7,3 @@ "use strict" | ||
test('#empty', function(){ | ||
test('#empty', function() { | ||
var f = new bloem.Bloem(8, 2) | ||
@@ -13,3 +13,3 @@ assert.equal(f.has(Buffer("foo")), false) | ||
test('#add', function(){ | ||
test('#add', function() { | ||
var f = new bloem.Bloem(8, 2) | ||
@@ -22,5 +22,14 @@ assert.equal(f.has(Buffer("foo")), false) | ||
test('#destringify', function() { | ||
var f = new bloem.Bloem(8, 2) | ||
f.add(Buffer("foo")) | ||
f = bloem.Bloem.destringify(JSON.parse(JSON.stringify(f))) | ||
assert.equal(f.has(Buffer("foo")), true) | ||
assert.equal(f.has(Buffer("bar")), false) | ||
}) | ||
suite('SafeBloem') | ||
test('#empty', function(){ | ||
test('#empty', function() { | ||
var f = new bloem.SafeBloem(10, 0.01) | ||
@@ -30,5 +39,4 @@ assert.equal(f.has(Buffer("foo")), false) | ||
test('#add-and-stop', function(){ | ||
var f = new bloem.SafeBloem(10, 0.01) | ||
test('#add-and-stop', function() { | ||
var f = new bloem.SafeBloem(10, 0.02) | ||
for(var i = 0; i < 10; i++) { | ||
@@ -48,6 +56,22 @@ var b = Buffer(i.toString()) | ||
test('#destringify', function() { | ||
var f = new bloem.SafeBloem(3, 0.02) | ||
assert.equal(f.add("one"), true) | ||
assert.equal(f.add("two"), true) | ||
f = bloem.SafeBloem.destringify(JSON.parse(JSON.stringify(f))) | ||
assert.equal(f.has("one"), true) | ||
assert.equal(f.has("two"), true) | ||
assert.equal(f.has("three"), false) | ||
assert.equal(f.add("three"), true) | ||
assert.equal(f.has("three"), true) | ||
assert.equal(f.has("four"), false) | ||
assert.equal(f.add("four"), false) | ||
}) | ||
suite('ScalingBloem') | ||
test('#empty', function(){ | ||
test('#empty', function() { | ||
var f = new bloem.ScalingBloem(0.01) | ||
@@ -57,4 +81,4 @@ assert.equal(f.has(Buffer("foo")), false) | ||
test('#add-and-grow', function(){ | ||
var f = new bloem.ScalingBloem(0.001, {initial_capacity: 20}) | ||
test('#add-and-grow', function() { | ||
var f = new bloem.ScalingBloem(0.0005, {initial_capacity: 20}) | ||
for(var i = 0; i < 100; i++) { | ||
@@ -74,2 +98,22 @@ var b = Buffer(i.toString()) | ||
assert.equal(f.filters.length, 3) | ||
}) | ||
}) | ||
test('#destringify', function() { | ||
var f = new bloem.ScalingBloem(0.02, {initial_capacity: 2}) | ||
f.add("one") | ||
f.add("two") | ||
f.add("three") | ||
f = bloem.ScalingBloem.destringify(JSON.parse(JSON.stringify(f))) | ||
assert.equal(f.has("one"), true) | ||
assert.equal(f.has("two"), true) | ||
assert.equal(f.has("three"), true) | ||
assert.equal(f.has("four"), false) | ||
f.add("four") | ||
f.add("five") | ||
f.add("six") | ||
f.add("seven") | ||
assert.equal(f.has("seven"), true) | ||
assert.equal(f.filters.length, 3) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10738
219