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

Make storage functional objects subclasses of metadata objects. #614

Merged
merged 6 commits into from
Feb 5, 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ Here is a code snippet showing a simple usage example from within Compute/App En
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.gcloud.storage.Blob;
import com.google.gcloud.storage.BlobInfo;
import com.google.gcloud.storage.BlobId;
import com.google.gcloud.storage.Storage;
import com.google.gcloud.storage.StorageOptions;
Expand All @@ -257,7 +258,7 @@ import java.nio.channels.WritableByteChannel;

Storage storage = StorageOptions.defaultInstance().service();
BlobId blobId = BlobId.of("bucket", "blob_name");
Blob blob = Blob.get(storage, blobId);
Blob blob = storage.get(blobId);
if (blob == null) {
BlobInfo blobInfo = BlobInfo.builder(blobId).contentType("text/plain").build();
storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.google.gcloud.storage.BlobId;
import com.google.gcloud.storage.BlobInfo;
import com.google.gcloud.storage.Bucket;
import com.google.gcloud.storage.BucketInfo;
import com.google.gcloud.storage.CopyWriter;
import com.google.gcloud.storage.Storage;
import com.google.gcloud.storage.Storage.ComposeRequest;
Expand Down Expand Up @@ -133,27 +132,27 @@ public void run(Storage storage, BlobId... blobIds) {
if (blobIds.length == 1) {
if (blobIds[0].name().isEmpty()) {
// get Bucket
Bucket bucket = Bucket.get(storage, blobIds[0].bucket());
Bucket bucket = storage.get(blobIds[0].bucket());
if (bucket == null) {
System.out.println("No such bucket");
return;
}
System.out.println("Bucket info: " + bucket.info());
System.out.println("Bucket info: " + bucket);
} else {
// get Blob
Blob blob = Blob.get(storage, blobIds[0]);
Blob blob = storage.get(blobIds[0]);
if (blob == null) {
System.out.println("No such object");
return;
}
System.out.println("Blob info: " + blob.info());
System.out.println("Blob info: " + blob);
}
} else {
// use batch to get multiple blobs.
List<Blob> blobs = Blob.get(storage, Arrays.asList(blobIds));
List<Blob> blobs = storage.get(blobIds);
for (Blob blob : blobs) {
if (blob != null) {
System.out.println(blob.info());
System.out.println(blob);
}
}
}
Expand Down Expand Up @@ -184,7 +183,7 @@ private static class DeleteAction extends BlobsAction {
@Override
public void run(Storage storage, BlobId... blobIds) {
// use batch operation
List<Boolean> deleteResults = Blob.delete(storage, blobIds);
List<Boolean> deleteResults = storage.delete(blobIds);
int index = 0;
for (Boolean deleted : deleteResults) {
if (deleted) {
Expand Down Expand Up @@ -218,20 +217,20 @@ String parse(String... args) {
public void run(Storage storage, String bucketName) {
if (bucketName == null) {
// list buckets
Iterator<BucketInfo> bucketInfoIterator = storage.list().iterateAll();
while (bucketInfoIterator.hasNext()) {
System.out.println(bucketInfoIterator.next());
Iterator<Bucket> bucketIterator = storage.list().iterateAll();
while (bucketIterator.hasNext()) {
System.out.println(bucketIterator.next());
}
} else {
// list a bucket's blobs
Bucket bucket = Bucket.get(storage, bucketName);
Bucket bucket = storage.get(bucketName);
if (bucket == null) {
System.out.println("No such bucket");
return;
}
Iterator<Blob> blobIterator = bucket.list().iterateAll();
while (blobIterator.hasNext()) {
System.out.println(blobIterator.next().info());
System.out.println(blobIterator.next());
}
}
}
Expand All @@ -257,8 +256,7 @@ private void run(Storage storage, Path uploadFrom, BlobInfo blobInfo) throws IOE
if (Files.size(uploadFrom) > 1_000_000) {
// When content is not available or large (1MB or more) it is recommended
// to write it in chunks via the blob's channel writer.
Blob blob = new Blob(storage, blobInfo);
try (WriteChannel writer = blob.writer()) {
try (WriteChannel writer = storage.writer(blobInfo)) {
byte[] buffer = new byte[1024];
try (InputStream input = Files.newInputStream(uploadFrom)) {
int limit;
Expand Down Expand Up @@ -311,7 +309,7 @@ public void run(Storage storage, Tuple<BlobId, Path> tuple) throws IOException {
}

private void run(Storage storage, BlobId blobId, Path downloadTo) throws IOException {
Blob blob = Blob.get(storage, blobId);
Blob blob = storage.get(blobId);
if (blob == null) {
System.out.println("No such object");
return;
Expand All @@ -320,7 +318,7 @@ private void run(Storage storage, BlobId blobId, Path downloadTo) throws IOExcep
if (downloadTo != null) {
writeTo = new PrintStream(new FileOutputStream(downloadTo.toFile()));
}
if (blob.info().size() < 1_000_000) {
if (blob.size() < 1_000_000) {
// Blob is small read all its content in one request
byte[] content = blob.content();
writeTo.write(content);
Expand Down Expand Up @@ -438,13 +436,13 @@ public void run(Storage storage, Tuple<BlobId, Map<String, String>> tuple)
}

private void run(Storage storage, BlobId blobId, Map<String, String> metadata) {
Blob blob = Blob.get(storage, blobId);
Blob blob = storage.get(blobId);
if (blob == null) {
System.out.println("No such object");
return;
}
Blob updateBlob = blob.update(blob.info().toBuilder().metadata(metadata).build());
System.out.println("Updated " + updateBlob.info());
Blob updateBlob = blob.toBuilder().metadata(metadata).build().update();
System.out.println("Updated " + updateBlob);
}

@Override
Expand Down Expand Up @@ -488,9 +486,8 @@ public void run(Storage storage, Tuple<ServiceAccountAuthCredentials, BlobInfo>
run(storage, tuple.x(), tuple.y());
}

private void run(Storage storage, ServiceAccountAuthCredentials cred, BlobInfo blobInfo)
throws IOException {
Blob blob = new Blob(storage, blobInfo);
private void run(Storage storage, ServiceAccountAuthCredentials cred, BlobInfo blobInfo) {
Blob blob = storage.get(blobInfo.blobId());
System.out.println("Signed URL: "
+ blob.signUrl(1, TimeUnit.DAYS, SignUrlOption.serviceAccount(cred)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@ public class Project extends ProjectInfo {
private final ResourceManagerOptions options;
private transient ResourceManager resourceManager;

/**
* Builder for {@code Project}.
*/
public static class Builder extends ProjectInfo.Builder {
private final ResourceManager resourceManager;
private ProjectInfo.BuilderImpl infoBuilder;

Builder(ResourceManager resourceManager) {
this.resourceManager = resourceManager;
this.infoBuilder = new ProjectInfo.BuilderImpl();
}
private final ProjectInfo.BuilderImpl infoBuilder;

This comment was marked as spam.


Builder(Project project) {
this.resourceManager = project.resourceManager;
Expand Down Expand Up @@ -124,16 +122,6 @@ public Project build() {
this.options = resourceManager.options();
}

/**
* Constructs a Project object that contains project information got from the server.
*
* @return Project object containing the project's metadata or {@code null} if not found
* @throws ResourceManagerException upon failure
*/
public static Project get(ResourceManager resourceManager, String projectId) {
return resourceManager.get(projectId);
}

/**
* Returns the {@link ResourceManager} service object associated with this Project.
*/
Expand All @@ -142,14 +130,14 @@ public ResourceManager resourceManager() {
}

/**
* Fetches the current project's latest information. Returns {@code null} if the job does not
* Fetches the project's latest information. Returns {@code null} if the project does not
* exist.
*
* @return Project containing the project's updated metadata or {@code null} if not found
* @throws ResourceManagerException upon failure
*/
public Project reload() {
return Project.get(resourceManager, projectId());
return resourceManager.get(projectId());

This comment was marked as spam.

}

/**
Expand Down Expand Up @@ -210,10 +198,6 @@ public Project replace() {
return resourceManager.replace(this);
}

static Builder builder(ResourceManager resourceManager, String projectId) {
return new Builder(resourceManager).projectId(projectId);
}

@Override
public Builder toBuilder() {
return new Builder(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ static ResourceId fromPb(
}
}

public static abstract class Builder {
/**

This comment was marked as spam.

* Builder for {@code ProjectInfo}.
*/
public abstract static class Builder {

/**
* Set the user-assigned name of the project.
Expand Down Expand Up @@ -184,7 +187,9 @@ static class BuilderImpl extends Builder {
private Long createTimeMillis;
private ResourceId parent;

BuilderImpl() {}
BuilderImpl(String projectId) {
this.projectId = projectId;
}

BuilderImpl(ProjectInfo info) {
this.name = info.name;
Expand Down Expand Up @@ -331,7 +336,7 @@ public Long createTimeMillis() {

@Override
public boolean equals(Object obj) {
return obj.getClass().equals(ProjectInfo.class)
return obj != null && obj.getClass().equals(ProjectInfo.class)
&& Objects.equals(toPb(), ((ProjectInfo) obj).toPb());
}

Expand All @@ -341,7 +346,7 @@ public int hashCode() {
}

public static Builder builder(String id) {
return new BuilderImpl().projectId(id);
return new BuilderImpl(id);
}

public Builder toBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

import com.google.common.collect.ImmutableMap;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Map;
Expand All @@ -54,6 +54,11 @@ public class ProjectTest {
private Project expectedProject;
private Project project;

@Before
public void setUp() {
resourceManager = createStrictMock(ResourceManager.class);
}

@After
public void tearDown() throws Exception {
verify(resourceManager);
Expand All @@ -62,7 +67,6 @@ public void tearDown() throws Exception {
private void initializeExpectedProject(int optionsCalls) {
expect(serviceMockReturnsOptions.options()).andReturn(mockOptions).times(optionsCalls);
replay(serviceMockReturnsOptions);
resourceManager = createStrictMock(ResourceManager.class);
expectedProject =
new Project(serviceMockReturnsOptions, new ProjectInfo.BuilderImpl(PROJECT_INFO));
}
Expand All @@ -71,39 +75,41 @@ private void initializeProject() {
project = new Project(resourceManager, new ProjectInfo.BuilderImpl(PROJECT_INFO));
}

@Test
public void testToBuilder() {
initializeExpectedProject(4);
replay(resourceManager);
compareProjects(expectedProject, expectedProject.toBuilder().build());
}

@Test
public void testBuilder() {
initializeExpectedProject(2);
initializeExpectedProject(4);
expect(resourceManager.options()).andReturn(mockOptions).times(4);
replay(resourceManager);
Project builtProject = Project.builder(serviceMockReturnsOptions, PROJECT_ID)
.name(NAME)
Project.Builder builder =
new Project.Builder(new Project(resourceManager, new ProjectInfo.BuilderImpl(PROJECT_ID)));
Project project = builder.name(NAME)
.labels(LABELS)
.projectNumber(PROJECT_NUMBER)
.createTimeMillis(CREATE_TIME_MILLIS)
.state(STATE)
.build();
assertEquals(PROJECT_ID, builtProject.projectId());
assertEquals(NAME, builtProject.name());
assertEquals(LABELS, builtProject.labels());
assertEquals(PROJECT_NUMBER, builtProject.projectNumber());
assertEquals(CREATE_TIME_MILLIS, builtProject.createTimeMillis());
assertEquals(STATE, builtProject.state());
assertSame(serviceMockReturnsOptions, builtProject.resourceManager());
}

@Test
public void testToBuilder() {
initializeExpectedProject(4);
replay(resourceManager);
compareProjects(expectedProject, expectedProject.toBuilder().build());
assertEquals(PROJECT_ID, project.projectId());
assertEquals(NAME, project.name());
assertEquals(LABELS, project.labels());
assertEquals(PROJECT_NUMBER, project.projectNumber());
assertEquals(CREATE_TIME_MILLIS, project.createTimeMillis());
assertEquals(STATE, project.state());
assertEquals(resourceManager.options(), project.resourceManager().options());
}

@Test
public void testGet() {
initializeExpectedProject(1);
expect(resourceManager.get(PROJECT_INFO.projectId())).andReturn(expectedProject);
replay(resourceManager);
Project loadedProject = Project.get(resourceManager, PROJECT_INFO.projectId());
Project loadedProject = resourceManager.get(PROJECT_INFO.projectId());
assertEquals(expectedProject, loadedProject);
}

Expand All @@ -126,7 +132,7 @@ public void testLoadNull() {
initializeExpectedProject(1);
expect(resourceManager.get(PROJECT_INFO.projectId())).andReturn(null);
replay(resourceManager);
assertNull(Project.get(resourceManager, PROJECT_INFO.projectId()));
assertNull(resourceManager.get(PROJECT_INFO.projectId()));
}

@Test
Expand Down
Loading