changeset 1369:709f7498a363

change Lucene.index() and add Lucene.recover()
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 19 Jun 2019 00:26:10 -0600
parents 5225cd6ed478
children ba4fc39423a4
files conv.txt examples/blog/src/lib/Db.luan examples/blog/src/lib/Post.luan scripts/test.luan src/luan/modules/lucene/Lucene.luan src/luan/modules/lucene/LuceneIndex.java
diffstat 6 files changed, 29 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/conv.txt	Tue Jun 18 21:56:53 2019 -0600
+++ b/conv.txt	Wed Jun 19 00:26:10 2019 -0600
@@ -1,3 +1,4 @@
+Lucene.index
 Thread.schedule
 
 java()
--- a/examples/blog/src/lib/Db.luan	Tue Jun 18 21:56:53 2019 -0600
+++ b/examples/blog/src/lib/Db.luan	Wed Jun 19 00:26:10 2019 -0600
@@ -5,7 +5,7 @@
 local Db = {}
 
 function Db.new(lucene_dir)
-	local dir = Io.uri(lucene_dir).to_string()
+	local dir = Io.uri(lucene_dir)
 	local db = Lucene.index( dir, Lucene.type.english, {"subject","content"} )
 	
 --	this is how you index a field
--- a/examples/blog/src/lib/Post.luan	Tue Jun 18 21:56:53 2019 -0600
+++ b/examples/blog/src/lib/Post.luan	Wed Jun 19 00:26:10 2019 -0600
@@ -46,7 +46,7 @@
 end
 
 function Post.get_all()
