FileDocCategorySizeDatePackage
SegmentInfos.javaAPI DocApache Lucene 1.94287Mon Feb 20 09:20:14 GMT 2006org.apache.lucene.index

SegmentInfos

public final class SegmentInfos extends Vector
Copyright 2004 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.

Fields Summary
public static final int
FORMAT
The file format version, a negative number.
public int
counter
private long
version
counts how often the index has been changed by adding or deleting docs. starting with the current time in milliseconds forces to create unique version numbers.
Constructors Summary
Methods Summary
public longgetVersion()
version number when this SegmentInfos was generated.

    return version;
  
public final org.apache.lucene.index.SegmentInfoinfo(int i)


       
    return (SegmentInfo) elementAt(i);
  
public final voidread(org.apache.lucene.store.Directory directory)

    
    IndexInput input = directory.openInput(IndexFileNames.SEGMENTS);
    try {
      int format = input.readInt();
      if(format < 0){     // file contains explicit format info
        // check that it is a format we can understand
        if (format < FORMAT)
          throw new IOException("Unknown format version: " + format);
        version = input.readLong(); // read version
        counter = input.readInt(); // read counter
      }
      else{     // file is in old format without explicit format info
        counter = format;
      }
      
      for (int i = input.readInt(); i > 0; i--) { // read segmentInfos
        SegmentInfo si =
          new SegmentInfo(input.readString(), input.readInt(), directory);
        addElement(si);
      }
      
      if(format >= 0){    // in old format the version number may be at the end of the file
        if (input.getFilePointer() >= input.length())
          version = System.currentTimeMillis(); // old file format without version number
        else
          version = input.readLong(); // read version
      }
    }
    finally {
      input.close();
    }
  
public static longreadCurrentVersion(org.apache.lucene.store.Directory directory)
Current version number from segments file.

      
    IndexInput input = directory.openInput(IndexFileNames.SEGMENTS);
    int format = 0;
    long version = 0;
    try {
      format = input.readInt();
      if(format < 0){
        if (format < FORMAT)
          throw new IOException("Unknown format version: " + format);
        version = input.readLong(); // read version
      }
    }
    finally {
      input.close();
    }
     
    if(format < 0)
      return version;

    // We cannot be sure about the format of the file.
    // Therefore we have to read the whole file and cannot simply seek to the version entry.

    SegmentInfos sis = new SegmentInfos();
    sis.read(directory);
    return sis.getVersion();
  
public final voidwrite(org.apache.lucene.store.Directory directory)

    IndexOutput output = directory.createOutput("segments.new");
    try {
      output.writeInt(FORMAT); // write FORMAT
      output.writeLong(++version); // every write changes the index
      output.writeInt(counter); // write counter
      output.writeInt(size()); // write infos
      for (int i = 0; i < size(); i++) {
        SegmentInfo si = info(i);
        output.writeString(si.name);
        output.writeInt(si.docCount);
      }         
    }
    finally {
      output.close();
    }

    // install new segment info
    directory.renameFile("segments.new", IndexFileNames.SEGMENTS);