Skip to content

Commit 6ff5e1e

Browse files
committed
---
yaml --- r: 6175 b: refs/heads/tswast-patch-1 c: a6ccc30 h: refs/heads/master i: 6173: 51f1a8b 6171: cd23c67 6167: 4920fb6 6159: 11e54c3 6143: b81efd6
1 parent 24f65d0 commit 6ff5e1e

9 files changed

Lines changed: 491 additions & 14 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: 00ec9564ba161ad92183bfcb980e3168e89ac24c
60+
refs/heads/tswast-patch-1: a6ccc302d9b9280940cbcdf788c4a8caa808697b
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b

branches/tswast-patch-1/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreTest.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
import org.junit.AfterClass;
4242
import org.junit.Before;
4343
import org.junit.BeforeClass;
44+
import org.junit.Rule;
4445
import org.junit.Test;
46+
import org.junit.rules.ExpectedException;
4547
import org.junit.runner.RunWith;
4648
import org.junit.runners.JUnit4;
4749

@@ -103,6 +105,9 @@ public class DatastoreTest {
103105
private static LocalGcdHelper gcdHelper;
104106
private static final int PORT = LocalGcdHelper.findAvailablePort(LocalGcdHelper.DEFAULT_PORT);
105107

108+
@Rule
109+
public ExpectedException thrown = ExpectedException.none();
110+
106111
@BeforeClass
107112
public static void beforeClass() throws IOException, InterruptedException {
108113
if (!LocalGcdHelper.isActive(PROJECT_ID, PORT)) {
@@ -636,7 +641,7 @@ public void testKeyFactory() {
636641
}
637642

638643
@Test
639-
public void testRetires() throws Exception {
644+
public void testRetryableException() throws Exception {
640645
DatastoreV1.LookupRequest requestPb =
641646
DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build();
642647
DatastoreV1.LookupResponse responsePb = DatastoreV1.LookupResponse.newBuilder()
@@ -658,4 +663,51 @@ public void testRetires() throws Exception {
658663
assertEquals(ENTITY1, entity);
659664
EasyMock.verify(rpcFactoryMock, rpcMock);
660665
}
666+
667+
@Test
668+
public void testNonRetryableException() throws Exception {
669+
DatastoreV1.LookupRequest requestPb =
670+
DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build();
671+
DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class);
672+
DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class);
673+
EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class)))
674+
.andReturn(rpcMock);
675+
EasyMock.expect(rpcMock.lookup(requestPb))
676+
.andThrow(new DatastoreRpc.DatastoreRpcException(Reason.PERMISSION_DENIED))
677+
.times(1);
678+
EasyMock.replay(rpcFactoryMock, rpcMock);
679+
RetryParams retryParams = RetryParams.builder().retryMinAttempts(2).build();
680+
DatastoreOptions options = this.options.toBuilder()
681+
.retryParams(retryParams)
682+
.serviceRpcFactory(rpcFactoryMock)
683+
.build();
684+
Datastore datastore = DatastoreFactory.instance().get(options);
685+
thrown.expect(DatastoreException.class);
686+
thrown.expectMessage(Reason.PERMISSION_DENIED.description());
687+
datastore.get(KEY1);
688+
EasyMock.verify(rpcFactoryMock, rpcMock);
689+
}
690+
691+
@Test
692+
public void testRuntimeException() throws Exception {
693+
DatastoreV1.LookupRequest requestPb =
694+
DatastoreV1.LookupRequest.newBuilder().addKey(KEY1.toPb()).build();
695+
DatastoreRpcFactory rpcFactoryMock = EasyMock.createStrictMock(DatastoreRpcFactory.class);
696+
DatastoreRpc rpcMock = EasyMock.createStrictMock(DatastoreRpc.class);
697+
EasyMock.expect(rpcFactoryMock.create(EasyMock.anyObject(DatastoreOptions.class)))
698+
.andReturn(rpcMock);
699+
String exceptionMessage = "Artificial runtime exception";
700+
EasyMock.expect(rpcMock.lookup(requestPb))
701+
.andThrow(new RuntimeException(exceptionMessage));
702+
EasyMock.replay(rpcFactoryMock, rpcMock);
703+
DatastoreOptions options = this.options.toBuilder()
704+
.retryParams(RetryParams.getDefaultInstance())
705+
.serviceRpcFactory(rpcFactoryMock)
706+
.build();
707+
Datastore datastore = DatastoreFactory.instance().get(options);
708+
thrown.expect(DatastoreException.class);
709+
thrown.expectMessage(exceptionMessage);
710+
datastore.get(KEY1);
711+
EasyMock.verify(rpcFactoryMock, rpcMock);
712+
}
661713
}

