Fields Summary |
---|
public static final int | FORMATThe file format version, a negative number. |
public static final int | FORMAT_LOCKLESSThis format adds details used for lockless commits. It differs
slightly from the previous format in that file names
are never re-used (write once). Instead, each file is
written to the next generation. For example,
segments_1, segments_2, etc. This allows us to not use
a commit lock. See file
formats for details. |
public static final int | FORMAT_SINGLE_NORM_FILEThis is the current file format written. It adds a
"hasSingleNormFile" flag into each segment info.
See LUCENE-756
for details. |
public int | counter |
private long | versioncounts 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. |
private long | generation |
private long | lastGeneration |
private static PrintStream | infoStreamIf non-null, information about loading segments_N files
will be printed here. @see #setInfoStream. |
private static int | defaultGenFileRetryCount |
private static int | defaultGenFileRetryPauseMsec |
private static int | defaultGenLookaheadCount |
Methods Summary |
---|
public java.lang.Object | clone()Returns a copy of this instance, also copying each
SegmentInfo.
SegmentInfos sis = (SegmentInfos) super.clone();
for(int i=0;i<sis.size();i++) {
sis.setElementAt(((SegmentInfo) sis.elementAt(i)).clone(), i);
}
return sis;
|
public static java.lang.String | getCurrentSegmentFileName(java.lang.String[] files)Get the filename of the current segments_N file
from a list of files.
return IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS,
"",
getCurrentSegmentGeneration(files));
|
public static java.lang.String | getCurrentSegmentFileName(org.apache.lucene.store.Directory directory)Get the filename of the current segments_N file
in the directory.
return IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS,
"",
getCurrentSegmentGeneration(directory));
|
public java.lang.String | getCurrentSegmentFileName()Get the segments_N filename in use by this segment infos.
return IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS,
"",
lastGeneration);
|
public static long | getCurrentSegmentGeneration(java.lang.String[] files)Get the generation (N) of the current segments_N file
from a list of files.
if (files == null) {
return -1;
}
long max = -1;
int prefixLen = IndexFileNames.SEGMENTS.length()+1;
for (int i = 0; i < files.length; i++) {
String file = files[i];
if (file.startsWith(IndexFileNames.SEGMENTS) && !file.equals(IndexFileNames.SEGMENTS_GEN)) {
if (file.equals(IndexFileNames.SEGMENTS)) {
// Pre lock-less commits:
if (max == -1) {
max = 0;
}
} else {
long v = Long.parseLong(file.substring(prefixLen), Character.MAX_RADIX);
if (v > max) {
max = v;
}
}
}
}
return max;
|
public static long | getCurrentSegmentGeneration(org.apache.lucene.store.Directory directory)Get the generation (N) of the current segments_N file
in the directory.
String[] files = directory.list();
if (files == null)
throw new IOException("Cannot read directory " + directory);
return getCurrentSegmentGeneration(files);
|
public static int | getDefaultGenFileRetryCount()
return defaultGenFileRetryCount;
|
public static int | getDefaultGenFileRetryPauseMsec()
return defaultGenFileRetryPauseMsec;
|
public static int | getDefaultGenLookahedCount()
return defaultGenLookaheadCount;
|
public static java.io.PrintStream | getInfoStream()
return infoStream;
|
public java.lang.String | getNextSegmentFileName()Get the next segments_N filename that will be written.
long nextGeneration;
if (generation == -1) {
nextGeneration = 1;
} else {
nextGeneration = generation+1;
}
return IndexFileNames.fileNameFromGeneration(IndexFileNames.SEGMENTS,
"",
nextGeneration);
|
public long | getVersion()version number when this SegmentInfos was generated.
return version;
|
public final org.apache.lucene.index.SegmentInfo | info(int i)
return (SegmentInfo) elementAt(i);
|
private static void | message(java.lang.String message)
if (infoStream != null) {
infoStream.println(Thread.currentThread().getName() + ": " + message);
}
|
public final void | read(org.apache.lucene.store.Directory directory, java.lang.String segmentFileName)Read a particular segmentFileName. Note that this may
throw an IOException if a commit is in process.
boolean success = false;
IndexInput input = directory.openInput(segmentFileName);
if (segmentFileName.equals(IndexFileNames.SEGMENTS)) {
generation = 0;
} else {
generation = Long.parseLong(segmentFileName.substring(1+IndexFileNames.SEGMENTS.length()),
Character.MAX_RADIX);
}
lastGeneration = generation;
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_SINGLE_NORM_FILE)
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
addElement(new SegmentInfo(directory, format, input));
}
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
}
success = true;
}
finally {
input.close();
if (!success) {
// Clear any segment infos we had loaded so we
// have a clean slate on retry:
clear();
}
}
|
public final void | read(org.apache.lucene.store.Directory directory)This version of read uses the retry logic (for lock-less
commits) to find the right segments file to load.
generation = lastGeneration = -1;
new FindSegmentsFile(directory) {
public Object doBody(String segmentFileName) throws IOException {
read(directory, segmentFileName);
return null;
}
}.run();
|
public static long | readCurrentVersion(org.apache.lucene.store.Directory directory)Current version number from segments file.
return ((Long) new FindSegmentsFile(directory) {
public Object doBody(String segmentFileName) throws IOException {
IndexInput input = directory.openInput(segmentFileName);
int format = 0;
long version = 0;
try {
format = input.readInt();
if(format < 0){
if (format < FORMAT_SINGLE_NORM_FILE)
throw new IOException("Unknown format version: " + format);
version = input.readLong(); // read version
}
}
finally {
input.close();
}
if(format < 0)
return new Long(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, segmentFileName);
return new Long(sis.getVersion());
}
}.run()).longValue();
|
public static void | setDefaultGenFileRetryCount(int count)Advanced: set how many times to try loading the
segments.gen file contents to determine current segment
generation. This file is only referenced when the
primary method (listing the directory) fails.
defaultGenFileRetryCount = count;
|
public static void | setDefaultGenFileRetryPauseMsec(int msec)Advanced: set how many milliseconds to pause in between
attempts to load the segments.gen file.
defaultGenFileRetryPauseMsec = msec;
|
public static void | setDefaultGenLookaheadCount(int count)Advanced: set how many times to try incrementing the
gen when loading the segments file. This only runs if
the primary (listing directory) and secondary (opening
segments.gen file) methods fail to find the segments
file.
defaultGenLookaheadCount = count;
|
public static void | setInfoStream(java.io.PrintStream infoStream)If non-null, information about retries when loading
the segments file will be printed to this.
SegmentInfos.infoStream = infoStream;
|
public final void | write(org.apache.lucene.store.Directory directory)
String segmentFileName = getNextSegmentFileName();
// Always advance the generation on write:
if (generation == -1) {
generation = 1;
} else {
generation++;
}
IndexOutput output = directory.createOutput(segmentFileName);
try {
output.writeInt(FORMAT_SINGLE_NORM_FILE); // 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++) {
info(i).write(output);
}
}
finally {
output.close();
}
try {
output = directory.createOutput(IndexFileNames.SEGMENTS_GEN);
try {
output.writeInt(FORMAT_LOCKLESS);
output.writeLong(generation);
output.writeLong(generation);
} finally {
output.close();
}
} catch (IOException e) {
// It's OK if we fail to write this file since it's
// used only as one of the retry fallbacks.
}
lastGeneration = generation;
|