home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (FormatPostingsPositionsWriter.java)

This example Lucene source code file (FormatPostingsPositionsWriter.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

closeable, formatpostingsdocswriter, formatpostingsdocswriter, formatpostingspositionsconsumer, formatpostingspositionswriter, formatpostingspositionswriter, indexoutput, io, ioexception, ioexception, override, override

The Lucene FormatPostingsPositionsWriter.java source code

package org.apache.lucene.index;

 * 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,
 * See the License for the specific language governing permissions and
 * limitations under the License.

import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.IOUtils;

import java.io.Closeable;
import java.io.IOException;

final class FormatPostingsPositionsWriter extends FormatPostingsPositionsConsumer implements Closeable {

  final FormatPostingsDocsWriter parent;
  final IndexOutput out;

  boolean omitTermFreqAndPositions;
  boolean storePayloads;
  int lastPayloadLength = -1;

  FormatPostingsPositionsWriter(SegmentWriteState state, FormatPostingsDocsWriter parent) throws IOException {
    this.parent = parent;
    omitTermFreqAndPositions = parent.omitTermFreqAndPositions;
    if (parent.parent.parent.fieldInfos.hasProx()) {
      // At least one field does not omit TF, so create the
      // prox file
      out = parent.parent.parent.dir.createOutput(IndexFileNames.segmentFileName(parent.parent.parent.segment, IndexFileNames.PROX_EXTENSION));
    } else
      // Every field omits TF so we will write no prox file
      out = null;

  int lastPosition;

  /** Add a new position & payload */
  void addPosition(int position, byte[] payload, int payloadOffset, int payloadLength) throws IOException {
    assert !omitTermFreqAndPositions: "omitTermFreqAndPositions is true";
    assert out != null;

    final int delta = position - lastPosition;
    lastPosition = position;

    if (storePayloads) {
      if (payloadLength != lastPayloadLength) {
        lastPayloadLength = payloadLength;
      } else
        out.writeVInt(delta << 1);
      if (payloadLength > 0)
        out.writeBytes(payload, payloadLength);
    } else

  void setField(FieldInfo fieldInfo) {
    omitTermFreqAndPositions = fieldInfo.omitTermFreqAndPositions;
    storePayloads = omitTermFreqAndPositions ? false : fieldInfo.storePayloads;

  /** Called when we are done adding positions & payloads */
  void finish() {       
    lastPosition = 0;
    lastPayloadLength = -1;

  public void close() throws IOException {
    IOUtils.closeSafely(false, out);

Other Lucene examples (source code examples)

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

my book on functional programming


new blog posts


Copyright 1998-2019 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.