view src/goodjava/lucene/logging/LogFile.java @ 1478:37e582f2e266

clone BufferedInputStream
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 24 Apr 2020 10:45:53 -0600
parents 7d145095cc0b
children 1f41e5921090
line wrap: on
line source

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.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;


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 {
		this.file = file;
		this.raf = new RandomAccessFile(file,mode);
		this.out = new DataOutputStream(baos);
		init();
	}

	private void init() throws IOException {
		if( raf.length() == 0 ) {
			end = 8;
			raf.writeLong(end);
		} else {
			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 long end() {
		return 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 {
		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);
	}

	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 ) {
			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() );
	}


	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);
	}

}