From 1701f49e45276a8dcfed8719605b80cd29aebfc4 Mon Sep 17 00:00:00 2001 From: lionsoul Date: Thu, 4 Oct 2018 10:22:01 +0800 Subject: [PATCH] Update the lua interface and add setDbFile interface --- .gitignore | 5 ++++- binding/lua/Ip2region.lua | 20 +++++++++++++++----- binding/lua/README.md | 13 +++++++------ binding/lua/testSearcher.lua | 14 ++++++++------ 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index f6df81b..1d9d2de 100644 --- a/.gitignore +++ b/.gitignore @@ -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 \ No newline at end of file +/binding/nodejs/coverage diff --git a/binding/lua/Ip2region.lua b/binding/lua/Ip2region.lua index 425038f..7eb8ae5 100644 --- a/binding/lua/Ip2region.lua +++ b/binding/lua/Ip2region.lua @@ -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 diff --git a/binding/lua/README.md b/binding/lua/README.md index 925d930..8f4becd 100644 --- a/binding/lua/README.md +++ b/binding/lua/README.md @@ -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); diff --git a/binding/lua/testSearcher.lua b/binding/lua/testSearcher.lua index 0dae8e7..e7c4831 100644 --- a/binding/lua/testSearcher.lua +++ b/binding/lua/testSearcher.lua @@ -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