Merge pull request #5 from lionsoul2014/master

merge from remote
This commit is contained in:
Rocher 2022-05-02 14:16:47 +08:00 committed by GitHub
commit dcac4ae358
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
100 changed files with 576927 additions and 140112 deletions

7
.gitignore vendored
View File

@ -3,6 +3,9 @@
*.o
*.pyc
*~
*.log
*.la
*.so
META-INF/
# Binary Files #
@ -37,3 +40,7 @@ target
/binding/c#/**/packages
/binding/c#/**/bin
/binding/c#/**/obj
# Nodejs
/binding/nodejs/tests/unitTests/__snapshots__
/binding/nodejs/coverage

View File

@ -1,3 +1,26 @@
### 1.8
1. 数据升级至2018/12/31的版本国外的也增加了大量的城市数据。
2. Java的binding的Searcher类增加了如下的接口和实现
```java
DbSearcher(DbConfig dbConfig, string dbBinStr);
```
maven坐标
```xml
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>1.7.2</version>
</dependency>
```
3. Javascript的pacakge上传到了npm。
4. 增加纯lua实现的lua binding (lua 5.2版本依赖bit模块对5.3的支持将使用lua自带的bit操作)。
5. 增加C实现的lua_c binding (和c的性能等同建议使用)。
6. 优化部分binding的demo运行和使用文档。
7. 修复python binding的benchmark的部分错误。
### 1.2.4
1, 花了近两周的时间重写了数据的升级算法,再次提高准确率,升级过程如下(此处应该有掌声):

181
README.md
View File

@ -1,76 +1,81 @@
ip2region - 最自由的ip地址查询库ip到地区的映射库提供Binary,B树和纯内存三种查询算法妈妈再也不用担心我的ip地址定位。
# Ip2region是什么
### 1. 99.9%准确率,定时更新:
ip2region - 准确率99.9%的离线IP地址定位库0.0x毫秒级查询ip2region.db数据库只有数MB提供了java,php,c,python,nodejs,golang,c#等查询绑定和Binary,B树,内存三种查询算法。
数据聚合了一些知名ip到地名查询提供商的数据这些是他们官方的的准确率经测试着实比纯真啥的准确多了。<br />
每次聚合一下数据需要1-2天会不定时更新。
### 2. 标准化的数据格式:
每条ip数据段都固定了格式_城市Id|国家|区域|省份|城市|ISP_
只有中国的数据精确到了城市其他国家只能定位到国家后前的选项全部是0已经包含了全部你能查到的大大小小的国家。
请忽略前面的城市Id个人项目需求
# Ip2region特性
### 3. 体积小:
### 99.9%准确率
生成的数据库文件ip2region.db只有1.5M1.2版本前是3.5M
数据聚合了一些知名ip到地名查询提供商的数据这些是他们官方的的准确率经测试着实比经典的纯真IP定位准确一些。<br />
ip2region的数据聚合自以下服务商的开放API或者数据(升级程序每秒请求次数2到4次): <br />
01, &gt;80%, 淘宝IP地址库, [http://ip.taobao.com/](http://ip.taobao.com/) <br />
02, ≈10%, GeoIP, [https://geoip.com/](https://geoip.com/) <br />
03, ≈2%, 纯真IP库, [http://www.cz88.net/](http://www.cz88.net/) <br />
<b>备注:</b>如果上述开放API或者数据都不给开放数据时ip2region将停止数据的更新服务。
### 4. 多查询客户端的支持0.0x毫秒级别的查询
已经集成的客户端有java, C#, php, c, pythonnodejsphp扩展(支持linux, php5, php7版本已支持)golang。
### 标准化的数据格式
提供了两种查询算法,响应时间如下:
客户端/binary算法/b-tree算法/Memory算法
c#/0.x毫秒/0.x毫秒/0.x毫秒
java/0.x毫秒/0.x毫秒/0.1x毫秒 (使用RandomAccessFile)
php/0.x毫秒/0.1x毫秒/0.1x毫秒
c/0.0x毫秒/0.0x毫秒/0.00x毫秒(b-tree算法基本稳定在0.02x毫秒级别)
python/0.x毫秒/0.1x毫秒/未知
任何客户端b-tree都比binary算法快当然Memory算法固然是最快的
maven仓库地址
```xml
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>1.4</version>
</dependency>
每条ip数据段都固定了格式
```
nuget安装命令
```shell
Install-Package IP2Region
_城市Id|国家|区域|省份|城市|ISP_
```
### 5. 测试程序:
C# 和 Node.js请具体参考文件夹中README.md 说明。
只有中国的数据精确到了城市其他国家有部分数据只能定位到国家后前的选项全部是0已经包含了全部你能查到的大大小小的国家请忽略前面的城市Id个人项目需求
java:
```shell
cd binding/java
ant all
java -jar ip2region-{version}.jar ./data/ip2region.db
```
php:
```shell
php binding/php/testSearcher.php ./data/ip2region.db
```
### 体积小
c:
包含了全部的IP生成的数据库文件ip2region.db只有几MB最小的版本只有1.5MB随着数据的详细度增加数据库的大小也慢慢增大目前还没超过8MB。
### 查询速度快
全部的查询客户端单次查询都在0.x毫秒级别内置了三种查询算法
1. memory算法整个数据库全部载入内存单次查询都在0.1x毫秒内C语言的客户端单次查询在0.00x毫秒级别。
2. binary算法基于二分查找基于ip2region.db文件不需要载入内存单次查询在0.x毫秒级别。
3. b-tree算法基于btree算法基于ip2region.db文件不需要载入内存单词查询在0.x毫秒级别比binary算法更快。
任何客户端b-tree都比binary算法快当然memory算法固然是最快的
### 多查询客户端的支持
已经集成的客户端有java、C#、php、c、python、nodejs、php扩展(php5和php7)、golang、rust、lua、lua_c, nginx。
binding | 描述 | 开发状态 | binary查询耗时 | b-tree查询耗时 | memory查询耗时
:-: | :-: | :-: | :-: | :-: | :-:
[c](binding/c) | ANSC c binding | 已完成 | 0.0x毫秒 | 0.0x毫秒 | 0.00x毫秒
[c#](binding/c#) | c# binding | 已完成 | 0.x毫秒 | 0.x毫秒 | 0.1x毫秒
[golang](binding/golang) | golang binding | 已完成 | 0.x毫秒 | 0.x毫秒 | 0.1x毫秒
[java](binding/java) | java binding | 已完成 | 0.x毫秒 | 0.x毫秒 | 0.1x毫秒
[lua](binding/lua) | lua实现的binding | 已完成 | 0.x毫秒 | 0.x毫秒 | 0.x毫秒
[lua_c](binding/lua_c) | lua的c扩展 | 已完成 | 0.0x毫秒 | 0.0x毫秒 | 0.00x毫秒
[nginx](binding/nginx) | nginx的c扩展 | 已完成 | 0.0x毫秒 | 0.0x毫秒 | 0.00x毫秒
[nodejs](binding/nodejs) | nodejs | 已完成 | 0.x毫秒 | 0.x毫秒 | 0.1x毫秒
[php](binding/php) | php实现的binding | 已完成 | 0.x毫秒 | 0.1x毫秒 | 0.1x毫秒
[php5_ext](binding/php5_ext) | php5的c扩展 | 已完成 | 0.0x毫秒 | 0.0x毫秒 | 0.00x毫秒
[php7_ext](binding/php7_ext) | php7的c扩展 | 已完成 | 0.0毫秒 | 0.0x毫秒 | 0.00x毫秒
[python](binding/python) | python bindng | 已完成 | 0.x毫秒 | 0.x毫秒 | 0.x毫秒
[rust](binding/rust) | rust binding | 已完成 | 0.x毫秒 | 0.x毫秒 | 0.x毫秒
# ip2region快速测试
请参考每个binding下的README说明去运行cli测试程序例如C语言的demo运行如下
```shell
cd binding/c/
gcc -g -O2 testSearcher.c ip2region.c
./a.out ../../data/ip2region.db
```
python:
```shell
python binding/python/testSearcher.py ./data/ip2region.db
```
均会看到如下界面:
会看到如下cli界面
```shell
initializing B-tree ...
+----------------------------------+
@ -82,16 +87,60 @@ p2region>> 101.105.35.57
2163|中国|华南|广东省|深圳市|鹏博士 in 0.02295 millseconds
```
输入ip地址开始测试第一次会稍微有点慢在运行命令后面接入binary,memory来尝试其他算法建议使用b-tree算法速度和并发需求的可以使用memory算法。
具体集成请参考不同客户端的测试源码。
输入IP地址开始测试第一次会稍微有点慢在运行命令后面接入binary,memory来尝试其他算法建议使用b-tree算法速度和并发需求的可以使用memory算法具体集成请参考不同binding下的测试源码。
### 6. 如何生成ip2region.db文件
# ip2region安装
具体请参考每个binding下的README文档和测试demo以下是一些可用的快捷安装方式
### maven仓库地址
```xml
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>1.7.2</version>
</dependency>
```
### nodejs
```
npm install node-ip2region --save
```
### nuget安装
```shell
Install-Package IP2Region
```
### php composer
```shell
# 插件来自https://github.com/zoujingli/ip2region
composer require zoujingli/ip2region
```
# ip2region 并发使用
1. 全部binding的各个search接口都<b>不是</b>线程安全的实现不同线程可以通过创建不同的查询对象来使用并发量很大的情况下binary和b-tree算法可能会打开文件数过多的错误请修改内核的最大允许打开文件数(fs.file-max=一个更高的值)或者使用持久化的memory算法。
2. memorySearch接口在发布对象前进行一次预查询(本质上是把ip2region.db文件加载到内存),可以安全用于多线程环境。
# ip2region.db的生成
从1.8版本开始ip2region开源了ip2region.db生成程序的java实现提供了ant编译支持编译后会得到以下提到的dbMaker-{version}.jar对于需要研究生成程序的或者更改自定义生成配置的请参考${ip2region_root}/maker/java内的java源码。
从ip2region 1.2.2版本开始里面提交了一个dbMaker-{version}.jar的可以执行jar文件用它来完成这个工作
* 1, 确保你安装好了java环境不玩Java的童鞋就自己谷歌找找拉临时用一用几分钟的事情
* 2, cd到ip2region的根目录然后运行如下命令
1. 确保你安装好了java环境不玩Java的童鞋就自己谷歌找找拉临时用一用几分钟的事情
2. cd到${ip2region_root}/maker/java然后运行如下命令
```shell
java -jar dbMaker-{version}.jar -src 文本数据文件 -region 地域csv文件 [-dst 生成的ip2region.db文件的目录]
@ -101,14 +150,24 @@ java -jar dbMaker-{version}.jar -src 文本数据文件 -region 地域csv文件
# ip2region.db文件的目录是可选参数没有指定的话会在当前目录生成一份./data/ip2region.db文件
```
* 3, 获取生成的ip2region.db文件覆盖原来的ip2region.db文件即可
* 4, 默认的ip2region.db文件生成命令:
3. 获取生成的ip2region.db文件覆盖原来的ip2region.db文件即可
4. 默认的ip2region.db文件生成命令:
```shell
cd ip2region项目根目录
cd ${ip2region_root}/java/
java -jar dbMaker-1.2.2.jar -src ./data/ip.merge.txt -region ./data/global_region.csv
# 会看到一大片的输出
```
* 5, 数据库文件的结构和原理请阅读 @冬芽 的blog[“ip2region数据库文件的结构和原理”](http://dongyado.com/tool/2016/08/18/structure-of-ip2region-database-file/)
# 相关备注
### 声明
ip2region重点在于<b>研究IP数据的存储设计和各种语言的查询实现</b>并没有原始IP数据的支撑数据来源请参考上面的描述升级数据需要很多IP的支撑并且会对原始平台造成一定量的请求压力本项目不保证及时的数据更新没有也不会有商用版本你可以使用自定义的数据导入ip2region进行自定义查询的实现。
### 技术交流
1. 数据库文件的结构和原理请阅读 @冬芽 的blog[“ip2region数据库文件的结构和原理”](https://github.com/dongyado/dongyado.github.io/blob/master/_posts/2016-08-18-structure-of-ip2region-database-file.md)
2. ip2region交流分享微信lionsoul2014(请备注ip2region)QQ1187582057(很少关注)

Binary file not shown.

BIN
binding/LabVIEW/Close.vi Normal file

Binary file not shown.

BIN
binding/LabVIEW/Example.vi Normal file

Binary file not shown.

BIN
binding/LabVIEW/Get data.vi Normal file

Binary file not shown.

BIN
binding/LabVIEW/Open.vi Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,13 @@
<?xml version='1.0' encoding='UTF-8'?>
<Library LVVersion="19008000">
<Property Name="NI.Lib.Icon" Type="Bin">'1#!!!!!!!)!"1!&amp;!!!-!%!!!@````]!!!!"!!%!!!(]!!!*Q(C=\&gt;7R=2MR%!81N=?"5Q&lt;/07RB7W!,&lt;'&amp;&lt;9+K1,7Q,&lt;)%N&lt;!NMA3X)DW?-RJ(JQ"I\%%Z,(@`BA#==ZB3RN;]28_,V7@PWW`:R`&gt;HV*SU_WE@\N_XF[3:^^TX\+2YP)D7K6;G-RV3P)R`ZS%=_]J'XP/5N&lt;XH,7V\SEJ?]Z#5P?=J4HP+5JTTFWS%0?=B$DD1G(R/.1==!IT.+D)`B':\B'2Z@9XC':XC':XBUC?%:HO%:HO&amp;R7QT0]!T0]!S0I4&lt;*&lt;)?=:XA-(]X40-X40-VDSGC?"GC4N9(&lt;)"D2,L;4ZGG?ZH%;T&gt;-]T&gt;-]T?.S.%`T.%`T.)^&lt;NF8J4@-YZ$S'C?)JHO)JHO)R&gt;"20]220]230[;*YCK=ASI2F=)1I.Z5/Z5PR&amp;)^@54T&amp;5TT&amp;5TQO&lt;5_INJ6Z;"[(H#&gt;ZEC&gt;ZEC&gt;Z$"(*ETT*ETT*9^B)HO2*HO2*(F.&amp;]C20]C2)GN4UE1:,.[:/+5A?0^NOS?UJ^3&lt;*\9B9GT@7JISVW7*NIFC&lt;)^:$D`5Q9TWE7)M@;V&amp;D,6;M29DVR]6#R],%GC47T9_/=@&gt;Z5V&gt;V57&gt;V5E&gt;V5(OV?^T[FTP?\`?YX7ZRP6\D=LH%_8S/U_E5R_-R$I&gt;$\0@\W/VW&lt;[_"\Y[X&amp;],0^^+,]T_J&gt;`J@_B_]'_.T`$KO.@I"XC-_N!!!!!!</Property>
<Property Name="NI.Lib.SourceVersion" Type="Int">419463168</Property>
<Property Name="NI.Lib.Version" Type="Str">1.0.0.0</Property>
<Property Name="NI.LV.All.SourceOnly" Type="Bool">false</Property>
<Item Name="binarySearch.vi" Type="VI" URL="../binarySearch.vi"/>
<Item Name="Close.vi" Type="VI" URL="../Close.vi"/>
<Item Name="example.vi" Type="VI" URL="../example.vi"/>
<Item Name="Get data.vi" Type="VI" URL="../Get data.vi"/>
<Item Name="ip2region.ctl" Type="VI" URL="../ip2region.ctl"/>
<Item Name="Open.vi" Type="VI" URL="../Open.vi"/>
</Library>

View File

@ -0,0 +1,7 @@
ip2region的LabVIEW库<br>
使用方法1. 将该目录下的所有文件复制到你的项目文件夹下;<br>
2. 导入ip2region.lvlib库文件<br>
3. 查看Example.vi了解使用方法<br>
<br>
<br>
注意该库依赖ni_lib_unicode-2.0.1.5.vip请使用VIPM安装。

View File

@ -23,7 +23,7 @@ namespace IP2Region
/// <summary>
/// db file access handler
/// </summary>
private FileStream _raf = null;
private Stream _raf = null;
/// <summary>
/// header blocks buffer
@ -75,6 +75,18 @@ namespace IP2Region
public DbSearcher(string dbFile) : this(null, dbFile) { }
public DbSearcher(DbConfig dbConfig, Stream dbFileStream)
{
if (_dbConfig == null)
{
_dbConfig = dbConfig;
}
_raf = dbFileStream;
}
public DbSearcher(Stream dbFileStream) : this(null, dbFileStream) { }
#region Sync Methods
/// <summary>
/// Get the region with a int ip address with memory binary search algorithm.

View File

@ -1,8 +1,8 @@
/**
* default ip2region implementation
*
* @see #ip2region.h
* @author chenxin<chenxin619315@gmail.com>
* @see #ip2region.h
* @author chenxin<chenxin619315@gmail.com>
* @date 2015-10-30
*/
@ -13,9 +13,9 @@
/**
* create a new ip2region object
*
* @param dbFile path
* @param dbFile path
*/
IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, char *dbFile)
IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, const char *dbFile)
{
memset(ip2rObj, 0x00, sizeof(ip2region_entry));
ip2rObj->headerLen = 0;
@ -51,22 +51,27 @@ IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, char *dbFile)
/**
* destroy the specifield ip2region object
*
* @param ip2region_t
* @param ip2region_t
*/
IP2R_API uint_t ip2region_destroy(ip2region_t ip2rObj)
{
IP2R_FREE(ip2rObj->HeaderSip);
ip2rObj->HeaderSip = NULL;
IP2R_FREE(ip2rObj->HeaderPtr);
ip2rObj->HeaderPtr = NULL;
if ( ip2rObj->HeaderSip != NULL ) {
IP2R_FREE(ip2rObj->HeaderSip);
ip2rObj->HeaderSip = NULL;
}
//close the db file resource
if ( ip2rObj->HeaderPtr != NULL ) {
IP2R_FREE(ip2rObj->HeaderPtr);
ip2rObj->HeaderPtr = NULL;
}
// close the db file resource
if ( ip2rObj->dbHandler != NULL ) {
fclose(ip2rObj->dbHandler);
ip2rObj->dbHandler = NULL;
}
//free the db binary string
// free the db binary string
if ( ip2rObj->dbBinStr != NULL ) {
IP2R_FREE(ip2rObj->dbBinStr);
ip2rObj->dbBinStr = NULL;
@ -149,7 +154,7 @@ IP2R_API uint_t ip2region_memory_search(ip2region_t ip2rObj, uint_t ip, databloc
return 1;
}
IP2R_API uint_t ip2region_memory_search_string(ip2region_t ip2rObj, char *ip, datablock_t datablock)
IP2R_API uint_t ip2region_memory_search_string(ip2region_t ip2rObj, const char *ip, datablock_t datablock)
{
return ip2region_memory_search(ip2rObj, ip2long(ip), datablock);
}
@ -157,10 +162,10 @@ IP2R_API uint_t ip2region_memory_search_string(ip2region_t ip2rObj, char *ip, da
/**
* get the region associated with the specifield ip address with binary search algorithm
*
* @param ip2rObj
* @param ip
* @param datablock
* @return uint_t
* @param ip2rObj
* @param ip
* @param datablock
* @return uint_t
*/
IP2R_API uint_t ip2region_binary_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock)
{
@ -226,7 +231,7 @@ IP2R_API uint_t ip2region_binary_search(ip2region_t ip2rObj, uint_t ip, databloc
return 1;
}
IP2R_API uint_t ip2region_binary_search_string(ip2region_t ip2rObj, char *ip, datablock_t datablock)
IP2R_API uint_t ip2region_binary_search_string(ip2region_t ip2rObj, const char *ip, datablock_t datablock)
{
return ip2region_binary_search(ip2rObj, ip2long(ip), datablock);
}
@ -234,10 +239,10 @@ IP2R_API uint_t ip2region_binary_search_string(ip2region_t ip2rObj, char *ip, da
/**
* get the region associated with the specifield ip address with b-tree algorithm
*
* @param ip2rObj
* @param ip
* @param datablock
* @return uint_t
* @param ip2rObj
* @param ip
* @param datablock
* @return uint_t
*/
IP2R_API uint_t ip2region_btree_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock)
{
@ -355,7 +360,7 @@ IP2R_API uint_t ip2region_btree_search(ip2region_t ip2rObj, uint_t ip, datablock
return 1;
}
IP2R_API uint_t ip2region_btree_search_string(ip2region_t ip2rObj, char *ip, datablock_t datablock)
IP2R_API uint_t ip2region_btree_search_string(ip2region_t ip2rObj, const char *ip, datablock_t datablock)
{
return ip2region_btree_search(ip2rObj, ip2long(ip), datablock);
}
@ -363,11 +368,11 @@ IP2R_API uint_t ip2region_btree_search_string(ip2region_t ip2rObj, char *ip, dat
/**
* get a unsinged long(4bytes) from a specifield buffer start from the specifield offset
*
* @param buffer
* @param offset
* @return uint_t
* @param buffer
* @param offset
* @return uint_t
*/
IP2R_API uint_t getUnsignedInt(char *buffer, int offset)
IP2R_API uint_t getUnsignedInt(const char *buffer, int offset)
{
return (
((buffer[offset ]) & 0x000000FF) |
@ -380,22 +385,28 @@ IP2R_API uint_t getUnsignedInt(char *buffer, int offset)
/**
* string ip to long
*
* @param ip
* @return uint_t
* @param ip
* @return uint_t
*/
IP2R_API uint_t ip2long(char *ip)
IP2R_API uint_t ip2long(const char *ip)
{
int i = 0, p = 24;
char buffer[4], *cs = ip;
char buffer[4];
const char *cs = ip;
uint_t ipval = 0;
while ( *cs != '\0' ) {
if ( *cs == ' ' ) {
cs++;
continue;
}
if ( i > 3 ) {
ipval = 0;
break;
}
if ( *cs == '.' ) {
//single part length limit
if ( i > 3 ) {
ipval = 0;
break;
}
if ( p < 0 ) break;
buffer[i] = '\0';
@ -420,9 +431,9 @@ IP2R_API uint_t ip2long(char *ip)
/**
* long to string ip
*
* @param ip
* @param buffer
* @return uint_t(1 for success and 0 for failed)
* @param ip
* @param buffer
* @return uint_t(1 for success and 0 for failed)
*/
IP2R_API uint_t long2ip(uint_t ip, char *buffer)
{

View File

@ -80,7 +80,7 @@ typedef datablock_entry * datablock_t;
* @param ip2rObj
* @param dbFile path
*/
IP2R_API uint_t ip2region_create(ip2region_t, char *);
IP2R_API uint_t ip2region_create(ip2region_t, const char *);
/**
* destroy the specified ip2region object
@ -98,7 +98,7 @@ IP2R_API uint_t ip2region_destroy(ip2region_t);
* @date 2016/06/30
*/
IP2R_API uint_t ip2region_memory_search(ip2region_t, uint_t, datablock_t);
IP2R_API uint_t ip2region_memory_search_string(ip2region_t, char *, datablock_t);
IP2R_API uint_t ip2region_memory_search_string(ip2region_t, const char *, datablock_t);
/**
@ -110,7 +110,7 @@ IP2R_API uint_t ip2region_memory_search_string(ip2region_t, char *, datablock_t)
* @return uint_t
*/
IP2R_API uint_t ip2region_binary_search(ip2region_t, uint_t, datablock_t);
IP2R_API uint_t ip2region_binary_search_string(ip2region_t, char *, datablock_t);
IP2R_API uint_t ip2region_binary_search_string(ip2region_t, const char *, datablock_t);
/**
* get the region associated with the specified ip address with b-tree algorithm
@ -121,7 +121,7 @@ IP2R_API uint_t ip2region_binary_search_string(ip2region_t, char *, datablock_t)
* @return uint_t
*/
IP2R_API uint_t ip2region_btree_search(ip2region_t, uint_t, datablock_t);
IP2R_API uint_t ip2region_btree_search_string(ip2region_t, char *, datablock_t);
IP2R_API uint_t ip2region_btree_search_string(ip2region_t, const char *, datablock_t);
/**
* get a unsinged long(4bytes) from a specified buffer start from the specified offset
@ -130,7 +130,7 @@ IP2R_API uint_t ip2region_btree_search_string(ip2region_t, char *, datablock_t);
* @param offset
* @return uint_t
*/
IP2R_API uint_t getUnsignedInt(char *, int);
IP2R_API uint_t getUnsignedInt(const char *, int);
/**
* string ip to long
@ -138,7 +138,7 @@ IP2R_API uint_t getUnsignedInt(char *, int);
* @param ip
* @return uint_t
*/
IP2R_API uint_t ip2long(char *);
IP2R_API uint_t ip2long(const char *);
/**
* long to string ip

View File

@ -1,7 +1,7 @@
/**
* test ip2region searcher program
*
* @author chenxin<chenxin619315@gmail.com>
* @author chenxin<chenxin619315@gmail.com>
* @date 2015-10-30
*/
@ -49,7 +49,7 @@ int main( int argc, char **argv )
datablock_entry datablock;
char *dbFile = NULL, *algorithm = NULL;
char line[256];
uint_t (*func_ptr)(ip2region_t, char *, datablock_t);
uint_t (*func_ptr)(ip2region_t, const char *, datablock_t);
double s_time, c_time;
memset(&datablock, 0x00, sizeof(datablock_entry));
@ -95,7 +95,7 @@ int main( int argc, char **argv )
printf("%d|%s in %.5f millseconds\n", datablock.city_id, datablock.region, c_time);
}
//destory the ip2rObj
// destory the ip2rObj
ip2region_destroy(&ip2rEntry);
return 0;

View File

@ -3,7 +3,7 @@
#### 获取
```
go get github.com/mohong122/ip2region/binding/golang
go get github.com/lionsoul2014/ip2region/binding/golang
```
@ -17,7 +17,7 @@ package main
import (
"fmt"
"github.com/mohong122/ip2region/binding/golang/ip2region"
"github.com/lionsoul2014/ip2region/binding/golang/ip2region"
)
func main() {

View File

@ -2,7 +2,7 @@ package main
import (
"os"
"github.com/mohong122/ip2region/binding/golang/ip2region"
"github.com/lionsoul2014/ip2region/binding/golang/ip2region"
"bufio"
"fmt"
"strings"

View File

@ -4,7 +4,7 @@
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>1.4</version>
<version>1.7.2</version>
<packaging>jar</packaging>
<name>ip2region</name>
@ -47,7 +47,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -58,6 +58,25 @@ public class DbSearcher
raf = new RandomAccessFile(dbFile, "r");
}
/**
* construct method with self-define std ip2region binary string support
* Thanks to the issue from Wendal at https://gitee.com/lionsoul/ip2region/issues/IILFL.
*
* Note: This construtor should be used for memory search ONLY !!!
*
* @param dbConfig
* @param dbBinStr
*/
public DbSearcher(DbConfig dbConfig, byte[] dbBinStr)
{
this.dbConfig = dbConfig;
this.dbBinStr = dbBinStr;
firstIndexPtr = Util.getIntLong(dbBinStr, 0);
lastIndexPtr = Util.getIntLong(dbBinStr, 4);
totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr)/IndexBlock.getIndexBlockLength()) + 1;
}
/**
* get the region with a int ip address with memory binary search algorithm
*
@ -163,8 +182,8 @@ public class DbSearcher
//check and load the header
if ( HeaderSip == null ) {
raf.seek(8L); //pass the super block
//byte[] b = new byte[dbConfig.getTotalHeaderSize()];
byte[] b = new byte[4096];
byte[] b = new byte[dbConfig.getTotalHeaderSize()];
// byte[] b = new byte[4096];
raf.readFully(b, 0, b.length);
//fill the header
@ -382,7 +401,9 @@ public class DbSearcher
HeaderSip = null; //let gc do its work
HeaderPtr = null;
dbBinStr = null;
raf.close();
if ( raf != null ) {
raf.close();
}
}
}

443
binding/lua/Ip2region.lua Normal file
View File

@ -0,0 +1,443 @@
--[[
Ip2region lua binding
@author chenxin<chenxin619315@gmail.com>
@date 2018/10/02
]]--
require("bit32");
local INDEX_BLOCK_LENGTH = 12;
local TOTAL_HEADER_LENGTH = 8192;
local _M = {
dbFile = "",
dbFileHandler = "",
dbBinStr = "",
HeaderSip = "",
HeaderPtr = "",
headerLen = 0,
firstIndexPtr = 0,
lastIndexPtr = 0,
totalBlocks = 0
};
_G["Ip2region"] = _M;
-- set the __index to itself
_M.__index = _M;
-- set the print meta-method
_M.__tostring = function(table)
local t = {
"dbFile=" .. table.dbFile,
-- "dbFileHandler=" .. table.dbFileHandler,
"headerLen=" .. table.headerLen,
"firstIndexPtr" .. table.firstIndexPtr,
"lastIndexPtr" .. table.lastIndexPtr,
"totalBlocks" .. table.totalBlocks
};
return table.concat(t, ",");
end
--[[
construct method
@param obj
@return Ip2region object
--]]
function _M.new(dbFile)
obj = {};
setmetatable(obj, _M);
obj.dbFile = dbFile;
return obj;
end
--[[
internal function to get a integer from a binary string
@param dbBinStr
@param idx
@return Integer
]]--
function getLong(bs, idx)
local a1 = string.byte(string.sub(bs, idx, idx));
local a2 = bit32.lshift(string.byte(string.sub(bs, idx+1, idx+1)), 8);
local a3 = bit32.lshift(string.byte(string.sub(bs, idx+2, idx+2)), 16);
local a4 = bit32.lshift(string.byte(string.sub(bs, idx+3, idx+3)), 24);
local val = bit32.bor(a1, a2);
val = bit32.bor(val, a3);
val = bit32.bor(val, a4);
return val;
end
--[[
internal function to convert the string ip to a long value
@param ip
@return Integer
]]--
function _M.ip2long(self, ip)
-- dynamic arguments checking
-- to support object.ip2long and object:ip2long access
if ( type(self) == "string") then
ip = self;
end
local ini = 1;
local iip = 0;
local off = 24;
while true do
local pos = string.find(ip, '.', ini, true);
if ( not pos ) then
break;
end
local sub = string.sub(ip, ini, pos - 1);
if ( string.len(sub) < 1 ) then
return nil;
end
iip = bit32.bor(iip, bit32.lshift(tonumber(sub), off));
ini = pos + 1;
off = off - 8;
end
-- check if it is a valid ip address
if ( off ~= 0 or ini > string.len(ip) ) then
return nil;
end
local sub = string.sub(ip, ini);
if ( string.len(sub) < 1 ) then
return nil;
end
return bit32.bor(iip, bit32.lshift(tonumber(sub), off));
end
--[[
internal function to get the whole content of a file
@param file
@return String
]]--
function get_file_contents(file)
local fi = io.input(file);
if ( not fi ) then
return nil;
end
local str = io.read("*a");
io.close();
return str;
end
--[[
set the current db file path
@param dbFile
]]--
function _M:setDbFile(dbFile)
self.dbFile = dbFile;
end
--[[
all the db binary string will be loaded into memory
then search the memory only and this will a lot faster than disk base search
@Note: invoke it once before put it to public invoke could make it thread safe
@param ip
@return table or nil for failed
]]--
function _M:memorySearch(ip)
-- string ip conversion
if ( type(ip) == "string" ) then
ip = self:ip2long(ip);
if ( ip == nil ) then
return nil;
end
end;
-- check and load the binary string for the first time
if ( self.dbBinStr == "" ) then
self.dbBinStr = get_file_contents(self.dbFile);
if ( not self.dbBinStr ) then
return nil;
end
self.firstIndexPtr = getLong(self.dbBinStr, 1);
self.lastIndexPtr = getLong(self.dbBinStr, 5);
self.totalBlocks = (self.lastIndexPtr - self.firstIndexPtr)/INDEX_BLOCK_LENGTH + 1;
end
-- binary search to define the data
local l = 0;
local h = self.totalBlocks;
local dataPtr = 0;
while ( l <= h ) do
local m = math.floor((l + h) / 2);
local p = self.firstIndexPtr + m * INDEX_BLOCK_LENGTH;
local sip = getLong(self.dbBinStr, p + 1);
if ( ip < sip ) then
h = m - 1;
else
local eip = getLong(self.dbBinStr, p + 5); -- 4 + 1
if ( ip > eip ) then
l = m + 1;
else
dataPtr = getLong(self.dbBinStr, p + 9); -- 8 + 1
break;
end
end
end
-- not matched just stop it here
if ( dataPtr == 0 ) then return nil end
-- get the data
local dataLen = bit32.band(bit32.rshift(dataPtr, 24), 0xFF);
dataPtr = bit32.band(dataPtr, 0x00FFFFFF);
local dptr = dataPtr + 5; -- 4 + 1
return {
city_id = getLong(self.dbBinStr, dataPtr),
region = string.sub(self.dbBinStr, dptr, dptr + dataLen - 5)
};
end
--[[
get the data block through the specified ip address
or long ip numeric with binary search algorithm
@param ip
@return table or nil for failed
]]--
function _M:binarySearch(ip)
-- check and conver the ip address
if ( type(ip) == "string" ) then
ip = self:ip2long(ip);
if ( ip == nil ) then
return nil;
end
end
if ( self.totalBlocks == 0 ) then
-- check and open the original db file
self.dbFileHandler = io.open(self.dbFile, "r");
if ( not self.dbFileHandler ) then
return nil;
end
self.dbFileHandler:seek("set", 0);
local superBlock = self.dbFileHandler:read(8);
self.firstIndexPtr = getLong(superBlock, 1); -- 0 + 1
self.lastIndexPtr = getLong(superBlock, 5); -- 4 + 1
self.totalBlocks = (self.lastIndexPtr-self.firstIndexPtr)/INDEX_BLOCK_LENGTH + 1;
end
-- binary search to define the data
local l = 0;
local h = self.totalBlocks;
local dataPtr = 0;
while ( l <= h ) do
local m = math.floor((l + h) / 2);
local p = m * INDEX_BLOCK_LENGTH;
self.dbFileHandler:seek("set", self.firstIndexPtr + p);
local buffer = self.dbFileHandler:read(INDEX_BLOCK_LENGTH);
local sip = getLong(buffer, 1); -- 0 + 1
if ( ip < sip ) then
h = m - 1;
else
local eip = getLong(buffer, 5); -- 4 + 1
if ( ip > eip ) then
l = m + 1;
else
dataPtr = getLong(buffer, 9); -- 8 + 1
break;
end
end
end
-- not matched just stop it here
if ( dataPtr == 0 ) then return nil; end
-- get the data
local dataLen = bit32.band(bit32.rshift(dataPtr, 24), 0xFF);
dataPtr = bit32.band(dataPtr, 0x00FFFFFF);
self.dbFileHandler:seek("set", dataPtr);
local data = self.dbFileHandler:read(dataLen);
return {
city_id = getLong(data, 1), -- 0 + 1
region = string.sub(data, 5) -- 4 + 1
};
end
--[[
get the data block associated with the specified ip with b-tree search algorithm
@param ip
@return table or nil for failed
]]--
function _M:btreeSearch(ip)
-- string ip to integer conversion
if ( type(ip) == "string" ) then
ip = self:ip2long(ip);
if ( ip == nil ) then
return nil;
end
end
-- check and load the header
if ( self.headerLen == 0 ) then
-- check and open the original db file
self.dbFileHandler = io.open(self.dbFile, 'r');
if ( not self.dbFileHandler ) then
return nil;
end
self.dbFileHandler:seek("set", 8);
local buffer = self.dbFileHandler:read(TOTAL_HEADER_LENGTH);
-- fill the header
local i = 0;
local idx = 0;
self.HeaderSip = {};
self.HeaderPtr = {};
for i=0, TOTAL_HEADER_LENGTH, 8 do
local startIp = getLong(buffer, i + 1); -- 0 + 1
local dataPtr = getLong(buffer, i + 5); -- 4 + 1
if ( dataPtr == 0 ) then
break;
end
table.insert(self.HeaderSip, startIp);
table.insert(self.HeaderPtr, dataPtr);
idx = idx + 1;
end
self.headerLen = idx;
end
-- 1. define the index block with the binary search
local l = 0;
local h = self.headerLen;
local sptr = 0;
local eptr = 0;
while ( l <= h ) do
local m = math.floor((l + h) / 2);
-- perfetc matched, just return it
if ( ip == self.HeaderSip[m] ) then
if ( m > 0 ) then
sptr = self.HeaderPtr[m-1];
eptr = self.HeaderPtr[m ];
else
sptr = self.HeaderPtr[m ];
eptr = self.HeaderPtr[m+1];
end
break;
end
-- less then the middle value
if ( ip < self.HeaderSip[m] ) then
if ( m == 0 ) then
sptr = self.HeaderPtr[m ];
eptr = self.HeaderPtr[m+1];
break;
elseif ( ip > self.HeaderSip[m-1] ) then
sptr = self.HeaderPtr[m-1];
eptr = self.HeaderPtr[m ];
break;
end
h = m - 1;
else
if ( m == self.headerLen - 1 ) then
sptr = self.HeaderPtr[m-1];
eptr = self.HeaderPtr[m ];
break;
elseif ( ip <= self.HeaderSip[m+1] ) then
sptr = self.HeaderPtr[m ];
eptr = self.HeaderPtr[m+1];
break;
end
l = m + 1;
end
end
-- match nothing just stop it
if ( sptr == 0 ) then return nil; end
-- 2. search the index blocks to define the data
self.dbFileHandler:seek("set", sptr);
local blockLen = eptr - sptr;
local index = self.dbFileHandler:read(blockLen + INDEX_BLOCK_LENGTH);
local dataPtr = 0;
l = 0;
h = blockLen / INDEX_BLOCK_LENGTH;
while ( l <= h ) do
local m = math.floor((l + h) / 2);
local p = m * INDEX_BLOCK_LENGTH;
local sip = getLong(index, p + 1); -- 0 + 1
if ( ip < sip ) then
h = m - 1;
else
local eip = getLong(index, p + 5); -- 4 + 1
if ( ip > eip ) then
l = m + 1;
else
dataPtr = getLong(index, p + 9); -- 8 + 1
break;
end
end
end
-- not matched
if ( dataPtr == 0 ) then return nil; end
-- 3. get the data
local dataLen = bit32.band(bit32.rshift(dataPtr, 24), 0xFF);
dataPtr = bit32.band(dataPtr, 0x00FFFFFF);
self.dbFileHandler:seek("set", dataPtr);
local data = self.dbFileHandler:read(dataLen);
return {
city_id = getLong(data, 1), -- 0 + 1
region = string.sub(data, 5) -- 4 + 1
};
end
--[[
close the object and do the basic gc
]]--
function _M.close(self)
if ( self.dbFileHandler ~= "" ) then
self.dbFileHandler:close();
end
if ( self.dbBinStr ~= "" ) then
self.dbBinStr = nil;
end
end
return _M;

59
binding/lua/README.md Normal file
View File

@ -0,0 +1,59 @@
# Lua ip2region binding
### 一,如何测试
* 1, cd到ip2region/binding/lua/根目录
* 2, 运行testSearcher测试程序
```shell
lua testSearcher.lua ../../data/ip2region.db
```
* 3, 输入ip地址开始测试即可
```shell
initializing btree
+----------------------------------+
| ip2region test script |
| Author: chenxin619315@gmail.com |
| Type 'quit' to exit program |
+----------------------------------+
ip2region>> 1.2.3.4
0|美国|0|华盛顿|0|0 in 8.001000 millseconds
ip2region>> 101.233.153.103
2163|中国|0|广东省|深圳市|鹏博士 in 0.176000 millseconds
ip2region>>
```
### 二,如何使用
* 1, 将Ip2region.lua拷贝到你的package.path下
* 2, 通过如下流程在你的lua程序中使用
```lua
-- 包含模块
local Ip2region = require "Ip2region";
-- 创建查询对象
-- 设置ip2region.db的文件地址dbFile表示ip2region.db数据库文件的地址
-- 注意new方法是通过“.”调用,而不是“:”
local ip2region = Ip2region.new("ip2region.db file path");
-- 也可以通过如下方式设置dbFile
-- ip2region.dbFile = "ip2region.db file path";
-- ip2region.setDbFile("ip2region.db file path");
local data;
-- 查询,备注,尽量请使用“:”调用方法,使用“.”需要主动传递ip2region对象参数
-- 1binary查询
data = ip2region:binarySearch("101.233.153.103");
-- 2btree查询
data = ip2region:btreeSearch("101.233.153.103");
-- 3memory查询
data = ip2region:memorySearch("101.233.153.103");
-- 返回结果如下
print("city_id=", data.city_id, "region=", data.region);
```
### 三,备注
* 1Ip2region模块的位运算依赖了bit32模块这个是lua 5.2才开始支持的。

View File

@ -0,0 +1,85 @@
--[[
ip2region lua client test script
@author chenxin<chenxin619315@gmail.com>
]]--
-- check the command line arguments
if ( not arg[1] ) then
print([[
Usage: lua testSearcher.lua [ip2region db file] [algorithm]
+-Optional Algorithm: binary, b-tree, memory]]);
os.exit();
end
local Ip2region = require "Ip2region";
-- local cjson = require "cjson";
-- local socket = require "socket";
-- check and parse the dbFile and the method algorithm
-- Create a new ip2region object by the new interface
local ip2region = Ip2region.new(arg[1]);
-- reset the dbFile by the follow two ways:
-- ip2region.dbFile = arg[1];
-- ip2region:setDbFile(arg[1]);
local algorithm = "btree";
if ( arg[2] ~= nil ) then
local arg_2 = string.lower(arg[2]);
if ( arg_2 ~= "binary" and arg_2 ~= "memory" ) then
algorithm = "binary";
elseif ( arg_2 == "memory" ) then
algorithm = "memory";
end
end
-- local data = searcher:memorySearch("120.79.17.142");
-- local data = searcher:binarySearch("120.79.17.142");
-- local data = searcher:btreeSearch("120.79.17.142");
print("initializing " .. algorithm ..[[
+----------------------------------+
| ip2region test script |
| Author: chenxin619315@gmail.com |
| Type 'quit' to exit program |
+----------------------------------+]]
);
while ( true ) do
io.write("ip2region>> ");
io.input(io.stdin);
local line = io.read();
if ( line == nil ) then
-- do nothing
break;
elseif ( line == "quit" ) then
break;
elseif ( ip2region.ip2long(line) == nil ) then
print("Invalid ip address=", line);
else
local data;
local s_time = os.clock();
if ( algorithm == "btree" ) then
data = ip2region:btreeSearch(line);
elseif ( algorithm == "binary" ) then
data = ip2region:binarySearch(line);
elseif ( algorithm == "memory" ) then
data = ip2region:memorySearch(line);
end
local cost_time = (os.clock() - s_time) * 1000; -- to millseconds
if ( data == nil ) then
io.write("Failed for ip=", line, " is it a valid ip address ?");
else
io.write(string.format("%u|%s in %5f millseconds\n", data.city_id, data.region, cost_time));
end
end
end
-- close the object
-- print(ip2region);
ip2region:close();

28
binding/lua_c/Makefile Normal file
View File

@ -0,0 +1,28 @@
# ip2region luc c module Make file
# @author chenxin<chenxin619315@gmail.com>
# @date 2018/10/05
# @Note
# Please modify the LIBS and the LIB_DIR to fit you system
#
VER = 5.2
CC = gcc
LIBS = -I ../c/ -I /usr/include/lua$(VER)/
FFLAGS = -O2 -Wall -fPIC
SO_FILE = Ip2region.so
LIB_DIR = /usr/local/share/lua/$(VER)
all: ../c/ip2region.c ../c/ip2region.h lua_ip2region.c
$(CC) $(FFLAGS) $(LIBS) ../c/ip2region.c lua_ip2region.c -fPIC -shared -o $(SO_FILE)
install:
sudo mkdir -p $(LIB_DIR); \
sudo cp $(SO_FILE) $(LIB_DIR);\
echo "install Ip2region to $(LIB_DIR) successfully.";\
clean:
find . -name \*.so | xargs rm -f
find . -name \*.o | xargs rm -f
.PHONY: clean

74
binding/lua_c/README.md Normal file
View File

@ -0,0 +1,74 @@
# Lua ip2region c module binding
### 一,如何安装
* 1, cd到ip2region/binding/lua_c/根目录
* 2, 运行如下命令
```
make
sudo make install
```
* 3, 关于Makefile你可能需要更改里面的VER(版本号)和LIB_DIR(lua的so lib目录)来适应你的系统
```shell
VER = 5.2
CC = gcc
LIBS = -I ../c/ -I /usr/include/lua$(VER)/
FFLAGS = -O2 -Wall -fPIC
SO_FILE = Ip2region.so
LIB_DIR = /usr/local/share/lua/$(VER)
```
### 二,如何测试
* 1, cd到ip2region/binding/lua_c/根目录
* 2, 运行testSearcher测试程序
```shell
lua testSearcher.lua ../../data/ip2region.db
```
* 3, 输入ip地址开始测试即可
```shell
initializing btree
+----------------------------------+
| ip2region test script |
| Author: chenxin619315@gmail.com |
| Type 'quit' to exit program |
+----------------------------------+
ip2region>> 1.2.3.4
0|美国|0|华盛顿|0|0 in 0.100000 millseconds
ip2region>> 101.233.153.103
2163|中国|0|广东省|深圳市|鹏博士 in 0.045000 millseconds
ip2region>>
```
### 三,如何使用
* 1, 先参考第一步安装把Ip2region.so安装到你系统lua的lib目录下
* 2, 通过如下流程在你的lua程序中使用
```lua
-- 包含模块
local Ip2region = require "Ip2region";
-- 创建查询对象
-- 设置ip2region.db的文件地址dbFile表示ip2region.db数据库文件的地址
-- 注意new方法是通过“.”调用,而不是“:”
local ip2region = Ip2region.new("ip2region.db file path");
local data;
-- 查询,备注,尽量请使用“:”调用方法,使用“.”需要主动传递ip2region对象参数
-- 1binary查询
data = ip2region:binarySearch("101.233.153.103");
-- 2btree查询
data = ip2region:btreeSearch("101.233.153.103");
-- 3memory查询
data = ip2region:memorySearch("101.233.153.103");
-- 返回结果如下
print("city_id=", data.city_id, "region=", data.region);
```
### 四,备注
* 1c模块的Ip2region以来binding/c/的实现请保持binding/c/存在并且和lua_c模块同目录。
* 2Ip2region的c模块拥有和c模块几乎等同的性能生产建议使用lua_c模块代替纯lua模块的使用。

View File

@ -0,0 +1,232 @@
/**
* Ip2region lua c binding
*
* @author chenxin<chenxin619315@gmail.com>
* @date 2018/10/04
*/
#include <stdio.h>
#include <stdlib.h>
#include <lua.h>
#include <lauxlib.h>
#include "../c/ip2region.h"
#define L_METATABLE_NAME "Ip2region_MT"
/** create a new ip2region object with a specified dbFile */
static int lua_ip2region_new(lua_State *L)
{
ip2region_entry *self;
const char *dbFile;
/* Check the arguments are valid */
dbFile = luaL_checkstring(L, 1);
if ( dbFile == NULL ) {
luaL_error(L, "dbFile cannot be empty");
}
/* Create the user data and push it onto the stack */
self = (ip2region_entry *) lua_newuserdata(L, sizeof(ip2region_entry));
/* Push the metatable onto the stack */
luaL_getmetatable(L, L_METATABLE_NAME);
/* Set the metatable on the userdata */
lua_setmetatable(L, -2);
/* Initialize the entry */
ip2region_create(self, dbFile);
return 1;
}
/** destroy the specified ip2region instance */
static int lua_ip2region_destroy(lua_State *L)
{
ip2region_entry *self;
self = (ip2region_entry *) luaL_checkudata(L, 1, L_METATABLE_NAME);
ip2region_destroy(self);
return 0;
}
#define get_search_args(L, entry, ip) \
do { \
luaL_argcheck(L, lua_gettop(L) == 2, 2, "Object and ip address needed"); \
entry = (ip2region_entry *) luaL_checkudata(L, 1, L_METATABLE_NAME); \
ip = luaL_checkstring(L, 2); \
} while (0); \
#define set_search_result(L, data) \
do { \
lua_newtable(L); \
lua_pushinteger(L, data.city_id); \
lua_setfield(L, -2, "city_id"); \
lua_pushfstring(L, "%s", data.region); \
lua_setfield(L, -2, "region"); \
} while (0); \
/** ip2region_memory_search wrapper */
static int lua_ip2region_memory_search(lua_State *L)
{
ip2region_entry *self;
const char *addr;
datablock_entry data;
// luaL_argcheck(L, lua_gettop(L) == 2, 2, "object and ip address needed");
// self = (ip2region_entry *) luaL_checkudata(L, 1, L_METATABLE_NAME);
// addr = luaL_checkstring(L, 2);
/* Check and get the search parameters */
get_search_args(L, self, addr);
/* do the memory search */
if ( ip2region_memory_search(self, ip2long(addr), &data) == 0 ) {
lua_pushnil(L);
return 1;
}
// lua_newtable(L);
// lua_pushinteger(L, data.city_id);
// lua_setfield(L, -2, "city_id");
// lua_pushfstring(L, "%s", data.region);
// lua_setfield(L, -2, "region");
set_search_result(L, data);
return 1;
}
/** ip2region_binary_search wrapper */
static int lua_ip2region_binary_search(lua_State *L)
{
ip2region_entry *self;
const char *addr;
datablock_entry data;
/* Check and get the search parameters */
get_search_args(L, self, addr);
/* Do the binary search */
if ( ip2region_binary_search(self, ip2long(addr), &data) == 0 ) {
lua_pushnil(L);
return 1;
}
/* Set the return value */
set_search_result(L, data);
return 1;
}
/** ip2region_btree_search wrapper */
static int lua_ip2region_btree_search(lua_State *L)
{
ip2region_entry *self;
const char *addr;
datablock_entry data;
/* Check and get the search parameters */
get_search_args(L, self, addr);
/* Do the btree search */
if ( ip2region_btree_search(self, ip2long(addr), &data) == 0 ) {
lua_pushnil(L);
return 1;
}
/* Set the return value */
set_search_result(L, data);
return 1;
}
/** ip2long wrapper */
static int lua_ip2long(lua_State *L)
{
int argc;
const char *addr;
uint_t ipval;
argc = lua_gettop(L);
if ( argc == 1 ) {
addr = luaL_checkstring(L, 1);
} else {
luaL_checkudata(L, 1, L_METATABLE_NAME);
addr = luaL_checkstring(L, 2);
}
if ( (ipval = ip2long(addr)) == 0 ) {
lua_pushnil(L);
return 1;
}
lua_pushinteger(L, (ipval & 0x7FFFFFFF));
return 1;
}
static int lua_ip2region_tostring(lua_State *L)
{
ip2region_entry *self;
self = (ip2region_entry *) luaL_checkudata(L, 1, L_METATABLE_NAME);
/* Push the string to return to lua */
lua_pushfstring(L,
"dbFile=%s, headerLen=%d, fristIndexPtr=%d, lastIndexPtr=%d, totalBlocks=%d",
self->dbFile, self->headerLen, self->firstIndexPtr,
self->lastIndexPtr, self->totalBlocks
);
return 1;
}
/** module method array */
static const struct luaL_Reg ip2region_methods[] = {
// { "new", lua_ip2region_new },
// { "ip2long", lua_ip2long },
{ "memorySearch", lua_ip2region_memory_search },
{ "binarySearch", lua_ip2region_binary_search },
{ "btreeSearch", lua_ip2region_btree_search },
{ "close", lua_ip2region_destroy },
{ "__gc", lua_ip2region_destroy },
{ "__tostring", lua_ip2region_tostring },
{ NULL, NULL },
};
/** module function array */
static const struct luaL_Reg ip2region_functions[] = {
{ "new", lua_ip2region_new },
{ "ip2long", lua_ip2long },
{ NULL, NULL }
};
/** module open function interface */
int luaopen_Ip2region(lua_State *L)
{
/* Create a metatable and push it onto the stack */
luaL_newmetatable(L, L_METATABLE_NAME);
/* Duplicate the metatable on the stack */
lua_pushvalue(L, -1);
/* Pop the first metatable off the stack
* and assign it to the __index of the second one.
* so we set the metatable to the table itself.
*/
lua_setfield(L, -2, "__index");
/* Set the methods fo the metatable that could and should be
* access via object:func in lua block
*/
luaL_setfuncs(L, ip2region_methods, 0);
luaL_setfuncs(L, ip2region_functions, 0);
/* Finally register the object.func functions
* into the table witch at the top of the stack */
// luaL_newlib(L, ip2region_functions);
return 1;
}

View File

@ -0,0 +1,86 @@
--[[
ip2region lua client test script
@author chenxin<chenxin619315@gmail.com>
]]--
-- check the command line arguments
if ( not arg[1] ) then
print([[
Usage: lua testSearcher.lua [ip2region db file] [algorithm]
+-Optional Algorithm: binary, b-tree, memory]]);
os.exit();
end
local Ip2region = require "Ip2region";
-- local cjson = require "cjson";
-- local socket = require "socket";
-- check and parse the dbFile and the method algorithm
-- Create a new ip2region object by the new interface
local ip2region = Ip2region.new(arg[1]);
-- reset the dbFile by the follow two ways:
-- ip2region.dbFile = arg[1];
-- ip2region:setDbFile(arg[1]);
local algorithm = "btree";
if ( arg[2] ~= nil ) then
local arg_2 = string.lower(arg[2]);
if ( arg_2 ~= "binary" and arg_2 ~= "memory" ) then
algorithm = "binary";
elseif ( arg_2 == "memory" ) then
algorithm = "memory";
end
end
-- local data = searcher:memorySearch("120.79.17.142");
-- local data = searcher:binarySearch("120.79.17.142");
-- local data = searcher:btreeSearch("120.79.17.142");
print("initializing " .. algorithm ..[[
+----------------------------------+
| ip2region test script |
| Author: chenxin619315@gmail.com |
| Type 'quit' to exit program |
+----------------------------------+]]
);
while ( true ) do
io.write("ip2region>> ");
io.input(io.stdin);
local line = io.read();
if ( line == nil ) then
-- do nothing
break;
elseif ( line == "quit" ) then
break;
elseif ( Ip2region.ip2long(line) == nil ) then
print("Invalid ip address=", line);
else
local data;
local s_time = os.clock();
if ( algorithm == "btree" ) then
data = ip2region:btreeSearch(line);
elseif ( algorithm == "binary" ) then
data = ip2region:binarySearch(line);
elseif ( algorithm == "memory" ) then
data = ip2region:memorySearch(line);
end
local cost_time = (os.clock() - s_time) * 1000; -- to millseconds
if ( data == nil ) then
io.write("Failed for ip=", line, " is it a valid ip address ?");
else
io.write(string.format("%u|%s in %5f millseconds\n", data.city_id, data.region, cost_time));
end
end
end
-- close the object
-- Also the lua gc will invoke it automatically
-- print(ip2region);
ip2region:close();

3
binding/nginx/README.md Normal file
View File

@ -0,0 +1,3 @@
# 说明
- 编译本模块时,请手动链接对../c/ip2region.c文件的依赖。

44
binding/nginx/config Normal file
View File

@ -0,0 +1,44 @@
ngx_addon_name="ngx_ip2region_module"
IP2REGION_CORE_MODULES=" \
ngx_ip2region_module \
"
IP2REGION_DEPS=" \
$ngx_addon_dir/ip2region.h \
$ngx_addon_dir/ngx_ip2region.h \
"
IP2REGION_CORE_SRCS=" \
$ngx_addon_dir/ip2region.c \
$ngx_addon_dir/ngx_ip2region_module.c \
"
if [ -f auto/module ] ; then
ngx_module_incs=$ngx_addon_dir
ngx_module_deps=$IP2REGION_DEPS
if [ $ngx_module_link = DYNAMIC ] ; then
ngx_module_name="$IP2REGION_CORE_MODULES"
ngx_module_srcs="$IP2REGION_CORE_SRCS"
. auto/module
else
ngx_module_type=CORE
ngx_module_name=$IP2REGION_CORE_MODULES
ngx_module_srcs=$IP2REGION_CORE_SRCS
. auto/module
fi
else
CORE_MODULES="$CORE_MODULES $IP2REGION_CORE_MODULES"
NGX_ADDON_DEPS="$NGX_ADDON_DEPS $IP2REGION_DEPS"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $IP2REGION_CORE_SRCS"
CFLAGS="$CFLAGS -I$ngx_addon_dir "
fi

View File

@ -0,0 +1,23 @@
#ifndef __NGX_IP2REGION_H__
#define __NGX_IP2REGION_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "../c/ip2region.h"
typedef struct ngx_ip2region_conf_s {
ngx_str_t db_file;
ngx_uint_t algo;
} ngx_ip2region_conf_t;
typedef uint_t (*search_func_ptr)(ip2region_t, uint_t, datablock_t);
extern ngx_int_t ngx_ip2region_search(ngx_str_t *addr_text, ngx_str_t *isp, ngx_str_t *city);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,260 @@
#include <ngx_config.h>
#include <ngx_core.h>
#include "ngx_ip2region.h"
static void *ngx_ip2region_create_conf(ngx_cycle_t *cycle);
static char *ngx_ip2region_init_conf(ngx_cycle_t *cycle, void *conf);
static ngx_int_t ngx_ip2region_init_process(ngx_cycle_t *cycle);
static void ngx_ip2region_exit_process(ngx_cycle_t *cycle);
static ip2region_entry g_ip2region_entry;
search_func_ptr ip2region_search_func;
#define NGX_IP2REGION_MEMORY 0
#define NGX_IP2REGION_BINARY 1
#define NGX_IP2REGION_BTREE 2
static ngx_conf_enum_t ngx_ip2region_search_algo[] = {
{ ngx_string("memory"), NGX_IP2REGION_MEMORY },
{ ngx_string("binary"), NGX_IP2REGION_BINARY },
{ ngx_string("btree"), NGX_IP2REGION_BTREE},
{ ngx_null_string, 0 }
};
static ngx_command_t ngx_ip2region_commands[] = {
{
ngx_string("ip2region_db_file"),
NGX_MAIN_CONF | NGX_DIRECT_CONF | NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
0,
offsetof(ngx_ip2region_conf_t, db_file),
NULL
},
{
ngx_string("ip2region_algo"),
NGX_MAIN_CONF | NGX_DIRECT_CONF | NGX_CONF_TAKE1,
ngx_conf_set_enum_slot,
0,
offsetof(ngx_ip2region_conf_t, algo),
&ngx_ip2region_search_algo
},
ngx_null_command
};
static ngx_core_module_t ngx_ip2region_module_ctx = {
ngx_string("ip2region"),
ngx_ip2region_create_conf,
ngx_ip2region_init_conf,
};
ngx_module_t ngx_ip2region_module = {
NGX_MODULE_V1,
&ngx_ip2region_module_ctx, /* module context */
ngx_ip2region_commands, /* module directives */
NGX_CORE_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_ip2region_init_process, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
ngx_ip2region_exit_process, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static void *
ngx_ip2region_create_conf(ngx_cycle_t *cycle)
{
ngx_ip2region_conf_t *ip2region_conf;
ip2region_conf = ngx_pcalloc(cycle->pool, sizeof(ngx_ip2region_conf_t));
if(ip2region_conf == NULL) {
return NULL;
}
ip2region_conf->algo = NGX_CONF_UNSET_UINT;
return ip2region_conf;
}
static char *
ngx_ip2region_init_conf(ngx_cycle_t *cycle, void *conf)
{
ngx_ip2region_conf_t *ip2region_conf = (ngx_ip2region_conf_t *)conf;
if(ip2region_conf->db_file.len == 0) {
//ngx_str_set(&ip2region_conf->db_file, "conf/ip2region.db");
return NGX_CONF_OK;
}
if(ngx_conf_full_name(cycle, &ip2region_conf->db_file, 0) != NGX_OK) {
return NGX_CONF_ERROR;
}
ngx_log_debug1(NGX_LOG_INFO, ngx_cycle->log, 0, "%V", &ip2region_conf->db_file);
if(ip2region_conf->algo == NGX_CONF_UNSET_UINT) {
ip2region_conf->algo = NGX_IP2REGION_MEMORY;
}
return NGX_CONF_OK;
}
static ngx_int_t
ngx_ip2region_init_process(ngx_cycle_t *cycle)
{
ngx_ip2region_conf_t *conf = (ngx_ip2region_conf_t *)ngx_get_conf(ngx_cycle->conf_ctx, ngx_ip2region_module);
if(conf->db_file.len == 0) {
ngx_log_debug0(NGX_LOG_INFO, ngx_cycle->log, 0, "ip2region not init");
return NGX_OK;
}
if(!ip2region_create(&g_ip2region_entry, (char *)conf->db_file.data)) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "ip2region create failed: %V", &conf->db_file);
return NGX_ERROR;
}
switch(conf->algo) {
default:
case NGX_IP2REGION_MEMORY:
ip2region_search_func = ip2region_memory_search;
break;
case NGX_IP2REGION_BINARY:
ip2region_search_func = ip2region_binary_search;
break;
case NGX_IP2REGION_BTREE:
ip2region_search_func = ip2region_btree_search;
break;
}
ngx_log_error(NGX_LOG_INFO, ngx_cycle->log, 0, "ip2region create succcess: algo=%ui", conf->algo);
return NGX_OK;
}
static void
ngx_ip2region_exit_process(ngx_cycle_t *cycle)
{
ip2region_destroy(&g_ip2region_entry);
ngx_log_debug0(NGX_LOG_INFO, ngx_cycle->log, 0, "ip2region destroy");
}
static ngx_inline u_char *
ngx_strrchr(u_char *first, u_char *p, u_char c)
{
while(p >= first) {
if(*p == c) {
return p;
}
p--;
}
return NULL;
}
static ngx_inline uint32_t
ngx_inet_aton(u_char *data, ngx_uint_t len)
{
long long result = 0;
long long temp = 0;
u_char *last = data + len;
int point = 0;
while(data <= last) {
if(*data == '.' || data == last) {
if(temp > 255) {
return NGX_ERROR;
}
result = (result << 8) + temp;
temp = 0;
++point;
if(point == 4) {
break;
}
} else if(*data <= '9' && *data >= '0') {
temp = temp * 10 + (*data - '0');
} else {
return NGX_ERROR;
}
++data;
}
if(point != 4) {
return NGX_ERROR;
}
return result;
}
ngx_int_t
ngx_ip2region_search(ngx_str_t *addr_text, ngx_str_t *isp, ngx_str_t *city)
{
if(g_ip2region_entry.dbHandler == NULL) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "search error ip %V, ip2region doesn't create", addr_text);
return NGX_ERROR;
}
uint32_t addr_binary = ngx_inet_aton(addr_text->data, addr_text->len);
if(addr_binary == (uint32_t)NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "search error ip %V: bad ip address", addr_text);
return NGX_ERROR;
}
datablock_entry datablock;
ngx_memzero(&datablock, sizeof(datablock));
uint_t rc = ip2region_search_func(&g_ip2region_entry, (uint_t)addr_binary, &datablock);
if(!rc) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "%V search failed", addr_text);
return NGX_ERROR;
}
ngx_log_debug2(NGX_LOG_INFO, ngx_cycle->log, 0, "%V: %s", addr_text, datablock.region);
//城市Id 国家|区域|省份|城市|ISP
u_char *end = (u_char *)(datablock.region + ngx_strlen(datablock.region));
u_char *pos = ngx_strrchr((u_char *)datablock.region, end, '|');
if(!pos) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "search isp failed");
return NGX_ERROR;
}
u_char isp_buffer[64] = {0};
ngx_snprintf(isp_buffer, sizeof(isp_buffer), "%*s%Z", end - pos, pos + 1);
ngx_log_debug3(NGX_LOG_INFO, ngx_cycle->log, 0, "%V: %s [%s]", addr_text, datablock.region, (char *)isp_buffer);
if(city) {
end = pos - 1;
u_char *pos_city = ngx_strrchr((u_char *)datablock.region, end, '|');
if(!pos_city) {
ngx_log_debug0(NGX_LOG_ERR, ngx_cycle->log, 0, "search city failed");
return NGX_ERROR;
}
u_char city_buffer[64] = {0};
ngx_snprintf(city_buffer, sizeof(city_buffer), "%*s%Z", end - pos_city, pos_city + 1);
}
return NGX_OK;
}

View File

@ -1 +1,2 @@
node_modules/
data/

View File

@ -1,5 +1,35 @@
# nodejs 客户端
官方维护的 ip2region, 每次数据更新后会更新到 npm
## Install
**node 版本 : >= 6.0.0**
```
npm install node-ip2region --save
```
## 已测试通过的 node 版本列表
```
6.0.0
6.11.2
8.0.0
10.0.0
12.0.0
```
## Example
```
const searcher = require('node-ip2region').create();
searcher.btreeSearchSync('xxx.xxx.xxx.xxx')
// => { city: 2163, region: '中国|0|广东省|深圳市|联通' }
```
## 实现情况:
现已实现同步和异步查询,具体使用方法可以参考 `nodejs\tests\constructorTest.spec.js` 和`nodejs\tests\createTest.spec.js`。
@ -9,7 +39,7 @@
你可以任意修改代码,但必须确保通过全部的单元测试。要保证通过全部的单元测试,请在 Nodejs 控制台下切换到 nodejs 目录:
1在此之前请先运行 `npm i` 确保你已经安装了各类初始化第三方工具。
2然后运行 `npm run coverage` 确保你的代码可以通过全部测试(必要时可以添加测试)同时保证代码覆盖率都是绿色80%以上)
2然后运行 `npm run coverage` 确保你的代码可以通过全部测试(必要时可以添加测试)。
```bash
D:\Projects\ip2region\binding\nodejs>npm run coverage
@ -53,4 +83,21 @@ Tests: 14 passed, 14 total
Snapshots: 168 passed, 168 total
Time: 1.792s
Ran all test suites.
```
```
3使用benchmark测试结果如下
```bash
D:\Projects\ip2region\binding\nodejs>node D:\Projects\ip2region\binding\nodejs\tests\benchmarkTests\main.js
MemorySearchSync x 55,969 ops/sec ±2.22% (90 runs sampled)
BinarySearchSync x 610 ops/sec ±5.41% (77 runs sampled)
BtreeSearchSync x 2,439 ops/sec ±6.93% (69 runs sampled)
MemorySearch x 2,924 ops/sec ±0.67% (85 runs sampled)
BinarySearch x 154 ops/sec ±2.20% (69 runs sampled)
BtreeSearch x 294 ops/sec ±2.58% (76 runs sampled)
Rand Name Time (in milliseconds)
1 MemorySearchSync 0.018
2 MemorySearch 0.342
3 BtreeSearchSync 0.410
4 BinarySearchSync 1.639
5 BtreeSearch 3.407
6 BinarySearch 6.497
```

11
binding/nodejs/deploy.sh Executable file
View File

@ -0,0 +1,11 @@
#!/bin/sh
echo 'create dir'
if [ ! -d "data" ]; then
mkdir data
fi
echo "copy db"
cp '../../data/ip2region.db' './data/'
echo "npm publish"
npm publish

View File

@ -8,6 +8,8 @@
* @author dongwei<maledong_github@outlook.com>
*/
const fs = require('fs');
const path = require('path');
const DEFAULT_DB_PATH = path.join(__dirname,'./data/ip2region.db') ;
//#region Private Functions
/**
@ -70,7 +72,7 @@ class IP2Region {
//#region Private Functions
[CalTotalBlocks]() {
const superBlock = new Buffer(8);
const superBlock = Buffer.alloc(8);
fs.readSync(this.dbFd, superBlock, 0, 8, 0);
this.firstIndexPtr = _getLong(superBlock, 0);
this.lastIndexPtr = _getLong(superBlock, 4);
@ -99,10 +101,10 @@ class IP2Region {
}
[ReadData](dataPos, callBack) {
if (dataPos == 0) return callBack(null,null);
if (dataPos == 0) return callBack(null, null);
const dataLen = (dataPos >> 24) & 0xff;
dataPos = dataPos & 0x00ffffff;
const dataBuffer = new Buffer(dataLen);
const dataBuffer = Buffer.alloc(dataLen);
fs.read(this.dbFd, dataBuffer, 0, dataLen, dataPos, (err, result) => {
if (err) {
@ -120,7 +122,7 @@ class IP2Region {
if (dataPos == 0) return null;
const dataLen = (dataPos >> 24) & 0xff;
dataPos = dataPos & 0x00ffffff;
const dataBuffer = new Buffer(dataLen);
const dataBuffer = Buffer.alloc(dataLen);
fs.readSync(this.dbFd, dataBuffer, 0, dataLen, dataPos);
@ -134,7 +136,7 @@ class IP2Region {
//#region Static Functions
// Single Instance
static create(dbPath) {
static create(dbPath = DEFAULT_DB_PATH) {
let existInstance = _globalInstances.get(dbPath);
if (existInstance == null) {
existInstance = new IP2Region({ dbPath: dbPath });
@ -157,6 +159,10 @@ class IP2Region {
const { dbPath } = options;
// Keep for MemorySearch
this.totalInMemoryBytesSize = fs.statSync(dbPath).size;
this.totalInMemoryBytes = null;
this.dbFd = fs.openSync(dbPath, 'r');
this.dbPath = dbPath;
@ -166,7 +172,7 @@ class IP2Region {
this.totalBlocks = this.firstIndexPtr = this.lastIndexPtr = 0;
this[CalTotalBlocks]();
this.headerIndexBuffer = new Buffer(TOTAL_HEADER_LENGTH);
this.headerIndexBuffer = Buffer.alloc(TOTAL_HEADER_LENGTH);
this.headerSip = [];
this.headerPtr = [];
this.headerLen = 0;
@ -196,7 +202,7 @@ class IP2Region {
let high = this.totalBlocks;
let pos = 0;
let sip = 0;
const indexBuffer = new Buffer(12);
const indexBuffer = Buffer.alloc(12);
// binary search
while (low <= high) {
@ -235,7 +241,7 @@ class IP2Region {
let high = this.totalBlocks;
let pos = 0;
let sip = 0;
const indexBuffer = new Buffer(12);
const indexBuffer = Buffer.alloc(12);
const _self = this;
// Because `while` is a sync method, we have to convert this to a recursive loop
@ -338,7 +344,7 @@ class IP2Region {
// second search (in index)
const blockLen = eptr - sptr;
const blockBuffer = new Buffer(blockLen + INDEX_BLOCK_LENGTH);
const blockBuffer = Buffer.alloc(blockLen + INDEX_BLOCK_LENGTH);
fs.readSync(
this.dbFd,
blockBuffer,
@ -436,7 +442,7 @@ class IP2Region {
// second search (in index)
const blockLen = eptr - sptr;
const blockBuffer = new Buffer(blockLen + INDEX_BLOCK_LENGTH);
const blockBuffer = Buffer.alloc(blockLen + INDEX_BLOCK_LENGTH);
low = 0;
high = blockLen / INDEX_BLOCK_LENGTH;
@ -488,7 +494,141 @@ class IP2Region {
_innerAsyncWhile();
}
//#endregion
}
/**
* Sync of MemorySearch.
* @param {String} ip
*/
memorySearchSync(ip) {
module.exports = IP2Region;
ip = _ip2long(ip);
if (this.totalInMemoryBytes === null) {
this.totalInMemoryBytes = Buffer.alloc(this.totalInMemoryBytesSize);
fs.readSync(this.dbFd, this.totalInMemoryBytes, 0, this.totalInMemoryBytesSize, 0);
this.firstIndexPtr = _getLong(this.totalInMemoryBytes, 0);
this.lastIndexPtr = _getLong(this.totalInMemoryBytes, 4);
this.totalBlocks = ((this.lastIndexPtr - this.firstIndexPtr) / INDEX_BLOCK_LENGTH) | 0 + 1;
}
let l = 0, h = this.totalBlocks;
let sip = 0;
let m = 0, p = 0;
while (l <= h) {
m = (l + h) >> 1;
p = (this.firstIndexPtr + m * INDEX_BLOCK_LENGTH) | 0;
sip = _getLong(this.totalInMemoryBytes, p);
if (ip < sip) {
h = m - 1;
}
else {
sip = _getLong(this.totalInMemoryBytes, p + 4);
if (ip > sip) {
l = m + 1;
}
else {
sip = _getLong(this.totalInMemoryBytes, p + 8);
//not matched
if (sip === 0) return null;
//get the data
let dataLen = ((sip >> 24) & 0xFF) | 0;
let dataPtr = ((sip & 0x00FFFFFF)) | 0;
let city = _getLong(this.totalInMemoryBytes, dataPtr);
const bufArray = new Array();
for (let startPos = dataPtr + 4, i = startPos; i < startPos + dataLen - 4; ++i) {
bufArray.push(this.totalInMemoryBytes[i]);
}
const region = Buffer.from(bufArray, 0).toString();
return { city, region };
}
}
}
}
/**
* Async of MemorySearch.
* @param {String} ip
*/
memorySearch(ip, callBack) {
let _ip = _ip2long(ip);
let l = 0, h = this.totalBlocks;
let sip = 0;
let m = 0, p = 0;
let self = this;
function _innerMemorySearchLoop() {
if (l <= h) {
m = (l + h) >> 1;
p = (self.firstIndexPtr + m * INDEX_BLOCK_LENGTH) | 0;
sip = _getLong(self.totalInMemoryBytes, p);
if (_ip < sip) {
h = m - 1;
setImmediate(_innerMemorySearchLoop);
}
else {
sip = _getLong(self.totalInMemoryBytes, p + 4);
if (_ip > sip) {
l = m + 1;
setImmediate(_innerMemorySearchLoop);
}
else {
sip = _getLong(self.totalInMemoryBytes, p + 8);
//not matched
if (sip === 0) return callBack(null, null);
//get the data
let dataLen = ((sip >> 24) & 0xFF) | 0;
let dataPtr = ((sip & 0x00FFFFFF)) | 0;
let city = _getLong(self.totalInMemoryBytes, dataPtr);
const bufArray = new Array();
for (let startPos = dataPtr + 4, i = startPos; i < startPos + dataLen - 4; ++i) {
bufArray.push(self.totalInMemoryBytes[i]);
}
const region = Buffer.from(bufArray).toString();
callBack(null, { city, region });
}
}
}
else {
callBack(null, null);
}
}
if (this.totalInMemoryBytes === null) {
this.totalInMemoryBytes = Buffer.alloc(this.totalInMemoryBytesSize);
fs.read(this.dbFd, this.totalInMemoryBytes, 0, this.totalInMemoryBytesSize, 0, (err) => {
if (err) {
callBack(err, null);
}
else {
this.firstIndexPtr = _getLong(this.totalInMemoryBytes, 0);
this.lastIndexPtr = _getLong(this.totalInMemoryBytes, 4);
this.totalBlocks = ((this.lastIndexPtr - this.firstIndexPtr) / INDEX_BLOCK_LENGTH) | 0 + 1;
_innerMemorySearchLoop();
}
});
}
else {
_innerMemorySearchLoop();
}
}
}
//#endregion
module.exports = IP2Region;

3930
binding/nodejs/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,15 +1,16 @@
{
"name": "ip2region",
"version": "0.0.1",
"description": "ip database ",
"name": "node-ip2region",
"version": "1.0.2",
"description": "official nodejs client of ip2region",
"main": "ip2region.js",
"scripts": {
"test": "jest",
"coverage":"npm run test && jest --coverage"
"coverage": "npm run test && jest --coverage"
},
"files": ["data/"],
"repository": {
"type": "git",
"url": "https://github.com/lionsoul2016/ip2region"
"url": "https://github.com/lionsoul2014/ip2region"
},
"keywords": [
"ip2region",
@ -19,10 +20,14 @@
"author": "dongyado",
"license": "ISC",
"bugs": {
"url": "https://github.com/lionsoul2016/ip2region/issues"
"url": "https://github.com/lionsoul2014/ip2region/issues"
},
"homepage": "https://github.com/lionsoul2016/ip2region",
"homepage": "https://github.com/lionsoul2014/ip2region",
"devDependencies": {
"jest": "^19.0.2"
"jest": "^19.0.2",
"benchmark": "^2.1.4"
},
"engines": {
"node" : ">=6.0.0"
}
}

View File

@ -0,0 +1,94 @@
const Benchmark = require('benchmark');
const suite = new Benchmark.Suite();
const searcher = require('../../ip2region').create('../../data/ip2region.db');
const testDatas = require('../utils/testData');
const asyncFor = require('../utils/asyncFor');
suite.add("MemorySearchSync", () => {
for (let i = 0; i < testDatas.length; ++i) {
searcher.memorySearchSync(testDatas[i]);
}
})
.add("BinarySearchSync", () => {
for (let i = 0; i < testDatas.length; ++i) {
searcher.binarySearchSync(testDatas[i]);
}
})
.add("BtreeSearchSync", () => {
for (let i = 0; i < testDatas.length; ++i) {
searcher.btreeSearchSync(testDatas[i]);
}
})
.add("MemorySearch", {
defer: true,
fn: function (completeCallBack) {
asyncFor(testDatas,
(v, c) => {
searcher.memorySearch(v, () => {
c();
});
},
() => {
completeCallBack.resolve();
});
}
})
.add("BinarySearch", {
defer: true,
fn: function (completeCallBack) {
asyncFor(testDatas,
(v, c) => {
searcher.binarySearch(v, () => {
c();
});
},
() => {
completeCallBack.resolve();
});
}
})
.add("BtreeSearch", {
defer: true,
fn: function (completeCallBack) {
asyncFor(testDatas,
(v, c) => {
searcher.btreeSearch(v, () => {
c();
});
},
() => {
completeCallBack.resolve();
});
}
})
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
let results = new Array();
for (let prop in this) {
if (!isNaN(prop)) {
const eachResult = {
name: this[prop].name,
mean: this[prop].stats.mean * 1000, //second => millisecond
moe: this[prop].stats.moe,
rme: this[prop].stats.rme,
sem: this[prop].stats.sem
}
results.push(eachResult);
}
}
results = results.sort((a, b) => { return a.mean - b.mean });
console.log(`Rand\t${'Name'.padEnd(20)}Time (in milliseconds)`);
let id = 1;
for (let r of results) {
console.log(`${id++}\t${r.name.padEnd(20)}${r.mean.toFixed(3)}`);
}
})
.run({ async: true });

View File

@ -1,7 +1,7 @@
// This test is used for tesing of a static function `create` of IP2Region
const IP2Region = require('../ip2region');
const testIps = require('./utils/testData');
const asyncFor = require('./utils/asyncFor');
const IP2Region = require('../../ip2region');
const testIps = require('../utils/testData');
const asyncFor = require('../utils/asyncFor');
describe('Constructor Test', () => {
let instance;
@ -26,6 +26,11 @@ describe('Constructor Test', () => {
}
});
test('memorySearchSync query', () => {
for (const ip of testIps) {
expect(instance.memorySearchSync(ip)).toMatchSnapshot();
}
});
//#region callBack
test('binarySearch query', (done) => {
@ -52,10 +57,22 @@ describe('Constructor Test', () => {
() => { done() });
});
test('memorySearch query', (done) => {
asyncFor(testIps,
(value, continueCallBack) => {
instance.memorySearch(value, (err, result) => {
expect(err).toBe(null);
expect(result).toMatchSnapshot();
continueCallBack();
});
},
() => { done() });
});
//#endregion
//#region Async Promisify test
const node_ver = require('./utils/fetchMainVersion');
const node_ver = require('../utils/fetchMainVersion');
// If we have Nodejs >= 8, we now support `async` and `await`
if (node_ver >= 8) {
@ -90,6 +107,19 @@ describe('Constructor Test', () => {
};
const asyncMemorySearch = async (ip) => {
return new Promise((succ, fail) => {
instance.memorySearch(ip, (err, result) => {
if (err) {
fail(err);
}
else {
succ(result);
}
});
});
}
test('async binarySearch query', async () => {
for (let i = 0; i < testIps.length; ++i) {
const result = await asyncBinarySearch(testIps[i]);
@ -103,6 +133,13 @@ describe('Constructor Test', () => {
expect(result).toMatchSnapshot();
}
});
test('async memorySearch query', async () => {
for (let i = 0; i < testIps.length; ++i) {
const result = await asyncMemorySearch(testIps[i]);
expect(result).toMatchSnapshot();
}
});
}
//#endregion
});

View File

@ -1,7 +1,7 @@
// This test is used for tesing of a static function `create` of IP2Region
const IP2Region = require('../ip2region');
const testIps = require('./utils/testData');
const asyncFor = require('./utils/asyncFor');
const IP2Region = require('../../ip2region');
const testIps = require('../utils/testData');
const asyncFor = require('../utils/asyncFor');
describe('Create Test', () => {
let instance;
@ -26,6 +26,11 @@ describe('Create Test', () => {
}
});
test('memorySearchSync query', () => {
for (const ip of testIps) {
expect(instance.memorySearchSync(ip)).toMatchSnapshot();
}
});
//#region callBack
test('binarySearch query', (done) => {
@ -52,10 +57,23 @@ describe('Create Test', () => {
() => { done() });
});
test('memorySearch query', (done) => {
asyncFor(testIps,
(value, continueCallBack) => {
instance.memorySearch(value, (err, result) => {
expect(err).toBe(null);
expect(result).toMatchSnapshot();
continueCallBack();
});
},
() => { done() });
});
//#endregion
//#region Async Promisify test
const node_ver = require('./utils/fetchMainVersion');
const node_ver = require('../utils/fetchMainVersion');
// If we have Nodejs >= 8, we now support `async` and `await`
if (node_ver >= 8) {
@ -90,6 +108,19 @@ describe('Create Test', () => {
};
const asyncMemorySearch = async (ip) => {
return new Promise((succ, fail) => {
instance.memorySearch(ip, (err, result) => {
if (err) {
fail(err);
}
else {
succ(result);
}
});
});
}
test('async binarySearch query', async () => {
for (let i = 0; i < testIps.length; ++i) {
const result = await asyncBinarySearch(testIps[i]);
@ -103,6 +134,13 @@ describe('Create Test', () => {
expect(result).toMatchSnapshot();
}
});
test('async memorySearch query', async () => {
for (let i = 0; i < testIps.length; ++i) {
const result = await asyncMemorySearch(testIps[i]);
expect(result).toMatchSnapshot();
}
});
}
//#endregion
});

View File

@ -1,6 +1,6 @@
// This test is used for tesing of exceptions
const IP2Region = require('../ip2region');
const IP2Region = require('../../ip2region');
describe('Constructor Test', () => {
let instance;

View File

@ -10,11 +10,11 @@ acorn-globals@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/acorn-globals/-/acorn-globals-3.1.0.tgz#fd8270f71fbb4996b004fa880ee5d46573a731bf"
dependencies:
acorn "^4.0.4"
acorn "^5.7.4"
acorn@^4.0.4:
version "4.0.11"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-4.0.11.tgz#edcda3bd937e7556410d42ed5860f67399c794c0"
acorn@^5.7.4:
version "5.7.4"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.7.4.tgz#edcda3bd937e7556410d42ed5860f67399c794c0"
ajv@^4.9.1:
version "4.11.6"
@ -23,14 +23,6 @@ ajv@^4.9.1:
co "^4.6.0"
json-stable-stringify "^1.0.1"
align-text@^0.1.1, align-text@^0.1.3:
version "0.1.4"
resolved "https://registry.yarnpkg.com/align-text/-/align-text-0.1.4.tgz#0cd90a561093f35d0a99256c22b7069433fad117"
dependencies:
kind-of "^3.0.2"
longest "^1.0.1"
repeat-string "^1.5.2"
amdefine@>=0.0.4:
version "1.0.1"
resolved "https://registry.yarnpkg.com/amdefine/-/amdefine-1.0.1.tgz#4a5282ac164729e93619bcfd3ad151f817ce91f5"
@ -106,15 +98,11 @@ assert-plus@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/assert-plus/-/assert-plus-0.2.0.tgz#d74e1b87e7affc0db8aadb7021f3fe48101ab234"
async@^1.4.0:
version "1.5.2"
resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a"
async@^2.1.4:
version "2.3.0"
resolved "https://registry.yarnpkg.com/async/-/async-2.3.0.tgz#1013d1051047dd320fe24e494d5c66ecaf6147d9"
version "2.6.4"
resolved "https://registry.yarnpkg.com/async/-/async-2.6.4.tgz#706b7ff6084664cd7eae713f6f965433b5504221"
dependencies:
lodash "^4.14.0"
lodash "^4.17.14"
asynckit@^0.4.0:
version "0.4.0"
@ -151,9 +139,9 @@ babel-core@^6.0.0, babel-core@^6.24.1:
babel-types "^6.24.1"
babylon "^6.11.0"
convert-source-map "^1.1.0"
debug "^2.1.1"
debug "^2.6.9"
json5 "^0.5.0"
lodash "^4.2.0"
lodash "^4.17.13"
minimatch "^3.0.2"
path-is-absolute "^1.0.0"
private "^0.1.6"
@ -169,7 +157,7 @@ babel-generator@^6.18.0, babel-generator@^6.24.1:
babel-types "^6.24.1"
detect-indent "^4.0.0"
jsesc "^1.3.0"
lodash "^4.2.0"
lodash "^4.17.13"
source-map "^0.5.0"
trim-right "^1.0.1"
@ -220,7 +208,7 @@ babel-register@^6.24.1:
babel-runtime "^6.22.0"
core-js "^2.4.0"
home-or-tmp "^2.0.0"
lodash "^4.2.0"
lodash "^4.17.13"
mkdirp "^0.5.1"
source-map-support "^0.4.2"
@ -239,7 +227,7 @@ babel-template@^6.16.0, babel-template@^6.24.1:
babel-traverse "^6.24.1"
babel-types "^6.24.1"
babylon "^6.11.0"
lodash "^4.2.0"
lodash "^4.17.13"
babel-traverse@^6.18.0, babel-traverse@^6.24.1:
version "6.24.1"
@ -250,10 +238,10 @@ babel-traverse@^6.18.0, babel-traverse@^6.24.1:
babel-runtime "^6.22.0"
babel-types "^6.24.1"
babylon "^6.15.0"
debug "^2.2.0"
debug "^2.6.9"
globals "^9.0.0"
invariant "^2.2.0"
lodash "^4.2.0"
lodash "^4.17.13"
babel-types@^6.18.0, babel-types@^6.24.1:
version "6.24.1"
@ -261,7 +249,7 @@ babel-types@^6.18.0, babel-types@^6.24.1:
dependencies:
babel-runtime "^6.22.0"
esutils "^2.0.2"
lodash "^4.2.0"
lodash "^4.17.13"
to-fast-properties "^1.0.1"
babylon@^6.11.0, babylon@^6.13.0, babylon@^6.15.0:
@ -278,11 +266,18 @@ bcrypt-pbkdf@^1.0.0:
dependencies:
tweetnacl "^0.14.3"
benchmark@^2.1.4:
version "2.1.4"
resolved "https://registry.yarnpkg.com/benchmark/-/benchmark-2.1.4.tgz#09f3de31c916425d498cc2ee565a0ebf3c2a5629"
dependencies:
lodash "^4.17.4"
platform "^1.3.3"
boom@2.x.x:
version "2.10.1"
resolved "https://registry.yarnpkg.com/boom/-/boom-2.10.1.tgz#39c8918ceff5799f83f9492a848f625add0c766f"
dependencies:
hoek "2.x.x"
hoek "4.2.1"
brace-expansion@^1.0.0:
version "1.1.7"
@ -291,9 +286,9 @@ brace-expansion@^1.0.0:
balanced-match "^0.4.1"
concat-map "0.0.1"
braces@^1.8.2:
version "1.8.5"
resolved "https://registry.yarnpkg.com/braces/-/braces-1.8.5.tgz#ba77962e12dff969d6b76711e914b737857bf6a7"
braces@^2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/braces/-/braces-2.3.1.tgz#ba77962e12dff969d6b76711e914b737857bf6a7"
dependencies:
expand-range "^1.8.1"
preserve "^0.2.0"
@ -325,10 +320,6 @@ callsites@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/callsites/-/callsites-2.0.0.tgz#06eb84f00eea413da86affefacbffb36093b3c50"
camelcase@^1.0.2:
version "1.2.1"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-1.2.1.tgz#9bb5304d2e0b56698b2c758b08a3eaa9daa58a39"
camelcase@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-3.0.0.tgz#32fc4b9fcdaf845fcdf7e73bb97cac2261f0ab0a"
@ -337,13 +328,6 @@ caseless@~0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc"
center-align@^0.1.1:
version "0.1.3"
resolved "https://registry.yarnpkg.com/center-align/-/center-align-0.1.3.tgz#aa0d32629b6ee972200411cbd4461c907bc2b7ad"
dependencies:
align-text "^0.1.3"
lazy-cache "^1.0.3"
chalk@^1.1.0, chalk@^1.1.1, chalk@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98"
@ -358,14 +342,6 @@ ci-info@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-1.0.0.tgz#dc5285f2b4e251821683681c381c3388f46ec534"
cliui@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/cliui/-/cliui-2.1.0.tgz#4b475760ff80264c762c3a1719032e91c7fea0d1"
dependencies:
center-align "^0.1.1"
right-align "^0.1.1"
wordwrap "0.0.2"
cliui@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/cliui/-/cliui-3.2.0.tgz#120601537a916d29940f934da3b48d585a39213d"
@ -414,9 +390,9 @@ core-js@^2.4.0:
version "2.4.1"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.4.1.tgz#4de911e667b0eae9124e34254b53aea6fc618d3e"
cryptiles@2.x.x:
version "2.0.5"
resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8"
cryptiles@4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-4.1.2.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8"
dependencies:
boom "2.x.x"
@ -436,13 +412,13 @@ dashdash@^1.12.0:
dependencies:
assert-plus "^1.0.0"
debug@^2.1.1, debug@^2.2.0:
version "2.6.3"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.3.tgz#0f7eb8c30965ec08c72accfa0130c8b79984141d"
debug@^2.6.9:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#0f7eb8c30965ec08c72accfa0130c8b79984141d"
dependencies:
ms "0.7.2"
decamelize@^1.0.0, decamelize@^1.1.1:
decamelize@^1.1.1:
version "1.2.0"
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
@ -466,9 +442,9 @@ detect-indent@^4.0.0:
dependencies:
repeating "^2.0.0"
diff@^3.0.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.2.0.tgz#c9ce393a4b7cbd0b058a725c93df299027868ff9"
diff@^3.5.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#c9ce393a4b7cbd0b058a725c93df299027868ff9"
ecc-jsbn@~0.1.1:
version "0.1.1"
@ -523,7 +499,7 @@ exec-sh@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/exec-sh/-/exec-sh-0.2.0.tgz#14f75de3f20d286ef933099b2ce50a90359cef10"
dependencies:
merge "^1.1.3"
merge "^1.2.1"
expand-brackets@^0.1.4:
version "0.1.5"
@ -537,9 +513,9 @@ expand-range@^1.8.1:
dependencies:
fill-range "^2.1.0"
extend@~3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.0.tgz#5a474353b9f3353ddd8176dfd37b91c83a46f1d4"
extend@~3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.2.tgz#5a474353b9f3353ddd8176dfd37b91c83a46f1d4"
extglob@^0.3.1:
version "0.3.2"
@ -584,7 +560,7 @@ fill-range@^2.1.0:
dependencies:
is-number "^2.1.0"
isobject "^2.0.0"
randomatic "^1.1.3"
randomatic "^3.0.0"
repeat-element "^1.1.2"
repeat-string "^1.5.2"
@ -673,15 +649,16 @@ growly@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
handlebars@^4.0.3:
version "4.0.6"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.0.6.tgz#2ce4484850537f9c97a8026d5399b935c4ed4ed7"
handlebars@^4.1.2:
version "4.7.7"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.7.7.tgz#9ce33416aad02dbd6c8fafa8240d5d98004945a1"
dependencies:
async "^1.4.0"
optimist "^0.6.1"
source-map "^0.4.4"
minimist "^1.2.5"
neo-async "^2.6.0"
source-map "^0.6.1"
wordwrap "^1.0.0"
optionalDependencies:
uglify-js "^2.6"
uglify-js "^3.1.4"
har-schema@^1.0.5:
version "1.0.5"
@ -709,7 +686,7 @@ hawk@~3.1.3:
resolved "https://registry.yarnpkg.com/hawk/-/hawk-3.1.3.tgz#078444bd7c1640b0fe540d2c9b73d59678e8e1c4"
dependencies:
boom "2.x.x"
cryptiles "2.x.x"
cryptiles "4.1.2"
hoek "2.x.x"
sntp "1.x.x"
@ -717,6 +694,10 @@ hoek@2.x.x:
version "2.16.3"
resolved "https://registry.yarnpkg.com/hoek/-/hoek-2.16.3.tgz#20bb7403d3cea398e91dc4710a8ff1b8274a25ed"
hoek@4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/hoek/-/hoek-4.2.1.tgz#9634502aa12c445dd5a7c5734b572bb8738aacbb"
home-or-tmp@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/home-or-tmp/-/home-or-tmp-2.0.0.tgz#e36c3f2d2cae7d746a857e38d18d5f32a7882db8"
@ -725,8 +706,8 @@ home-or-tmp@^2.0.0:
os-tmpdir "^1.0.1"
hosted-git-info@^2.1.4:
version "2.4.2"
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.4.2.tgz#0076b9f46a270506ddbaaea56496897460612a67"
version "2.8.9"
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.9.tgz#dffc0bf9a21c02209090f2aa69429e1414daf3f9"
html-encoding-sniffer@^1.0.1:
version "1.0.1"
@ -740,7 +721,7 @@ http-signature@~1.1.0:
dependencies:
assert-plus "^0.2.0"
jsprim "^1.2.2"
sshpk "^1.7.0"
sshpk "^1.13.2"
iconv-lite@0.4.13:
version "0.4.13"
@ -875,7 +856,7 @@ istanbul-api@^1.1.0-alpha.1:
istanbul-lib-report "^1.0.0"
istanbul-lib-source-maps "^1.1.1"
istanbul-reports "^1.0.2"
js-yaml "^3.7.0"
js-yaml "^3.13.1"
mkdirp "^0.5.1"
once "^1.4.0"
@ -923,7 +904,7 @@ istanbul-reports@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/istanbul-reports/-/istanbul-reports-1.0.2.tgz#4e8366abe6fa746cc1cd6633f108de12cc6ac6fa"
dependencies:
handlebars "^4.0.3"
handlebars "^4.1.2"
jest-changed-files@^19.0.2:
version "19.0.2"
@ -979,7 +960,7 @@ jest-diff@^19.0.0:
resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-19.0.0.tgz#d1563cfc56c8b60232988fbc05d4d16ed90f063c"
dependencies:
chalk "^1.1.3"
diff "^3.0.0"
diff "^3.5.0"
jest-matcher-utils "^19.0.0"
pretty-format "^19.0.0"
@ -1137,9 +1118,9 @@ js-tokens@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7"
js-yaml@^3.7.0:
version "3.8.3"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.8.3.tgz#33a05ec481c850c8875929166fe1beb61c728766"
js-yaml@^3.13.1:
version "3.13.1"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.13.1.tgz#33a05ec481c850c8875929166fe1beb61c728766"
dependencies:
argparse "^1.0.7"
esprima "^3.1.1"
@ -1153,7 +1134,7 @@ jsdom@^9.11.0:
resolved "https://registry.yarnpkg.com/jsdom/-/jsdom-9.12.0.tgz#e8c546fffcb06c00d4833ca84410fed7f8a097d4"
dependencies:
abab "^1.0.3"
acorn "^4.0.4"
acorn "^5.7.4"
acorn-globals "^3.1.0"
array-equal "^1.0.0"
content-type-parser "^1.0.1"
@ -1166,7 +1147,7 @@ jsdom@^9.11.0:
request "^2.79.0"
sax "^1.2.1"
symbol-tree "^3.2.1"
tough-cookie "^2.3.2"
tough-cookie "^2.3.3"
webidl-conversions "^4.0.0"
whatwg-encoding "^1.0.1"
whatwg-url "^4.3.0"
@ -1213,10 +1194,6 @@ kind-of@^3.0.2:
dependencies:
is-buffer "^1.0.2"
lazy-cache@^1.0.3:
version "1.0.4"
resolved "https://registry.yarnpkg.com/lazy-cache/-/lazy-cache-1.0.4.tgz#a1d78fc3a50474cb80845d3b3b6e1da49a446e8e"
lcid@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/lcid/-/lcid-1.0.0.tgz#308accafa0bc483a3867b4b6f2b9506251d1b835"
@ -1251,13 +1228,9 @@ locate-path@^2.0.0:
p-locate "^2.0.0"
path-exists "^3.0.0"
lodash@^4.14.0, lodash@^4.2.0:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
longest@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097"
lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.4:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
loose-envify@^1.0.0:
version "1.3.1"
@ -1271,9 +1244,9 @@ makeerror@1.0.x:
dependencies:
tmpl "1.0.x"
merge@^1.1.3:
version "1.2.0"
resolved "https://registry.yarnpkg.com/merge/-/merge-1.2.0.tgz#7531e39d4949c281a66b8c5a6e0265e8b05894da"
merge@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/merge/-/merge-1.2.1.tgz#7531e39d4949c281a66b8c5a6e0265e8b05894da"
micromatch@^2.1.5, micromatch@^2.3.11:
version "2.3.11"
@ -1281,7 +1254,7 @@ micromatch@^2.1.5, micromatch@^2.3.11:
dependencies:
arr-diff "^2.0.0"
array-unique "^0.2.1"
braces "^1.8.2"
braces "^2.3.1"
expand-brackets "^0.1.4"
extglob "^0.3.1"
filename-regex "^2.0.0"
@ -1309,19 +1282,19 @@ minimatch@^3.0.2, minimatch@^3.0.3:
dependencies:
brace-expansion "^1.0.0"
minimist@0.0.8, minimist@~0.0.1:
version "0.0.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
minimist@1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.3.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"
minimist@^1.1.1:
version "1.2.0"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
minimist@^1.2.3, minimist@^1.2.5:
version "1.2.5"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602"
mkdirp@^0.5.1:
version "0.5.1"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903"
dependencies:
minimist "0.0.8"
minimist "1.2.3"
ms@0.7.2:
version "0.7.2"
@ -1331,6 +1304,10 @@ natural-compare@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
neo-async@^2.6.0:
version "2.6.2"
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.2.tgz#b4aafb93e3aeb2d8174ca53cf163ab7d7308305f"
node-int64@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
@ -1364,8 +1341,8 @@ number-is-nan@^1.0.0:
resolved "https://registry.yarnpkg.com/number-is-nan/-/number-is-nan-1.0.1.tgz#097b602b53422a522c1afb8790318336941a011d"
"nwmatcher@>= 1.3.9 < 2.0.0":
version "1.3.9"
resolved "https://registry.yarnpkg.com/nwmatcher/-/nwmatcher-1.3.9.tgz#8bab486ff7fa3dfd086656bbe8b17116d3692d2a"
version "1.4.4"
resolved "https://registry.yarnpkg.com/nwmatcher/-/nwmatcher-1.4.4.tgz#8bab486ff7fa3dfd086656bbe8b17116d3692d2a"
oauth-sign@~0.8.1:
version "0.8.2"
@ -1388,13 +1365,6 @@ once@^1.3.0, once@^1.4.0:
dependencies:
wrappy "1"
optimist@^0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/optimist/-/optimist-0.6.1.tgz#da3ea74686fa21a19a111c326e90eb15a0196686"
dependencies:
minimist "~0.0.1"
wordwrap "~0.0.2"
optionator@^0.8.1:
version "0.8.2"
resolved "https://registry.yarnpkg.com/optionator/-/optionator-0.8.2.tgz#364c5e409d3f4d6301d6c0b4c05bba50180aeb64"
@ -1464,8 +1434,8 @@ path-is-absolute@^1.0.0:
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
path-parse@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.5.tgz#3c1adf871ea9cd6c9431b6ea2bd74a0ff055c4c1"
version "1.0.7"
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
path-type@^1.0.0:
version "1.1.0"
@ -1493,6 +1463,10 @@ pinkie@^2.0.0:
version "2.0.4"
resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870"
platform@^1.3.3:
version "1.3.5"
resolved "https://registry.yarnpkg.com/platform/-/platform-1.3.5.tgz#fb6958c696e07e2918d2eeda0f0bc9448d733444"
prelude-ls@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
@ -1523,9 +1497,9 @@ qs@~6.4.0:
version "6.4.0"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.4.0.tgz#13e26d28ad6b0ffaa91312cd3bf708ed351e7233"
randomatic@^1.1.3:
version "1.1.6"
resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-1.1.6.tgz#110dcabff397e9dcff7c0789ccc0a49adf1ec5bb"
randomatic@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-3.0.0.tgz#110dcabff397e9dcff7c0789ccc0a49adf1ec5bb"
dependencies:
is-number "^2.0.2"
kind-of "^3.0.2"
@ -1582,7 +1556,7 @@ request@^2.79.0:
aws4 "^1.2.1"
caseless "~0.12.0"
combined-stream "~1.0.5"
extend "~3.0.0"
extend "~3.0.2"
forever-agent "~0.6.1"
form-data "~2.1.1"
har-validator "~4.2.1"
@ -1596,8 +1570,8 @@ request@^2.79.0:
performance-now "^0.2.0"
qs "~6.4.0"
safe-buffer "^5.0.1"
stringstream "~0.0.4"
tough-cookie "~2.3.0"
stringstream "~0.0.6"
tough-cookie "~2.3.3"
tunnel-agent "^0.6.0"
uuid "^3.0.0"
@ -1619,12 +1593,6 @@ resolve@^1.2.0:
dependencies:
path-parse "^1.0.5"
right-align@^0.1.1:
version "0.1.3"
resolved "https://registry.yarnpkg.com/right-align/-/right-align-0.1.3.tgz#61339b722fe6a3515689210d24e14c96148613ef"
dependencies:
align-text "^0.1.1"
rimraf@^2.4.4:
version "2.6.1"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.1.tgz#c2338ec643df7a1b7fe5c54fa86f57428a55f33d"
@ -1643,7 +1611,7 @@ sane@~1.5.0:
exec-sh "^0.2.0"
fb-watchman "^1.8.0"
minimatch "^3.0.2"
minimist "^1.1.1"
minimist "^1.2.3"
walker "~1.0.5"
watch "~0.10.0"
@ -1671,7 +1639,7 @@ sntp@1.x.x:
version "1.0.9"
resolved "https://registry.yarnpkg.com/sntp/-/sntp-1.0.9.tgz#6541184cc90aeea6c6e7b35e2659082443c66198"
dependencies:
hoek "2.x.x"
hoek "4.2.1"
source-map-support@^0.4.2:
version "0.4.14"
@ -1679,16 +1647,14 @@ source-map-support@^0.4.2:
dependencies:
source-map "^0.5.6"
source-map@^0.4.4:
version "0.4.4"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.4.4.tgz#eba4f5da9c0dc999de68032d8b4f76173652036b"
dependencies:
amdefine ">=0.0.4"
source-map@^0.5.0, source-map@^0.5.3, source-map@^0.5.6, source-map@~0.5.1:
source-map@^0.5.0, source-map@^0.5.3, source-map@^0.5.6:
version "0.5.6"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.6.tgz#75ce38f52bf0733c5a7f0c118d81334a2bb5f412"
source-map@^0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
source-map@~0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.2.0.tgz#dab73fbcfc2ba819b4de03bd6f6eaa48164b3f9d"
@ -1713,9 +1679,9 @@ sprintf-js@~1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
sshpk@^1.7.0:
version "1.13.0"
resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.13.0.tgz#ff2a3e4fd04497555fed97b39a0fd82fafb3a33c"
sshpk@^1.13.2:
version "1.13.2"
resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.13.2.tgz#ff2a3e4fd04497555fed97b39a0fd82fafb3a33c"
dependencies:
asn1 "~0.2.3"
assert-plus "^1.0.0"
@ -1742,9 +1708,9 @@ string-width@^1.0.1, string-width@^1.0.2:
is-fullwidth-code-point "^1.0.0"
strip-ansi "^3.0.0"
stringstream@~0.0.4:
version "0.0.5"
resolved "https://registry.yarnpkg.com/stringstream/-/stringstream-0.0.5.tgz#4e484cd4de5a0bbbee18e46307710a8a81621878"
stringstream@~0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/stringstream/-/stringstream-0.0.6.tgz#4e484cd4de5a0bbbee18e46307710a8a81621878"
strip-ansi@^3.0.0, strip-ansi@^3.0.1:
version "3.0.1"
@ -1791,16 +1757,22 @@ throat@^3.0.0:
resolved "https://registry.yarnpkg.com/throat/-/throat-3.0.0.tgz#e7c64c867cbb3845f10877642f7b60055b8ec0d6"
tmpl@1.0.x:
version "1.0.4"
resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.4.tgz#23640dd7b42d00433911140820e5cf440e521dd1"
version "1.0.5"
resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.5.tgz#8683e0b902bb9c20c4f726e3c0b69f36518c07cc"
to-fast-properties@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-1.0.2.tgz#f3f5c0c3ba7299a7ef99427e44633257ade43320"
tough-cookie@^2.3.2, tough-cookie@~2.3.0:
version "2.3.2"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.3.2.tgz#f081f76e4c85720e6c37a5faced737150d84072a"
tough-cookie@^2.3.3:
version "2.3.3"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.3.3.tgz#f081f76e4c85720e6c37a5faced737150d84072a"
dependencies:
punycode "^1.4.1"
tough-cookie@~2.3.3:
version "2.3.4"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.3.4.tgz#ec60cee38ac675063ffc97a5c18970578ee83655"
dependencies:
punycode "^1.4.1"
@ -1828,18 +1800,9 @@ type-check@~0.3.2:
dependencies:
prelude-ls "~1.1.2"
uglify-js@^2.6:
version "2.8.22"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.8.22.tgz#d54934778a8da14903fa29a326fb24c0ab51a1a0"
dependencies:
source-map "~0.5.1"
yargs "~3.10.0"
optionalDependencies:
uglify-to-browserify "~1.0.0"
uglify-to-browserify@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/uglify-to-browserify/-/uglify-to-browserify-1.0.2.tgz#6e0924d6bda6b5afe349e39a6d632850a0f882b7"
uglify-js@^3.1.4:
version "3.13.5"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.13.5.tgz#5d71d6dbba64cf441f32929b1efce7365bb4f113"
uuid@^3.0.0:
version "3.0.1"
@ -1899,19 +1862,7 @@ which@^1.1.1, which@^1.2.12:
dependencies:
isexe "^2.0.0"
window-size@0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.1.0.tgz#5438cd2ea93b202efa3a19fe8887aee7c94f9c9d"
wordwrap@0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.2.tgz#b79669bb42ecb409f83d583cad52ca17eaa1643f"
wordwrap@~0.0.2:
version "0.0.3"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.3.tgz#a3d5da6cd5c0bc0008d37234bbaf1bed63059107"
wordwrap@~1.0.0:
wordwrap@^1.0.0, wordwrap@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
@ -1942,8 +1893,8 @@ xml-name-validator@^2.0.1:
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"
y18n@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-3.2.1.tgz#6d15fba884c08679c0d77e88e7759e811e07fa41"
version "3.2.2"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-3.2.2.tgz#85c901bd6470ce71fc4bb723ad209b70f7f28696"
yargs-parser@^4.2.0:
version "4.2.1"
@ -1968,12 +1919,3 @@ yargs@^6.3.0:
which-module "^1.0.0"
y18n "^3.2.1"
yargs-parser "^4.2.0"
yargs@~3.10.0:
version "3.10.0"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-3.10.0.tgz#f7ee7bd857dd7c1d2d38c0e74efbd681d1431fd1"
dependencies:
camelcase "^1.0.2"
cliui "^2.1.0"
decamelize "^1.0.0"
window-size "0.1.0"

View File

@ -40,9 +40,9 @@ class Ip2Region
/**
* construct method
*
* @param ip2regionFile
* @param ip2regionFile
*/
public function __construct( $ip2regionFile )
public function __construct( $ip2regionFile)
{
$this->dbFile = $ip2regionFile;
}
@ -266,7 +266,7 @@ class Ip2Region
fseek($this->dbFileHandler, $sptr);
$index = fread($this->dbFileHandler, $blockLen + INDEX_BLOCK_LENGTH);
$dataptr = 0;
$dataPtr = 0;
$l = 0; $h = $blockLen / INDEX_BLOCK_LENGTH;
while ( $l <= $h ) {
$m = (($l + $h) >> 1);
@ -279,18 +279,18 @@ class Ip2Region
if ( $ip > $eip ) {
$l = $m + 1;
} else {
$dataptr = self::getLong($index, $p + 8);
$dataPtr = self::getLong($index, $p + 8);
break;
}
}
}
//not matched
if ( $dataptr == 0 ) return NULL;
if ( $dataPtr == 0 ) return NULL;
//3. get the data
$dataLen = (($dataptr >> 24) & 0xFF);
$dataPtr = ($dataptr & 0x00FFFFFF);
$dataLen = (($dataPtr >> 24) & 0xFF);
$dataPtr = ($dataPtr & 0x00FFFFFF);
fseek($this->dbFileHandler, $dataPtr);
$data = fread($this->dbFileHandler, $dataLen);
@ -358,4 +358,3 @@ class Ip2Region
$this->HeaderPtr = NULL;
}
}
?>

View File

@ -8,7 +8,7 @@
if ( $argc < 2 ) {
$usage = <<<EOF
Usage: php Test.php [ip2region db file] [alrogrithm]
+-Algorithm: binary or b-tree\n
+-Optional Algorithm: binary, b-tree, memory\n
EOF;
exit($usage);
}

View File

@ -3,7 +3,7 @@
### 安装步骤
* git clone https://github.com/lionsoul2014/ip2region.git
* cd ip2region
* cp binding/php_extension/php5/ip2region 到 php source code 的 ext 目录下
* cp binding/php5_ext 到 php source code 的 ext/ip2region 目录下
* cp binding/c/ 里面所有的文件到 php source code 的 ext/ip2region/lib 目录下
* 在 ext/ip2region 下,运行

View File

@ -50,7 +50,7 @@ static zend_class_entry *ip2region_class_entry_ptr;
//}
void search(
static void search(
ip2region_t g_resouce_ptr,
uint_t (*func_ptr) (ip2region_t, uint_t, datablock_t),
long ip,
@ -182,7 +182,7 @@ PHP_METHOD(ip2region_class_entry_ptr, binarySearch)
PHP_MSHUTDOWN_FUNCTION(ip2region)
{
UNREGISTER_INI_ENTRIES();
ip2region_destroy(&g_resource);
if(g_resource_ptr != NULL) ip2region_destroy(&g_resource);
return SUCCESS;
}
/* }}} */
@ -256,8 +256,8 @@ PHP_MINIT_FUNCTION(ip2region)
ZEND_INIT_MODULE_GLOBALS(ip2region, php_ip2region_init_globals, NULL);
REGISTER_INI_ENTRIES();
if (ip2region_create( &g_resource, IP2REGION_G(db_file)) == 0)
char* _db_file = IP2REGION_G(db_file);
if ( _db_file == NULL || ip2region_create( &g_resource, _db_file) == 0)
{
g_resource_ptr = NULL;
} else {

View File

@ -65,7 +65,6 @@ static void php_ip2region_init_globals(zend_ip2region_globals *);
#define le_ip2region_name "Ip2region"
void search( ip2region_t, uint_t (*func_ptr) (ip2region_t, uint_t, datablock_t), long , zval **, datablock_t);
#endif /* PHP_IP2REGION_H */
/*

View File

@ -3,7 +3,7 @@
### 安装步骤
* git clone https://github.com/lionsoul2014/ip2region.git
* cd ip2region
* cp binding/php_extension/php7/ip2region 到 php source code 的 ext 目录下
* cp binding/php7_ext 到 php source code 的 ext/ip2region 目录下
* cp binding/c/ 里面所有的文件到 php source code 的 ext/ip2region/lib 目录下
* 在 ext/ip2region 下,运行

View File

@ -50,7 +50,7 @@ static zend_class_entry *ip2region_class_entry_ptr;
//}
void search(
static void search(
ip2region_t g_resouce_ptr,
uint_t (*func_ptr) (ip2region_t, uint_t, datablock_t),
long ip,
@ -188,7 +188,7 @@ PHP_METHOD(ip2region_class_entry_ptr, binarySearch)
PHP_MSHUTDOWN_FUNCTION(ip2region)
{
UNREGISTER_INI_ENTRIES();
ip2region_destroy(&g_resource);
if( g_resource_ptr != NULL ) ip2region_destroy(&g_resource);
return SUCCESS;
}
/* }}} */
@ -262,8 +262,8 @@ PHP_MINIT_FUNCTION(ip2region)
ZEND_INIT_MODULE_GLOBALS(ip2region, php_ip2region_init_globals, NULL);
REGISTER_INI_ENTRIES();
if (ip2region_create( &g_resource, IP2REGION_G(db_file)) == 0)
char* _db_file = IP2REGION_G(db_file);
if ( _db_file == NULL || ip2region_create( &g_resource, _db_file) == 0)
{
g_resource_ptr = NULL;
} else {

View File

@ -65,7 +65,6 @@ static void php_ip2region_init_globals(zend_ip2region_globals *);
#define le_ip2region_name "Ip2region"
void search( ip2region_t, uint_t (*func_ptr) (ip2region_t, uint_t, datablock_t), long , zval **, datablock_t);
#endif /* PHP_IP2REGION_H */
/*

View File

@ -0,0 +1,54 @@
#-*- coding:utf-8 -*-
"""
" ip2region python seacher client module benchmark test file
"
" Autho: koma<komazhang@foxmail.com>
" Date : 2018-10-04
"""
import threading
import time, sys
from ip2Region import Ip2Region
class BenchmarkThread(threading.Thread):
__searcher = None
__lock = None
def __init__(self, searcher, lock):
self.__searcher = searcher
self.__lock = lock
threading.Thread.__init__(self)
def run(self):
self.__lock.acquire()
try:
sTime = time.time() * 1000
data = self.__searcher.memorySearch("49.220.138.233")
eTime = time.time() * 1000
# @Note uncomment the print to make it more like the product environment
print("%s|%s in %5f millseconds" % (data["city_id"], data["region"].decode('utf-8'), eTime - sTime))
finally:
self.__lock.release()
if __name__ == "__main__":
dbFile = "./data/ip2region.db"
if ( len(sys.argv) > 2 ):
dbFile = sys.argv[1];
threads = []
searcher = Ip2Region(dbFile)
lock = threading.Lock()
for i in range(10000):
t = BenchmarkThread(searcher, lock)
threads.append(t)
sTime = time.time() * 1000
for t in threads:
t.start()
for t in threads:
t.join()
eTime = time.time() * 1000
print("Benchmark done: %5f" % (eTime - sTime))

View File

@ -8,14 +8,18 @@
import struct, io, socket, sys
class Ip2Region(object):
__headerSip = []
__headerPtr = []
__f = None
__sPtr = 0
__indexLen = 0
__dbBinStr = ''
def __init__(self, dbfile):
self.__INDEX_BLOCK_LENGTH = 12
self.__TOTAL_HEADER_LENGTH = 8192
self.__f = None
self.__headerSip = []
self.__headerPtr = []
self.__headerLen = 0
self.__indexSPtr = 0
self.__indexLPtr = 0
self.__indexCount = 0
self.__dbBinStr = ''
self.initDatabase(dbfile)
def memorySearch(self, ip):
@ -26,35 +30,30 @@ class Ip2Region(object):
if not ip.isdigit(): ip = self.ip2long(ip)
if self.__dbBinStr == '':
self.__dbBinStr = self.__f.read() #read all the contents in file
self.__sPtr = self.getLong(self.__dbBinStr, 0)
endPtr = self.getLong(self.__dbBinStr, 4)
self.__indexLen = endPtr - self.__sPtr
self.__dbBinStr = self.__f.read() #read all the contents in file
self.__indexSPtr = self.getLong(self.__dbBinStr, 0)
self.__indexLPtr = self.getLong(self.__dbBinStr, 4)
self.__indexCount = int((self.__indexLPtr - self.__indexSPtr)/self.__INDEX_BLOCK_LENGTH)+1
startPtr = self.__sPtr
indexLen = self.__indexLen
dbBinStr = self.__dbBinStr
l, h, mixPtr = (0, int(indexLen/12), 0)
l, h, dataPtr = (0, self.__indexCount, 0)
while l <= h:
m = int((l+h)/2)
ptr = startPtr + m*12
m = int((l+h) >> 1)
p = self.__indexSPtr + m*self.__INDEX_BLOCK_LENGTH
sip = self.getLong(self.__dbBinStr, p)
sip = self.getLong(dbBinStr, ptr)
eip = self.getLong(dbBinStr, ptr+4)
if ip >= sip:
if ip > eip:
l = m + 1
else:
mixPtr = self.getLong(dbBinStr, ptr+8)
break;
if ip < sip:
h = m -1
else:
h = m - 1
eip = self.getLong(self.__dbBinStr, p+4)
if ip > eip:
l = m + 1;
else:
dataPtr = self.getLong(self.__dbBinStr, p+8)
break
if mixPtr == 0: return "N2"
if dataPtr == 0: raise Exception("Data pointer not found")
return self.returnData(mixPtr)
return self.returnData(dataPtr)
def binarySearch(self, ip):
"""
@ -63,41 +62,34 @@ class Ip2Region(object):
"""
if not ip.isdigit(): ip = self.ip2long(ip)
if self.__indexLen < 1:
if self.__indexCount == 0:
self.__f.seek(0)
b = self.__f.read(8)
self.__sPtr = self.getLong(b, 0)
endPtr = self.getLong(b, 4)
self.__indexLen = endPtr - self.__sPtr
superBlock = self.__f.read(8)
self.__indexSPtr = self.getLong(superBlock, 0)
self.__indexLPtr = self.getLong(superBlock, 4)
self.__indexCount = int((self.__indexLPtr - self.__indexSPtr) / self.__INDEX_BLOCK_LENGTH) + 1
startPtr = self.__sPtr
indexLen = self.__indexLen
self.__f.seek(startPtr)
b = self.__f.read(indexLen+12)
l, h, mixPtr = (0, int(indexLen/12), 0)
l, h, dataPtr = (0, self.__indexCount, 0)
while l <= h:
m = int((l+h)/2)
ptr = startPtr + m*12
self.__f.seek(ptr)
m = int((l+h) >> 1)
p = m*self.__INDEX_BLOCK_LENGTH
b = self.__f.read(12)
sip = self.getLong(b, 0)
eip = self.getLong(b, 4)
if ip >= sip:
self.__f.seek(self.__indexSPtr+p)
buffer = self.__f.read(self.__INDEX_BLOCK_LENGTH)
sip = self.getLong(buffer, 0)
if ip < sip:
h = m - 1
else:
eip = self.getLong(buffer, 4)
if ip > eip:
l = m + 1
else:
mixPtr = self.getLong(b, 8)
break;
else:
h = m - 1
dataPtr = self.getLong(buffer, 8)
break
if mixPtr == 0: return "N2"
if dataPtr == 0: raise Exception("Data pointer not found")
return self.returnData(mixPtr)
return self.returnData(dataPtr)
def btreeSearch(self, ip):
"""
@ -107,82 +99,81 @@ class Ip2Region(object):
if not ip.isdigit(): ip = self.ip2long(ip)
if len(self.__headerSip) < 1:
headerLen = 0
#pass the super block
self.__f.seek(8)
#read the header block
b = self.__f.read(8192)
b = self.__f.read(self.__TOTAL_HEADER_LENGTH)
#parse the header block
sip = None
ptr = None
for i in range(0, len(b)-1, 8):
for i in range(0, len(b), 8):
sip = self.getLong(b, i)
ptr = self.getLong(b, i+4)
if ptr == 0:
break
self.__headerSip.append(sip)
self.__headerPtr.append(ptr)
headerLen += 1
self.__headerLen = headerLen
headerLen = len(self.__headerSip) - 1
l, h, sptr, eptr = (0, headerLen, 0, 0)
l, h, sptr, eptr = (0, self.__headerLen, 0, 0)
while l <= h:
m = int((l+h)/2)
m = int((l+h) >> 1)
if ip == self.__headerSip[m]:
if m > 0:
sptr = self.__headerPtr[m-1]
eptr = self.__headerPtr[m]
break;
else:
sptr = self.__headerPtr[m]
eptr = self.__headerPtr[m+1]
break;
break
if ip > self.__headerSip[m]:
if m == headerLen:
sptr = self.__headerPtr[m-1]
eptr = self.__headerPtr[m]
break;
elif ip < self.__headerSip[m+1]:
sptr = self.__headerPtr[m]
eptr = self.__headerPtr[m+1]
break;
l = m + 1
else:
if ip < self.__headerSip[m]:
if m == 0:
sptr = self.__headerPtr[m]
eptr = self.__headerPtr[m+1]
break;
break
elif ip > self.__headerSip[m-1]:
sptr = self.__headerPtr[m-1]
eptr = self.__headerPtr[m]
break;
break
h = m - 1
else:
if m == self.__headerLen - 1:
sptr = self.__headerPtr[m-1]
eptr = self.__headerPtr[m]
break
elif ip <= self.__headerSip[m+1]:
sptr = self.__headerPtr[m]
eptr = self.__headerPtr[m+1]
break
l = m + 1
if sptr == 0: return "N1"
if sptr == 0: raise Exception("Index pointer not found")
indexLen = eptr - sptr
self.__f.seek(sptr)
b = self.__f.read(indexLen + 12)
index = self.__f.read(indexLen + self.__INDEX_BLOCK_LENGTH)
l, h, mixPtr = (0, int(indexLen/12), 0)
l, h, dataPrt = (0, int(indexLen/self.__INDEX_BLOCK_LENGTH), 0)
while l <= h:
m = int((l+h)/2)
offset = m * 12
m = int((l+h) >> 1)
offset = int(m * self.__INDEX_BLOCK_LENGTH)
sip = self.getLong(index, offset)
if ip >= self.getLong(b, offset):
if ip > self.getLong(b, offset+4):
l = m + 1
else:
mixPtr = self.getLong(b, offset+8)
break;
else:
if ip < sip:
h = m - 1
else:
eip = self.getLong(index, offset+4)
if ip > eip:
l = m + 1;
else:
dataPrt = self.getLong(index, offset+8)
break
if mixPtr == 0: return "N2"
if dataPrt == 0: raise Exception("Data pointer not found")
return self.returnData(mixPtr)
return self.returnData(dataPrt)
def initDatabase(self, dbfile):
"""
@ -191,18 +182,18 @@ class Ip2Region(object):
"""
try:
self.__f = io.open(dbfile, "rb")
except IOError, e:
print "[Error]: ", e
except IOError as e:
print("[Error]: %s" % e)
sys.exit()
def returnData(self, dsptr):
def returnData(self, dataPtr):
"""
" get ip data from db file by data start ptr
" param: dsptr
"""
dataPtr = dsptr & 0x00FFFFFFL
dataLen = (dsptr >> 24) & 0xFF
dataLen = (dataPtr >> 24) & 0xFF
dataPtr = dataPtr & 0x00FFFFFF
self.__f.seek(dataPtr)
data = self.__f.read(dataLen)
@ -213,7 +204,6 @@ class Ip2Region(object):
def ip2long(self, ip):
_ip = socket.inet_aton(ip)
return struct.unpack("!L", _ip)[0]
def isip(self, ip):
@ -228,13 +218,14 @@ class Ip2Region(object):
return True
def getLong(self, b, offset):
if len( b[offset:offset+4] ) == 4:
if len(b[offset:offset+4]) == 4:
return struct.unpack('I', b[offset:offset+4])[0]
return 0
def close(self):
self.__headerSip = None
if self.__f != None:
self.__f.close()
self.__dbBinStr = None
self.__headerPtr = None
self.__f.close()
self.__f = None
self.__headerSip = None

View File

@ -6,72 +6,76 @@
" Date : 2015-11-06
"""
import struct, sys, os, time
from platform import python_version
from ip2Region import Ip2Region
def testSearch():
"""
" ip2region test function
"""
llen = len(sys.argv)
argLen = len(sys.argv)
version = python_version()
algorithms = ["binary", "b-tree", "memory"]
if llen < 2:
print "Usage: python testSearcher.py [ip2region db file] [alrogrithm]"
print "Algorithm: binary or b-tree"
if argLen < 2:
print("Usage: python testSearcher.py [ip2region db file] [alrogrithm]")
print("Algorithm: %s" % ", ".join(algorithms))
return 0
dbFile = sys.argv[1]
method = 1
algorithm = "b-tree"
dbFile = sys.argv[1]
if (not os.path.isfile(dbFile)) or (not os.path.exists(dbFile)):
print "[Error]: Specified db file is not exists."
print("[Error]: Specified db file is not exists.")
return 0
if llen > 2:
if argLen > 2:
algorithm = sys.argv[2]
if algorithm == "binary":
method = 2
elif algorithm == "memory":
method = 3
try:
algorithms.index(algorithm)
except Exception as e:
algorithm = "b-tree"
print "initializing %s..." % (algorithm)
print "+----------------------------------+"
print "| ip2region test program |"
print "| Author: chenxin619315@gmail.com. |"
print "| Type 'quit' to exit program |"
print "+----------------------------------+"
print("initializing %s..." % (algorithm))
print("+----------------------------------+")
print("| ip2region test program |")
print("| Author: chenxin619315@gmail.com. |")
print("| Type 'quit' to exit program |")
print("+----------------------------------+")
searcher = Ip2Region(dbFile);
searcher = Ip2Region(dbFile)
while True:
line = raw_input("ip2region>> ")
if version[:1] == "2":
line = raw_input("ip2region>> ")
else:
line = input("ip2region>> ")
line = line.strip()
if line == "":
print "[Error]: Invalid ip address."
print("[Error]: Invalid ip address.")
continue
if line == "quit":
print "[Info]: Thanks for your use, Bye."
print("[Info]: Thanks for your use, Bye.")
break
if not searcher.isip(line):
print "[Error]: Invalid ip address."
print("[Error]: Invalid ip address.")
continue
sTime = time.time() * 1000
if method == 1:
data = searcher.btreeSearch(line)
elif method == 2:
data = searcher.binarySearch(line)
else:
data = searcher.memorySearch(line)
eTime = time.time() * 1000
if isinstance(data, dict):
print "%s|%s in %f millseconds" % (data["city_id"], data["region"], eTime-sTime)
else:
print "[Error]: ", data
try:
sTime = time.time()*1000
if algorithm == "binary":
data = searcher.binarySearch(line)
elif algorithm == "memory":
data = searcher.memorySearch(line)
else:
data = searcher.btreeSearch(line)
eTime = time.time()*1000
print("%s|%s in %5f millseconds" % (data["city_id"], data["region"].decode('utf-8'), eTime - sTime))
except Exception as e:
print("[Error]: %s" % e)
searcher.close()

View File

@ -1,240 +0,0 @@
#-*- coding:utf-8 -*-
"""
" ip2region python seacher client module
"
" Author: koma<komazhang@foxmail.com>
" Date : 2015-11-06
"""
import struct, io, socket, sys
class Ip2Region(object):
__headerSip = []
__headerPtr = []
__f = None
__sPtr = 0
__indexLen = 0
__dbBinStr = ''
def __init__(self, dbfile):
self.initDatabase(dbfile)
def memorySearch(self, ip):
"""
" memory search method
" param: ip
"""
if not ip.isdigit(): ip = self.ip2long(ip)
if self.__dbBinStr == '':
self.__dbBinStr = self.__f.read() #read all the contents in file
self.__sPtr = self.getLong(self.__dbBinStr, 0)
endPtr = self.getLong(self.__dbBinStr, 4)
self.__indexLen = endPtr - self.__sPtr
startPtr = self.__sPtr
indexLen = self.__indexLen
dbBinStr = self.__dbBinStr
l, h, mixPtr = (0, int(indexLen/12), 0)
while l <= h:
m = int((l+h)/2)
ptr = startPtr + m*12
sip = self.getLong(dbBinStr, ptr)
eip = self.getLong(dbBinStr, ptr+4)
if ip >= sip:
if ip > eip:
l = m + 1
else:
mixPtr = self.getLong(dbBinStr, ptr+8)
break;
else:
h = m - 1
if mixPtr == 0: return "N2"
return self.returnData(mixPtr)
def binarySearch(self, ip):
"""
" binary search method
" param: ip
"""
if not ip.isdigit(): ip = self.ip2long(ip)
if self.__indexLen < 1:
self.__f.seek(0)
b = self.__f.read(8)
self.__sPtr = self.getLong(b, 0)
endPtr = self.getLong(b, 4)
self.__indexLen = endPtr - self.__sPtr
startPtr = self.__sPtr
indexLen = self.__indexLen
self.__f.seek(startPtr)
b = self.__f.read(indexLen+12)
l, h, mixPtr = (0, int(indexLen/12), 0)
while l <= h:
m = int((l+h)/2)
ptr = startPtr + m*12
self.__f.seek(ptr)
b = self.__f.read(12)
sip = self.getLong(b, 0)
eip = self.getLong(b, 4)
if ip >= sip:
if ip > eip:
l = m + 1
else:
mixPtr = self.getLong(b, 8)
break;
else:
h = m - 1
if mixPtr == 0: return "N2"
return self.returnData(mixPtr)
def btreeSearch(self, ip):
"""
" b-tree search method
" param: ip
"""
if not ip.isdigit(): ip = self.ip2long(ip)
if len(self.__headerSip) < 1:
#pass the super block
self.__f.seek(8)
#read the header block
b = self.__f.read(8192)
#parse the header block
sip = None
ptr = None
for i in range(0, len(b)-1, 8):
sip = self.getLong(b, i)
ptr = self.getLong(b, i+4)
if ptr == 0:
break
self.__headerSip.append(sip)
self.__headerPtr.append(ptr)
headerLen = len(self.__headerSip) - 1
l, h, sptr, eptr = (0, headerLen, 0, 0)
while l <= h:
m = int((l+h)/2)
if ip == self.__headerSip[m]:
if m > 0:
sptr = self.__headerPtr[m-1]
eptr = self.__headerPtr[m]
break;
else:
sptr = self.__headerPtr[m]
eptr = self.__headerPtr[m+1]
break;
if ip > self.__headerSip[m]:
if m == headerLen:
sptr = self.__headerPtr[m-1]
eptr = self.__headerPtr[m]
break;
elif ip < self.__headerSip[m+1]:
sptr = self.__headerPtr[m]
eptr = self.__headerPtr[m+1]
break;
l = m + 1
else:
if m == 0:
sptr = self.__headerPtr[m]
eptr = self.__headerPtr[m+1]
break;
elif ip > self.__headerSip[m-1]:
sptr = self.__headerPtr[m-1]
eptr = self.__headerPtr[m]
break;
h = m - 1
if sptr == 0: return "N1"
indexLen = eptr - sptr
self.__f.seek(sptr)
b = self.__f.read(indexLen + 12)
l, h, mixPtr = (0, int(indexLen/12), 0)
while l <= h:
m = int((l+h)/2)
offset = m * 12
if ip >= self.getLong(b, offset):
if ip > self.getLong(b, offset+4):
l = m + 1
else:
mixPtr = self.getLong(b, offset+8)
break;
else:
h = m - 1
if mixPtr == 0: return "N2"
return self.returnData(mixPtr)
def initDatabase(self, dbfile):
"""
" initialize the database for search
" param: dbFile
"""
try:
self.__f = io.open(dbfile, "rb")
except IOError as e:
print ("[Error]: ", e)
sys.exit()
def returnData(self, dsptr):
"""
" get ip data from db file by data start ptr
" param: dsptr
"""
dataPtr = dsptr & 0x00FFFFFF
dataLen = (dsptr >> 24) & 0xFF
self.__f.seek(dataPtr)
data = self.__f.read(dataLen)
return {
"city_id": self.getLong(data, 0),
"region" : data[4:]
}
def ip2long(self, ip):
_ip = socket.inet_aton(ip)
return struct.unpack("!L", _ip)[0]
def isip(self, ip):
p = ip.split(".")
if len(p) != 4 : return False
for pp in p:
if not pp.isdigit() : return False
if len(pp) > 3 : return False
if int(pp) > 255 : return False
return True
def getLong(self, b, offset):
if len( b[offset:offset+4] ) == 4:
return struct.unpack('I', b[offset:offset+4])[0]
return 0
def close(self):
self.__headerSip = None
self.__headerPtr = None
self.__f.close()
self.__f = None

View File

@ -1,79 +0,0 @@
#-*- coding:utf-8 -*-
"""
" ip2region python seacher client module
"
" Autho: koma<komazhang@foxmail.com>
" Date : 2015-11-06
"""
import struct, sys, os, time
from ip2Region import Ip2Region
def testSearch():
"""
" ip2region test function
"""
llen = len(sys.argv)
if llen < 2:
print ("Usage: python testSearcher.py [ip2region db file] [alrogrithm]")
print ("Algorithm: binary or b-tree")
return 0
dbFile = sys.argv[1]
method = 1
algorithm = "b-tree"
if (not os.path.isfile(dbFile)) or (not os.path.exists(dbFile)):
print ("[Error]: Specified db file is not exists.")
return 0
if llen > 2:
algorithm = sys.argv[2]
if algorithm == "binary":
method = 2
elif algorithm == "memory":
method = 3
print ("initializing %s..."% algorithm)
print ("+----------------------------------+")
print ("| ip2region test program |")
print ("| Author: chenxin619315@gmail.com. |")
print ("| Type 'quit' or 'exit' to exit program |")
print ("+----------------------------------+")
searcher = Ip2Region(dbFile);
while True:
line = input("ip2region>> ")
line = line.strip()
if line == "":
print ("[Error]: Invalid ip address.")
continue
if line == "quit" or line == "exit" :
print ("[Info]: Thanks for your use, Bye.")
break
if not searcher.isip(line):
print ("[Error]: Invalid ip address.")
continue
sTime = time.time() * 1000
if method == 1:
data = searcher.btreeSearch(line)
elif method == 2:
data = searcher.binarySearch(line)
else:
data = searcher.memorySearch(line)
eTime = time.time() * 1000
if isinstance(data, dict):
print ("%s|%s in %f millseconds" % (data["city_id"], data["region"].decode('utf-8'), eTime-sTime))
else:
print ("[Error]: ", data)
searcher.close()
if __name__ == "__main__":
testSearch()

View File

@ -28,7 +28,7 @@ version = "*"
取消上面 toml 的 `# features = ["lazy"]` 行的注释即可使用,其 api 是 `memory_search``memory_search_ip`
只是目前 DB 有3.2M,还是有些感人的。
只是目前 DB 有3.2M,还是有些感人的。
关键的一行是 `features = ["lazy"]` ,不需要则可以注释或者删掉。

