From 24c42bc25b84eac5abd13f37d76cf680d6b2b6aa Mon Sep 17 00:00:00 2001 From: newlife2002 Date: Tue, 9 Aug 2022 19:38:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81dump=E5=9B=9Etxt=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- binding/nodejs/index.js | 70 +++++++++++++++++++++++++++++++- binding/nodejs/tests/test.app.js | 5 ++- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/binding/nodejs/index.js b/binding/nodejs/index.js index 0ed4896..aa71638 100644 --- a/binding/nodejs/index.js +++ b/binding/nodejs/index.js @@ -56,9 +56,26 @@ class Searcher { } } - [openFilePromise] (fileName) { + async writeString(fd, str) { return new Promise((resolve, reject) => { - fs.open(fileName, 'r', (err, fd) => { + fs.write(fd, str, (err) => { + if (err) reject(err); + else resolve(err); + }) + }) + } + + async closeFilePromise(fd) { + return new Promise((resolve) => { + fs.close(fd, (err) => { + resolve(err); + }) + }) + } + + [openFilePromise] (fileName, op='r') { + return new Promise((resolve, reject) => { + fs.open(fileName, op, (err, fd) => { if (err) { reject(err) } else { @@ -68,6 +85,55 @@ class Searcher { }) } + async dumptxt(fileName) { + const startTime = process.hrtime(); + const ioStatus = { + ioCount: 0 + }; + + let fd = null + + if (!this._buffer) { + fd = await this[openFilePromise](this._dbFile) + } + + let fdOut = await this[openFilePromise](fileName, "w+"); + + const idx0 = 0; + const idx1 = 255 * VectorIndexCols * VectorIndexSize + 255 * VectorIndexSize; + const { sPtr:sPtr0, ePtr:_ePtr0 } = await this[getStartEndPtr](idx0, fd, ioStatus) + const { sPtr:_sPtr1, ePtr:ePtr1 } = await this[getStartEndPtr](idx1, fd, ioStatus) + + console.log(`${sPtr0}: ${ePtr1}`); + let p = sPtr0; + while (p < ePtr1) { + p += SegmentIndexSize; + const buff = await this[getBuffer](p, SegmentIndexSize, fd, ioStatus); + if (buff.length == SegmentIndexSize) { + const ip0 = buff.readUInt32LE(0); + const ip1 = buff.readUInt32LE(4); + const sip0 = this.Int2IP(ip0); + const sip1 = this.Int2IP(ip1); + const dataLen = buff.readUInt16LE(8); + const dataPtr = buff.readUInt32LE(10); + const data = await this[getBuffer](dataPtr, dataLen, fd, ioStatus); + const result = data.toString('utf-8'); + const line = `${sip0}|${sip1}|${result}\n`; + //console.log(line); + await this.writeString(fdOut, line); + } + } + await this.closeFilePromise(fdOut); + } + + Int2IP(i) { + let i0 = i >>> 24; + let i1 = (i >>> 16) % 256; + let i2 = (i >>> 8) % 256; + let i3 = i % 256; + return `${i0}.${i1}.${i2}.${i3}`; + } + async search (ip) { const startTime = process.hrtime() const ioStatus = { diff --git a/binding/nodejs/tests/test.app.js b/binding/nodejs/tests/test.app.js index 26abbd0..84d3f4b 100644 --- a/binding/nodejs/tests/test.app.js +++ b/binding/nodejs/tests/test.app.js @@ -59,12 +59,15 @@ const readlineSync = () => { const searcher = createSearcher() const main = async () => { - console.log('type \'quit\' to exit') + console.log('type \'quit\' to exit'); + let dmp; while (true) { process.stdout.write('ip2region>> ') const ip = (await readlineSync()).trim() if (ip === 'quit') { process.exit(0) + } else if(dmp = /dump (.*)/.exec(ip) ) { + await searcher.dumptxt(dmp[1]); } else { try { const response = await searcher.search(ip)