`
guming123416
  • 浏览: 18655 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

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

阅读更多

其中常用的就是FSDirectory:表示对文件系统目录的操作。RAMDirectory :内存中的目录操作。

首先我们看看类FSDirectory的源代码

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.RandomAccessFile;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.util.HashMap;

import java.util.Map;

import org.apache.lucene.index.IndexFileNameFilter;

// Used only for WRITE_LOCK_NAME in deprecated create=true case:

import org.apache.lucene.index.IndexWriter;

public class FSDirectory extends Directory {

  //主要是用来缓存FilePath与FSDirectory,主要是保证针对统一目录,只能有一个FSDirectory实例

  private static final Map DIRECTORIES = new HashMap();

  //主要判断是否给目录上锁

  private static boolean disableLocks = false;

  //设置是否给目录上锁,一般只能用户只读目录。

  //They should only be disabled if the index

  // is on a read-only medium like a CD-ROM

  public static void setDisableLocks(boolean doDisableLocks) {

    FSDirectory.disableLocks = doDisableLocks;

  }

  //f返回是否给目录上锁

  public static boolean getDisableLocks() {

    return FSDirectory.disableLocks;

  }

  public static final String LOCK_DIR = System.getProperty("org.apache.lucene.lockDir",

                                                           System.getProperty("java.io.tmpdir"));

  private static Class IMPL; //主要是获取FSDirectory的实例

  static {

    try {

      String name =System.getProperty("org.apache.lucene.FSDirectory.class",

                           FSDirectory.class.getName());

      IMPL = Class.forName(name); //首先获取该类的名称,然后在获取该类的Class实例

    } catch (ClassNotFoundException e) {

      throw new RuntimeException("cannot load FSDirectory class: " + e.toString(), e);

    } catch (SecurityException se) {

      try {

        IMPL = Class.forName(FSDirectory.class.getName()); //直接使用JAVA反射获取该类Class实例

      } catch (ClassNotFoundException e) {

        throw new RuntimeException("cannot load default FSDirectory class: " + e.toString(), e);

      }

    }

  }

  private static MessageDigest DIGESTER; //获取加密

  static {

    try {

      DIGESTER = MessageDigest.getInstance("MD5"); //使用MD5加密

    } catch (NoSuchAlgorithmException e) {

        throw new RuntimeException(e.toString(), e);

    }

  }

  private byte[] buffer = null;

  /**静态方法,根据基于指定目录,返回该目录中对应的一个FSDirectory实例 */

  public static FSDirectory getDirectory(String path) throws IOException {

    return getDirectory(new File(path), null);

  }

  /** 静态方法,根据指定的路径path以及锁工厂LockFactory参数,返回该路径的一个FSDirectiry实例 */

  public static FSDirectory getDirectory(String path, LockFactory lockFactory) throws IOException {

    return getDirectory(new File(path), lockFactory);

  }

  /**静态方法,根据指定的File对象,返回该路径的一个FSDirectiry实例 */

  public static FSDirectory getDirectory(File file) throws IOException {

    return getDirectory(file, null);

  }

  /**静态方法,根据指定File对象以及锁工厂LockFactory参数,返回该路径的一个FSDirectiry实例

其他方法,都最终转化为该方法来实现 */

  public static FSDirectory getDirectory(File file, LockFactory lockFactory)  throws IOException

  {

    file = new File(file.getCanonicalPath()); //定义一个File对象,以传递的参数作为目录

    if (file.exists() && !file.isDirectory()) //首先判断文件存在时,是否是目录

      throw new IOException(file + " not a directory");

    if (!file.exists()) //如果文件不存在。则创建

      if (!file.mkdirs())  throw new IOException("Cannot create directory: " + file);  //创建所有目录

    FSDirectory dir; //定义一个类 FSDirectory 

    synchronized (DIRECTORIES) {

      dir = (FSDirectory)DIRECTORIES.get(file); //首先从Map查找是否存在该实例,

      if (dir == null) {

        try {

          dir = (FSDirectory)IMPL.newInstance(); //调用静态内部类IMPL获取一个与文件系统目录有关的Directory类,并加载该类

        } catch (Exception e) {

          throw new RuntimeException("cannot load FSDirectory class: " + e.toString(), e);

        }

        dir.init(file, lockFactory); // 根据指定的file和lockFactory,调用该类Directory的init方法,进行FSDirectory的初始化初始化工作

        DIRECTORIES.put(file, dir); //将该实例放置到HashMap中

      } else {

        //如果该目录dir管理所用的锁工厂实例为空,或者不是同一个锁工厂实例,抛出异常

        if (lockFactory != null && lockFactory != dir.getLockFactory()) {

          throw new IOException("Directory was previously created with a different LockFactory instance; please pass null as the lockFactory instance and use setLockFactory to change it");

        }

      }

    }

    synchronized (dir) {

      dir.refCount++; //用于记录该目录dir被引用的计数增加1

    }

    return dir;

  }

  /** 废弃的方法  */

  public static FSDirectory getDirectory(String path, boolean create) throws IOException {

    return getDirectory(new File(path), create);

  }

 /** 废弃的方法  */

  public static FSDirectory getDirectory(File file, boolean create)  throws IOException

  {

    FSDirectory dir = getDirectory(file, null);

    if (create) {

      dir.create();

    }

    return dir;

  }

  private void create() throws IOException {

    if (directory.exists()) {

      String[] files = directory.list(IndexFileNameFilter.getFilter());            // clear old files

      if (files == null)

        throw new IOException("cannot read directory " + directory.getAbsolutePath() + ": list() returned null");

      for (int i = 0; i < files.length; i++) {

        File file = new File(directory, files[i]);

        if (!file.delete())

          throw new IOException("Cannot delete " + file);

      }

    }

    lockFactory.clearLock(IndexWriter.WRITE_LOCK_NAME);

  }

  private File directory = null; //File directory是FSDirectory类的一个成员

  private int refCount; //用于记录该目录dir被引用的计数增加1

  protected FSDirectory() {};                     // permit subclassing

  private void init(File path, LockFactory lockFactory) throws IOException {

//根据指定的file和lockFactory,调用该类Directory的init方法,进行FSDirectory的初始化初始化工作    

    directory = path;

    boolean doClearLockID = false;

    if (lockFactory == null) { //锁工厂实例为null

      if (disableLocks) { //如果锁不可以使用

        lockFactory = NoLockFactory.getNoLockFactory(); //调用NoLockFactory类,获取NoLockFactory实例,为当前的锁工厂实例。其实NoLockFactory是一个单态(singleton)模式的工厂类,应用中只能有一个锁实例,不需要进行同步

      } else {                //如果锁可以使用,获取锁工厂类名称的字符串描述

        String lockClassName = System.getProperty("org.apache.lucene.store.FSDirectoryLockFactoryClass");

        if (lockClassName != null && !lockClassName.equals("")) {//如果获取的锁工厂类名称的字符串描述不为null,而且者不为空

          Class c;

          try {

            c = Class.forName(lockClassName); //创建一个Class对象,加载该锁工厂类

          } catch (ClassNotFoundException e) {

            throw new IOException("unable to find LockClass " + lockClassName);

          }

          try {

            lockFactory = (LockFactory) c.newInstance();   //获取一个锁工厂的实例      

          } catch (IllegalAccessException e) {

            throw new IOException("IllegalAccessException when instantiating LockClass " + lockClassName);

          } catch (InstantiationException e) {

            throw new IOException("InstantiationException when instantiating LockClass " + lockClassName);

          } catch (ClassCastException e) {

            throw new IOException("unable to cast LockClass " + lockClassName + " instance to a LockFactory");

          }

// 根据获取的锁工厂实例的类型来设置对文件File path加锁的方式

          if (lockFactory instanceof NativeFSLockFactory) {

            ((NativeFSLockFactory) lockFactory).setLockDir(path);

          } else if (lockFactory instanceof SimpleFSLockFactory) {

            ((SimpleFSLockFactory) lockFactory).setLockDir(path);

          }

        } else {   

// 没有其他的锁工厂类可用,则使用默认的锁工厂类创建一个锁工厂实例          

lockFactory = new SimpleFSLockFactory(path);

          doClearLockID = true;

        }

      }

    }

// 设置当前FSDirectory相关锁工厂实例

    setLockFactory(lockFactory);

    if (doClearLockID) {

      // Clear the prefix because write.lock will be

      // stored in our directory:

      lockFactory.setLockPrefix(null);

    }

  }

  /** 返回所有的在当前目录下的Lucene索引文件名,并且保存在数组中*/

  public String[] list() {

    ensureOpen();

    return directory.list(IndexFileNameFilter.getFilter());

  }

  /**检查指定名称的文件是否存在 */

  public boolean fileExists(String name) {

    ensureOpen();

    File file = new File(directory, name);

    return file.exists();

  }

  /**  返回指定文件最后修改的时间*/

  public long fileModified(String name) {

    ensureOpen();

    File file = new File(directory, name);

    return file.lastModified();

  }

  /** 返回指定目录和文件名的文件最后修改的时间*/

  public static long fileModified(File directory, String name) {

    File file = new File(directory, name);

    return file.lastModified();

  }

  /**设置指定文件最后修改的时间为当前时间*/

  public void touchFile(String name) {

    ensureOpen();

    File file = new File(directory, name);

    file.setLastModified(System.currentTimeMillis());

  }

  /**返回指定文件的长度*/

  public long fileLength(String name) {

    ensureOpen();

    File file = new File(directory, name);

    return file.length();

  }

  /**删除当前目录中指定文件名的文件*/

  public void deleteFile(String name) throws IOException {

    ensureOpen();

    File file = new File(directory, name);

    if (!file.delete())

      throw new IOException("Cannot delete " + file);

  }

  /** 创建一个名称为name的文件,返回一个输出流,以便对该文件进行写入操作 */

font-size: 10.5pt; font-family:

分享到:
评论

相关推荐

    MMAnalyzer 分词必导入jar包(lucene-core-2.4.1.jar je-analysis-1.5.3.jar)

    然而,使用MMAnalyzer时,仅引入`jeasy.analysis.MMAnalyzer`的JAR包是不够的,因为这个类依赖于Lucene库,特别是`lucene-core-2.4.1.jar`。Lucene是一个高性能、全功能的全文搜索引擎库,它提供了一套完整的搜索...

    MMAnalyzer 分词jar包(lucene-core-2.4.1.jar je-analysis-1.5.3.jar)

    "lucene-core-2.4.1.jar"是Lucene的核心库,包含了Lucene的基本功能,如索引构建、查询解析、搜索等功能。虽然2.4.1版本相对较老,但在当时,它已经相当成熟,能够支持各种复杂的文本处理任务。这个库是MMAnalyzer...

    je-analysis-1.5.3、lucene-core-2.4.1分词组件

    本文将深入探讨"je-analysis-1.5.3"和"lucene-core-2.4.1"这两个分词组件,以及它们在相关场景中的应用。 首先,让我们了解什么是分词。分词,即词语切分,是自然语言处理(NLP)中的基本任务之一,它的目标是将...

    lucene-analyzers-smartcn-7.7.0-API文档-中文版.zip

    赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-analyzers-smartcn-7.7.0.pom; 包含翻译后的API文档:lucene-analyzers-smartcn-7.7.0-javadoc-API文档-中文(简体)版....

    lucene-analyzers-common-6.6.0-API文档-中文版.zip

    赠送源代码:lucene-analyzers-common-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-analyzers-common-6.6.0.pom; 包含翻译后的API文档:lucene-analyzers-common-6.6.0-javadoc-API文档-中文(简体)版.zip;...

    lucene-core-7.7.0-API文档-中文版.zip

    赠送源代码:lucene-core-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.7.0.pom; 包含翻译后的API文档:lucene-core-7.7.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.lucene:lucene...

    lucene-backward-codecs-7.3.1-API文档-中英对照版.zip

    赠送源代码:lucene-backward-codecs-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-codecs-7.3.1.pom; 包含翻译后的API文档:lucene-backward-codecs-7.3.1-javadoc-API文档-中文(简体)-英语-对照...

    lucene-spatial-extras-7.3.1-API文档-中英对照版.zip

    赠送源代码:lucene-spatial-extras-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras-7.3.1.pom; 包含翻译后的API文档:lucene-spatial-extras-7.3.1-javadoc-API文档-中文(简体)-英语-对照版....

    lucene-analyzers-smartcn-7.7.0-API文档-中英对照版.zip

    赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-analyzers-smartcn-7.7.0.pom; 包含翻译后的API文档:lucene-analyzers-smartcn-7.7.0-javadoc-API文档-中文(简体)-英语...

    lucene-spatial-extras-7.2.1-API文档-中英对照版.zip

    赠送源代码:lucene-spatial-extras-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras-7.2.1.pom; 包含翻译后的API文档:lucene-spatial-extras-7.2.1-javadoc-API文档-中文(简体)-英语-对照版....

    lucene-spatial-extras-6.6.0-API文档-中英对照版.zip

    赠送源代码:lucene-spatial-extras-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras-6.6.0.pom; 包含翻译后的API文档:lucene-spatial-extras-6.6.0-javadoc-API文档-中文(简体)-英语-对照版....

    lucene-backward-codecs-7.2.1-API文档-中英对照版.zip

    赠送源代码:lucene-backward-codecs-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-codecs-7.2.1.pom; 包含翻译后的API文档:lucene-backward-codecs-7.2.1-javadoc-API文档-中文(简体)-英语-对照...

    lucene-backward-codecs-6.6.0-API文档-中英对照版.zip

    赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-codecs-6.6.0.pom; 包含翻译后的API文档:lucene-backward-codecs-6.6.0-javadoc-API文档-中文(简体)-英语-对照...

    lucene-backward-codecs-6.6.0-API文档-中文版.zip

    赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-codecs-6.6.0.pom; 包含翻译后的API文档:lucene-backward-codecs-6.6.0-javadoc-API文档-中文(简体)版.zip; ...

    lucene-core-7.2.1-API文档-中文版.zip

    赠送源代码:lucene-core-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.2.1.pom; 包含翻译后的API文档:lucene-core-7.2.1-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.lucene:lucene...

    lucene-suggest-6.6.0-API文档-中文版.zip

    赠送源代码:lucene-suggest-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-suggest-6.6.0.pom; 包含翻译后的API文档:lucene-suggest-6.6.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache....

    lucene-highlighter-6.6.0-API文档-中文版.zip

    赠送源代码:lucene-highlighter-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-highlighter-6.6.0.pom; 包含翻译后的API文档:lucene-highlighter-6.6.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org...

    lucene-core-6.6.0-API文档-中文版.zip

    赠送源代码:lucene-core-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-6.6.0.pom; 包含翻译后的API文档:lucene-core-6.6.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.lucene:lucene...

    lucene 2.4.1源码在eclipse调试运行通过

    3. 选择“Select root directory”,浏览并选择刚刚下载的“lucene-2.4.1-src”文件夹,然后点击“Finish”。 项目结构理解: Lucene 2.4.1的源码结构清晰,主要分为几个核心模块,如core、analysis、demo、 ...

    lucene-sandbox-7.2.1-API文档-中文版.zip

    赠送源代码:lucene-sandbox-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-sandbox-7.2.1.pom; 包含翻译后的API文档:lucene-sandbox-7.2.1-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache....

Global site tag (gtag.js) - Google Analytics