changeset 1429:82415c9c0015

move versioning into Lucene
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 24 Nov 2019 23:07:21 -0700
parents d21a7cf8fa9e
children 103d0ce70385
files examples/blog/src/lib/Db.luan examples/blog/src/lib/Post.luan examples/blog/src/private/tools/backup.html.luan examples/blog/src/private/tools/lucene.html.luan examples/blog/test.luan examples/blog/test.sh scripts/test.luan src/luan/modules/lucene/Lucene.luan src/luan/modules/lucene/Versioning.luan
diffstat 9 files changed, 47 insertions(+), 144 deletions(-) [+]
line wrap: on
line diff
--- a/examples/blog/src/lib/Db.luan	Sun Nov 24 21:02:38 2019 -0700
+++ b/examples/blog/src/lib/Db.luan	Sun Nov 24 23:07:21 2019 -0700
@@ -10,8 +10,6 @@
 local logger = Logging.logger "Db"
 
 
-local Db = {}
-
 local postgres_spec = Hosted.postgres_spec()
 --logger.info(stringify(postgres_spec))
 
@@ -19,25 +17,23 @@
 	return doc
 end
 
-function Db.new(lucene_dir)
-	local dir = Io.uri(lucene_dir)
-	local db = Lucene.index( dir, {
-		version = "2"
-		default_type = Lucene.type.english
-		default_fields = {"subject","content"}
-		completer = completer
-		postgres_spec = postgres_spec
-	} )
-	
+local dir = Io.uri("site:/private/local/lucene")
+local Db = Lucene.index( dir, {
+	default_type = Lucene.type.english
+	default_fields = {"subject","content"}
+	completer = completer
+	postgres_spec = postgres_spec
+} )
+
 --	this is how you index a field
 --	db.indexed_fields.post_date = Lucene.type.long
 
-	return db
-end
-
-Db.db = Db.new("site:/private/local/lucene")
-
-Db.db.restore_from_postgres()
-Thread.schedule( Db.db.check, { delay=0, repeating_delay=Time.period{hours=1}, id="blog-db-check" } )
+Db.restore_from_postgres()
+Db.update{
+	[1] = function()
+		logger.info "update"
+	end
+}
+Thread.schedule( Db.check, { delay=0, repeating_delay=Time.period{hours=1}, id="blog-db-check" } )
 
 return Db
--- a/examples/blog/src/lib/Post.luan	Sun Nov 24 21:02:38 2019 -0700
+++ b/examples/blog/src/lib/Post.luan	Sun Nov 24 23:07:21 2019 -0700
@@ -6,7 +6,7 @@
 local now = Time.now or error()
 local String = require "luan:String.luan"
 local trim = String.trim or error()
-local db = require("site:/lib/Db.luan").db or error()
+local Db = require "site:/lib/Db.luan"
 
 
 local Post = {}
@@ -33,7 +33,7 @@
 			content = this.content
 			date = this.date
 		}
-		db.save(doc)
+		Db.save(doc)
 		this.id = doc.id
 	end
 
@@ -41,12 +41,12 @@
 end
 
 function Post.get_by_id(id)
-	local doc = db.get_document("id:"..id)
+	local doc = Db.get_document("id:"..id)
 	return doc and from_doc(doc)
 end
 
 function Post.get_all()
