Add: use_async function

This commit is contained in:
Coder12a 2019-05-19 22:19:20 -05:00
parent 81bbc51dee
commit 19b6648d69

View File

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