Skip to content

Commit

Permalink
Add getFileInfoObject API in gcsio (#1160)
Browse files Browse the repository at this point in the history
  • Loading branch information
singhravidutt committed May 29, 2024
1 parent f39c78f commit 449cd8c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,9 @@ public void testOutputStreamIOStatistics() throws IOException {
@Override
public void testGetFileInfos() {}

@Override
public void testGetFileInfoObject() {}

@Override
public void testFileCreationSetsAttributes() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@ default List<FileInfo> listFileInfo(URI path) throws IOException {
*/
FileInfo getFileInfo(URI path) throws IOException;

/**
* Gets information about the given path item. Here path should be pointing to a gcs object and
* can't be a directory
*
* @param path The path we want information about.
* @return Information about the given path item.
*/
FileInfo getFileInfoObject(URI path) throws IOException;

/**
* Gets information about each path in the given list; more efficient than calling getFileInfo()
* on each path individually in a loop.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,20 @@ public FileInfo getFileInfo(URI path) throws IOException {
return fileInfo;
}

@Override
public FileInfo getFileInfoObject(URI path) throws IOException {
checkArgument(path != null, "path must not be null");
StorageResourceId resourceId = StorageResourceId.fromUriPath(path, true);
checkArgument(
!resourceId.isDirectory(),
String.format(
"path must be an object and not a directory, path: %s, resourceId: %s",
path, resourceId));
FileInfo fileInfo = FileInfo.fromItemInfo(gcs.getItemInfo(resourceId));
logger.atFiner().log("getFileInfoObject(path: %s): %s", path, fileInfo);
return fileInfo;
}

/**
* @see #getFileInfo(URI)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -226,6 +227,22 @@ protected void validateGetFileInfo(String bucketName, String objectName, boolean
validateFileInfoInternal(bucketName, objectName, expectedToExist, fileInfo);
}

protected void validateGetFileInfoObject(
String bucketName, String objectName, boolean expectedToExist) throws IOException {
URI path = gcsiHelper.getPath(bucketName, objectName);
if (StringPaths.isDirectoryPath(objectName)) {
IllegalArgumentException exception =
assertThrows(IllegalArgumentException.class, () -> gcsfs.getFileInfoObject(path));
assertThat(exception)
.hasMessageThat()
.containsMatch(Pattern.compile("path must be an object and not a directory"));
} else {
FileInfo fileInfo = gcsfs.getFileInfoObject(path);
assertThat(fileInfo.getPath()).isEqualTo(path);
validateFileInfoInternal(bucketName, objectName, expectedToExist, fileInfo);
}
}

/**
* Validates FileInfo returned by listFileInfo().
*
Expand Down Expand Up @@ -477,6 +494,32 @@ public void testGetAndListFileInfo() throws Exception {
null, null, /* expectedToExist= */ true, sharedBucketName1, sharedBucketName2, testBucket);
}

@Test
public void testGetFileInfoObject() throws Exception {
// Objects created for this test.
String[] objectNames = {"d1/c1"};

// -------------------------------------------------------
// Create test objects.
String testBucket = gcsiHelper.createUniqueBucket("list");
gcsiHelper.createObjectsWithSubdirs(testBucket, objectNames);

// -------------------------------------------------------
// Tests for getItemInfoObject().
// -------------------------------------------------------

// Verify that getItemInfoObject() returns correct info for each object.
for (String objectName : objectNames) {
validateGetFileInfoObject(testBucket, objectName, /* expectedToExist= */ true);
}

String[] pathDoesNotExist = {"/", "d1", "d1/"};

for (String objectName : pathDoesNotExist) {
validateGetFileInfoObject(testBucket, objectName, /* expectedToExist= */ false);
}
}

@Test
@SuppressWarnings("EqualsIncompatibleType")
public void testGoogleCloudStorageItemInfoNegativeEquality() {
Expand Down

0 comments on commit 449cd8c

Please sign in to comment.