From 8850f57ae37acd3508a33ec071c5b503ca594baa Mon Sep 17 00:00:00 2001 From: uleelx Date: Sat, 2 May 2015 21:57:17 +0800 Subject: [PATCH] use lua-MessagePack instead of pp --- README-zh.md | 6 +- README.md | 6 +- cli.lua | 189 --------------------------------------------------- flatdb.lua | 13 ++-- 4 files changed, 13 insertions(+), 201 deletions(-) delete mode 100644 cli.lua diff --git a/README-zh.md b/README-zh.md index 6862b5f..b87f637 100644 --- a/README-zh.md +++ b/README-zh.md @@ -68,7 +68,7 @@ local flatdb = require 'flatdb' -- 'page' will be saved to './db/page' ``` -更多用法可参考本项目中的*cli.lua*(一个用FlatDB模拟的类Redis命令行客户端例子)。 +更多用法可参考另一个项目[Ledis](https://github.com/uleelx/ledis)(一个使用FlatDB作为存储后端的Redis实现)。 范例概览 ========== @@ -155,9 +155,7 @@ API 依赖项 ======= -- [pp](https://github.com/luapower/pp) - -以上的依赖库都可以在[LuaPower](https://luapower.com/)上找到。 +- [lua-MessagePack](https://github.com/fperrad/lua-MessagePack) 许可证 ======= diff --git a/README.md b/README.md index 0d4b462..4f1cb50 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ local flatdb = require 'flatdb' -- 'page' will be saved to './db/page' ``` -More usage can be found in the *cli.lua*(a Redis-like command line interface example using FlatDB). +More usage can be found in the [Ledis](https://github.com/uleelx/ledis)(an alternative of Redis server using FlatDB). Quick Look ========== @@ -151,9 +151,7 @@ API Dependencies ======= -- [pp](https://github.com/luapower/pp) - -All above libraries can be found in [LuaPower](https://luapower.com/). +- [lua-MessagePack](https://github.com/fperrad/lua-MessagePack) License ======= diff --git a/cli.lua b/cli.lua deleted file mode 100644 index 4b33c7f..0000000 --- a/cli.lua +++ /dev/null @@ -1,189 +0,0 @@ -local flatdb = require("flatdb") -local pp = require("pp") - -local function split(s, sep, maxsplit, plain) - assert(sep and sep ~= "") - maxsplit = maxsplit or 1/0 - local items = {} - if #s > 0 then - local init = 1 - for i = 1, maxsplit do - local m, n = s:find(sep, init, plain) - if m and m <= n then - table.insert(items, s:sub(init, m - 1)) - init = n + 1 - else - break - end - end - table.insert(items, s:sub(init)) - end - return items -end - -local db = assert(flatdb("./db")) -local page = "0" - -local COMMANDS = { - SAVE = function(key) - print(db:save(key) and "OK" or "ERROR") - end, - TOUCH = function(key) - if not key then - print("USAGE: TOUCH page") - return - end - if not db[key] then - db[key] = {} - end - print("OK") - end, - SELECT = function(key) - if not key then - print("USAGE: SELECT page") - return - end - if tonumber(key) and tonumber(key) < 16 then - if not db[key] then - db[key] = {} - end - end - if db[key] then - page = key - print("OK") - else - print("ERROR: database not found, try using 'TOUCH' command first") - end - end, - GET = function(key) - if not key then - print("USAGE: GET key") - return - end - print(pp.format(db[page][key])) - end, - SET = function(key, value) - if not key then - print("USAGE: SET key value") - return - end - local ok, tmp = pcall(load("return "..tostring(value), "=(load)", "t", db)) - db[page][key] = ok and tmp or value - print("OK") - end, - INCR = function(key) - if not key then - print("USAGE: INCR key") - return - end - if not db[page][key] then - db[page][key] = 0 - end - db[page][key] = db[page][key] + 1 - print("OK") - end, - DECR = function(key) - if not key then - print("USAGE: DECR key") - return - end - if not db[page][key] then - db[page][key] = 0 - end - db[page][key] = db[page][key] - 1 - print("OK") - end, - RPUSH = function(key, value) - if not key then - print("USAGE: RPUSH list value") - return - end - if not db[page][key] then - db[page][key] = {} - end - table.insert(db[page][key], value) - print(#db[page][key]) - end, - LPUSH = function(key, value) - if not key then - print("USAGE: LPUSH list value") - return - end - if not db[page][key] then - db[page][key] = {} - end - table.insert(db[page][key], 1, value) - print(#db[page][key]) - end, - RPOP = function(key) - if not key then - print("USAGE: RPOP list") - return - end - if db[page][key] then - print(table.remove(db[page][key])) - else - print("nil") - end - end, - LPOP = function(key) - if not key then - print("USAGE: LPOP list") - return - end - if db[page][key] then - print(table.remove(db[page][key], 1)) - else - print("nil") - end - end, - LLEN = function(key) - if not key then - print("USAGE: LLEN list") - return - end - print(db[page][key] and #db[page][key] or 0) - end, - LINDEX = function(key, value) - if not key and not value then - print("USAGE: LINDEX list index") - return - end - if db[page][key] then - local i = tonumber(value) - if i < 0 then i = i + #db[page][key] end - i = i + 1 - print(db[page][key][i]) - else - print("nil") - end - end -} - -local function handle(input) - local c = split(input, " ", 2) - local cmd, key, value = string.upper(c[1]), c[2], c[3] - if COMMANDS[cmd] then - COMMANDS[cmd](key, value) - else - print("ERROR: command not found") - end -end - -local function main() - if not db["0"] then - db["0"] = {} - end - io.stdout:setvbuf("no") - while true do - local prefix = page == "0" and "" or ("["..page.."]") - io.write("flatdb"..prefix.."> ") - local input = io.read("*l") - if string.upper(input) == "EXIT" or string.upper(input) == "QUIT" then - break - end - handle(input) - end -end - -main() diff --git a/flatdb.lua b/flatdb.lua index 91f5282..7414953 100644 --- a/flatdb.lua +++ b/flatdb.lua @@ -1,4 +1,4 @@ -local pp = require("pp") +local mp = require("MessagePack") local function isFile(path) local f = io.open(path, "r") @@ -21,15 +21,20 @@ local function isDir(path) end local function load_page(path) - return dofile(path) + local ret + local f = io.open(path, "rb") + if f then + ret = mp.unpack(f:read("*a")) + f:close() + end + return ret end local function store_page(path, page) if type(page) == "table" then local f = io.open(path, "wb") if f then - f:write("return ") - f:write(pp.format(page)) + f:write(mp.pack(page)) f:close() return true end