branches/tswast-patch-1/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.google.api.client.http.HttpResponse;
4444
import com.google.api.client.http.HttpResponseException;
4545
import com.google.api.client.http.HttpTransport;
46+
import com.google.api.client.http.InputStreamContent;
4647
import com.google.api.client.http.json.JsonHttpContent;
4748
import com.google.api.client.json.JsonFactory;
4849
import com.google.api.client.json.jackson.JacksonFactory;
@@ -63,6 +64,7 @@
6364

6465
import java.io.ByteArrayOutputStream;
6566
import java.io.IOException;
67+
import java.io.InputStream;
6668
import java.util.ArrayList;
6769
import java.util.List;
6870
import java.util.Map;
@@ -119,13 +121,14 @@ public Bucket create(Bucket bucket, Map<Option, ?> options) throws StorageExcept
119121
}
120122

121123
@Override
122-
public StorageObject create(StorageObject storageObject, final byte[] content,
124+
public StorageObject create(StorageObject storageObject, final InputStream content,
123125
Map<Option, ?> options) throws StorageException {
124126
try {
125-
return storage.objects()
127+
Storage.Objects.Insert insert = storage.objects()
126128
.insert(storageObject.getBucket(), storageObject,
127-
new ByteArrayContent(storageObject.getContentType(), content))
128-
.setProjection(DEFAULT_PROJECTION)
129+
new InputStreamContent(storageObject.getContentType(), content));
130+
insert.getMediaHttpUploader().setDirectUploadEnabled(true);
131+
return insert.setProjection(DEFAULT_PROJECTION)
129132
.setPredefinedAcl(PREDEFINED_ACL.getString(options))
130133
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options))
131134
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options))
@@ -521,4 +524,3 @@ public String open(StorageObject object, Map<Option, ?> options)
521524
}
522525
}
523526
}
524-

branches/tswast-patch-1/gcloud-java-storage/src/main/java/com/google/gcloud/spi/StorageRpc.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.common.collect.ImmutableMap;
2323
import com.google.gcloud.storage.StorageException;
2424

25+
import java.io.InputStream;
2526
import java.util.List;
2627
import java.util.Map;
2728

