/**
   * Parameter write indicates either an orderly close, or a fast close without backup.
   *
   * <p>When false, just closes the file.
   *
   * <p>When true, writes out all cached rows that have been modified and the free position pointer
   * for the *.data file and then closes the file.
   */
  public void close(boolean write) throws HsqlException {

    try {
      if (cacheReadonly) {
        dataFile.close();

        return;
      }

      StopWatch sw = new StopWatch();

      if (write) {
        cache.saveAll();
        Trace.printSystemOut("saveAll: " + sw.elapsedTime());

        // set empty
        dataFile.seek(LONG_EMPTY_SIZE);
        dataFile.writeLong(freeBlocks.getLostBlocksSize());

        // set end
        dataFile.seek(LONG_FREE_POS_POS);
        dataFile.writeLong(fileFreePosition);

        // set saved flag;
        dataFile.seek(FLAGS_POS);

        int flag = BitMap.set(0, FLAG_ISSAVED);

        if (hasRowInfo) {
          flag = BitMap.set(flag, FLAG_ROWINFO);
        }

        dataFile.writeInt(flag);

        //
        dataFile.seek(fileFreePosition);
        Trace.printSystemOut("pos and flags: " + sw.elapsedTime());
      }

      if (dataFile != null) {
        dataFile.close();

        dataFile = null;

        Trace.printSystemOut("close: " + sw.elapsedTime());
      }

      boolean empty = fileFreePosition == INITIAL_FREE_POS;

      if (empty) {
        fa.removeElement(fileName);
        fa.removeElement(backupFileName);
      }
    } catch (Exception e) {
      database.logger.appLog.logContext(e);

      throw Trace.error(Trace.FILE_IO_ERROR, Trace.DataFileCache_close, new Object[] {e, fileName});
    }
  }
  void postClose(boolean keep) throws HsqlException {

    SimpleLog appLog = database.logger.appLog;

    if (cacheReadonly) {
      return;
    }

    try {
      appLog.sendLine(SimpleLog.LOG_NORMAL, "DataFileCache.postClose(" + keep + ") : start");

      if (keep) {
        database
            .getProperties()
            .setProperty(
                HsqlDatabaseProperties.hsqldb_cache_version,
                HsqlDatabaseProperties.VERSION_STRING_1_7_0);
        database.getProperties().save();
        appLog.sendLine(SimpleLog.LOG_NORMAL, "DataFileCache.postClose() : save props");

        if (fileModified) {
          backup();
        }
      } else {
        fa.removeElement(backupFileName);
        appLog.sendLine(SimpleLog.LOG_NORMAL, "DataFileCache.postClose() : delete backup");
        deleteOrResetFreePos(database, fileName);
        appLog.sendLine(SimpleLog.LOG_NORMAL, "DataFileCache.postClose() : delete file");
      }
    } catch (IOException e) {
      throw new HsqlException(e, Trace.getMessage(Trace.GENERAL_IO_ERROR), Trace.GENERAL_IO_ERROR);
    }
  }
  /**
   * Saves the *.data file as compressed *.backup.
   *
   * @throws HsqlException
   */
  public void backup() throws IOException {

    try {
      ZipUnzipFile.compressFile(fileName, backupFileName + ".new", database.getFileAccess());
      fa.renameElement(backupFileName + ".new", backupFileName);
    } catch (IOException e) {
      database.logger.appLog.logContext(e);

      throw e;
    }
  }
  void postClose(boolean keep) throws HsqlException {

    if (cacheReadonly) {
      return;
    }

    try {
      if (keep) {
        database
            .getProperties()
            .setProperty("hsqldb.cache_version", HsqlDatabaseProperties.VERSION_STRING_1_7_0);
        database.getProperties().save();

        if (fileModified) {
          backup();
        }
      } else {
        fa.removeElement(backupFileName);
        deleteOrResetFreePos(database, fileName);
      }
    } catch (IOException e) {
      throw new HsqlException(e, Trace.getMessage(Trace.GENERAL_IO_ERROR), Trace.GENERAL_IO_ERROR);
    }
  }
  /** 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
      });
      */
    }
  }
  /**
   * Opens the *.data file for this cache, setting the variables that allow access to the particular
   * database version of the *.data file.
   */
  public void open(boolean readonly) throws HsqlException {

    fileFreePosition = 0;

    try {
      boolean preexists = database.isFilesInJar();
      long freesize = 0;

      if (!preexists && fa.isStreamElement(fileName)) {
        if (database.isStoredFileAccess()) {
          preexists = true;
        } else {

          // discard "empty" databases
          File f = new File(fileName);

          preexists = f.length() > INITIAL_FREE_POS;
        }
      }

      if (preexists) {
        String version =
            database.getProperties().getProperty(HsqlDatabaseProperties.hsqldb_cache_version);
        boolean v17 = HsqlDatabaseProperties.VERSION_STRING_1_7_0.equals(version);

        // for later versions
        boolean v18 = HsqlDatabaseProperties.VERSION_STRING_1_8_0.equals(version);

        if (!v17) {
          throw Trace.error(Trace.WRONG_DATABASE_FILE_VERSION);
        }
      }

      boolean isNio = database.getProperties().isPropertyTrue("hsqldb.nio_data_file");
      int fileType = isNio ? ScaledRAFile.DATA_FILE_NIO : ScaledRAFile.DATA_FILE_RAF;

      if (database.isFilesInJar()) {
        fileType = ScaledRAFile.DATA_FILE_JAR;
      }

      // [email protected] - change to file access api
      String cname = database.getURLProperties().getProperty("storage_class_name");
      String skey = database.getURLProperties().getProperty("storage_key");

      dataFile = ScaledRAFile.newScaledRAFile(fileName, readonly, fileType, cname, skey);

      if (preexists) {
        dataFile.seek(FLAGS_POS);

        int flags = dataFile.readInt();

        hasRowInfo = BitMap.isSet(flags, FLAG_ROWINFO);

        dataFile.seek(LONG_EMPTY_SIZE);

        freesize = dataFile.readLong();

        dataFile.seek(LONG_FREE_POS_POS);

        fileFreePosition = dataFile.readLong();

        if (fileFreePosition < INITIAL_FREE_POS) {
          fileFreePosition = INITIAL_FREE_POS;
        }
      } else {
        fileFreePosition = INITIAL_FREE_POS;
      }

      resetBuffers();

      fileModified = false;
      freeBlocks = new DataFileBlockManager(FREE_BLOCKS_COUNT, cacheFileScale, freesize);
    } catch (Exception e) {
      database.logger.appLog.logContext(e);
      close(false);

      throw Trace.error(Trace.FILE_IO_ERROR, Trace.DataFileCache_open, new Object[] {e, fileName});
    }
  }
  /**
   * Parameter write indicates either an orderly close, or a fast close without backup.
   *
   * <p>When false, just closes the file.
   *
   * <p>When true, writes out all cached rows that have been modified and the free position pointer
   * for the *.data file and then closes the file.
   */
  public void close(boolean write) throws HsqlException {

    SimpleLog appLog = database.logger.appLog;

    try {
      if (cacheReadonly) {
        if (dataFile != null) {
          dataFile.close();
        }

        return;
      }

      StopWatch sw = new StopWatch();

      appLog.sendLine(SimpleLog.LOG_NORMAL, "DataFileCache.close(" + write + ") : start");

      if (write) {
        cache.saveAll();
        Trace.printSystemOut("saveAll: " + sw.elapsedTime());
        appLog.sendLine(SimpleLog.LOG_NORMAL, "DataFileCache.close() : save data");

        if (fileModified || freeBlocks.isModified()) {

          // set empty
          dataFile.seek(LONG_EMPTY_SIZE);
          dataFile.writeLong(freeBlocks.getLostBlocksSize());

          // set end
          dataFile.seek(LONG_FREE_POS_POS);
          dataFile.writeLong(fileFreePosition);

          // set saved flag;
          dataFile.seek(FLAGS_POS);

          int flag = BitMap.set(0, FLAG_ISSAVED);

          if (hasRowInfo) {
            flag = BitMap.set(flag, FLAG_ROWINFO);
          }

          dataFile.writeInt(flag);
          appLog.sendLine(SimpleLog.LOG_NORMAL, "DataFileCache.close() : flags");

          //
          if (dataFile.length() != fileFreePosition) {
            dataFile.seek(fileFreePosition);
          }

          appLog.sendLine(SimpleLog.LOG_NORMAL, "DataFileCache.close() : seek end");
          Trace.printSystemOut("pos and flags: " + sw.elapsedTime());
        }
      }

      if (dataFile != null) {
        dataFile.close();
        appLog.sendLine(SimpleLog.LOG_NORMAL, "DataFileCache.close() : close");

        dataFile = null;

        Trace.printSystemOut("close: " + sw.elapsedTime());
      }

      boolean empty = fileFreePosition == INITIAL_FREE_POS;

      if (empty) {
        fa.removeElement(fileName);
        fa.removeElement(backupFileName);
      }
    } catch (Throwable e) {
      appLog.logContext(e);

      throw Trace.error(Trace.FILE_IO_ERROR, Trace.DataFileCache_close, new Object[] {e, fileName});
    }
  }