Optimize the lua module by add the __index and the __tostring meta-method
This commit is contained in:
parent
e9a08e27a3
commit
7032830d23
|
|
@ -11,10 +11,10 @@ local INDEX_BLOCK_LENGTH = 12;
|
||||||
local TOTAL_HEADER_LENGTH = 8192;
|
local TOTAL_HEADER_LENGTH = 8192;
|
||||||
local _M = {
|
local _M = {
|
||||||
dbFile = "",
|
dbFile = "",
|
||||||
dbFileHandler = nil,
|
dbFileHandler = "",
|
||||||
dbBinStr = "",
|
dbBinStr = "",
|
||||||
HeaderSip = nil,
|
HeaderSip = "",
|
||||||
HeaderPtr = nil,
|
HeaderPtr = "",
|
||||||
headerLen = 0,
|
headerLen = 0,
|
||||||
firstIndexPtr = 0,
|
firstIndexPtr = 0,
|
||||||
lastIndexPtr = 0,
|
lastIndexPtr = 0,
|
||||||
|
|
@ -22,12 +22,35 @@ local _M = {
|
||||||
};
|
};
|
||||||
|
|
||||||
_G["Ip2region"] = _M;
|
_G["Ip2region"] = _M;
|
||||||
-- function _M:new(obj)
|
|
||||||
-- obj = obj or {};
|
|
||||||
-- setmetatable(obj, {__index = self});
|
|
||||||
-- return obj;
|
|
||||||
-- end
|
|
||||||
|
|
||||||
|
-- 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(obj)
|
||||||
|
obj = obj or {};
|
||||||
|
setmetatable(obj, _M);
|
||||||
|
return obj;
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
|
|
@ -203,11 +226,9 @@ function _M:binarySearch(ip)
|
||||||
|
|
||||||
if ( self.totalBlocks == 0 ) then
|
if ( self.totalBlocks == 0 ) then
|
||||||
-- check and open the original db file
|
-- check and open the original db file
|
||||||
if ( self.dbFileHandler == nil ) then
|
self.dbFileHandler = io.open(self.dbFile, "r");
|
||||||
self.dbFileHandler = io.open(self.dbFile, "r");
|
if ( not self.dbFileHandler ) then
|
||||||
if ( not self.dbFileHandler ) then
|
return nil;
|
||||||
return nil;
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
self.dbFileHandler:seek("set", 0);
|
self.dbFileHandler:seek("set", 0);
|
||||||
|
|
@ -278,11 +299,9 @@ function _M:btreeSearch(ip)
|
||||||
-- check and load the header
|
-- check and load the header
|
||||||
if ( self.headerLen == 0 ) then
|
if ( self.headerLen == 0 ) then
|
||||||
-- check and open the original db file
|
-- check and open the original db file
|
||||||
if ( self.dbFileHandler == nil ) then
|
self.dbFileHandler = io.open(self.dbFile, 'r');
|
||||||
self.dbFileHandler = io.open(self.dbFile, 'r');
|
if ( not self.dbFileHandler ) then
|
||||||
if ( not self.dbFileHandler ) then
|
return nil;
|
||||||
return nil;
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
self.dbFileHandler:seek("set", 8);
|
self.dbFileHandler:seek("set", 8);
|
||||||
|
|
@ -400,4 +419,18 @@ function _M:btreeSearch(ip)
|
||||||
end
|
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;
|
return _M;
|
||||||
|
|
|
||||||
|
|
@ -27,15 +27,20 @@ ip2region>>
|
||||||
* 2, 通过如下流程在你的lua程序中使用
|
* 2, 通过如下流程在你的lua程序中使用
|
||||||
```lua
|
```lua
|
||||||
-- 包含模块
|
-- 包含模块
|
||||||
local ip2region = require "Ip2region";
|
require "Ip2region";
|
||||||
|
|
||||||
-- 创建查询对象
|
-- 创建查询对象
|
||||||
-- 设置ip2region.db的文件地址,dbFile表示ip2region.db数据库文件的地址
|
-- 设置ip2region.db的文件地址,dbFile表示ip2region.db数据库文件的地址
|
||||||
-- 或者通过设置属性,ip2region.dbFile = "ip2region.db file path";
|
local ip2region = Ip2region:new({dbFile = "ip2region.db file path"});
|
||||||
ip2region:setDbFile("ip2region.db file path");
|
|
||||||
|
-- 也可以通过如下方式设置dbFile
|
||||||
|
-- ip2region.dbFile = "ip2region.db file path";
|
||||||
|
-- ip2region.setDbFile("ip2region.db file path");
|
||||||
|
|
||||||
|
|
||||||
local data;
|
local data;
|
||||||
|
|
||||||
-- 查询,备注请使用“:”调用方法,不要使用“.”
|
-- 查询,备注,尽量请使用“:”调用方法,使用“.”需要主动传递ip2region对象参数
|
||||||
-- 1,binary查询
|
-- 1,binary查询
|
||||||
data = ip2region:binarySearch("101.233.153.103");
|
data = ip2region:binarySearch("101.233.153.103");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,15 +12,20 @@ Usage: lua testSearcher.lua [ip2region db file] [algorithm]
|
||||||
os.exit();
|
os.exit();
|
||||||
end
|
end
|
||||||
|
|
||||||
local ip2region = require "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
|
||||||
-- set the dbFile
|
-- Create a new ip2region object by the new interface
|
||||||
|
local ip2region = Ip2region:new({dbFile = arg[1]});
|
||||||
|
|
||||||
|
-- reset the dbFile by the follow two ways:
|
||||||
-- ip2region.dbFile = arg[1];
|
-- ip2region.dbFile = arg[1];
|
||||||
ip2region:setDbFile(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]);
|
||||||
|
|
@ -74,3 +79,7 @@ while ( true ) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- close the object
|
||||||
|
-- print(ip2region);
|
||||||
|
ip2region:close();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue