2018-11-06 03:05:04 +01:00
|
|
|
colddb = {}
|
|
|
|
|
2019-01-15 01:17:45 +01:00
|
|
|
function colddb.Colddb(dir)
|
|
|
|
local dir = string.format("%s/%s/", minetest.get_worldpath(), dir)
|
|
|
|
if not minetest.mkdir(dir) then
|
|
|
|
error(string.format("%s is not a directory.", dir))
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
|
|
|
|
local self = {}
|
|
|
|
|
2019-01-15 01:17:45 +01:00
|
|
|
local directory = dir
|
|
|
|
local tags = {}
|
|
|
|
local mem_pool = {}
|
|
|
|
local mem_pool_del = {}
|
|
|
|
local indexes_pool = {}
|
|
|
|
local iterate_queue = {}
|
|
|
|
local add_to_mem_pool = true
|
|
|
|
local async = extended_api.Async()
|
|
|
|
local path_count = {}
|
|
|
|
|
|
|
|
async.priority(150, 250)
|
2019-01-08 07:32:34 +01:00
|
|
|
|
2018-11-08 23:15:35 +01:00
|
|
|
-- make tables weak so the garbage-collector will remove unused data
|
2019-01-15 01:17:45 +01:00
|
|
|
setmetatable(tags, {__mode = "kv"})
|
|
|
|
setmetatable(mem_pool, {__mode = "kv"})
|
|
|
|
setmetatable(mem_pool_del, {__mode = "kv"})
|
|
|
|
setmetatable(indexes_pool, {__mode = "kv"})
|
2019-01-08 07:32:34 +01:00
|
|
|
|
2019-01-15 01:49:21 +01:00
|
|
|
local function file_exists(name, tag_name)
|
2019-01-08 07:32:34 +01:00
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
local f = io.open(string.format("%s%s%s.cold", directory, t, name), "r")
|
2019-01-08 07:32:34 +01:00
|
|
|
if f ~= nil then
|
|
|
|
io.close(f)
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return false
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
|
|
|
|
2019-01-08 07:32:34 +01:00
|
|
|
local function delete_lines_func_begin(args)
|
|
|
|
local f = io.open(args.copyfile, "w")
|
|
|
|
if f then
|
|
|
|
args.file = f
|
|
|
|
args.removedlist = {}
|
|
|
|
return args
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
return false
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
|
|
|
|
2019-01-08 07:32:34 +01:00
|
|
|
local function delete_lines_func_i(line, i, args)
|
|
|
|
local f = args.file
|
2019-01-15 01:17:45 +01:00
|
|
|
local om = indexes_pool[args.cs]
|
2019-01-08 07:32:34 +01:00
|
|
|
if om and not om.deleted_items[line] then
|
|
|
|
f:write(string.format("\n%s", line))
|
|
|
|
else
|
|
|
|
args.count = args.count - 1
|
|
|
|
args.removedlist[line] = true
|
|
|
|
end
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
|
|
|
|
2019-01-08 07:32:34 +01:00
|
|
|
local function delete_lines_func_end(args)
|
|
|
|
local cs = args.cs
|
2019-01-15 01:17:45 +01:00
|
|
|
local index = indexes_pool[cs]
|
2019-01-09 23:26:26 +01:00
|
|
|
if index and index.file then
|
|
|
|
index.file:close()
|
|
|
|
index.file = nil
|
2019-01-08 07:32:34 +01:00
|
|
|
args.file:seek("set")
|
|
|
|
args.file:write(string.format("%i", args.count))
|
|
|
|
args.file:close()
|
|
|
|
if args.count < 1 then
|
|
|
|
os.remove(args.oldfile)
|
|
|
|
os.remove(args.copyfile)
|
|
|
|
else
|
|
|
|
os.remove(args.oldfile)
|
|
|
|
os.rename(args.copyfile, args.oldfile)
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
local pool = indexes_pool[cs]
|
2019-01-08 07:32:34 +01:00
|
|
|
for i, l in pairs(args.removedlist) do
|
2019-01-09 23:26:26 +01:00
|
|
|
pool.deleted_items[i] = nil
|
2019-01-08 07:32:34 +01:00
|
|
|
end
|
2019-01-09 23:26:26 +01:00
|
|
|
pool.deleting = false
|
2019-01-15 01:17:45 +01:00
|
|
|
indexes_pool[cs] = pool
|
2019-01-08 07:32:34 +01:00
|
|
|
end
|
|
|
|
args = nil
|
2018-11-06 05:13:37 +01:00
|
|
|
end
|
|
|
|
|
2019-01-08 07:32:34 +01:00
|
|
|
local function iterate(func_on_iterate, end_func, count, cs, args)
|
2019-01-15 01:17:45 +01:00
|
|
|
local f = indexes_pool[cs]
|
2019-01-08 07:32:34 +01:00
|
|
|
local fl = f.file
|
2019-01-15 01:17:45 +01:00
|
|
|
async.iterate(1, count, function(i)
|
2019-01-08 07:32:34 +01:00
|
|
|
local line = fl:read("*l")
|
2019-01-09 23:26:26 +01:00
|
|
|
if args.do_not_skip_removed_items or not f.deleted_items[line] then
|
2019-01-08 07:32:34 +01:00
|
|
|
local ar = func_on_iterate(line, i, args)
|
|
|
|
if ar ~= nil then
|
|
|
|
args = ar
|
|
|
|
return args
|
|
|
|
end
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
end, function()
|
2019-01-08 07:32:34 +01:00
|
|
|
if end_func then
|
|
|
|
end_func(args)
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
local iterate_queue = iterate_queue[cs]
|
2019-01-09 23:26:26 +01:00
|
|
|
if iterate_queue and iterate_queue[1] then
|
|
|
|
local copy = iterate_queue[1]
|
2019-01-15 01:17:45 +01:00
|
|
|
f = indexes_pool[cs]
|
2019-01-08 07:32:34 +01:00
|
|
|
if not f or not f.file then
|
|
|
|
self.open_index_table(copy.tag_name)
|
2019-01-15 01:17:45 +01:00
|
|
|
f = indexes_pool[cs]
|
2019-01-08 07:32:34 +01:00
|
|
|
end
|
|
|
|
if copy.begin_func then
|
|
|
|
local a = copy.begin_func(copy.args)
|
|
|
|
if a and type(a) == "table" then
|
|
|
|
copy.args = a
|
|
|
|
end
|
|
|
|
end
|
|
|
|
minetest.after(0, iterate, copy.func_on_iterate, copy.end_func, copy.count, copy.cs, copy.args)
|
2019-01-15 01:17:45 +01:00
|
|
|
table.remove(iterate_queue[cs], 1)
|
2019-01-08 07:32:34 +01:00
|
|
|
return false
|
|
|
|
else
|
|
|
|
fl:close()
|
2019-01-15 01:17:45 +01:00
|
|
|
iterate_queue[cs] = nil
|
2019-01-08 07:32:34 +01:00
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
indexes_pool[cs].iterating = false
|
2019-01-08 07:32:34 +01:00
|
|
|
return false
|
|
|
|
end)
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
|
|
|
|
2019-01-08 07:32:34 +01:00
|
|
|
local function load_into_mem(name, _table, tag_name)
|
2019-01-15 01:17:45 +01:00
|
|
|
if add_to_mem_pool then
|
2019-01-08 07:32:34 +01:00
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
|
|
|
end
|
|
|
|
local cs = string.format("%s%s", t, name)
|
2019-01-15 01:17:45 +01:00
|
|
|
mem_pool[cs] = {mem = _table}
|
2019-01-08 07:32:34 +01:00
|
|
|
end
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
|
|
|
|
2019-01-08 07:32:34 +01:00
|
|
|
local function _remove_tag(delete_path, prev_dp)
|
2019-01-15 01:17:45 +01:00
|
|
|
local pc = path_count[delete_path]
|
|
|
|
if pc and pc > 0 then
|
2019-01-08 07:32:34 +01:00
|
|
|
minetest.after(1.5, _remove_tag, delete_path)
|
|
|
|
return
|
2019-01-15 01:17:45 +01:00
|
|
|
elseif pc and pc < 1 then
|
|
|
|
mem_pool = {}
|
|
|
|
mem_pool_del = {}
|
2019-01-08 07:32:34 +01:00
|
|
|
os.remove(delete_path)
|
2019-01-15 01:17:45 +01:00
|
|
|
path_count[delete_path] = nil
|
|
|
|
|
2019-01-08 07:32:34 +01:00
|
|
|
return
|
2019-01-15 01:17:45 +01:00
|
|
|
elseif not pc then
|
2019-01-08 07:32:34 +01:00
|
|
|
path_count[delete_path] = 0
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
|
|
|
|
local list = minetest.get_dir_list(delete_path)
|
2019-01-15 01:17:45 +01:00
|
|
|
async.foreach(list, function(k, v)
|
2019-01-08 07:32:34 +01:00
|
|
|
v = string.format("%s/%s", delete_path, v)
|
|
|
|
local err = os.remove(v)
|
|
|
|
if err == nil then
|
|
|
|
minetest.after(0, _remove_tag, v, delete_path)
|
|
|
|
path_count[delete_path] = path_count[delete_path] + 1
|
|
|
|
end
|
|
|
|
end, function()
|
|
|
|
if prev_dp then
|
|
|
|
path_count[prev_dp] = path_count[prev_dp] - 1
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
if pc > 0 then
|
2019-01-08 07:32:34 +01:00
|
|
|
minetest.after(1.5, _remove_tag, delete_path)
|
|
|
|
else
|
2019-01-15 01:17:45 +01:00
|
|
|
mem_pool = {}
|
|
|
|
mem_pool_del = {}
|
2019-01-08 07:32:34 +01:00
|
|
|
os.remove(delete_path)
|
2019-01-15 01:17:45 +01:00
|
|
|
path_count[delete_path] = nil
|
2019-01-08 07:32:34 +01:00
|
|
|
end
|
|
|
|
end)
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
|
2019-01-15 01:49:21 +01:00
|
|
|
self.exists = function(name, tag_name)
|
|
|
|
return file_exists(name, tag_name)
|
|
|
|
end
|
|
|
|
|
2019-01-15 01:17:45 +01:00
|
|
|
self.add_tag = function(name, tag, no_new_path)
|
2019-01-08 07:32:34 +01:00
|
|
|
local t = ""
|
2019-01-15 01:17:45 +01:00
|
|
|
if not tags[name] then
|
|
|
|
tags[name] = ""
|
2019-01-08 07:32:34 +01:00
|
|
|
end
|
|
|
|
if type(tag) == "table" then
|
|
|
|
for key, value in pairs(tag) do
|
|
|
|
t = string.format("%s%s/", t, value)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
t = string.format("%s/", tag)
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
tags[name] = string.format("%s%s", tags[name], t)
|
|
|
|
if no_new_path then
|
|
|
|
return
|
|
|
|
end
|
2019-01-15 01:49:21 +01:00
|
|
|
local test_path = string.format("%s%s", directory, tags[name])
|
2019-01-15 01:17:45 +01:00
|
|
|
if not minetest.mkdir(test_path) then
|
2019-01-08 07:32:34 +01:00
|
|
|
error(string.format("%s is not a directory.", test_path))
|
|
|
|
end
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
|
|
|
|
self.get_tag = function(name)
|
|
|
|
if not name then
|
|
|
|
return ""
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
local tag = tags[name]
|
2019-01-08 07:32:34 +01:00
|
|
|
if tag then
|
|
|
|
return tag
|
|
|
|
end
|
|
|
|
return ""
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
|
2019-01-15 01:17:45 +01:00
|
|
|
self.get_or_add_tag = function(name, tag, no_new_path)
|
|
|
|
if not tags[name] then
|
|
|
|
self.add_tag(name, tag, no_new_path)
|
2019-01-08 07:32:34 +01:00
|
|
|
end
|
|
|
|
return name
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
|
|
|
|
self.remove_tag = function(name)
|
2019-01-15 01:17:45 +01:00
|
|
|
if not name then
|
|
|
|
local delete_path = directory
|
|
|
|
local wc = delete_path:len()
|
|
|
|
delete_path = delete_path:sub(0, wc-1)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if tags[name] then
|
|
|
|
local delete_path = string.format("%s%s", directory, tags[name])
|
2019-01-08 07:32:34 +01:00
|
|
|
local wc = delete_path:len()
|
|
|
|
delete_path = delete_path:sub(0, wc-1)
|
2019-01-15 01:17:45 +01:00
|
|
|
tags[name] = nil
|
2019-01-08 07:32:34 +01:00
|
|
|
local err = os.remove(delete_path)
|
|
|
|
if err == nil then
|
|
|
|
minetest.after(0.1, _remove_tag, delete_path)
|
|
|
|
end
|
|
|
|
end
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
|
2019-01-15 01:17:45 +01:00
|
|
|
local function delete_file(name, tag_name)
|
2019-01-08 07:32:34 +01:00
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
local text = string.format("%s%s%s.cold", directory, t, name)
|
2019-01-08 07:32:34 +01:00
|
|
|
|
|
|
|
local err, msg = os.remove(text)
|
|
|
|
if err == nil then
|
|
|
|
print(string.format("error removing db data %s error message: %s", text, msg))
|
|
|
|
end
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
|
2019-01-15 01:17:45 +01:00
|
|
|
local function load_table(name, tag_name)
|
2019-01-08 07:32:34 +01:00
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
local f = io.open(string.format("%s%s%s.cold", directory, t, name), "r")
|
2018-11-06 03:05:04 +01:00
|
|
|
if f then
|
2019-01-08 07:32:34 +01:00
|
|
|
local data = minetest.deserialize(f:read("*a"))
|
2018-11-06 03:05:04 +01:00
|
|
|
f:close()
|
2019-01-08 07:32:34 +01:00
|
|
|
return data
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
return nil
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
|
2019-01-15 01:17:45 +01:00
|
|
|
local function save_table(name, _table, tag_name)
|
2019-01-08 07:32:34 +01:00
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
|
|
|
end
|
2019-01-15 01:49:21 +01:00
|
|
|
minetest.log(string.format("%s%s%s.cold", directory, t, name))
|
2019-01-15 01:17:45 +01:00
|
|
|
return minetest.safe_file_write(string.format("%s%s%s.cold", directory, t, name), minetest.serialize(_table))
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
|
|
|
|
2019-01-15 01:17:45 +01:00
|
|
|
local function save_key(name, tag_name)
|
2019-01-08 07:32:34 +01:00
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
return minetest.safe_file_write(string.format("%s%s%s.cold", directory, t, name), "")
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
|
2019-01-15 01:17:45 +01:00
|
|
|
local function load_key(name, tag_name)
|
2019-01-08 07:32:34 +01:00
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
local f = io.open(string.format("%s%s%s.cold", directory, t, name), "r")
|
2018-11-06 03:05:04 +01:00
|
|
|
if f then
|
2019-01-08 07:32:34 +01:00
|
|
|
f:close()
|
|
|
|
return true
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
return false
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
|
|
|
|
2019-01-15 01:17:45 +01:00
|
|
|
self.delete_entries = function(name ,_lines, tag_name)
|
2019-01-08 07:32:34 +01:00
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
|
|
|
end
|
|
|
|
local cs = string.format("%s%s", t, name)
|
2019-01-15 01:17:45 +01:00
|
|
|
local f = indexes_pool[cs]
|
2019-01-08 07:32:34 +01:00
|
|
|
local k = type(_lines)
|
|
|
|
if k == "string" then
|
|
|
|
f.deleted_items[_lines] = true
|
|
|
|
else
|
|
|
|
for i, l in pairs(_lines) do
|
|
|
|
f.deleted_items[i] = true
|
|
|
|
end
|
|
|
|
end
|
2019-01-09 23:26:26 +01:00
|
|
|
if not f.deleting then
|
2019-01-15 01:17:45 +01:00
|
|
|
indexes_pool[cs].deleting = false
|
2019-01-08 07:32:34 +01:00
|
|
|
end
|
2019-01-09 23:26:26 +01:00
|
|
|
if f and f.file and not f.deleting then
|
2019-01-15 01:17:45 +01:00
|
|
|
indexes_pool[cs].deleting = true
|
2019-01-09 23:26:26 +01:00
|
|
|
if f.needs_flushing == true then
|
2019-01-08 07:32:34 +01:00
|
|
|
f.file:flush()
|
2019-01-15 01:17:45 +01:00
|
|
|
indexes_pool[cs].needs_flushing = false
|
2019-01-08 07:32:34 +01:00
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
local oldfile = string.format("%s%s%s.cold", directory, t, name)
|
|
|
|
local copyfile = string.format("%s%s%s.cold.replacer", directory, t, name)
|
2019-01-08 07:32:34 +01:00
|
|
|
local args = {cs = cs, oldfile = oldfile, copyfile = copyfile, do_not_skip_removed_items = true}
|
2019-01-15 01:17:45 +01:00
|
|
|
indexes_pool[cs] = f
|
|
|
|
iterate_index_table(delete_entries_func_begin, delete_entries_func_i, delete_entries_func_end, args, tag_name)
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-15 01:17:45 +01:00
|
|
|
self.open_entry_file = function(name, tag_name)
|
2019-01-08 07:32:34 +01:00
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
|
|
|
end
|
|
|
|
local cs = string.format("%s%s", t, name)
|
2019-01-15 01:17:45 +01:00
|
|
|
local fs = indexes_pool[cs]
|
2019-01-08 07:32:34 +01:00
|
|
|
if not fs then
|
2019-01-15 01:17:45 +01:00
|
|
|
local p = string.format("%s%s%s.cold", directory, t, name)
|
2019-01-15 01:49:21 +01:00
|
|
|
if not file_exists(name,tag_name) then
|
2019-01-08 07:32:34 +01:00
|
|
|
local f = io.open(p, "w")
|
|
|
|
if f then
|
|
|
|
f:seek("set")
|
|
|
|
f:write("0")
|
|
|
|
f:close()
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
local f = io.open(p, "r+")
|
|
|
|
if f then
|
2019-01-15 01:17:45 +01:00
|
|
|
indexes_pool[cs] = {file = f, needs_flushing = false, deleted_items = {}, iterating = false}
|
2019-01-08 07:32:34 +01:00
|
|
|
return f
|
|
|
|
end
|
|
|
|
return nil
|
2018-11-06 03:05:04 +01:00
|
|
|
else
|
2019-01-08 07:32:34 +01:00
|
|
|
return fs.file
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
return nil
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
|
2019-02-03 01:28:55 +01:00
|
|
|
self.append_entry_file = function(name, key, op, tag_name)
|
2019-01-08 07:32:34 +01:00
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
2018-12-08 21:56:38 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
local cs = string.format("%s%s", t, name)
|
2019-01-15 01:17:45 +01:00
|
|
|
local f = indexes_pool[cs]
|
2019-01-08 07:32:34 +01:00
|
|
|
local k = type(key)
|
|
|
|
if f and f.file and k == "string" then
|
|
|
|
local fl = f.file
|
|
|
|
if f.needs_flushing == true then
|
|
|
|
fl:flush()
|
|
|
|
f.needs_flushing = false
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
indexes_pool[cs].needs_flushing = true
|
2019-01-08 07:32:34 +01:00
|
|
|
fl:seek("end")
|
|
|
|
fl:write(string.format("\n%s", key))
|
|
|
|
fl:seek("set")
|
|
|
|
local count = tonumber(fl:read("*l"))
|
|
|
|
count = count + 1
|
|
|
|
fl:seek("set")
|
|
|
|
fl:write(string.format("%i", count))
|
|
|
|
fl:close()
|
|
|
|
elseif f and f.file then
|
|
|
|
local fl = f.file
|
|
|
|
if f.needs_flushing == true then
|
|
|
|
fl:flush()
|
|
|
|
f.needs_flushing = false
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
indexes_pool[cs].needs_flushing = true
|
2019-01-08 07:32:34 +01:00
|
|
|
local c = 0
|
2019-02-03 01:28:55 +01:00
|
|
|
if op and op == "key" then
|
|
|
|
for i in pairs(key) do
|
|
|
|
fl:seek("end")
|
|
|
|
fl:write(string.format("\n%s", i))
|
|
|
|
c = c + 1
|
|
|
|
end
|
|
|
|
elseif not op or op == "value" then
|
|
|
|
for i, j in pairs(key) do
|
|
|
|
fl:seek("end")
|
|
|
|
fl:write(string.format("\n%s", j))
|
|
|
|
c = c + 1
|
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
end
|
|
|
|
fl:seek("set")
|
|
|
|
local count = tonumber(fl:read("*l"))
|
|
|
|
count = count + c
|
|
|
|
fl:seek("set")
|
|
|
|
fl:write(string.format("%i", count))
|
|
|
|
fl:close()
|
|
|
|
else
|
2018-11-06 03:05:04 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-15 01:17:45 +01:00
|
|
|
self.get_entry_file_count = function(name, tag_name)
|
2018-11-06 03:05:04 +01:00
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
2019-01-08 07:32:34 +01:00
|
|
|
t = self.get_tag(tag_name)
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
local cs = string.format("%s%s", t, name)
|
|
|
|
local f = indexes_pool[cs]
|
|
|
|
if not f or not f.file then
|
|
|
|
self.open_entry_file(name, tag_name)
|
|
|
|
f = indexes_pool[cs]
|
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
if f and f.file then
|
|
|
|
local fl = f.file
|
|
|
|
if f.needs_flushing == true then
|
|
|
|
fl:flush()
|
|
|
|
f.needs_flushing = false
|
|
|
|
end
|
|
|
|
fl:seek("set")
|
|
|
|
local count = tonumber(fl:read("*l"))
|
|
|
|
fl:close()
|
|
|
|
return count
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
return nil
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
|
|
|
|
2019-01-15 01:17:45 +01:00
|
|
|
self.iterate_entry_file = function(name, begin_func, func_on_iterate, end_func, args, tag_name)
|
2019-01-08 07:32:34 +01:00
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
|
|
|
end
|
|
|
|
local cs = string.format("%s%s", t, name)
|
2019-01-15 01:17:45 +01:00
|
|
|
local f = indexes_pool[cs]
|
2019-01-08 07:32:34 +01:00
|
|
|
if not f or not f.file then
|
2019-01-15 01:17:45 +01:00
|
|
|
self.open_entry_file(tag_name)
|
|
|
|
f = indexes_pool[cs]
|
2019-01-08 07:32:34 +01:00
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
if f and f.file and indexes_pool[cs].iterating == false then
|
|
|
|
indexes_pool[cs].iterating = true
|
2019-01-08 07:32:34 +01:00
|
|
|
local fl = f.file
|
|
|
|
if f.needs_flushing == true then
|
|
|
|
fl:flush()
|
|
|
|
f.needs_flushing = false
|
|
|
|
end
|
|
|
|
-- Get count
|
|
|
|
fl:seek("set")
|
|
|
|
local c = tonumber(fl:read("*l"))
|
|
|
|
if not args then
|
|
|
|
args = {}
|
|
|
|
end
|
|
|
|
args.count = c
|
|
|
|
-- Run the begin function
|
|
|
|
if begin_func then
|
|
|
|
local a = begin_func(args)
|
|
|
|
if a and type(a) == "table" then
|
|
|
|
args = a
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if c < 1 then
|
|
|
|
-- If theres nothing to index then return
|
|
|
|
end_func(args)
|
2019-01-15 01:17:45 +01:00
|
|
|
indexes_pool[cs].iterating = false
|
2019-01-08 07:32:34 +01:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
-- Start iterating the index table
|
|
|
|
iterate(func_on_iterate, end_func, c, cs, args)
|
|
|
|
elseif f and f.file then
|
|
|
|
local fl = f.file
|
|
|
|
-- If its iterating some other function then add this one to the queue list
|
|
|
|
fl:seek("set")
|
|
|
|
local c = tonumber(fl:read("*l"))
|
|
|
|
if c < 1 then
|
|
|
|
-- If theres nothing to index then return
|
|
|
|
return false
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
if not iterate_queue[cs] then
|
|
|
|
iterate_queue[cs] = {}
|
2019-01-08 07:32:34 +01:00
|
|
|
end
|
|
|
|
local _table = {begin_func = begin_func, func_on_iterate = func_on_iterate, end_func = end_func, count = c, cs = cs, tag_name = tag_name, args = args}
|
2019-01-15 01:17:45 +01:00
|
|
|
table.insert(iterate_queue[cs], _table)
|
2019-01-08 07:32:34 +01:00
|
|
|
end
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-02-03 01:28:55 +01:00
|
|
|
|
|
|
|
self.set_mem = function(name, _table, tag_name)
|
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
|
|
|
end
|
|
|
|
load_into_mem(name, _table, tag_name)
|
|
|
|
local cs = string.format("%s%s", t, name)
|
|
|
|
mem_pool_del[cs] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
self.save_mem = function(name, tag_name)
|
|
|
|
local t = ""
|
|
|
|
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
local cs = string.format("%s%s", t, name)
|
|
|
|
|
|
|
|
async.queue_task(function()
|
|
|
|
if mem_pool[cs] ~= nil then
|
|
|
|
save_table(name, mem_pool[cs].mem, tag_name)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
mem_pool_del[cs] = nil
|
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
|
|
|
|
self.set = function(name, _table, tag_name)
|
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
async.queue_task(function()
|
|
|
|
save_table(name, _table, tag_name)
|
2018-11-11 04:40:49 +01:00
|
|
|
end)
|
2019-01-15 01:17:45 +01:00
|
|
|
if add_to_mem_pool then
|
2019-01-08 07:32:34 +01:00
|
|
|
load_into_mem(name, _table, tag_name)
|
|
|
|
end
|
|
|
|
local cs = string.format("%s%s", t, name)
|
2019-01-15 01:17:45 +01:00
|
|
|
mem_pool_del[cs] = nil
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
|
|
|
|
2019-01-08 07:32:34 +01:00
|
|
|
self.set_key = function(name, tag_name)
|
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
async.queue_task(function()
|
|
|
|
save_key(name, tag_name)
|
2018-11-11 04:40:49 +01:00
|
|
|
end)
|
2019-01-15 01:17:45 +01:00
|
|
|
if add_to_mem_pool then
|
2019-01-08 07:32:34 +01:00
|
|
|
load_into_mem(name, "", tag_name)
|
|
|
|
end
|
|
|
|
local cs = string.format("%s%s", t, name)
|
2019-01-15 01:17:45 +01:00
|
|
|
mem_pool_del[cs] = nil
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
|
|
|
|
2019-01-08 07:32:34 +01:00
|
|
|
self.get = function(name, tag_name, callback)
|
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
|
|
|
end
|
|
|
|
local cs = string.format("%s%s", t, name)
|
2019-01-15 01:17:45 +01:00
|
|
|
if mem_pool_del[cs] then
|
2019-01-08 07:32:34 +01:00
|
|
|
if callback then
|
|
|
|
callback(nil)
|
2018-12-08 21:56:38 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
if callback then
|
2019-01-15 01:17:45 +01:00
|
|
|
async.queue_task(function()
|
|
|
|
local pm = mem_pool[cs]
|
2019-01-08 07:32:34 +01:00
|
|
|
if pm then
|
|
|
|
return pm.mem
|
|
|
|
else
|
2019-01-15 01:17:45 +01:00
|
|
|
local _table = load_table(name, tag_name)
|
2019-01-08 07:32:34 +01:00
|
|
|
if _table then
|
|
|
|
load_into_mem(name, _table, tag_name)
|
|
|
|
return _table
|
|
|
|
end
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
mem_pool_del[cs] = true
|
2019-01-08 07:32:34 +01:00
|
|
|
return nil
|
|
|
|
end,callback)
|
|
|
|
else
|
2019-01-15 01:17:45 +01:00
|
|
|
local pm = mem_pool[cs]
|
2018-12-08 21:56:38 +01:00
|
|
|
if pm then
|
|
|
|
return pm.mem
|
|
|
|
else
|
2019-01-15 01:17:45 +01:00
|
|
|
local _table = load_table(name, tag_name)
|
2018-12-08 21:56:38 +01:00
|
|
|
if _table then
|
2019-01-08 07:32:34 +01:00
|
|
|
load_into_mem(name, _table, tag_name)
|
2018-12-08 21:56:38 +01:00
|
|
|
return _table
|
|
|
|
end
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
mem_pool_del[cs] = true
|
2018-12-08 21:56:38 +01:00
|
|
|
return nil
|
2019-01-08 07:32:34 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.get_key = function(name, tag_name, callback)
|
2018-11-11 04:40:49 +01:00
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
2019-01-08 07:32:34 +01:00
|
|
|
t = self.get_tag(tag_name)
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
local cs = string.format("%s%s", t, name)
|
2019-01-15 01:17:45 +01:00
|
|
|
if mem_pool_del[cs] then
|
2019-01-08 07:32:34 +01:00
|
|
|
if callback then
|
|
|
|
callback(false)
|
2018-11-11 04:40:49 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
return false
|
2018-11-11 04:40:49 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
if callback then
|
2019-01-15 01:17:45 +01:00
|
|
|
async.queue_task(function()
|
2019-01-08 07:32:34 +01:00
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
|
|
|
t = self.get_tag(tag_name)
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
local pm = mem_pool[cs]
|
2019-01-08 07:32:34 +01:00
|
|
|
if pm then
|
|
|
|
return true
|
|
|
|
else
|
2019-01-15 01:17:45 +01:00
|
|
|
local bool = load_key(name, tag_name)
|
2019-01-08 07:32:34 +01:00
|
|
|
if bool then
|
|
|
|
load_into_mem(name, bool, tag_name)
|
|
|
|
return bool
|
|
|
|
end
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
mem_pool_del[cs] = true
|
2019-01-08 07:32:34 +01:00
|
|
|
return nil
|
|
|
|
end,callback)
|
|
|
|
else
|
2019-01-15 01:17:45 +01:00
|
|
|
local pm = mem_pool[cs]
|
2018-12-08 21:56:38 +01:00
|
|
|
if pm then
|
|
|
|
return true
|
|
|
|
else
|
2019-01-15 01:17:45 +01:00
|
|
|
local bool = load_key(name, tag_name)
|
2018-12-08 21:56:38 +01:00
|
|
|
if bool then
|
2019-01-08 07:32:34 +01:00
|
|
|
load_into_mem(name, bool, tag_name)
|
2018-12-08 21:56:38 +01:00
|
|
|
return bool
|
|
|
|
end
|
|
|
|
end
|
2019-01-15 01:17:45 +01:00
|
|
|
mem_pool_del[cs] = true
|
2018-12-08 21:56:38 +01:00
|
|
|
return nil
|
2019-01-08 07:32:34 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.remove = function(name, tag_name)
|
2018-11-11 04:40:49 +01:00
|
|
|
local t = ""
|
|
|
|
if tag_name then
|
2019-01-08 07:32:34 +01:00
|
|
|
t = self.get_tag(tag_name)
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
local cs = string.format("%s%s", t, name)
|
2019-01-15 01:17:45 +01:00
|
|
|
mem_pool[cs] = nil
|
|
|
|
mem_pool_del[cs] = true
|
|
|
|
async.queue_task(function()
|
|
|
|
delete_file(name, tag_name)
|
2018-11-11 04:40:49 +01:00
|
|
|
end)
|
2018-11-06 03:05:04 +01:00
|
|
|
end
|
2019-01-08 07:32:34 +01:00
|
|
|
|
|
|
|
return self
|
|
|
|
end
|