支持dump回txt文件

This commit is contained in:
newlife2002 2022-08-09 19:38:04 +08:00
parent 65edd65bcf
commit 24c42bc25b
2 changed files with 72 additions and 3 deletions

View File

@ -56,9 +56,26 @@ class Searcher {
} }
} }
[openFilePromise] (fileName) { async writeString(fd, str) {
return new Promise((resolve, reject) => { 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) { if (err) {
reject(err) reject(err)
} else { } 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) { async search (ip) {
const startTime = process.hrtime() const startTime = process.hrtime()
const ioStatus = { const ioStatus = {

View File

@ -59,12 +59,15 @@ const readlineSync = () => {
const searcher = createSearcher() const searcher = createSearcher()
const main = async () => { const main = async () => {
console.log('type \'quit\' to exit') console.log('type \'quit\' to exit');
let dmp;
while (true) { while (true) {
process.stdout.write('ip2region>> ') process.stdout.write('ip2region>> ')
const ip = (await readlineSync()).trim() const ip = (await readlineSync()).trim()
if (ip === 'quit') { if (ip === 'quit') {
process.exit(0) process.exit(0)
} else if(dmp = /dump (.*)/.exec(ip) ) {
await searcher.dumptxt(dmp[1]);
} else { } else {
try { try {
const response = await searcher.search(ip) const response = await searcher.search(ip)