Skip to content

Commit 21aaa44

Browse files
committed
---
yaml --- r: 5855 b: refs/heads/tswast-patch-1 c: 98b2391 h: refs/heads/master i: 5853: 6e2b651 5851: bf8f3f1 5847: c29f1b4 5839: 5e4d20d 5823: 832444a
1 parent bc3d303 commit 21aaa44

13 files changed

Lines changed: 201 additions & 129 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: 44f4eb53295a3212d1cbd0c78a69fe50041c1d07
60+
refs/heads/tswast-patch-1: 98b239194485db4356d3d3ad09147a42002528fc
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.google.gcloud.datastore;
2+
3+
/**
4+
* An interface to represent a batch of write operations.
5+
* Any write operation that is applied on a batch will only be sent
6+
* to the Datastore upon {@link #submit}.
7+
* A usage example:
8+
* <pre> {@code
9+
* Entity entity1 = datastore.get(key1);
10+
* Batch batch = datastore.newBatch();
11+
* Entity entity2 = Entity.builder(key2).set("name", "John").build();
12+
* entity1 = Entity.builder(entity1).clear().setNull("bla").build();
13+
* Entity entity3 = Entity.builder(key3).set("title", "title").build();
14+
* batch.update(entity1);
15+
* batch.add(entity2, entity3);
16+
* batch.submit();
17+
* } </pre>
18+
*/
19+
public interface Batch extends DatastoreBatchWriter {
20+
21+
interface Response extends DatastoreBatchWriter.Response {
22+
}
23+
24+
/**
25+
* Submit the batch to the Datastore.
26+
*
27+
* @throws DatastoreServiceException if there was any failure or if batch is not longer active
28+
*/
29+
Response submit();
30+
}

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/BatchWriterImpl.java renamed to branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/BatchImpl.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import static com.google.gcloud.datastore.DatastoreServiceException.throwInvalidRequest;
44

55
import com.google.api.services.datastore.DatastoreV1;
6-
import com.google.gcloud.datastore.BatchWriteOption.ForceWrites;
6+
import com.google.common.base.Function;
7+
import com.google.common.collect.Lists;
8+
import com.google.gcloud.datastore.BatchOption.ForceWrites;
79

810
import java.util.LinkedHashMap;
911
import java.util.LinkedHashSet;
@@ -12,7 +14,7 @@
1214
import java.util.Map;
1315
import java.util.Set;
1416

