diff src/goodjava/lucene/logging/LoggingIndexWriter.java @ 1486:2469aa31f31b

LogOutputStream
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 01 May 2020 16:09:35 -0600
parents 1fa6e8ec2d53
children 044a360c2300
line wrap: on
line diff
--- a/src/goodjava/lucene/logging/LoggingIndexWriter.java	Fri May 01 11:23:29 2020 -0600
+++ b/src/goodjava/lucene/logging/LoggingIndexWriter.java	Fri May 01 16:09:35 2020 -0600
@@ -47,6 +47,7 @@
 	public final LuceneIndexWriter indexWriter;
 	private final File logDir;
 	private final List<LogFile> logs = new ArrayList<LogFile>();
+	private LogOutputStream log;
 	private final File index;
 	private boolean isMerging = false;
 
@@ -64,9 +65,10 @@
 					final int n = dis.readInt();
 					for( int i=0; i<n; i++ ) {
 						File file = new File( logDir, dis.readUTF() );
-						logs.add( LogFile.newLogFile(file) );
+						logs.add( new LogFile(file) );
 					}
 					deleteUnusedFiles();
+					setLog();
 					return;
 				}
 			} finally {
@@ -76,6 +78,12 @@
 		newLogs();
 	}
 
+	private void setLog() throws IOException {
+		if( log != null )
+			log.close();
+		log = logs.get(logs.size()-1).output();
+	}
+
 	public synchronized boolean isMerging() {
 		return isMerging;
 	}
@@ -94,10 +102,12 @@
 		}
 		logLucene( System.currentTimeMillis(), logs.get(0), indexWriter );
 		writeIndex();
+		setLog();
 		logger.info("done building new logs");
 	}
 
-	private static void logLucene(long time,LogFile log,LuceneIndexWriter indexWriter) throws IOException {
+	private static void logLucene(long time,LogFile logLucene,LuceneIndexWriter indexWriter) throws IOException {
+		LogOutputStream log = logLucene.output();
 		IndexReader reader = indexWriter.openReader();
 		final IndexSearcher searcher = new IndexSearcher(reader);
 		Query query = new MatchAllDocsQuery();
@@ -112,6 +122,7 @@
 		});
 		reader.close();
 		log.commit();
+		log.close();
 	}
 
 	private LogFile newLogFile() throws IOException {
@@ -119,7 +130,7 @@
 		do {
 			file = new File(logDir,"_"+rnd.nextInt(100)+".log");
 		} while( file.exists() );
-		return LogFile.newLogFile(file);
+		return new LogFile(file);
 	}
 
 	private void deleteUnusedFiles() throws IOException {
@@ -306,25 +317,21 @@
 		return col.total;
 	}
 
-	private LogFile log() {
-		return logs.get(logs.size()-1);
-	}
-
 	public synchronized void close() throws IOException {
 		indexWriter.close();
-		LogFile log = log();
 		log.commit();
+		log.close();
 	}
 
 	public synchronized void commit() throws IOException {
 		indexWriter.commit();
-		LogFile log = log();
 		log.commit();
 		if( isMerging )
 			return;
-		if( log.end() > logs.get(0).end() ) {
+		if( log.logFile.end() > logs.get(0).end() ) {
 			logs.add( newLogFile() );
 			writeIndex();
+			setLog();
 		}
 		if( logs.size() > 3 ) {
 			isMerging = true;
@@ -335,34 +342,29 @@
 
 	public synchronized void rollback() throws IOException {
 		indexWriter.rollback();
-		LogFile log = log();
 		log.rollback();
 	}
 
 	public synchronized void deleteAll() throws IOException {
 		indexWriter.deleteAll();
-		LogFile log = log();
-		writeOp(log,OP_DELETE_ALL);
+		writeOp(OP_DELETE_ALL);
 	}
 
 	public synchronized void deleteDocuments(Query query) throws IOException {
 		indexWriter.deleteDocuments(query);
-		LogFile log = log();
-		writeOp(log,OP_DELETE_DOCUMENTS);
+		writeOp(OP_DELETE_DOCUMENTS);
 		log.writeQuery(query);
 	}
 
 	public synchronized void addDocument(Map<String,Object> storedFields) throws IOException {
 		indexWriter.addDocument(storedFields);
-		LogFile log = log();
-		writeOp(log,OP_ADD_DOCUMENT);
+		writeOp(OP_ADD_DOCUMENT);
 		log.writeMap(storedFields);
 	}
 
 	public synchronized void updateDocument(String keyFieldName,Map<String,Object> storedFields) throws IOException {
 		indexWriter.updateDocument(keyFieldName,storedFields);
-		LogFile log = log();
-		writeOp(log,OP_UPDATE_DOCUMENT);
+		writeOp(OP_UPDATE_DOCUMENT);
 		log.writeUTF(keyFieldName);
 		log.writeMap(storedFields);
 	}
@@ -371,7 +373,7 @@
 		indexWriter.reindexDocuments(keyFieldName,query);
 	}
 
-	private void writeOp(LogFile log,int op) throws IOException {
+	private void writeOp(int op) throws IOException {
 		log.writeLong(System.currentTimeMillis());
 		log.writeByte(op);
 	}