Skip to content

Commit

Permalink
Update logic for dealing with open filesystems
Browse files Browse the repository at this point in the history
  • Loading branch information
kraefrei committed Aug 1, 2022
1 parent bf4c1b7 commit 2b41d87
Showing 1 changed file with 10 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,16 @@ class BlobPathBuilder(credential: AzureSasCredential, container: String, endpoin
def build(string: String): Try[BlobPath] = {
validateBlobPath(string, container, endpoint) match {
case ValidBlobPath(path) =>
Try {
val fileSystem = Try {
FileSystems.getFileSystem(new URI("azb://?endpoint=" + endpoint))
} recover {
// If no filesystem already exists, this will create a new connection, with the provided configs
case _: FileSystemNotFoundException => FileSystems.newFileSystem(new URI("azb://?endpoint=" + endpoint), fileSystemConfig.asJava)
}

val blobStoragePath = fileSystem.map(_.getPath(path))
// If there is a remaining issue reaching the filesystem, this will rethrow the exception
blobStoragePath.map(BlobPath(_, endpoint, container)).get
}
for {
fileSystem <- Try {
FileSystems.getFileSystem(new URI("azb://?endpoint=" + endpoint))
} recover {
// If no filesystem already exists, this will create a new connection, with the provided configs
case _: FileSystemNotFoundException => FileSystems.newFileSystem(new URI("azb://?endpoint=" + endpoint), fileSystemConfig.asJava)
}
nioPath <- Try {fileSystem.getPath(path)}
blobPath <- Try {BlobPath(nioPath, endpoint, container)}

This comment has been minimized.

Copy link
@jgainerdewar

jgainerdewar Aug 1, 2022

Collaborator

No need to wrap everything in Try, the thing we were talking about where everything needs to be Try is about the things you flatMap (<-) being Try as opposed to some other flatMappable thing. For calls that won't throw an error, you can instead do:

blobPath = BlobPath(nioPath, endpoint, container)

Also, braces are necessary for multi-line expressions but for a single line it's preferred to use parens:

nioPath <- Try(fileSystem.getPath(path))
} yield blobPath
case UnparsableBlobPath(errorMessage: Throwable) => Failure(errorMessage)
}
}
Expand Down

0 comments on commit 2b41d87

Please sign in to comment.