diff src/goodjava/lucene/logging/LogFile.java @ 1481:3cff066f3bbc

output buffering
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 24 Apr 2020 15:57:30 -0600
parents 1f41e5921090
children 1fa6e8ec2d53
line wrap: on
line diff
--- a/src/goodjava/lucene/logging/LogFile.java	Fri Apr 24 14:32:20 2020 -0600
+++ b/src/goodjava/lucene/logging/LogFile.java	Fri Apr 24 15:57:30 2020 -0600
@@ -2,11 +2,12 @@
 
 import java.io.File;
 import java.io.InputStream;
-import java.io.DataOutput;
+import java.io.OutputStream;
+import java.io.BufferedOutputStream;
 import java.io.DataOutputStream;
 import java.io.RandomAccessFile;
-import java.io.ByteArrayOutputStream;
 import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.List;
 import java.util.Map;
@@ -32,14 +33,15 @@
 	private static final Logger logger = LoggerFactory.getLogger(LogFile.class);
 	public final File file;
 	private final RandomAccessFile raf;
-	private ByteArrayOutputStream baos = new ByteArrayOutputStream();
-	private DataOutput out;
+	private final DataOutputStream out;
 	private long end;
 
 	public LogFile(File file,String mode) throws IOException {
 		this.file = file;
 		this.raf = new RandomAccessFile(file,mode);
-		this.out = new DataOutputStream(baos);
+		OutputStream out = new FileOutputStream(raf.getFD());
+		out = new BufferedOutputStream(out);
+		this.out = new DataOutputStream(out);
 		init();
 	}
 
@@ -61,6 +63,10 @@
 		this.end = lf.end;
 	}
 
+	public void close() throws IOException {
+		raf.close();
+	}
+
 	public LogFile snapshot() throws IOException {
 		return new LogFile(this);
 	}
@@ -87,22 +93,16 @@
 	}
 
 	public void commit() throws IOException {
-		raf.seek(end);
-		raf.write(baos.toByteArray());
-		//logger.info("size "+baos.size());
-		if( baos.size() < 10000 ) {
-			baos.reset();
-		} else {
-			baos = new ByteArrayOutputStream();
-			out = new DataOutputStream(baos);
-		}
+		out.flush();
 		end = raf.getFilePointer();
 		raf.seek(0L);
 		raf.writeLong(end);
+		raf.seek(end);
 	}
 
 	public void rollback() throws IOException {
-		baos.reset();
+		out.flush();
+		raf.seek(end);
 	}
 
 	static final int TYPE_NULL = 0;