File diff suppressed because it is too large Load Diff

Binary file not shown.

7
maker/c/README.md Normal file
View File

@ -0,0 +1,7 @@
# db maker C implementation
### 1, How to build ?
### 2, How to make ?
comming soon ...

18
maker/csharp/README.md Normal file
View File

@ -0,0 +1,18 @@
# db maker C# implementation
### 1, 如何编译 ?
```
cd src
dotnet build
```
### 2, 如何生成数据库文件 ?
```
dbMaker -src ./data/ip.merge.txt -region ./data/global_region.csv
```
### 3, 迁移介绍
https://www.coderbusy.com/archives/855.html

BIN
maker/csharp/dbMaker.exe Normal file

Binary file not shown.

199
maker/csharp/src/.gitignore vendored Normal file
View File

@ -0,0 +1,199 @@
#################
## Visual Studio
#################
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
# User-specific files
*.suo
*.user
*.sln.docstates
# Build results
.vs/
[Dd]ebug/
[Rr]elease/
x64/
[Bb]in/
[Oo]bj/
# Build scripts folder
[Aa]pp[Bb]uild/
[Oo]ut/
# Visual Studio Code files
.vscode/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
*_i.c
*_p.c
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.log
*.scc
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
*.cachefile
# Visual Studio profiler
*.psess
*.vsp
*.vspx
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# NCrunch
*.ncrunch*
.*crunch*.local.xml
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.Publish.xml
*.pubxml
#Launch settings
**/Properties/launchSettings.json
# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
#packages/
# Windows Azure Build Output
csx
*.build.csdef
# Windows Store app package directory
AppPackages/
# Others
sql/
*.Cache
ClientBin/
[Ss]tyle[Cc]op.*
~$*
*~
*.dbmdl
*.[Pp]ublish.xml
*.pfx
*.publishsettings
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
App_Data/*.mdf
App_Data/*.ldf
#############
## Windows detritus
#############
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Mac crap
.DS_Store
#######################
## nopCommerce specific
###########
glob:*.user
*.patch
*.hg
src/Presentation/Nop.Web/.config/*
src/Presentation/Nop.Web/App_Data/browscap.crawlersonly.xml
src/Presentation/Nop.Web/App_Data/installedPlugins.json
src/Presentation/Nop.Web/App_Data/plugins.json
src/Presentation/Nop.Web/App_Data/dataSettings.json
src/Presentation/Nop.Web/App_Data/appsettings.json
src/Presentation/Nop.Web/App_Data/TempUploads/*
!src/Presentation/Nop.Web/App_Data/TempUploads/Index.htm
src/Presentation/Nop.Web/App_Data/DataProtectionKeys/*
!src/Presentation/Nop.Web/App_Data/DataProtectionKeys/Index.htm
src/Presentation/Nop.Web/node_modules/*
src/Presentation/Nop.Web/Plugins/*
!src/Presentation/Nop.Web/Plugins/bin
src/Presentation/Nop.Web/Plugins/bin/*
!src/Presentation/Nop.Web/Plugins/bin/placeholder.txt
src/Presentation/Nop.Web/wwwroot/bundles/*
!src/Presentation/Nop.Web/wwwroot/bundles/Index.htm
src/Presentation/Nop.Web/wwwroot/db_backups/*.bak
!src/Presentation/Nop.Web/wwwroot/db_backups/Index.htm
src/Presentation/Nop.Web/wwwroot/images/thumbs/*
!src/Presentation/Nop.Web/wwwroot/images/thumbs/placeholder.txt
src/Presentation/Nop.Web/wwwroot/images/uploaded/*
!src/Presentation/Nop.Web/wwwroot/images/uploaded/placeholder.txt
src/Presentation/Nop.Web/wwwroot/files/exportimport/*
!src/Presentation/Nop.Web/wwwroot/files/exportimport/Index.htm
src/Presentation/Nop.Web/wwwroot/lib/cldr-data/main*
!src/Presentation/Nop.Web/wwwroot/lib/cldr-data/main/en/*
!src/Presentation/Nop.Web/wwwroot/lib/cldr-data/main/main.zip
src/packages/*

View File

@ -0,0 +1,89 @@
using System;
namespace IP2RegionDotNetDbMaker
{
/**
* data block class
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class DataBlock
{
/**
* city id
*/
private int city_id;
/**
* region address
*/
private String region;
/**
* region ptr in the db file
*/
private int dataPtr;
/**
* construct method
*
* @param city_id
* @param region region string
* @param dataPtr data ptr
*/
public DataBlock(int city_id, String region, int dataPtr)
{
this.city_id = city_id;
this.region = region;
this.dataPtr = dataPtr;
}
public DataBlock(int city_id, String region) : this(city_id, region, 0)
{
}
public int getCityId()
{
return city_id;
}
public DataBlock setCityId(int city_id)
{
this.city_id = city_id;
return this;
}
public String getRegion()
{
return region;
}
public DataBlock setRegion(String region)
{
this.region = region;
return this;
}
public int getDataPtr()
{
return dataPtr;
}
public DataBlock setDataPtr(int dataPtr)
{
this.dataPtr = dataPtr;
return this;
}
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append(city_id).append('|').append(region).append('|').append(dataPtr);
return sb.toString();
}
}
}

