-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkyototycoon_ext.lua
78 lines (73 loc) · 1.73 KB
/
kyototycoon_ext.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
kt = __kyototycoon__
db = kt.db
-- log the start-up message
if kt.thid == 0 then
kt.log("system", "the Lua script has been loaded")
end
-- atomically increment counts
function incrementbulk(inmap, outmap)
--db:begin_transaction()
for key, value in pairs(inmap) do
if not db:increment(key, tonumber(value), 0) then
return kt.RVEINTERNAL
end
end
--db:end_transaction()
return kt.RVSUCCESS
end
function incrementbulk2(inmap, outmap)
local function visit(key, value)
return key, value + inmap[key]
end
local keys = {}
for key, value in pairs(inmap) do
table.insert(keys, key)
end
if not db:accept_bulk(keys, visit) then
return kt.RVEINTERNAL
end
return kt.RVSUCCESS
end
-- remove records at once
function incrementbulk3(inmap, outmap)
db:begin_transaction()
local keys = {}
for key, value in pairs(inmap) do
print (key)
table.insert(keys, key)
end
local values = db:get_bulk(keys)
for key,value in pairs(values) do
if value == "" then
value = 0
end
print ("*" .. tonumber(value) .. "*")
print ("*" .. string.len(value) .. "*")
print("*")
for i=1,string.len(value) do
print (value:byte(i))
end
print ("*")
values[key] = tonumber(value) + inmap[key]
end
--[[
num = db:set_bulk(values)
if num < 0 then
db:end_transaction()
return kt.RVEINTERNAL
end
outmap["num"] = num
db:end_transaction()]]--
return kt.RVSUCCESS
end
-- list all records
function list(inmap, outmap)
local cur = db:cursor()
cur:jump()
while true do
local key, value, xt = cur:get(true)
if not key then break end
outmap[key] = value
end
return kt.RVSUCCESS
end