public DataFileCache(Database db, String fileName, String backupName) throws HsqlException {

    this.fileName = fileName;
    this.backupFileName = backupName;
    database = db;
    fa = database.getFileAccess();

    initParams();

    cache = new Cache(this);
  }
  /** Writes out all the rows to a new file without fragmentation. */
  public void defrag() throws HsqlException {

    if (cacheReadonly) {
      return;
    }

    if (fileFreePosition == INITIAL_FREE_POS) {
      return;
    }

    try {
      boolean wasNio = dataFile.wasNio();
      DataFileDefrag dfd = new DataFileDefrag(database, this, fileName);

      dfd.process();
      close(false);
      Trace.printSystemOut("closed old cache");

      // first attemp to delete
      fa.removeElement(fileName);

      if (wasNio) {
        System.gc();

        if (fa.isStreamElement(fileName)) {
          fa.removeElement(fileName);

          if (fa.isStreamElement(fileName)) {
            fa.renameElement(fileName, fileName + ".old");

            File oldfile = new File(fileName + ".old");

            FileUtil.deleteOnExit(oldfile);
          }
        }
      }

      // [email protected] - change to file access api
      fa.renameElement(fileName + ".new", fileName);
      backup();
      database
          .getProperties()
          .setProperty("hsqldb.cache_version", HsqlDatabaseProperties.VERSION_STRING_1_7_0);
      database.getProperties().save();
      initParams();

      cache = new Cache(this);

      open(cacheReadonly);
      dfd.updateTableIndexRoots();
      Trace.printSystemOut("opened cache");
    } catch (Exception e) {
      database.logger.appLog.logContext(e);

      throw new HsqlException(e, Trace.getMessage(Trace.GENERAL_IO_ERROR), Trace.GENERAL_IO_ERROR);
      /*
      Trace.error(Trace.FILE_IO_ERROR, Trace.DataFileCache_defrag, new Object[] {
          e, fileName
      });
      */
    }
  }
  public DataFileCache(Database db, String baseFileName) throws HsqlException {

    initParams(db, baseFileName);

    cache = new Cache(this);
  }