forked from asb/lua-tinycdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.lua
59 lines (49 loc) · 1.98 KB
/
example.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#! /usr/bin/env lua
-- lua-tinycdb does not assign to any global variables, so name it yourself
cdb = require("cdb")
-- make new cdb, creating a file called example.cdb.tmp that will be renamed
-- to example.cdb when finished. Due to the semantics of rename, this will
-- atomically replace example.cdb with the newly built database
maker = assert(cdb.make("example.cdb", "example.cdb.tmp"))
maker:add("key", "value")
maker:add("foo", "bar")
-- note it is possible to have multiple values for the same key
maker:add("foo", "baz")
maker:add("baz", "qux")
-- a 3rd argument can be given to maker:add() that controls the behaviour when
-- adding a key that already exists:
-- * "add": the default. No duplicate checking will be performed.
--
-- * "replace": If the key already exists, it will be removed from the
-- database before adding new key,value pair. This requires moving data in
-- the file, and can be quite slow if the file is large. All matching old
-- records will be removed this way.
--
-- * "replace0": If the key already exists and it isn't the last record in
-- the file, old record will be zeroed out before adding new key,value
-- pair. This is alot faster than CDB_PUT_REPLACE, but some extra data will
-- still be present in the file. The data -- old record -- will not be
-- accessible by normal searches, but will appear in sequential database
-- traversal.
--
-- * "insert": add key,value pair only if such a key does not exists in the
-- database.
-- renames example.cdb.tmp to example.cdb and closes file
assert(maker:finish())
-- open our new database
db = cdb.open("example.cdb")
local var = db:get("key") -- "value"
local var2 = db:get("baz") -- "qux"
local var3 = db:get("foo") -- "bar", as get() returns the first value added
-- outputs:
--
-- key value
-- foo bar
-- foo baz
-- baz qux
for k, v in db:pairs() do
print(k, v)
end
-- find_all returns a table of all values for the given key
local t = db:find_all("foo") -- t = { "bar", "baz" }
db:close()