rename 'book' to 'page'

This commit is contained in:
uleelx 2015-04-07 15:37:11 +08:00
parent 544ae720a5
commit 52988e3b02
3 changed files with 61 additions and 61 deletions

View File

@ -16,7 +16,7 @@ Concept
| FlatDB | Disk | Lua | | FlatDB | Disk | Lua |
|:--------------:|:-------------:|:--------------:| |:--------------:|:-------------:|:--------------:|
| Database | Directory | Table | | Database | Directory | Table |
| Book | File | Table | | Page | File | Table |
| Key-value pair | File content | Key-value pair | | Key-value pair | File content | Key-value pair |
Keys and values can be all Lua types except coroutines, userdata, cdata and C functions. Keys and values can be all Lua types except coroutines, userdata, cdata and C functions.
@ -36,32 +36,32 @@ local flatdb = require 'flatdb'
local db = flatdb('./db') local db = flatdb('./db')
``` ```
2. Open or create a book 2. Open or create a page
```lua ```lua
if not db.book then if not db.page then
db.book = {} db.page = {}
end end
``` ```
3. Store key-value items 3. Store key-value items
```lua ```lua
db.book.key = 'value' db.page.key = 'value'
-- equivalent to db.book['key'] = 'value' -- equivalent to db.page['key'] = 'value'
``` ```
4. Retrieve items 4. Retrieve items
```lua ```lua
print(db.book.key) -- prints 'value' print(db.page.key) -- prints 'value'
``` ```
5. Save to file 5. Save to file
```lua ```lua
db:save() db:save()
-- 'book' will be saved to './db/book' -- '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 *cli.lua*(a Redis-like command line interface example using FlatDB).
@ -73,21 +73,21 @@ Quick Look
local flatdb = require("flatdb") local flatdb = require("flatdb")
-- open a directory as a database -- open a directory as a database
-- 'db' is just a plain empty Lua table that can contain books -- 'db' is just a plain empty Lua table that can contain pages
local db = flatdb("./db") local db = flatdb("./db")
-- open or create a book named "default" -- open or create a page named "default"
-- it is also a plain empty Lua table where key-value pair stored in -- it is also a plain empty Lua table where key-value pair stored in
if not db.default then if not db.default then
db.default = {} db.default = {}
end end
-- extend db methods for getting values from 'default' book -- extend db methods for getting values from 'default' page
flatdb.hack.get = function(db, key) flatdb.hack.get = function(db, key)
return db.default[key] return db.default[key]
end end
-- extend db methods for setting values to 'default' book -- extend db methods for setting values to 'default' page
flatdb.hack.set = function(db, key, value) flatdb.hack.set = function(db, key, value)
db.default[key] = value db.default[key] = value
end end
@ -97,21 +97,21 @@ flatdb.hack.guard = function(db, f)
setmetatable(db.default, {__newindex = f}) setmetatable(db.default, {__newindex = f})
end end
-- get key-value data from 'default' book -- get key-value data from 'default' page
print(db:get("hello")) print(db:get("hello"))
-- set key-value data to 'default' book -- set key-value data to 'default' page
db:set("hello", "world") db:set("hello", "world")
-- get key-value data from 'default' book -- get key-value data from 'default' page
print(db:get("hello")) print(db:get("hello"))
-- set guard function -- set guard function
db:guard(function(book, key, value) db:guard(function(page, key, value)
print("CREATE KEY permission denied!") print("CREATE KEY permission denied!")
end) end)
-- try creating new key-value pair to 'default' book -- try creating new key-value pair to 'default' page
db:set("key1", 1) db:set("key1", 1)
db:set("key2", 2) db:set("key2", 2)
@ -122,7 +122,7 @@ print(db:get("key1")) -- prints nil
print(db:get("key2")) -- prints nil print(db:get("key2")) -- prints nil
print(db:get("hello")) -- prints 'bye' print(db:get("hello")) -- prints 'bye'
-- store 'default' book to './db/default' file -- store 'default' page to './db/default' file
db:save() db:save()
``` ```
@ -136,15 +136,15 @@ API
Bind a directory as a database, returns nil if 'dir' doesn't exists. Otherwise, it returns a 'db' obeject. Bind a directory as a database, returns nil if 'dir' doesn't exists. Otherwise, it returns a 'db' obeject.
- **db:save([book])** - **db:save([page])**
Save all books or the given book(if specified) contained in db to file. The 'book' argument is a string, the book's name. Save all pages or the given page(if specified) contained in db to file. The 'page' argument is a string, the page's name.
- **Tables** - **Tables**
- **flatdb.hack** - **flatdb.hack**
The 'hack' table contains db's methods. There is only one method 'save(db, book)' in it by default. The 'hack' table contains db's methods. There is only one method 'save(db, page)' in it by default.
It is usually used to extend db methods. It is usually used to extend db methods.
Dependencies Dependencies

58
cli.lua
View File