-	local docs = db.search("type:post",1,1000,{sort="id desc"})
+	local docs = Db.search("type:post",1,1000,{sort="id desc"})
 	local posts = {}
 	for _, doc in ipairs(docs) do
 		posts[#posts+1] = from_doc(doc)
@@ -59,7 +59,7 @@
 	if #query == 0 then
 		return Post.get_all()
 	end
-	local docs = db.search(query,1,1000)
+	local docs = Db.search(query,1,1000)
 	local posts = {}
 	for _, doc in ipairs(docs) do
 		posts[#posts+1] = from_doc(doc)
@@ -68,7 +68,7 @@
 end
 
 function Post.delete_by_id(id)
-	db.delete("id:"..id)
+	Db.delete("id:"..id)
 end
 
 return Post
--- a/examples/blog/src/private/tools/backup.html.luan	Sun Nov 24 21:02:38 2019 -0700
+++ b/examples/blog/src/private/tools/backup.html.luan	Sun Nov 24 23:07:21 2019 -0700
@@ -2,13 +2,13 @@
 local error = Luan.error
 local Io = require "luan:Io.luan"
 local Http = require "luan:http/Http.luan"
-local db = require("site:/lib/Db.luan").db or error()
+local Db = require "site:/lib/Db.luan"
 
 
 return function()
 	local backup = Io.uri "site:/private/local/backup.zip"
 	backup.delete()
-	db.zip(backup)
+	Db.zip(backup)
 
 	Io.stdout = Http.response.text_writer()
 %>
--- a/examples/blog/src/private/tools/lucene.html.luan	Sun Nov 24 21:02:38 2019 -0700
+++ b/examples/blog/src/private/tools/lucene.html.luan	Sun Nov 24 23:07:21 2019 -0700
@@ -1,6 +1,6 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
 local Web_search = require "luan:lucene/Web_search.luan"
-local db = require("site:/lib/Db.luan").db or error()
+local Db = require "site:/lib/Db.luan"
 
-return Web_search.of(db)
+return Web_search.of(Db)
--- a/examples/blog/test.luan	Sun Nov 24 21:02:38 2019 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,58 +0,0 @@
-local Luan = require "luan:Luan.luan"
-local error = Luan.error
-local to_string = Luan.to_string or error()
-local Http = require "luan:http/Http.luan"
-local Http_test = require "luan:http/Http_test.luan"
-local init = Http_test.init or error()
-local get_page = Http_test.get_page or error()
-local Io = require "luan:Io.luan"
-local print = Io.print or error()
-local String = require "luan:String.luan"
-local matches = String.matches or error()
-
-local Db = require "site:/lib/Db.luan"
-Db.db = Db.new("site:/private/local/lucene_test")
-Db.db.delete_all()
-
-local Post = require "site:/lib/Post.luan"
-local page
-
-print 'go'
-
-init()
-print '/'
-get_page '/'
-
-init()
-print '/new.html'
-get_page '/new.html'
-
-init()
-Http.request.parameters.subject = 'test'
-Http.request.parameters.content = 'this is a test'
-Http.request.parameters.save = 'whatever'
-print '/new.html submit'
-get_page '/new.html'
-local posts = Post.get_all()
-#posts == 1 or error()
-local post_id = to_string(posts[1].id)
-
-init()
-Http.request.parameters.post = post_id
-print '/edit.html'
-get_page '/edit.html'
-
-init()
-Http.request.parameters.post = post_id
-Http.request.parameters.subject = 'test'
-Http.request.parameters.content = 'this is an edit'
-Http.request.parameters.save = 'whatever'
-print '/edit.html submit'
-get_page '/edit.html'
-
-init()
-print '/ again'
-page = get_page '/'
-matches(page,'this is an edit') or error()
-
-print 'done'
--- a/examples/blog/test.sh	Sun Nov 24 21:02:38 2019 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-luan luan:http/test.luan src test.luan 2>&1 | tee err
--- a/scripts/test.luan	Sun Nov 24 21:02:38 2019 -0700
+++ b/scripts/test.luan	Sun Nov 24 23:07:21 2019 -0700
@@ -12,7 +12,6 @@
 local Http_test = require "luan:http/Http_test.luan"
 local Server = require "luan:http/Server.luan"
 local Lucene = require "luan:lucene/Lucene.luan"
-local Versioning = require "luan:lucene/Versioning.luan"
 local Mail = require "luan:mail/Mail.luan"
 
 local error = Luan.error
--- a/src/luan/modules/lucene/Lucene.luan	Sun Nov 24 21:02:38 2019 -0700
+++ b/src/luan/modules/lucene/Lucene.luan	Sun Nov 24 23:07:21 2019 -0700
@@ -2,10 +2,13 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
 local ipairs = Luan.ipairs or error()
+local pairs = Luan.pairs or error()
 local type = Luan.type or error()
 local set_metatable = Luan.set_metatable or error()
 local Boot = require "luan:Boot.luan"
 local Html = require "luan:Html.luan"
+local Number = require "luan:Number.luan"
+local integer = Number.integer or error()
 local Io = require "luan:Io.luan"
 local uri = Io.uri or error()
 local String = require "luan:String.luan"
@@ -159,6 +162,25 @@
 	end
 	index.restore = Boot.no_security(index.restore)
 
+	function index.update(steps)
+		local version = 0
+		for i in pairs(steps) do
+			if version < i then
+				version = i
+			end
+		end
+		local doc = index.get_document"type:version" or { type="version", version=integer(0) }
+		while doc.version < version do
+			doc.version = integer(doc.version + 1)
+			logger.error("step "..doc.version)
+			index.run_in_transaction( function()
+				local step = steps[doc.version] or error("missing step "..doc.version)
+				step()
+				index.save(doc)
+			end )
+		end
+	end
+
 	local function multi_error()
 		error "multiple lucene instances"
 	end
--- a/src/luan/modules/lucene/Versioning.luan	Sun Nov 24 21:02:38 2019 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,55 +0,0 @@
-local Luan = require "luan:Luan.luan"
-local error = Luan.error
-local pairs = Luan.pairs or error()
-local Number = require "luan:Number.luan"
-local integer = Number.integer or error()
-local long = Number.long or error()
-local String = require "luan:String.luan"
-local matches = String.matches or error()
-local sub = String.sub or error()
-local string_to_number = String.to_number or error()
-local Table = require "luan:Table.luan"
-local copy = Table.copy or error()
-local Lucene = require "luan:lucene/Lucene.luan"
-local Logging = require "luan:logging/Logging.luan"
-
-local logger = Logging.logger "lucene versioning"
-
-
-local Versioning = {}
-
-function Versioning.update(db,steps,version)
-	local doc = db.get_document"type:version" or { type="version", version=integer(0) }
-	while doc.version < version do
-		doc.version = integer(doc.version + 1)
-		logger.error("step "..doc.version)
-		db.run_in_transaction( function()
-			local step = steps[doc.version]
-			step and step(db)
-			db.save(doc)
-		end )
-	end
-end
-
-
--- hack to deal with latest changes
-function Versioning.a_big_step(db)
-	db.indexed_fields["id index"] = Lucene.type.string
-	db.advanced_search( Lucene.quote"id index" .. ":*", function(_,doc_fn)
-		local doc = doc_fn()
-		for field, value in pairs(copy(doc)) do
-			if matches(field," index$") then
-				local new_field = sub(field,1,-7)
-				db.indexed_fields[new_field] or error("field '"..new_field.."' not indexed")
-				doc[new_field] = value
-				doc[field] = nil
-			end
-		end
-		doc.id = long(string_to_number(doc.id))
-		db.save(doc)
-	end )
-	db.indexed_fields["type index"] = Lucene.type.string
-	db.delete( Lucene.quote"type index" .. ":*" )
-end
-
-return Versioning