view src/goodjava/lucene/logging/LogFile.java @ 1484:1fa6e8ec2d53

lucene.logging cleanup
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 25 Apr 2020 11:14:25 -0600
parents 3cff066f3bbc
children 2469aa31f31b
line wrap: on
line source

package goodjava.lucene.logging;

import java.io.File;
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 {
	private static final Logger logger = LoggerFactory.getLogger(LogFile.class);
	public final File file;
	private final RandomAccessFile raf;
	private long end;

	public static LogFile newLogFile(File file) throws IOException {
		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);
		} else {
			raf.seek(0L);
			end = raf.readLong();
			raf.seek(end);
		}
	}

	public String toString() {
		return "LogFile<" + file.getName() + ">";
	}

	public long end() {
		return end;
	}

	public LogInputStream input() throws IOException {
		InputStream in = new FileInputStream(file);
		in = new LimitedInputStream(in,end);
		in = new BufferedInputStream(in);
		LogInputStream lis = newLogInputStream(in);
		lis.readLong();  // skip end
		return lis;
	}

	protected LogInputStream newLogInputStream(InputStream in) {
		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;
	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 ) {
			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() );
	}

}