`
heilwolf
  • 浏览: 32724 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Lucene(2.4.1)技术研究(5)--IndexWrite类源代码解析(一)

阅读更多
1、indexWriter类主要功能是对索引的创建,加入Document后,合并各种索引段,以及控制与索引相关的其他方面,它是lucene索引的主要操纵者。

2、我们首先看看IndexWriter的构造函数

public IndexWriter(String path, Analyzer a, boolean create, MaxFieldLength mfl)

public IndexWriter(String path, Analyzer a, boolean create)(废弃,不提倡使用)

public IndexWriter(File path, Analyzer a, boolean create, MaxFieldLength mfl)

public IndexWriter(File path, Analyzer a, boolean create)(废弃,不提倡使用)

public IndexWriter(Directory d, Analyzer a, boolean create, MaxFieldLength mfl)

public IndexWriter(Directory d, Analyzer a, boolean create)(废弃,不提倡使用)

public IndexWriter(String path, Analyzer a, MaxFieldLength mfl)

public IndexWriter(String path, Analyzer a)(废弃,不提倡使用)

public IndexWriter(File path, Analyzer a, MaxFieldLength mfl)

public IndexWriter(File path, Analyzer a)(废弃,不提倡使用)

public IndexWriter(Directory d, Analyzer a, MaxFieldLength mfl)

public IndexWriter(Directory d, Analyzer a)(废弃,不提倡使用)

public IndexWriter(Directory d, boolean autoCommit, Analyzer a)(废弃,不提倡使用)

public IndexWriter(Directory d, boolean autoCommit, Analyzer a, boolean create)(废弃,不提倡使用)

public IndexWriter(Directory d, Analyzer a, IndexDeletionPolicy deletionPolicy, MaxFieldLength mfl)

public IndexWriter(Directory d, boolean autoCommit, Analyzer a, IndexDeletionPolicy deletionPolicy)(废弃,不提倡使用)

public IndexWriter(Directory d, Analyzer a, boolean create, IndexDeletionPolicy deletionPolicy, MaxFieldLength mfl)

public IndexWriter(Directory d, boolean autoCommit, Analyzer a, boolean create, IndexDeletionPolicy deletionPolicy)(废弃,不提倡使用)

细心的读者肯定会发现,废弃的构造函数与提倡使用的构造函数,多了一个MaxFieldLength参数。带有该参数的构造函数都是允许正常使用的。(注释:废弃的构造函数将在Lucene3.0中移除)

仔细查看其构造函数的实现呢,最终都转化成一个私有的构造函数:如图:

/**

* 该构造函数主要是创建一个IndexWrite对象

* d :指定的存放建立索引文件的索引目录

* a :Analyzer  分词分析器

* create :是否要重新写入索引文件,如果为true,则重写索引文件;如果为false,则追加写入索引文件

* closeDir :一个boolean型变量,表示是否关闭索引目录Directory d,与IndexWriter的一个成员变量相关

* deletionPolicy :指定删除索引文件使用的策略

* autoCommit :建立索引文件后,自动提交。

* maxFieldLength : 表示索引中Field的最大长度。

*/

private void init(Directory d, Analyzer a, final boolean create, boolean closeDir, IndexDeletionPolicy deletionPolicy, boolean autoCommit, int maxFieldLength)

    throws CorruptIndexException, LockObtainFailedException, IOException {

    this.closeDir = closeDir;

    directory = d;

    analyzer = a;

    setMessageID(defaultInfoStream);//这里主要是指定infoStream,是一个PrintStream输出流对象

    this.maxFieldLength = maxFieldLength; //指定Field数据的最大长度


    if (create) {

      // 如果是重新创建索引文件,清除写锁文件write.lock

      directory.clearLock(WRITE_LOCK_NAME);

    }

    Lock writeLock = directory.makeLock(WRITE_LOCK_NAME);

    if (!writeLock.obtain(writeLockTimeout)) // 获取写锁文件

      throw new LockObtainFailedException("Index locked for write: " + writeLock);

    this.writeLock = writeLock;             //保存新的写锁文件


    try {

      if (create) {

        // 如果create为true,表示重写索引文件。重写索引文件之前,要先读取已经存在的索引文件,并且要清除掉历史写入的segment信息

        try {

          segmentInfos.read(directory);

          segmentInfos.clear();

        } catch (IOException e) {

        }

        segmentInfos.commit(directory);  // 向指定的索引存放目录中写入segment信息  

      } else {

        segmentInfos.read(directory); //读取segment信息


        // 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));

        }

      }


      this.autoCommit = autoCommit; //执行提交写入索引的标志

      setRollbackSegmentInfos(segmentInfos); //克隆原来的segment状态信息,并且将信息保存到HashSet。


      docWriter = new DocumentsWriter(directory, this); //创建一个DocumentsWriter对象

      docWriter.setInfoStream(infoStream); //设置DocumentsWriter对象的infoStream信息

      docWriter.setMaxFieldLength(maxFieldLength); //设置DocumentsWriter对象的maxFieldLength信息

      //默认的删除策略实现类为KeepOnlyLastCommitDeletionPolicy,它只是保证将最近提交删除的索引文件,提交删除动作

      // IndexFileDeleter deleter是IndexWriter类的一个私有的成员变量,它在org.apache.lucene.index包里面,主要对删除索引文件进行实现和管理

      deleter = new IndexFileDeleter(directory,

                                     deletionPolicy == null ? new KeepOnlyLastCommitDeletionPolicy() : deletionPolicy,

                                     segmentInfos, infoStream, docWriter);


      pushMaxBufferedDocs(); //刷新DocsBuffer。


      if (infoStream != null) { //如果infoStream是null

        message("init: create=" + create);

        messageState();

      }


    } catch (IOException e) {

      this.writeLock.release();

      this.writeLock = null;

      throw e;

    }

  }

  通过IndexWrite的构造函数,以及最终的实现方法的init分发,其主要是实现了根据指定的建立索引的方式(重写、追加写入),通过create标志位来判断,从而指定一种在操作索引文件的过程中删除索引文件的策略。

在理解lucene的时间,必须熟悉其初始化IndexWrite的原理,才能深入了解该框架在创建索引的核心实现机制。

<------------------我是快乐的转载符----------------------->
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics