Skip to content

Commit

Permalink
cleanup v1beta3 changes and fix LocalGcdHelper bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Sep 28, 2015
1 parent e078469 commit 959d67d
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,6 @@ protected Set<Key> toDelete() {
return toDelete;
}

protected int numAutoAllocatedIds() {
return toAddAutoId.size();
}

protected void deactivate() {
active = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.google.gcloud.datastore;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;


Expand All @@ -39,7 +39,7 @@ static class ResponseImpl implements Batch.Response {
public List<Key> generatedKeys() {
Iterator<com.google.datastore.v1beta3.MutationResult> results =
response.getMutationResultsList().iterator();
List<Key> generated = new LinkedList<Key>();
List<Key> generated = new ArrayList<>(numAutoAllocatedIds);
for (int i = 0; i < numAutoAllocatedIds; i++) {
generated.add(Key.fromPb(results.next().getKey()));
}
Expand All @@ -62,7 +62,7 @@ public Batch.Response submit() {
requestPb.addAllMutations(mutationsPb);
com.google.datastore.v1beta3.CommitResponse responsePb = datastore.commit(requestPb.build());
deactivate();
return new ResponseImpl(responsePb, numAutoAllocatedIds());
return new ResponseImpl(responsePb, toAddAutoId().size());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,33 +176,32 @@ public List<Entity> add(FullEntity<?>... entities) {
return Collections.emptyList();
}
List<com.google.datastore.v1beta3.Mutation> mutationsPb = new ArrayList<>();
Map<Key, Entity> completeEntities = new LinkedHashMap<>();
Set<Entity> completeEntities = new LinkedHashSet<>();
for (FullEntity<?> entity : entities) {
Entity completeEntity = null;
if (entity.key() instanceof Key) {
completeEntity = Entity.convert((FullEntity<Key>) entity);
}
if (completeEntity != null) {
if (completeEntities.put(completeEntity.key(), completeEntity) != null) {
if (completeEntities.contains(completeEntity)) {
throw DatastoreException.throwInvalidRequest(
"Duplicate entity with the key %s", entity.key());
}
mutationsPb.add(com.google.datastore.v1beta3.Mutation.newBuilder()
.setInsert(completeEntity.toPb()).build());
completeEntities.add(completeEntity);
} else {
Preconditions.checkArgument(entity.hasKey(), "entity %s is missing a key", entity);
mutationsPb.add(com.google.datastore.v1beta3.Mutation.newBuilder()
.setInsert(entity.toPb()).build());
}
mutationsPb.add(com.google.datastore.v1beta3.Mutation.newBuilder()
.setInsert(entity.toPb()).build());
}
com.google.datastore.v1beta3.CommitResponse commitResponse = commitMutation(mutationsPb);
Iterator<com.google.datastore.v1beta3.MutationResult> mutationResults =
commitResponse.getMutationResultsList().iterator();
Iterator<Entity> completeEntitiesIt = completeEntities.iterator();
ImmutableList.Builder<Entity> responseBuilder = ImmutableList.builder();
for (FullEntity<?> entity : entities) {
Entity completeEntity = completeEntities.get(entity.key());
if (completeEntity != null) {
responseBuilder.add(completeEntity);
if (completeEntities.contains(entity)) {
responseBuilder.add(completeEntitiesIt.next());
mutationResults.next();
} else {
responseBuilder.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ private DatastoreOptions normalize() {
.build();
requestPb.addKeys(key);
try {
com.google.datastore.v1beta3.LookupResponse responsePb = datastoreRpc().lookup(requestPb.build());
com.google.datastore.v1beta3.LookupResponse responsePb =
datastoreRpc().lookup(requestPb.build());
if (responsePb.getDeferredCount() > 0) {
key = responsePb.getDeferred(0);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import com.google.protobuf.ByteString;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

final class TransactionImpl extends BaseDatastoreBatchWriter implements Transaction {
Expand All @@ -42,7 +42,7 @@ static class ResponseImpl implements Transaction.Response {
public List<Key> generatedKeys() {
Iterator<com.google.datastore.v1beta3.MutationResult> results =
response.getMutationResultsList().iterator();
List<Key> generated = new LinkedList<Key>();
List<Key> generated = new ArrayList<>(numAutoAllocatedIds);
for (int i = 0; i < numAutoAllocatedIds; i++) {
generated.add(Key.fromPb(results.next().getKey()));
}
Expand Down Expand Up @@ -98,7 +98,7 @@ public Transaction.Response commit() {
requestPb.addAllMutations(mutationsPb);
com.google.datastore.v1beta3.CommitResponse responsePb = datastore.commit(requestPb.build());
deactivate();
return new ResponseImpl(responsePb, numAutoAllocatedIds());
return new ResponseImpl(responsePb, toAddAutoId().size());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import com.google.gcloud.datastore.DatastoreOptions;
import com.google.gcloud.spi.DatastoreRpc.DatastoreRpcException.Reason;

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -42,20 +46,26 @@ public class DefaultDatastoreRpc implements DatastoreRpc {
}

public DefaultDatastoreRpc(DatastoreOptions options) {
if (options.host().contains("localhost")) {
client = com.google.datastore.v1beta3.client.DatastoreFactory.get().create(
new com.google.datastore.v1beta3.client.DatastoreOptions.Builder()
.projectId(options.projectId())
.localHost(options.host())
.initializer(options.httpRequestInitializer())
.build());
} else {
client = com.google.datastore.v1beta3.client.DatastoreFactory.get().create(
new com.google.datastore.v1beta3.client.DatastoreOptions.Builder()
.projectId(options.projectId())
.initializer(options.httpRequestInitializer())
.build());
com.google.datastore.v1beta3.client.DatastoreOptions.Builder clientBuilder =
new com.google.datastore.v1beta3.client.DatastoreOptions.Builder()
.projectId(options.projectId())
.initializer(options.httpRequestInitializer());
if (options.host() != null) {
try {
String normalizedHost = options.host();
if (!normalizedHost.startsWith("http")) {
normalizedHost = "http://" + normalizedHost;
}
InetAddress hostAddr = InetAddress.getByName(new URL(normalizedHost).getHost());
if (hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress()) {
clientBuilder = clientBuilder.localHost(options.host());
}
} catch (UnknownHostException | MalformedURLException e) {
// ignore
}
}
client = com.google.datastore.v1beta3.client.DatastoreFactory.get()
.create(clientBuilder.build());
}

private static DatastoreRpcException translate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public static void main(String... args) throws IOException, InterruptedException
public static boolean isActive(String projectId) {
try {
StringBuilder urlBuilder = new StringBuilder("http://localhost:").append(PORT);
urlBuilder.append("/datastore/v1beta3/datasets/").append(projectId).append(":lookup");
urlBuilder.append("/datastore/v1beta3/projects/").append(projectId).append(":lookup");
URL url = new URL(urlBuilder.toString());
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) {
Expand Down

0 comments on commit 959d67d

Please sign in to comment.