changeset 756:9092e52f94eb

better synchronization for lucene restore
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 18 Jul 2016 05:14:19 -0600
parents ff865e954934
children e1dfeddfbc7b
files lucene/src/luan/modules/lucene/Lucene.luan lucene/src/luan/modules/lucene/LuceneIndex.java
diffstat 2 files changed, 23 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/lucene/src/luan/modules/lucene/Lucene.luan	Mon Jul 18 05:01:38 2016 -0600
+++ b/lucene/src/luan/modules/lucene/Lucene.luan	Mon Jul 18 05:14:19 2016 -0600
@@ -9,8 +9,6 @@
 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"
@@ -116,15 +114,16 @@
 	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()
+		java_index.run_in_lock( function()
+			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 )
 	end
-	index.restore = synchronized(index.restore)
 
 	local function multi_error()
 		error "multiple lucene instances"
--- a/lucene/src/luan/modules/lucene/LuceneIndex.java	Mon Jul 18 05:01:38 2016 -0600
+++ b/lucene/src/luan/modules/lucene/LuceneIndex.java	Mon Jul 18 05:14:19 2016 -0600
@@ -237,6 +237,20 @@
 		}
 	}
 
+	public void run_in_lock(LuanState luan,LuanFunction fn) throws IOException, LuanException {
+		if( writeLock.isHeldByCurrentThread() )
+			throw new RuntimeException();
+		writeLock.lock();
+		try {
+			synchronized(this) {
+				fn.call(luan);
+			}
+		} finally {
+			wrote();
+			writeLock.unlock();
+		}
+	}
+
 
 	private long id;
 	private long idLim;