View File

@ -0,0 +1,66 @@
using System;
namespace IP2RegionDotNetDbMaker
{
/**
* database configuration class
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class DbConfig
{
/**
* total header data block size
*/
private int totalHeaderSize;
/**
* max index data block size
* u should always choice the fastest read block size
*/
private int indexBlockSize;
/**
* construct method
*
* @param totalHeaderSize
* @
*/
public DbConfig(int totalHeaderSize)
{
if ((totalHeaderSize % 8) != 0)
{
throw new DbMakerConfigException("totalHeaderSize must be times of 8");
}
this.totalHeaderSize = totalHeaderSize;
this.indexBlockSize = 8192; //4 * 2048
}
public DbConfig() : this(8 * 2048)
{
}
public int getTotalHeaderSize()
{
return totalHeaderSize;
}
public DbConfig setTotalHeaderSize(int totalHeaderSize)
{
this.totalHeaderSize = totalHeaderSize;
return this;
}
public int getIndexBlockSize()
{
return indexBlockSize;
}
public DbConfig setIndexBlockSize(int dataBlockSize)
{
this.indexBlockSize = dataBlockSize;
return this;
}
}
}

View File

@ -0,0 +1,433 @@
using System;
namespace IP2RegionDotNetDbMaker
{
/**
* fast ip db maker
*
* db struct:
* 1. header part
* 1): super part:
* +------------+-----------+
* | 4 bytes | 4 bytes |
* +------------+-----------+
* start index ptr, end index ptr
*
* 2): b-tree index part
* +------------+-----------+-----------+-----------+
* | 4bytes | 4bytes | 4bytes | 4bytes | ...
* +------------+-----------+-----------+-----------+
* start ip ptr index ptr
*
* 2. data part:
* +------------+-----------------------+
* | 2bytes | dynamic length |
* +------------+-----------------------+
* data length city_id|Country|Province|Area|City|ISP
*
* 3. index part: (ip range)
* +------------+-----------+---------------+
* | 4bytes | 4bytes | 4bytes |
* +------------+-----------+---------------+
* start ip end ip 3 byte data ptr & 1 byte data length
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class DbMaker
{
/**
* db config
*/
private DbConfig dbConfig;
/**
* ip source file path
*/
private File ipSrcFile;
/**
* buffer
*/
private LinkedList<IndexBlock> indexPool;
private LinkedList<HeaderBlock> headerPool;
/**
* global region Id mapping data
*/
private File globalRegionFile = null;
private HashMap<String, int?> globalRegionMap = null;
/**
* region and data ptr mapping data
*/
private HashMap<String, DataBlock> regionPtrPool = null;
/**
* construct method
*
* @param config
* @param ipSrcFile tb source ip file
* @param globalRegionFile global_region.csv file offer by lion
* @throws DbMakerConfigException
* @throws IOException
*/
public DbMaker(
DbConfig config,
String ipSrcFile,
String globalRegionFile)
{
this.dbConfig = config;
this.ipSrcFile = new File(ipSrcFile);
this.globalRegionMap = new HashMap<String, int?>();
this.regionPtrPool = new HashMap<String, DataBlock>();
if (globalRegionFile != null)
{
this.globalRegionFile = new File(globalRegionFile);
}
if (this.ipSrcFile.exists() == false)
{
throw new Exception("Error: Invalid file path " + ipSrcFile);
}
}
/**
* initialize the db file
*
* @param raf
* @
*/
private void initDbFile(RandomAccessFile raf)
{
//1. zero fill the header part
raf.seek(0L);
raf.write(new byte[8]); //super block
raf.write(new byte[dbConfig.getTotalHeaderSize()]); //header block
headerPool = new LinkedList<HeaderBlock>();
indexPool = new LinkedList<IndexBlock>();
}
/**
* make the Db file
*
* @param dbFile target output file path
* @
*/
public void make(String dbFile)
{
//check and load the gloabl region
if (globalRegionFile != null)
{
Console.WriteLine("+-Try to load the global region data ...");
BufferedReader greader = new BufferedReader(new FileReader(globalRegionFile));
String gline = null;
while ((gline = greader.readLine()) != null)
{
String[] p = gline.split(",");
if (p.Length != 5) continue;
//push the mapping
globalRegionMap.put(p[2], Int32.Parse(p[0]));
}
greader.close();
Console.WriteLine("|--[Ok]");
}
//alloc the header size
BufferedReader reader = new BufferedReader(new FileReader(this.ipSrcFile));
RandomAccessFile raf = new RandomAccessFile(dbFile, "rw");
//init the db file
initDbFile(raf);
Console.WriteLine("+-Db file initialized.");
//analysis main loop
Console.WriteLine("+-Try to write the data blocks ... ");
String line = null;
while ((line = reader.readLine()) != null)
{
line = line.trim();
if (line.length() == 0) continue;
if (line.charAt(0) == '#') continue;
//1. get the start ip
int sIdx = 0, eIdx = 0;
if ((eIdx = line.indexOf('|', sIdx + 1)) == -1) continue;
String startIp = line.substring(sIdx, eIdx);
//2. get the end ip
sIdx = eIdx + 1;
if ((eIdx = line.indexOf('|', sIdx + 1)) == -1) continue;
String endIp = line.substring(sIdx, eIdx);
//3. get the region
sIdx = eIdx + 1;
String region = line.substring(sIdx);
Console.WriteLine("+-Try to process item " + line);
addDataBlock(raf, startIp, endIp, region);
Console.WriteLine("|--[Ok]");
}
Console.WriteLine("|--Data block flushed!");
Console.WriteLine("|--Data file pointer: " + raf.getFilePointer() + "\n");
//write the index bytes
Console.WriteLine("+-Try to write index blocks ... ");
//record the start block
IndexBlock indexBlock = null;
HeaderBlock hb = null;
indexBlock = indexPool.getFirst();
long indexStartIp = indexBlock.getStartIp(),
indexStratPtr = raf.getFilePointer(), indexEndPtr;
headerPool.add(new HeaderBlock(indexStartIp, (int)(indexStratPtr)));
int blockLength = IndexBlock.getIndexBlockLength();
int counter = 0, shotCounter = (dbConfig.getIndexBlockSize() / blockLength) - 1;
//var indexIt = indexPool.iterator();
//while (indexIt.hasNext())
foreach (var indexIt in indexPool.iterator())
{
indexBlock = indexIt;
if (++counter >= shotCounter)
{
hb = new HeaderBlock(
indexBlock.getStartIp(),
(int)raf.getFilePointer()
);
headerPool.add(hb);
counter = 0;
}
//write the buffer
raf.write(indexBlock.getBytes());
}
//record the end block
if (counter > 0)
{
indexBlock = indexPool.getLast();
hb = new HeaderBlock(
indexBlock.getStartIp(),
((int)raf.getFilePointer()) - IndexBlock.getIndexBlockLength()
);
headerPool.add(hb);
}
indexEndPtr = raf.getFilePointer();
Console.WriteLine("|--[Ok]");
//write the super blocks
Console.WriteLine("+-Try to write the super blocks ... ");
raf.seek(0L); //reset the file pointer
byte[] superBuffer = new byte[8];
Util.writeIntLong(superBuffer, 0, indexStratPtr);
Util.writeIntLong(superBuffer, 4, indexEndPtr - blockLength);
raf.write(superBuffer);
Console.WriteLine("|--[Ok]");
//write the header blocks
Console.WriteLine("+-Try to write the header blocks ... ");
//var headerIt = headerPool.iterator();
//while (headerIt.hasNext())
//{
// HeaderBlock headerBlock = headerIt.next();
// raf.write(headerBlock.getBytes());
//}
foreach (var headerBlock in headerPool.iterator())
{
raf.write(headerBlock.getBytes());
}
//write the copyright and the release timestamp info
Console.WriteLine("+-Try to write the copyright and release date info ... ");
raf.seek(raf.length());
//Calendar cal = Calendar.getInstance();
//SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
String copyright = "Created by lionsoul at " + DateTime.Now.ToString("yyyy/MM/dd"); //dateFormat.format(cal.getTime());
var timestamp = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
raf.write((Int32)timestamp); //the unix timestamp
raf.write(copyright.getBytes());
Console.WriteLine("|--[Ok]");
reader.close();
raf.close();
}
/**
* internal method to add a new data block record
*
* @param raf
* @param startIp
* @param endIp
* @param region data
*/
private void addDataBlock(
RandomAccessFile raf,
String startIp, String endIp, String region)
{
try
{
byte[] data = region.getBytes("UTF-8");
int dataPtr = 0;
/*byte[] city = new byte[4];
int city_id = getCityId(region);
Util.writeIntLong(city, 0, city_id);
dataPtr = (int)raf.getFilePointer();
raf.write(city);
raf.write(data);*/
//check region ptr pool first
if (regionPtrPool.containsKey(region))
{
DataBlock dataBlock = regionPtrPool.get(region);
dataPtr = dataBlock.getDataPtr();
Console.WriteLine("dataPtr: " + dataPtr + ", region: " + region);
}
else
{
byte[] city = new byte[4];
int city_id = getCityId(region);
Util.writeIntLong(city, 0, city_id);
dataPtr = (int)raf.getFilePointer();
raf.write(city);
raf.write(data);
regionPtrPool.put(region, new DataBlock(city_id, region, dataPtr));
}
//add the data index blocks
IndexBlock ib = new IndexBlock(
Util.ip2long(startIp),
Util.ip2long(endIp),
dataPtr,
data.Length + 4 //4 bytes for the city id
);
indexPool.add(ib);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
/**
* get the city id base on the global region data
*
* @param region
* @return int
*/
public int getCityId(String region)
{
String[] p = region.split("|");
if (p.Length != 5) return 0;
String key = null;
Int32? intv = null;
for (int i = 3; i >= 0; i--)
{
if (p[i].equals("0")) continue;
if (i == 3
&& p[i].indexOf("省直辖县级") > -1)
{
key = p[2] + p[3];
}
else
{
key = p[i];
}
intv = globalRegionMap.get(key);
if (intv == null) return 0;
return intv.Value;
}
return 0;
}
public DbConfig getDbConfig()
{
return dbConfig;
}
public DbMaker setDbConfig(DbConfig dbConfig)
{
this.dbConfig = dbConfig;
return this;
}
public File getIpSrcFile()
{
return ipSrcFile;
}
public DbMaker setIpSrcFile(File ipSrcFile)
{
this.ipSrcFile = ipSrcFile;
return this;
}
/**
* make this directly a runnable application
* interface to make the database file
*/
public static void Main(String[] args)
{
String dstDir = "./data/";
String[] path = new String[] { null, null };
for (int i = 0; i < args.Length; i++)
{
if (args[i].equals("-src"))
{
path[0] = args[++i];
}
else if (args[i].equals("-region"))
{
path[1] = args[++i];
}
else if (args[i].equals("-dst"))
{
dstDir = args[++i];
}
}
for (int i = 0; i < path.Length; i++)
{
if (path[i] == null)
{
Console.WriteLine("Usage: dbMaker "
+ "-src [source text file path] "
+ "-region [global region file path]");
Console.WriteLine("eg: dbMaker "
+ "-src ./data/ip.merge.txt -region ./data/origin/global_region.csv");
Environment.Exit(0);
return;
}
}
//check and stdlize the destination directory
if (!dstDir.endsWith("/"))
{
dstDir = dstDir + "/";
}
try
{
DbConfig config = new DbConfig();
DbMaker dbMaker = new DbMaker(config, path[0], path[1]);
dbMaker.make(dstDir + "ip2region.db");
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}

View File

@ -0,0 +1,17 @@
using System;
namespace IP2RegionDotNetDbMaker
{
/**
* configuration exception
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class DbMakerConfigException : Exception
{
public DbMakerConfigException(string info) : base(info)
{
}
}
}

View File

@ -0,0 +1,449 @@
using System;
using System.Text;
namespace IP2RegionDotNetDbMaker
{
/**
* ip db searcher class (Not thread safe)
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class DbSearcher
{
public static int BTREE_ALGORITHM = 1;
public static int BINARY_ALGORITHM = 2;
public static int MEMORY_ALGORITYM = 3;
/**
* db config
*/
private DbConfig dbConfig = null;
/**
* db file access handler
*/
private RandomAccessFile raf = null;
/**
* header blocks buffer
*/
private long[] HeaderSip = null;
private int[] HeaderPtr = null;
private int headerLength;
/**
* super blocks info
*/
private long firstIndexPtr = 0;
private long lastIndexPtr = 0;
private int totalIndexBlocks = 0;
/**
* for memory mode
* the original db binary string
*/
private byte[] dbBinStr = null;
/**
* construct class
*
* @param bdConfig
* @param dbFile
* @
*/
public DbSearcher(DbConfig dbConfig, String dbFile)
{
this.dbConfig = dbConfig;
raf = new RandomAccessFile(dbFile, "r");
}
/**
* construct method with self-define std ip2region bianry string support
* Thanks to the issue from Wendal at https://gitee.com/lionsoul/ip2region/issues/IILFL
*
* @param dbConfig
* @param dbBinStr
*/
public DbSearcher(DbConfig dbConfig, byte[] dbBinStr)
{
this.dbConfig = dbConfig;
this.dbBinStr = dbBinStr;
firstIndexPtr = Util.getIntLong(dbBinStr, 0);
lastIndexPtr = Util.getIntLong(dbBinStr, 4);
totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr) / IndexBlock.getIndexBlockLength()) + 1;
}
/**
* get the region with a int ip address with memory binary search algorithm
*
* @param ip
* @
*/
public DataBlock memorySearch(long ip)
{
int blen = IndexBlock.getIndexBlockLength();
if (dbBinStr == null)
{
dbBinStr = new byte[(int)raf.length()];
raf.seek(0L);
raf.readFully(dbBinStr, 0, dbBinStr.Length);
//initialize the global vars
firstIndexPtr = Util.getIntLong(dbBinStr, 0);
lastIndexPtr = Util.getIntLong(dbBinStr, 4);
totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr) / blen) + 1;
}
//search the index blocks to define the data
int l = 0, h = totalIndexBlocks;
long sip, eip, dataptr = 0;
while (l <= h)
{
int m = (l + h) >> 1;
int p = (int)(firstIndexPtr + m * blen);
sip = Util.getIntLong(dbBinStr, p);
if (ip < sip)
{
h = m - 1;
}
else
{
eip = Util.getIntLong(dbBinStr, p + 4);
if (ip > eip)
{
l = m + 1;
}
else
{
dataptr = Util.getIntLong(dbBinStr, p + 8);
break;
}
}
}
//not matched
if (dataptr == 0) return null;
//get the data
int dataLen = (int)((dataptr >> 24) & 0xFF);
int dataPtr = (int)((dataptr & 0x00FFFFFF));
int city_id = (int)Util.getIntLong(dbBinStr, dataPtr);
//String region = new String(dbBinStr, dataPtr + 4, dataLen - 4, "UTF-8");
var region = Encoding.UTF8.GetString(dbBinStr, dataPtr + 4, dataLen - 4);
return new DataBlock(city_id, region, dataPtr);
}
/**
* get the region throught the ip address with memory binary search algorithm
*
* @param ip
* @return DataBlock
* @
*/
public DataBlock memorySearch(String ip)
{
return memorySearch(Util.ip2long(ip));
}
/**
* get by index ptr
*
* @param indexPtr
* @
*/
public DataBlock getByIndexPtr(long ptr)
{
raf.seek(ptr);
byte[] buffer = new byte[12];
raf.readFully(buffer, 0, buffer.Length);
//long startIp = Util.getIntLong(buffer, 0);
//long endIp = Util.getIntLong(buffer, 4);
long extra = Util.getIntLong(buffer, 8);
int dataLen = (int)((extra >> 24) & 0xFF);
int dataPtr = (int)((extra & 0x00FFFFFF));
raf.seek(dataPtr);
byte[] data = new byte[dataLen];
raf.readFully(data, 0, data.Length);
int city_id = (int)Util.getIntLong(data, 0);
//String region = new String(data, 4, data.Length - 4, "UTF-8");
var region = Encoding.UTF8.GetString(data, 4, data.Length - 4);
return new DataBlock(city_id, region, dataPtr);
}
/**
* get the region with a int ip address with b-tree algorithm
*
* @param ip
* @
*/
public DataBlock btreeSearch(long ip)
{
//check and load the header
if (HeaderSip == null)
{
raf.seek(8L); //pass the super block
byte[] b = new byte[dbConfig.getTotalHeaderSize()];
// byte[] b = new byte[4096];
raf.readFully(b, 0, b.Length);
//fill the header
int len = b.Length >> 3, idx = 0; //b.lenght / 8
HeaderSip = new long[len];
HeaderPtr = new int[len];
long startIp, dataPtr2;
for (int i = 0; i < b.Length; i += 8)
{
startIp = Util.getIntLong(b, i);
dataPtr2 = Util.getIntLong(b, i + 4);
if (dataPtr2 == 0) break;
HeaderSip[idx] = startIp;
HeaderPtr[idx] = (int)dataPtr2;
idx++;
}
headerLength = idx;
}
//1. define the index block with the binary search
if (ip == HeaderSip[0])
{
return getByIndexPtr(HeaderPtr[0]);
}
else if (ip == HeaderSip[headerLength - 1])
{
return getByIndexPtr(HeaderPtr[headerLength - 1]);
}
int l = 0, h = headerLength, sptr = 0, eptr = 0;
while (l <= h)
{
int m = (l + h) >> 1;
//perfetc matched, just return it
if (ip == HeaderSip[m])
{
if (m > 0)
{
sptr = HeaderPtr[m - 1];
eptr = HeaderPtr[m];
}
else
{
sptr = HeaderPtr[m];
eptr = HeaderPtr[m + 1];
}
break;
}
//less then the middle value
if (ip < HeaderSip[m])
{
if (m == 0)
{
sptr = HeaderPtr[m];
eptr = HeaderPtr[m + 1];
break;
}
else if (ip > HeaderSip[m - 1])
{
sptr = HeaderPtr[m - 1];
eptr = HeaderPtr[m];
break;
}
h = m - 1;
}
else
{
if (m == headerLength - 1)
{
sptr = HeaderPtr[m - 1];
eptr = HeaderPtr[m];
break;
}
else if (ip <= HeaderSip[m + 1])
{
sptr = HeaderPtr[m];
eptr = HeaderPtr[m + 1];
break;
}
l = m + 1;
}
}
//match nothing just stop it
if (sptr == 0) return null;
//2. search the index blocks to define the data
int blockLen = eptr - sptr, blen = IndexBlock.getIndexBlockLength();
byte[] iBuffer = new byte[blockLen + blen]; //include the right border block
raf.seek(sptr);
raf.readFully(iBuffer, 0, iBuffer.Length);
l = 0; h = blockLen / blen;
long sip, eip, dataptr = 0;
while (l <= h)
{
int m = (l + h) >> 1;
int p = m * blen;
sip = Util.getIntLong(iBuffer, p);
if (ip < sip)
{
h = m - 1;
}
else
{
eip = Util.getIntLong(iBuffer, p + 4);
if (ip > eip)
{
l = m + 1;
}
else
{
dataptr = Util.getIntLong(iBuffer, p + 8);
break;
}
}
}
//not matched
if (dataptr == 0) return null;
//3. get the data
int dataLen = (int)((dataptr >> 24) & 0xFF);
int dataPtr = (int)((dataptr & 0x00FFFFFF));
raf.seek(dataPtr);
byte[] data = new byte[dataLen];
raf.readFully(data, 0, data.Length);
int city_id = (int)Util.getIntLong(data, 0);
//String region = new String(data, 4, data.Length - 4, "UTF-8");
var region = Encoding.UTF8.GetString(data, 4, data.Length);
return new DataBlock(city_id, region, dataPtr);
}
/**
* get the region throught the ip address with b-tree search algorithm
*
* @param ip
* @return DataBlock
* @
*/
public DataBlock btreeSearch(String ip)
{
return btreeSearch(Util.ip2long(ip));
}
/**
* get the region with a int ip address with binary search algorithm
*
* @param ip
* @
*/
public DataBlock binarySearch(long ip)
{
int blen = IndexBlock.getIndexBlockLength();
if (totalIndexBlocks == 0)
{
raf.seek(0L);
byte[] superBytes = new byte[8];
raf.readFully(superBytes, 0, superBytes.Length);
//initialize the global vars
firstIndexPtr = Util.getIntLong(superBytes, 0);
lastIndexPtr = Util.getIntLong(superBytes, 4);
totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr) / blen) + 1;
}
//search the index blocks to define the data
int l = 0, h = totalIndexBlocks;
byte[] buffer = new byte[blen];
long sip, eip, dataptr = 0;
while (l <= h)
{
int m = (l + h) >> 1;
raf.seek(firstIndexPtr + m * blen); //set the file pointer
raf.readFully(buffer, 0, buffer.Length);
sip = Util.getIntLong(buffer, 0);
if (ip < sip)
{
h = m - 1;
}
else
{
eip = Util.getIntLong(buffer, 4);
if (ip > eip)
{
l = m + 1;
}
else
{
dataptr = Util.getIntLong(buffer, 8);
break;
}
}
}
//not matched
if (dataptr == 0) return null;
//get the data
int dataLen = (int)((dataptr >> 24) & 0xFF);
int dataPtr = (int)((dataptr & 0x00FFFFFF));
raf.seek(dataPtr);
byte[] data = new byte[dataLen];
raf.readFully(data, 0, data.Length);
int city_id = (int)Util.getIntLong(data, 0);
//String region = new String(data, 4, data.length - 4, "UTF-8");
var region = Encoding.UTF8.GetString(data, 4, data.Length - 4);
return new DataBlock(city_id, region, dataPtr);
}
/**
* get the region throught the ip address with binary search algorithm
*
* @param ip
* @return DataBlock
* @
*/
public DataBlock binarySearch(String ip)
{
return binarySearch(Util.ip2long(ip));
}
/**
* get the db config
*
* @return DbConfig
*/
public DbConfig getDbConfig()
{
return dbConfig;
}
/**
* close the db
*
* @
*/
public void close()
{
HeaderSip = null; //let gc do its work
HeaderPtr = null;
dbBinStr = null;
raf.close();
}
}
}

