【发布时间】:2015-08-26 18:28:09
【问题描述】:
我在应用中使用布隆过滤器来扫描重复项:
var BloomFilter = require('bloom-filter');
var numberOfElements = 30000;
var falsePositiveRate = 0.005;
var filter = BloomFilter.create(numberOfElements, falsePositiveRate);
var workFunction = function(var1, var2) {
var bloomData = new Buffer(var1 + var2, "hex");
if(!filter.contains(bloomData)){
console.log("In the loop, adding the element to the bloom filter now!");
filter.insert(bloomData);
// work with the data and see if I found a needle in the haystack .. if yes, the bloom filter needs a reset !
if (var === "123") {
console.log("Needle found, resetting to fresh filter, new round !");
var filter = BloomFilter.create(numberOfElements, falsePositiveRate); // WRONG HERE but you get the idea!
}
} else {
console.log("This entry is already in the bloom filter !");
}
}
workFunction("1234", "1234");
经过一定数量的回合后,我想重置/清除/更新过滤器,正如您在上面的声明中看到的那样,但这当然会失败,因为那时它仍在使用中。
如何编写回调或类似的方法来重置过滤器?
谢谢
【问题讨论】:
-
如果您在回调中删除
var filter = ...之前的var,它应该可以工作,您正在重新定义一个新的本地范围变量filter
标签: javascript node.js bloom-filter