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

Include prefixes when listing blobs with field selector #933

Merged
merged 1 commit into from
Apr 15, 2016
Merged
Show file tree
Hide file tree
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
40 changes: 29 additions & 11 deletions gcloud-java-core/src/main/java/com/google/cloud/FieldSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public interface FieldSelector {
*/
class Helper {

private static final String[] EMPTY_FIELDS = {};

private Helper() {}

private static final Function<FieldSelector, String> FIELD_TO_STRING_FUNCTION =
Expand All @@ -63,33 +65,49 @@ private static String selector(List<? extends FieldSelector> required, FieldSele
}

/**
* Returns a composite selector given a number of fields. The string selector returned by this
* method can be used for field selection in API calls that return a single resource. This
* method is not supposed to be used directly by users.
* Returns a composite selector given a number of resource fields. The string selector returned
* by this method can be used for field selection in API calls that return a single resource.
* This method is not supposed to be used directly by users.
*/
public static String selector(List<? extends FieldSelector> required, FieldSelector... others) {
return selector(required, others, new String[]{});
}

/**
* Returns a composite selector given a number of fields and a container name. The string
* selector returned by this method can be used for field selection in API calls that return a
* list of resources. This method is not supposed to be used directly by users.
* Returns a composite selector given a number of resource fields and a container name. The
* string selector returned by this method can be used for field selection in API calls that
* return a list of resources. This method is not supposed to be used directly by users.
*/
public static String listSelector(String containerName, List<? extends FieldSelector> required,
FieldSelector... others) {
return "nextPageToken," + containerName + '(' + selector(required, others) + ')';
}

/**
* Returns a composite selector given a number of fields and a container name. This methods also
* takes an {@code extraResourceFields} parameter to specify some extra fields as strings. The
* string selector returned by this method can be used for field selection in API calls that
* return a list of resources. This method is not supposed to be used directly by users.
* Returns a composite selector given a number of resource fields and a container name. This
* method also takes an {@code extraResourceFields} parameter to specify some extra resource
* fields as strings. The string selector returned by this method can be used for field
* selection in API calls that return a list of resources. This method is not supposed to be
* used directly by users.
*/
public static String listSelector(String containerName, List<? extends FieldSelector> required,
FieldSelector[] others, String... extraResourceFields) {
return "nextPageToken," + containerName + '('
return listSelector(EMPTY_FIELDS, containerName, required, others, extraResourceFields);
}

/**
* Returns a composite selector given a number of top level fields as strings, a number of
* resource fields and a container name. This method also takes an {@code extraResourceFields}
* parameter to specify some extra resource fields as strings. The string selector returned by
* this method can be used for field selection in API calls that return a list of resources.
* This method is not supposed to be used directly by users.
*/
public static String listSelector(String[] topLevelFields, String containerName,
List<? extends FieldSelector> required, FieldSelector[] others,
String... extraResourceFields) {
Set<String> topLevelStrings = Sets.newHashSetWithExpectedSize(topLevelFields.length + 1);
topLevelStrings.addAll(Lists.asList("nextPageToken", topLevelFields));
return Joiner.on(',').join(topLevelStrings) + "," + containerName + '('
+ selector(required, others, extraResourceFields) + ')';
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public String selector() {
return "field3";
}
};
private static final String[] FIRST_LEVEL_FIELDS = {"firstLevel1", "firstLevel2"};
private static final List<FieldSelector> REQUIRED_FIELDS = ImmutableList.of(FIELD1, FIELD2);
private static final String CONTAINER = "container";

Expand Down Expand Up @@ -81,4 +82,20 @@ public void testListSelectorWithExtraFields() {
assertTrue(selector.endsWith(")"));
assertEquals(52, selector.length());
}

@Test
public void testListSelectorWithFirstLevelFields() {
String selector = Helper.listSelector(FIRST_LEVEL_FIELDS, CONTAINER, REQUIRED_FIELDS,
new FieldSelector[]{FIELD3}, "field4");
assertTrue(selector.contains("firstLevel1"));
assertTrue(selector.contains("firstLevel2"));
assertTrue(selector.contains("nextPageToken"));
assertTrue(selector.contains("container("));
assertTrue(selector.contains("field1"));
assertTrue(selector.contains("field2"));
assertTrue(selector.contains("field3"));
assertTrue(selector.contains("field4"));
assertTrue(selector.endsWith(")"));
assertEquals(76, selector.length());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ public static BucketListOption fields(BucketField... fields) {
*/
class BlobListOption extends Option {

private static final String[] TOP_LEVEL_FIELDS = {"prefixes"};
private static final long serialVersionUID = 9083383524788661294L;

private BlobListOption(StorageRpc.Option option, Object value) {
Expand Down Expand Up @@ -713,7 +714,7 @@ public static BlobListOption versions(boolean versions) {
*/
public static BlobListOption fields(BlobField... fields) {
return new BlobListOption(StorageRpc.Option.FIELDS,
Helper.listSelector("items", BlobField.REQUIRED_FIELDS, fields));
Helper.listSelector(TOP_LEVEL_FIELDS, "items", BlobField.REQUIRED_FIELDS, fields));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,12 +584,13 @@ public void testListBucketsWithSelectedFields() {
initializeService();
ImmutableList<Bucket> bucketList = ImmutableList.of(expectedBucket1, expectedBucket2);
Page<Bucket> page = storage.list(BUCKET_LIST_FIELDS);
String selector = (String) capturedOptions.getValue().get(BLOB_LIST_FIELDS.rpcOption());
assertTrue(selector.contains("items"));
String selector = (String) capturedOptions.getValue().get(BUCKET_LIST_FIELDS.rpcOption());
assertTrue(selector.contains("items("));
assertTrue(selector.contains("name"));
assertTrue(selector.contains("acl"));
assertTrue(selector.contains("location"));
assertTrue(selector.contains("nextPageToken"));
assertTrue(selector.endsWith(")"));
assertEquals(38, selector.length());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(bucketList.toArray(), Iterables.toArray(page.values(), Bucket.class));
Expand All @@ -607,10 +608,11 @@ public void testListBucketsWithEmptyFields() {
initializeService();
ImmutableList<Bucket> bucketList = ImmutableList.of(expectedBucket1, expectedBucket2);
Page<Bucket> page = storage.list(BUCKET_LIST_EMPTY_FIELDS);
String selector = (String) capturedOptions.getValue().get(BLOB_LIST_FIELDS.rpcOption());
assertTrue(selector.contains("items"));
String selector = (String) capturedOptions.getValue().get(BUCKET_LIST_EMPTY_FIELDS.rpcOption());
assertTrue(selector.contains("items("));
assertTrue(selector.contains("name"));
assertTrue(selector.contains("nextPageToken"));
assertTrue(selector.endsWith(")"));
assertEquals(25, selector.length());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(bucketList.toArray(), Iterables.toArray(page.values(), Bucket.class));
Expand Down Expand Up @@ -679,13 +681,15 @@ public void testListBlobsWithSelectedFields() {
assertEquals(BLOB_LIST_PREFIX.value(),
capturedOptions.getValue().get(BLOB_LIST_PREFIX.rpcOption()));
String selector = (String) capturedOptions.getValue().get(BLOB_LIST_FIELDS.rpcOption());
assertTrue(selector.contains("items"));
assertTrue(selector.contains("prefixes"));
assertTrue(selector.contains("items("));
assertTrue(selector.contains("bucket"));
assertTrue(selector.contains("name"));
assertTrue(selector.contains("contentType"));
assertTrue(selector.contains("md5Hash"));
assertTrue(selector.contains("nextPageToken"));
assertEquals(52, selector.length());
assertTrue(selector.endsWith(")"));
assertEquals(61, selector.length());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(blobList.toArray(), Iterables.toArray(page.values(), Blob.class));
}
Expand All @@ -710,11 +714,13 @@ public void testListBlobsWithEmptyFields() {
assertEquals(BLOB_LIST_PREFIX.value(),
capturedOptions.getValue().get(BLOB_LIST_PREFIX.rpcOption()));
String selector = (String) capturedOptions.getValue().get(BLOB_LIST_EMPTY_FIELDS.rpcOption());
assertTrue(selector.contains("items"));
assertTrue(selector.contains("prefixes"));
assertTrue(selector.contains("items("));
assertTrue(selector.contains("bucket"));
assertTrue(selector.contains("name"));
assertTrue(selector.contains("nextPageToken"));
assertEquals(32, selector.length());
assertTrue(selector.endsWith(")"));
assertEquals(41, selector.length());
assertEquals(cursor, page.nextPageCursor());
assertArrayEquals(blobList.toArray(), Iterables.toArray(page.values(), Blob.class));
}
Expand Down