From 19b6648d69578ab7f0853ee514568ade6759bb75 Mon Sep 17 00:00:00 2001 From: Coder12a <38924418+Coder12a@users.noreply.github.com> Date: Sun, 19 May 2019 22:19:20 -0500 Subject: [PATCH] Add: use_async function --- colddb.lua | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/colddb.lua b/colddb.lua index 421eb31..a6cf88f 100644 --- a/colddb.lua +++ b/colddb.lua @@ -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