Skip to content

Commit

Permalink
Update the lua interface and add setDbFile interface
Browse files Browse the repository at this point in the history
  • Loading branch information
lionsoul2014 committed Oct 4, 2018
1 parent 5f871ba commit a2d2509
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*.o
*.pyc
*~
*.log
*.la
*.so
META-INF/

# Binary Files #
Expand Down Expand Up @@ -40,4 +43,4 @@ target

# Nodejs
/binding/nodejs/tests/unitTests/__snapshots__
/binding/nodejs/coverage
/binding/nodejs/coverage
20 changes: 15 additions & 5 deletions binding/lua/Ip2region.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand Down Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions binding/lua/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 8 additions & 6 deletions binding/lua/testSearcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a2d2509

Please sign in to comment.