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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to