Skip to content

Commit

Permalink
Refactor Storage's delimiter support
Browse files Browse the repository at this point in the history
- Minor fixes to javadoc
- Favor firstNonNull over ternary operator when possible
- Move prefix-object transformer to static function
- Simplify StorageOptions equals method
  • Loading branch information
mziccard committed Mar 9, 2016
1 parent d930b4b commit de9b0d6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.gcloud.spi;

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.gcloud.spi.StorageRpc.Option.DELIMITER;
import static com.google.gcloud.spi.StorageRpc.Option.FIELDS;
import static com.google.gcloud.spi.StorageRpc.Option.IF_GENERATION_MATCH;
Expand Down Expand Up @@ -58,7 +59,6 @@
import com.google.api.services.storage.model.Objects;
import com.google.api.services.storage.model.StorageObject;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -167,24 +167,29 @@ public Tuple<String, Iterable<StorageObject>> list(final String bucket, Map<Opti
.setFields(FIELDS.getString(options))
.execute();
Iterable<StorageObject> storageObjects = Iterables.concat(
objects.getItems() != null ? objects.getItems() : ImmutableList.<StorageObject>of(),
firstNonNull(objects.getItems(), ImmutableList.<StorageObject>of()),
objects.getPrefixes() != null
? Lists.transform(objects.getPrefixes(), new Function<String, StorageObject>() {
@Override
public StorageObject apply(String prefix) {
return new StorageObject()
.set("isDirectory", true)
.setBucket(bucket)
.setName(prefix)
.setSize(BigInteger.ZERO);
}
}) : ImmutableList.<StorageObject>of());
? Lists.transform(objects.getPrefixes(), objectFromPrefix(bucket))
: ImmutableList.<StorageObject>of());
return Tuple.of(objects.getNextPageToken(), storageObjects);
} catch (IOException ex) {
throw translate(ex);
}
}

private static Function<String, StorageObject> objectFromPrefix(final String bucket) {
return new Function<String, StorageObject>() {
@Override
public StorageObject apply(String prefix) {
return new StorageObject()
.set("isDirectory", true)
.setBucket(bucket)
.setName(prefix)
.setSize(BigInteger.ZERO);
}
};
}

@Override
public Bucket get(Bucket bucket, Map<Option, ?> options) {
try {
Expand Down Expand Up @@ -549,7 +554,7 @@ public String open(StorageObject object, Map<Option, ?> options) {
HttpRequest httpRequest =
requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, object));
httpRequest.getHeaders().set("X-Upload-Content-Type",
MoreObjects.firstNonNull(object.getContentType(), "application/octet-stream"));
firstNonNull(object.getContentType(), "application/octet-stream"));
HttpResponse response = httpRequest.execute();
if (response.getStatusCode() != 200) {
GoogleJsonError error = new GoogleJsonError();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ public Long updateTime() {
/**
* Returns {@code true} if the current blob represents a directory. This can only happen if the
* blob is returned by {@link Storage#list(String, Storage.BlobListOption...)} when the
* {@link Storage.BlobListOption#currentDirectory()} option is used. If {@code true} only
* {@link Storage.BlobListOption#currentDirectory()} option is used. When this is the case only
* {@link #blobId()} and {@link #size()} are set for the current blob: {@link BlobId#name()} ends
* with the '/' character, {@link BlobId#generation()} returns {@code null} and {@link #size()} is
* {@code 0}.
Expand Down Expand Up @@ -785,7 +785,7 @@ public Acl apply(ObjectAccessControl objectAccessControl) {
}
}));
}
if (storageObject.get("isDirectory") != null) {
if (storageObject.containsKey("isDirectory")) {
builder.isDirectory(Boolean.TRUE);
}
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,14 +694,14 @@ public static BlobListOption prefix(String prefix) {
}

/**
* If specified, results are returned in a directory-like mode. Blobs whose names, aside from
* a possible {@link #prefix(String)}, do not contain the '/' delimiter are returned as is.
* Blobs whose names, aside from a possible {@link #prefix(String)}, contain the '/' delimiter,
* will have their name truncated after the delimiter and will be returned as {@link Blob}
* objects where only {@link Blob#blobId()}, {@link Blob#size()} and {@link Blob#isDirectory()}
* are set. For such directory blobs, ({@link BlobId#generation()} returns {@code null}),
* {@link Blob#size()} returns {@code 0} while {@link Blob#isDirectory()} returns {@code true}.
* Duplicate directory blobs are omitted.
* If specified, results are returned in a directory-like mode. Blobs whose names, after a
* possible {@link #prefix(String)}, do not contain the '/' delimiter are returned as is. Blobs
* whose names, after a possible {@link #prefix(String)}, contain the '/' delimiter, will have
* their name truncated after the delimiter and will be returned as {@link Blob} objects where
* only {@link Blob#blobId()}, {@link Blob#size()} and {@link Blob#isDirectory()} are set. For
* such directory blobs, ({@link BlobId#generation()} returns {@code null}), {@link Blob#size()}
* returns {@code 0} while {@link Blob#isDirectory()} returns {@code true}. Duplicate directory
* blobs are omitted.
*/
public static BlobListOption currentDirectory() {
return new BlobListOption(StorageRpc.Option.DELIMITER, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ public int hashCode() {

@Override
public boolean equals(Object obj) {
if (!(obj instanceof StorageOptions)) {
return false;
}
StorageOptions other = (StorageOptions) obj;
return baseEquals(other);
return obj instanceof StorageOptions && baseEquals((StorageOptions) obj);
}

public static Builder builder() {
Expand Down

0 comments on commit de9b0d6

Please sign in to comment.