Class UnixFSUtils
java.lang.Object
dev.getelements.elements.rt.transact.unix.UnixFSUtils
A collection of useful utility routines when accessing the filesystem.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
A result for thecommit(UnixFSHasFilesystemPath, String)
operation.static interface
Defines an operation which may throw an instance ofIOException
static interface
Defines an operation which may throw an instance ofIOException
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Pattern
static final String
static final String
static final String
static final String
static final String
static final String
static final String
static final String
static final String
static final String
static final String
static final String
static final int
static final String
static final String
static final String
static final String
static final String
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAllocates a directory in the garbage directory.appendExtension
(Path path, String extension) Appends the file extension to the supplied path.appendResourceExtension
(Path path) Appends theRESOURCE_EXTENSION
extension to the supplied path.Appends theREVERSE_PATH_EXTENSION
extension to the supplied path.appendTaskExtension
(Path path) Appends theTASK_EXTENSION
extension to the supplied path.Checks that the suppliedPath
is part of the datastore.void
cleanup
(UnixFSHasFilesystemPath hasFilesystemPath, String transactionId) Cleans up the object with the transaction ID.void
Cleans up any garbage in the garbage directory.void
cleanupGarbage
(Path garbageDirectory) Cleans up the garbage in the suppliedPath
.commit
(UnixFSHasFilesystemPath hasFilesystemPath, String transactionId) Commits the supplied file, provided the transaction ID.<T> T
doOperation
(UnixFSUtils.IOOperation<T> action) Performs and operation that may throw an instance ofIOException
, and re-throws it wrapped inside of aInternalException
.<T,
ExceptionT extends InternalException>
TdoOperation
(UnixFSUtils.IOOperation<T> action, Function<Throwable, ExceptionT> exceptionTFunction) Performs and operation that may throw an instance ofIOException
, and re-throws it wrapped inside of aInternalException
.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.<ExceptionT extends InternalException>
voiddoOperationV
(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.Gets the configured and preferred checksum algorithm.Gets the directory for temporary storge of garbage data.Gets thePath
to the lock file.Gets the storage root for node storage.Returns the path separator string associated with the filesystem used by the storage directory.Gets the storage root directory.Gets thePath
to the tombstone marker file.getTransactionFilePath
(String transactionId) Allocates a journal file.Returns the path to the journal directory.Returns the path to the journal file.void
Initializes the directory contents for all the necessary sub directories.boolean
isMatchingExtension
(Path path, String extension) Checks if the path matches the supplied extension.boolean
isRegularFile
(UnixFSHasFilesystemPath hasFilesystemPath) Checks if the supplied FS Path is a regular file and not a tombstone.boolean
isRegularFile
(Path path) Checks if the suppliedPath
is a regular file.boolean
isTombstone
(Path path) Checks if the suppliedPath
is a tombstone.Safely lists the contents of the supplied directory.void
Writes a lock file in the storage root, throwing aFatalException
if there is already a lock file.void
markTombstone
(Path path) Marks the suppliedPath
as a tombstone.void
Prune the suppliedPath
.resolveNodeStorageRoot
(HasNodeId hasNodeId) resolvePathStorageRoot
(HasNodeId hasNodeId) resolveResourceStorageRoot
(HasNodeId hasNodeId) resolveReversePathStorageRoot
(HasNodeId hasNodeId) Resolves the reverse paths storage directory.resolveTaskStorageRoot
(HasNodeId hasNodeId) void
Recursively removes the object at the suppliedPath
.stripExtension
(Path path) Returns aPath
with the extension stripped.void
Deletes the lock file in the storage root, throwing aFatalException
if the was a problem unlocking.Safely lists the contents of the supplied directory as well as its children.
-
Field Details
-
UNIXFS_STORAGE_ROOT_DIRECTORY
- See Also:
-
LOCK_FILE_NAME
- See Also:
-
TOMBSTONE_FILE_NAME
- See Also:
-
TRANSACTION_JOURNAL_FILE_NAME
- See Also:
-
TRANSACTION_JOURNAL_DIRECTORY
- See Also:
-
NODE_DIRECTORY
- See Also:
-
GARBAGE_DIRECTORY
- See Also:
-
PATHS_DIRECTORY
- See Also:
-
TASKS_DIRECTORY
- See Also:
-
RESOURCES_DIRECTORY
- See Also:
-
REVERSE_PATHS_DIRECTORY
- See Also:
-
TEMP_NAME_LENGTH_CHARS
public static final int TEMP_NAME_LENGTH_CHARS- See Also:
-
TASK_EXTENSION
- See Also:
-
RESOURCE_EXTENSION
- See Also:
-
REVERSE_PATH_EXTENSION
- See Also:
-
TRANSACTION_EXTENSION
- See Also:
-
EXTENSION_REGEX
-
EXTENSION_PATTERN
-
TEMP_FILE_CHARACTERS
- See Also:
-
-
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
Performs and operation that may throw an instance ofIOException
, and re-throws it wrapped inside of aInternalException
.- 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 ExceptionTPerforms and operation that may throw an instance ofIOException
, and re-throws it wrapped inside of aInternalException
.- Type Parameters:
T
- the return type- Parameters:
action
- the action to perform- Returns:
- the value returned from the
UnixFSUtils.IOOperation<T>
- Throws:
ExceptionT
-
doOperationV
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 perfromexceptionTFunction
- a supplier to constrcut the exception
-
getChecksumAlgorithm
Gets the configured and preferred checksum algorithm.- Returns:
- the
UnixFSChecksumAlgorithm
to use
-
getStorageRoot
Gets the storage root directory.- Returns:
- the storage root
-
getNodeStorageRoot
Gets the storage root for node storage.- Returns:
- the
Path
-
getGarbageDirectory
Gets the directory for temporary storge of garbage data.- Returns:
- the garbage directory
-
getTransactionJournalFilePath
Returns the path to the journal file.- Returns:
- the path to the journal file.
-
getTransactionJournalDirectoryPath
Returns the path to the journal directory.- Returns:
- the path to the journal file.
-
getTombstone
Gets thePath
to the tombstone marker file.- Returns:
- the
Path
to the tombstone file.
-
getLockFilePath
Gets thePath
to the lock file.- Returns:
- the
Path
to the lock file.
-
getPathSeparator
Returns the path separator string associated with the filesystem used by the storage directory.- Returns:
- the path separator.
-
check
Checks that the suppliedPath
is part of the datastore. -
lockStorageRoot
public void lockStorageRoot()Writes a lock file in the storage root, throwing aFatalException
if there is already a lock file. This uses theFile.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 aFatalException
if the was a problem unlocking. -
resolveNodeStorageRoot
-
resolvePathStorageRoot
-
resolveTaskStorageRoot
-
resolveResourceStorageRoot
-
resolveReversePathStorageRoot
Resolves the reverse paths storage directory.- Returns:
- the
Path
to the directory.
-
markTombstone
Marks the suppliedPath
as a tombstone.- Parameters:
path
- the path to mark
-
isRegularFile
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
Checks if the suppliedPath
is a regular file.- Parameters:
path
- the path to check- Returns:
- true if the path is a tombstone, false otherwise
-
isTombstone
Checks if the suppliedPath
is a tombstone.- Parameters:
path
- the path to check- Returns:
- true if the path is a tombstone, false otherwise
-
rmrf
Recursively removes the object at the suppliedPath
.- Parameters:
path
- the path.
-
prune
Prune the suppliedPath
. This will delete all empty directories and subdirectories of the path provided that they are empty.- Parameters:
path
- the path to prune
-
getTransactionFilePath
Allocates a journal file.- Parameters:
transactionId
- the transaction ID- Returns:
- the
Path
to the journal file
-
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
Cleans up the garbage in the suppliedPath
.- 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 pathtransactionId
- the transaction ID
-
cleanup
Cleans up the object with the transaction ID.- Parameters:
hasFilesystemPath
- a mapping of a type which has a filesystem pathtransactionId
- the transaction ID
-
appendExtension
Appends the file extension to the supplied path.- Parameters:
path
- the base pathextension
- the extension to append- Returns:
- the fully-formed path
-
isMatchingExtension
Checks if the path matches the supplied extension.- Parameters:
path
- thePath
extension
- the extension- Returns:
- true if matches, otherwise false
-
stripExtension
Returns aPath
with the extension stripped. The extension must be one of the known extensions in this class, or else this will throw an instance ofIllegalArgumentException
.- Parameters:
path
- the path- Returns:
- the path, with file extension stripped.
-
appendTaskExtension
Appends theTASK_EXTENSION
extension to the supplied path.- Parameters:
path
- the base path- Returns:
- the fully-formed path
-
appendResourceExtension
Appends theRESOURCE_EXTENSION
extension to the supplied path.- Parameters:
path
- the base path- Returns:
- the fully-formed path
-
appendReversePathExtension
Appends theREVERSE_PATH_EXTENSION
extension to the supplied path.- Parameters:
path
- the base path- Returns:
- the fully-formed path
-
list
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 toFiles.list(Path)
-
walk
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 toFiles.walk(Path, FileVisitOption...)
-