Esempio n. 1
0
  /**
   * Empties the log, discarding all records it contains.
   *
   * <p>All cursors open on the log are aborted.
   *
   * @throws ChangelogException If cursors are opened on this log, or if a problem occurs during
   *     clearing operation.
   */
  public void clear() throws ChangelogException {
    exclusiveLock.lock();
    try {
      if (isClosed) {
        return;
      }
      if (!openCursors.isEmpty()) {
        // All open cursors are aborted, which means the change number indexer thread
        // should manage AbortedChangelogCursorException specifically to avoid being
        // stopped
        abortAllOpenCursors();
      }

      // delete all log files
      final List<String> undeletableFiles = new ArrayList<>();
      for (LogFile<K, V> logFile : logFiles.values()) {
        try {
          logFile.close();
          logFile.delete();
        } catch (ChangelogException e) {
          undeletableFiles.add(logFile.getFile().getPath());
        }
      }
      if (!undeletableFiles.isEmpty()) {
        throw new ChangelogException(
            ERR_CHANGELOG_UNABLE_TO_DELETE_LOG_FILE.get(
                Utils.joinAsString(", ", undeletableFiles)));
      }
      logFiles.clear();

      // recreate an empty head log file
      openHeadLogFile();
    } catch (Exception e) {
      throw new ChangelogException(
          ERR_ERROR_CLEARING_DB.get(logPath.getPath(), stackTraceToSingleLineString(e)));
    } finally {
      exclusiveLock.unlock();
    }
  }