Skip to content

Commit 41ca27f

Browse files
author
Ajay Kannan
committed
Use gcd v1beta3 (so DatastoreTests.java will run), remove BatchOption and TransactionOption (superfluous because force writes and isolation levels are no longer exposed). Also includes some minor cleanup.
1 parent 1b9c9ca commit 41ca27f

17 files changed

+165
-375
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public final void put(Entity... entities) {
138138
for (Entity entity : entities) {
139139
Key key = entity.key();
140140
toAdd.remove(key);
141-
toUpdate.remove(key);
141+
toUpdate.remove(key);
142142
toDelete.remove(key);
143143
toPut.put(key, entity);
144144
}
@@ -183,6 +183,10 @@ protected Map<Key, FullEntity<Key>> toPut() {
183183
protected Set<Key> toDelete() {
184184
return toDelete;
185185
}
186+
187+
protected int numAutoAllocatedIds() {
188+
return toAddAutoId.size();
189+
}
186190

187191
protected void deactivate() {
188192
active = false;
@@ -198,7 +202,7 @@ protected DatastoreException newInvalidRequest(String msg, Object... params) {
198202
return DatastoreException.throwInvalidRequest(String.format(msg, params));
199203
}
200204

201-
protected List<com.google.datastore.v1beta3.Mutation> toMutationPb() {
205+
protected List<com.google.datastore.v1beta3.Mutation> toMutationPbList() {
202206
List<com.google.datastore.v1beta3.Mutation> mutationsPb =
203207
new ArrayList<>();
204208
for (FullEntity<IncompleteKey> entity : toAddAutoId()) {

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

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,61 +16,53 @@
1616

1717
package com.google.gcloud.datastore;
1818

19-
import com.google.common.base.Function;
20-
import com.google.common.collect.Lists;
21-
import com.google.gcloud.datastore.BatchOption.ForceWrites;
22-
19+
import java.util.Iterator;
20+
import java.util.LinkedList;
2321
import java.util.List;
24-
import java.util.Map;
2522

2623

2724
class BatchImpl extends BaseDatastoreBatchWriter implements Batch {
2825

2926
private final DatastoreImpl datastore;
30-
private final boolean force;
3127

3228
static class ResponseImpl implements Batch.Response {
3329

3430
private final com.google.datastore.v1beta3.CommitResponse response;
31+
private final int numAutoAllocatedIds;
3532

36-
ResponseImpl(com.google.datastore.v1beta3.CommitResponse response) {
33+
ResponseImpl(com.google.datastore.v1beta3.CommitResponse response, int numAutoAllocatedIds) {
3734
this.response = response;
35+
this.numAutoAllocatedIds = numAutoAllocatedIds;
3836
}
3937

4038
@Override
4139
public List<Key> generatedKeys() {
42-
return Lists.transform(response.getMutationResult().getInsertAutoIdKeyList(),
43-
new Function<com.google.datastore.v1beta3.Key, Key>() {
44-
@Override public Key apply(com.google.datastore.v1beta3.Key keyPb) {
45-
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
46-
//return Key.fromPb(keyPb);
47-
return Key.builder(null).build(); // TODO(ajaykannan): remove this line when possible
48-
}
49-
});
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;
5047
}
5148
}
5249

53-
BatchImpl(DatastoreImpl datastore, BatchOption... options) {
50+
BatchImpl(DatastoreImpl datastore) {
5451
super("batch");
5552
this.datastore = datastore;
56-
Map<Class<? extends BatchOption>, BatchOption> optionsMap = BatchOption.asImmutableMap(options);
57-
if (optionsMap.containsKey(ForceWrites.class)) {
58-
force = ((ForceWrites) optionsMap.get(ForceWrites.class)).force();
59-
} else {
60-
force = datastore.options().force();
61-
}
6253
}
6354

6455
@Override
6556
public Batch.Response submit() {
6657
validateActive();
67-
com.google.datastore.v1beta3.Mutation.Builder mutationPb = toMutationPb();
68-
com.google.datastore.v1beta3.CommitRequest.Builder requestPb = com.google.datastore.v1beta3.CommitRequest.newBuilder();
58+
List<com.google.datastore.v1beta3.Mutation> mutationsPb = toMutationPbList();
59+
com.google.datastore.v1beta3.CommitRequest.Builder requestPb =
60+
com.google.datastore.v1beta3.CommitRequest.newBuilder();
6961
requestPb.setMode(com.google.datastore.v1beta3.CommitRequest.Mode.NON_TRANSACTIONAL);
70-
requestPb.setMutation(mutationPb);
62+
requestPb.addAllMutations(mutationsPb);
7163
com.google.datastore.v1beta3.CommitResponse responsePb = datastore.commit(requestPb.build());
7264
deactivate();
73-
return new ResponseImpl(responsePb);
65+
return new ResponseImpl(responsePb, numAutoAllocatedIds());
7466
}
7567

7668
@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 & 3 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
/**
@@ -57,12 +57,12 @@ interface TransactionCallable<T> {
5757
* @param options the options for the created transaction
5858
* @throws DatastoreException upon failure
5959
*/
60-
<T> T runInTransaction(TransactionCallable<T> callable, TransactionOption... options);
60+
<T> T runInTransaction(TransactionCallable<T> callable);
6161

6262
/**
6363
* Returns a new Batch for processing multiple write operations in one request.
6464
*/
65-
Batch newBatch(BatchOption... options);
65+
Batch newBatch();
6666

6767
/**
6868
* Allocate a unique id for the given key.

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

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

7272
static <T> T runInTransaction(Datastore datastore,
73-
Datastore.TransactionCallable<T> callable, TransactionOption... options) {
74-
Transaction transaction = datastore.newTransaction(options);
73+
Datastore.TransactionCallable<T> callable) {
74+
Transaction transaction = datastore.newTransaction();
7575
try {
7676
T value = callable.run(transaction);
7777
transaction.commit();

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,18 @@ public RetryResult beforeEval(Exception exception) {
7979
}
8080

8181
@Override
82-
public Batch newBatch(BatchOption... options) {
83-
return new BatchImpl(this, options);
82+
public Batch newBatch() {
83+
return new BatchImpl(this);
8484
}
8585

8686
@Override
87-
public Transaction newTransaction(TransactionOption... options) {
88-
return new TransactionImpl(this, options);
87+
public Transaction newTransaction() {
88+
return new TransactionImpl(this);
8989
}
9090

9191
@Override
92-
public <T> T runInTransaction(TransactionCallable<T> callable, TransactionOption... options) {
93-
return DatastoreHelper.runInTransaction(this, callable, options);
92+
public <T> T runInTransaction(TransactionCallable<T> callable) {
93+
return DatastoreHelper.runInTransaction(this, callable);
9494
}
9595

9696
@Override
@@ -99,9 +99,9 @@ public <T> QueryResults<T> run(Query<T> query) {
9999
}
100100

101101
<T> QueryResults<T> run(com.google.datastore.v1beta3.ReadOptions readOptionsPb, Query<T> query) {
102-
// TODO(ajaykannan): uncomment this line when possible in datastore v1beta3 transition
102+
// TODO(ajaykannan): fix me!
103103
//return new QueryResultsImpl<>(this, readOptionsPb, query);
104-
return null; // TODO(ajaykannan): remove this line when possible
104+
return null; // TODO(ajaykannan): fix me!
105105
}
106106

107107
com.google.datastore.v1beta3.RunQueryResponse runQuery(

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818

1919
import static com.google.gcloud.datastore.Validator.validateNamespace;
2020

21-
import com.google.api.services.datastore.DatastoreV1;
22-
import com.google.api.services.datastore.DatastoreV1.EntityResult;
23-
import com.google.api.services.datastore.DatastoreV1.LookupResponse;
21+
import com.google.common.base.MoreObjects;
2422
import com.google.common.collect.ImmutableSet;
2523
import com.google.common.collect.Iterables;
2624
import com.google.gcloud.ServiceOptions;
@@ -102,21 +100,23 @@ private DatastoreOptions normalize() {
102100
Builder builder = toBuilder();
103101
builder.normalizeDataset(false);
104102
// Replace provided project-id with full project-id (s~xxx, e~xxx,...)
105-
DatastoreV1.LookupRequest.Builder requestPb = DatastoreV1.LookupRequest.newBuilder();
106-
DatastoreV1.Key key = DatastoreV1.Key.newBuilder()
107-
.addPathElement(DatastoreV1.Key.PathElement.newBuilder().setKind("__foo__").setName("bar"))
103+
com.google.datastore.v1beta3.LookupRequest.Builder requestPb =
104+
com.google.datastore.v1beta3.LookupRequest.newBuilder();
105+
com.google.datastore.v1beta3.Key key = com.google.datastore.v1beta3.Key.newBuilder()
106+
.addPath(com.google.datastore.v1beta3.Key.PathElement.newBuilder()
107+
.setKind("__foo__").setName("bar"))
108108
.build();
109-
requestPb.addKey(key);
109+
requestPb.addKeys(key);
110110
try {
111-
LookupResponse responsePb = datastoreRpc().lookup(requestPb.build());
111+
com.google.datastore.v1beta3.LookupResponse responsePb = datastoreRpc().lookup(requestPb.build());
112112
if (responsePb.getDeferredCount() > 0) {
113113
key = responsePb.getDeferred(0);
114114
} else {
115-
Iterator<EntityResult> combinedIter =
115+
Iterator<com.google.datastore.v1beta3.EntityResult> combinedIter =
116116
Iterables.concat(responsePb.getMissingList(), responsePb.getFoundList()).iterator();
117117
key = combinedIter.next().getEntity().getKey();
118118
}
119-
builder.projectId(key.getPartitionId().getDatasetId());
119+
builder.projectId(key.getPartitionId().getProjectId());
120120
return new DatastoreOptions(builder);
121121
} catch (DatastoreRpcException e) {
122122
throw DatastoreException.translateAndThrow(e);
@@ -149,10 +149,10 @@ private static String defaultNamespace() {
149149
Class<?> clazz = Class.forName("com.google.appengine.api.NamespaceManager");
150150
Method method = clazz.getMethod("get");
151151
String namespace = (String) method.invoke(null);
152-
return namespace == null || namespace.isEmpty() ? null : namespace;
152+
return MoreObjects.firstNonNull(namespace, "");
153153
} catch (Exception ignore) {
154-
// return null (Datastore default namespace) if could not automatically determine
155-
return null;
154+
// return empty string (Datastore default namespace) if could not automatically determine
155+
return "";
156156
}
157157
}
158158

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,12 @@ public Builder newBuilder(FullEntity<?> value) {
3939

4040
@Override
4141
protected FullEntity<?> getValue(com.google.datastore.v1beta3.Value from) {
42-
// TODO(ajaykannan): uncomment this line when possible in datastore v1beta3 transition
43-
//return FullEntity.fromPb(from.getEntityValue());
44-
return null; // TODO(ajaykannan): remove this line when possible
42+
return FullEntity.fromPb(from.getEntityValue());
4543
}
4644

4745
@Override
4846
protected void setValue(EntityValue from, com.google.datastore.v1beta3.Value.Builder to) {
49-
// TODO(ajaykannan): uncomment this line when possible in datastore v1beta3 transition
50-
//to.setEntityValue(from.get().toPb());
47+
to.setEntityValue(from.get().toPb());
5148
}
5249
};
5350

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ public abstract static class ResultType<V> implements java.io.Serializable {
6969
//TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
7070
//return Key.fromPb(entityPb.getKey());
7171
}
72-
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
73-
//return ProjectionEntity.fromPb(entityPb);
74-
return ProjectionEntity.fromPb((com.google.datastore.v1beta3.Entity) null); // remove this line when possible
72+
return ProjectionEntity.fromPb(entityPb);
7573
}
7674
};
7775

@@ -81,9 +79,7 @@ public abstract static class ResultType<V> implements java.io.Serializable {
8179
private static final long serialVersionUID = 7712959777507168274L;
8280

8381
@Override protected Entity convert(DatastoreV1.Entity entityPb) {
84-
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
85-
//return Entity.fromPb(entityPb);
86-
return Entity.fromPb((com.google.datastore.v1beta3.Entity) null); // remove this line when possible
82+
return Entity.fromPb(entityPb);
8783
}
8884
};
8985

@@ -106,9 +102,7 @@ public abstract static class ResultType<V> implements java.io.Serializable {
106102
private static final long serialVersionUID = -7591409419690650246L;
107103

108104
@Override protected ProjectionEntity convert(DatastoreV1.Entity entityPb) {
109-
// TODO(ajaykannan): uncomment when possible in datastore v1beta3 transition
110-
//return ProjectionEntity.fromPb(entityPb);
111-
return ProjectionEntity.fromPb((com.google.datastore.v1beta3.Entity) null); // remove this line when possible
105+
return ProjectionEntity.fromPb(entityPb);
112106
}
113107
};
114108

0 commit comments

Comments
 (0)