Optimize the lua module by add the __index and the __tostring meta-method

This commit is contained in:
lionsoul 2018-10-05 10:55:24 +08:00
parent e9a08e27a3
commit 7032830d23
3 changed files with 72 additions and 25 deletions

View File

@ -11,10 +11,10 @@ local INDEX_BLOCK_LENGTH = 12;
local TOTAL_HEADER_LENGTH = 8192;
local _M = {
dbFile = "",
dbFileHandler = nil,
dbFileHandler = "",
dbBinStr = "",
HeaderSip = nil,
HeaderPtr = nil,
HeaderSip = "",
HeaderPtr = "",
headerLen = 0,
firstIndexPtr = 0,
lastIndexPtr = 0,
@ -22,12 +22,35 @@ local _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
-- check and open the original db file
if ( self.dbFileHandler == nil ) then
self.dbFileHandler = io.open(self.dbFile, "r");
if ( not self.dbFileHandler ) then
return nil;
end
self.dbFileHandler = io.open(self.dbFile, "r");
if ( not self.dbFileHandler ) then
return nil;
end
self.dbFileHandler:seek("set", 0);
@ -278,11 +299,9 @@ function _M:btreeSearch(ip)
-- check and load the header
if ( self.headerLen == 0 ) then
-- check and open the original db file
if ( self.dbFileHandler == nil ) then
self.dbFileHandler = io.open(self.dbFile, 'r');
if ( not self.dbFileHandler ) then
return nil;
end
self.dbFileHandler = io.open(self.dbFile, 'r');
if ( not self.dbFileHandler ) then
return nil;
end
self.dbFileHandler:seek("set", 8);
@ -400,4 +419,18 @@ function _M:btreeSearch(ip)
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;

View File

@ -27,15 +27,20 @@ ip2region>>
* 2, 通过如下流程在你的lua程序中使用
```lua
-- 包含模块
local ip2region = require "Ip2region";
require "Ip2region";
-- 创建查询对象
-- 设置ip2region.db的文件地址dbFile表示ip2region.db数据库文件的地址
-- 或者通过设置属性ip2region.dbFile = "ip2region.db file path";
ip2region:setDbFile("ip2region.db file path");
local ip2region = Ip2region:new({dbFile = "ip2region.db file path"});
-- 也可以通过如下方式设置dbFile
-- ip2region.dbFile = "ip2region.db file path";
-- ip2region.setDbFile("ip2region.db file path");
local data;
-- 查询,备注请使用“:”调用方法,不要使用“.”
-- 查询,备注,尽量请使用“:”调用方法,使用“.”需要主动传递ip2region对象参数
-- 1binary查询
data = ip2region:binarySearch("101.233.153.103");

View File

@ -12,15 +12,20 @@ Usage: lua testSearcher.lua [ip2region db file] [algorithm]
os.exit();
end
local ip2region = require "Ip2region";
require "Ip2region";
-- local cjson = require "cjson";
-- local socket = require "socket";
-- 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:setDbFile(arg[1]);
-- ip2region:setDbFile(arg[1]);
local algorithm = "btree";
if ( arg[2] ~= nil ) then
local arg_2 = string.lower(arg[2]);
@ -74,3 +79,7 @@ while ( true ) do
end
end
end
-- close the object
-- print(ip2region);
ip2region:close();