alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (InstantiatedTermDocs.java)

This example Lucene source code file (InstantiatedTermDocs.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

instantiatedindexreader, instantiatedterm, instantiatedterm, instantiatedtermdocs, instantiatedtermdocs, instantiatedtermdocumentinformation, instantiatedtermdocumentinformation, termdocs, termdocs

The Lucene InstantiatedTermDocs.java source code

package org.apache.lucene.store.instantiated;

/**
 * Copyright 2006 The Apache Software Foundation
 *
 * Licensed 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.
 */

import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;

/**
 * A {@link org.apache.lucene.index.TermDocs} navigating an {@link InstantiatedIndexReader}.
 */
public class InstantiatedTermDocs
    implements TermDocs {

  private final InstantiatedIndexReader reader;

  public InstantiatedTermDocs(InstantiatedIndexReader reader) {
    this.reader = reader;
  }

  private int currentDocumentIndex;
  protected InstantiatedTermDocumentInformation currentDocumentInformation;
  protected InstantiatedTerm currentTerm;


  public void seek(Term term) {
    currentTerm = reader.getIndex().findTerm(term);
    currentDocumentIndex = -1;
  }

  public void seek(org.apache.lucene.index.TermEnum termEnum) {
    seek(termEnum.term());
  }


  public int doc() {
    return currentDocumentInformation.getDocument().getDocumentNumber();
  }

  public int freq() {
    return currentDocumentInformation.getTermPositions().length;
  }


  public boolean next() {
    if (currentTerm != null) {
      currentDocumentIndex++;
      if (currentDocumentIndex < currentTerm.getAssociatedDocuments().length) {
        currentDocumentInformation = currentTerm.getAssociatedDocuments()[currentDocumentIndex];
        if (reader.isDeleted(currentDocumentInformation.getDocument().getDocumentNumber())) {
          return next();
        } else {
          return true;
        }
      } else {
        // mimic SegmentTermDocs
        currentDocumentIndex = currentTerm.getAssociatedDocuments().length -1;
      }
    }
    return false;
  }


  public int read(int[] docs, int[] freqs) {
    int i;
    for (i = 0; i < docs.length; i++) {
      if (!next()) {
        break;
      }
      docs[i] = doc();
      freqs[i] = freq();
    }
    return i;
  }

  /**
   * Skips entries to the first beyond the current whose document number is
   * greater than or equal to <i>target. 

Returns true if there is such * an entry. <p>Behaves as if written:

   *   boolean skipTo(int target) {
   *     do {
   *       if (!next())
   * 	     return false;
   *     } while (target > doc());
   *     return true;
   *   }
   * </pre>
   * This implementation is considerably more efficient than that.
   *
   */
  public boolean skipTo(int target) {
    if (currentTerm == null) {
      return false;
    }
    
    if (currentDocumentIndex >= target) {
      return next();
    }

    int startOffset = currentDocumentIndex >= 0 ? currentDocumentIndex : 0;
    int pos = currentTerm.seekCeilingDocumentInformationIndex(target, startOffset);

    if (pos == -1) {
      // mimic SegmentTermDocs that positions at the last index
      currentDocumentIndex = currentTerm.getAssociatedDocuments().length -1;
      return false;
    }

    currentDocumentInformation = currentTerm.getAssociatedDocuments()[pos];
    currentDocumentIndex = pos;
    if (reader.hasDeletions() && reader.isDeleted(currentDocumentInformation.getDocument().getDocumentNumber())) {
      return next();
    } else {
      return true;
    }
  }

  /**
   * Does nothing
   */
  public void close() {
  }
}

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene InstantiatedTermDocs.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.