其中常用的就是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: