Add: use_async function

This commit is contained in:
Coder12a 2019-05-19 22:19:20 -05:00
parent 81bbc51dee
commit 19b6648d69
1 changed files with 35 additions and 9 deletions

View File

@ -12,6 +12,7 @@ function colddb.Colddb(dir)
local mem_pool_del = {}
local add_to_mem_pool = true
local async = async.Async()
local use_async = true
async.priority(150, 250)
@ -52,6 +53,10 @@ function colddb.Colddb(dir)
end
return add_to_mem_pool
end
self.use_async = function(value)
use_async = value
end
local function file_exists(name)
local f = io.open(string.format("%s/%s.cold", directory, name), "r")
@ -79,7 +84,7 @@ function colddb.Colddb(dir)
local err, msg = os.remove(text)
if err == nil then
print(string.format("error removing db data %s error message: %s", text, msg))
minetest.log(string.format("error removing db data %s error message: %s", text, msg))
end
end
@ -117,11 +122,17 @@ function colddb.Colddb(dir)
end
self.save_mem = function(name)
async.queue_task(function()
if use_async then
async.queue_task(function()
if mem_pool[name] ~= nil then
save_table(name, mem_pool[name].mem)
end
end)
else
if mem_pool[name] ~= nil then
save_table(name, mem_pool[name].mem)
end
end)
end
mem_pool_del[name] = nil
end
@ -132,9 +143,14 @@ function colddb.Colddb(dir)
end
self.set = function(name, _table)
async.queue_task(function()
if use_async then
async.queue_task(function()
save_table(name, _table)
end)
else
save_table(name, _table)
end)
end
if add_to_mem_pool then
load_into_mem(name, _table)
end
@ -143,9 +159,14 @@ function colddb.Colddb(dir)
end
self.set_key = function(name)
async.queue_task(function()
if use_async then
async.queue_task(function()
save_key(name)
end)
else
save_key(name)
end)
end
if add_to_mem_pool then
load_into_mem(name, "")
end
@ -231,9 +252,14 @@ function colddb.Colddb(dir)
self.remove = function(name)
mem_pool[name] = nil
mem_pool_del[name] = true
async.queue_task(function()
if use_async then
async.queue_task(function()
delete_file(name)
end)
else
delete_file(name)
end)
end
end
return self