Skip to content

Commit e078469

Browse files
author
Ajay Kannan
committed
Updates to write-related code for datastore v1beta3 update
1 parent 660d67b commit e078469

24 files changed

Lines changed: 349 additions & 570 deletions

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseDatastoreBatchWriter.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
package com.google.gcloud.datastore;
1818

19-
import com.google.api.services.datastore.DatastoreV1;
2019
import com.google.common.base.Preconditions;
2120
import com.google.common.collect.Iterables;
2221
import com.google.common.collect.Lists;
2322

23+
import java.util.ArrayList;
2424
import java.util.Collections;
2525
import java.util.Iterator;
2626
import java.util.LinkedHashMap;
@@ -88,9 +88,7 @@ public final List<Entity> add(FullEntity<?>... entities) {
8888
for (FullEntity<?> entity : entities) {
8989
IncompleteKey key = entity.key();
9090
Preconditions.checkArgument(key != null, "Entity must have a key");
91-
if (key instanceof Key) {
92-
addInternal((FullEntity<Key>) entity);
93-
} else {
91+
if (!(key instanceof Key)) {
9492
incompleteKeys.add(key);
9593
}
9694
}
@@ -104,6 +102,7 @@ public final List<Entity> add(FullEntity<?>... entities) {
104102
List<Entity> answer = Lists.newArrayListWithExpectedSize(entities.length);
105103
for (FullEntity<?> entity : entities) {
106104
if (entity.key() instanceof Key) {
105+
addInternal((FullEntity<Key>) entity);
107106
answer.add(Entity.convert((FullEntity<Key>) entity));
108107
} else {
109108
Entity entityWithAllocatedId = Entity.builder(allocated.next(), entity).build();
@@ -185,6 +184,10 @@ protected Set<Key> toDelete() {
185184
return toDelete;
186185
}
187186

187+
protected int numAutoAllocatedIds() {
188+
return toAddAutoId.size();
189+
}
190+
188191
protected void deactivate() {
189192
active = false;
190193
}
@@ -199,25 +202,30 @@ protected DatastoreException newInvalidRequest(String msg, Object... params) {
199202
return DatastoreException.throwInvalidRequest(String.format(msg, params));
200203
}
201204

202-
protected DatastoreV1.Mutation.Builder toMutationPb() {
203-
DatastoreV1.Mutation.Builder mutationPb = DatastoreV1.Mutation.newBuilder();
205+
protected List<com.google.datastore.v1beta3.Mutation> toMutationPbList() {
206+
List<com.google.datastore.v1beta3.Mutation> mutationsPb =
207+
new ArrayList<>();
204208
for (FullEntity<IncompleteKey> entity : toAddAutoId()) {
205-
mutationPb.addInsertAutoId(entity.toPb());
209+
mutationsPb.add(
210+
com.google.datastore.v1beta3.Mutation.newBuilder().setInsert(entity.toPb()).build());
206211
}
207212
for (FullEntity<Key> entity : toAdd().values()) {
208-
mutationPb.addInsert(entity.toPb());
213+
mutationsPb.add(
214+
com.google.datastore.v1beta3.Mutation.newBuilder().setInsert(entity.toPb()).build());
209215
}
210216
for (FullEntity<Key> entity : toUpdate().values()) {
211-
mutationPb.addUpdate(entity.toPb());
217+
mutationsPb.add(
218+
com.google.datastore.v1beta3.Mutation.newBuilder().setUpdate(entity.toPb()).build());
212219
}
213220
for (FullEntity<Key> entity : toPut().values()) {
214-
mutationPb.addUpsert(entity.toPb());
221+
mutationsPb.add(
222+
com.google.datastore.v1beta3.Mutation.newBuilder().setUpsert(entity.toPb()).build());
215223
}
216224
for (Key key : toDelete()) {
217-
// TODO(ajaykannan): fix me!
218-
//mutationPb.addDelete(key.toPb());
225+
mutationsPb.add(
226+
com.google.datastore.v1beta3.Mutation.newBuilder().setDelete(key.toPb()).build());
219227
}
220-
return mutationPb;
228+
return mutationsPb;
221229
}
222230

223231
protected abstract Datastore datastore();

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BaseEntity.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import static com.google.gcloud.datastore.NullValue.of;
2828
import static com.google.gcloud.datastore.StringValue.of;
2929

30-
import com.google.api.services.datastore.DatastoreV1;
3130
import com.google.common.collect.ImmutableSortedMap;
3231
import com.google.common.collect.Maps;
3332
import com.google.protobuf.InvalidProtocolBufferException;
@@ -48,7 +47,8 @@
4847
* @see <a href="https://cloud.google.com/datastore/docs/concepts/entities">Google Cloud Datastore
4948
* Entities, Properties, and Keys</a>
5049
*/
51-
public abstract class BaseEntity<K extends IncompleteKey> extends Serializable<DatastoreV1.Entity> {
50+
public abstract class BaseEntity<K extends IncompleteKey>
51+
extends Serializable<com.google.datastore.v1beta3.Entity> {
5252

5353
private static final long serialVersionUID = 8175618724683792766L;
5454

@@ -90,16 +90,15 @@ private B self() {
9090
}
9191

9292
@SuppressWarnings("unchecked")
93-
protected B fill(DatastoreV1.Entity entityPb) {
93+
protected B fill(com.google.datastore.v1beta3.Entity entityPb) {
9494
Map<String, Value<?>> copiedProperties = Maps.newHashMap();
95-
for (DatastoreV1.Property property : entityPb.getPropertyList()) {
96-
// TODO(ajaykannan): fix me!
97-
//copiedProperties.put(property.getName(), Value.fromPb(property.getValue()));
95+
for (Map.Entry<String, com.google.datastore.v1beta3.Value> entry :
96+
entityPb.getProperties().entrySet()) {
97+
copiedProperties.put(entry.getKey(), Value.fromPb(entry.getValue()));
9898
}
9999
properties(copiedProperties);
100100
if (entityPb.hasKey()) {
101-
// TODO(ajaykannan): fix me!
102-
//key((K) IncompleteKey.fromPb(entityPb.getKey()));
101+
key((K) IncompleteKey.fromPb(entityPb.getKey()));
103102
}
104103
return self();
105104
}
@@ -379,25 +378,22 @@ ImmutableSortedMap<String, Value<?>> properties() {
379378
@Override
380379
protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
381380
Builder<?, ?> builder = emptyBuilder();
382-
builder.fill(DatastoreV1.Entity.parseFrom(bytesPb));
381+
builder.fill(com.google.datastore.v1beta3.Entity.parseFrom(bytesPb));
383382
return builder.build();
384383
}
385384

386385
protected abstract Builder<?, ?> emptyBuilder();
387386

388387
@Override
389-
protected final DatastoreV1.Entity toPb() {
390-
DatastoreV1.Entity.Builder entityPb = DatastoreV1.Entity.newBuilder();
388+
protected final com.google.datastore.v1beta3.Entity toPb() {
389+
com.google.datastore.v1beta3.Entity.Builder entityPb =
390+
com.google.datastore.v1beta3.Entity.newBuilder();
391+
Map<String, com.google.datastore.v1beta3.Value> propertiesPb = entityPb.getMutableProperties();
391392
for (Map.Entry<String, Value<?>> entry : properties.entrySet()) {
392-
DatastoreV1.Property.Builder propertyPb = DatastoreV1.Property.newBuilder();
393-
propertyPb.setName(entry.getKey());
394-
// TODO(ajaykannan): fix me!
395-
//propertyPb.setValue(entry.getValue().toPb());
396-
entityPb.addProperty(propertyPb.build());
393+
propertiesPb.put(entry.getKey(), entry.getValue().toPb());
397394
}
398395
if (key != null) {
399-
// TODO(ajaykannan): fix me!
400-
//entityPb.setKey(key.toPb());
396+
entityPb.setKey(key.toPb());
401397
}
402398
return entityPb.build();
403399
}

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchImpl.java

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,65 +16,53 @@
1616

1717
package com.google.gcloud.datastore;
1818

19-
import com.google.api.services.datastore.DatastoreV1;
20-
import com.google.common.base.Function;
21-
import com.google.common.collect.Lists;
22-
import com.google.gcloud.datastore.BatchOption.ForceWrites;
23-
19+
import java.util.Iterator;
20+
import java.util.LinkedList;
2421
import java.util.List;
25-
import java.util.Map;
2622

2723

2824
class BatchImpl extends BaseDatastoreBatchWriter implements Batch {
2925

3026
private final DatastoreImpl datastore;
31-
private final boolean force;
3227

3328
static class ResponseImpl implements Batch.Response {
3429

35-
private final DatastoreV1.CommitResponse response;
30+
private final com.google.datastore.v1beta3.CommitResponse response;
31+
private final int numAutoAllocatedIds;
3632

37-
ResponseImpl(DatastoreV1.CommitResponse response) {
33+
ResponseImpl(com.google.datastore.v1beta3.CommitResponse response, int numAutoAllocatedIds) {
3834
this.response = response;
35+
this.numAutoAllocatedIds = numAutoAllocatedIds;
3936
}
4037

4138
@Override
4239
public List<Key> generatedKeys() {
43-
return Lists.transform(response.getMutationResult().getInsertAutoIdKeyList(),
44-
new Function<DatastoreV1.Key, Key>() {
45-
@Override public Key apply(DatastoreV1.Key keyPb) {
46-
// TODO(ajaykannan): fix me!
47-
//return Key.fromPb(keyPb);
48-
return Key.builder(null).build(); // TODO(ajaykannan): fix me!
49-
}
50-
});
40+
Iterator<com.google.datastore.v1beta3.MutationResult> results =
41+
response.getMutationResultsList().iterator();
42+
List<Key> generated = new LinkedList<Key>();
43+
for (int i = 0; i < numAutoAllocatedIds; i++) {
44+
generated.add(Key.fromPb(results.next().getKey()));
45+
}
46+
return generated;
5147
}
5248
}
5349

54-
BatchImpl(DatastoreImpl datastore, BatchOption... options) {
50+
BatchImpl(DatastoreImpl datastore) {
5551
super("batch");
5652
this.datastore = datastore;
57-
Map<Class<? extends BatchOption>, BatchOption> optionsMap = BatchOption.asImmutableMap(options);
58-
if (optionsMap.containsKey(ForceWrites.class)) {
59-
force = ((ForceWrites) optionsMap.get(ForceWrites.class)).force();
60-
} else {
61-
force = datastore.options().force();
62-
}
6353
}
6454

6555
@Override
6656
public Batch.Response submit() {
6757
validateActive();
68-
DatastoreV1.Mutation.Builder mutationPb = toMutationPb();
69-
if (force) {
70-
mutationPb.setForce(force);
71-
}
72-
DatastoreV1.CommitRequest.Builder requestPb = DatastoreV1.CommitRequest.newBuilder();
73-
requestPb.setMode(DatastoreV1.CommitRequest.Mode.NON_TRANSACTIONAL);
74-
requestPb.setMutation(mutationPb);
75-
DatastoreV1.CommitResponse responsePb = datastore.commit(requestPb.build());
58+
List<com.google.datastore.v1beta3.Mutation> mutationsPb = toMutationPbList();
59+
com.google.datastore.v1beta3.CommitRequest.Builder requestPb =
60+
com.google.datastore.v1beta3.CommitRequest.newBuilder();
61+
requestPb.setMode(com.google.datastore.v1beta3.CommitRequest.Mode.NON_TRANSACTIONAL);
62+
requestPb.addAllMutations(mutationsPb);
63+
com.google.datastore.v1beta3.CommitResponse responsePb = datastore.commit(requestPb.build());
7664
deactivate();
77-
return new ResponseImpl(responsePb);
65+
return new ResponseImpl(responsePb, numAutoAllocatedIds());
7866
}
7967

8068
@Override

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/BatchOption.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/Datastore.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public interface Datastore extends Service<DatastoreOptions>, DatastoreReaderWri
3030
*
3131
* @throws DatastoreException upon failure
3232
*/
33-
Transaction newTransaction(TransactionOption... options);
33+
Transaction newTransaction();
3434

3535

3636
/**
@@ -54,15 +54,14 @@ interface TransactionCallable<T> {
5454
* as a {@link DatastoreException} with the original exception as its root cause.
5555
*
5656
* @param callable the callback to call with a newly created transactional readerWriter
57-
* @param options the options for the created transaction
5857
* @throws DatastoreException upon failure
5958
*/
60-
<T> T runInTransaction(TransactionCallable<T> callable, TransactionOption... options);
59+
<T> T runInTransaction(TransactionCallable<T> callable);
6160

6261
/**
6362
* Returns a new Batch for processing multiple write operations in one request.
6463
*/
65-
Batch newBatch(BatchOption... options);
64+
Batch newBatch();
6665

6766
/**
6867
* Allocate a unique id for the given key.

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreHelper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ static List<Entity> fetch(DatastoreReader reader, Key... keys) {
6969
return list;
7070
}
7171

72-
static <T> T runInTransaction(Datastore datastore,
73-
Datastore.TransactionCallable<T> callable, TransactionOption... options) {
74-
Transaction transaction = datastore.newTransaction(options);
72+
static <T> T runInTransaction(Datastore datastore, Datastore.TransactionCallable<T> callable) {
73+
Transaction transaction = datastore.newTransaction();
7574
try {
7675
T value = callable.run(transaction);
7776
transaction.commit();

0 commit comments

Comments
 (0)