storage backend prepared for sqlite

This commit is contained in:
Joachim Stolberg 2020-05-19 22:24:50 +02:00
parent bb6f85c9c7
commit 2eb9546625
4 changed files with 47 additions and 7 deletions

View File

@ -62,19 +62,19 @@ function backend.get_nodepos(number)
end end
function backend.set_nodepos(number, pos) function backend.set_nodepos(number, pos)
storage:get_string(number, minetest.pos_to_string(pos)) storage:set_string(number, minetest.pos_to_string(pos))
end end
function backend.add_nodepos(pos) function backend.add_nodepos(pos)
local num = tostring(NextNumber) local num = tostring(NextNumber)
NextNumber = NextNumber + 1 NextNumber = NextNumber + 1
storage:set_int("NextNumber", NextNumber) storage:set_int("NextNumber", NextNumber)
storage:get_string(num, minetest.pos_to_string(pos)) storage:set_string(num, minetest.pos_to_string(pos))
return num return num
end end
function backend.del_nodepos(number) function backend.del_nodepos(number)
storage:get_string(number, "") storage:set_string(number, "")
end end
-- delete invalid entries -- delete invalid entries
@ -87,6 +87,8 @@ function backend.delete_invalid_entries(node_def)
local name = techage.get_node_lvm(pos).name local name = techage.get_node_lvm(pos).name
if not node_def[name] then if not node_def[name] then
backend.del_nodepos(number) backend.del_nodepos(number)
else
minetest.get_meta(pos):set_string("node_number", number)
end end
end end
end end

View File

@ -19,7 +19,7 @@ local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local NodeInfoCache = {} local NodeInfoCache = {}
local MP = minetest.get_modpath("techage") local MP = minetest.get_modpath("techage")
local use_database = minetest.settings:get_bool('techage.use_database', false) local use_database = minetest.settings:get_bool('techage_use_database', false)
-- Localize functions to avoid table lookups (better performance) -- Localize functions to avoid table lookups (better performance)
local string_split = string.split local string_split = string.split
@ -60,7 +60,7 @@ end
-- Keep the cache size small by deleting entries randomly -- Keep the cache size small by deleting entries randomly
local function keep_small(number) local function keep_small(number)
number = delete_nodeinfo_entry(number) number = delete_nodeinfo_entry(number)
minetest.after(2, keep_small, number) minetest.after(10, keep_small, number)
end end
keep_small() keep_small()

View File

@ -26,6 +26,34 @@ local StoredNodes = 0
local NextNum = 0 local NextNum = 0
local Timeslot = 0 local Timeslot = 0
local FNAME = minetest.get_worldpath()..DIR_DELIM.."techage_metadata.txt" local FNAME = minetest.get_worldpath()..DIR_DELIM.."techage_metadata.txt"
local use_marshal = minetest.settings:get_bool('techage_use_marshal', false)
local MAR_MAGIC = 0x8e
-- default functions
local serialize = minetest.serialize
local deserialize = minetest.deserialize
if use_marshal then
if not techage.IE then
error("Please add 'secure.trusted_mods = techage' to minetest.conf!")
end
local marshal = techage.IE.require("marshal")
if not marshal then
error("Please install marshal via 'luarocks install lua-marshal'")
end
serialize = marshal.encode
deserialize = function(s)
if s ~= "" then
if s:byte(1) == MAR_MAGIC then
return marshal.decode(s)
else
return minetest.deserialize(s)
end
end
end
end
local function read_file() local function read_file()
local f = io.open(FNAME, "r") local f = io.open(FNAME, "r")
@ -51,10 +79,13 @@ minetest.register_on_shutdown(function()
end) end)
local function set_metadata(hash, tbl) local function set_metadata(hash, tbl)
local pos = minetest.get_position_from_hash(hash) local pos = minetest.get_position_from_hash(hash)
tbl.USED = nil tbl.USED = nil
local data = minetest.serialize(tbl) local data = serialize(tbl)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("ta_data", data) meta:set_string("ta_data", data)
meta:mark_as_private("ta_data") meta:mark_as_private("ta_data")
@ -65,7 +96,7 @@ local function get_metadata(hash)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local s = meta:get_string("ta_data") local s = meta:get_string("ta_data")
if s ~= "" then if s ~= "" then
return minetest.deserialize(s) return deserialize(s)
end end
end end

View File

@ -46,6 +46,9 @@ techage.basalt_stone_enabled = minetest.settings:get_bool("techage_basalt_stone_
techage.ore_rarity = tonumber(minetest.settings:get("techage_ore_rarity")) or 1 techage.ore_rarity = tonumber(minetest.settings:get("techage_ore_rarity")) or 1
techage.modified_recipes_enabled = minetest.settings:get_bool("techage_modified_recipes_enabled") ~= false techage.modified_recipes_enabled = minetest.settings:get_bool("techage_modified_recipes_enabled") ~= false
-- allow to load marshal and sqlite3
techage.IE = minetest.request_insecure_environment()
-- Load support for I18n. -- Load support for I18n.
techage.S = minetest.get_translator("techage") techage.S = minetest.get_translator("techage")
@ -296,3 +299,7 @@ end
-- Carts -- Carts
dofile(MP.."/carts/tank_cart.lua") dofile(MP.."/carts/tank_cart.lua")
dofile(MP.."/carts/chest_cart.lua") dofile(MP.."/carts/chest_cart.lua")
-- Prevent other mods from using IE
techage.IE = nil