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

LogOutputStream
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 01 May 2020 16:09:35 -0600
parents 1fa6e8ec2d53
children 9a2a2181a58f
line wrap: on
line diff
--- a/src/goodjava/lucene/logging/LogFile.java	Fri May 01 11:23:29 2020 -0600
+++ b/src/goodjava/lucene/logging/LogFile.java	Fri May 01 16:09:35 2020 -0600
@@ -4,49 +4,24 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.BufferedOutputStream;
-import java.io.DataOutputStream;
 import java.io.RandomAccessFile;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.util.List;
-import java.util.Map;
-import org.apache.lucene.index.Term;
-import org.apache.lucene.search.Query;
-import org.apache.lucene.search.MatchAllDocsQuery;
-import org.apache.lucene.search.TermQuery;
-import org.apache.lucene.search.PrefixQuery;
-import org.apache.lucene.search.WildcardQuery;
-import org.apache.lucene.search.TermRangeQuery;
-import org.apache.lucene.search.PhraseQuery;
-import org.apache.lucene.search.NumericRangeQuery;
-import org.apache.lucene.search.BooleanQuery;
-import org.apache.lucene.search.BooleanClause;
-import org.apache.lucene.util.BytesRef;
 import goodjava.logging.Logger;
 import goodjava.logging.LoggerFactory;
 import goodjava.io.LimitedInputStream;
 import goodjava.io.BufferedInputStream;
 
 
-public class LogFile extends DataOutputStream {
+public class LogFile {
 	private static final Logger logger = LoggerFactory.getLogger(LogFile.class);
 	public final File file;
-	private final RandomAccessFile raf;
-	private long end;
+	long end;
 
-	public static LogFile newLogFile(File file) throws IOException {
+	public LogFile(File file) throws IOException {
+		this.file = file;
 		RandomAccessFile raf = new RandomAccessFile(file,"rwd");
-		OutputStream out = new FileOutputStream(raf.getFD());
-		out = new BufferedOutputStream(out);
-		return new LogFile(file,raf,out);
-	}
-
-	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);
@@ -55,6 +30,7 @@
 			end = raf.readLong();
 			raf.seek(end);
 		}
+		raf.close();
 	}
 
 	public String toString() {
@@ -65,6 +41,17 @@
 		return end;
 	}
 
+	public LogOutputStream output() throws IOException {
+		RandomAccessFile raf = new RandomAccessFile(file,"rwd");
+		OutputStream out = new FileOutputStream(raf.getFD());
+		out = new BufferedOutputStream(out);
+		return newLogOutputStream(raf,out);
+	}
+
+	protected LogOutputStream newLogOutputStream(RandomAccessFile raf,OutputStream out) throws IOException {
+		return new LogOutputStream(this,raf,out);
+	}
+
 	public LogInputStream input() throws IOException {
 		InputStream in = new FileInputStream(file);
 		in = new LimitedInputStream(in,end);
@@ -78,19 +65,6 @@
 		return new LogInputStream(in);
 	}
 
-	public void commit() throws IOException {
-		flush();
-		end = raf.getFilePointer();
-		raf.seek(0L);
-		raf.writeLong(end);
-		raf.seek(end);
-	}
-
-	public void rollback() throws IOException {
-		flush();
-		raf.seek(end);
-	}
-
 	static final int TYPE_NULL = 0;
 	static final int TYPE_STRING = 1;
 	static final int TYPE_INT = 2;
@@ -107,150 +81,4 @@
 	static final int TYPE_QUERY_PHRASE = 13;
 	static final int TYPE_QUERY_NUMERIC_RANGE = 14;
 	static final int TYPE_QUERY_BOOLEAN = 15;
