Minetest mod - based on https://github.com/uleelx/FlatDB
Go to file
sys4-fr e28a2b28e2 Ajoute message de chargement du mod dans le journal action 2019-02-08 18:24:26 +01:00
LICENSE Upload mod 2018-11-05 20:05:04 -06:00
README.md Code clean up and function rename 2019-01-14 18:17:45 -06:00
async.lua Major update 2019-01-08 00:32:34 -06:00
colddb.lua set and save: Memory 2019-02-02 18:28:55 -06:00
depends.txt Make database asynchronous 2018-11-10 21:40:49 -06:00
description.txt Make database asynchronous 2018-11-10 21:40:49 -06:00
init.lua Ajoute message de chargement du mod dans le journal action 2019-02-08 18:24:26 +01:00
mod.conf Upload mod 2018-11-05 20:05:04 -06:00

README.md

ColdDB

ColdDB is a minetest mod that implements a serverless, asynchronous, NoSQL database engine.
It provides a key or key-value based storage system using plain Lua tables.

Usage

Copy both colddb.lua and async files to your minetest mod or game. Copy the code from colddb's init file to your mods init file
Write this code in your lua file.

  1. create a directory and link it as a database.
coldbase = colddb.Colddb("mydb")
  1. store key item(this key has no value)
coldbase.set_key("MyKey")
  1. store key-value item
coldbase.set("MyKeyAndValue", "Hello world")
  1. retrieve items (get_key's callback(arg) will return true, false, or nil)
coldbase.get("MyKeyAndValue", nil, function(arg)
	if arg then
		minetest.log(string.format("value:%s", arg))
	end
end)
coldbase.get_key("MyKey", nil, function(arg)
	if arg then
		minetest.log("Found key")
	else
		minetest.log("Key not found")
	end
end)
  1. delete key(file) this function works on both keys and key-value keys.
coldbase.remove("MyKeyAndValue")
  1. if add_to_mem_pool is true(true by default). keys are stored in a weak lua table(memory) it will be removed by the gc if its not in-use. Storing data in memory is to prevent the database from constantly loading up data from files.
coldbase.add_to_mem_pool = true
  1. returns the amount of entries from the entry file.
coldbase.get_entry_file_count(name, tag_name)
  1. iterates through the entry file(breaks and ends if it reaches the end of the file).
coldbase.iterate_entry_file(name, begin_func, func_on_iterate, end_func, args, tag_name)
  1. adds a folder which can be used in other functions that have tag_name arg.
coldbase.add_tag("Extra_Folder", {"Extra", "Folder"})
  1. returns the tag name if the tag does not exists it creates one.
coldbase.get_or_add_tag("Extra_Folder", {"Extra", "Folder"}, no_new_path)
  1. remove tag by name.
coldbase.remove_tag("Extra_Folder")

Quick Look

-- create an directory(watchlist) and link it as a database.
ip_db = colddb.Colddb("watchlist")

-- When ever a player join's his/her ip address is recorded to the database by player name.
minetest.register_on_prejoinplayer(function(name, ip)
	ip_db.set(name, ip, ip_db.get_or_add_tag("ips", "ips"))
end)

minetest.register_chatcommand("ip", {
	params = "<player>",
	description = "Get an player's ip address.",
    func = function(name, param)
		-- Get the ip record asynchronously.
		ip_db.get(param, ip_db.get_or_add_tag("ips", "ips"), function(record)
			-- If record is contains data send it to the player.
			if record then
				minetest.chat_send_player(name, string.format("%s:%s", param, record))
			else
				-- No record was found.
				minetest.chat_send_player(name, "Can not find ip record.")
			end
		end)
    end
})

minetest.register_chatcommand("clear", {
	params = "<player>",
	description = "Clear out the ip database.",
    func = function(name, param)
		ip_db.remove_tag(ip_db.get_or_add_tag("ips", "ips"))
		minetest.chat_send_player(name, "Ip Database Cleared!")
    end
})

Quick Look Notes

In the example above we could also create a more complex ip database using tags. Creating tags named after the player then assigning the ip files to them.
This way we could store many ips associated with the player instead of just one ip.

License

ColdDB is distributed under the LGPLv2.1+ license.