SegmentInfos.java
001 package gate.creole.annic.apache.lucene.index;
002 
003 /**
004  * Copyright 2004 The Apache Software Foundation
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 
019 import java.util.Vector;
020 import java.io.IOException;
021 import gate.creole.annic.apache.lucene.store.Directory;
022 import gate.creole.annic.apache.lucene.store.InputStream;
023 import gate.creole.annic.apache.lucene.store.OutputStream;
024 
025 final class SegmentInfos extends Vector {
026 
027   /** The file format version, a negative number. */
028   /* Works since counter, the old 1st entry, is always >= 0 */
029   public static final int FORMAT = -1;
030 
031   public int counter = 0;    // used to name new segments
032   private long version = 0//counts how often the index has been changed by adding or deleting docs
033 
034   public final SegmentInfo info(int i) {
035     return (SegmentInfoelementAt(i);
036   }
037 
038   public final void read(Directory directorythrows IOException {
039 
040     InputStream input = directory.openFile("segments");
041     try {
042       int format = input.readInt();
043       if(format < 0){     // file contains explicit format info
044         // check that it is a format we can understand
045         if (format < FORMAT)
046           throw new IOException("Unknown format version: " + format);
047         version = input.readLong()// read version
048         counter = input.readInt()// read counter
049       }
050       else{     // file is in old format without explicit format info
051         counter = format;
052       }
053 
054       for (int i = input.readInt(); i > 0; i--) { // read segmentInfos
055         SegmentInfo si =
056           new SegmentInfo(input.readString(), input.readInt(), directory);
057         addElement(si);
058       }
059 
060       if(format >= 0){    // in old format the version number may be at the end of the file
061         if (input.getFilePointer() >= input.length())
062           version = 0// old file format without version number
063         else
064           version = input.readLong()// read version
065       }
066     }
067     finally {
068       input.close();
069     }
070   }
071 
072   public final void write(Directory directorythrows IOException {
073     OutputStream output = directory.createFile("segments.new");
074     try {
075       output.writeInt(FORMAT)// write FORMAT
076       output.writeLong(++version)// every write changes the index
077       output.writeInt(counter)// write counter
078       output.writeInt(size())// write infos
079       for (int i = 0; i < size(); i++) {
080         SegmentInfo si = info(i);
081         output.writeString(si.name);
082         output.writeInt(si.docCount);
083       }
084     }
085     finally {
086       output.close();
087     }
088 
089     // install new segment info
090     directory.renameFile("segments.new""segments");
091   }
092 
093   /**
094    * version number when this SegmentInfos was generated.
095    */
096   public long getVersion() {
097     return version;
098   }
099 
100   /**
101    * Current version number from segments file.
102    */
103   public static long readCurrentVersion(Directory directory)
104     throws IOException {
105 
106     InputStream input = directory.openFile("segments");
107     int format = 0;
108     long version = 0;
109     try {
110       format = input.readInt();
111       if(format < 0){
112         if (format < FORMAT)
113           throw new IOException("Unknown format version: " + format);
114         version = input.readLong()// read version
115        }
116      }
117      finally {
118        input.close();
119      }
120 
121      if(format < 0)
122       return version;
123 
124     // We cannot be sure about the format of the file.
125     // Therefore we have to read the whole file and cannot simply seek to the version entry.
126 
127     SegmentInfos sis = new SegmentInfos();
128     sis.read(directory);
129     return sis.getVersion();
130   }
131 }