Mercurial Hosting > luan
changeset 1536:34ae786771b6
make Web_search transaction safe
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 07 Aug 2020 13:34:25 -0600 |
parents | e73b72a510b4 |
children | f7649ad6e3e7 |
files | src/luan/modules/lucene/Web_search.luan |
diffstat | 1 files changed, 38 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/modules/lucene/Web_search.luan Sun Aug 02 21:30:47 2020 -0600 +++ b/src/luan/modules/lucene/Web_search.luan Fri Aug 07 13:34:25 2020 -0600 @@ -5,6 +5,7 @@ local range = Luan.range or error() local stringify = Luan.stringify or error() local eval = Luan.eval or error() +local type = Luan.type or error() local Io = require "luan:Io.luan" local Http = require "luan:http/Http.luan" local String = require "luan:String.luan" @@ -12,6 +13,8 @@ local Html = require "luan:Html.luan" local html_encode = Html.encode or error() local Number = require "luan:Number.luan" +local Table = require "luan:Table.luan" +local size = Table.size or error() local Web_search = {} @@ -44,6 +47,9 @@ padding: .5em; border-radius: 4px; } + textarea[name="old"] { + display: none; + } <% end @@ -174,6 +180,7 @@ local id = string_to_number(Http.request.parameters.id) local doc = index.get_document("id:"..id) doc = stringify(doc,{strict=true,number_types=true}) + doc = html_encode(doc) %> <!doctype html> <html> @@ -187,7 +194,8 @@ <h2>Lucene Edit</h2> <form action="?" method=post> <input hidden name=id value="<%=id%>"> - <div><textarea name="doc" rows="20" cols="90" autofocus><%=html_encode(doc)%></textarea></div> + <textarea name="old" ><%=doc%></textarea> + <div><textarea name="doc" rows="20" cols="90" autofocus><%=doc%></textarea></div> <div><input type="submit" value="Update"></div> </form> </body> @@ -196,15 +204,38 @@ end +local function equal(tbl1,tbl2) + if size(tbl1) ~= size(tbl2) then + return false + end + for key, value1 in pairs(tbl1) do + local value2 = tbl2[key] + if type(value1) == "table" then + if type(value2) ~= "table" or not equal(value1,value2) then + return false + end + elseif value1 ~= value2 then + return false + end + end + return true +end + local function update(index) local doc = Http.request.parameters.doc doc = eval( doc, "lucene", Number ) - if doc == nil then - local id = Http.request.parameters.id - index.delete("id:"..id) - else - index.save(doc) - end + local old = Http.request.parameters.old + old = eval( old, "lucene", Number ) + local id = Http.request.parameters.id + index.run_in_transaction( function() + local current = index.get_document("id:"..id) + equal(current,old) or error "document has changed" + if doc == nil then + index.delete("id:"..id) + else + index.save(doc) + end + end ) %> <!doctype html> <html>