FileDocCategorySizeDatePackage
SegmentTermPositions.javaAPI DocApache Lucene 2.2.06222Sat Jun 16 22:20:36 BST 2007org.apache.lucene.index

SegmentTermPositions

public final class SegmentTermPositions extends SegmentTermDocs implements TermPositions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Fields Summary
private IndexInput
proxStream
private int
proxCount
private int
position
private int
payloadLength
private boolean
needToLoadPayload
private long
lazySkipPointer
private int
lazySkipProxCount
Constructors Summary
SegmentTermPositions(SegmentReader p)

  
    
    super(p);
    this.proxStream = null;  // the proxStream will be cloned lazily when nextPosition() is called for the first time
  
Methods Summary
public final voidclose()

    super.close();
    if (proxStream != null) proxStream.close();
  
public byte[]getPayload(byte[] data, int offset)

    if (!needToLoadPayload) {
      throw new IOException("Payload cannot be loaded more than once for the same term position.");
    }

    // read payloads lazily
    byte[] retArray;
    int retOffset;
    if (data == null || data.length - offset < payloadLength) {
      // the array is too small to store the payload data,
      // so we allocate a new one
      retArray = new byte[payloadLength];
      retOffset = 0;
    } else {
      retArray = data;
      retOffset = offset;
    }
    proxStream.readBytes(retArray, retOffset, payloadLength);
    needToLoadPayload = false;
    return retArray;
  
public intgetPayloadLength()

    return payloadLength;
  
public booleanisPayloadAvailable()

    return needToLoadPayload && payloadLength > 0;
  
private voidlazySkip()

    if (proxStream == null) {
      // clone lazily
      proxStream = (IndexInput)parent.proxStream.clone();
    }
    
    // we might have to skip the current payload
    // if it was not read yet
    skipPayload();
      
    if (lazySkipPointer != 0) {
      proxStream.seek(lazySkipPointer);
      lazySkipPointer = 0;
    }
     
    if (lazySkipProxCount != 0) {
      skipPositions(lazySkipProxCount);
      lazySkipProxCount = 0;
    }
  
public final booleannext()

    // we remember to skip the remaining positions of the current
    // document lazily
    lazySkipProxCount += proxCount;
    
    if (super.next()) {               // run super
      proxCount = freq;               // note frequency
      position = 0;               // reset position
      return true;
    }
    return false;
  
public final intnextPosition()

    // perform lazy skips if neccessary
    lazySkip();
    proxCount--;
    return position += readDeltaPosition();
  
public final intread(int[] docs, int[] freqs)

    throw new UnsupportedOperationException("TermPositions does not support processing multiple documents in one call. Use TermDocs instead.");
  
private final intreadDeltaPosition()

    int delta = proxStream.readVInt();
    if (currentFieldStoresPayloads) {
      // if the current field stores payloads then
      // the position delta is shifted one bit to the left.
      // if the LSB is set, then we have to read the current
      // payload length
      if ((delta & 1) != 0) {
        payloadLength = proxStream.readVInt();
      } 
      delta >>>= 1;
      needToLoadPayload = true;
    } else {
      payloadLength = 0;
      needToLoadPayload = false;
    }
    return delta;
  
final voidseek(org.apache.lucene.index.TermInfo ti, org.apache.lucene.index.Term term)

    super.seek(ti, term);
    if (ti != null)
      lazySkipPointer = ti.proxPointer;
    
    lazySkipProxCount = 0;
    proxCount = 0;
    payloadLength = 0;
    needToLoadPayload = false;
  
private voidskipPayload()

    if (needToLoadPayload && payloadLength > 0) {
      proxStream.seek(proxStream.getFilePointer() + payloadLength);
    }
    needToLoadPayload = false;
  
private voidskipPositions(int n)

    for (int f = n; f > 0; f--) {        // skip unread positions
      readDeltaPosition();
      skipPayload();
    }      
  
protected voidskipProx(long proxPointer, int payloadLength)
Called by super.skipTo().

    // we save the pointer, we might have to skip there lazily
    lazySkipPointer = proxPointer;
    lazySkipProxCount = 0;
    proxCount = 0;
    this.payloadLength = payloadLength;
    needToLoadPayload = false;
  
protected final voidskippingDoc()

    // we remember to skip a document lazily
    lazySkipProxCount += freq;