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
5
.gitignore
vendored
5
.gitignore
vendored
@ -3,6 +3,9 @@
|
||||
*.o
|
||||
*.pyc
|
||||
*~
|
||||
*.log
|
||||
*.la
|
||||
*.so
|
||||
META-INF/
|
||||
|
||||
# Binary Files #
|
||||
@ -40,4 +43,4 @@ target
|
||||
|
||||
# Nodejs
|
||||
/binding/nodejs/tests/unitTests/__snapshots__
|
||||
/binding/nodejs/coverage
|
||||
/binding/nodejs/coverage
|
||||
|
||||
@ -22,11 +22,11 @@ local _M = {
|
||||
};
|
||||
|
||||
_G["Ip2region"] = _M;
|
||||
function _M:new(obj)
|
||||
obj = obj or {};
|
||||
setmetatable(obj, {__index = self});
|
||||
return obj;
|
||||
end
|
||||
-- function _M:new(obj)
|
||||
-- obj = obj or {};
|
||||
-- setmetatable(obj, {__index = self});
|
||||
-- return obj;
|
||||
-- end
|
||||
|
||||
|
||||
|
||||
@ -109,6 +109,16 @@ function get_file_contents(file)
|
||||
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
|
||||
|
||||
@ -27,22 +27,23 @@ ip2region>>
|
||||
* 2, 通过如下流程在你的lua程序中使用
|
||||
```lua
|
||||
-- 包含模块
|
||||
require "Ip2region";
|
||||
local ip2region = require "Ip2region";
|
||||
|
||||
-- 创建查询对象
|
||||
-- dbFile表示ip2region.db数据库文件的地址
|
||||
local searcher = Ip2region:new({dbFile: "ip2region.db file path"});
|
||||
-- 设置ip2region.db的文件地址,dbFile表示ip2region.db数据库文件的地址
|
||||
-- 或者通过设置属性,ip2region.dbFile = "ip2region.db file path";
|
||||
ip2region:setDbFile("ip2region.db file path");
|
||||
local data;
|
||||
|
||||
-- 查询,备注请使用“:”调用方法,不要使用“.”
|
||||
-- 1,binary查询
|
||||
data = searcher:binarySearch("101.233.153.103");
|
||||
data = ip2region:binarySearch("101.233.153.103");
|
||||
|
||||
-- 2,btree查询
|
||||
data = searcher:btreeSearch("101.233.153.103");
|
||||
data = ip2region:btreeSearch("101.233.153.103");
|
||||
|
||||
-- 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);
|
||||
|
||||
@ -12,13 +12,15 @@ Usage: lua testSearcher.lua [ip2region db file] [algorithm]
|
||||
os.exit();
|
||||
end
|
||||
|
||||
local Ip2region = require "Ip2region";
|
||||
local ip2region = require "Ip2region";
|
||||
-- local cjson = require "cjson";
|
||||
-- local socket = require "socket";
|
||||
|
||||
|
||||
-- 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";
|
||||
if ( arg[2] ~= nil ) then
|
||||
local arg_2 = string.lower(arg[2]);
|
||||
@ -51,17 +53,17 @@ while ( true ) do
|
||||
break;
|
||||
elseif ( line == "quit" ) then
|
||||
break;
|
||||
elseif ( searcher:ip2long(line) == nil ) then
|
||||
elseif ( ip2region:ip2long(line) == nil ) then
|
||||
print("Invalid ip address=", line);
|
||||
else
|
||||
local data;
|
||||
local s_time = os.clock();
|
||||
if ( algorithm == "btree" ) then
|
||||
data = searcher:btreeSearch(line);
|
||||
data = ip2region:btreeSearch(line);
|
||||
elseif ( algorithm == "binary" ) then
|
||||
data = searcher:binarySearch(line);
|
||||
data = ip2region:binarySearch(line);
|
||||
elseif ( algorithm == "memory" ) then
|
||||
data = searcher:memorySearch(line);
|
||||
data = ip2region:memorySearch(line);
|
||||
end
|
||||
|
||||
local cost_time = (os.clock() - s_time) * 1000; -- to millseconds
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user