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); // 加载该锁工厂类
} 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;
}
}
}
setLockFactory(lockFactory); // 设置当前FSDirectory相关锁工厂实例
if (doClearLockID) {
// Clear the prefix because write.lock will be
// stored in our directory:
lockFactory.setLockPrefix(null);
}
}
// 返回目录文件File directory下的所有索引文件,索引文件使用文件名的字符串描述
public String[] list() {
return directory.list(IndexFileNameFilter.getFilter());
}
// 如果文件name存在,则返回true
public boolean fileExists(String name) {
File file = new File(directory, name); // 根据File directory指定的抽象路径,以及name指定的名称创建一个文件
return file.exists();
}