Class UnixFSUtils

java.lang.Object
dev.getelements.elements.rt.transact.unix.UnixFSUtils

public class UnixFSUtils extends Object
A collection of useful utility routines when accessing the filesystem.
  • Field Details

  • Constructor Details

    • UnixFSUtils

      @Inject public UnixFSUtils(UnixFSChecksumAlgorithm checksumAlgorithm, @Named("dev.getelements.elements.rt.transact.unix.fs.root") Path storageRoot)
  • Method Details

    • initialize

      public void initialize()
      Initializes the directory contents for all the necessary sub directories.
    • doOperation

      public <T> T doOperation(UnixFSUtils.IOOperation<T> action)
      Performs and operation that may throw an instance of IOException, and re-throws it wrapped inside of a InternalException.
      Type Parameters:
      T - the return type
      Parameters:
      action - the action to perform
      Returns:
      the value returned from the UnixFSUtils.IOOperation<T>
    • doOperation

      public <T, ExceptionT extends InternalException> T doOperation(UnixFSUtils.IOOperation<T> action, Function<Throwable,ExceptionT> exceptionTFunction) throws ExceptionT
      Performs and operation that may throw an instance of IOException, and re-throws it wrapped inside of a InternalException.
      Type Parameters:
      T - the return type
      Parameters:
      action - the action to perform
      Returns:
      the value returned from the UnixFSUtils.IOOperation<T>
      Throws:
      ExceptionT
    • doOperationV

      public void doOperationV(UnixFSUtils.IOOperationV action)
      Performs an IO Operation which may throw, catching the exception and wrapping it in the type specified in the function.
      Parameters:
      action - the action to perfrom
    • doOperationV

      public <ExceptionT extends InternalException> void doOperationV(UnixFSUtils.IOOperationV action, Function<Throwable,ExceptionT> exceptionTFunction)
      Performs an IO Operation which may throw, catching the exception and wrapping it in the type specified in the function.
      Type Parameters:
      ExceptionT - the specified exception type
      Parameters:
      action - the action to perfrom
      exceptionTFunction - a supplier to constrcut the exception
    • getChecksumAlgorithm

      public UnixFSChecksumAlgorithm getChecksumAlgorithm()
      Gets the configured and preferred checksum algorithm.
      Returns:
      the UnixFSChecksumAlgorithm to use
    • getStorageRoot

      public Path getStorageRoot()
      Gets the storage root directory.
      Returns:
      the storage root
    • getNodeStorageRoot

      public Path getNodeStorageRoot()
      Gets the storage root for node storage.
      Returns:
      the Path
    • getGarbageDirectory

      public Path getGarbageDirectory()
      Gets the directory for temporary storge of garbage data.
      Returns:
      the garbage directory
    • getTransactionJournalFilePath

      public Path getTransactionJournalFilePath()
      Returns the path to the journal file.
      Returns:
      the path to the journal file.
    • getTransactionJournalDirectoryPath

      public Path getTransactionJournalDirectoryPath()
      Returns the path to the journal directory.
      Returns:
      the path to the journal file.
    • getTombstone

      public Path getTombstone()
      Gets the Path to the tombstone marker file.
      Returns:
      the Path to the tombstone file.
    • getLockFilePath

      public Path getLockFilePath()
      Gets the Path to the lock file.
      Returns:
      the Path to the lock file.
    • getPathSeparator

      public String getPathSeparator()
      Returns the path separator string associated with the filesystem used by the storage directory.
      Returns:
      the path separator.
    • check

      public Path check(Path path)
      Checks that the supplied Path is part of the datastore.
      Parameters:
      path - the Path
      Returns:
      the supplied Path unchanged
    • lockStorageRoot

      public void lockStorageRoot()
      Writes a lock file in the storage root, throwing a FatalException if there is already a lock file. This uses the File.deleteOnExit() facility to attempt to unlock the directoy regardless of exit, however this uses shutdown hooks and is not guaranteed.
    • unlockStorageRoot

      public void unlockStorageRoot()
      Deletes the lock file in the storage root, throwing a FatalException if the was a problem unlocking.
    • resolveNodeStorageRoot

      public Path resolveNodeStorageRoot(HasNodeId hasNodeId)
      Returns a Path to the path storage directory for the supplied NodeId.
      Parameters:
      hasNodeId - the HasNodeId
      Returns:
      the Path for the HasNodeId
    • resolvePathStorageRoot

      public Path resolvePathStorageRoot(HasNodeId hasNodeId)
      Returns a Path to the path storage directory for the supplied NodeId.
      Parameters:
      hasNodeId - the HasNodeId
      Returns:
      the Path for the HasNodeId
    • resolveTaskStorageRoot

      public Path resolveTaskStorageRoot(HasNodeId hasNodeId)
      Returns a Path to the task storage directory for the supplied NodeId.
      Returns:
      the Path for the NodeId
    • resolveResourceStorageRoot

      public Path resolveResourceStorageRoot(HasNodeId hasNodeId)
      Returns a Path to the resource storage directory for the supplied NodeId.
      Returns:
      the Path for the NodeId
    • resolveReversePathStorageRoot

      public Path resolveReversePathStorageRoot(HasNodeId hasNodeId)
      Resolves the reverse paths storage directory.
      Returns:
      the Path to the directory.
    • markTombstone

      public void markTombstone(Path path)
      Marks the supplied Path as a tombstone.
      Parameters:
      path - the path to mark
    • isRegularFile

      public boolean isRegularFile(UnixFSHasFilesystemPath hasFilesystemPath)
      Checks if the supplied FS Path is a regular file and not a tombstone.
      Parameters:
      hasFilesystemPath - if the path is a regular file.
      Returns:
      true if regular file
    • isRegularFile

      public boolean isRegularFile(Path path)
      Checks if the supplied Path is a regular file.
      Parameters:
      path - the path to check
      Returns:
      true if the path is a tombstone, false otherwise
    • isTombstone

      public boolean isTombstone(Path path)
      Checks if the supplied Path is a tombstone.
      Parameters:
      path - the path to check
      Returns:
      true if the path is a tombstone, false otherwise
    • rmrf

      public void rmrf(Path path)
      Recursively removes the object at the supplied Path.
      Parameters:
      path - the path.
    • prune

      public void prune(Path path)
      Prune the supplied Path. This will delete all empty directories and subdirectories of the path provided that they are empty.
      Parameters:
      path - the path to prune
    • getTransactionFilePath

      public Path getTransactionFilePath(String transactionId)
      Allocates a journal file.
      Parameters:
      transactionId - the transaction ID
      Returns:
      the Path to the journal file
    • allocateGarbageDirectory

      public Path allocateGarbageDirectory()
      Allocates a directory in the garbage directory.
      Returns:
      the Path to the file.
    • cleanupGarbage

      public void cleanupGarbage()
      Cleans up any garbage in the garbage directory.
    • cleanupGarbage

      public void cleanupGarbage(Path garbageDirectory)
      Cleans up the garbage in the supplied Path.
      Parameters:
      garbageDirectory - the garbage to clean up
    • commit

      public UnixFSUtils.CommitResult commit(UnixFSHasFilesystemPath hasFilesystemPath, String transactionId)
      Commits the supplied file, provided the transaction ID.
      Parameters:
      hasFilesystemPath - a mapping of a type which has a filesystem path
      transactionId - the transaction ID
    • cleanup

      public void cleanup(UnixFSHasFilesystemPath hasFilesystemPath, String transactionId)
      Cleans up the object with the transaction ID.
      Parameters:
      hasFilesystemPath - a mapping of a type which has a filesystem path
      transactionId - the transaction ID
    • appendExtension

      public Path appendExtension(Path path, String extension)
      Appends the file extension to the supplied path.
      Parameters:
      path - the base path
      extension - the extension to append
      Returns:
      the fully-formed path
    • isMatchingExtension

      public boolean isMatchingExtension(Path path, String extension)
      Checks if the path matches the supplied extension.
      Parameters:
      path - the Path
      extension - the extension
      Returns:
      true if matches, otherwise false
    • stripExtension

      public Path stripExtension(Path path)
      Returns a Path with the extension stripped. The extension must be one of the known extensions in this class, or else this will throw an instance of IllegalArgumentException.
      Parameters:
      path - the path
      Returns:
      the path, with file extension stripped.
    • appendTaskExtension

      public Path appendTaskExtension(Path path)
      Appends the TASK_EXTENSION extension to the supplied path.
      Parameters:
      path - the base path
      Returns:
      the fully-formed path
    • appendResourceExtension

      public Path appendResourceExtension(Path path)
      Appends the RESOURCE_EXTENSION extension to the supplied path.
      Parameters:
      path - the base path
      Returns:
      the fully-formed path
    • appendReversePathExtension

      public Path appendReversePathExtension(Path path)
      Appends the REVERSE_PATH_EXTENSION extension to the supplied path.
      Parameters:
      path - the base path
      Returns:
      the fully-formed path
    • list

      public Stream<Path> list(Path directory)
      Safely lists the contents of the supplied directory. If the list process encounters a missing file, for example if it had been deleted by the garbage collector, then the stream will simply skip that file. This ensures that it will be possible to work only with live fields when iterating the contents of a directory. Internally this uses a spliterator and lazily fetches as needed. Otherwise, this must behave in a manner identical to Files.list(Path)
      Parameters:
      directory - the directory to list
      Returns:
      a Stream<Path>
    • walk

      public Stream<Path> walk(Path directory)
      Safely lists the contents of the supplied directory as well as its children. If the walk process encounters a missing file, for example if it had been deleted by the garbage collector, then the stream will simply skip that file. This ensures that it will be possible to work only with live fields when iterating the contents of a directory. Internally this uses a spliterator and lazily fetches as needed. Otherwise this must behave in a manner identical to Files.walk(Path, FileVisitOption...)
      Parameters:
      directory - the directory to list
      Returns:
      a Stream<Path>