-
-	public void writeObject(Object obj) throws IOException {
-		if( obj==null ) {
-			writeByte(TYPE_NULL);
-			return;
-		}
-		if( obj instanceof String ) {
-			writeByte(TYPE_STRING);
-			writeUTF((String)obj);
-			return;
-		}
-		if( obj instanceof Integer ) {
-			writeByte(TYPE_INT);
-			writeInt((Integer)obj);
-			return;
-		}
-		if( obj instanceof Long ) {
-			writeByte(TYPE_LONG);
-			writeLong((Long)obj);
-			return;
-		}
-		if( obj instanceof Float ) {
-			writeByte(TYPE_FLOAT);
-			writeFloat((Float)obj);
-			return;
-		}
-		if( obj instanceof Double ) {
-			writeByte(TYPE_DOUBLE);
-			writeDouble((Double)obj);
-			return;
-		}
-		if( obj instanceof byte[] ) {
-			writeByte(TYPE_BYTES);
-			writeByteArray((byte[])obj);
-			return;
-		}
-		if( obj instanceof List ) {
-			writeByte(TYPE_LIST);
-			writeList((List)obj);
-			return;
-		}
-		if( obj instanceof MatchAllDocsQuery ) {
-			writeByte(TYPE_QUERY_MATCH_ALL_DOCS);
-			return;
-		}
-		if( obj instanceof TermQuery ) {
-			writeByte(TYPE_QUERY_TERM);
-			TermQuery query = (TermQuery)obj;
-			writeTerm( query.getTerm() );
-			return;
-		}
-		if( obj instanceof PrefixQuery ) {
-			writeByte(TYPE_QUERY_PREFIX);
-			PrefixQuery query = (PrefixQuery)obj;
-			writeTerm( query.getPrefix() );
-			return;
-		}
-		if( obj instanceof WildcardQuery ) {
-			writeByte(TYPE_QUERY_TERM_RANGE);
-			WildcardQuery query = (WildcardQuery)obj;
-			writeTerm( query.getTerm() );
-			return;
-		}
-		if( obj instanceof TermRangeQuery ) {
-			writeByte(TYPE_QUERY_TERM_RANGE);
-			TermRangeQuery query = (TermRangeQuery)obj;
-			writeUTF( query.getField() );
-			writeBytesRef( query.getLowerTerm() );
-			writeBytesRef( query.getUpperTerm() );
-			writeBoolean( query.includesLower() );
-			writeBoolean( query.includesUpper() );
-			return;
-		}
-		if( obj instanceof PhraseQuery ) {
-			writeByte(TYPE_QUERY_PHRASE);
-			PhraseQuery query = (PhraseQuery)obj;
-			Term[] terms = query.getTerms();
-			int[] positions = query.getPositions();
-			if( terms.length != positions.length )
-				throw new RuntimeException();
-			writeInt( terms.length );
-			for( int i=0; i<terms.length; i++ ) {
-				writeTerm( terms[i] );
-				writeInt( positions[i] );
-			}
-			return;
-		}
-		if( obj instanceof NumericRangeQuery ) {
-			writeByte(TYPE_QUERY_NUMERIC_RANGE);
-			NumericRangeQuery query = (NumericRangeQuery)obj;
-			writeUTF( query.getField() );
-			writeObject( query.getMin() );
-			writeObject( query.getMax() );
-			writeBoolean( query.includesMin() );
-			writeBoolean( query.includesMax() );
-			return;
-		}
-		if( obj instanceof BooleanQuery ) {
-			writeByte(TYPE_QUERY_BOOLEAN);
-			BooleanQuery query = (BooleanQuery)obj;
-			BooleanClause[] a = query.getClauses();
-			writeInt(a.length);
-			for( BooleanClause bc : a ) {
-				writeQuery( bc.getQuery() );
-				writeUTF( bc.getOccur().name() );
-			}
-			return;
-		}
-		throw new IllegalArgumentException("invalid type for "+obj);
-	}
-
-	public void writeByteArray(byte[] bytes) throws IOException {
-		writeInt(bytes.length);
-		write(bytes);
-	}
-
-	public void writeList(List list) throws IOException {
-		writeInt(list.size());
-		for( Object obj : list ) {
-			writeObject(obj);
-		}
-	}
-
-	public void writeMap(Map map) throws IOException {
-		writeInt(map.size());
-		for( Object obj : map.entrySet() ) {
-			Map.Entry entry = (Map.Entry)obj;
-			writeObject( entry.getKey() );
-			writeObject( entry.getValue() );
-		}
-	}
-
-	public void writeQuery(Query query) throws IOException {
-		writeObject(query);
-	}
-
-	public void writeBytesRef(BytesRef br) throws IOException {
-		writeInt(br.length);
-		write(br.bytes,0,br.length);
-	}
-
-	public void writeTerm(Term term) throws IOException {
-		writeUTF(term.field());
-		writeBytesRef( term.bytes() );
-	}
-
 }