comparison src/luan/modules/lucene/Web_search.luan @ 1536:34ae786771b6

make Web_search transaction safe
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 07 Aug 2020 13:34:25 -0600
parents 4cc4f08a94e1
children f7649ad6e3e7
comparison
equal deleted inserted replaced
1535:e73b72a510b4 1536:34ae786771b6
3 local pairs = Luan.pairs or error() 3 local pairs = Luan.pairs or error()
4 local ipairs = Luan.ipairs or error() 4 local ipairs = Luan.ipairs or error()
5 local range = Luan.range or error() 5 local range = Luan.range or error()
6 local stringify = Luan.stringify or error() 6 local stringify = Luan.stringify or error()
7 local eval = Luan.eval or error() 7 local eval = Luan.eval or error()
8 local type = Luan.type or error()
8 local Io = require "luan:Io.luan" 9 local Io = require "luan:Io.luan"
9 local Http = require "luan:http/Http.luan" 10 local Http = require "luan:http/Http.luan"
10 local String = require "luan:String.luan" 11 local String = require "luan:String.luan"
11 local string_to_number = String.to_number or error() 12 local string_to_number = String.to_number or error()
12 local Html = require "luan:Html.luan" 13 local Html = require "luan:Html.luan"
13 local html_encode = Html.encode or error() 14 local html_encode = Html.encode or error()
14 local Number = require "luan:Number.luan" 15 local Number = require "luan:Number.luan"
16 local Table = require "luan:Table.luan"
17 local size = Table.size or error()
15 18
16 19
17 local Web_search = {} 20 local Web_search = {}
18 21
19 local function style() 22 local function style()
41 } 44 }
42 input[type="submit"] { 45 input[type="submit"] {
43 cursor: pointer; 46 cursor: pointer;
44 padding: .5em; 47 padding: .5em;
45 border-radius: 4px; 48 border-radius: 4px;
49 }
50 textarea[name="old"] {
51 display: none;
46 } 52 }
47 <% 53 <%
48 end 54 end
49 55
50 local function form() 56 local function form()
172 178
173 local function edit(index) 179 local function edit(index)
174 local id = string_to_number(Http.request.parameters.id) 180 local id = string_to_number(Http.request.parameters.id)
175 local doc = index.get_document("id:"..id) 181 local doc = index.get_document("id:"..id)
176 doc = stringify(doc,{strict=true,number_types=true}) 182 doc = stringify(doc,{strict=true,number_types=true})
183 doc = html_encode(doc)
177 %> 184 %>
178 <!doctype html> 185 <!doctype html>
179 <html> 186 <html>
180 <head> 187 <head>
181 <title>Lucene</title> 188 <title>Lucene</title>
185 </head> 192 </head>
186 <body> 193 <body>
187 <h2>Lucene Edit</h2> 194 <h2>Lucene Edit</h2>
188 <form action="?" method=post> 195 <form action="?" method=post>
189 <input hidden name=id value="<%=id%>"> 196 <input hidden name=id value="<%=id%>">
190 <div><textarea name="doc" rows="20" cols="90" autofocus><%=html_encode(doc)%></textarea></div> 197 <textarea name="old" ><%=doc%></textarea>
198 <div><textarea name="doc" rows="20" cols="90" autofocus><%=doc%></textarea></div>
191 <div><input type="submit" value="Update"></div> 199 <div><input type="submit" value="Update"></div>
192 </form> 200 </form>
193 </body> 201 </body>
194 </html> 202 </html>
195 <% 203 <%
196 end 204 end
197 205
206
207 local function equal(tbl1,tbl2)
208 if size(tbl1) ~= size(tbl2) then
209 return false
210 end
211 for key, value1 in pairs(tbl1) do
212 local value2 = tbl2[key]
213 if type(value1) == "table" then
214 if type(value2) ~= "table" or not equal(value1,value2) then
215 return false
216 end
217 elseif value1 ~= value2 then
218 return false
219 end
220 end
221 return true
222 end
198 223
199 local function update(index) 224 local function update(index)
200 local doc = Http.request.parameters.doc 225 local doc = Http.request.parameters.doc
201 doc = eval( doc, "lucene", Number ) 226 doc = eval( doc, "lucene", Number )
202 if doc == nil then 227 local old = Http.request.parameters.old
203 local id = Http.request.parameters.id 228 old = eval( old, "lucene", Number )
204 index.delete("id:"..id) 229 local id = Http.request.parameters.id
205 else 230 index.run_in_transaction( function()
206 index.save(doc) 231 local current = index.get_document("id:"..id)
207 end 232 equal(current,old) or error "document has changed"
233 if doc == nil then
234 index.delete("id:"..id)
235 else
236 index.save(doc)
237 end
238 end )
208 %> 239 %>
209 <!doctype html> 240 <!doctype html>
210 <html> 241 <html>
211 <head> 242 <head>
212 <title>Lucene</title> 243 <title>Lucene</title>