Hi guys,I am doing little practice. Where I have BloomFilterSerilizable class that has load() and save() methods. The load() will read a local file and should change the class property "this.bitMap" to whatever it reads. But the load() function dosen't do anything. Below is the code:1.the readfile method will read the data and I can console log the obj in the callback 2.This class inherit BloomFilter that will initiate a new bitMap as its propertyTestFile: ``` const { BlommFilterSerilizable } = require("./bloomFilterSerilization");let bfs = new BlommFilterSerilizable(12, "test.bin");bfs.load();console.log(bfs); //just constructor initiation ```BlommFilterSerilizable ``` class BlommFilterSerilizable extends BloomFilter { constructor(nbits, fileName) { super(nbits); this.fileName = fileName; }load() { const sourcePath = ${CONSTANTS.cwd}${CONSTANTS.filterPath}/${this.fileName}; var _this = this;fs.readFile(sourcePath, (err, data) => { if (err) { throw err; } _this.bitMap = JSON.parse(data); }); }save() { const sourcePath = ${CONSTANTS.cwd}${CONSTANTS.filterPath}/${this.fileName};fs.writeFile(sourcePath, JSON.stringify(this.bitMap), err => { if (err) { throw err; } console.log("file saved"); }); } } BloomFilter class BloomFilter { constructor(nbits) { this.bitMap = new Bitmap(nbits); }generateCodes(input) { return [ murmurhash.v3(input, RANDOMSEEDS.seed1), murmurhash.v3(input, RANDOMSEEDS.seed2), murmurhash.v3(input, RANDOMSEEDS.seed3) ]; }set(urlString) { if (!urlString) { throw Error("Input cannot be empty"); }const codes = this.generateCodes(urlString); for (let code of codes) { this.bitMap.set(code); } }get(urlString) { const codes = this.generateCodes(urlString);for (let code of codes) { if (!this.bitMap.get(code)) { return false; } } return true; } }BitMap:class Bitmap { constructor(nbits) { this.nbits = Math.pow(2, nbits); this.map = new Array(takeInt(nbits / CONST.BITNUMBER + 1)).fill(""); }_preprocessing(k) { k = k > this.nbits ? absolute(k) % this.nbits : absolute(k); return [takeInt(k / CONST.BITNUMBER), k % CONST.BITNUMBER]; }set(k) { const [byteIndex, bitIndex] = this._preprocessing(k); this.map[byteIndex] |= 1 << bitIndex; }get(k) { const [byteIndex, bitIndex] = this._preprocessing(k); return (this.map[byteIndex] & (1 << bitIndex)) != 0; } } ```
Submitted February 14, 2020 at 11:08AM by GreatFireWallSucks
No comments:
Post a Comment