View File

@ -0,0 +1,71 @@
using System;
namespace IP2RegionDotNetDbMaker
{
/**
* header block class
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class HeaderBlock
{
/**
* index block start ip address
*/
private long indexStartIp;
/**
* ip address
*/
private int indexPtr;
public HeaderBlock(long indexStartIp, int indexPtr)
{
this.indexStartIp = indexStartIp;
this.indexPtr = indexPtr;
}
public long getIndexStartIp()
{
return indexStartIp;
}
public HeaderBlock setIndexStartIp(long indexStartIp)
{
this.indexStartIp = indexStartIp;
return this;
}
public int getIndexPtr()
{
return indexPtr;
}
public HeaderBlock setIndexPtr(int indexPtr)
{
this.indexPtr = indexPtr;
return this;
}
/**
* get the bytes for db storage
*
* @return byte[]
*/
public byte[] getBytes()
{
/*
* +------------+-----------+
* | 4bytes | 4bytes |
* +------------+-----------+
* start ip index ptr
*/
byte[] b = new byte[8];
Util.writeIntLong(b, 0, indexStartIp);
Util.writeIntLong(b, 4, indexPtr);
return b;
}
}
}

View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net452;net5.0</TargetFrameworks>
<AssemblyName>dbMaker</AssemblyName>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,116 @@
using System;
namespace IP2RegionDotNetDbMaker
{
/**
* item index class
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class IndexBlock
{
private static int LENGTH = 12;
/**
* start ip address
*/
private long startIp;
/**
* end ip address
*/
private long endIp;
/**
* data ptr and data length
*/
private int dataPtr;
/**
* data length
*/
private int dataLen;
public IndexBlock(long startIp, long endIp, int dataPtr, int dataLen)
{
this.startIp = startIp;
this.endIp = endIp;
this.dataPtr = dataPtr;
this.dataLen = dataLen;
}
public long getStartIp()
{
return startIp;
}
public IndexBlock setStartIp(long startIp)
{
this.startIp = startIp;
return this;
}
public long getEndIp()
{
return endIp;
}
public IndexBlock setEndIp(long endIp)
{
this.endIp = endIp;
return this;
}
public int getDataPtr()
{
return dataPtr;
}
public IndexBlock setDataPtr(int dataPtr)
{
this.dataPtr = dataPtr;
return this;
}
public int getDataLen()
{
return dataLen;
}
public IndexBlock setDataLen(int dataLen)
{
this.dataLen = dataLen;
return this;
}
public static int getIndexBlockLength()
{
return LENGTH;
}
/**
* get the bytes for storage
*
* @return byte[]
*/
public byte[] getBytes()
{
/*
* +------------+-----------+-----------+
* | 4bytes | 4bytes | 4bytes |
* +------------+-----------+-----------+
* start ip end ip data ptr + len
*/
byte[] b = new byte[12];
Util.writeIntLong(b, 0, startIp); //start ip
Util.writeIntLong(b, 4, endIp); //end ip
//write the data ptr and the length
long mix = dataPtr | ((dataLen << 24) & 0xFF000000L);
Util.writeIntLong(b, 8, mix);
return b;
}
}
}

