lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gol...@apache.org
Subject cvs commit: jakarta-lucene/src/java/org/apache/lucene/index SegmentInfos.java
Date Thu, 08 Apr 2004 09:13:20 GMT
goller      2004/04/08 02:13:20

  Modified:    src/java/org/apache/lucene/index SegmentInfos.java
  Log:
  file format introduced in order to optimize
  readCurrentVersion()
  
  Revision  Changes    Path
  1.5       +48 -9     jakarta-lucene/src/java/org/apache/lucene/index/SegmentInfos.java
  
  Index: SegmentInfos.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/index/SegmentInfos.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- SegmentInfos.java	29 Mar 2004 22:48:02 -0000	1.4
  +++ SegmentInfos.java	8 Apr 2004 09:13:19 -0000	1.5
  @@ -23,6 +23,11 @@
   import org.apache.lucene.store.OutputStream;
   
   final class SegmentInfos extends Vector {
  +  
  +  /** The file format version, a negative number. */
  +  /* Works since counter, the old 1st entry, is always >= 0 */
  +  public static final int FORMAT = -1;
  +  
     public int counter = 0;    // used to name new segments
     private long version = 0; //counts how often the index has been changed by adding or
deleting docs
   
  @@ -31,18 +36,33 @@
     }
   
     public final void read(Directory directory) throws IOException {
  +    
       InputStream input = directory.openFile("segments");
       try {
  -      counter = input.readInt(); // read counter
  +      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 (input.getFilePointer() >= input.length())
  -        version = 0; // old file format without version number
  -      else
  -        version = input.readLong(); // read version
  +      
  +      if(format >= 0){    // in old format the version number may be at the end of the
file
  +        if (input.getFilePointer() >= input.length())
  +          version = 0; // old file format without version number
  +        else
  +          version = input.readLong(); // read version
  +      }
       }
       finally {
         input.close();
  @@ -52,14 +72,15 @@
     public final void write(Directory directory) throws IOException {
       OutputStream output = directory.createFile("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);
  -      }
  -      output.writeLong(++version); // every write changes the index         
  +      }         
       }
       finally {
         output.close();
  @@ -81,9 +102,27 @@
      */
     public static long readCurrentVersion(Directory directory)
       throws IOException {
  +      
  +    InputStream input = directory.openFile("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 whether the segments file is in the old format or the new one.
  -    // Therefore we have to read the whole file and cannot simple seek to the version entry.
  +    // 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);
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-dev-help@jakarta.apache.org


Mime
View raw message