colddb/cli.lua

190 lines
3.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
2015-04-07 14:20:45 +02:00
local COMMANDS = {
SAVE = function(key)
2015-04-06 15:44:20 +02:00
print(db:save(key) and "OK" or "ERROR")
2015-04-07 14:20:45 +02:00
end,
TOUCH = function(key)
if not key then
print("USAGE: TOUCH page")
return
end
2015-04-06 15:44:20 +02:00
if not db[key] then
db[key] = {}
end
print("OK")
2015-04-07 14:20:45 +02:00
end,
SELECT = function(key)
if not key then
print("USAGE: SELECT page")
return
end
2015-04-06 17:17:43 +02:00
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
2015-04-07 14:20:45 +02:00
print("ERROR: database not found, try using 'TOUCH' command first")
end
end,
GET = function(key)
if not key then
print("USAGE: GET key")
return
2015-04-06 15:44:20 +02:00
end
2015-04-07 09:37:11 +02:00
print(pp.format(db[page][key]))
2015-04-07 14:20:45 +02:00
end,
SET = function(key, value)
if not key then
print("USAGE: SET key value")
return
end
2015-04-06 17:17:43 +02:00
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-07 14:20:45 +02:00
end,
INCR = function(key)
if not key then
print("USAGE: INCR key")
return
end
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-07 14:20:45 +02:00
end,
DECR = function(key)
if not key then
print("USAGE: DECR key")
return
end
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-07 14:20:45 +02:00
end,
RPUSH = function(key, value)
if not key then
print("USAGE: RPUSH list value")
return
end
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-07 14:20:45 +02:00
end,
LPUSH = function(key, value)
if not key then
print("USAGE: LPUSH list value")
return
end
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-07 14:20:45 +02:00
end,
RPOP = function(key)
if not key then
print("USAGE: RPOP list")
return
2015-04-06 17:17:43 +02:00
end
2015-04-07 14:20:45 +02:00
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
2015-04-06 17:17:43 +02:00
end
2015-04-07 09:37:11 +02:00
print(db[page][key] and #db[page][key] or 0)
2015-04-07 14:20:45 +02:00
end,
LINDEX = function(key, value)
if not key and not value then
print("USAGE: LINDEX list index")
return
end
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
2015-04-07 14:20:45 +02:00
}
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
2015-04-06 15:44:20 +02:00
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()