@@ -128,7 +129,7 @@ public BatchResponse(Map<StorageObject, Tuple<Boolean, StorageException>> delete
128129

129130
Bucket create(Bucket bucket, Map<Option, ?> options) throws StorageException;
130131

131-
StorageObject create(StorageObject object, byte[] content, Map<Option, ?> options)
132+
StorageObject create(StorageObject object, InputStream content, Map<Option, ?> options)
132133
throws StorageException;
133134

134135
Tuple<String, Iterable<Bucket>> list(Map<Option, ?> options) throws StorageException;

branches/tswast-patch-1/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriterChannelImpl.java renamed to branches/tswast-patch-1/gcloud-java-storage/src/main/java/com/google/gcloud/storage/BlobWriteChannelImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
/**
3333
* Default implementation for BlobWriteChannel.
3434
*/
35-
class BlobWriterChannelImpl implements BlobWriteChannel {
35+
class BlobWriteChannelImpl implements BlobWriteChannel {
3636

3737
private static final long serialVersionUID = 8675286882724938737L;
3838
private static final int MIN_CHUNK_SIZE = 256 * 1024;
@@ -50,12 +50,12 @@ class BlobWriterChannelImpl implements BlobWriteChannel {
5050
private transient StorageRpc storageRpc;
5151
private transient StorageObject storageObject;
5252

53-
BlobWriterChannelImpl(StorageOptions options, BlobInfo blobInfo,
53+
BlobWriteChannelImpl(StorageOptions options, BlobInfo blobInfo,
5454
Map<StorageRpc.Option, ?> optionsMap) {
5555
this.options = options;
5656
this.blobInfo = blobInfo;
5757
initTransients();
58-
uploadId = options.storageRpc().open(storageObject, optionsMap);
58+
uploadId = storageRpc.open(storageObject, optionsMap);
5959
}
6060

6161
private void writeObject(ObjectOutputStream out) throws IOException {

branches/tswast-patch-1/gcloud-java-storage/src/main/java/com/google/gcloud/storage/Storage.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.gcloud.Service;
2626
import com.google.gcloud.spi.StorageRpc;
2727

28+
import java.io.InputStream;
2829
import java.io.Serializable;
2930
import java.net.URL;
3031
import java.util.Arrays;
@@ -493,13 +494,31 @@ public static Builder builder() {
493494
BucketInfo create(BucketInfo bucketInfo, BucketTargetOption... options);
494495

495496
/**
496-
* Create a new blob.
497+
* Create a new blob with no content.
498+
*
499+
* @return a complete blob information.
500+
* @throws StorageException upon failure
501+
*/
502+
BlobInfo create(BlobInfo blobInfo, BlobTargetOption... options);
503+
504+
/**
505+
* Create a new blob. Direct upload is used to upload {@code content}. For large content,
506+
* {@link #writer} is recommended as it uses resumable upload.
497507
*
498508
* @return a complete blob information.
499509
* @throws StorageException upon failure
500510
*/
501511
BlobInfo create(BlobInfo blobInfo, byte[] content, BlobTargetOption... options);
502512

513+
/**
514+
* Create a new blob. Direct upload is used to upload {@code content}. For large content,
515+
* {@link #writer} is recommended as it uses resumable upload.
516+
*
517+
* @return a complete blob information.
518+
* @throws StorageException upon failure
519+
*/
520+
BlobInfo create(BlobInfo blobInfo, InputStream content, BlobTargetOption... options);
521+
503522
/**
504523
* Return the requested bucket or {@code null} if not found.
505524
*

branches/tswast-patch-1/gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import com.google.gcloud.spi.StorageRpc;
5050
import com.google.gcloud.spi.StorageRpc.Tuple;
5151

52+
import java.io.ByteArrayInputStream;
53+
import java.io.InputStream;
5254
import java.io.Serializable;
5355
import java.io.UnsupportedEncodingException;
5456
import java.net.MalformedURLException;
@@ -112,14 +114,27 @@ public com.google.api.services.storage.model.Bucket call() {
112114
}, options().retryParams(), EXCEPTION_HANDLER));
113115
}
114116

117+
@Override
118+
public BlobInfo create(BlobInfo blobInfo, BlobTargetOption... options) {
119+
return create(blobInfo, new ByteArrayInputStream(EMPTY_BYTE_ARRAY), options);
120+
}
121+
115122
@Override
116123
public BlobInfo create(BlobInfo blobInfo, final byte[] content, BlobTargetOption... options) {
124+
return create(blobInfo,
125+
new ByteArrayInputStream(firstNonNull(content, EMPTY_BYTE_ARRAY)), options);
126+
}
127+
128+
@Override
129+
public BlobInfo create(BlobInfo blobInfo, final InputStream content,
130+
BlobTargetOption... options) {
117131
final StorageObject blobPb = blobInfo.toPb();
118132
final Map<StorageRpc.Option, ?> optionsMap = optionMap(blobInfo, options);
119133
return BlobInfo.fromPb(runWithRetries(new Callable<StorageObject>() {
120134
@Override
121135
public StorageObject call() {
122-
return storageRpc.create(blobPb, firstNonNull(content, EMPTY_BYTE_ARRAY), optionsMap);
136+
return storageRpc.create(blobPb,
137+
firstNonNull(content, new ByteArrayInputStream(EMPTY_BYTE_ARRAY)), optionsMap);
123138
}
124139
}, options().retryParams(), EXCEPTION_HANDLER));
125140
}
@@ -443,7 +458,7 @@ public BlobReadChannel reader(String bucket, String blob, BlobSourceOption... op
443458
@Override
444459
public BlobWriteChannel writer(BlobInfo blobInfo, BlobTargetOption... options) {
445460
final Map<StorageRpc.Option, ?> optionsMap = optionMap(blobInfo, options);
446-
return new BlobWriterChannelImpl(options(), blobInfo, optionsMap);
461+
return new BlobWriteChannelImpl(options(), blobInfo, optionsMap);
447462
}
448463

449464
@Override

0 commit comments

Comments
 (0)