- 1、本文档共11页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
[计算机]Lucene代码分析
Lucene源代码分析[1]
首先讲一下Lucene的发音是Loo-seen,这是Lucene in Action中提到过的。另外强调的一点是我用的版本是1.9版,大家看到这里不要惊讶,我用这么早的版本,是为了能更好的了解Lucene的核心。如果有人看过最新的版本就应该了解,对于一个初学者,Lucene最高版本并不那么简单,它涉及了多线程,设计模式,对我反正是很挑战了。我先看老版本这也是受《LINUX内核完全注释》作者赵炯的启发,他分析的不是最新的Linux内核,而是1.11的版本。
我开始还是用调试的方式来解释,我想大家和我一样,如果看了半天analyzer也会有点不耐烦,我先写一个什么意义都没有例子(有那么一点意义的例子,网上很多):
package forfun;
?
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.index.IndexWriter;
?
public class Test
{
public static void main( String[] args ) throws Exception
{
IndexWriter writer = new IndexWriter( E:\\a\\, new
SimpleAnalyzer(), true);
}
}
IndexWriter是最核心的一个类,一般的Blogger把其它所有的包都分析完了,就剩这最核心的一个包的时候,就分析不动了。
我们先看一下它的参数,第一个就是索引存放的路径,第二个参数是一个Analyzer对象,它对输入数据进行过滤,分词等,第三个参数如果为true,那么它删除原目录内的所有内容重建索引,如果为false,就在已经存在的索引上追加新的内容。
你可以先运行一下,就会发现指定的目录下有一个segments文件。
调试的时候,暂时不去管SimpleAnalyzer类。
我们看一下IndexWriter类的构造函数:
public IndexWriter(String path, Analyzer a, boolean create)
throws IOException {
this(FSDirectory.getDirectory(path, create), a, create, true);
}
这里我们看到一个新的类FSDirectory:
public static FSDirectory getDirectory(String path, boolean create)
throws IOException {
return getDirectory(new File(path), create);
}
再看getDirectory函数:
public static FSDirectory getDirectory(File file, boolean create)
throws IOException {
file = new File(file.getCanonicalPath());
FSDirectory dir;
synchronized (DIRECTORIES) {
dir = (FSDirectory) DIRECTORIES.get(file);
if (dir == null) {
try {
dir = (FSDirectory) IMPL.newInstance();
} catch (Exception e) {
throw new RuntimeException(
cannot load FSDirectory class: + e.toString());
}
dir.init(file, create);
DIRECTORIES.put(file, dir);
} else if (create) {
dir.create();
}
}
synchronized (dir) {
dir.refCount++;
}
return dir;
}
DIRECTORIES是一个Hashtable对象,DIRECTORIES注释上讲,目录的缓存,保证唯一的路径和Directory对应,所以在Directory上同步可以对读写进行同步访问。
(This cache of directories ensures that there is a unique Directory instance per path, so that synchro
文档评论(0)