diff src/goodjava/lucene/logging/LogFile.java @ 1476:7d145095cc0b

lucene.logging check
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 19 Apr 2020 20:42:26 -0600
parents 5e3870618377
children 1f41e5921090
line wrap: on
line diff
--- a/src/goodjava/lucene/logging/LogFile.java	Sat Apr 18 11:02:18 2020 -0600
+++ b/src/goodjava/lucene/logging/LogFile.java	Sun Apr 19 20:42:26 2020 -0600
@@ -1,12 +1,15 @@
 package goodjava.lucene.logging;
 
 import java.io.File;
+import java.io.InputStream;
+import java.io.DataOutput;
+import java.io.DataOutputStream;
 import java.io.RandomAccessFile;
+import java.io.ByteArrayOutputStream;
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.util.List;
-import java.util.ArrayList;
 import java.util.Map;
-import java.util.LinkedHashMap;
 import org.apache.lucene.index.Term;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.MatchAllDocsQuery;
@@ -23,67 +26,97 @@
 import goodjava.logging.LoggerFactory;
 
 
-public class LogFile extends RandomAccessFile {
+public class LogFile {
 	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 long end;
 
 	public LogFile(File file,String mode) throws IOException {
-		super(file,mode);
 		this.file = file;
+		this.raf = new RandomAccessFile(file,mode);
+		this.out = new DataOutputStream(baos);
 		init();
 	}
 
 	private void init() throws IOException {
-		if( length() == 0 ) {
+		if( raf.length() == 0 ) {
 			end = 8;
-			writeLong(end);
+			raf.writeLong(end);
 		} else {
-			seek(0L);
-			end = readLong();
-			gotoEnd();
+			raf.seek(0L);
+			end = raf.readLong();
+			raf.seek(end);
 		}
 	}
 
+	private LogFile(LogFile lf) throws IOException {
+		this.file = lf.file;
+		this.raf = new RandomAccessFile(file,"r");
+		this.out = null;
+		this.end = lf.end;
+	}
+
+	public LogFile snapshot() throws IOException {
+		return new LogFile(this);
+	}
+
 	public String toString() {
 		return "LogFile<" + file.getName() + ">";
 	}
 
-	public void gotoStart() throws IOException {
-		seek(8L);
+	public long end() {
+		return end;
 	}
 
-	public void gotoEnd() throws IOException {
-		seek(end);
+	public LogInputStream input() throws IOException {
+		byte[] a = new byte[(int)end - 8];
+		raf.seek(8L);
+		raf.readFully(a);
+		return newLogInputStream(new ByteArrayInputStream(a));
+	}
+
+	protected LogInputStream newLogInputStream(InputStream in) {
+		return new LogInputStream(in);
 	}
 
 	public void commit() throws IOException {
-		end = getFilePointer();
-		seek(0L);
-		writeLong(end);
-		gotoEnd();
-	}
-
-	public boolean hasMore() throws IOException {
-		return getFilePointer() < end;
+		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);
+		}
+		end = raf.getFilePointer();
+		raf.seek(0L);
+		raf.writeLong(end);
 	}
 
-	private static final int TYPE_NULL = 0;
-	private static final int TYPE_STRING = 1;
-	private static final int TYPE_INT = 2;
-	private static final int TYPE_LONG = 3;
-	private static final int TYPE_FLOAT = 4;
-	private static final int TYPE_DOUBLE = 5;
-	private static final int TYPE_BYTES = 6;
-	private static final int TYPE_LIST = 7;
-	private static final int TYPE_QUERY_MATCH_ALL_DOCS = 8;
-	private static final int TYPE_QUERY_TERM = 9;
-	private static final int TYPE_QUERY_PREFIX = 10;
-	private static final int TYPE_QUERY_WILDCARD = 11;
-	private static final int TYPE_QUERY_TERM_RANGE = 12;
-	private static final int TYPE_QUERY_PHRASE = 13;
-	private static final int TYPE_QUERY_NUMERIC_RANGE = 14;
-	private static final int TYPE_QUERY_BOOLEAN = 15;
+	public void rollback() throws IOException {
+		baos.reset();
+	}
+
+	static final int TYPE_NULL = 0;
+	static final int TYPE_STRING = 1;
+	static final int TYPE_INT = 2;
+	static final int TYPE_LONG = 3;
+	static final int TYPE_FLOAT = 4;
+	static final int TYPE_DOUBLE = 5;
+	static final int TYPE_BYTES = 6;
+	static final int TYPE_LIST = 7;
+	static final int TYPE_QUERY_MATCH_ALL_DOCS = 8;
+	static final int TYPE_QUERY_TERM = 9;
+	static final int TYPE_QUERY_PREFIX = 10;
+	static final int TYPE_QUERY_WILDCARD = 11;
+	static final int TYPE_QUERY_TERM_RANGE = 12;
+	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 ) {
@@ -195,103 +228,11 @@
 		throw new IllegalArgumentException("invalid type for "+obj);
 	}
 
-	public Object readObject() throws IOException {
-		int type = readByte();
-		return readObject(type);
-	}
-
-	protected Object readObject(int type) throws IOException {
-		switch(type) {
-		case TYPE_NULL:
-			return null;
-		case TYPE_STRING:
-			return readUTF();
-		case TYPE_INT:
-			return readInt();
-		case TYPE_LONG:
-			return readLong();
-		case TYPE_FLOAT:
-			return readFloat();
-		case TYPE_DOUBLE:
-			return readDouble();
-		case TYPE_BYTES:
-			return readByteArray();
-		case TYPE_LIST:
-			return readList();
-		case TYPE_QUERY_MATCH_ALL_DOCS:
-			return new MatchAllDocsQuery();
-		case TYPE_QUERY_TERM:
-			return new TermQuery( readTerm() );
-		case TYPE_QUERY_PREFIX:
-			return new PrefixQuery( readTerm() );
-		case TYPE_QUERY_WILDCARD:
-			return new WildcardQuery( readTerm() );
-		case TYPE_QUERY_TERM_RANGE:
-			{
-				String field = readUTF();
-				BytesRef lowerTerm = readBytesRef();
-				BytesRef upperTerm = readBytesRef();
-				boolean includeLower = readBoolean();
-				boolean includeUpper = readBoolean();
-				return new TermRangeQuery(field,lowerTerm,upperTerm,includeLower,includeUpper);
-			}
-		case TYPE_QUERY_PHRASE:
-			{
-				PhraseQuery query = new PhraseQuery();
-				int n = readInt();
-				for( int i=0; i<n; i++ ) {
-					Term term = readTerm();
-					int position = readInt();
-					query.add(term,position);
-				}
-				return query;
-			}
-		case TYPE_QUERY_NUMERIC_RANGE:
-			{
-				String field = readUTF();
-				Number min = (Number)readObject();
-				Number max = (Number)readObject();
-				boolean minInclusive = readBoolean();
-				boolean maxInclusive = readBoolean();
-				Number n = min!=null ? min : max;
-				if( n instanceof Integer )
-					return NumericRangeQuery.newIntRange(field,(Integer)min,(Integer)max,minInclusive,maxInclusive);
-				if( n instanceof Long )
-					return NumericRangeQuery.newLongRange(field,(Long)min,(Long)max,minInclusive,maxInclusive);
-				if( n instanceof Float )
-					return NumericRangeQuery.newFloatRange(field,(Float)min,(Float)max,minInclusive,maxInclusive);
-				if( n instanceof Double )
-					return NumericRangeQuery.newDoubleRange(field,(Double)min,(Double)max,minInclusive,maxInclusive);
-				throw new RuntimeException("bad numeric type for "+n);
-			}
-		case TYPE_QUERY_BOOLEAN:
-			{
-				BooleanQuery query = new BooleanQuery();
-				int n = readInt();
-				for( int i=0; i<n; i++ ) {
-					Query subquery = readQuery();
-					BooleanClause.Occur occur = BooleanClause.Occur.valueOf( readUTF() );
-					query.add(subquery,occur);
-				}
-				return query;
-			}
-		default:
-			throw new RuntimeException("invalid type "+type);
-		}
-	}
-
 	public void writeByteArray(byte[] bytes) throws IOException {
 		writeInt(bytes.length);
 		write(bytes);
 	}
 
-	public byte[] readByteArray() throws IOException {
-		int len = readInt();
-		byte[] bytes = new byte[len];
-		readFully(bytes);
-		return bytes;
-	}
-
 	public void writeList(List list) throws IOException {
 		writeInt(list.size());
 		for( Object obj : list ) {
@@ -299,15 +240,6 @@
 		}
 	}
 
-	public List readList() throws IOException {
-		final int size = readInt();
-		List list = new ArrayList(size);
-		for( int i=0; i<size; i++ ) {
-			list.add( readObject() );
-		}
-		return list;
-	}
-
 	public void writeMap(Map map) throws IOException {
 		writeInt(map.size());
 		for( Object obj : map.entrySet() ) {
@@ -317,43 +249,55 @@
 		}
 	}
 
-	public Map readMap() throws IOException {
-		final int size = readInt();
-		Map map = new LinkedHashMap();
-		for( int i=0; i<size; i++ ) {
-			Object key = readObject();
-			Object value = readObject();
-			map.put(key,value);
-		}
-		return map;
-	}
-
 	public void writeQuery(Query query) throws IOException {
 		writeObject(query);
 	}
 
-	public Query readQuery() throws IOException {
-		return (Query)readObject();
-	}
-
 	public void writeBytesRef(BytesRef br) throws IOException {
 		writeInt(br.length);
 		write(br.bytes,0,br.length);
 	}
 
-	public BytesRef readBytesRef() throws IOException {
-		return new BytesRef( readByteArray() );
-	}
-
 	public void writeTerm(Term term) throws IOException {
 		writeUTF(term.field());
 		writeBytesRef( term.bytes() );
 	}
 
-	public Term readTerm() throws IOException {
-		String key = readUTF();
-		BytesRef value = readBytesRef();
-		return new Term(key,value);
+
+	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);
 	}
 
 }