Skip to content

Commit 3311969

Browse files
committed
Set blob generation before sending requests in DefaultStorageRpc
1 parent f0ce897 commit 3311969

2 files changed

Lines changed: 43 additions & 16 deletions

File tree

gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ private Storage.Objects.Get getRequest(StorageObject object, Map<Option, ?> opti
207207
throws IOException {
208208
return storage.objects()
209209
.get(object.getBucket(), object.getName())
210+
.setGeneration(object.getGeneration())
210211
.setProjection(DEFAULT_PROJECTION)
211212
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options))
212213
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options))
@@ -288,6 +289,7 @@ private Storage.Objects.Delete deleteRequest(StorageObject blob, Map<Option, ?>
288289
throws IOException {
289290
return storage.objects()
290291
.delete(blob.getBucket(), blob.getName())
292+
.setGeneration(blob.getGeneration())
291293
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options))
292294
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options))
293295
.setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options))
@@ -332,6 +334,7 @@ public byte[] load(StorageObject from, Map<Option, ?> options)
332334
try {
333335
Storage.Objects.Get getRequest = storage.objects()
334336
.get(from.getBucket(), from.getName())
337+
.setGeneration(from.getGeneration())
335338
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options))
336339
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options))
337340
.setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options))
@@ -409,8 +412,10 @@ public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
409412
public byte[] read(StorageObject from, Map<Option, ?> options, long position, int bytes)
410413
throws StorageException {
411414
try {
412-
Get req = storage.objects().get(from.getBucket(), from.getName());
413-
req.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options))
415+
Get req = storage.objects()
416+
.get(from.getBucket(), from.getName())
417+
.setGeneration(from.getGeneration())
418+
.setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options))
414419
.setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options))
415420
.setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options))
416421
.setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options));
@@ -521,6 +526,7 @@ private RewriteResponse rewrite(RewriteRequest req, String token) throws Storage
521526
com.google.api.services.storage.model.RewriteResponse rewriteReponse = storage.objects()
522527
.rewrite(req.source.getBucket(), req.source.getName(), req.target.getBucket(),
523528
req.target.getName(), req.target.getContentType() != null ? req.target : null)
529+
.setSourceGeneration(req.source.getGeneration())
524530
.setRewriteToken(token)
525531
.setMaxBytesRewrittenPerCall(maxBytesRewrittenPerCall)
526532
.setProjection(DEFAULT_PROJECTION)

gcloud-java-storage/src/test/java/com/google/gcloud/storage/ITStorageTest.java

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static java.nio.charset.StandardCharsets.UTF_8;
2020
import static org.junit.Assert.assertArrayEquals;
2121
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertFalse;
2223
import static org.junit.Assert.assertNotNull;
2324
import static org.junit.Assert.assertNull;
2425
import static org.junit.Assert.assertTrue;
@@ -130,7 +131,8 @@ public void testCreateBlob() {
130131
BlobInfo remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT);
131132
assertNotNull(remoteBlob);
132133
assertEquals(blob.bucket(), remoteBlob.bucket());
133-
assertEquals(blob.name(), remoteBlob.name()); byte[] readBytes = storage.readAllBytes(BUCKET, blobName);
134+
assertEquals(blob.name(), remoteBlob.name());
135+
byte[] readBytes = storage.readAllBytes(BUCKET, blobName);
134136
assertArrayEquals(BLOB_BYTE_CONTENT, readBytes);
135137
assertTrue(storage.delete(BUCKET, blobName));
136138
}
@@ -245,16 +247,26 @@ public void testGetBlobFail() {
245247
String blobName = "test-get-blob-fail";
246248
BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
247249
assertNotNull(storage.create(blob));
248-
BlobId wrongGenerationBlob = BlobId.of(BUCKET, blobName, -1L);
250+
BlobId wrongGenerationBlob = BlobId.of(BUCKET, blobName);
249251
try {
250-
storage.get(wrongGenerationBlob, Storage.BlobGetOption.generationMatch());
252+
storage.get(wrongGenerationBlob, Storage.BlobGetOption.generationMatch(-1));
251253
fail("StorageException was expected");
252254
} catch (StorageException ex) {
253255
// expected
254256
}
255257
assertTrue(storage.delete(BUCKET, blobName));
256258
}
257259

260+
@Test
261+
public void testGetBlobFailNonExistingGeneration() {
262+
String blobName = "test-get-blob-fail-non-existing-generation";
263+
BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
264+
assertNotNull(storage.create(blob));
265+
BlobId wrongGenerationBlob = BlobId.of(BUCKET, blobName, -1L);
266+
assertNull(storage.get(wrongGenerationBlob));
267+
assertTrue(storage.delete(BUCKET, blobName));
268+
}
269+
258270
@Test
259271
public void testListBlobsSelectedFields() {
260272
String[] blobNames = {"test-list-blobs-selected-fields-blob1",
@@ -405,6 +417,14 @@ public void testDeleteNonExistingBlob() {
405417
assertTrue(!storage.delete(BUCKET, blobName));
406418
}
407419

420+
@Test
421+
public void testDeleteBlobNonExistingGeneration() {
422+
String blobName = "test-delete-blob-non-existing-generation";
423+
BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
424+
assertNotNull(storage.create(blob));
425+
assertTrue(!storage.delete(BlobId.of(BUCKET, blobName, -1L)));
426+
}
427+
408428
@Test
409429
public void testDeleteBlobFail() {
410430
String blobName = "test-delete-blob-fail";
@@ -611,19 +631,20 @@ public void testBatchRequestFail() {
611631
BatchRequest batchRequest = BatchRequest.builder()
612632
.update(updatedBlob, Storage.BlobTargetOption.generationMatch())
613633
.delete(BUCKET, blobName, Storage.BlobSourceOption.generationMatch(-1L))
614-
.delete(BlobId.of(BUCKET, blobName, -1L), Storage.BlobSourceOption.generationMatch())
634+
.delete(BlobId.of(BUCKET, blobName, -1L))
615635
.get(BUCKET, blobName, Storage.BlobGetOption.generationMatch(-1L))
616-
.get(BlobId.of(BUCKET, blobName, -1L), Storage.BlobGetOption.generationMatch())
636+
.get(BlobId.of(BUCKET, blobName, -1L))
617637
.build();
618-
BatchResponse updateResponse = storage.apply(batchRequest);
619-
assertEquals(1, updateResponse.updates().size());
620-
assertEquals(2, updateResponse.deletes().size());
621-
assertEquals(2, updateResponse.gets().size());
622-
assertTrue(updateResponse.updates().get(0).failed());
623-
assertTrue(updateResponse.gets().get(0).failed());
624-
assertTrue(updateResponse.gets().get(1).failed());
625-
assertTrue(updateResponse.deletes().get(0).failed());
626-
assertTrue(updateResponse.deletes().get(1).failed());
638+
BatchResponse batchResponse = storage.apply(batchRequest);
639+
assertEquals(1, batchResponse.updates().size());
640+
assertEquals(2, batchResponse.deletes().size());
641+
assertEquals(2, batchResponse.gets().size());
642+
assertTrue(batchResponse.updates().get(0).failed());
643+
assertTrue(batchResponse.gets().get(0).failed());
644+
assertFalse(batchResponse.gets().get(1).failed());
645+
assertNull(batchResponse.gets().get(1).get());
646+
assertTrue(batchResponse.deletes().get(0).failed());
647+
assertTrue(batchResponse.deletes().get(1).failed());
627648
assertTrue(storage.delete(BUCKET, blobName));
628649
}
629650

0 commit comments

Comments
 (0)