View File

@ -0,0 +1,244 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IP2RegionDotNetDbMaker
{
public static class Extensions
{
public static Int32 length(this string str)
{
if (String.IsNullOrWhiteSpace(str))
{
return 0;
}
return str.Length;
}
public static string trim(this string str)
{
return str.Trim();
}
public static char charAt(this string str, Int32 i)
{
return str[i];
}
public static int indexOf(this string str, string value)
{
return str.IndexOf(value);
}
public static int indexOf(this string str, char value, Int32 start)
{
return str.IndexOf(value, start);
}
public static string substring(this string str, Int32 startIndex)
{
return str.Substring(startIndex);
}
public static string substring(this string str, Int32 startIndex, Int32 endIndex)
{
return str.Substring(startIndex, endIndex - startIndex);
}
public static bool equals(this string str1, string str2)
{
return String.Equals(str1, str2, StringComparison.InvariantCultureIgnoreCase);
}
public static string[] split(this string str, string separator)
{
return str.Split(new[] { separator }, StringSplitOptions.None);
}
public static byte[] getBytes(this string str)
{
return Encoding.UTF8.GetBytes(str);
}
public static byte[] getBytes(this string str, string encoding)
{
return Encoding.GetEncoding(encoding).GetBytes(str);
}
public static bool endsWith(this string str, string value)
{
return str.EndsWith(value);
}
}
public class StringBuilder
{
private readonly System.Text.StringBuilder _builder = new System.Text.StringBuilder();
internal StringBuilder append(object value)
{
_builder.Append(value);
return this;
}
internal string toString()
{
return _builder.ToString();
}
}
public class File
{
public File(string ipSrcFile)
{
_fileInfo = new FileInfo(ipSrcFile);
}
private FileInfo _fileInfo;
public FileInfo FileInfo => _fileInfo;
internal bool exists()
{
return _fileInfo.Exists;
}
}
public class LinkedList<T> : List<T>
{
internal T getFirst()
{
return this[0];
}
internal void add(T item)
{
this.Add(item);
}
internal T getLast()
{
return this[this.Count - 1];
}
internal IEnumerable<T> iterator()
{
return this;
}
}
public class HashMap<K, V> : Dictionary<K, V>
{
internal void put(K k, V v)
{
this[k] = v;
}
internal bool containsKey(K key)
{
return this.ContainsKey(key);
}
internal V get(K k)
{
if (containsKey(k))
{
return this[k];
}
return default;
}
}
public class FileReader
{
private File globalRegionFile;
private Queue<String> _lines = new Queue<string>();
public FileReader(File file)
{
this.globalRegionFile = file;
using (var fs = file.FileInfo.OpenRead())
{
using (var sr = new StreamReader(fs))
{
while (!sr.EndOfStream)
{
var line = sr.ReadLine();
_lines.Enqueue(line);
}
}
}
}
internal string readLine()
{
if (_lines.Count > 0)
{
return _lines.Dequeue();
}
return null;
}
internal void close()
{
}
}
public class BufferedReader
{
private FileReader fileReader;
public BufferedReader(FileReader fileReader)
{
this.fileReader = fileReader;
}
internal string readLine()
{
return fileReader.readLine();
}
internal void close()
{
fileReader.close();
}
}
public class RandomAccessFile
{
private string dbFile;
private Stream stream;
internal void seek(long v)
{
stream.Seek(v, SeekOrigin.Begin);
}
internal void write(byte[] vs)
{
stream.Write(vs, 0, vs.Length);
}
internal void readFully(byte[] dbBinStr, int v, int length)
{
stream.Read(dbBinStr, v, length);
}
private string v;
public RandomAccessFile(string dbFile, string v)
{
this.dbFile = dbFile;
this.v = v;
this.stream = new FileStream(dbFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
}
public long length()
{
return this.stream.Length;
}
internal void close()
{
if (stream == null)
{
return;
}
this.stream.Flush();
this.stream.Close();
this.stream = null;
}
internal long getFilePointer()
{
return stream.Position;
}
internal void write(int v)
{
var bytes = BitConverter.GetBytes(v);
stream.Write(bytes, 0, bytes.Length);
}
}
}

View File

@ -0,0 +1,148 @@
using System;
namespace IP2RegionDotNetDbMaker
{
/**
* util class
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class Util
{
/**
* write specfield bytes to a byte array start from offset
*
* @param b
* @param offset
* @param v
* @param bytes
*/
public static void write(byte[] b, int offset, long v, int bytes)
{
for (int i = 0; i < bytes; i++)
{
b[offset++] = (byte)((v >> (8 * i)) & 0xFF);
}
}
/**
* write a int to a byte array
*
* @param b
* @param offet
* @param v
*/
public static void writeIntLong(byte[] b, int offset, long v)
{
b[offset++] = (byte)((v >> 0) & 0xFF);
b[offset++] = (byte)((v >> 8) & 0xFF);
b[offset++] = (byte)((v >> 16) & 0xFF);
b[offset] = (byte)((v >> 24) & 0xFF);
}
/**
* get a int from a byte array start from the specifiled offset
*
* @param b
* @param offset
*/
public static long getIntLong(byte[] b, int offset)
{
return (
((b[offset++] & 0x000000FFL)) |
((b[offset++] << 8) & 0x0000FF00L) |
((b[offset++] << 16) & 0x00FF0000L) |
((b[offset] << 24) & 0xFF000000L)
);
}
/**
* get a int from a byte array start from the specifield offset
*
* @param b
* @param offset
*/
public static int getInt3(byte[] b, int offset)
{
return (
(b[offset++] & 0x000000FF) |
(b[offset++] & 0x0000FF00) |
(b[offset] & 0x00FF0000)
);
}
public static int getInt2(byte[] b, int offset)
{
return (
(b[offset++] & 0x000000FF) |
(b[offset] & 0x0000FF00)
);
}
public static int getInt1(byte[] b, int offset)
{
return (
(b[offset] & 0x000000FF)
);
}
/**
* string ip to long ip
*
* @param ip
* @return long
*/
public static long ip2long(String ip)
{
ip = ip.Trim();
String[] ips = ip.Split('.');
long ip1 = Int64.Parse(ips[0]);
long ip2 = Int64.Parse(ips[1]);
long ip3 = Int64.Parse(ips[2]);
long ip4 = Int64.Parse(ips[3]);
long ip2long = 1L * ip1 * 256 * 256 * 256 + ip2 * 256 * 256 + ip3 * 256 + ip4;
return ip2long;
}
/**
* int to ip string
*
* @param ip
* @return string
*/
public static String long2ip(long ip)
{
StringBuilder sb = new StringBuilder();
sb
.append((ip >> 24) & 0xFF).append('.')
.append((ip >> 16) & 0xFF).append('.')
.append((ip >> 8) & 0xFF).append('.')
.append((ip >> 0) & 0xFF);
return sb.toString();
}
/**
* check the validate of the specifeld ip address
*
* @param ip
* @return boolean
*/
public static bool isIpAddress(String ip)
{
String[] p = ip.split(".");
if (p.Length != 4) return false;
foreach (var pp in p)
{
if (pp.Length > 3) return false;
var val = Int32.Parse(pp);
if (val > 255) return false;
}
return true;
}
}
}