@ -22,7 +22,7 @@ local function split(s, sep, maxsplit, plain)
end end
local db = assert(flatdb("./db")) local db = assert(flatdb("./db"))
local book = "0" local page = "0"
local function handle(input) local function handle(input)
local c = split(input, " ", 2) local c = split(input, " ", 2)
@ -41,59 +41,59 @@ local function handle(input)
end end
end end
if db[key] then if db[key] then
book = key page = key
print("OK") print("OK")
else else
print("ERROR") print("ERROR")
end end
elseif cmd == "GET" then elseif cmd == "GET" then
print(pp.format(db[book][key])) print(pp.format(db[page][key]))
elseif cmd == "SET" then elseif cmd == "SET" then
local ok, tmp = pcall(load("return "..tostring(value), "=(load)", "t", db)) local ok, tmp = pcall(load("return "..tostring(value), "=(load)", "t", db))
db[book][key] = ok and tmp or value db[page][key] = ok and tmp or value
print("OK") print("OK")
elseif cmd == "INCR" then elseif cmd == "INCR" then
if not db[book][key] then if not db[page][key] then
db[book][key] = 0 db[page][key] = 0
end end
db[book][key] = db[book][key] + 1 db[page][key] = db[page][key] + 1
print("OK") print("OK")
elseif cmd == "DECR" then elseif cmd == "DECR" then
if not db[book][key] then if not db[page][key] then
db[book][key] = 0 db[page][key] = 0
end end
db[book][key] = db[book][key] - 1 db[page][key] = db[page][key] - 1
print("OK") print("OK")
elseif cmd == "RPUSH" then elseif cmd == "RPUSH" then
if not db[book][key] then if not db[page][key] then
db[book][key] = {} db[page][key] = {}
end end
table.insert(db[book][key], value) table.insert(db[page][key], value)
print(#db[book][key]) print(#db[page][key])
elseif cmd == "LPUSH" then elseif cmd == "LPUSH" then
if not db[book][key] then if not db[page][key] then
db[book][key] = {} db[page][key] = {}
end end
table.insert(db[book][key], 1, value) table.insert(db[page][key], 1, value)
print(#db[book][key]) print(#db[page][key])
elseif cmd == "RPOP" then elseif cmd == "RPOP" then
if not db[book][key] then if not db[page][key] then
db[book][key] = {} db[page][key] = {}
end end
print(table.remove(db[book][key], value)) print(table.remove(db[page][key], value))
elseif cmd == "LPOP" then elseif cmd == "LPOP" then
if not db[book][key] then if not db[page][key] then
db[book][key] = {} db[page][key] = {}
end end
print(table.remove(db[book][key], 1)) print(table.remove(db[page][key], 1))
elseif cmd == "LLEN" then elseif cmd == "LLEN" then
print(db[book][key] and #db[book][key] or 0) print(db[page][key] and #db[page][key] or 0)
elseif cmd == "LINDEX" then elseif cmd == "LINDEX" then
if db[book][key] then if db[page][key] then
local i = tonumber(value) local i = tonumber(value)
if i < 0 then i = i + #db[book][key] end if i < 0 then i = i + #db[page][key] end
i = i + 1 i = i + 1
print(db[book][key][i]) print(db[page][key][i])
else else
print("nil") print("nil")
end end
@ -106,7 +106,7 @@ local function main()
end end
io.stdout:setvbuf("no") io.stdout:setvbuf("no")
while true do while true do
local prefix = book == "0" and "" or ("["..book.."]") local prefix = page == "0" and "" or ("["..page.."]")
io.write("flatdb"..prefix.."> ") io.write("flatdb"..prefix.."> ")
local input = io.read("*l") local input = io.read("*l")
if string.upper(input) == "EXIT" or string.upper(input) == "QUIT" then if string.upper(input) == "EXIT" or string.upper(input) == "QUIT" then

View File

@ -9,16 +9,16 @@ local function isDir(path)
return lfs.attributes(path, "mode") == "directory" return lfs.attributes(path, "mode") == "directory"
end end
local function load_book(path) local function load_page(path)
return dofile(path) return dofile(path)
end end
local function store_book(path, book) local function store_page(path, page)
if type(book) == "table" then if type(page) == "table" then
local f = io.open(path, "wb") local f = io.open(path, "wb")
if f then if f then
f:write("return ") f:write("return ")
f:write(pp.format(book)) f:write(pp.format(page))
f:close() f:close()
return true return true
end end
@ -29,16 +29,16 @@ end
local pool = {} local pool = {}
local db_funcs = { local db_funcs = {
save = function(db, bk) save = function(db, p)
if bk then if p then
if type(bk) == "string" and type(db[bk]) == "table" then if type(p) == "string" and type(db[p]) == "table" then
return store_book(pool[db].."/"..bk, db[bk]) return store_page(pool[db].."/"..p, db[p])
else else
return false return false
end end
end end
for bk, book in pairs(db) do for p, page in pairs(db) do
store_book(pool[db].."/"..bk, book) store_page(pool[db].."/"..p, page)
end end
return true return true
end end
@ -48,7 +48,7 @@ local mt = {
__index = function(db, k) __index = function(db, k)
if db_funcs[k] then return db_funcs[k] end if db_funcs[k] then return db_funcs[k] end
if isFile(pool[db].."/"..k) then if isFile(pool[db].."/"..k) then
db[k] = load_book(pool[db].."/"..k) db[k] = load_page(pool[db].."/"..k)
end end
return rawget(db, k) return rawget(db, k)
end end