-	local docs = db.search("type:post",1,1000,"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)
--- a/scripts/test.luan	Tue Jun 18 21:56:53 2019 -0600
+++ b/scripts/test.luan	Wed Jun 19 00:26:10 2019 -0600
@@ -36,11 +36,12 @@
 page = run_page(require("luan:http/tools/Run.luan").respond)
 trim(page) == "hi" or error "failed"
 
+--[[
 init()
 Http.request.parameters.cmd = "'ab'..'cd'"
 page = run_page(require("luan:http/tools/Shell.luan").respond)
 find(page,"abcd") or error "failed"
-
+]]
 
 -- lucene
 
@@ -48,7 +49,7 @@
 local this_dir = this_file.parent()
 local lucene_dir = this_dir.parent().child("build").child("lucene_test")
 --print(lucene_dir.to_string())
-local db = Lucene.index(lucene_dir.to_string())
+local db = Lucene.index(lucene_dir)
 db.delete_all()
 
 local Web_search = require "luan:lucene/Web_search.luan"
@@ -70,12 +71,14 @@
 	return Io.uri( "file:../website/src"..path )
 end
 
+--[[
 init(); get_page "/"
 init(); get_page "/docs.html"
 init(); get_page "/tutorial.html"
 init(); get_page "/pil.html"
 init(); get_page "/manual.html"
 init(); get_page "/diff.html"
+]]
 init(); get_page "/examples/hi.html"
 init(); get_page "/examples/hi2.html"
 init(); get_page "/examples/shell.html"
--- a/src/luan/modules/lucene/Lucene.luan	Tue Jun 18 21:56:53 2019 -0600
+++ b/src/luan/modules/lucene/Lucene.luan	Wed Jun 19 00:26:10 2019 -0600
@@ -37,9 +37,11 @@
 Lucene.literal = SaneQueryParser.literal
 
 function Lucene.index(index_dir,default_type,default_fields)
+	type(index_dir)=="table" or error "index_dir must be table"
+	index_dir.to_uri_string and matches(index_dir.to_uri_string(),"^file:") or error "must be file"
 	local index = {}
 	index.dir = index_dir
-	local java_index, closer = LuceneIndex.getLuceneIndex(index_dir,default_type,default_fields)
+	local java_index, closer = LuceneIndex.getLuceneIndex(index_dir.java.file,default_type,default_fields)
 	index.java = java_index
 	index.closer = closer or error()
 
@@ -145,7 +147,7 @@
 	end
 
 	function index.schedule_backups_to(backup_dir,repeating_delay)
-		local lucene_dir = uri("file:"..index.dir)
+		local lucene_dir = index.dir
 		local function backup()
 			if backup_dir.last_modified() < lucene_dir.last_modified() then
 				run_for_backup(function()
@@ -178,7 +180,7 @@
 
 	function index.restore(zip_file)
 		java_index.run_in_lock( function()
-			local lucene_dir = uri("file:"..index.dir)
+			local lucene_dir = index.dir
 			local before_restore = lucene_dir.parent().child("before_restore.zip")
 			index.zip(before_restore)
 			java_index.doClose()
@@ -197,14 +199,14 @@
 
 		function Rpc.functions.lucene_backup(password)
 			Io.password == password or error "wrong password"
-			local zip_file = uri("file:"..index.dir).parent().child("backup.zip")
+			local zip_file = index.dir.parent().child("backup.zip")
 			index.zip(zip_file)
 			return zip_file
 		end
 
 		function Rpc.functions.lucene_restore(password,zip_file)
 			Io.password == password or error "wrong password"
-			local backup_zip = uri("file:"..index.dir).parent().child("backup.zip")
+			local backup_zip = index.dir.parent().child("backup.zip")
 			backup_zip.write(zip_file)
 			index.restore(backup_zip)
 		end
@@ -217,4 +219,14 @@
 	return index
 end
 
+function Lucene.recover(index_dir,restore_dir)
+	if not index_dir.exists() and restore_dir.exists() then
+		index_dir.mkdir()
+		for _, child in ipairs(restore_dir.children()) do
+			index_dir.child(child.name()).link_to(child)
+		end
+		logger.error "recovered"
+	end
+end
+
 return Lucene
--- a/src/luan/modules/lucene/LuceneIndex.java	Tue Jun 18 21:56:53 2019 -0600
+++ b/src/luan/modules/lucene/LuceneIndex.java	Wed Jun 19 00:26:10 2019 -0600
@@ -110,14 +110,14 @@
 
 	private static Map<String,LuceneIndex> indexes = new HashMap<String,LuceneIndex>();
 
-	public static Object[] getLuceneIndex(Luan luan,String indexDirStr,FieldParser defaultFieldParser,String[] defaultFields)
+	public static Object[] getLuceneIndex(Luan luan,File indexDir,FieldParser defaultFieldParser,String[] defaultFields)
 		throws LuanException, IOException
 	{
-		String key = new File(indexDirStr).getCanonicalPath();
+		String key = indexDir.getCanonicalPath();
 		synchronized(indexes) {
 			LuceneIndex li = indexes.get(key);
 			if( li == null ) {
-				li = new LuceneIndex(indexDirStr,defaultFieldParser,defaultFields,key);
+				li = new LuceneIndex(indexDir,defaultFieldParser,defaultFields,key);
 				li.openCount = 1;
 				indexes.put(key,li);
 			} else {
@@ -157,7 +157,7 @@
 	private final FieldParser defaultFieldParser;
 	private final String[] defaultFields;
 
-	private LuceneIndex(String indexDirStr,FieldParser defaultFieldParser,String[] defaultFields,String key)
+	private LuceneIndex(File indexDir,FieldParser defaultFieldParser,String[] defaultFields,String key)
 		throws LuanException, IOException
 	{
 		this.key = key;
@@ -166,7 +166,6 @@
 		mfp = defaultFieldParser==null ? new MultiFieldParser() : new MultiFieldParser(defaultFieldParser,defaultFields);
 		mfp.fields.put( "type", STRING_FIELD_PARSER );
 		mfp.fields.put( "id", NumberFieldParser.LONG );
-		File indexDir = new File(indexDirStr);
 		this.indexDir = indexDir;
 		Analyzer analyzer = STRING_FIELD_PARSER.analyzer;
 		if( defaultFieldParser instanceof StringFieldParser ) {