lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Busch <busch...@gmail.com>
Subject Re: Payload Loading and Reloading
Date Fri, 30 Nov 2007 00:30:04 GMT
Michael Busch wrote:
> once. For convenience, user could also create a very simple
> Termpositions decorator that caches the most recently loaded payload and
> allows calling getPayload() more than once.

Something like this should do the trick (I stole resizeBuffer() from
Token). It's untested code:

package org.apache.lucene.index;

import java.io.IOException;

public class PayloadBufferingTermPositions implements TermPositions {
	private final static int MIN_BUFFER_SIZE = 1;
	
	private TermPositions in;
	private byte[] buffer;
	private boolean bufferValid;
	private int bufferLength;
	
	public PayloadBufferingTermPositions(TermPositions in) {
		buffer = new byte[MIN_BUFFER_SIZE];
		bufferValid = false;
		this.in = in;
	}	
	
	public byte[] getPayload(byte[] data, int offset) throws IOException {
		if (in.isPayloadAvailable()) {
			bufferLength = in.getPayloadLength();
			resizeBuffer(bufferLength);
			in.getPayload(buffer, 0);
			bufferValid = true;
		}
		
		if (!bufferValid) {
			throw new IOException("No payload stored with this position.");
		}
		
		byte[] retArray;
		int retOffset;
    if (data == null || data.length - offset < bufferLength) {
      // the array is too small to store the payload data,
      // so we allocate a new one
      retArray = new byte[bufferLength];
      retOffset = 0;
    } else {
      retArray = data;
      retOffset = offset;
    }

    System.arraycopy(buffer, 0, retArray, retOffset, bufferLength);
    return retArray;
	}

	public int getPayloadLength() {
		return in.getPayloadLength();
	}

	public boolean isPayloadAvailable() {
		return in.isPayloadAvailable() || bufferValid;
	}

	public int nextPosition() throws IOException {
		bufferValid = false;
		return in.nextPosition();
	}

	public void close() throws IOException {
		in.close();		
	}

	public int doc() {
		return in.doc();
	}

	public int freq() {
		return in.freq();
	}

	public boolean next() throws IOException {
		bufferValid = false;
		return in.next();
	}

	public int read(int[] docs, int[] freqs) throws IOException {
		bufferValid = false;
		return in.read(docs, freqs);
	}

	public void seek(Term term) throws IOException {
		bufferValid = false;
		in.seek(term);
	}

	public void seek(TermEnum termEnum) throws IOException {
		bufferValid = false;
		in.seek(termEnum);
	}

	public boolean skipTo(int target) throws IOException {
		bufferValid = false;
		return in.skipTo(target);
	}
	
  private void resizeBuffer(int newSize) {
    if (newSize > buffer.length) {
      int size = buffer.length;
      while(size < newSize)
        size *= 2;
      byte[] newBuffer = new byte[size];
      System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
      buffer = newBuffer;
    }
  }


}


---------------------------------------------------------------------
To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-dev-help@lucene.apache.org


Mime
View raw message