colddb/cli.lua

120 lines
2.7 KiB
Lua
Raw Normal View History

2015-04-06 15:44:20 +02:00
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"))
2015-04-07 09:37:11 +02:00
local page = "0"
2015-04-06 15:44:20 +02:00
local function handle(input)
local c = split(input, " ", 2)
local cmd, key, value = string.upper(c[1]), c[2], c[3]
2015-04-06 17:17:43 +02:00
if cmd == "SAVE" then
2015-04-06 15:44:20 +02:00
print(db:save(key) and "OK" or "ERROR")
elseif cmd == "TOUCH" then
if not db[key] then
db[key] = {}
end
print("OK")
2015-04-06 17:17:43 +02:00
elseif cmd == "SELECT" then
if tonumber(key) and tonumber(key) < 16 then
if not db[key] then
db[key] = {}
end
end
2015-04-06 15:44:20 +02:00
if db[key] then
2015-04-07 09:37:11 +02:00
page = key
2015-04-06 15:44:20 +02:00
print("OK")
else
print("ERROR")
end
2015-04-06 17:17:43 +02:00
elseif cmd == "GET" then
2015-04-07 09:37:11 +02:00
print(pp.format(db[page][key]))
2015-04-06 17:17:43 +02:00
elseif cmd == "SET" then
local ok, tmp = pcall(load("return "..tostring(value), "=(load)", "t", db))
2015-04-07 09:37:11 +02:00
db[page][key] = ok and tmp or value
2015-04-06 17:17:43 +02:00
print("OK")
2015-04-06 15:44:20 +02:00
elseif cmd == "INCR" then
2015-04-07 09:37:11 +02:00
if not db[page][key] then
db[page][key] = 0
2015-04-06 15:44:20 +02:00
end
2015-04-07 09:37:11 +02:00
db[page][key] = db[page][key] + 1
2015-04-06 15:44:20 +02:00
print("OK")
elseif cmd == "DECR" then
2015-04-07 09:37:11 +02:00
if not db[page][key] then
db[page][key] = 0
2015-04-06 15:44:20 +02:00
end
2015-04-07 09:37:11 +02:00
db[page][key] = db[page][key] - 1
2015-04-06 15:44:20 +02:00
print("OK")
2015-04-06 17:17:43 +02:00
elseif cmd == "RPUSH" then
2015-04-07 09:37:11 +02:00
if not db[page][key] then
db[page][key] = {}
2015-04-06 17:17:43 +02:00
end
2015-04-07 09:37:11 +02:00
table.insert(db[page][key], value)
print(#db[page][key])
2015-04-06 17:17:43 +02:00
elseif cmd == "LPUSH" then
2015-04-07 09:37:11 +02:00
if not db[page][key] then
db[page][key] = {}
2015-04-06 17:17:43 +02:00
end
2015-04-07 09:37:11 +02:00
table.insert(db[page][key], 1, value)
print(#db[page][key])
2015-04-06 17:17:43 +02:00
elseif cmd == "RPOP" then
2015-04-07 09:37:11 +02:00
if not db[page][key] then
db[page][key] = {}
2015-04-06 17:17:43 +02:00
end
2015-04-07 09:37:11 +02:00
print(table.remove(db[page][key], value))
2015-04-06 17:17:43 +02:00
elseif cmd == "LPOP" then
2015-04-07 09:37:11 +02:00
if not db[page][key] then
db[page][key] = {}
2015-04-06 17:17:43 +02:00
end
2015-04-07 09:37:11 +02:00
print(table.remove(db[page][key], 1))
2015-04-06 17:17:43 +02:00
elseif cmd == "LLEN" then
2015-04-07 09:37:11 +02:00
print(db[page][key] and #db[page][key] or 0)
2015-04-06 17:17:43 +02:00
elseif cmd == "LINDEX" then
2015-04-07 09:37:11 +02:00
if db[page][key] then
2015-04-06 17:17:43 +02:00
local i = tonumber(value)
2015-04-07 09:37:11 +02:00
if i < 0 then i = i + #db[page][key] end
2015-04-06 17:17:43 +02:00
i = i + 1
2015-04-07 09:37:11 +02:00
print(db[page][key][i])
2015-04-06 17:17:43 +02:00
else
print("nil")
end
2015-04-06 15:44:20 +02:00
end
end
local function main()
2015-04-06 17:17:43 +02:00
if not db["0"] then
db["0"] = {}
2015-04-06 15:44:20 +02:00
end
io.stdout:setvbuf("no")
while true do
2015-04-07 09:37:11 +02:00
local prefix = page == "0" and "" or ("["..page.."]")
2015-04-06 17:17:43 +02:00
io.write("flatdb"..prefix.."> ")
2015-04-06 15:44:20 +02:00
local input = io.read("*l")
2015-04-06 17:17:43 +02:00
if string.upper(input) == "EXIT" or string.upper(input) == "QUIT" then
2015-04-06 15:44:20 +02:00
break
end
handle(input)
end
end
main()