12
maker/java/README.md Normal file
View File

@ -0,0 +1,12 @@
# db maker java implementation
### 1, How to build ?
```
ant all
```
### 2, How to make ?
```
java -jar dbMaker-{version}.jar -src path of ip.merge.txt -region path of global_region.csv -dst ip2region.db target path
```

79
maker/java/build.xml Normal file
View File

@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="ip2region" default="usage" basedir=".">
<!--Initialize-->
<target name="init">
<echo message="------------------------------------------------------------"/>
<echo message="----------BUILDING JCSEG PACKAGE----------------------------"/>
<echo message=""/>
<property name="bottom" value="ip2region 2015-2019"/>
<property name="jars" value="${basedir}"/>
<property name="sources" value="${basedir}/src"/>
<property name="classes" value="${basedir}/classes"/>
<property name="version" value="1.2.2"/>
<property name="api" value="${basedir}/doc"/>
<mkdir dir="${classes}"/>
<mkdir dir="${api}"/>
</target>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<!--Build-->
<target name="build" depends="init">
<echo message="-------------Compiling Application--------------"/>
<javac srcdir="${sources}" destdir="${classes}"></javac>
</target>
<!--Archive-->
<target name="dist" depends="build">
<jar jarfile="${jars}/ip2region-${version}.jar" basedir="${classes}">
<!--class fileter-->
<include name="**/*"/>
<!--manifest information setting-->
<manifest>
<attribute name="Main-Class" value="org.lionsoul.ip2region.test.TestSearcher"/>
<attribute name="Class-Path" value=""/>
</manifest>
</jar>
<jar jarfile="${jars}/dbMaker-${version}.jar" basedir="${classes}" includes="org/lionsoul/ip2region/*">
<!--manifest information setting-->
<manifest>
<attribute name="Main-Class" value="org.lionsoul.ip2region.DbMaker"/>
<attribute name="Class-Path" value=""/>
</manifest>
</jar>
</target>
<!--Java document-->
<target name="all" depends="dist">
<echo message="------------Making Java Document------------------"/>
<javadoc packagenames="org.*"
sourcepath="${sources}"
destdir="${api}"
bottom="${bottom}"
charset="UTF-8"
author="false">
<classpath>
<pathelement location="${classes}"/>
</classpath>
</javadoc>
<jar jarfile="${jars}/ip2region-${version}-javadoc.jar" basedir="${api}" includes="**/*"></jar>
<!--do the data clean up-->
<delete dir="${classes}"/>
<delete dir="${api}"/>
</target>
<target name="usage">
<echo message="*** ip2region ANT Build Script ***"/>
<echo message="Usage: "/>
<echo message=" ant [target]"/>
<echo message=""/>
<echo message=" target : "/>
<echo message=" build : Build Application"/>
<echo message=" dist : Build Application + Archive (JAR)"/>
<echo message=" all : Build Application + Archive + JavaDoc"/>
</target>
</project>

View File

@ -0,0 +1,86 @@
package org.lionsoul.ip2region;
/**
* data block class
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class DataBlock
{
/**
* city id
*/
private int city_id;
/**
* region address
*/
private String region;
/**
* region ptr in the db file
*/
private int dataPtr;
/**
* construct method
*
* @param city_id
* @param region region string
* @param dataPtr data ptr
*/
public DataBlock( int city_id, String region, int dataPtr )
{
this.city_id = city_id;
this.region = region;
this.dataPtr = dataPtr;
}
public DataBlock(int city_id, String region)
{
this(city_id, region, 0);
}
public int getCityId()
{
return city_id;
}
public DataBlock setCityId(int city_id)
{
this.city_id = city_id;
return this;
}
public String getRegion()
{
return region;
}
public DataBlock setRegion(String region)
{
this.region = region;
return this;
}
public int getDataPtr()
{
return dataPtr;
}
public DataBlock setDataPtr(int dataPtr)
{
this.dataPtr = dataPtr;
return this;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append(city_id).append('|').append(region).append('|').append(dataPtr);
return sb.toString();
}
}

View File

@ -0,0 +1,63 @@
package org.lionsoul.ip2region;
/**
* database configuration class
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class DbConfig
{
/**
* total header data block size
*/
private int totalHeaderSize;
/**
* max index data block size
* u should always choice the fastest read block size
*/
private int indexBlockSize;
/**
* construct method
*
* @param totalHeaderSize
* @throws DbMakerConfigException
*/
public DbConfig( int totalHeaderSize ) throws DbMakerConfigException
{
if ( (totalHeaderSize % 8) != 0 ) {
throw new DbMakerConfigException("totalHeaderSize must be times of 8");
}
this.totalHeaderSize = totalHeaderSize;
this.indexBlockSize = 8192; //4 * 2048
}
public DbConfig() throws DbMakerConfigException
{
this(8 * 2048);
}
public int getTotalHeaderSize()
{
return totalHeaderSize;
}
public DbConfig setTotalHeaderSize(int totalHeaderSize)
{
this.totalHeaderSize = totalHeaderSize;
return this;
}
public int getIndexBlockSize()
{
return indexBlockSize;
}
public DbConfig setIndexBlockSize(int dataBlockSize)
{
this.indexBlockSize = dataBlockSize;
return this;
}
}

View File

