Class FileSyncImpl

java.lang.Object
org.aksw.commons.txn.impl.FileSyncImpl
All Implemented Interfaces:
ContentSync, FileSync, TxnComponent

public class FileSyncImpl extends Object implements FileSync
Protocol implementation to create/read/update/(delete) data with syncing to a file in a transaction-safe way.
  • Field Details

    • targetFile

      protected Path targetFile
    • newContentFile

      protected Path newContentFile
    • newContentTmpFile

      protected Path newContentTmpFile
    • oldContentFile

      protected Path oldContentFile
    • deleteTargetFileOnUpdateWithEmptyContent

      protected boolean deleteTargetFileOnUpdateWithEmptyContent
      Whether to delete 'targetFile' if the new content is empty
    • TO_BASENAME

      public static final Pattern TO_BASENAME
  • Constructor Details

    • FileSyncImpl

      public FileSyncImpl(Path targetFile, Path oldContentFile, Path newContentFile, boolean deleteTargetFileOnUpdateWithEmptyContent)
  • Method Details

    • create

      public static FileSyncImpl create(Path path, boolean deleteTargetFileOnUpdateWithEmptyContent)
    • getBaseName

      public static String getBaseName(String str)
      Return the base name for a file name that is has special meaning w.r.t. syncing. For example, getBaseName("myfile.sync.old") yields "myfile". Useful for iterating directory content and getting the view of "effective" file names with those special sync files hidden.
    • getTargetFile

      public Path getTargetFile()
      Description copied from interface: FileSync
      Get the path to the file affected by the sync
      Specified by:
      getTargetFile in interface FileSync
    • getOldContentPath

      public Path getOldContentPath()
      Description copied from interface: FileSync
      Get the path to the file that contains the original content
      Specified by:
      getOldContentPath in interface FileSync
    • getCurrentPath

      public Path getCurrentPath()
      Description copied from interface: FileSync
      Get the path to file with the current content - can be the original file or the temp file
      Specified by:
      getCurrentPath in interface FileSync
    • openCurrentContent

      public InputStream openCurrentContent() throws IOException
      Description copied from interface: ContentSync
      Read the most recent content (maybe open the original file). Beware that calling
      invalid reference
      #newOutputStreamToNewTmpContent()
      may practically invalidate the returned input stream.
      Specified by:
      openCurrentContent in interface ContentSync
      Throws:
      IOException
    • newOutputStreamToNewTmpContent

      public OutputStream newOutputStreamToNewTmpContent(boolean truncate) throws IOException
      Description copied from interface: ContentSync
      Open an output stream to the tmp content. If the new content is currently a symlink then it gets deleted and replaced with a regular file
      Specified by:
      newOutputStreamToNewTmpContent in interface ContentSync
      Throws:
      IOException
    • exists

      public boolean exists()
      Description copied from interface: ContentSync
      Returns true iff there is at least one backing resource with the old or new state
      Specified by:
      exists in interface ContentSync
    • getLastModifiedTime

      public Instant getLastModifiedTime() throws IOException
      Description copied from interface: ContentSync
      Returns null if not exists
      Specified by:
      getLastModifiedTime in interface ContentSync
      Throws:
      IOException
    • putContent

      public void putContent(Consumer<OutputStream> outputStreamSupplier) throws IOException
      Set the new content of a resource. The new content is not committed.
      Specified by:
      putContent in interface ContentSync
      Parameters:
      outputStreamSupplier -
      Throws:
      IOException
    • preCommit

      public void preCommit() throws IOException
      Replace the new content with the current temporary content
      Specified by:
      preCommit in interface TxnComponent
      Throws:
      IOException
    • finalizeCommit

      public void finalizeCommit() throws IOException
      Specified by:
      finalizeCommit in interface TxnComponent
      Throws:
      IOException
    • rollback

      public void rollback() throws IOException
      Specified by:
      rollback in interface TxnComponent
      Throws:
      IOException
    • getNewContentTmpFile

      public Path getNewContentTmpFile()
      Description copied from interface: FileSync
      Get the file that represents the new content
      Specified by:
      getNewContentTmpFile in interface FileSync
    • getLastModifiedTimeAsInstant

      public static Instant getLastModifiedTimeAsInstant(Path path)
    • getState

      public static PathDiffState getState(FileSync fileSync)