Dissect Lucene - 文件系统

org.apache.lucene.store

Directory, InputStream, OutputStream这三个抽象类构成了一个抽象的文件系统。

Directory中定义了一个基本文件系统的基本操作:

  1. public abstract class Directory {
  2.   /** Returns an array of strings, one for each file in the directory. */
  3.   public abstract String[] list()
  4.        throws IOException;
  5.  
  6.   /** Returns true iff a file with the given name exists. */
  7.   public abstract boolean fileExists(String name)
  8.        throws IOException;
  9.  
  10.   /** Returns the time the named file was last modified. */
  11.   public abstract long fileModified(String name)
  12.        throws IOException;
  13.  
  14.   /** Set the modified time of an existing file to now. */
  15.   public abstract void touchFile(String name)
  16.        throws IOException;
  17.  
  18.   /** Removes an existing file in the directory. */
  19.   public abstract void deleteFile(String name)
  20.        throws IOException;
  21.  
  22.   /** Renames an existing file in the directory.
  23.     If a file already exists with the new name, then it is replaced.
  24.     This replacement should be atomic. */
  25.   public abstract void renameFile(String from, String to)
  26.        throws IOException;
  27.  
  28.   /** Returns the length of a file in the directory. */
  29.   public abstract long fileLength(String name)
  30.        throws IOException;
  31.  
  32.   /** Creates a new, empty file in the directory with the given name.
  33.       Returns a stream writing this file. */
  34.   public abstract OutputStream createFile(String name)
  35.        throws IOException;
  36.  
  37.   /** Returns a stream reading an existing file. */
  38.   public abstract InputStream openFile(String name)
  39.        throws IOException;
  40.  
  41.   /** Construct a {@link Lock}.
  42.    * @param name the name of the lock file
  43.    */
  44.   public abstract Lock makeLock(String name);
  45.  
  46.   /** Closes the store. */
  47.   public abstract void close()
  48.        throws IOException;
  49. }

InputStream, OutputStream中的分别定义了文件的Input以及Output(这里的文件并不是java.io.File)。

Lucene中对文件(索引文件)的操作也都是通过这三个最基本的类,而不是通过Java的I/O API,这样做的好处就是增加一层抽象,减少一层耦合。

比如Lucene中就提供了

FSDirectory - File System Directory,真实的磁盘文件系统

RAMDirectory - Random Access Memory Directory, 内存中的虚拟文件系统

这样,我们还可以实现其它的,比如

MYSQLDirectory - 存储在MySQL数据库中的文件系统

很多问题都是这样,通过多一层的抽象来解决。

1) FSDirectory

File System Directory

包括三个类,FSDirectory,FSInputStream, FSOutputStream。

很简单,是一个对Java I/O的Wrapper。

FSDirectory的构造函数是Private的,而它提供一个工厂方法(Factory Method)来构造具体的FSDirectory。构造的FSDirectory是被Cached的,主要原因有二:效率/同步。

  1. /** Returns the directory instance for the named location.
  2.    *
  3.    * <p>Directories are cached, so that, for a given canonical path, the same
  4.    * FSDirectory instance will always be returned.  This permits
  5.    * synchronization on directories.
  6.    *
  7.    * @param file the path to the directory.
  8.    * @param create if true, create, or erase any existing contents.
  9.    * @return the FSDirectory for the named file.  */
  10.   public static FSDirectory getDirectory(File file, boolean create)
  11.     throws IOException {
  12.     file = new File(file.getCanonicalPath());
  13.     FSDirectory dir;
  14.     synchronized (DIRECTORIES) {
  15.       dir = (FSDirectory)DIRECTORIES.get(file);
  16.       if (dir == null) {
  17.         dir = new FSDirectory(file, create);
  18.         DIRECTORIES.put(file, dir);
  19.       } else if (create) {
  20.         dir.create();
  21.       }
  22.     }
  23.     synchronized (dir) {
  24.       dir.refCount++;
  25.     }
  26.     return dir;
  27.   }

2) RAMDirectory

Memory resident Directory

包括:RAMFile, RAMDirectory, RAMInputStream, RAMOutputStream

用一种简单直观的内存来映射整个虚拟文件系统。

文件:RAMFile

  1. class RAMFile {
  2.   Vector buffers = new Vector();
  3.   long length;
  4.   long lastModified = System.currentTimeMillis();
  5. }

RAMFile是这个虚拟文件系统的基本组成单位,由大小为BUFFER_SIZE的buffer数组来模拟文件的二进制流。

目录:采用String到RAMFile的名值对应的HashTable,存放在RAMDirectory中。

Popularity: 25%

Related entries:

  • No Related Posts

One Response to “Dissect Lucene - 文件系统”

  1. How To Start A Blog Says:

    How To Start A Blog…

    I couldn’t understand some parts of this article, but it sounds interesting…

Leave a comment

(required)

(required)


Information for comment users
Line and paragraph breaks are implemented automatically. Your e-mail address is never displayed. Please consider what you're posting.

Use the buttons below to customise your comment.


RSS feed for comments on this post | TrackBack URI

 

Creative Commons License
This work is licensed under a Creative Commons License.

一天就好象是这短暂的一生,一生它只是无尽的路上短暂的一天