small clean up and bug fix

This commit is contained in:
Coder12a 2018-11-08 16:15:35 -06:00
parent 1807225d7b
commit ab7337ba83
1 changed files with 18 additions and 61 deletions

View File

@ -34,6 +34,10 @@ function colddb.get_db(directory)
indexes = false, indexes = false,
add_to_mem_pool = true, 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 return db
end end
@ -269,7 +273,7 @@ function colddb.create_index_table(db,tag_name)
if f then if f then
f:seek("set") f:seek("set")
f:write("0") 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 return true
end end
return false return false
@ -295,7 +299,7 @@ function colddb.open_index_table(db,tag_name)
end end
local f = io.open(p, "r+") local f = io.open(p, "r+")
if f then 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 return f
end end
return nil return nil
@ -320,7 +324,6 @@ function colddb.append_index_table(db,key,tag_name)
fl:flush() fl:flush()
end end
db.indexes_pool[cs].needs_flushing = true db.indexes_pool[cs].needs_flushing = true
db.indexes_pool[cs].inuse = true
fl:seek("end") fl:seek("end")
fl:write(string.format("\n%s",key)) fl:write(string.format("\n%s",key))
fl:seek("set") fl:seek("set")
@ -334,7 +337,6 @@ function colddb.append_index_table(db,key,tag_name)
fl:flush() fl:flush()
end end
db.indexes_pool[cs].needs_flushing = true db.indexes_pool[cs].needs_flushing = true
db.indexes_pool[cs].inuse = true
local c = 0 local c = 0
for i in pairs(key) do for i in pairs(key) do
fl:seek("end") fl:seek("end")
@ -367,7 +369,6 @@ function colddb.get_count(db,tag_name)
end end
fl:seek("set") fl:seek("set")
local count = tonumber(fl:read("*l")) local count = tonumber(fl:read("*l"))
db.indexes_pool[cs].inuse = true
return count return count
end end
return nil return nil
@ -375,7 +376,6 @@ end
local function iterate(db,func_on_iterate,end_func,cycles_per_tick,count,cs,args) local function iterate(db,func_on_iterate,end_func,cycles_per_tick,count,cs,args)
local f = db.indexes_pool[cs] local f = db.indexes_pool[cs]
f.inuse = true
local fl = f.file local fl = f.file
for i=1,cycles_per_tick do for i=1,cycles_per_tick do
if count < 1 then if count < 1 then
@ -394,6 +394,11 @@ local function iterate(db,func_on_iterate,end_func,cycles_per_tick,count,cs,args
end end
if db.iterate_queue[cs] and db.iterate_queue[cs][1] then if db.iterate_queue[cs] and db.iterate_queue[cs][1] then
local copy = db.iterate_queue[cs][1] 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 if copy.begin_func then
local a = copy.begin_func(copy.args) local a = copy.begin_func(copy.args)
if a and type(a) == "table" then 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] f = db.indexes_pool[cs]
end end
if f and f.file and db.indexes_pool[cs].iterating == false then 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 db.indexes_pool[cs].iterating = true
local fl = f.file local fl = f.file
if f.needs_flushing == true then 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
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) local function load_into_mem(db,name,_table,tag_name)
if db.add_to_mem_pool then if db.add_to_mem_pool then
local t = "" local t = ""
@ -513,14 +480,10 @@ local function load_into_mem(db,name,_table,tag_name)
end end
local cs = string.format("%s%s",t,name) local cs = string.format("%s%s",t,name)
if not db.mem_pool[cs] then if not db.mem_pool[cs] then
db.mem_pool[cs] = {mem = {},inuse = false,check_timer = false,indexes = db.indexes} db.mem_pool[cs] = {mem = _table,indexes = db.indexes}
end else
db.mem_pool[cs].mem = _table 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].indexes = db.indexes
db.mem_pool[cs].check_timer = true
minetest.after(30,check_use,name,tag_name,30,0)
end end
end 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) minetest.after(0,function(db,name,tag_name)colddb.append_index_table(db,name,tag_name)end,db,name,tag_name)
end end
minetest.after(1,function(db,name, _table,tag_name)colddb.save_table(db,name, _table,tag_name)end,db,name, _table,tag_name) 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 then
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) load_into_mem(db,name,_table,tag_name)
end end
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) minetest.after(0,function(db,name,tag_name)colddb.append_index_table(db,name,tag_name)end,db,name,tag_name)
end end
minetest.after(1,function(db,name, tag_name)colddb.save_key(db,name, tag_name)end,db,name, tag_name) 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 then
if db.add_to_mem_pool and not (db.mem_pool or db.mem_pool[cs]) then load_into_mem(db,name,"",tag_name)
db.mem_pool[cs] = {mem = {},inuse = false,check_timer = false,indexes = db.indexes}
load_into_mem(db,name,_table,tag_name)
end end
end end
@ -575,7 +534,6 @@ function colddb.get(db,name,tag_name)
local cs = string.format("%s%s",t,name) local cs = string.format("%s%s",t,name)
local pm = db.mem_pool[cs] local pm = db.mem_pool[cs]
if pm then if pm then
db.mem_pool[cs].inuse = true
return pm.mem return pm.mem
else else
local _table = colddb.load_table(db,name,tag_name) 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 cs = string.format("%s%s",t,name)
local pm = db.mem_pool[cs] local pm = db.mem_pool[cs]
if pm then if pm then
db.mem_pool[cs].inuse = true
return true return true
else else
local bool = colddb.load_key(db,name,tag_name) 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) t = colddb.get_tag(db,tag_name)
end end
local cs = string.format("%s%s",t,name) 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 db.mem_pool[cs] = nil
end end
if db.indexes and colddb.file_Exists(db,"æIndex_table",tag_name) then if db.indexes and colddb.file_Exists(db,"æIndex_table",tag_name) then