colddb/colddb.lua

241 lines
4.6 KiB
Lua
Raw Normal View History

2018-11-06 03:05:04 +01:00
colddb = {}
2019-01-15 01:17:45 +01:00
function colddb.Colddb(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 mem_pool = {}
local mem_pool_del = {}
local add_to_mem_pool = true
local async = extended_api.Async()
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(mem_pool, {__mode = "kv"})
setmetatable(mem_pool_del, {__mode = "kv"})
2019-03-16 03:14:30 +01:00
self.get_directory = function()
return directory
end
self.set_directory = function(new_directory)
directory = new_directory
if not minetest.mkdir(directory) then
error(string.format("%s is not a directory.", dir))
end
end
self.get_memory_pool = function()
return mem_pool
end
self.set_memory_pool = function(pool)
mem_pool = pool
end
self.get_async = function()
return async
end
self.set_async = function(new_async)
async = new_async
end
self.add_to_memory_pool = function(value)
if value then
add_to_mem_pool = value
end
return add_to_mem_pool
end
2019-01-08 07:32:34 +01:00
2019-03-16 03:02:31 +01:00
local function file_exists(name)
2019-03-16 05:55:57 +01:00
local f = io.open(string.format("%s/%s.cold", directory, 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-03-16 03:02:31 +01:00
local function load_into_mem(name, _table)
2019-01-15 01:17:45 +01:00
if add_to_mem_pool then
2019-03-16 03:02:31 +01:00
mem_pool[name] = {mem = _table}
2019-01-08 07:32:34 +01:00
end
2018-11-06 03:05:04 +01:00
end
2019-03-16 03:02:31 +01:00
self.exists = function(name)
return file_exists(name)
2018-11-06 03:05:04 +01:00
end
2019-01-08 07:32:34 +01:00
2019-03-16 03:02:31 +01:00
local function delete_file(name)
2019-03-16 05:55:57 +01:00
local text = string.format("%s/%s.cold", directory, 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-03-16 03:02:31 +01:00
local function load_table(name)
2019-03-16 05:55:57 +01:00
local f = io.open(string.format("%s/%s.cold", directory, 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-03-16 03:02:31 +01:00
local function save_table(name, _table)
2019-03-16 05:55:57 +01:00
minetest.log(string.format("%s/%s.cold", directory, name))
return minetest.safe_file_write(string.format("%s/%s.cold", directory, name), minetest.serialize(_table))
2018-11-06 03:05:04 +01:00
end
2019-03-16 03:02:31 +01:00
local function save_key(name)
2019-03-16 05:55:57 +01:00
return minetest.safe_file_write(string.format("%s/%s.cold", directory, name), "")
2018-11-06 03:05:04 +01:00
end
2019-01-08 07:32:34 +01:00
2019-03-16 03:02:31 +01:00
local function load_key(name)
2019-03-16 05:55:57 +01:00
local f = io.open(string.format("%s/%s.cold", directory, 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-02-03 01:28:55 +01:00
2019-03-16 03:02:31 +01:00
self.set_mem = function(name, _table)
load_into_mem(name, _table)
mem_pool_del[name] = nil
2019-02-03 01:28:55 +01:00
end
2019-03-16 03:02:31 +01:00
self.save_mem = function(name)
2019-02-03 01:28:55 +01:00
async.queue_task(function()
2019-03-16 03:02:31 +01:00
if mem_pool[name] ~= nil then
save_table(name, mem_pool[name].mem)
2019-02-03 01:28:55 +01:00
end
end)
2019-03-16 03:02:31 +01:00
mem_pool_del[name] = nil
end
2019-03-16 05:55:57 +01:00
self.clear_mem = function(name)
2019-03-16 03:02:31 +01:00
mem_pool[name] = nil
mem_pool_del[name] = nil
2019-02-03 01:28:55 +01:00
end
2019-01-08 07:32:34 +01:00
2019-03-16 03:02:31 +01:00
self.set = function(name, _table)
2019-01-15 01:17:45 +01:00
async.queue_task(function()
2019-03-16 03:02:31 +01:00
save_table(name, _table)
2018-11-11 04:40:49 +01:00
end)
2019-01-15 01:17:45 +01:00
if add_to_mem_pool then
2019-03-16 03:02:31 +01:00
load_into_mem(name, _table)
2019-01-08 07:32:34 +01:00
end
2019-03-16 03:02:31 +01:00
mem_pool_del[name] = nil
2018-11-06 03:05:04 +01:00
end
2019-03-16 03:02:31 +01:00
self.set_key = function(name)
2019-01-15 01:17:45 +01:00
async.queue_task(function()
2019-03-16 03:02:31 +01:00
save_key(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-03-16 03:02:31 +01:00
load_into_mem(name, "")
2019-01-08 07:32:34 +01:00
end
2019-03-16 03:02:31 +01:00
mem_pool_del[name] = nil
2018-11-06 03:05:04 +01:00
end
2019-03-16 03:02:31 +01:00
self.get = function(name, callback)
if mem_pool_del[name] 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()
2019-03-16 03:02:31 +01:00
local pm = mem_pool[name]
2019-01-08 07:32:34 +01:00
if pm then
return pm.mem
else
2019-03-16 03:02:31 +01:00
local _table = load_table(name)
2019-01-08 07:32:34 +01:00
if _table then
2019-03-16 03:02:31 +01:00
load_into_mem(name, _table)
2019-01-08 07:32:34 +01:00
return _table
end
end
2019-03-16 03:02:31 +01:00
mem_pool_del[name] = true
2019-01-08 07:32:34 +01:00
return nil
end,callback)
else
2019-03-16 03:02:31 +01:00
local pm = mem_pool[name]
2018-12-08 21:56:38 +01:00
if pm then
return pm.mem
else
2019-03-16 03:02:31 +01:00
local _table = load_table(name)
2018-12-08 21:56:38 +01:00
if _table then
2019-03-16 03:02:31 +01:00
load_into_mem(name, _table)
2018-12-08 21:56:38 +01:00
return _table
end
end
2019-03-16 03:02:31 +01:00
mem_pool_del[name] = true
2018-12-08 21:56:38 +01:00
return nil
2019-01-08 07:32:34 +01:00
end
end
2019-03-16 03:02:31 +01:00
self.get_key = function(name, callback)
if mem_pool_del[name] 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-03-16 03:02:31 +01:00
local pm = mem_pool[name]
2019-01-08 07:32:34 +01:00
if pm then
return true
else
2019-03-16 03:02:31 +01:00
local bool = load_key(name)
2019-01-08 07:32:34 +01:00
if bool then
2019-03-16 03:02:31 +01:00
load_into_mem(name, bool)
2019-01-08 07:32:34 +01:00
return bool
end
end
2019-03-16 03:02:31 +01:00
mem_pool_del[name] = true
2019-01-08 07:32:34 +01:00
return nil
end,callback)
else
2019-03-16 03:02:31 +01:00
local pm = mem_pool[name]
2018-12-08 21:56:38 +01:00
if pm then
return true
else
2019-03-16 03:02:31 +01:00
local bool = load_key(name)
2018-12-08 21:56:38 +01:00
if bool then
2019-03-16 03:02:31 +01:00
load_into_mem(name, bool)
2018-12-08 21:56:38 +01:00
return bool
end
end
2019-03-16 03:02:31 +01:00
mem_pool_del[name] = true
2018-12-08 21:56:38 +01:00
return nil
2019-01-08 07:32:34 +01:00
end
end
2019-03-16 03:02:31 +01:00
self.remove = function(name)
mem_pool[name] = nil
mem_pool_del[name] = true
2019-01-15 01:17:45 +01:00
async.queue_task(function()
2019-03-16 03:02:31 +01:00
delete_file(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