small clean up and bug fix

This commit is contained in:
Coder12a 2018-11-08 16:15:35 -06:00
parent 1807225d7b
commit ab7337ba83

View File

@ -34,6 +34,10 @@ function colddb.get_db(directory)
indexes = false,
add_to_mem_pool = true,
}
-- make tables weak so the garbage-collector will remove unused data
setmetatable(db.tags, {__mode = "kv"})
setmetatable(db.mem_pool, {__mode = "kv"})
setmetatable(db.indexes_pool, {__mode = "kv"})
return db
end
@ -269,7 +273,7 @@ function colddb.create_index_table(db,tag_name)
if f then
f:seek("set")
f:write("0")
db.indexes_pool[string.format("%s%s",t,name)] = {file = f,needs_flushing = false,deleted_items = {},iterating = false,inuse = false}
db.indexes_pool[string.format("%s%s",t,name)] = {file = f,needs_flushing = false,deleted_items = {},iterating = false}
return true
end
return false
@ -295,7 +299,7 @@ function colddb.open_index_table(db,tag_name)
end
local f = io.open(p, "r+")
if f then
db.indexes_pool[cs] = {file = f,needs_flushing = false,deleted_items = {},iterating = false,inuse = true}
db.indexes_pool[cs] = {file = f,needs_flushing = false,deleted_items = {},iterating = false}
return f
end
return nil
@ -320,7 +324,6 @@ function colddb.append_index_table(db,key,tag_name)
fl:flush()
end
db.indexes_pool[cs].needs_flushing = true
db.indexes_pool[cs].inuse = true
fl:seek("end")
fl:write(string.format("\n%s",key))
fl:seek("set")
@ -334,7 +337,6 @@ function colddb.append_index_table(db,key,tag_name)
fl:flush()
end
db.indexes_pool[cs].needs_flushing = true
db.indexes_pool[cs].inuse = true
local c = 0
for i in pairs(key) do
fl:seek("end")
@ -367,7 +369,6 @@ function colddb.get_count(db,tag_name)
end
fl:seek("set")
local count = tonumber(fl:read("*l"))
db.indexes_pool[cs].inuse = true
return count
end
return nil
@ -375,7 +376,6 @@ end
local function iterate(db,func_on_iterate,end_func,cycles_per_tick,count,cs,args)
local f = db.indexes_pool[cs]
f.inuse = true
local fl = f.file
for i=1,cycles_per_tick do
if count < 1 then
@ -394,6 +394,11 @@ local function iterate(db,func_on_iterate,end_func,cycles_per_tick,count,cs,args
end
if db.iterate_queue[cs] and db.iterate_queue[cs][1] then
local copy = db.iterate_queue[cs][1]
f = db.indexes_pool[cs]
if not f or not f.file then
colddb.open_index_table(db,copy.tag_name)
f = db.indexes_pool[cs]
end
if copy.begin_func then
local a = copy.begin_func(copy.args)
if a and type(a) == "table" then
@ -425,8 +430,6 @@ function colddb.iterate_index_table(db,begin_func,func_on_iterate,end_func,cycle
f = db.indexes_pool[cs]
end
if f and f.file and db.indexes_pool[cs].iterating == false then
-- set inuse and iterating to true
db.indexes_pool[cs].inuse = true
db.indexes_pool[cs].iterating = true
local fl = f.file
if f.needs_flushing == true then
@ -469,42 +472,6 @@ function colddb.iterate_index_table(db,begin_func,func_on_iterate,end_func,cycle
end
end
local function check_use(db,name,tag_name,d)
if not d then
d = 0
end
local delay = 30 + d
local t = ""
if tag_name then
t = colddb.get_tag(db,tag_name)
end
local cs = string.format("%s%s",t,name)
if not db.mem_pool or not db.mem_pool[cs] then
return false
end
if db.mem_pool[cs].inuse == false then
if db.mem_pool[cs].indexes == true then
local cs2 = string.format("%s%s",t,"æIndex_table")
local fs = db.indexes_pool[cs2]
if fs and fs.file then
if fs.inuse == true or fs.iterating == true then
db.indexes_pool[cs2].inuse = false
minetest.after(delay,check_use,name,tag_name,delay,d)
return false
else
fs.file:close()
db.indexes_pool[cs2] = nil
end
end
end
db.mem_pool[cs] = nil
else
db.mem_pool[cs].inuse = false
minetest.after(delay,check_use,name,tag_name,delay,d)
return false
end
end
local function load_into_mem(db,name,_table,tag_name)
if db.add_to_mem_pool then
local t = ""
@ -513,14 +480,10 @@ local function load_into_mem(db,name,_table,tag_name)
end
local cs = string.format("%s%s",t,name)
if not db.mem_pool[cs] then
db.mem_pool[cs] = {mem = {},inuse = false,check_timer = false,indexes = db.indexes}
end
db.mem_pool[cs] = {mem = _table,indexes = db.indexes}
else
db.mem_pool[cs].mem = _table
if not db.mem_pool[cs].check_timer then
db.mem_pool[cs].inuse = false
db.mem_pool[cs].indexes = db.indexes
db.mem_pool[cs].check_timer = true
minetest.after(30,check_use,name,tag_name,30,0)
end
end
end
@ -539,9 +502,7 @@ function colddb.set(db,name,_table,tag_name)
minetest.after(0,function(db,name,tag_name)colddb.append_index_table(db,name,tag_name)end,db,name,tag_name)
end
minetest.after(1,function(db,name, _table,tag_name)colddb.save_table(db,name, _table,tag_name)end,db,name, _table,tag_name)
local cs = string.format("%s%s",t,name)
if db.add_to_mem_pool and not (db.mem_pool or db.mem_pool[cs]) then
db.mem_pool[cs] = {mem = {},inuse = false,check_timer = false,indexes = db.indexes}
if db.add_to_mem_pool then
load_into_mem(db,name,_table,tag_name)
end
end
@ -560,10 +521,8 @@ function colddb.set_key(db,name,tag_name)
minetest.after(0,function(db,name,tag_name)colddb.append_index_table(db,name,tag_name)end,db,name,tag_name)
end
minetest.after(1,function(db,name, tag_name)colddb.save_key(db,name, tag_name)end,db,name, tag_name)
local cs = string.format("%s%s",t,name)
if db.add_to_mem_pool and not (db.mem_pool or db.mem_pool[cs]) then
db.mem_pool[cs] = {mem = {},inuse = false,check_timer = false,indexes = db.indexes}
load_into_mem(db,name,_table,tag_name)
if db.add_to_mem_pool then
load_into_mem(db,name,"",tag_name)
end
end
@ -575,7 +534,6 @@ function colddb.get(db,name,tag_name)
local cs = string.format("%s%s",t,name)
local pm = db.mem_pool[cs]
if pm then
db.mem_pool[cs].inuse = true
return pm.mem
else
local _table = colddb.load_table(db,name,tag_name)
@ -595,7 +553,6 @@ function colddb.get_key(db,name,tag_name)
local cs = string.format("%s%s",t,name)
local pm = db.mem_pool[cs]
if pm then
db.mem_pool[cs].inuse = true
return true
else
local bool = colddb.load_key(db,name,tag_name)
@ -613,7 +570,7 @@ function colddb.remove(db,name,tag_name)
t = colddb.get_tag(db,tag_name)
end
local cs = string.format("%s%s",t,name)
if db.mem_pool and db.mem_pool[cs] then
if db.mem_pool[cs] then
db.mem_pool[cs] = nil
end
if db.indexes and colddb.file_Exists(db,"æIndex_table",tag_name) then