mirror of https://github.com/apache/lucene.git
file format introduced in order to optimize
readCurrentVersion() git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150283 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
77e80269d3
commit
281004951b
|
@ -23,6 +23,11 @@ import org.apache.lucene.store.InputStream;
|
||||||
import org.apache.lucene.store.OutputStream;
|
import org.apache.lucene.store.OutputStream;
|
||||||
|
|
||||||
final class SegmentInfos extends Vector {
|
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
|
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
|
private long version = 0; //counts how often the index has been changed by adding or deleting docs
|
||||||
|
|
||||||
|
@ -31,18 +36,33 @@ final class SegmentInfos extends Vector {
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void read(Directory directory) throws IOException {
|
public final void read(Directory directory) throws IOException {
|
||||||
|
|
||||||
InputStream input = directory.openFile("segments");
|
InputStream input = directory.openFile("segments");
|
||||||
try {
|
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
|
for (int i = input.readInt(); i > 0; i--) { // read segmentInfos
|
||||||
SegmentInfo si =
|
SegmentInfo si =
|
||||||
new SegmentInfo(input.readString(), input.readInt(), directory);
|
new SegmentInfo(input.readString(), input.readInt(), directory);
|
||||||
addElement(si);
|
addElement(si);
|
||||||
}
|
}
|
||||||
if (input.getFilePointer() >= input.length())
|
|
||||||
version = 0; // old file format without version number
|
if(format >= 0){ // in old format the version number may be at the end of the file
|
||||||
else
|
if (input.getFilePointer() >= input.length())
|
||||||
version = input.readLong(); // read version
|
version = 0; // old file format without version number
|
||||||
|
else
|
||||||
|
version = input.readLong(); // read version
|
||||||
|
}
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
input.close();
|
input.close();
|
||||||
|
@ -52,6 +72,8 @@ final class SegmentInfos extends Vector {
|
||||||
public final void write(Directory directory) throws IOException {
|
public final void write(Directory directory) throws IOException {
|
||||||
OutputStream output = directory.createFile("segments.new");
|
OutputStream output = directory.createFile("segments.new");
|
||||||
try {
|
try {
|
||||||
|
output.writeInt(FORMAT); // write FORMAT
|
||||||
|
output.writeLong(++version); // every write changes the index
|
||||||
output.writeInt(counter); // write counter
|
output.writeInt(counter); // write counter
|
||||||
output.writeInt(size()); // write infos
|
output.writeInt(size()); // write infos
|
||||||
for (int i = 0; i < size(); i++) {
|
for (int i = 0; i < size(); i++) {
|
||||||
|
@ -59,7 +81,6 @@ final class SegmentInfos extends Vector {
|
||||||
output.writeString(si.name);
|
output.writeString(si.name);
|
||||||
output.writeInt(si.docCount);
|
output.writeInt(si.docCount);
|
||||||
}
|
}
|
||||||
output.writeLong(++version); // every write changes the index
|
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
output.close();
|
output.close();
|
||||||
|
@ -82,8 +103,26 @@ final class SegmentInfos extends Vector {
|
||||||
public static long readCurrentVersion(Directory directory)
|
public static long readCurrentVersion(Directory directory)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
// We cannot be sure whether the segments file is in the old format or the new one.
|
InputStream input = directory.openFile("segments");
|
||||||
// Therefore we have to read the whole file and cannot simple seek to the version entry.
|
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();
|
SegmentInfos sis = new SegmentInfos();
|
||||||
sis.read(directory);
|
sis.read(directory);
|
||||||
|
|
Loading…
Reference in New Issue