changeset 1764:527c53b91a50

lucene error handling
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 22 May 2023 20:43:52 -0600
parents 164c6ea53147
children 1ffe1e06ea55
files src/luan/modules/lucene/LuceneIndex.java
diffstat 1 files changed, 14 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/lucene/LuceneIndex.java	Mon May 22 19:21:14 2023 -0600
+++ b/src/luan/modules/lucene/LuceneIndex.java	Mon May 22 20:43:52 2023 -0600
@@ -1,56 +1,31 @@
 package luan.modules.lucene;
 
-import java.io.Closeable;
 import java.io.File;
-import java.io.FileOutputStream;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.lang.ref.Reference;
 import java.lang.ref.WeakReference;
-import java.util.Arrays;
-import java.util.Iterator;
 import java.util.Map;
 import java.util.HashMap;
-import java.util.List;
 import java.util.ArrayList;
-import java.util.Set;
-import java.util.HashSet;
-import java.util.Collections;
 import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
-import java.util.zip.ZipOutputStream;
-import java.util.zip.ZipEntry;
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.analysis.TokenStream;
 import org.apache.lucene.analysis.core.KeywordAnalyzer;
 import org.apache.lucene.analysis.core.SimpleAnalyzer;
 import org.apache.lucene.analysis.en.EnglishAnalyzer;
 import org.apache.lucene.document.Document;
-import org.apache.lucene.document.Field;
-import org.apache.lucene.document.StoredField;
-import org.apache.lucene.document.StringField;
-import org.apache.lucene.document.TextField;
-import org.apache.lucene.document.IntField;
-import org.apache.lucene.document.LongField;
-import org.apache.lucene.document.DoubleField;
-import org.apache.lucene.index.IndexableField;
-import org.apache.lucene.index.IndexWriter;
-import org.apache.lucene.index.IndexWriterConfig;
 import org.apache.lucene.index.DirectoryReader;
 import org.apache.lucene.index.Term;
 import org.apache.lucene.index.SnapshotDeletionPolicy;
 import org.apache.lucene.index.IndexCommit;
 import org.apache.lucene.index.AtomicReaderContext;
 import org.apache.lucene.index.CheckIndex;
-import org.apache.lucene.store.Directory;
 import org.apache.lucene.store.FSDirectory;
 import org.apache.lucene.util.Version;
 import org.apache.lucene.util.BytesRef;
 import org.apache.lucene.util.NumericUtils;
 import org.apache.lucene.search.Query;
-import org.apache.lucene.search.PrefixQuery;
-import org.apache.lucene.search.TermQuery;
 import org.apache.lucene.search.MatchAllDocsQuery;
 import org.apache.lucene.search.TopDocs;
 import org.apache.lucene.search.Sort;
@@ -64,7 +39,6 @@
 import org.apache.lucene.search.highlight.Formatter;
 import org.apache.lucene.search.highlight.Highlighter;
 import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
-import org.apache.lucene.search.highlight.Fragmenter;
 import org.apache.lucene.search.highlight.NullFragmenter;
 import org.apache.lucene.search.highlight.SimpleSpanFragmenter;
 import org.apache.lucene.search.highlight.QueryScorer;
@@ -89,7 +63,6 @@
 import luan.LuanFunction;
 import luan.LuanException;
 import luan.LuanRuntimeException;
-import luan.modules.parsers.LuanToString;
 import goodjava.logging.Logger;
 import goodjava.logging.LoggerFactory;
 
@@ -190,17 +163,22 @@
 		fsDir = FSDirectory.open(indexDir);
 		boolean wasCreated = !fsDir.getDirectory().exists();
 		writer = new LuceneIndexWriter(fsDir,config);
-		if( logDir != null ) {
-			if( BackupIndexWriter.backupDomains == null ) {
-				writer = new LoggingIndexWriter((LuceneIndexWriter)writer,logDir,logTime);
-			} else {
-				writer = BackupIndexWriter.newWithRestore((LuceneIndexWriter)writer,logDir,logTime,domain,name);
+		try {
+			if( logDir != null ) {
+				if( BackupIndexWriter.backupDomains == null ) {
+					writer = new LoggingIndexWriter((LuceneIndexWriter)writer,logDir,logTime);
+				} else {
+					writer = BackupIndexWriter.newWithRestore((LuceneIndexWriter)writer,logDir,logTime,domain,name);
+				}
 			}
+			reader = DirectoryReader.open(fsDir);
+			searcher = new IndexSearcher(reader);
+			initId();
+			return wasCreated;
+		} catch(IOException e) {
+			writer.close();
+			throw e;
 		}
-		reader = DirectoryReader.open(fsDir);
-		searcher = new IndexSearcher(reader);
-		initId();
-		return wasCreated;
 	}
 
 	private void wrote() {
@@ -358,26 +336,6 @@
 		}
 	}
 
-/*
-	public void backup(String zipFile) throws LuanException, IOException {
-		if( !zipFile.endsWith(".zip") )
-			throw new LuanException("file "+zipFile+" doesn't end with '.zip'");
-		IndexCommit ic = snapshotDeletionPolicy.snapshot();
-		try {
-			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
-			for( String fileName : ic.getFileNames() ) {
-				out.putNextEntry(new ZipEntry(fileName));
-				FileInputStream in = new FileInputStream(new File(indexDir,fileName));
-				Utils.copyAll(in,out);
-				in.close();
-				out.closeEntry();
-			}
-			out.close();
-		} finally {
-			snapshotDeletionPolicy.release(ic);
-		}
-	}
-*/
 	public SnapshotDeletionPolicy snapshotDeletionPolicy() {
 		return (SnapshotDeletionPolicy)writer.getLuceneIndexWriter().getConfig().getIndexDeletionPolicy();
 	}