Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] code inspection improvements #260

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 38 additions & 22 deletions src/main/java/software/amazon/nio/spi/s3/S3FileSystemProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public void close() {

@Override
public Iterator<Path> iterator() {
return iterator;
return Objects.requireNonNull(iterator);
}
};
}
Expand Down Expand Up @@ -535,16 +535,7 @@ public FileStore getFileStore(Path path) {
public void checkAccess(Path path, AccessMode... modes) throws IOException {
try {
final S3Path s3Path = checkPath(path.toRealPath(NOFOLLOW_LINKS));
final S3FileSystem fs = s3Path.getFileSystem();
final String bucketName = fs.bucketName();
final S3AsyncClient s3Client = fs.client();

final CompletableFuture<? extends S3Response> response;
if (s3Path.equals(s3Path.getRoot())) {
response = s3Client.headBucket(request -> request.bucket(bucketName));
} else {
response = s3Client.headObject(req -> req.bucket(bucketName).key(s3Path.getKey()));
}
final CompletableFuture<? extends S3Response> response = getCompletableFutureForHead(s3Path);

long timeOut = TimeOutUtils.TIMEOUT_TIME_LENGTH_1;
TimeUnit unit = MINUTES;
Expand Down Expand Up @@ -579,6 +570,20 @@ public void checkAccess(Path path, AccessMode... modes) throws IOException {
}
}

private CompletableFuture<? extends S3Response> getCompletableFutureForHead(S3Path s3Path) {
final S3FileSystem fs = s3Path.getFileSystem();
final String bucketName = fs.bucketName();
final S3AsyncClient s3Client = fs.client();

final CompletableFuture<? extends S3Response> response;
if (s3Path.equals(s3Path.getRoot())) {
response = s3Client.headBucket(request -> request.bucket(bucketName));
} else {
response = s3Client.headObject(req -> req.bucket(bucketName).key(s3Path.getKey()));
}
return response;
}

/**
* Returns a file attribute view of a given type. This method works in
* exactly the manner specified by the {@link Files#getFileAttributeView}
Expand Down Expand Up @@ -728,18 +733,27 @@ S3FileSystem forUri(URI uri, S3FileSystemInfo info){
void closeFileSystem(FileSystem fs) {
for (String key: cache.keySet()) {
if (fs == cache.get(key)) {
cache.remove(key); return;
try (FileSystem closeable = cache.remove(key)) {
closeFileSystemIfOpen(closeable);
return;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
try {
if(fs.isOpen()) {
fs.close();
}
closeFileSystemIfOpen(fs);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private void closeFileSystemIfOpen(FileSystem fs) throws IOException {
if(fs.isOpen()){
fs.close();
}
}


boolean exists(S3AsyncClient s3Client, S3Path path) throws InterruptedException, TimeoutException {
try {
Expand Down Expand Up @@ -788,7 +802,9 @@ private Iterator<Path> pathIteratorForPublisher (
try {
return filter.accept(path);
} catch (IOException e) {
e.printStackTrace();
logger.warn("An IOException was thrown while filtering the path: {}." +
" Set log level to debug to show stack trace", path);
logger.debug(e.getMessage(), e);
return false;
}
}).collect(Collectors.toList());
Expand All @@ -801,23 +817,23 @@ private Iterator<Path> pathIteratorForPublisher (
/**
* This method parses the provided URI into elements useful to address
* and configure the access to a bucket. These are:
*
* <br>
* - key: the file system key that can be used to uniquely identify a S3
* file systems instance (for example for caching purposes)
* - bucket: the name of the bucked to be addressed
* - endpoint: non default endpoint where the bucket is located
*
* <br>
* The default implementation in {@code S3FileSystemProvider} treats {@code uri}
* strictly a AWS S3 URI (see Accessing a bucket using S3:// section in
* https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-bucket-intro.html).
* strictly a AWS S3 URI (see Accessing a bucket using S3:// section <a href="in
">* https://docs.aws.amazon.com/AmazonS3/latest/userguide/access-bucket-</a>intro.html).
* As such, it returns an empty endpoint and the name of the bucket as key.
*
* <br>
* Subclasses can override this method to implement alternative parsing of
* the provided URI so that they can implement alternative URI schemes.
*
* @param uri the uri to address the bucket
*
* @return the information estracted from {@code uri}
* @return the information extracted from {@code uri}
*/
S3FileSystemInfo fileSystemInfo(URI uri) {
return new S3FileSystemInfo(uri);
Expand Down