public final class SegmentInfos extends java.lang.Object implements java.lang.Cloneable, java.lang.Iterable<SegmentCommitInfo>
The active segments in the index are stored in the segment info file,
segments_N. There may be one or more segments_N files in
the index; however, the one with the largest generation is the active one
(when older segments_N files are present it's because they temporarily cannot
be deleted, or a custom IndexDeletionPolicy
is in
use). This file lists each segment by name and has details about the codec
and generation of deletes.
Files:
IndexHeader
Version
was used for this commit, written as three vInt
: major, minor, bugfix
Version
of the oldest segment, written as three vInt
: major, minor, bugfix; this is only
written only if there's at least one segment
Int32
Int64
Int8ID_LENGTH
String
Map<String,String>
Int32
,
Set<String>
>CodecFooter
LiveDocsFormat
.name
of the Codec that encoded
this segment.IndexWriter.setLiveCommitData(Iterable)
.FieldInfosFormat
.DocValuesFormat
.Modifier and Type | Class and Description |
---|---|
static class |
SegmentInfos.FindSegmentsFile<T>
Utility class for executing code that needs to do
something with the current segments file.
|
Modifier and Type | Field and Description |
---|---|
long |
counter
Used to name new segments.
|
private long |
generation |
private byte[] |
id
Id for this commit; only written starting with Lucene 5.0
|
private int |
indexCreatedVersionMajor
The Lucene version major that was used to create the index.
|
private static java.io.PrintStream |
infoStream
If non-null, information about loading segments_N files
will be printed here.
|
private long |
lastGeneration |
private Version |
luceneVersion
Which Lucene version wrote this commit.
|
private Version |
minSegmentLuceneVersion
Version of the oldest segment in the index, or null if there are no segments.
|
(package private) boolean |
pendingCommit |
private java.util.List<SegmentCommitInfo> |
segments |
java.util.Map<java.lang.String,java.lang.String> |
userData
Opaque Map<String, String> that user can specify during IndexWriter.commit
|
long |
version
Counts how often the index has been changed.
|
static int |
VERSION_70
The version that added information about the Lucene version at the time when the index has been created.
|
static int |
VERSION_72
The version that updated segment name counter to be long instead of int.
|
static int |
VERSION_74
The version that recorded softDelCount
|
(package private) static int |
VERSION_CURRENT |
Constructor and Description |
---|
SegmentInfos(int indexCreatedVersionMajor)
Sole constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
add(SegmentCommitInfo si)
Appends the provided
SegmentCommitInfo . |
void |
addAll(java.lang.Iterable<SegmentCommitInfo> sis)
Appends the provided
SegmentCommitInfo s. |
(package private) void |
applyMergeChanges(MergePolicy.OneMerge merge,
boolean dropSegment)
applies all changes caused by committing a merge to this SegmentInfos
|
java.util.List<SegmentCommitInfo> |
asList()
Returns all contained segments as an unmodifiable
List view. |
void |
changed()
Call this before committing if changes have been made to the
segments.
|
void |
clear()
Clear all
SegmentCommitInfo s. |
SegmentInfos |
clone()
Returns a copy of this instance, also copying each
SegmentInfo.
|
void |
commit(Directory dir)
Writes and syncs to the Directory dir, taking care to
remove the segments file on exception
|
(package private) boolean |
contains(SegmentCommitInfo si)
Return true if the provided
SegmentCommitInfo is contained. |
(package private) java.util.List<SegmentCommitInfo> |
createBackupSegmentInfos() |
java.util.Collection<java.lang.String> |
files(boolean includeSegmentsFile)
Returns all file names referenced by SegmentInfo.
|
(package private) java.lang.String |
finishCommit(Directory dir)
Returns the committed segments_N filename.
|
static long |
generationFromSegmentsFileName(java.lang.String fileName)
Parse the generation off the segments file name and
return it.
|
Version |
getCommitLuceneVersion()
Returns which Lucene
Version wrote this commit, or null if the
version this index was written with did not directly record the version. |
long |
getGeneration()
Returns current generation.
|
byte[] |
getId()
Since Lucene 5.0, every commit (segments_N) writes a unique id.
|
int |
getIndexCreatedVersionMajor()
Return the version major that was used to initially create the index.
|
static java.io.PrintStream |
getInfoStream()
Returns
infoStream . |
static long |
getLastCommitGeneration(Directory directory)
Get the generation of the most recent commit to the
index in this directory (N in the segments_N file).
|
static long |
getLastCommitGeneration(java.lang.String[] files)
Get the generation of the most recent commit to the
list of index files (N in the segments_N file).
|
static java.lang.String |
getLastCommitSegmentsFileName(Directory directory)
Get the filename of the segments_N file for the most
recent commit to the index in this Directory.
|
static java.lang.String |
getLastCommitSegmentsFileName(java.lang.String[] files)
Get the filename of the segments_N file for the most
recent commit in the list of index files.
|
long |
getLastGeneration()
Returns last succesfully read or written generation.
|
Version |
getMinSegmentLuceneVersion()
Returns the version of the oldest segment, or null if there are no segments.
|
private long |
getNextPendingGeneration()
return generation of the next pending_segments_N that will be written
|
java.lang.String |
getSegmentsFileName()
Get the segments_N filename in use by this segment infos.
|
java.util.Map<java.lang.String,java.lang.String> |
getUserData()
Return
userData saved with this commit. |
long |
getVersion()
version number when this SegmentInfos was generated.
|
(package private) int |
indexOf(SegmentCommitInfo si)
Returns index of the provided
SegmentCommitInfo . |
SegmentCommitInfo |
info(int i)
Returns
SegmentCommitInfo at the provided
index. |
java.util.Iterator<SegmentCommitInfo> |
iterator()
Returns an unmodifiable
Iterator of contained segments in order. |
private static void |
message(java.lang.String message)
Prints the given message to the infoStream.
|
(package private) void |
prepareCommit(Directory dir)
Call this to start a commit.
|
private static Codec |
readCodec(DataInput input) |
static SegmentInfos |
readCommit(Directory directory,
ChecksumIndexInput input,
long generation)
Read the commit from the provided
ChecksumIndexInput . |
static SegmentInfos |
readCommit(Directory directory,
java.lang.String segmentFileName)
Read a particular segmentFileName.
|
static SegmentInfos |
readLatestCommit(Directory directory)
Find the latest commit (
segments_N file ) and
load all SegmentCommitInfo s. |
(package private) void |
remove(int index)
Remove the
SegmentCommitInfo at the
provided index. |
boolean |
remove(SegmentCommitInfo si)
Remove the provided
SegmentCommitInfo . |
(package private) void |
replace(SegmentInfos other)
Replaces all segments in this instance, but keeps
generation, version, counter so that future commits
remain write once.
|
(package private) void |
rollbackCommit(Directory dir) |
(package private) void |
rollbackSegmentInfos(java.util.List<SegmentCommitInfo> infos) |
static void |
setInfoStream(java.io.PrintStream infoStream)
If non-null, information about retries when loading
the segments file will be printed to this.
|
void |
setNextWriteGeneration(long generation)
Set the generation to be used for the next commit
|
void |
setUserData(java.util.Map<java.lang.String,java.lang.String> data,
boolean doIncrementVersion)
Sets the commit data.
|
(package private) void |
setVersion(long newVersion) |
int |
size()
Returns number of
SegmentCommitInfo s. |
java.lang.String |
toString()
Returns readable description of this segment.
|
int |
totalMaxDoc()
Returns sum of all segment's maxDocs.
|
void |
updateGeneration(SegmentInfos other)
Carry over generation numbers from another SegmentInfos
|
(package private) void |
updateGenerationVersionAndCounter(SegmentInfos other) |
private void |
write(Directory directory) |
void |
write(Directory directory,
IndexOutput out)
Write ourselves to the provided
IndexOutput |
public static final int VERSION_70
public static final int VERSION_72
public static final int VERSION_74
static final int VERSION_CURRENT
public long counter
public long version
private long generation
private long lastGeneration
public java.util.Map<java.lang.String,java.lang.String> userData
private java.util.List<SegmentCommitInfo> segments
private static java.io.PrintStream infoStream
private byte[] id
private Version luceneVersion
private Version minSegmentLuceneVersion
private final int indexCreatedVersionMajor
boolean pendingCommit
public SegmentInfos(int indexCreatedVersionMajor)
indexCreatedVersionMajor
- the Lucene version major at index creation time, or 6 if the index was created before 7.0public SegmentCommitInfo info(int i)
SegmentCommitInfo
at the provided
index.public static long getLastCommitGeneration(java.lang.String[] files)
files
- -- array of file names to checkpublic static long getLastCommitGeneration(Directory directory) throws java.io.IOException
directory
- -- directory to search for the latest segments_N filejava.io.IOException
public static java.lang.String getLastCommitSegmentsFileName(java.lang.String[] files)
files
- -- array of file names to checkpublic static java.lang.String getLastCommitSegmentsFileName(Directory directory) throws java.io.IOException
directory
- -- directory to search for the latest segments_N filejava.io.IOException
public java.lang.String getSegmentsFileName()
public static long generationFromSegmentsFileName(java.lang.String fileName)
private long getNextPendingGeneration()
public byte[] getId()
public static final SegmentInfos readCommit(Directory directory, java.lang.String segmentFileName) throws java.io.IOException
directory
- -- directory containing the segments filesegmentFileName
- -- segment file to loadCorruptIndexException
- if the index is corruptjava.io.IOException
- if there is a low-level IO errorpublic static final SegmentInfos readCommit(Directory directory, ChecksumIndexInput input, long generation) throws java.io.IOException
ChecksumIndexInput
.java.io.IOException
private static Codec readCodec(DataInput input) throws java.io.IOException
java.io.IOException
public static final SegmentInfos readLatestCommit(Directory directory) throws java.io.IOException
segments_N file
) and
load all SegmentCommitInfo
s.java.io.IOException
private void write(Directory directory) throws java.io.IOException
java.io.IOException
public void write(Directory directory, IndexOutput out) throws java.io.IOException
IndexOutput
java.io.IOException
public SegmentInfos clone()
clone
in class java.lang.Object
public long getVersion()
public long getGeneration()
public long getLastGeneration()
public static void setInfoStream(java.io.PrintStream infoStream)
public static java.io.PrintStream getInfoStream()
infoStream
.setInfoStream(java.io.PrintStream)
private static void message(java.lang.String message)
public void updateGeneration(SegmentInfos other)
void updateGenerationVersionAndCounter(SegmentInfos other)
public void setNextWriteGeneration(long generation)
final void rollbackCommit(Directory dir)
final void prepareCommit(Directory dir) throws java.io.IOException
finishCommit(org.apache.lucene.store.Directory)
to complete
the commit or rollbackCommit(org.apache.lucene.store.Directory)
to abort it.
Note: changed()
should be called prior to this
method if changes have been made to this SegmentInfos
instance
java.io.IOException
public java.util.Collection<java.lang.String> files(boolean includeSegmentsFile) throws java.io.IOException
java.io.IOException
final java.lang.String finishCommit(Directory dir) throws java.io.IOException
java.io.IOException
public final void commit(Directory dir) throws java.io.IOException
Note: changed()
should be called prior to this
method if changes have been made to this SegmentInfos
instance
java.io.IOException
public java.lang.String toString()
toString
in class java.lang.Object
public java.util.Map<java.lang.String,java.lang.String> getUserData()
userData
saved with this commit.IndexWriter.commit()
public void setUserData(java.util.Map<java.lang.String,java.lang.String> data, boolean doIncrementVersion)
void replace(SegmentInfos other)
public int totalMaxDoc()
public void changed()
void setVersion(long newVersion)
void applyMergeChanges(MergePolicy.OneMerge merge, boolean dropSegment)
java.util.List<SegmentCommitInfo> createBackupSegmentInfos()
void rollbackSegmentInfos(java.util.List<SegmentCommitInfo> infos)
public java.util.Iterator<SegmentCommitInfo> iterator()
Iterator
of contained segments in order.iterator
in interface java.lang.Iterable<SegmentCommitInfo>
public java.util.List<SegmentCommitInfo> asList()
List
view.public int size()
SegmentCommitInfo
s.public void add(SegmentCommitInfo si)
SegmentCommitInfo
.public void addAll(java.lang.Iterable<SegmentCommitInfo> sis)
SegmentCommitInfo
s.public void clear()
SegmentCommitInfo
s.public boolean remove(SegmentCommitInfo si)
SegmentCommitInfo
.
WARNING: O(N) cost
void remove(int index)
SegmentCommitInfo
at the
provided index.
WARNING: O(N) cost
boolean contains(SegmentCommitInfo si)
SegmentCommitInfo
is contained.
WARNING: O(N) cost
int indexOf(SegmentCommitInfo si)
SegmentCommitInfo
.
WARNING: O(N) cost
public Version getCommitLuceneVersion()
Version
wrote this commit, or null if the
version this index was written with did not directly record the version.public Version getMinSegmentLuceneVersion()
public int getIndexCreatedVersionMajor()