mirror of
https://github.com/lionsoul2014/ip2region.git
synced 2025-12-08 19:25:22 +00:00
Update the lua interface and add setDbFile interface
This commit is contained in:
parent
b1463a23a0
commit
1701f49e45
3
.gitignore
vendored
3
.gitignore
vendored
@ -3,6 +3,9 @@
|
|||||||
*.o
|
*.o
|
||||||
*.pyc
|
*.pyc
|
||||||
*~
|
*~
|
||||||
|
*.log
|
||||||
|
*.la
|
||||||
|
*.so
|
||||||
META-INF/
|
META-INF/
|
||||||
|
|
||||||
# Binary Files #
|
# Binary Files #
|
||||||
|
|||||||
@ -22,11 +22,11 @@ local _M = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
_G["Ip2region"] = _M;
|
_G["Ip2region"] = _M;
|
||||||
function _M:new(obj)
|
-- function _M:new(obj)
|
||||||
obj = obj or {};
|
-- obj = obj or {};
|
||||||
setmetatable(obj, {__index = self});
|
-- setmetatable(obj, {__index = self});
|
||||||
return obj;
|
-- return obj;
|
||||||
end
|
-- end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -109,6 +109,16 @@ function get_file_contents(file)
|
|||||||
end
|
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
|
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
|
then search the memory only and this will a lot faster than disk base search
|
||||||
|
|||||||
@ -27,22 +27,23 @@ ip2region>>
|
|||||||
* 2, 通过如下流程在你的lua程序中使用
|
* 2, 通过如下流程在你的lua程序中使用
|
||||||
```lua
|
```lua
|
||||||
-- 包含模块
|
-- 包含模块
|
||||||
require "Ip2region";
|
local ip2region = require "Ip2region";
|
||||||
|
|
||||||
-- 创建查询对象
|
-- 创建查询对象
|
||||||
-- dbFile表示ip2region.db数据库文件的地址
|
-- 设置ip2region.db的文件地址,dbFile表示ip2region.db数据库文件的地址
|
||||||
local searcher = Ip2region:new({dbFile: "ip2region.db file path"});
|
-- 或者通过设置属性,ip2region.dbFile = "ip2region.db file path";
|
||||||
|
ip2region:setDbFile("ip2region.db file path");
|
||||||
local data;
|
local data;
|
||||||
|
|
||||||
-- 查询,备注请使用“:”调用方法,不要使用“.”
|
-- 查询,备注请使用“:”调用方法,不要使用“.”
|
||||||
-- 1,binary查询
|
-- 1,binary查询
|
||||||
data = searcher:binarySearch("101.233.153.103");
|
data = ip2region:binarySearch("101.233.153.103");
|
||||||
|
|
||||||
-- 2,btree查询
|
-- 2,btree查询
|
||||||
data = searcher:btreeSearch("101.233.153.103");
|
data = ip2region:btreeSearch("101.233.153.103");
|
||||||
|
|
||||||
-- 3,memory查询
|
-- 3,memory查询
|
||||||
data = searcher:memorySearch("101.233.153.103");
|
data = ip2region:memorySearch("101.233.153.103");
|
||||||
|
|
||||||
-- 返回结果如下
|
-- 返回结果如下
|
||||||
print("city_id=", data.city_id, "region=", data.region);
|
print("city_id=", data.city_id, "region=", data.region);
|
||||||
|
|||||||
@ -12,13 +12,15 @@ Usage: lua testSearcher.lua [ip2region db file] [algorithm]
|
|||||||
os.exit();
|
os.exit();
|
||||||
end
|
end
|
||||||
|
|
||||||
local Ip2region = require "Ip2region";
|
local ip2region = require "Ip2region";
|
||||||
-- local cjson = require "cjson";
|
-- local cjson = require "cjson";
|
||||||
-- local socket = require "socket";
|
-- local socket = require "socket";
|
||||||
|
|
||||||
|
|
||||||
-- check and parse the dbFile and the method algorithm
|
-- check and parse the dbFile and the method algorithm
|
||||||
local searcher = Ip2region:new({dbFile = arg[1]});
|
-- set the dbFile
|
||||||
|
-- ip2region.dbFile = arg[1];
|
||||||
|
ip2region:setDbFile(arg[1]);
|
||||||
local algorithm = "btree";
|
local algorithm = "btree";
|
||||||
if ( arg[2] ~= nil ) then
|
if ( arg[2] ~= nil ) then
|
||||||
local arg_2 = string.lower(arg[2]);
|
local arg_2 = string.lower(arg[2]);
|
||||||
@ -51,17 +53,17 @@ while ( true ) do
|
|||||||
break;
|
break;
|
||||||
elseif ( line == "quit" ) then
|
elseif ( line == "quit" ) then
|
||||||
break;
|
break;
|
||||||
elseif ( searcher:ip2long(line) == nil ) then
|
elseif ( ip2region:ip2long(line) == nil ) then
|
||||||
print("Invalid ip address=", line);
|
print("Invalid ip address=", line);
|
||||||
else
|
else
|
||||||
local data;
|
local data;
|
||||||
local s_time = os.clock();
|
local s_time = os.clock();
|
||||||
if ( algorithm == "btree" ) then
|
if ( algorithm == "btree" ) then
|
||||||
data = searcher:btreeSearch(line);
|
data = ip2region:btreeSearch(line);
|
||||||
elseif ( algorithm == "binary" ) then
|
elseif ( algorithm == "binary" ) then
|
||||||
data = searcher:binarySearch(line);
|
data = ip2region:binarySearch(line);
|
||||||
elseif ( algorithm == "memory" ) then
|
elseif ( algorithm == "memory" ) then
|
||||||
data = searcher:memorySearch(line);
|
data = ip2region:memorySearch(line);
|
||||||
end
|
end
|
||||||
|
|
||||||
local cost_time = (os.clock() - s_time) * 1000; -- to millseconds
|
local cost_time = (os.clock() - s_time) * 1000; -- to millseconds
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user