colddb = {} function colddb.Colddb(dir) if not minetest.mkdir(dir) then error(string.format("%s is not a directory.", dir)) end local self = {} local directory = dir local mem_pool = {} local mem_pool_del = {} local add_to_mem_pool = true local async = async.Async() async.priority(150, 250) -- make tables weak so the garbage-collector will remove unused data setmetatable(mem_pool, {__mode = "kv"}) setmetatable(mem_pool_del, {__mode = "kv"}) 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 local function file_exists(name) local f = io.open(string.format("%s/%s.cold", directory, name), "r") if f ~= nil then io.close(f) return true else return false end return false end local function load_into_mem(name, _table) if add_to_mem_pool then mem_pool[name] = {mem = _table} end end self.exists = function(name) return file_exists(name) end local function delete_file(name) local text = string.format("%s/%s.cold", directory, name) local err, msg = os.remove(text) if err == nil then print(string.format("error removing db data %s error message: %s", text, msg)) end end local function load_table(name) local f = io.open(string.format("%s/%s.cold", directory, name), "r") if f then local data = minetest.deserialize(f:read("*a")) f:close() return data end return nil end local function save_table(name, _table) minetest.log(string.format("%s/%s.cold", directory, name)) return minetest.safe_file_write(string.format("%s/%s.cold", directory, name), minetest.serialize(_table)) end local function save_key(name) return minetest.safe_file_write(string.format("%s/%s.cold", directory, name), "") end local function load_key(name) local f = io.open(string.format("%s/%s.cold", directory, name), "r") if f then f:close() return true end return false end self.set_mem = function(name, _table) load_into_mem(name, _table) mem_pool_del[name] = nil end self.save_mem = function(name) async.queue_task(function() if mem_pool[name] ~= nil then save_table(name, mem_pool[name].mem) end end) mem_pool_del[name] = nil end self.clear_mem = function(name) mem_pool[name] = nil mem_pool_del[name] = nil end self.set = function(name, _table) async.queue_task(function() save_table(name, _table) end) if add_to_mem_pool then load_into_mem(name, _table) end mem_pool_del[name] = nil end self.set_key = function(name) async.queue_task(function() save_key(name) end) if add_to_mem_pool then load_into_mem(name, "") end mem_pool_del[name] = nil end self.get = function(name, callback) if mem_pool_del[name] then if callback then callback(nil) end return nil end if callback then async.queue_task(function() local pm = mem_pool[name] if pm then return pm.mem else local _table = load_table(name) if _table then load_into_mem(name, _table) return _table end end mem_pool_del[name] = true return nil end,callback) else local pm = mem_pool[name] if pm then return pm.mem else local _table = load_table(name) if _table then load_into_mem(name, _table) return _table end end mem_pool_del[name] = true return nil end end self.get_key = function(name, callback) if mem_pool_del[name] then if callback then callback(false) end return false end if callback then async.queue_task(function() local pm = mem_pool[name] if pm then return true else local bool = load_key(name) if bool then load_into_mem(name, bool) return bool end end mem_pool_del[name] = true return nil end,callback) else local pm = mem_pool[name] if pm then return true else local bool = load_key(name) if bool then load_into_mem(name, bool) return bool end end mem_pool_del[name] = true return nil end end self.remove = function(name) mem_pool[name] = nil mem_pool_del[name] = true async.queue_task(function() delete_file(name) end) end return self end