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

          RAMDirectory是Directory抽象类在使用内存最为文件存储的实现类,其主要是将所有的索引文件保存到内存中。这样可以提高效率。但是如果索引文件过大的话,则会导致内存不足,因此,小型的系统推荐使用,如果大型的,索引文件达到G级别上,推荐使用FSDirectory
首先我们看看该类的源代码:
首先该类继承与抽象类Directory,并且实现了序列话接口

/**一个对于Directory的内存实现的类,锁工厂是有SingleInstanceLockFactory来实现,但是锁工厂可以更改 */
public class RAMDirectory extends Directory implements Serializable {
  private static final long serialVersionUID = 1l;
  HashMap fileMap = new HashMap();			//首先定了保存文件名以及的Map
  long sizeInBytes = 0;						//文件占用的字节数
 /** 构造函数。指定LockFactory为该类的一个实例SingleInstanceLockFactory,SingleInstanceLockFactory类的特点是,所有的加锁操作必须通过该SingleInstanceLockFactory的一个实例而发生,也就是说,在进行加锁操作的时候,必须获取到这个SingleInstanceLockFactory的实例*/
  public RAMDirectory() {
    setLockFactory(new SingleInstanceLockFactory());
  }
  /** 定义一个新的RAMDirectory,并且指定其目录,这个做的目的可以将一个文件系统的索引加载到内存中,这个只能使用在该索引能够被全部加在到内存中。这次只是将系统文件的索引拷贝一份到内存中,加载后,系统文件索引发生变化,将不会在该实例中体现。 */
  public RAMDirectory(Directory dir) throws IOException {
    this(dir, false);
  }
  /** 根据目录,以及参数将该目录中的所有文件复制到内存中,需要复制的文件已经存在,则直接覆盖。closeDir表示是否关闭源目录*/
  private RAMDirectory(Directory dir, boolean closeDir) throws IOException {
    this();
    Directory.copy(dir, this, closeDir);
  }
  /**根据提供的文件路径,将该文件路径下的所有的文件复制到内存中,并且创建一个RAMDirectory对象。如果需要复制的对象已经存在,则覆盖原来的文件*/
  public RAMDirectory(File dir) throws IOException {
    this(FSDirectory.getDirectory(dir), true);
  }
/**根据提供的String路径,将该文件路径下的所有的文件复制到内存中,并且创建一个RAMDirectory对象。如果需要复制的对象已经存在,则覆盖原来的文件*/
  public RAMDirectory(String dir) throws IOException {
    this(FSDirectory.getDirectory(dir), true);
  }
  /**返回所有该文件该内存索引中的所有文件  */
  public synchronized final String[] list() {
    ensureOpen();
    Set fileNames = fileMap.keySet();		//该文件的所有key
    String[] result = new String[fileNames.size()];
    int i = 0;
    Iterator it = fileNames.iterator();
    while (it.hasNext())
      result[i++] = (String)it.next();
    return result;
  }
  /**如果指定文件名的文件在索引中存在,则返回true,否则返回false*/
  public final boolean fileExists(String name) {
    ensureOpen();
    RAMFile file;
    synchronized (this) {
      file = (RAMFile)fileMap.get(name);
    }
    return file != null;
  }
/**返回指定文件最后的修改时间   */
  public final long fileModified(String name) throws IOException {
    ensureOpen();
    RAMFile file;
    synchronized (this) {
      file = (RAMFile)fileMap.get(name);
    }
    if (file==null)
      throw new FileNotFoundException(name);
    return file.getLastModified();
  }
  /**将指定文件的最后修改时间设置为现在 */
  public void touchFile(String name) throws IOException {
    ensureOpen();
    RAMFile file;
    synchronized (this) {
      file = (RAMFile)fileMap.get(name);
    }
    if (file==null)
      throw new FileNotFoundException(name);
    
    long ts2, ts1 = System.currentTimeMillis();
    do {
      try {
        Thread.sleep(0, 1);
      } catch (InterruptedException e) {}
      ts2 = System.currentTimeMillis();
    } while(ts1 == ts2);
    
    file.setLastModified(ts2);
  }
  /** 返回指定文件的大小*/
  public final long fileLength(String name) throws IOException {
    ensureOpen();
    RAMFile file;
    synchronized (this) {
      file = (RAMFile)fileMap.get(name);
    }
    if (file==null)
      throw new FileNotFoundException(name);
    return file.getLength();
  }
  
  /** 返回当前目录中所有的文件大小*/
  public synchronized final long sizeInBytes() {
    ensureOpen();
    return sizeInBytes;
  }
  /** 删除指定文件名的文件*/
  public synchronized void deleteFile(String name) throws IOException {
    ensureOpen();
    RAMFile file = (RAMFile)fileMap.get(name);
    if (file!=null) {
        fileMap.remove(name);
        file.directory = null;
        sizeInBytes -= file.sizeInBytes;       // updates to RAMFile.sizeInBytes synchronized on directory
    } else
      throw new FileNotFoundException(name);
  }
  /**根据制定的名称,创建一个空文件,并且返回操作该文件的输出流。 */
  public IndexOutput createOutput(String name) throws IOException {
    ensureOpen();
    RAMFile file = new RAMFile(this);
    synchronized (this) {
      RAMFile existing = (RAMFile)fileMap.get(name);
      if (existing!=null) {
        sizeInBytes -= existing.sizeInBytes;
        existing.directory = null;
      }
      fileMap.put(name, file);
    }
    return new RAMOutputStream(file);
  }
  /** 根据指定的文件名返回一个操作该文件的输入流 */
  public IndexInput openInput(String name) throws IOException {
    ensureOpen();
    RAMFile file;
    synchronized (this) {
      file = (RAMFile)fileMap.get(name);
    }
    if (file == null)
      throw new FileNotFoundException(name);
    return new RAMInputStream(file);
  }

  /** 关闭并且释放索引占用的内存 */
  public void close() {
    isOpen = false;
    fileMap = null;
  }
}

  

由于RAMDirectory是抽象类Directory根据内存来实现的,所以他不是永久性存在,一旦内存释放,索引就会消失。不像FSDirectory有一个本地的文件来存储索引。

我们看看RAMDirectory的构造函数,特别是根据本地的目录来创建。将永久保存的文件初始化到内存中。因为内存的操作速度肯定比操作文件要快很倍。这样索引的速度就大大的提供了。

将指定的dir目录拷贝到当前的内存中,即实例化一个RAMDirectory。这里,closeDir是一个很重要的状态变量,指定了拷贝完成后,源目录dir是否关闭。如果实例化一个RAMDirectory完成后就关闭源目录dir,可能会因为处理的时间非常短,而需要再次打开源目录dir,持久化到文件系统目录,开销可能会比直接操作源目录dir要大,这点要权衡

<!--EndFragment-->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值