KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > SegmentInfos


1 package org.apache.lucene.index;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.Vector JavaDoc;
20 import java.io.IOException JavaDoc;
21 import org.apache.lucene.store.Directory;
22 import org.apache.lucene.store.IndexInput;
23 import org.apache.lucene.store.IndexOutput;
24 import org.apache.lucene.util.Constants;
25
26 final class SegmentInfos extends Vector JavaDoc {
27   
28   /** The file format version, a negative number. */
29   /* Works since counter, the old 1st entry, is always >= 0 */
30   public static final int FORMAT = -1;
31   
32   public int counter = 0; // used to name new segments
33
/**
34    * counts how often the index has been changed by adding or deleting docs.
35    * starting with the current time in milliseconds forces to create unique version numbers.
36    */

37   private long version = System.currentTimeMillis();
38
39   public final SegmentInfo info(int i) {
40     return (SegmentInfo) elementAt(i);
41   }
42
43   public final void read(Directory directory) throws IOException JavaDoc {
44     
45     IndexInput input = directory.openInput(IndexFileNames.SEGMENTS);
46     try {
47       int format = input.readInt();
48       if(format < 0){ // file contains explicit format info
49
// check that it is a format we can understand
50
if (format < FORMAT)
51           throw new IOException JavaDoc("Unknown format version: " + format);
52         version = input.readLong(); // read version
53
counter = input.readInt(); // read counter
54
}
55       else{ // file is in old format without explicit format info
56
counter = format;
57       }
58       
59       for (int i = input.readInt(); i > 0; i--) { // read segmentInfos
60
SegmentInfo si =
61           new SegmentInfo(input.readString(), input.readInt(), directory);
62         addElement(si);
63       }
64       
65       if(format >= 0){ // in old format the version number may be at the end of the file
66
if (input.getFilePointer() >= input.length())
67           version = System.currentTimeMillis(); // old file format without version number
68
else
69           version = input.readLong(); // read version
70
}
71     }
72     finally {
73       input.close();
74     }
75   }
76
77   public final void write(Directory directory) throws IOException JavaDoc {
78     IndexOutput output = directory.createOutput("segments.new");
79     try {
80       output.writeInt(FORMAT); // write FORMAT
81
output.writeLong(++version); // every write changes the index
82
output.writeInt(counter); // write counter
83
output.writeInt(size()); // write infos
84
for (int i = 0; i < size(); i++) {
85         SegmentInfo si = info(i);
86         output.writeString(si.name);
87         output.writeInt(si.docCount);
88       }
89     }
90     finally {
91       output.close();
92     }
93
94     // install new segment info
95
directory.renameFile("segments.new", IndexFileNames.SEGMENTS);
96   }
97
98   /**
99    * version number when this SegmentInfos was generated.
100    */

101   public long getVersion() {
102     return version;
103   }
104
105   /**
106    * Current version number from segments file.
107    */

108   public static long readCurrentVersion(Directory directory)
109     throws IOException JavaDoc {
110       
111     IndexInput input = directory.openInput(IndexFileNames.SEGMENTS);
112     int format = 0;
113     long version = 0;
114     try {
115       format = input.readInt();
116       if(format < 0){
117         if (format < FORMAT)
118           throw new IOException JavaDoc("Unknown format version: " + format);
119         version = input.readLong(); // read version
120
}
121     }
122     finally {
123       input.close();
124     }
125      
126     if(format < 0)
127       return version;
128
129     // We cannot be sure about the format of the file.
130
// Therefore we have to read the whole file and cannot simply seek to the version entry.
131

132     SegmentInfos sis = new SegmentInfos();
133     sis.read(directory);
134     return sis.getVersion();
135   }
136 }
137
Popular Tags