This commit is contained in:
parent
90cb936a18
commit
00c9d2dbcb
|
|
@ -4,10 +4,84 @@
|
||||||
|
|
||||||
### 完全基于文件的查询
|
### 完全基于文件的查询
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
// 导入包
|
||||||
|
const Searcher = require('.')
|
||||||
|
// 指定ip2region数据文件路径
|
||||||
|
const dbPath = 'ip2region.xdb file path'
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 创建searcher对象
|
||||||
|
const searcher = Searcher.newWithFileOnly(dbPath)
|
||||||
|
// 查询
|
||||||
|
const data = await searcher.search('218.4.167.70')
|
||||||
|
// data: '中国|0|江苏省|苏州市|电信'
|
||||||
|
} catch(e) {
|
||||||
|
console.log(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
### 缓存 `VectorIndex` 索引
|
### 缓存 `VectorIndex` 索引
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
// 导入包
|
||||||
|
const Searcher = require('.')
|
||||||
|
// 指定ip2region数据文件路径
|
||||||
|
const dbPath = 'ip2region.xdb file path'
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 同步读取vectorIndex
|
||||||
|
const vectorIndex = Searcher.loadVectorIndexFromFile(dbPath)
|
||||||
|
// 创建searcher对象
|
||||||
|
const searcher = Searcher.newWithVectorIndex(dbPath, vectorIndex)
|
||||||
|
// 查询 await 或 promise均可
|
||||||
|
const data = await searcher.search('218.4.167.70')
|
||||||
|
// data: '中国|0|江苏省|苏州市|电信'
|
||||||
|
} catch(e) {
|
||||||
|
console.log(e)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### 缓存整个 `xdb` 数据
|
### 缓存整个 `xdb` 数据
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
// 导入包
|
||||||
|
const Searcher = require('.')
|
||||||
|
// 指定ip2region数据文件路径
|
||||||
|
const dbPath = 'ip2region.xdb file path'
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 同步读取buffer
|
||||||
|
const buffer = Searcher.loadContentFromFile(dbPath)
|
||||||
|
// 创建searcher对象
|
||||||
|
const searcher = Searcher.newWithVectorIndex(buffer)
|
||||||
|
// 查询 await 或 promise均可
|
||||||
|
const data = await searcher.search('218.4.167.70')
|
||||||
|
// data: '中国|0|江苏省|苏州市|电信'
|
||||||
|
} catch(e) {
|
||||||
|
console.log(e)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## 查询测试
|
## 查询测试
|
||||||
|
|
||||||
## bench 测试
|
## bench 测试
|
||||||
|
|
||||||
|
## 单元测试结果
|
||||||
|
|
||||||
|
```shell
|
||||||
|
ip2region
|
||||||
|
✔ #newWithFileOnly
|
||||||
|
✔ #newWithVectorIndex
|
||||||
|
✔ #newWithBuffer
|
||||||
|
|
||||||
|
3 passing (10ms)
|
||||||
|
|
||||||
|
----------|---------|----------|---------|---------|----------------------------------
|
||||||
|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
|
||||||
|
----------|---------|----------|---------|---------|----------------------------------
|
||||||
|
All files | 91.17 | 60.71 | 100 | 91.17 |
|
||||||
|
index.js | 91.17 | 60.71 | 100 | 91.17 | 56,70,80,137,143,173,179,193,201
|
||||||
|
----------|---------|----------|---------|---------|----------------------------------
|
||||||
|
```
|
||||||
|
|
|
||||||
|
|
@ -157,9 +157,11 @@ class Searcher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 异步关闭文件,使用同步关闭的话,会极大影响性能
|
||||||
|
// 超高并发下,由于是异步close,会导致too many open files错误
|
||||||
|
// 建议使用完全缓存xdb文件模式
|
||||||
if (fd) {
|
if (fd) {
|
||||||
// 这边直接关闭,不需要等待
|
fs.close(fd)
|
||||||
fs.close(fd, () => {})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"name": "ip2region",
|
"name": " node-ip2region",
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"description": "official nodejs client of ip2region",
|
"description": "official nodejs client of ip2region",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
|
|
|
||||||
|
|
@ -1,49 +0,0 @@
|
||||||
const path = require('path')
|
|
||||||
const Searcher = require('.')
|
|
||||||
|
|
||||||
const dbPath = path.join(__dirname, '..', '..', '..', 'data', 'ip2region.xdb')
|
|
||||||
const buffer = Searcher.loadContentFromFile(dbPath)
|
|
||||||
const searcher1 = Searcher.newWithBuffer(buffer)
|
|
||||||
|
|
||||||
const vectorIndex = Searcher.loadVectorIndexFromFile(dbPath)
|
|
||||||
const searcher2 = Searcher.newWithVectorIndex(dbPath, vectorIndex)
|
|
||||||
|
|
||||||
const searcher3 = Searcher.newWithFileOnly(dbPath)
|
|
||||||
|
|
||||||
;(async () => {
|
|
||||||
const data1 = await Promise
|
|
||||||
.all([
|
|
||||||
searcher1.search('202.97.77.50'),
|
|
||||||
searcher1.search('218.4.167.70'),
|
|
||||||
searcher1.search('112.31.187.151'),
|
|
||||||
searcher1.search('202.98.17.164'),
|
|
||||||
searcher1.search('170.148.132.136'),
|
|
||||||
searcher1.search('194.138.202.210')
|
|
||||||
])
|
|
||||||
|
|
||||||
console.log(data1)
|
|
||||||
|
|
||||||
const data2 = await Promise
|
|
||||||
.all([
|
|
||||||
searcher2.search('202.97.77.50'),
|
|
||||||
searcher2.search('218.4.167.70'),
|
|
||||||
searcher2.search('112.31.187.151'),
|
|
||||||
searcher2.search('202.98.17.164'),
|
|
||||||
searcher2.search('170.148.132.136'),
|
|
||||||
searcher2.search('194.138.202.210')
|
|
||||||
])
|
|
||||||
|
|
||||||
console.log(data2)
|
|
||||||
|
|
||||||
const data3 = await Promise
|
|
||||||
.all([
|
|
||||||
searcher3.search('202.97.77.50'),
|
|
||||||
searcher3.search('218.4.167.70'),
|
|
||||||
searcher3.search('112.31.187.151'),
|
|
||||||
searcher3.search('202.98.17.164'),
|
|
||||||
searcher3.search('170.148.132.136'),
|
|
||||||
searcher3.search('194.138.202.210')
|
|
||||||
])
|
|
||||||
|
|
||||||
console.log(data3)
|
|
||||||
})()
|
|
||||||
|
|
@ -4,17 +4,26 @@ const Searcher = require('..')
|
||||||
|
|
||||||
const dbPath = path.join(__dirname, '..', '..', '..', 'data', 'ip2region.xdb')
|
const dbPath = path.join(__dirname, '..', '..', '..', 'data', 'ip2region.xdb')
|
||||||
const buffer = Searcher.loadContentFromFile(dbPath)
|
const buffer = Searcher.loadContentFromFile(dbPath)
|
||||||
const seacher = Searcher.newWithBuffer(buffer)
|
const searcher1 = Searcher.newWithBuffer(buffer)
|
||||||
|
|
||||||
|
const vectorIndex = Searcher.loadVectorIndexFromFile(dbPath)
|
||||||
|
const searcher2 = Searcher.newWithVectorIndex(dbPath, vectorIndex)
|
||||||
|
|
||||||
|
const searcher3 = Searcher.newWithFileOnly(dbPath)
|
||||||
|
|
||||||
const suite = new Benchmark.Suite()
|
const suite = new Benchmark.Suite()
|
||||||
suite
|
suite
|
||||||
.add('#search - 1', async () => {
|
.add('#缓存整个xdb数据【搜索218.4.167.70】', async () => {
|
||||||
const ip = '202.97.77.50'
|
|
||||||
return seacher.search(ip)
|
|
||||||
})
|
|
||||||
.add('#search - 2', async () => {
|
|
||||||
const ip = '218.4.167.70'
|
const ip = '218.4.167.70'
|
||||||
return seacher.search(ip)
|
return searcher1.search(ip)
|
||||||
|
})
|
||||||
|
.add('#缓存VectorIndex索引【搜索218.4.167.70】', async () => {
|
||||||
|
const ip = '218.4.167.70'
|
||||||
|
return searcher2.search(ip)
|
||||||
|
})
|
||||||
|
.add('#完全基于文件的查询【搜索218.4.167.70】', async () => {
|
||||||
|
const ip = '218.4.167.70'
|
||||||
|
return searcher3.search(ip)
|
||||||
})
|
})
|
||||||
.on('cycle', function (event) {
|
.on('cycle', function (event) {
|
||||||
console.log(String(event.target)) // eslint-disable-line
|
console.log(String(event.target)) // eslint-disable-line
|
||||||
|
|
|
||||||
|
|
@ -12,17 +12,17 @@ const searcher2 = Searcher.newWithVectorIndex(dbPath, vectorIndex)
|
||||||
const searcher3 = Searcher.newWithFileOnly(dbPath)
|
const searcher3 = Searcher.newWithFileOnly(dbPath)
|
||||||
|
|
||||||
describe('ip2region', () => {
|
describe('ip2region', () => {
|
||||||
it('#newWithFileOnly', async () => {
|
it('#newWithFileOnly and search', async () => {
|
||||||
const d = await searcher3.search('218.4.167.70')
|
const d = await searcher3.search('218.4.167.70')
|
||||||
expect(d).equal('中国|0|江苏省|苏州市|电信')
|
expect(d).equal('中国|0|江苏省|苏州市|电信')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('#newWithVectorIndex', async () => {
|
it('#newWithVectorIndex and search', async () => {
|
||||||
const d = await searcher2.search('218.4.167.70')
|
const d = await searcher2.search('218.4.167.70')
|
||||||
expect(d).equal('中国|0|江苏省|苏州市|电信')
|
expect(d).equal('中国|0|江苏省|苏州市|电信')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('#newWithBuffer', async () => {
|
it('#newWithBuffer and search', async () => {
|
||||||
const d = await searcher1.search('218.4.167.70')
|
const d = await searcher1.search('218.4.167.70')
|
||||||
expect(d).equal('中国|0|江苏省|苏州市|电信')
|
expect(d).equal('中国|0|江苏省|苏州市|电信')
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue