Directory抽象类及其子类的总结 (2)

if (file.exists() && !file.isDirectory())    // 如果file存在并且不是一个目录文件,抛出异常
      throw new IOException(file + " not a directory");

if (!file.exists())    // 如果file不存在
      if (!file.mkdirs())    //   如果创建由file指定的路径名所指定的目录失败,则很可能已经存在,抛出异常
        throw new IOException("Cannot create directory: " + file);

FSDirectory dir;
    synchronized (DIRECTORIES) {
      dir = (FSDirectory)DIRECTORIES.get(file);
      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);
      } 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;
}

//   获取FSDirectory实例,该方法已经废弃
public static FSDirectory getDirectory(String path, boolean create)
      throws IOException {
    return getDirectory(new File(path), create);
}

//   获取FSDirectory实例,该方法已经废弃
public static FSDirectory getDirectory(File file, boolean create)
    throws IOException
{
    FSDirectory dir = getDirectory(file, null);

// This is now deprecated (creation should only be done
    // by IndexWriter):
    if (create) {
      dir.create();
    }

return dir;
}

private void create() throws IOException {
    if (directory.exists()) {    // File directory是FSDirectory类的一个成员
      String[] files = directory.list(IndexFileNameFilter.getFilter());            // 获取经过IndexFileNameFilter过滤后的该目录文件directory下的文件列表
      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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzjsjs.html