大家用的最多的就是IndexReader上的open,其实在lucene里有DirectoryIndexerReader,关系如下:
abstract class DirectoryIndexReader extends IndexReader{}
其中定义的几个方法,请注意2.4和2.3.2的open方法的差别:
2.3.2:
static DirectoryIndexReader open(final Directory directory, final boolean closeDirectory, final IndexDeletionPolicy deletionPolicy) throws CorruptIndexException, IOException
2.4.0:
static DirectoryIndexReader open(final Directory directory, final boolean closeDirectory, final IndexDeletionPolicy deletionPolicy, final IndexCommit commit, final boolean readOnly) throws CorruptIndexException, IOException
实际上在2.4上赋予了Directory一个io核心的概念,我们可以看到所有的io操作最终都会转换成directory上的操作,因此才会在directory上赋予了commit和只读概念.
我们来看两个函数的变化
1 init:
在2.4中增加了如下的代码:
if (!readOnly && segmentInfos != null) {
// We assume that this segments_N was previously
// properly sync'd:
for(int i=0;i<segmentInfos.size();i++) {
final SegmentInfo info = segmentInfos.info(i);
List files = info.files();
for(int j=0;j<files.size();j++)
synced.add(files.get(j));
}
}
这段新加的代码在directory上增加了区段信息,区段信息可以做两件事情:
1 索引版本
2 是否优化
因此在reopen中2.4的代码得到了很多的改进,我曾经说过2.3里的reopen是个垃圾,因为它把所有的判断扔还给了开发人员,这是不合理的,下面看看reopen的新变化:
public final synchronized IndexReader reopen() throws CorruptIndexException, IOException {
ensureOpen();
if (this.hasChanges || this.isCurrent()) {
// this has changes, therefore we have the lock and don't need to reopen
这个应该就是针对changes中的:
LUCENE-1228: IndexWriter.commit() was not updating the index version and as result IndexReader.reopen() failed to sense index changes.
(Doron Cohen)
// OR: the index in the directory hasn't changed - nothing to do here
return this;
}
return (DirectoryIndexReader) new SegmentInfos.FindSegmentsFile(directory) {
protected Object doBody(String segmentFileName) throws CorruptIndexException, IOException {
SegmentInfos infos = new SegmentInfos();
infos.read(directory, segmentFileName);
DirectoryIndexReader newReader = doReopen(infos);
if (DirectoryIndexReader.this != newReader) {
newReader.init(directory, infos, closeDirectory, readOnly);
newReader.deletionPolicy = deletionPolicy;
}
return newReader;
}
}.run();
}
最后看看open:
SegmentInfos.FindSegmentsFile finder = new SegmentInfos.FindSegmentsFile(directory) {
protected Object doBody(String segmentFileName) throws CorruptIndexException, IOException {
SegmentInfos infos = new SegmentInfos();
infos.read(directory, segmentFileName);
DirectoryIndexReader reader;
if (infos.size() == 1) { // index is optimized
reader = SegmentReader.get(readOnly, infos, infos.info(0), closeDirectory);
} else if (readOnly) {
reader = new ReadOnlyMultiSegmentReader(directory, infos, closeDirectory);
} else {
原来代码中一旦没有优化就会使用多区段reader但是现在增加了ReadOnly版本,应该是commit策略有关的玩意,稍后我们来看看这东西好了. reader = new MultiSegmentReader(directory, infos, closeDirectory, false);
}
reader.setDeletionPolicy(deletionPolicy);
return reader;
}
};
if (commit == null)
return (DirectoryIndexReader) finder.run();
else {
if (directory != commit.getDirectory())
throw new IOException("the specified commit does not match the specified Directory");
// This can & will directly throw IOException if the
// specified commit point has been deleted:
return (DirectoryIndexReader) finder.doBody(commit.getSegmentsFileName());
}
commit上 2.4中已经废止了原来的commitPoint类,改成了IndexCommit,作用类似主要方法定义如下:
* Returns all index files referenced by this commit point.
*/
public abstract Collection getFileNames() throws IOException;
/**
* Returns the {@link Directory} for the index.
*/
public abstract Directory getDirectory();
增加如上的两个成员之后使得对于文件控制直接挂载到了directory,正像我说的2.4开始选择Directory为核心io处理了,呵呵.
最后来看看我们的readonlyMutiSegmentReader
class ReadOnlyMultiSegmentReader extends MultiSegmentReader {
ReadOnlyMultiSegmentReader(Directory directory, SegmentInfos sis, boolean closeDirectory) throws IOException {
super(directory, sis, closeDirectory, true);
}
ReadOnlyMultiSegmentReader(Directory directory, SegmentInfos infos, boolean closeDirectory, SegmentReader[] oldReaders, int[] oldStarts, Map oldNormsCache) throws IOException {
super(directory, infos, closeDirectory, oldReaders, oldStarts, oldNormsCache, true);
}
protected void acquireWriteLock() {
ReadOnlySegmentReader.noWrite();
}
}
看来花头不多,只是改变了默认参数后需要增加这样一个类加以判断而已.今天就这些吧,下次继续...