lucene-core-2.9.4-dev

g.apache.lucene.index.RawPostingList
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.index;

import java.io.IOException;
import java.util.Map;
import org.apache.lucene.store.Directory;

class ReadOnlyDirectoryReader
  extends DirectoryReader
{
  ReadOnlyDirectoryReader(Directory directory, SegmentInfos sis, IndexDeletionPolicy deletionPolicy, int termInfosIndexDivisor)
    throws IOException
  {
    super(directory, sis, deletionPolicy, true, termInfosIndexDivisor);
  }
  
  ReadOnlyDirectoryReader(Directory directory, SegmentInfos infos, SegmentReader[] oldReaders, int[] oldStarts, Map oldNormsCache, boolean doClone, int termInfosIndexDivisor)
    throws IOException
  {
    super(directory, infos, oldReaders, oldStarts, oldNormsCache, true, doClone, termInfosIndexDivisor);
  }
  
  ReadOnlyDirectoryReader(IndexWriter writer, SegmentInfos infos, int termInfosIndexDivisor)
    throws IOException
  {
    super(writer, infos, termInfosIndexDivisor);
  }
  
  protected void acquireWriteLock() {}
}

/* Location:
 * Qualified Name:     org.apache.lucene.index.ReadOnlyDirectoryReader
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.index;

import org.apache.lucene.util.BitVector;

class ReadOnlySegmentReader
  extends SegmentReader
{
  static void noWrite()
  {
    throw new UnsupportedOperationException("This IndexReader cannot make any changes to the index (it was opened with readOnly = true)");
  }
  
  protected void acquireWriteLock() {}
  
  public boolean isDeleted(int n)
  {
    return (deletedDocs != null) && (deletedDocs.get(n));
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.index.ReadOnlySegmentReader
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.index;

import java.io.Reader;

final class ReusableStringReader
  extends Reader
{
  int upto;
  int left;
  String s;
  
  void init(String s)
  {
    this.s = s;
    left = s.length();
    upto = 0;
  }
  
  public int read(char[] c)
  {
    return read(c, 0, c.length);
  }
  
  public int read(char[] c, int off, int len)
  {
    if (left > len)
    {
      s.getChars(upto, upto + len, c, off);
      upto += len;
      left -= len;
      return len;
    }
    if (0 == left)
    {
      s = null;
      return -1;
    }
    s.getChars(upto, upto + left, c, off);
    int r = left;
    left = 0;
    upto = s.length();
    return r;
  }
  
  public void close() {}
}

/* Location:
 * Qualified Name:     org.apache.lucene.index.ReusableStringReader
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.index;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.BitVector;

public final class SegmentInfo
{
  static final int NO = -1;
  static final int YES = 1;
  static final int CHECK_DIR = 0;
  static final int WITHOUT_GEN = 0;
  public String name;
  public int docCount;
  public Directory dir;
  private boolean preLockless;
  private long delGen;
  private long[] normGen;
  private byte isCompoundFile;
  private boolean hasSingleNormFile;
  private List files;
  long sizeInBytes = -1L;
  private int docStoreOffset;
  private String docStoreSegment;
  private boolean docStoreIsCompoundFile;
  private int delCount;
  private boolean hasProx;
  private Map diagnostics;
  
  public String toString()
  {
    return "si: " + dir.toString() + " " + name + " docCount: " + docCount + " delCount: " + delCount + " delFileName: " + getDelFileName();
  }
  
  public SegmentInfo(String name, int docCount, Directory dir)
  {
    this.name = name;
    this.docCount = docCount;
    this.dir = dir;
    delGen = -1L;
    isCompoundFile = 0;
    preLockless = true;
    hasSingleNormFile = false;
    docStoreOffset = -1;
    docStoreSegment = name;
    docStoreIsCompoundFile = false;
    delCount = 0;
    hasProx = true;
  }
  
  public SegmentInfo(String name, int docCount, Directory dir, boolean isCompoundFile, boolean hasSingleNormFile)
  {
    this(name, docCount, dir, isCompoundFile, hasSingleNormFile, -1, null, false, true);
  }
  
  public SegmentInfo(String name, int docCount, Directory dir, boolean isCompoundFile, boolean hasSingleNormFile, int docStoreOffset, String docStoreSegment, boolean docStoreIsCompoundFile, boolean hasProx)
  {
    this(name, docCount, dir);
    this.isCompoundFile = ((byte)(isCompoundFile ? 1 : -1));
    this.hasSingleNormFile = hasSingleNormFile;
    preLockless = false;
    this.docStoreOffset = docStoreOffset;
    this.docStoreSegment = docStoreSegment;
    this.docStoreIsCompoundFile = docStoreIsCompoundFile;
    this.hasProx = hasProx;
    delCount = 0;
    assert ((docStoreOffset == -1) || (docStoreSegment != null)) : ("dso=" + docStoreOffset + " dss=" + docStoreSegment + " docCount=" + docCount);
  }
  
  void reset(SegmentInfo src)
  {
    clearFiles();
    name = name;
    docCount = docCount;
    dir = dir;
    preLockless = preLockless;
    delGen = delGen;
    docStoreOffset = docStoreOffset;
    docStoreIsCompoundFile = docStoreIsCompoundFile;
    if (normGen == null)
    {
      normGen = null;
    }
    else
    {
      normGen = new long[normGen.length];
      System.arraycopy(normGen, 0, normGen, 0, normGen.length);
    }
    isCompoundFile = isCompoundFile;
    hasSingleNormFile = hasSingleNormFile;
    delCount = delCount;
  }
  
  void setDiagnostics(Map diagnostics)
  {
    this.diagnostics = diagnostics;
  }
  
  public Map getDiagnostics()
  {
    return diagnostics;
  }
  
  SegmentInfo(Directory dir, int format, IndexInput input)
    throws IOException
  {
    this.dir = dir;
    name = input.readString();
    docCount = input.readInt();
    if (format <= -2)
    {
      delGen = input.readLong();
      if (format <= -4)
      {
        docStoreOffset = input.readInt();
        if (docStoreOffset != -1)
        {
          docStoreSegment = input.readString();
          docStoreIsCompoundFile = (1 == input.readByte());
        }
        else
        {
          docStoreSegment = name;
          docStoreIsCompoundFile = false;
        }
      }
      else
      {
        docStoreOffset = -1;
        docStoreSegment = name;
        docStoreIsCompoundFile = false;
      }
      if (format <= -3) {
        hasSingleNormFile = (1 == input.readByte());
      } else {
        hasSingleNormFile = false;
      }
      int numNormGen = input.readInt();
      if (numNormGen == -1)
      {
        normGen = null;
      }
      else
      {
        normGen = new long[numNormGen];
        for (int j = 0; j < numNormGen; j++) {
          normGen[j] = input.readLong();
        }
      }
      isCompoundFile = input.readByte();
      preLockless = (isCompoundFile == 0);
      if (format <= -6)
      {
        delCount = input.readInt();
        if ((!$assertionsDisabled) && (delCount > docCount)) {
          throw new AssertionError();
        }
      }
      else
      {
        delCount = -1;
      }
      if (format <= -7) {
        hasProx = (input.readByte() == 1);
      } else {
        hasProx = true;
      }
      if (format <= -9) {
        diagnostics = input.readStringStringMap();
      } else {
        diagnostics = Collections.EMPTY_MAP;
      }
    }
    else
    {
      delGen = 0L;
      normGen = null;
      isCompoundFile = 0;
      preLockless = true;
      hasSingleNormFile = false;
      docStoreOffset = -1;
      docStoreIsCompoundFile = false;
      docStoreSegment = null;
      delCount = -1;
      hasProx = true;
      diagnostics = Collections.EMPTY_MAP;
    }
  }
  
  void setNumFields(int numFields)
  {
    if (normGen == null)
    {
      normGen = new long[numFields];
      if (!preLockless) {
        for (int i = 0; i < numFields; i++) {
          normGen[i] = -1L;
        }
      }
    }
  }
  
  public long sizeInBytes()
    throws IOException
  {
    if (sizeInBytes == -1L)
    {
      List files = files();
      int size = files.size();
      sizeInBytes = 0L;
      for (int i = 0; i < size; i++)
      {
        String fileName = (String)files.get(i);
        if ((docStoreOffset == -1) || (!IndexFileNames.isDocStoreFile(fileName))) {
          sizeInBytes += dir.fileLength(fileName);
        }
      }
    }
    return sizeInBytes;
  }
  
  public boolean hasDeletions()
    throws IOException
  {
    if (delGen == -1L) {
      return false;
    }
    if (delGen >= 1L) {
      return true;
    }
    return dir.fileExists(getDelFileName());
  }
  
  void advanceDelGen()
  {
    if (delGen == -1L) {
      delGen = 1L;
    } else {
      delGen += 1L;
    }
    clearFiles();
  }
  
  void clearDelGen()
  {
    delGen = -1L;
    clearFiles();
  }
  
  public Object clone()
  {
    SegmentInfo si = new SegmentInfo(name, docCount, dir);
    isCompoundFile = isCompoundFile;
    delGen = delGen;
    delCount = delCount;
    hasProx = hasProx;
    preLockless = preLockless;
    hasSingleNormFile = hasSingleNormFile;
    diagnostics = new HashMap(diagnostics);
    if (normGen != null) {
      normGen = ((long[])normGen.clone());
    }
    docStoreOffset = docStoreOffset;
    docStoreSegment = docStoreSegment;
    docStoreIsCompoundFile = docStoreIsCompoundFile;
    return si;
  }
  
  public String getDelFileName()
  {
    if (delGen == -1L) {
      return null;
    }
    return IndexFileNames.fileNameFromGeneration(name, ".del", delGen);
  }
  
  public boolean hasSeparateNorms(int fieldNumber)
    throws IOException
  {
    if (((normGen == null) && (preLockless)) || ((normGen != null) && (normGen[fieldNumber] == 0L)))
    {
      String fileName = name + ".s" + fieldNumber;
      return dir.fileExists(fileName);
    }
    if ((normGen == null) || (normGen[fieldNumber] == -1L)) {
      return false;
    }
    return true;
  }
  
  public boolean hasSeparateNorms()
    throws IOException
  {
    if (normGen == null)
    {
      if (!preLockless) {
        return false;
      }
      String[] result = dir.list();
      if (result == null) {
        throw new IOException("cannot read directory " + dir + ": list() returned null");
      }
      String pattern = name + ".s";
      int patternLength = pattern.length();
      for (int i = 0; i < result.length; i++) {
        if ((result[i].startsWith(pattern)) && (Character.isDigit(result[i].charAt(patternLength)))) {
          return true;
        }
      }
      return false;
    }
    for (int i = 0; i < normGen.length; i++) {
      if (normGen[i] >= 1L) {
        return true;
      }
    }
    for (int i = 0; i < normGen.length; i++) {
      if ((normGen[i] == 0L) && 
        (hasSeparateNorms(i))) {
        return true;
      }
    }
    return false;
  }
  
  void advanceNormGen(int fieldIndex)
  {
    if (normGen[fieldIndex] == -1L) {
      normGen[fieldIndex] = 1L;
    } else {
      normGen[fieldIndex] += 1L;
    }
    clearFiles();
  }
  
  public String getNormFileName(int number)
    throws IOException
  {
    long gen;
    long gen;
    if (normGen == null) {
      gen = 0L;
    } else {
      gen = normGen[number];
    }
    if (hasSeparateNorms(number))
    {
      String prefix = ".s";
      return IndexFileNames.fileNameFromGeneration(name, prefix + number, gen);
    }
    if (hasSingleNormFile)
    {
      String prefix = ".nrm";
      return IndexFileNames.fileNameFromGeneration(name, prefix, 0L);
    }
    String prefix = ".f";
    return IndexFileNames.fileNameFromGeneration(name, prefix + number, 0L);
  }
  
  void setUseCompoundFile(boolean isCompoundFile)
  {
    if (isCompoundFile) {
      this.isCompoundFile = 1;
    } else {
      this.isCompoundFile = -1;
    }
    clearFiles();
  }
  
  public boolean getUseCompoundFile()
    throws IOException
  {
    if (isCompoundFile == -1) {
      return false;
    }
    if (isCompoundFile == 1) {
      return true;
    }
    return dir.fileExists(name + "." + "cfs");
  }
  
  public int getDelCount()
    throws IOException
  {
    if (delCount == -1) {
      if (hasDeletions())
      {
        String delFileName = getDelFileName();
        delCount = new BitVector(dir, delFileName).count();
      }
      else
      {
        delCount = 0;
      }
    }
    assert (delCount <= docCount);
    return delCount;
  }
  
  void setDelCount(int delCount)
  {
    this.delCount = delCount;
    assert (delCount <= docCount);
  }
  
  public int getDocStoreOffset()
  {
    return docStoreOffset;
  }
  
  public boolean getDocStoreIsCompoundFile()
  {
    return docStoreIsCompoundFile;
  }
  
  void setDocStoreIsCompoundFile(boolean v)
  {
    docStoreIsCompoundFile = v;
    clearFiles();
  }
  
  public String getDocStoreSegment()
  {
    return docStoreSegment;
  }
  
  void setDocStoreOffset(int offset)
  {
    docStoreOffset = offset;
    clearFiles();
  }
  
  void setDocStore(int offset, String segment, boolean isCompoundFile)
  {
    docStoreOffset = offset;
    docStoreSegment = segment;
    docStoreIsCompoundFile = isCompoundFile;
  }
  
  void write(IndexOutput output)
    throws IOException
  {
    output.writeString(name);
    output.writeInt(docCount);
    output.writeLong(delGen);
    output.writeInt(docStoreOffset);
    if (docStoreOffset != -1)
    {
      output.writeString(docStoreSegment);
      output.writeByte((byte)(docStoreIsCompoundFile ? 1 : 0));
    }
    output.writeByte((byte)(hasSingleNormFile ? 1 : 0));
    if (normGen == null)
    {
      output.writeInt(-1);
    }
    else
    {
      output.writeInt(normGen.length);
      for (int j = 0; j < normGen.length; j++) {
        output.writeLong(normGen[j]);
      }
    }
    output.writeByte(isCompoundFile);
    output.writeInt(delCount);
    output.writeByte((byte)(hasProx ? 1 : 0));
    output.writeStringStringMap(diagnostics);
  }
  
  void setHasProx(boolean hasProx)
  {
    this.hasProx = hasProx;
    clearFiles();
  }
  
  public boolean getHasProx()
  {
    return hasProx;
  }
  
  private void addIfExists(List files, String fileName)
    throws IOException
  {
    if (dir.fileExists(fileName)) {
      files.add(fileName);
    }
  }
  
  public List files()
    throws IOException
  {
    if (files != null) {
      return files;
    }
    files = new ArrayList();
    
    boolean useCompoundFile = getUseCompoundFile();
    if (useCompoundFile)
    {
      files.add(name + "." + "cfs");
    }
    else
    {
      String[] exts = IndexFileNames.NON_STORE_INDEX_EXTENSIONS;
      for (int i = 0; i < exts.length; i++) {
        addIfExists(files, name + "." + exts[i]);
      }
    }
    if (docStoreOffset != -1)
    {
      assert (docStoreSegment != null);
      if (docStoreIsCompoundFile)
      {
        files.add(docStoreSegment + "." + "cfx");
      }
      else
      {
        String[] exts = IndexFileNames.STORE_INDEX_EXTENSIONS;
        for (int i = 0; i < exts.length; i++) {
          addIfExists(files, docStoreSegment + "." + exts[i]);
        }
      }
    }
    else if (!useCompoundFile)
    {
      String[] exts = IndexFileNames.STORE_INDEX_EXTENSIONS;
      for (int i = 0; i < exts.length; i++) {
        addIfExists(files, name + "." + exts[i]);
      }
    }
    String delFileName = IndexFileNames.fileNameFromGeneration(name, ".del", delGen);
    if ((delFileName != null) && ((delGen >= 1L) || (dir.fileExists(delFileName)))) {
      files.add(delFileName);
    }
    if (normGen != null)
    {
      for (int i = 0; i < normGen.length; i++)
      {
        long gen = normGen[i];
        if (gen >= 1L)
        {
          files.add(IndexFileNames.fileNameFromGeneration(name, ".s" + i, gen));
        }
        else if (-1L == gen)
        {
          if ((!hasSingleNormFile) && (!useCompoundFile))
          {
            String fileName = name + "." + "f" + i;
            if (dir.fileExists(fileName)) {
              files.add(fileName);
            }
          }
        }
        else if (0L == gen)
        {
          String fileName = null;
          if (useCompoundFile) {
            fileName = name + "." + "s" + i;
          } else if (!hasSingleNormFile) {
            fileName = name + "." + "f" + i;
          }
          if ((fileName != null) && (dir.fileExists(fileName))) {
            files.add(fileName);
          }
        }
      }
    }
    else if ((preLockless) || ((!hasSingleNormFile) && (!useCompoundFile)))
    {
      String prefix;
      String prefix;
      if (useCompoundFile) {
        prefix = name + "." + "s";
      } else {
        prefix = name + "." + "f";
      }
      int prefixLength = prefix.length();
      String[] allFiles = dir.listAll();
      IndexFileNameFilter filter = IndexFileNameFilter.getFilter();
      for (int i = 0; i < allFiles.length; i++)
      {
        String fileName = allFiles[i];
        if ((filter.accept(null, fileName)) && (fileName.length() > prefixLength) && (Character.isDigit(fileName.charAt(prefixLength))) && (fileName.startsWith(prefix))) {
          files.add(fileName);
        }
      }
    }
    return files;
  }
  
  private void clearFiles()
  {
    files = null;
    sizeInBytes = -1L;
  }
  
  public String segString(Directory dir)
  {
    String cfs;
    try
    {
      String cfs;
      if (getUseCompoundFile()) {
        cfs = "c";
      } else {
        cfs = "C";
      }
    }
    catch (IOException ioe)
    {
      cfs = "?";
    }
    String docStore;
    String docStore;
    if (docStoreOffset != -1) {
      docStore = "->" + docStoreSegment;
    } else {
      docStore = "";
    }
    return name + ":" + cfs + (this.dir == dir ? "" : "x") + docCount + docStore;
  }
  
  public boolean equals(Object obj)
  {
    SegmentInfo other;
    try
    {
      other = (SegmentInfo)obj;
    }
    catch (ClassCastException cce)
    {
      return false;
    }
    return (dir == dir) && (name.equals(name));
  }
  
  public int hashCode()
  {
    return dir.hashCode() + name.hashCode();
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.index.SegmentInfo
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.index;

import java.io.IOException;
import org.apache.lucene.store.Directory;

class SegmentInfos$1
  extends SegmentInfos.FindSegmentsFile
{
  private final SegmentInfos this$0;
  
  SegmentInfos$1(SegmentInfos paramSegmentInfos, Directory x0)
  {
    super(x0);
  }
  
  protected Object doBody(String segmentFileName)
    throws CorruptIndexException, IOException
  {
    this$0.read(directory, segmentFileName);
    return null;
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.index.SegmentInfos.1
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.index;

import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IndexInput;

public abstract class SegmentInfos$FindSegmentsFile
{
  final Directory directory;
  
  public SegmentInfos$FindSegmentsFile(Directory directory)
  {
    this.directory = directory;
  }
  
  public Object run()
    throws CorruptIndexException, IOException
  {
    return run(null);
  }
  
  public Object run(IndexCommit commit)
    throws CorruptIndexException, IOException
  {
    if (commit != null)
    {
      if (directory != commit.getDirectory()) {
        throw new IOException("the specified commit does not match the specified Directory");
      }
      return doBody(commit.getSegmentsFileName());
    }
    String segmentFileName = null;
    long lastGen = -1L;
    long gen = 0L;
    int genLookaheadCount = 0;
    IOException exc = null;
    boolean retry = false;
    
    int method = 0;
    for (;;)
    {
      if (0 == method)
      {
        String[] files = null;
        
        long genA = -1L;
        
        files = directory.listAll();
        if (files != null) {
          genA = SegmentInfos.getCurrentSegmentGeneration(files);
        }
        SegmentInfos.access$000("directory listing genA=" + genA);
        
        long genB = -1L;
        for (int i = 0; i < SegmentInfos.access$100(); i++)
        {
          IndexInput genInput = null;
          try
          {
            genInput = directory.openInput("segments.gen");
          }
          catch (FileNotFoundException e)
          {
            SegmentInfos.access$000("segments.gen open: FileNotFoundException " + e);
            break;
          }
          catch (IOException e)
          {
            SegmentInfos.access$000("segments.gen open: IOException " + e);
          }
          if (genInput != null) {
            try
            {
              int version = genInput.readInt();
              if (version == -2)
              {
                long gen0 = genInput.readLong();
                long gen1 = genInput.readLong();
                SegmentInfos.access$000("fallback check: " + gen0 + "; " + gen1);
                if (gen0 == gen1)
                {
                  genB = gen0;
                  
                  genInput.close(); break;
                }
              }
            }
            catch (IOException err2) {}finally
            {
              genInput.close();
            }
          }
          try
          {
            Thread.sleep(SegmentInfos.access$200());
          }
          catch (InterruptedException ie)
          {
            Thread.currentThread().interrupt();
            throw new RuntimeException(ie);
          }
        }
        SegmentInfos.access$000("segments.gen check: genB=" + genB);
        if (genA > genB) {
          gen = genA;
        } else {
          gen = genB;
        }
        if (gen == -1L)
        {
          String s;
          if (files != null)
          {
            String s = "";
            for (int i = 0; i < files.length; i++) {
              s = s + " " + files[i];
            }
          }
          else
          {
            s = " null";
          }
          throw new FileNotFoundException("no segments* file found in " + directory + ": files:" + s);
        }
      }
      if ((1 == method) || ((0 == method) && (lastGen == gen) && (retry)))
      {
        method = 1;
        if (genLookaheadCount < SegmentInfos.access$300())
        {
          gen += 1L;
          genLookaheadCount++;
          SegmentInfos.access$000("look ahead increment gen to " + gen);
        }
      }
      if (lastGen == gen)
      {
        if (retry) {
          throw exc;
        }
        retry = true;
      }
      else if (0 == method)
      {
        retry = false;
      }
      lastGen = gen;
      
      segmentFileName = IndexFileNames.fileNameFromGeneration("segments", "", gen);
      try
      {
        Object v = doBody(segmentFileName);
        SegmentInfos.access$000("success on " + segmentFileName);
        return v;
      }
      catch (IOException err)
      {
        if (exc == null) {
          exc = err;
        }
        SegmentInfos.access$000("primary Exception on '" + segmentFileName + "': " + err + "'; will retry: retry=" + retry + "; gen = " + gen);
        if ((!retry) && (gen > 1L))
        {
          String prevSegmentFileName = IndexFileNames.fileNameFromGeneration("segments", "", gen - 1L);
          
          boolean prevExists = directory.fileExists(prevSegmentFileName);
          if (prevExists)
          {
            SegmentInfos.access$000("fallback to prior segment file '" + prevSegmentFileName + "'");
            try
            {
              Object v = doBody(prevSegmentFileName);
              if (exc != null) {
                SegmentInfos.access$000("success on fallback " + prevSegmentFileName);
              }
              return v;
            }
            catch (IOException err2)
            {
              SegmentInfos.access$000("secondary Exception on '" + prevSegmentFileName + "': " + err2 + "'; will retry");
            }
          }
        }
      }
    }
  }
  
  protected abstract Object doBody(String paramString)
    throws CorruptIndexException, IOException;
}

/* Location:
 * Qualified Name:     org.apache.lucene.index.SegmentInfos.FindSegmentsFile
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.index;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Vector;
import org.apache.lucene.store.ChecksumIndexInput;
import org.apache.lucene.store.ChecksumIndexOutput;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.NoSuchDirectoryException;

public final class SegmentInfos
  extends Vector
{
  public static final int FORMAT = -1;
  public static final int FORMAT_LOCKLESS = -2;
  public static final int FORMAT_SINGLE_NORM_FILE = -3;
  public static final int FORMAT_SHARED_DOC_STORE = -4;
  public static final int FORMAT_CHECKSUM = -5;
  public static final int FORMAT_DEL_COUNT = -6;
  public static final int FORMAT_HAS_PROX = -7;
  public static final int FORMAT_USER_DATA = -8;
  public static final int FORMAT_DIAGNOSTICS = -9;
  static final int CURRENT_FORMAT = -9;
  public int counter;
  private long version;
  private long generation;
  private long lastGeneration;
  private Map userData;
  private static PrintStream infoStream;
  ChecksumIndexOutput pendingSegnOutput;
  
  public SegmentInfos()
  {
    counter = 0;
    
    version = System.currentTimeMillis();
    
    generation = 0L;
    lastGeneration = 0L;
    
    userData = Collections.EMPTY_MAP;
  }
  
  public final SegmentInfo info(int i)
  {
    return (SegmentInfo)get(i);
  }
  
  public static long getCurrentSegmentGeneration(String[] files)
  {
    if (files == null) {
      return -1L;
    }
    long max = -1L;
    for (int i = 0; i < files.length; i++)
    {
      String file = files[i];
      if ((file.startsWith("segments")) && (!file.equals("segments.gen")))
      {
        long gen = generationFromSegmentsFileName(file);
        if (gen > max) {
          max = gen;
        }
      }
    }
    return max;
  }
  
  public static long getCurrentSegmentGeneration(Directory directory)
    throws IOException
  {
    try
    {
      return getCurrentSegmentGeneration(directory.listAll());
    }
    catch (NoSuchDirectoryException nsde) {}
    return -1L;
  }
  
  public static String getCurrentSegmentFileName(String[] files)
    throws IOException
  {
    return IndexFileNames.fileNameFromGeneration("segments", "", getCurrentSegmentGeneration(files));
  }
  
  public static String getCurrentSegmentFileName(Directory directory)
    throws IOException
  {
    return IndexFileNames.fileNameFromGeneration("segments", "", getCurrentSegmentGeneration(directory));
  }
  
  public String getCurrentSegmentFileName()
  {
    return IndexFileNames.fileNameFromGeneration("segments", "", lastGeneration);
  }
  
  public static long generationFromSegmentsFileName(String fileName)
  {
    if (fileName.equals("segments")) {
      return 0L;
    }
    if (fileName.startsWith("segments")) {
      return Long.parseLong(fileName.substring(1 + "segments".length()), 36);
    }
    throw new IllegalArgumentException("fileName \"" + fileName + "\" is not a segments file");
  }
  
  public String getNextSegmentFileName()
  {
    long nextGeneration;
    long nextGeneration;
    if (generation == -1L) {
      nextGeneration = 1L;
    } else {
      nextGeneration = generation + 1L;
    }
    return IndexFileNames.fileNameFromGeneration("segments", "", nextGeneration);
  }
  
  public final void read(Directory directory, String segmentFileName)
    throws CorruptIndexException, IOException
  {
    boolean success = false;
    
    clear();
    
    ChecksumIndexInput input = new ChecksumIndexInput(directory.openInput(segmentFileName));
    
    generation = generationFromSegmentsFileName(segmentFileName);
    
    lastGeneration = generation;
    try
    {
      int format = input.readInt();
      if (format < 0)
      {
        if (format < -9) {
          throw new CorruptIndexException("Unknown format version: " + format);
        }
        version = input.readLong();
        counter = input.readInt();
      }
      else
      {
        counter = format;
      }
      for (int i = input.readInt(); i > 0; i--) {
        add(new SegmentInfo(directory, format, input));
      }
      if (format >= 0) {
        if (input.getFilePointer() >= input.length()) {
          version = System.currentTimeMillis();
        } else {
          version = input.readLong();
        }
      }
      if (format <= -8)
      {
        if (format <= -9) {
          userData = input.readStringStringMap();
        } else if (0 != input.readByte()) {
          userData = Collections.singletonMap("userData", input.readString());
        } else {
          userData = Collections.EMPTY_MAP;
        }
      }
      else {
        userData = Collections.EMPTY_MAP;
      }
      if (format <= -5)
      {
        long checksumNow = input.getChecksum();
        long checksumThen = input.readLong();
        if (checksumNow != checksumThen) {
          throw new CorruptIndexException("checksum mismatch in segments file");
        }
      }
      success = true;
    }
    finally
    {
      input.close();
      if (!success) {
        clear();
      }
    }
  }
  
  public final void read(Directory directory)
    throws CorruptIndexException, IOException
  {
    generation = (lastGeneration = -1L);
    
    new FindSegmentsFile(directory)
    {
      protected Object doBody(String segmentFileName)
        throws CorruptIndexException, IOException
      {
        read(directory, segmentFileName);
        return null;
      }
    }.run();
  }
  
  private final void write(Directory directory)
    throws IOException
  {
    String segmentFileName = getNextSegmentFileName();
    if (generation == -1L) {
      generation = 1L;
    } else {
      generation += 1L;
    }
    ChecksumIndexOutput segnOutput = new ChecksumIndexOutput(directory.createOutput(segmentFileName));
    
    boolean success = false;
    try
    {
      segnOutput.writeInt(-9);
      segnOutput.writeLong(++version);
      
      segnOutput.writeInt(counter);
      segnOutput.writeInt(size());
      for (int i = 0; i < size(); i++) {
        info(i).write(segnOutput);
      }
      segnOutput.writeStringStringMap(userData);
      segnOutput.prepareCommit();
      success = true;
      pendingSegnOutput = segnOutput; return;
    }
    finally
    {
      if (!success)
      {
        try
        {
          segnOutput.close();
        }
        catch (Throwable t) {}
        try
        {
          directory.deleteFile(segmentFileName);
        }
        catch (Throwable t) {}
      }
    }
  }
  
  public Object clone()
  {
    SegmentInfos sis = (SegmentInfos)super.clone();
    for (int i = 0; i < sis.size(); i++) {
      sis.set(i, sis.info(i).clone());
    }
    userData = new HashMap(userData);
    return sis;
  }
  
  public long getVersion()
  {
    return version;
  }
  
  public long getGeneration()
  {
    return generation;
  }
  
  public long getLastGeneration()
  {
    return lastGeneration;
  }
  
  public static long readCurrentVersion(Directory directory)
    throws CorruptIndexException, IOException
  {
    SegmentInfos sis = new SegmentInfos();
    sis.read(directory);
    return version;
  }
  
  public static Map readCurrentUserData(Directory directory)
    throws CorruptIndexException, IOException
  {
    SegmentInfos sis = new SegmentInfos();
    sis.read(directory);
    return sis.getUserData();
  }
  
  public static void setInfoStream(PrintStream infoStream)
  {
    infoStream = infoStream;
  }
  
  private static int defaultGenFileRetryCount = 10;
  private static int defaultGenFileRetryPauseMsec = 50;
  private static int defaultGenLookaheadCount = 10;
  
  public static void setDefaultGenFileRetryCount(int count)
  {
    defaultGenFileRetryCount = count;
  }
  
  public static int getDefaultGenFileRetryCount()
  {
    return defaultGenFileRetryCount;
  }
  
  public static void setDefaultGenFileRetryPauseMsec(int msec)
  {
    defaultGenFileRetryPauseMsec = msec;
  }
  
  public static int getDefaultGenFileRetryPauseMsec()
  {
    return defaultGenFileRetryPauseMsec;
  }
  
  public static void setDefaultGenLookaheadCount(int count)
  {
    defaultGenLookaheadCount = count;
  }
  
  public static int getDefaultGenLookahedCount()
  {
    return defaultGenLookaheadCount;
  }
  
  public static PrintStream getInfoStream()
  {
    return infoStream;
  }
  
  private static void message(String message)
  {
    if (infoStream != null) {
      infoStream.println("SIS [" + Thread.currentThread().getName() + "]: " + message);
    }
  }
  
  public static abstract class FindSegmentsFile
  {
    final Directory directory;
    
    public FindSegmentsFile(Directory directory)
    {
      this.directory = directory;
    }
    
    public Object run()
      throws CorruptIndexException, IOException
    {
      return run(null);
    }
    
    public Object run(IndexCommit commit)
      throws CorruptIndexException, IOException
    {
      if (commit != null)
      {
        if (directory != commit.getDirectory()) {
          throw new IOException("the specified commit does not match the specified Directory");
        }
        return doBody(commit.getSegmentsFileName());
      }
      String segmentFileName = null;
      long lastGen = -1L;
      long gen = 0L;
      int genLookaheadCount = 0;
      IOException exc = null;
      boolean retry = false;
      
      int method = 0;
      for (;;)
      {
        if (0 == method)
        {
          String[] files = null;
          
          long genA = -1L;
          
          files = directory.listAll();
          if (files != null) {
            genA = SegmentInfos.getCurrentSegmentGeneration(files);
          }
          SegmentInfos.message("directory listing genA=" + genA);
          
          long genB = -1L;
          for (int i = 0; i < SegmentInfos.defaultGenFileRetryCount; i++)
          {
            IndexInput genInput = null;
            try
            {
              genInput = directory.openInput("segments.gen");
            }
            catch (FileNotFoundException e)
            {
              SegmentInfos.message("segments.gen open: FileNotFoundException " + e);
              break;
            }
            catch (IOException e)
            {
              SegmentInfos.message("segments.gen open: IOException " + e);
            }
            if (genInput != null) {
              try
              {
                int version = genInput.readInt();
                if (version == -2)
                {
                  long gen0 = genInput.readLong();
                  long gen1 = genInput.readLong();
                  SegmentInfos.message("fallback check: " + gen0 + "; " + gen1);
                  if (gen0 == gen1)
                  {
                    genB = gen0;
                    
                    genInput.close(); break;
                  }
                }
              }
              catch (IOException err2) {}finally
              {
                genInput.close();
              }
            }
            try
            {
              Thread.sleep(SegmentInfos.defaultGenFileRetryPauseMsec);
            }
            catch (InterruptedException ie)
            {
              Thread.currentThread().interrupt();
              throw new RuntimeException(ie);
            }
          }
          SegmentInfos.message("segments.gen check: genB=" + genB);
          if (genA > genB) {
            gen = genA;
          } else {
            gen = genB;
          }
          if (gen == -1L)
          {
            String s;
            if (files != null)
            {
              String s = "";
              for (int i = 0; i < files.length; i++) {
                s = s + " " + files[i];
              }
            }
            else
            {
              s = " null";
            }
            throw new FileNotFoundException("no segments* file found in " + directory + ": files:" + s);
          }
        }
        if ((1 == method) || ((0 == method) && (lastGen == gen) && (retry)))
        {
          method = 1;
          if (genLookaheadCount < SegmentInfos.defaultGenLookaheadCount)
          {
            gen += 1L;
            genLookaheadCount++;
            SegmentInfos.message("look ahead increment gen to " + gen);
          }
        }
        if (lastGen == gen)
        {
          if (retry) {
            throw exc;
          }
          retry = true;
        }
        else if (0 == method)
        {
          retry = false;
        }
        lastGen = gen;
        
        segmentFileName = IndexFileNames.fileNameFromGeneration("segments", "", gen);
        try
        {
          Object v = doBody(segmentFileName);
          SegmentInfos.message("success on " + segmentFileName);
          return v;
        }
        catch (IOException err)
        {
          if (exc == null) {
            exc = err;
          }
          SegmentInfos.message("primary Exception on '" + segmentFileName + "': " + err + "'; will retry: retry=" + retry + "; gen = " + gen);
          if ((!retry) && (gen > 1L))
          {
            String prevSegmentFileName = IndexFileNames.fileNameFromGeneration("segments", "", gen - 1L);
            
            boolean prevExists = directory.fileExists(prevSegmentFileName);
            if (prevExists)
            {
              SegmentInfos.message("fallback to prior segment file '" + prevSegmentFileName + "'");
              try
              {
                Object v = doBody(prevSegmentFileName);
                if (exc != null) {
                  SegmentInfos.message("success on fallback " + prevSegmentFileName);
                }
                return v;
              }
              catch (IOException err2)
              {
                SegmentInfos.message("secondary Exception on '" + prevSegmentFileName + "': " + err2 + "'; will retry");
              }
            }
          }
        }
      }
    }
    
    protected abstract Object doBody(String paramString)
      throws CorruptIndexException, IOException;
  }
  
  public SegmentInfos range(int first, int last)
  {
    SegmentInfos infos = new SegmentInfos();
    infos.addAll(super.subList(first, last));
    return infos;
  }
  
  void updateGeneration(SegmentInfos other)
  {
    lastGeneration = lastGeneration;
    generation = generation;
    version = version;
  }
  
  final void rollbackCommit(Directory dir)
    throws IOException
  {
    if (pendingSegnOutput != null)
    {
      try
      {
        pendingSegnOutput.close();
      }
      catch (Throwable t) {}
      try
      {
        String segmentFileName = IndexFileNames.fileNameFromGeneration("segments", "", generation);
        
        dir.deleteFile(segmentFileName);
      }
      catch (Throwable t) {}
      pendingSegnOutput = null;
    }
  }
  
  final void prepareCommit(Directory dir)
    throws IOException
  {
    if (pendingSegnOutput != null) {
      throw new IllegalStateException("prepareCommit was already called");
    }
    write(dir);
  }
  
  public Collection files(Directory dir, boolean includeSegmentsFile)
    throws IOException
  {
    HashSet files = new HashSet();
    if (includeSegmentsFile) {
      files.add(getCurrentSegmentFileName());
    }
    int size = size();
    for (int i = 0; i < size; i++)
    {
      SegmentInfo info = info(i);
      if (dir == dir) {
        files.addAll(info(i).files());
      }
    }
    return files;
  }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd