/usr/share/doc/libkyototycoon2/example/ktscrjsonex.lua is in kyototycoon-doc 0.9.56-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | kt = __kyototycoon__
db = kt.db
json = require("json")
-- post a new document
function writedoc(inmap, outmap)
local doc = inmap.doc
if not doc then
return kt.RVEINVALID
end
local doc = json.decode(doc)
local owner = tonumber(doc.owner)
local date = tonumber(doc.date)
local title = tostring(doc.title)
local text = tostring(doc.text)
local key = string.format("%010d:%010d", owner, date)
local doc = {}
doc.title = title
doc.text = text
local value = json.encode(doc)
if not db:add(key, value) then
local err = db:error()
if err:code() == kt.Error.DUPREC then
return kt.RVELOGIC
end
return kt.RVEINTERNAL
end
return kt.RVSUCCESS
end
-- post a new comment
function addcomment(inmap, outmap)
local owner = tonumber(inmap.owner)
local date = tonumber(inmap.date)
local comowner = tonumber(inmap.comowner)
local comdate = tostring(inmap.comdate)
local comtext = tostring(inmap.comtext)
if not owner or not date or not comowner or not comdate or not comtext then
return kt.RVEINVALID
end
local key = string.format("%010d:%010d", owner, date)
local hit = false
local function visit(rkey, rvalue)
local doc = json.decode(rvalue)
if not doc.comments then
doc.comments = {}
end
local newcom = {}
newcom.owner = comowner
newcom.date = comdate
newcom.text = comtext
table.insert(doc.comments, newcom)
hit = true
return json.encode(doc)
end
if not db:accept(key, visit) then
local err = db:error()
if err:code() == kt.Error.DUPREC then
return kt.RVELOGIC
end
return kt.RVEINTERNAL
end
if not hit then
return kt.RVELOGIC
end
return kt.RVSUCCESS
end
-- get a document
function getdoc(inmap, outmap)
local owner = tonumber(inmap.owner)
local date = tonumber(inmap.date)
if not owner or not date then
return kt.RVEINVALID
end
local key = string.format("%010d:%010d", owner, date)
local value = db:get(key)
if not value then
return kt.RVELOGIC
end
local doc = _composedoc(key, value)
outmap.doc = json.encode(doc)
return kt.RVSUCCESS
end
-- list documents of a user
function listnewdocs(inmap, outmap)
local owner = tonumber(inmap.owner)
local max = tonumber(inmap.max)
if not owner then
return kt.RVEINVALID
end
if not max then
max = 10
end
local key = string.format("%010d:", owner)
local cur = db:cursor()
cur:jump_back(key .. "~")
local num = 0
while num < max do
local rkey, rvalue = cur:get()
if not rkey or not kt.strfwm(rkey, key) then
break
end
cur:step_back()
local doc = _composedoc(rkey, rvalue)
num = num + 1
outmap[num] = json.encode(doc)
end
return kt.RVSUCCESS
end
-- list documents of a user without the text and the comments
function listnewdocslight(inmap, outmap)
local owner = tonumber(inmap.owner)
local max = tonumber(inmap.max)
if not owner then
return kt.RVEINVALID
end
if not max then
max = 10
end
local key = string.format("%010d:", owner)
local cur = db:cursor()
cur:jump_back(key .. "~")
local num = 0
while num < max do
local rkey, rvalue = cur:get()
if not rkey or not kt.strfwm(rkey, key) then
break
end
cur:step_back()
local doc = _composedoc(rkey, rvalue)
doc.text = nil
doc.comnum = #doc.comments
doc.comments = nil
num = num + 1
outmap[num] = json.encode(doc)
end
return kt.RVSUCCESS
end
-- helper to compose a document object
function _composedoc(key, value)
local doc = json.decode(value)
doc.owner = string.gsub(key, ":%d*", "")
doc.date = string.gsub(key, "%d*:", "")
if not doc.comments then
doc.comments = {}
end
return doc
end
|