@ -0,0 +1,408 @@
package org.lionsoul.ip2region;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
/**
* fast ip db maker
*
* db struct:
* 1. header part
* 1): super part:
* +------------+-----------+
* | 4 bytes | 4 bytes |
* +------------+-----------+
* start index ptr, end index ptr
*
* 2): b-tree index part
* +------------+-----------+-----------+-----------+
* | 4bytes | 4bytes | 4bytes | 4bytes | ...
* +------------+-----------+-----------+-----------+
* start ip ptr index ptr
*
* 2. data part:
* +------------+-----------------------+
* | 2bytes | dynamic length |
* +------------+-----------------------+
* data length city_id|Country|Province|Area|City|ISP
*
* 3. index part: (ip range)
* +------------+-----------+---------------+
* | 4bytes | 4bytes | 4bytes |
* +------------+-----------+---------------+
* start ip end ip 3 byte data ptr & 1 byte data length
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class DbMaker
{
/**
* db config
*/
private DbConfig dbConfig;
/**
* ip source file path
*/
private File ipSrcFile;
/**
* buffer
*/
private LinkedList<IndexBlock> indexPool;
private LinkedList<HeaderBlock> headerPool;
/**
* global region Id mapping data
*/
private File globalRegionFile = null;
private HashMap<String, Integer> globalRegionMap = null;
/**
* region and data ptr mapping data
*/
private HashMap<String, DataBlock> regionPtrPool = null;
/**
* construct method
*
* @param config
* @param ipSrcFile tb source ip file
* @param globalRegionFile global_region.csv file offer by lion
* @throws DbMakerConfigException
* @throws IOException
*/
public DbMaker(
DbConfig config,
String ipSrcFile,
String globalRegionFile ) throws DbMakerConfigException, IOException
{
this.dbConfig = config;
this.ipSrcFile = new File(ipSrcFile);
this.globalRegionMap = new HashMap<String, Integer>();
this.regionPtrPool = new HashMap<String, DataBlock>();
if ( globalRegionFile != null ) {
this.globalRegionFile = new File(globalRegionFile);
}
if ( this.ipSrcFile.exists() == false ) {
throw new IOException("Error: Invalid file path " + ipSrcFile);
}
}
/**
* initialize the db file
*
* @param raf
* @throws IOException
*/
private void initDbFile( RandomAccessFile raf ) throws IOException
{
//1. zero fill the header part
raf.seek(0L);
raf.write(new byte[8]); //super block
raf.write(new byte[dbConfig.getTotalHeaderSize()]); //header block
headerPool = new LinkedList<HeaderBlock>();
indexPool = new LinkedList<IndexBlock>();
}
/**
* make the Db file
*
* @param dbFile target output file path
* @throws IOException
*/
public void make(String dbFile) throws IOException
{
//check and load the gloabl region
if ( globalRegionFile != null ) {
System.out.println("+-Try to load the global region data ...");
BufferedReader greader = new BufferedReader(new FileReader(globalRegionFile));
String gline = null;
while ( (gline = greader.readLine()) != null ) {
String[] p = gline.split(",");
if ( p.length != 5 ) continue;
//push the mapping
globalRegionMap.put(p[2], Integer.valueOf(p[0]));
}
greader.close();
System.out.println("|--[Ok]");
}
//alloc the header size
BufferedReader reader = new BufferedReader(new FileReader(this.ipSrcFile));
RandomAccessFile raf = new RandomAccessFile(dbFile, "rw");
//init the db file
initDbFile(raf);
System.out.println("+-Db file initialized.");
//analysis main loop
System.out.println("+-Try to write the data blocks ... ");
String line = null;
while ( ( line = reader.readLine() ) != null ) {
line = line.trim();
if ( line.length() == 0 ) continue;
if ( line.charAt(0) == '#' ) continue;
//1. get the start ip
int sIdx = 0, eIdx = 0;
if ( (eIdx = line.indexOf('|', sIdx + 1)) == -1 ) continue;
String startIp = line.substring(sIdx, eIdx);
//2. get the end ip
sIdx = eIdx + 1;
if ( (eIdx = line.indexOf('|', sIdx + 1)) == -1 ) continue;
String endIp = line.substring(sIdx, eIdx);
//3. get the region
sIdx = eIdx + 1;
String region = line.substring(sIdx);
System.out.println("+-Try to process item " + line);
addDataBlock(raf, startIp, endIp, region);
System.out.println("|--[Ok]");
}
System.out.println("|--Data block flushed!");
System.out.println("|--Data file pointer: "+raf.getFilePointer()+"\n");
//write the index bytes
System.out.println("+-Try to write index blocks ... ");
//record the start block
IndexBlock indexBlock = null;
HeaderBlock hb = null;
indexBlock = indexPool.getFirst();
long indexStartIp = indexBlock.getStartIp(),
indexStratPtr = raf.getFilePointer(), indexEndPtr;
headerPool.add(new HeaderBlock(indexStartIp, (int)(indexStratPtr)));
int blockLength = IndexBlock.getIndexBlockLength();
int counter = 0, shotCounter = (dbConfig.getIndexBlockSize()/blockLength) - 1;
Iterator<IndexBlock> indexIt = indexPool.iterator();
while ( indexIt.hasNext() ) {
indexBlock = indexIt.next();
if ( ++counter >= shotCounter ) {
hb = new HeaderBlock(
indexBlock.getStartIp(),
(int)raf.getFilePointer()
);
headerPool.add(hb);
counter = 0;
}
//write the buffer
raf.write(indexBlock.getBytes());
}
//record the end block
if ( counter > 0 ) {
indexBlock = indexPool.getLast();
hb = new HeaderBlock(
indexBlock.getStartIp(),
((int)raf.getFilePointer()) - IndexBlock.getIndexBlockLength()
);
headerPool.add(hb);
}
indexEndPtr = raf.getFilePointer();
System.out.println("|--[Ok]");
//write the super blocks
System.out.println("+-Try to write the super blocks ... ");
raf.seek(0L); //reset the file pointer
byte[] superBuffer = new byte[8];
Util.writeIntLong(superBuffer, 0, indexStratPtr);
Util.writeIntLong(superBuffer, 4, indexEndPtr - blockLength);
raf.write(superBuffer);
System.out.println("|--[Ok]");
//write the header blocks
System.out.println("+-Try to write the header blocks ... ");
Iterator<HeaderBlock> headerIt = headerPool.iterator();
while ( headerIt.hasNext() ) {
HeaderBlock headerBlock = headerIt.next();
raf.write(headerBlock.getBytes());
}
//write the copyright and the release timestamp info
System.out.println("+-Try to write the copyright and release date info ... ");
raf.seek(raf.length());
Calendar cal = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
String copyright = "Created by lionsoul at " + dateFormat.format(cal.getTime());
raf.write((int)(cal.getTime().getTime()/1000)); //the unix timestamp
raf.write(copyright.getBytes());
System.out.println("|--[Ok]");
reader.close();
raf.close();
}
/**
* internal method to add a new data block record
*
* @param raf
* @param startIp
* @param endIp
* @param region data
*/
private void addDataBlock(
RandomAccessFile raf,
String startIp, String endIp, String region)
{
try {
byte[] data = region.getBytes("UTF-8");
int dataPtr = 0;
/*byte[] city = new byte[4];
int city_id = getCityId(region);
Util.writeIntLong(city, 0, city_id);
dataPtr = (int)raf.getFilePointer();
raf.write(city);
raf.write(data);*/
//check region ptr pool first
if ( regionPtrPool.containsKey(region) ) {
DataBlock dataBlock = regionPtrPool.get(region);
dataPtr = dataBlock.getDataPtr();
System.out.println("dataPtr: " + dataPtr + ", region: " + region);
} else {
byte[] city = new byte[4];
int city_id = getCityId(region);
Util.writeIntLong(city, 0, city_id);
dataPtr = (int)raf.getFilePointer();
raf.write(city);
raf.write(data);
regionPtrPool.put(region, new DataBlock(city_id, region, dataPtr));
}
//add the data index blocks
IndexBlock ib = new IndexBlock(
Util.ip2long(startIp),
Util.ip2long(endIp),
dataPtr,
data.length + 4 //4 bytes for the city id
);
indexPool.add(ib);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* get the city id base on the global region data
*
* @param region
* @return int
*/
public int getCityId( String region )
{
String[] p = region.split("\\|");
if ( p.length != 5 ) return 0;
String key = null;
Integer intv = null;
for ( int i = 3; i >= 0; i-- ) {
if ( p[i].equals("0") ) continue;
if ( i == 3
&& p[i].indexOf("省直辖县级") > -1 ) {
key = p[2]+p[3];
} else {
key = p[i];
}
intv = globalRegionMap.get(key);
if ( intv == null ) return 0;
return intv.intValue();
}
return 0;
}
public DbConfig getDbConfig()
{
return dbConfig;
}
public DbMaker setDbConfig(DbConfig dbConfig)
{
this.dbConfig = dbConfig;
return this;
}
public File getIpSrcFile()
{
return ipSrcFile;
}
public DbMaker setIpSrcFile(File ipSrcFile)
{
this.ipSrcFile = ipSrcFile;
return this;
}
/**
* make this directly a runnable application
* interface to make the database file
*/
public static void main(String args[])
{
String dstDir = "./data/";
String[] path = new String[]{null, null};
for ( int i = 0; i < args.length; i++ ) {
if ( args[i].equals("-src") ) {
path[0] = args[++i];
} else if ( args[i].equals("-region") ) {
path[1] = args[++i];
} else if ( args[i].equals("-dst") ) {
dstDir = args[++i];
}
}
for ( int i = 0; i < path.length; i++ ) {
if ( path[i] == null ) {
System.out.println("Usage: java -jar dbMaker.jar "
+ "-src [source text file path] "
+ "-region [global region file path]");
System.out.println("eg: java -jar dbMaker.jar "
+ "-src ./data/ip.merge.txt -region ./data/origin/global_region.csv");
System.exit(0);
}
}
//check and stdlize the destination directory
if ( ! dstDir.endsWith("/") ) {
dstDir = dstDir + "/";
}
try {
DbConfig config = new DbConfig();
DbMaker dbMaker = new DbMaker(config, path[0], path[1]);
dbMaker.make(dstDir + "ip2region.db");
} catch (DbMakerConfigException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,26 @@
package org.lionsoul.ip2region;
/**
* configuration exception
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class DbMakerConfigException extends Exception
{
private static final long serialVersionUID = 4495714680349884838L;
public DbMakerConfigException( String info )
{
super(info);
}
public DbMakerConfigException( Throwable res )
{
super(res);
}
public DbMakerConfigException( String info, Throwable res )
{
super(info, res);
}
}

View File

@ -0,0 +1,405 @@
package org.lionsoul.ip2region;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* ip db searcher class (Not thread safe)
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class DbSearcher
{
public static final int BTREE_ALGORITHM = 1;
public static final int BINARY_ALGORITHM = 2;
public static final int MEMORY_ALGORITYM = 3;
/**
* db config
*/
private DbConfig dbConfig = null;
/**
* db file access handler
*/
private RandomAccessFile raf = null;
/**
* header blocks buffer
*/
private long[] HeaderSip = null;
private int[] HeaderPtr = null;
private int headerLength;
/**
* super blocks info
*/
private long firstIndexPtr = 0;
private long lastIndexPtr = 0;
private int totalIndexBlocks = 0;
/**
* for memory mode
* the original db binary string
*/
private byte[] dbBinStr = null;
/**
* construct class
*
* @param bdConfig
* @param dbFile
* @throws FileNotFoundException
*/
public DbSearcher( DbConfig dbConfig, String dbFile ) throws FileNotFoundException
{
this.dbConfig = dbConfig;
raf = new RandomAccessFile(dbFile, "r");
}
/**
* construct method with self-define std ip2region bianry string support
* Thanks to the issue from Wendal at https://gitee.com/lionsoul/ip2region/issues/IILFL
*
* @param dbConfig
* @param dbBinStr
*/
public DbSearcher(DbConfig dbConfig, byte[] dbBinStr)
{
this.dbConfig = dbConfig;
this.dbBinStr = dbBinStr;
firstIndexPtr = Util.getIntLong(dbBinStr, 0);
lastIndexPtr = Util.getIntLong(dbBinStr, 4);
totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr)/IndexBlock.getIndexBlockLength()) + 1;
}
/**
* get the region with a int ip address with memory binary search algorithm
*
* @param ip
* @throws IOException
*/
public DataBlock memorySearch(long ip) throws IOException
{
int blen = IndexBlock.getIndexBlockLength();
if ( dbBinStr == null ) {
dbBinStr = new byte[(int)raf.length()];
raf.seek(0L);
raf.readFully(dbBinStr, 0, dbBinStr.length);
//initialize the global vars
firstIndexPtr = Util.getIntLong(dbBinStr, 0);
lastIndexPtr = Util.getIntLong(dbBinStr, 4);
totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr)/blen) + 1;
}
//search the index blocks to define the data
int l = 0, h = totalIndexBlocks;
long sip, eip, dataptr = 0;
while ( l <= h ) {
int m = (l + h) >> 1;
int p = (int)(firstIndexPtr + m * blen);
sip = Util.getIntLong(dbBinStr, p);
if ( ip < sip ) {
h = m - 1;
} else {
eip = Util.getIntLong(dbBinStr, p + 4);
if ( ip > eip ) {
l = m + 1;
} else {
dataptr = Util.getIntLong(dbBinStr, p + 8);
break;
}
}
}
//not matched
if ( dataptr == 0 ) return null;
//get the data
int dataLen = (int)((dataptr >> 24) & 0xFF);
int dataPtr = (int)((dataptr & 0x00FFFFFF));
int city_id = (int)Util.getIntLong(dbBinStr, dataPtr);
String region = new String(dbBinStr, dataPtr + 4, dataLen - 4, "UTF-8");
return new DataBlock(city_id, region, dataPtr);
}
/**
* get the region throught the ip address with memory binary search algorithm
*
* @param ip
* @return DataBlock
* @throws IOException
*/
public DataBlock memorySearch( String ip ) throws IOException
{
return memorySearch(Util.ip2long(ip));
}
/**
* get by index ptr
*
* @param indexPtr
* @throws IOException
*/
public DataBlock getByIndexPtr( long ptr ) throws IOException
{
raf.seek(ptr);
byte[] buffer = new byte[12];
raf.readFully(buffer, 0, buffer.length);
//long startIp = Util.getIntLong(buffer, 0);
//long endIp = Util.getIntLong(buffer, 4);
long extra = Util.getIntLong(buffer, 8);
int dataLen = (int)((extra >> 24) & 0xFF);
int dataPtr = (int)((extra & 0x00FFFFFF));
raf.seek(dataPtr);
byte[] data = new byte[dataLen];
raf.readFully(data, 0, data.length);
int city_id = (int)Util.getIntLong(data, 0);
String region = new String(data, 4, data.length - 4, "UTF-8");
return new DataBlock(city_id, region, dataPtr);
}
/**
* get the region with a int ip address with b-tree algorithm
*
* @param ip
* @throws IOException
*/
public DataBlock btreeSearch( long ip ) throws IOException
{
//check and load the header
if ( HeaderSip == null ) {
raf.seek(8L); //pass the super block
byte[] b = new byte[dbConfig.getTotalHeaderSize()];
// byte[] b = new byte[4096];
raf.readFully(b, 0, b.length);
//fill the header
int len = b.length >> 3, idx = 0; //b.lenght / 8
HeaderSip = new long[len];
HeaderPtr = new int [len];
long startIp, dataPtr;
for ( int i = 0; i < b.length; i += 8 ) {
startIp = Util.getIntLong(b, i);
dataPtr = Util.getIntLong(b, i + 4);
if ( dataPtr == 0 ) break;
HeaderSip[idx] = startIp;
HeaderPtr[idx] = (int)dataPtr;
idx++;
}
headerLength = idx;
}
//1. define the index block with the binary search
if ( ip == HeaderSip[0] ) {
return getByIndexPtr(HeaderPtr[0]);
} else if ( ip == HeaderSip[headerLength-1] ) {
return getByIndexPtr(HeaderPtr[headerLength-1]);
}
int l = 0, h = headerLength, sptr = 0, eptr = 0;
while ( l <= h ) {
int m = (l + h) >> 1;
//perfetc matched, just return it
if ( ip == HeaderSip[m] ) {
if ( m > 0 ) {
sptr = HeaderPtr[m-1];
eptr = HeaderPtr[m ];
} else {
sptr = HeaderPtr[m ];
eptr = HeaderPtr[m+1];
}
break;
}
//less then the middle value
if ( ip < HeaderSip[m] ) {
if ( m == 0 ) {
sptr = HeaderPtr[m ];
eptr = HeaderPtr[m+1];
break;
} else if ( ip > HeaderSip[m-1] ) {
sptr = HeaderPtr[m-1];
eptr = HeaderPtr[m ];
break;
}
h = m - 1;
} else {
if ( m == headerLength - 1 ) {
sptr = HeaderPtr[m-1];
eptr = HeaderPtr[m ];
break;
} else if ( ip <= HeaderSip[m+1] ) {
sptr = HeaderPtr[m ];
eptr = HeaderPtr[m+1];
break;
}
l = m + 1;
}
}
//match nothing just stop it
if ( sptr == 0 ) return null;
//2. search the index blocks to define the data
int blockLen = eptr - sptr, blen = IndexBlock.getIndexBlockLength();
byte[] iBuffer = new byte[blockLen + blen]; //include the right border block
raf.seek(sptr);
raf.readFully(iBuffer, 0, iBuffer.length);
l = 0; h = blockLen / blen;
long sip, eip, dataptr = 0;
while ( l <= h ) {
int m = (l + h) >> 1;
int p = m * blen;
sip = Util.getIntLong(iBuffer, p);
if ( ip < sip ) {
h = m - 1;
} else {
eip = Util.getIntLong(iBuffer, p + 4);
if ( ip > eip ) {
l = m + 1;
} else {
dataptr = Util.getIntLong(iBuffer, p + 8);
break;
}
}
}
//not matched
if ( dataptr == 0 ) return null;
//3. get the data
int dataLen = (int)((dataptr >> 24) & 0xFF);
int dataPtr = (int)((dataptr & 0x00FFFFFF));
raf.seek(dataPtr);
byte[] data = new byte[dataLen];
raf.readFully(data, 0, data.length);
int city_id = (int)Util.getIntLong(data, 0);
String region = new String(data, 4, data.length - 4, "UTF-8");
return new DataBlock(city_id, region, dataPtr);
}
/**
* get the region throught the ip address with b-tree search algorithm
*
* @param ip
* @return DataBlock
* @throws IOException
*/
public DataBlock btreeSearch( String ip ) throws IOException
{
return btreeSearch(Util.ip2long(ip));
}
/**
* get the region with a int ip address with binary search algorithm
*
* @param ip
* @throws IOException
*/
public DataBlock binarySearch( long ip ) throws IOException
{
int blen = IndexBlock.getIndexBlockLength();
if ( totalIndexBlocks == 0 ) {
raf.seek(0L);
byte[] superBytes = new byte[8];
raf.readFully(superBytes, 0, superBytes.length);
//initialize the global vars
firstIndexPtr = Util.getIntLong(superBytes, 0);
lastIndexPtr = Util.getIntLong(superBytes, 4);
totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr)/blen) + 1;
}
//search the index blocks to define the data
int l = 0, h = totalIndexBlocks;
byte[] buffer = new byte[blen];
long sip, eip, dataptr = 0;
while ( l <= h ) {
int m = (l + h) >> 1;
raf.seek(firstIndexPtr + m * blen); //set the file pointer
raf.readFully(buffer, 0, buffer.length);
sip = Util.getIntLong(buffer, 0);
if ( ip < sip ) {
h = m - 1;
} else {
eip = Util.getIntLong(buffer, 4);
if ( ip > eip ) {
l = m + 1;
} else {
dataptr = Util.getIntLong(buffer, 8);
break;
}
}
}
//not matched
if ( dataptr == 0 ) return null;
//get the data
int dataLen = (int)((dataptr >> 24) & 0xFF);
int dataPtr = (int)((dataptr & 0x00FFFFFF));
raf.seek(dataPtr);
byte[] data = new byte[dataLen];
raf.readFully(data, 0, data.length);
int city_id = (int)Util.getIntLong(data, 0);
String region = new String(data, 4, data.length - 4, "UTF-8");
return new DataBlock(city_id, region, dataPtr);
}
/**
* get the region throught the ip address with binary search algorithm
*
* @param ip
* @return DataBlock
* @throws IOException
*/
public DataBlock binarySearch( String ip ) throws IOException
{
return binarySearch(Util.ip2long(ip));
}
/**
* get the db config
*
* @return DbConfig
*/
public DbConfig getDbConfig()
{
return dbConfig;
}
/**
* close the db
*
* @throws IOException
*/
public void close() throws IOException
{
HeaderSip = null; //let gc do its work
HeaderPtr = null;
dbBinStr = null;
raf.close();
}
}

View File

@ -0,0 +1,68 @@
package org.lionsoul.ip2region;
/**
* header block class
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class HeaderBlock
{
/**
* index block start ip address
*/
private long indexStartIp;
/**
* ip address
*/
private int indexPtr;
public HeaderBlock( long indexStartIp, int indexPtr )
{
this.indexStartIp = indexStartIp;
this.indexPtr = indexPtr;
}
public long getIndexStartIp()
{
return indexStartIp;
}
public HeaderBlock setIndexStartIp(long indexStartIp)
{
this.indexStartIp = indexStartIp;
return this;
}
public int getIndexPtr()
{
return indexPtr;
}
public HeaderBlock setIndexPtr(int indexPtr)
{
this.indexPtr = indexPtr;
return this;
}
/**
* get the bytes for db storage
*
* @return byte[]
*/
public byte[] getBytes()
{
/*
* +------------+-----------+
* | 4bytes | 4bytes |
* +------------+-----------+
* start ip index ptr
*/
byte[] b = new byte[8];
Util.writeIntLong(b, 0, indexStartIp);
Util.writeIntLong(b, 4, indexPtr);
return b;
}
}

View File

@ -0,0 +1,113 @@
package org.lionsoul.ip2region;
/**
* item index class
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class IndexBlock
{
private static int LENGTH = 12;
/**
* start ip address
*/
private long startIp;
/**
* end ip address
*/
private long endIp;
/**
* data ptr and data length
*/
private int dataPtr;
/**
* data length
*/
private int dataLen;
public IndexBlock(long startIp, long endIp, int dataPtr, int dataLen)
{
this.startIp = startIp;
this.endIp = endIp;
this.dataPtr = dataPtr;
this.dataLen = dataLen;
}
public long getStartIp()
{
return startIp;
}
public IndexBlock setStartIp(long startIp)
{
this.startIp = startIp;
return this;
}
public long getEndIp()
{
return endIp;
}
public IndexBlock setEndIp(long endIp)
{
this.endIp = endIp;
return this;
}
public int getDataPtr()
{
return dataPtr;
}
public IndexBlock setDataPtr(int dataPtr)
{
this.dataPtr = dataPtr;
return this;
}
public int getDataLen()
{
return dataLen;
}
public IndexBlock setDataLen(int dataLen)
{
this.dataLen = dataLen;
return this;
}
public static int getIndexBlockLength()
{
return LENGTH;
}
/**
* get the bytes for storage
*
* @return byte[]
*/
public byte[] getBytes()
{
/*
* +------------+-----------+-----------+
* | 4bytes | 4bytes | 4bytes |
* +------------+-----------+-----------+
* start ip end ip data ptr + len
*/
byte[] b = new byte[12];
Util.writeIntLong(b, 0, startIp); //start ip
Util.writeIntLong(b, 4, endIp); //end ip
//write the data ptr and the length
long mix = dataPtr | ((dataLen << 24) & 0xFF000000L);
Util.writeIntLong(b, 8, mix);
return b;
}
}

View File

@ -0,0 +1,143 @@
package org.lionsoul.ip2region;
/**
* util class
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class Util
{
/**
* write specfield bytes to a byte array start from offset
*
* @param b
* @param offset
* @param v
* @param bytes
*/
public static void write( byte[] b, int offset, long v, int bytes)
{
for ( int i = 0; i < bytes; i++ ) {
b[offset++] = (byte)((v >>> (8 * i)) & 0xFF);
}
}
/**
* write a int to a byte array
*
* @param b
* @param offet
* @param v
*/
public static void writeIntLong( byte[] b, int offset, long v )
{
b[offset++] = (byte)((v >> 0) & 0xFF);
b[offset++] = (byte)((v >> 8) & 0xFF);
b[offset++] = (byte)((v >> 16) & 0xFF);
b[offset ] = (byte)((v >> 24) & 0xFF);
}
/**
* get a int from a byte array start from the specifiled offset
*
* @param b
* @param offset
*/
public static long getIntLong( byte[] b, int offset )
{
return (
((b[offset++] & 0x000000FFL)) |
((b[offset++] << 8) & 0x0000FF00L) |
((b[offset++] << 16) & 0x00FF0000L) |
((b[offset ] << 24) & 0xFF000000L)
);
}
/**
* get a int from a byte array start from the specifield offset
*
* @param b
* @param offset
*/
public static int getInt3( byte[] b, int offset )
{
return (
(b[offset++] & 0x000000FF) |
(b[offset++] & 0x0000FF00) |
(b[offset ] & 0x00FF0000)
);
}
public static int getInt2( byte[] b, int offset )
{
return (
(b[offset++] & 0x000000FF) |
(b[offset ] & 0x0000FF00)
);
}
public static int getInt1( byte[] b, int offset )
{
return (
(b[offset] & 0x000000FF)
);
}
/**
* string ip to long ip
*
* @param ip
* @return long
*/
public static long ip2long( String ip )
{
String[] p = ip.split("\\.");
if ( p.length != 4 ) return 0;
int p1 = ((Integer.valueOf(p[0]) << 24) & 0xFF000000);
int p2 = ((Integer.valueOf(p[1]) << 16) & 0x00FF0000);
int p3 = ((Integer.valueOf(p[2]) << 8) & 0x0000FF00);
int p4 = ((Integer.valueOf(p[3]) << 0) & 0x000000FF);
return ((p1 | p2 | p3 | p4) & 0xFFFFFFFFL);
}
/**
* int to ip string
*
* @param ip
* @return string
*/
public static String long2ip( long ip )
{
StringBuilder sb = new StringBuilder();
sb
.append((ip >> 24) & 0xFF).append('.')
.append((ip >> 16) & 0xFF).append('.')
.append((ip >> 8) & 0xFF).append('.')
.append((ip >> 0) & 0xFF);
return sb.toString();
}
/**
* check the validate of the specifeld ip address
*
* @param ip
* @return boolean
*/
public static boolean isIpAddress( String ip )
{
String[] p = ip.split("\\.");
if ( p.length != 4 ) return false;
for ( String pp : p ) {
if ( pp.length() > 3 ) return false;
int val = Integer.valueOf(pp);
if ( val > 255 ) return false;
}
return true;
}
}

View File

@ -0,0 +1,35 @@
package org.lionsoul.ip2region.test;
import java.io.IOException;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbMaker;
import org.lionsoul.ip2region.DbMakerConfigException;
/**
* project test script
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class TestMaker
{
public static void main(String[] argv)
{
try {
DbConfig config = new DbConfig();
DbMaker dbMaker = new DbMaker(
config,
"/data0/code/java/ip2region-dev/data/ip.merge.txt",
"/data0/code/java/ip2region-dev/data/origin/global_region.csv"
);
dbMaker.make("/data0/code/java/ip2region-dev/data/ip2region.db");
} catch (DbMakerConfigException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,120 @@
package org.lionsoul.ip2region.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbMakerConfigException;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;
/**
* project test script
*
* @author chenxin<chenxin619315@gmail.com>
*/
public class TestSearcher
{
public static void main(String[] argv)
{
if ( argv.length == 0 ) {
System.out.println("| Usage: java -jar ip2region-{version}.jar [ip2region db file]");
return;
}
File file = new File(argv[0]);
if ( file.exists() == false ) {
System.out.println("Error: Invalid ip2region.db file");
return;
}
int algorithm = DbSearcher.BTREE_ALGORITHM;
String algoName = "B-tree";
if ( argv.length > 1 ) {
if ( argv[1].equalsIgnoreCase("binary")) {
algoName = "Binary";
algorithm = DbSearcher.BINARY_ALGORITHM;
} else if ( argv[1].equalsIgnoreCase("memory") ) {
algoName = "Memory";
algorithm = DbSearcher.MEMORY_ALGORITYM;
}
}
try {
System.out.println("initializing "+algoName+" ... ");
DbConfig config = new DbConfig();
DbSearcher searcher = new DbSearcher(config, argv[0]);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//define the method
Method method = null;
switch ( algorithm )
{
case DbSearcher.BTREE_ALGORITHM:
method = searcher.getClass().getMethod("btreeSearch", String.class);
break;
case DbSearcher.BINARY_ALGORITHM:
method = searcher.getClass().getMethod("binarySearch", String.class);
break;
case DbSearcher.MEMORY_ALGORITYM:
method = searcher.getClass().getMethod("memorySearch", String.class);
break;
}
System.out.println("+----------------------------------+");
System.out.println("| ip2region test shell |");
System.out.println("| Author: chenxin619315@gmail.com |");
System.out.println("| Type 'quit' to exit program |");
System.out.println("+----------------------------------+");
double sTime = 0, cTime = 0;
String line = null;
DataBlock dataBlock = null;
while ( true ) {
System.out.print("ip2region>> ");
line = reader.readLine().trim();
if ( line.length() < 2 ) continue;
if ( line.equalsIgnoreCase("quit") ) break;
if ( Util.isIpAddress(line) == false ) {
System.out.println("Error: Invalid ip address");
continue;
}
sTime = System.nanoTime();
dataBlock = (DataBlock) method.invoke(searcher, line);
cTime = (System.nanoTime() - sTime) / 1000000;
System.out.printf("%s in %.5f millseconds\n", dataBlock, cTime);
}
reader.close();
searcher.close();
System.out.println("+--Bye");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DbMakerConfigException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,96 @@
package org.lionsoul.ip2region.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbMakerConfigException;
import org.lionsoul.ip2region.DbSearcher;
/**
* data check class
*
* @author koma<komazhang@foxmail.com>
**/
public class TestUnit {
public static void main(String[] args) {
try {
DbSearcher _searcher = new DbSearcher(new DbConfig(), "./data/ip2region.db");
BufferedReader bfr = new BufferedReader(new FileReader("./data/ip.merge.txt"));
BufferedWriter bwr = new BufferedWriter(new FileWriter("./data/error_log.txt", true));
int errCount = 0;
int lineCount = 0;
String str = null;
while ( (str = bfr.readLine()) != null ) {
StringBuffer line = new StringBuffer(str);
//get first ip
int first_idx = line.indexOf("|");
String first_ip = line.substring(0, first_idx);
line = new StringBuffer( line.substring(first_idx + 1) );
//get second ip
int second_idx = line.indexOf("|");
String second_ip = line.substring(0, second_idx);
//get addr
String source_region = line.substring(second_idx + 1);
//search from DbSearcher
System.out.println("+---Start, start to search");
System.out.println("+---[Info]: Source region = "+source_region);
System.out.println("+---[Info]: Step1, search for first IP: "+first_ip);
DataBlock fdata = _searcher.binarySearch(first_ip);
if ( ! fdata.getRegion().equalsIgnoreCase( source_region ) ) {
System.out.println("[Error]: Search first IP failed, DB region = "+fdata.getRegion());
bwr.write("[Source]: Region: "+fdata.getRegion());
bwr.newLine();
bwr.write("[Source]: First Ip: "+first_ip);
bwr.newLine();
bwr.write("[DB]: Region: "+fdata.getRegion());
bwr.newLine();
bwr.flush();
errCount++;
}
System.out.println("+---[Info]: Step2, search for second IP: "+second_ip);
DataBlock sdata = _searcher.btreeSearch(second_ip);
if ( ! sdata.getRegion().equalsIgnoreCase( source_region ) ) {
System.out.println("[Error]: Search second IP failed, DB region = "+sdata.getRegion());
bwr.write("[Source]: Region: "+sdata.getRegion());
bwr.newLine();
bwr.write("[Source]: First Ip: "+second_ip);
bwr.newLine();
bwr.write("[DB]: Region: "+sdata.getRegion());
bwr.newLine();
bwr.flush();
errCount++;
}
lineCount++;
}
bwr.close();
bfr.close();
System.out.println("+---Done, search complished");
System.out.println("+---Statistics, Error count = "+errCount
+", Total line = "+lineCount
+", Fail ratio = "+((float)(errCount/lineCount))*100+"%");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DbMakerConfigException e) {
e.printStackTrace();
} catch ( Exception e ) {
e.printStackTrace();
}
}
}