changeset 1398:67c0e47b5be3

more lucene
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 11 Sep 2019 15:48:49 -0600
parents 0dc9837c16be
children 38a1c1b4279a
files examples/blog/src/lib/Db.luan src/luan/modules/lucene/Lucene.luan src/luan/modules/lucene/LuceneIndex.java src/luan/modules/lucene/PostgresBackup.java
diffstat 4 files changed, 24 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/examples/blog/src/lib/Db.luan	Wed Sep 11 01:31:21 2019 -0600
+++ b/examples/blog/src/lib/Db.luan	Wed Sep 11 15:48:49 2019 -0600
@@ -20,6 +20,7 @@
 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
--- a/src/luan/modules/lucene/Lucene.luan	Wed Sep 11 01:31:21 2019 -0600
+++ b/src/luan/modules/lucene/Lucene.luan	Wed Sep 11 15:48:49 2019 -0600
@@ -70,7 +70,7 @@
 	index.highlighter = java_index.highlighter
 	index.indexed_only_fields = java_index.indexed_only_fields
 	index.count_tokens = java_index.count_tokens
-	index.close = java_index.close
+	--index.close = java_index.close
 
 	index.has_postgres_backup = java_index.hasPostgresBackup()
 	function index.rebuild_postgres_backup()
@@ -186,7 +186,7 @@
 			local lucene_dir = index.dir
 			local before_restore = lucene_dir.parent().child("before_restore.zip")
 			index.zip(before_restore)
-			java_index.doClose()
+			java_index.close()
 			lucene_dir.delete()
 			Io.uri("os:unzip "..zip_file.canonical().to_string(),{dir=lucene_dir.parent()}).read_text()
 			java_index.reopen()
--- a/src/luan/modules/lucene/LuceneIndex.java	Wed Sep 11 01:31:21 2019 -0600
+++ b/src/luan/modules/lucene/LuceneIndex.java	Wed Sep 11 15:48:49 2019 -0600
@@ -85,7 +85,7 @@
 import luan.lib.logging.LoggerFactory;
 
 
-public final class LuceneIndex implements Closeable {
+public final class LuceneIndex {
 	private static final Logger sysLogger = LoggerFactory.getLogger(LuceneIndex.class);
 
 	private static Map<String,Reference<LuceneIndex>> indexes = new HashMap<String,Reference<LuceneIndex>>();
@@ -98,8 +98,12 @@
 			Reference<LuceneIndex> ref = indexes.get(key);
 			if( ref != null ) {
 				LuceneIndex li = ref.get();
-				if( li != null )
+				if( li != null ) {
+					Object version = options.get("version");
+					if( version==null || version.equals(li.version) )
+						return li;
 					li.closeWriter();
+				}
 			}
 			LuceneIndex li = new LuceneIndex(luan,indexDir,options);
 			indexes.put(key, new WeakReference<LuceneIndex>(li));
@@ -107,12 +111,13 @@
 		}
 	}
 
-	private static final Version version = Version.LUCENE_4_9;
+	private static final Version luceneVersion = Version.LUCENE_4_9;
 	private static final String FLD_NEXT_ID = "nextId";
 	public static final StringFieldParser STRING_FIELD_PARSER = new StringFieldParser(new KeywordAnalyzer());
-	public static final StringFieldParser ENGLISH_FIELD_PARSER = new StringFieldParser(new EnglishAnalyzer(version));
+	public static final StringFieldParser ENGLISH_FIELD_PARSER = new StringFieldParser(new EnglishAnalyzer(luceneVersion));
 
 	private final Logger luanLogger;
+	private final Object version;
 
 	private final ReentrantLock writeLock = new ReentrantLock();
 	private final File indexDir;
@@ -129,9 +134,6 @@
 	private AtomicInteger writeCounter = new AtomicInteger();
 
 	private Set<String> indexOnly = new HashSet<String>();
-
-	private boolean isClosed = false;
-	private final Exception created = new Exception("created");
 	private final FieldParser defaultFieldParser;
 	private final String[] defaultFields;
 
@@ -142,6 +144,7 @@
 		throws LuanException, IOException, ClassNotFoundException, SQLException
 	{
 		Map map = options.asMap();
+		this.version = map.remove("version");
 		FieldParser defaultFieldParser = (FieldParser)map.remove("default_type");
 		LuanTable defaultFieldsTbl = Utils.removeTable(map,"default_fields");
 		String[] defaultFields = defaultFieldsTbl==null ? null : (String[])defaultFieldsTbl.asList().toArray(new String[0]);
@@ -180,19 +183,10 @@
 */
 			}
 		}
-		luan.onClose(this);
-	}
-
-	protected void finalize() throws Throwable {
-		if( !isClosed ) {
-			sysLogger.error("not closed",created);
-			close();
-		}
-		super.finalize();
 	}
 
 	public boolean reopen() throws IOException {
-		IndexWriterConfig conf = new IndexWriterConfig(version,analyzer);
+		IndexWriterConfig conf = new IndexWriterConfig(luceneVersion,analyzer);
 		snapshotDeletionPolicy = new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy());
 		conf.setIndexDeletionPolicy(snapshotDeletionPolicy);
 		fsDir = FSDirectory.open(indexDir);
@@ -434,26 +428,22 @@
 		return writer.getDirectory().toString();
 	}
 
-	public synchronized void close() throws IOException {
-		try {
-			doClose();
-		} catch(SQLException e) {
-			throw new RuntimeException(e);
-		}
-		isClosed = true;
+	protected void finalize() throws Throwable {
+		close();
+		super.finalize();
 	}
 
-	public void doClose() throws IOException, SQLException {
-		writer.close();
+	public void close() throws IOException, SQLException {
+		closeWriter();
 		reader.close();
-		if( postgresBackup != null )
-			postgresBackup.close();
 	}
 
-	private void closeWriter() throws IOException {
+	private void closeWriter() throws IOException, SQLException {
 		writeLock.lock();
 		try {
 			writer.close();
+			if( postgresBackup != null )
+				postgresBackup.close();
 		} finally {
 			writeLock.unlock();
 		}
@@ -860,7 +850,6 @@
 		if( postgresBackup!=null && wasCreated && !postgresBackup.wasCreated ) {
 			luanLogger.error("restoring from postgres");
 			force_restore_from_postgres();
-			wasCreated = false;
 		}
 	}
 
@@ -882,6 +871,7 @@
 			saveNextId(nextId);
 			ok = true;
 			writer.commit();
+			wasCreated = false;
 		} finally {
 			if( !ok ) {
 				writer.rollback();
--- a/src/luan/modules/lucene/PostgresBackup.java	Wed Sep 11 01:31:21 2019 -0600
+++ b/src/luan/modules/lucene/PostgresBackup.java	Wed Sep 11 15:48:49 2019 -0600
@@ -98,11 +98,8 @@
 	}
 
 	protected void finalize() throws Throwable {
+		close();
 		super.finalize();
-		if( !con.isClosed() ) {
-			sysLogger.error("con not closed");
-			con.close();
-		}
 	}
 
 	void add(LuanTable doc) throws LuanException, SQLException {