changeset 1484:1fa6e8ec2d53

lucene.logging cleanup
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 25 Apr 2020 11:14:25 -0600
parents 97740900c820
children 6a24c8b33d6b
files src/goodjava/lucene/logging/LogFile.java src/goodjava/lucene/logging/LoggingIndexWriter.java
diffstat 2 files changed, 32 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
diff -r 97740900c820 -r 1fa6e8ec2d53 src/goodjava/lucene/logging/LogFile.java
--- a/src/goodjava/lucene/logging/LogFile.java	Sat Apr 25 10:31:49 2020 -0600
+++ b/src/goodjava/lucene/logging/LogFile.java	Sat Apr 25 11:14:25 2020 -0600
@@ -29,23 +29,24 @@
 import goodjava.io.BufferedInputStream;
 
 
-public class LogFile {
+public class LogFile extends DataOutputStream {
 	private static final Logger logger = LoggerFactory.getLogger(LogFile.class);
 	public final File file;
 	private final RandomAccessFile raf;
-	private final DataOutputStream out;
 	private long end;
 
-	public LogFile(File file,String mode) throws IOException {
-		this.file = file;
-		this.raf = new RandomAccessFile(file,mode);
+	public static LogFile newLogFile(File file) throws IOException {
+		RandomAccessFile raf = new RandomAccessFile(file,"rwd");
 		OutputStream out = new FileOutputStream(raf.getFD());
 		out = new BufferedOutputStream(out);
-		this.out = new DataOutputStream(out);
-		init();
+		return new LogFile(file,raf,out);
 	}
 
-	private void init() throws IOException {
+	protected LogFile(File file,RandomAccessFile raf,OutputStream out) throws IOException {
+		super(out);
+		this.file = file;
+		this.raf = raf;
+
 		if( raf.length() == 0 ) {
 			end = 8;
 			raf.writeLong(end);
@@ -56,21 +57,6 @@
 		}
 	}
 
-	private LogFile(LogFile lf) throws IOException {
-		this.file = lf.file;
-		this.raf = new RandomAccessFile(file,"r");
-		this.out = null;
-		this.end = lf.end;
-	}
-
-	public void close() throws IOException {
-		raf.close();
-	}
-
-	public LogFile snapshot() throws IOException {
-		return new LogFile(this);
-	}
-
 	public String toString() {
 		return "LogFile<" + file.getName() + ">";
 	}
@@ -93,7 +79,7 @@
 	}
 
 	public void commit() throws IOException {
-		out.flush();
+		flush();
 		end = raf.getFilePointer();
 		raf.seek(0L);
 		raf.writeLong(end);
@@ -101,7 +87,7 @@
 	}
 
 	public void rollback() throws IOException {
-		out.flush();
+		flush();
 		raf.seek(end);
 	}
 
@@ -267,41 +253,4 @@
 		writeBytesRef( term.bytes() );
 	}
 
-
-	public void writeByte(int v) throws IOException {
-		out.writeByte(v);
-	}
-
-	public void writeInt(int v) throws IOException {
-		out.writeInt(v);
-	}
-
-	public void writeLong(long v) throws IOException {
-		out.writeLong(v);
-	}
-
-	public void writeFloat(float v) throws IOException {
-		out.writeFloat(v);
-	}
-
-	public void writeDouble(double v) throws IOException {
-		out.writeDouble(v);
-	}
-
-	public void writeBoolean(boolean v) throws IOException {
-		out.writeBoolean(v);
-	}
-
-	public void writeUTF(String s) throws IOException {
-		out.writeUTF(s);
-	}
-
-	public void write(byte[] b) throws IOException {
-		out.write(b);
-	}
-
-	public void write(byte[] b, int off, int len) throws IOException {
-		out.write(b,off,len);
-	}
-
 }
diff -r 97740900c820 -r 1fa6e8ec2d53 src/goodjava/lucene/logging/LoggingIndexWriter.java
--- a/src/goodjava/lucene/logging/LoggingIndexWriter.java	Sat Apr 25 10:31:49 2020 -0600
+++ b/src/goodjava/lucene/logging/LoggingIndexWriter.java	Sat Apr 25 11:14:25 2020 -0600
@@ -64,7 +64,7 @@
 					final int n = dis.readInt();
 					for( int i=0; i<n; i++ ) {
 						File file = new File( logDir, dis.readUTF() );
-						logs.add( new LogFile(file,"rwd") );
+						logs.add( LogFile.newLogFile(file) );
 					}
 					deleteUnusedFiles();
 					return;
@@ -119,7 +119,7 @@
 		do {
 			file = new File(logDir,"_"+rnd.nextInt(100)+".log");
 		} while( file.exists() );
-		return new LogFile(file,"rwd");
+		return LogFile.newLogFile(file);
 	}
 
 	private void deleteUnusedFiles() throws IOException {
@@ -162,8 +162,8 @@
 			throw new RuntimeException();
 		Directory dir = FSDirectory.open(dirFile);
 		LuceneIndexWriter mergeWriter = new LuceneIndexWriter( indexWriter.luceneVersion, dir, indexWriter.goodConfig );
-		playLog(first,mergeWriter);
-		playLog(second,mergeWriter);
+		playLog( first.input(), mergeWriter );
+		playLog( second.input(), mergeWriter );
 		mergeWriter.commit();
 		LogFile merge = newLogFile();
 		logLucene( lastTime, merge, mergeWriter );
@@ -208,7 +208,7 @@
 
 	public void check(SortField sortField) throws IOException {
 		IndexReader indexReader;
-		List<LogFile> logs;
+		List<LogInputStream> logReaders;
 		synchronized(this) {
 			if( isMerging ) {
 				logger.warn("is merging, check aborted");
@@ -216,10 +216,7 @@
 			}
 			isMerging = true;
 			indexReader = indexWriter.openReader();
-			logs = new ArrayList<LogFile>(this.logs);
-			int i = logs.size() - 1;
-			LogFile last = logs.get(i);
-			logs.set(i,last.snapshot());
+			logReaders = logReaders(logs);
 		}
 		try {
 			logger.info("check start");
@@ -228,7 +225,7 @@
 			IoUtils.deleteRecursively(dirFile);
 			Directory dir = FSDirectory.open(dirFile);
 			LuceneIndexWriter checkWriter = new LuceneIndexWriter( indexWriter.luceneVersion, dir, indexWriter.goodConfig );
-			playLogs(logs,checkWriter);
+			playLogs(logReaders,checkWriter);
 			logger.info("check lucene");
 			IndexReader checkReader = checkWriter.openReader();
 			if( sortField == null ) {
@@ -380,14 +377,22 @@
 	}
 
 	public synchronized void playLogs() throws IOException {
-		playLogs(logs,indexWriter);
+		playLogs( logReaders(logs), indexWriter );
 	}
 
-	private static void playLogs(List<LogFile> logs,LuceneIndexWriter indexWriter) throws IOException {
+	private static List<LogInputStream> logReaders(List<LogFile> logs) throws IOException {
+		List<LogInputStream> logReaders = new ArrayList<LogInputStream>();
+		for( LogFile log : logs ) {
+			logReaders.add( log.input() );
+		}
+		return logReaders;
+	}
+
+	private static void playLogs(List<LogInputStream> logReaders,LuceneIndexWriter indexWriter) throws IOException {
 		if( numDocs(indexWriter) != 0 )
 			throw new RuntimeException ("not empty");
-		for( LogFile log : logs ) {
-			playLog(log,indexWriter);
+		for( LogInputStream reader : logReaders ) {
+			playLog(reader,indexWriter);
 		}
 		indexWriter.commit();
 	}
@@ -399,11 +404,11 @@
 		return n;
 	}
 
-	private static void playLog(LogFile log,LuceneIndexWriter indexWriter) throws IOException {
-		LogInputStream in = log.input();
+	private static void playLog(LogInputStream in,LuceneIndexWriter indexWriter) throws IOException {
 		while( in.available() > 0 ) {
 			playOp(in,indexWriter);
 		}
+		in.close();
 	}
 
 	private static void playOp(LogInputStream in,LuceneIndexWriter indexWriter) throws IOException {