15-
class BatchWriterImpl implements BatchWriter {
17+
class BatchImpl implements Batch {
1618

1719
private final Map<Key, Entity> toAdd = new LinkedHashMap<>();
1820
private final List<PartialEntity> toAddAutoId = new LinkedList<>();
@@ -24,10 +26,30 @@ class BatchWriterImpl implements BatchWriter {
2426

2527
private boolean active = true;
2628

27-
BatchWriterImpl(DatastoreServiceImpl datastore, BatchWriteOption... options) {
29+
30+
class ResponseImpl implements Response {
31+
32+
private final DatastoreV1.CommitResponse response;
33+
34+
public ResponseImpl(DatastoreV1.CommitResponse response) {
35+
this.response = response;
36+
}
37+
38+
@Override
39+
public List<Key> generatedKeys() {
40+
return Lists.transform(response.getMutationResult().getInsertAutoIdKeyList(),
41+
new Function<DatastoreV1.Key, Key>() {
42+
@Override public Key apply(DatastoreV1.Key keyPb) {
43+
return Key.fromPb(keyPb);
44+
}
45+
});
46+
}
47+
}
48+
49+
BatchImpl(DatastoreServiceImpl datastore, BatchOption... options) {
2850
this.datastore = datastore;
29-
Map<Class<? extends BatchWriteOption>, BatchWriteOption> optionsMap =
30-
BatchWriteOption.asImmutableMap(options);
51+
Map<Class<? extends BatchOption>, BatchOption> optionsMap =
52+
BatchOption.asImmutableMap(options);
3153
if (optionsMap.containsKey(ForceWrites.class)) {
3254
force = ((ForceWrites) optionsMap.get(ForceWrites.class)).force();
3355
} else {
@@ -115,7 +137,11 @@ public void delete(Key... keys) {
115137
}
116138

117139
@Override
118-
public void submit() {
140+
public Response submit() {
141+
return new ResponseImpl(commitRequest());
142+
}
143+
144+
DatastoreV1.CommitResponse commitRequest() {
119145
validateActive();
120146
DatastoreV1.Mutation.Builder mutationPb = DatastoreV1.Mutation.newBuilder();
121147
for (PartialEntity entity : toAddAutoId) {
@@ -138,8 +164,9 @@ public void submit() {
138164
}
139165
DatastoreV1.CommitRequest.Builder requestPb = newCommitRequest();
140166
requestPb.setMutation(mutationPb);
141-
datastore.commit(requestPb.build());
167+
DatastoreV1.CommitResponse response = datastore.commit(requestPb.build());
142168
active = false;
169+
return response;
143170
}
144171

145172
@Override

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/BatchWriteOption.java renamed to branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/BatchOption.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
import java.util.Map;
66

7-
public abstract class BatchWriteOption implements java.io.Serializable {
7+
public abstract class BatchOption implements java.io.Serializable {
88

99
private static final long serialVersionUID = -3932758377282659839L;
1010

11-
public static final class ForceWrites extends BatchWriteOption {
11+
public static final class ForceWrites extends BatchOption {
1212

1313
private static final long serialVersionUID = 2555054296046232799L;
1414

@@ -23,19 +23,18 @@ public boolean force() {
2323
}
2424
}
2525

26-
BatchWriteOption() {
26+
BatchOption() {
2727
// package protected
2828
}
2929

3030
public static ForceWrites forceWrites() {
3131
return new ForceWrites(true);
3232
}
3333

34-
static Map<Class<? extends BatchWriteOption>, BatchWriteOption> asImmutableMap(
35-
BatchWriteOption... options) {
36-
ImmutableMap.Builder<Class<? extends BatchWriteOption>, BatchWriteOption> builder =
34+
static Map<Class<? extends BatchOption>, BatchOption> asImmutableMap(BatchOption... options) {
35+
ImmutableMap.Builder<Class<? extends BatchOption>, BatchOption> builder =
3736
ImmutableMap.builder();
38-
for (BatchWriteOption option : options) {
37+
for (BatchOption option : options) {
3938
builder.put(option.getClass(), option);
4039
}
4140
return builder.build();

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/BatchWriter.java

Lines changed: 0 additions & 79 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud.datastore;
18+
19+
import java.util.List;
20+
21+
/**
22+
* An interface to represent a batch of write operations.
23+
* All write operation for a batch writer will be applied to the Datastore in one RPC call.
24+
*/
25+
interface DatastoreBatchWriter extends DatastoreWriter {
26+
27+
interface Response {
28+
List<Key> generatedKeys();
29+
}
30+
31+
/**
32+
* {@inheritDoc}
33+
* This operation will be converted to {@link #put} operation for entities that were already
34+
* marked for deletion in this writer.
35+
* @throws com.google.gcloud.datastore.DatastoreServiceException if a given entity already added
36+
* to this writer or if not active
37+
*/
38+
@Override
39+
void add(Entity... entity);
40+
41+
/**
42+
* Datastore add operation.
43+
* This method will automatically allocate id for any entity with an incomplete key.
44+
*
45+
* @throws IllegalArgumentException if any of the given entities is missing a key
46+
* @throws com.google.gcloud.datastore.DatastoreServiceException if a given entity with a
47+
* complete key was already added to this writer or if not active
48+
*/
49+
void add(PartialEntity... entity);
50+
51+
/**
52+
* {@inheritDoc}
53+
* This operation will be converted to {@link #put} operation for entities that were already
54+
* added or put in this writer
55+
* @throws com.google.gcloud.datastore.DatastoreServiceException if an entity is marked for
56+
* deletion in this writer or if not active
57+
*/
58+
@Override
59+
void update(Entity... entity);
60+
61+
/**
62+
* {@inheritDoc}
63+
* This operation will also remove from this batch any prior writes for entities with the same
64+
* keys
65+
* @throws com.google.gcloud.datastore.DatastoreServiceException if not active
66+
*/
67+
@Override
68+
void delete(Key... key);
69+
70+
/**
71+
* {@inheritDoc}
72+
* This operation will also remove from this writer any prior writes for the same entities.
73+
* @throws com.google.gcloud.datastore.DatastoreServiceException if not active
74+
*/
75+
@Override
76+
void put(Entity... entity);
77+
78+
/**
79+
* Returns {@code true} if still active (write operations were not sent to the Datastore).
80+
*/
81+
boolean active();
82+
}

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/DatastoreHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public Transaction newTransaction(TransactionOption... options) {
4545
}
4646

4747
@Override
48-
public BatchWriter newBatchWriter(BatchWriteOption... options) {
49-
return delegate.newBatchWriter(options);
48+
public Batch newBatch(BatchOption... options) {
49+
return delegate.newBatch(options);
5050
}
5151

5252
@Override

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/DatastoreService.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ public interface DatastoreService extends DatastoreReaderWriter {
2020
Transaction newTransaction(TransactionOption... options);
2121

2222
/**
23-
* Returns a new Batch writer for processing multiple write operations
24-
* in one request.
23+
* Returns a new Batch for processing multiple write operations in one request.
2524
*/
26-
BatchWriter newBatchWriter(BatchWriteOption... options);
25+
Batch newBatch(BatchOption... options);
2726

2827
/**
2928
* Allocate a unique id for the given key.

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/DatastoreServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public DatastoreServiceOptions options() {
7070
}
7171

7272
@Override
73-
public BatchWriter newBatchWriter(BatchWriteOption... options) {
74-
return new BatchWriterImpl(this, options);
73+
public Batch newBatch(BatchOption... options) {
74+
return new BatchImpl(this, options);
7575
}
7676

7777
@Override

branches/tswast-patch-1/src/main/java/com/google/gcloud/datastore/Transaction.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
* A Google cloud datastore transaction.
7-
* Any write operation that is applied on a transaction will only be sent
7+
* Similar to {@link Batch} any write operation that is applied on a transaction will only be sent
88
* to the Datastore upon {@link #commit}. A call to {@link #rollback} will invalidate
99
* the transaction and discard the changes. Any read operation that is done by a transaction
1010
* will be part of it and therefore a {@code commit} is guaranteed to fail if an entity
@@ -33,7 +33,10 @@
3333
* @see <a href="https://cloud.google.com/datastore/docs/concepts/transactions">Google Cloud Datastore transactions</a>
3434
*
3535
*/
36-
public interface Transaction extends DatastoreReaderWriter {
36+
public interface Transaction extends DatastoreBatchWriter, DatastoreReaderWriter {
37+
38+
interface Response extends DatastoreBatchWriter.Response {
39+
}
3740

3841
/**
3942
* {@inheritDoc}
@@ -74,7 +77,7 @@ public interface Transaction extends DatastoreReaderWriter {
7477
*
7578
* @throws DatastoreServiceException if could not commit the transaction or if no longer active
7679
*/
77-
void commit();
80+
Response commit();
7881

7982
/**
8083
* Rollback the transaction.
@@ -86,5 +89,6 @@ public interface Transaction extends DatastoreReaderWriter {
8689
/**
8790
* Returns {@code true} if the transaction is still active (was not committed or rolledback).
8891
*/
92+
@Override
8993
boolean active();
9094
}

0 commit comments

Comments
 (0)