diff lucene/src/luan/modules/lucene/Lucene.luan @ 754:1a101ac9ea46

add lucene restore
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 17 Jul 2016 19:21:52 -0600
parents 5e3970ccd86a
children 9092e52f94eb
line wrap: on
line diff
--- a/lucene/src/luan/modules/lucene/Lucene.luan	Fri Jul 15 17:35:50 2016 -0600
+++ b/lucene/src/luan/modules/lucene/Lucene.luan	Sun Jul 17 19:21:52 2016 -0600
@@ -9,6 +9,8 @@
 local String = require "luan:String.luan"
 local matches = String.matches or error()
 local Rpc = require "luan:Rpc.luan"
+local Thread = require "luan:Thread.luan"
+local synchronized = Thread.synchronized or error()
 local LuceneIndex = require "java:luan.modules.lucene.LuceneIndex"
 local NumberFieldParser = require "java:luan.modules.lucene.queryparser.NumberFieldParser"
 local StringFieldParser = require "java:luan.modules.lucene.queryparser.StringFieldParser"
@@ -113,7 +115,23 @@
 		end )
 	end
 
+	function index.restore(zip_file)
+		local lucene_dir = uri("file:"..index.dir)
+		local before_restore = lucene_dir.parent().child("before_restore.zip")
+		index.zip(before_restore)
+		java_index.close()
+		lucene_dir.delete()
+		zip_file.unzip(lucene_dir.parent().to_string())
+		java_index.reopen()
+	end
+	index.restore = synchronized(index.restore)
+
+	local function multi_error()
+		error "multiple lucene instances"
+	end
+
 	if Rpc.functions.backup == nil then
+
 		function Rpc.functions.lucene_backup(password)
 			if Io.password ~= password then
 				error "wrong password"
@@ -122,10 +140,17 @@
 			index.zip(zip_file)
 			return zip_file
 		end
+
+		function Rpc.functions.lucene_restore(password,zip_file)
+			if Io.password ~= password then
+				error "wrong password"
+			end
+			index.restore(zip_file)
+		end
+
 	else
-		function Rpc.functions.lucene_backup()
-			error "multiple lucene instance"
-		end
+		Rpc.functions.lucene_backup = multi_error
+		Rpc.functions.lucene_restore = multi_error
 	end
 
 	return index