Skip to content

Commit

Permalink
Fix handling of host in DatastoreOptions.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Oct 1, 2015
1 parent 959d67d commit 9463c55
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,17 @@ public List<Entity> add(FullEntity<?>... entities) {
return Collections.emptyList();
}
List<com.google.datastore.v1beta3.Mutation> mutationsPb = new ArrayList<>();
Set<Entity> completeEntities = new LinkedHashSet<>();
Map<Key, Entity> completeEntities = new LinkedHashMap<>();
for (FullEntity<?> entity : entities) {
Entity completeEntity = null;
if (entity.key() instanceof Key) {
completeEntity = Entity.convert((FullEntity<Key>) entity);
}
if (completeEntity != null) {
if (completeEntities.contains(completeEntity)) {
if (completeEntities.put(completeEntity.key(), completeEntity) != null) {
throw DatastoreException.throwInvalidRequest(
"Duplicate entity with the key %s", entity.key());
}
completeEntities.add(completeEntity);
} else {
Preconditions.checkArgument(entity.hasKey(), "entity %s is missing a key", entity);
}
Expand All @@ -197,11 +196,11 @@ public List<Entity> add(FullEntity<?>... entities) {
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) {
if (completeEntities.contains(entity)) {
responseBuilder.add(completeEntitiesIt.next());
Entity completeEntity = completeEntities.get(entity.key());
if (completeEntity != null) {
responseBuilder.add(completeEntity);
mutationResults.next();
} else {
responseBuilder.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,25 @@
public class DatastoreOptions extends ServiceOptions<DatastoreRpc, DatastoreOptions> {

private static final long serialVersionUID = -8636602944160689193L;
private static final String DATASET_ENV_NAME = "DATASTORE_DATASET";
private static final String HOST_ENV_NAME = "DATASTORE_HOST";
private static final String DATASTORE_SCOPE = "https://www.googleapis.com/auth/datastore";
private static final String USERINFO_SCOPE = "https://www.googleapis.com/auth/userinfo.email";
private static final Set<String> SCOPES = ImmutableSet.of(DATASTORE_SCOPE, USERINFO_SCOPE);

private final String namespace;
private final boolean force;
private final boolean normalizeDataset;
private transient DatastoreRpc datastoreRpc;

public static class Builder extends
ServiceOptions.Builder<DatastoreRpc, DatastoreOptions, Builder> {

private String namespace;
private boolean force;
private boolean normalizeDataset = true;

private Builder() {
}

private Builder(DatastoreOptions options) {
super(options);
force = options.force;
namespace = options.namespace;
normalizeDataset = options.normalizeDataset;
}
Expand All @@ -74,11 +69,6 @@ public Builder namespace(String namespace) {
return this;
}

public Builder force(boolean force) {
this.force = force;
return this;
}

Builder normalizeDataset(boolean normalizeDataset) {
this.normalizeDataset = normalizeDataset;
return this;
Expand All @@ -89,7 +79,6 @@ private DatastoreOptions(Builder builder) {
super(builder);
normalizeDataset = builder.normalizeDataset;
namespace = builder.namespace != null ? builder.namespace : defaultNamespace();
force = builder.force;
}

private DatastoreOptions normalize() {
Expand Down Expand Up @@ -126,13 +115,17 @@ private DatastoreOptions normalize() {

@Override
protected String defaultHost() {
String host = System.getProperty(HOST_ENV_NAME, System.getenv(HOST_ENV_NAME));
String host = System.getProperty(
com.google.datastore.v1beta3.client.DatastoreHelper.LOCAL_HOST_ENV_VAR,
System.getenv(com.google.datastore.v1beta3.client.DatastoreHelper.LOCAL_HOST_ENV_VAR));
return host != null ? host : super.defaultHost();
}

@Override
protected String defaultProject() {
String projectId = System.getProperty(DATASET_ENV_NAME, System.getenv(DATASET_ENV_NAME));
String projectId = System.getProperty(
com.google.datastore.v1beta3.client.DatastoreHelper.PROJECT_ID_ENV_VAR,
System.getenv(com.google.datastore.v1beta3.client.DatastoreHelper.PROJECT_ID_ENV_VAR));
if (projectId == null) {
projectId = appEngineAppId();
}
Expand All @@ -157,10 +150,6 @@ private static String defaultNamespace() {
}
}

public boolean force() {
return force;
}

@Override
protected Set<String> scopes() {
return SCOPES;
Expand All @@ -173,7 +162,7 @@ public Builder toBuilder() {

@Override
public int hashCode() {
return baseHashCode() ^ Objects.hash(namespace, force, normalizeDataset);
return baseHashCode() ^ Objects.hash(namespace, normalizeDataset);
}

@Override
Expand All @@ -183,7 +172,6 @@ public boolean equals(Object obj) {
}
DatastoreOptions other = (DatastoreOptions) obj;
return baseEquals(other) && Objects.equals(namespace, other.namespace)
&& Objects.equals(force, other.force)
&& Objects.equals(normalizeDataset, other.normalizeDataset);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,41 @@ public DefaultDatastoreRpc(DatastoreOptions options) {
new com.google.datastore.v1beta3.client.DatastoreOptions.Builder()
.projectId(options.projectId())
.initializer(options.httpRequestInitializer());
if (options.host() != null) {
if (isLocalHost(options.host())) {
clientBuilder = clientBuilder.localHost(options.host());
} else if (!options.host()
.equals(com.google.datastore.v1beta3.client.DatastoreFactory.DEFAULT_HOST)) {
String fullURL = options.host();
if (fullURL.charAt(fullURL.length() - 1) != '/') {
fullURL = fullURL + '/';
}
fullURL = fullURL + "datastore/v1beta3/projects/" + options.projectId();
clientBuilder = clientBuilder.projectId(null).projectEndpoint(fullURL);
}
client = com.google.datastore.v1beta3.client.DatastoreFactory.get()
.create(clientBuilder.build());
}

private static boolean isLocalHost(String host) {
if (host != null) {
try {
String normalizedHost = options.host();
if (!normalizedHost.startsWith("http")) {
String normalizedHost = host;
if (!includesScheme(normalizedHost)) {
normalizedHost = "http://" + normalizedHost;
}
InetAddress hostAddr = InetAddress.getByName(new URL(normalizedHost).getHost());
if (hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress()) {
clientBuilder = clientBuilder.localHost(options.host());
return true;
}
} catch (UnknownHostException | MalformedURLException e) {
// ignore
}
}
client = com.google.datastore.v1beta3.client.DatastoreFactory.get()
.create(clientBuilder.build());
return false;
}

private static boolean includesScheme(String url) {
return url.startsWith("http://") || url.startsWith("https://");
}

private static DatastoreRpcException translate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ public void testNamespace() throws Exception {
assertEquals("ns1", options.namespace("ns1").build().namespace());
}

@Test
public void testForce() throws Exception {
assertFalse(options.build().force());
assertTrue(options.force(true).build().force());
}

@Test
public void testDatastore() throws Exception {
assertSame(datastoreRpcFactory, options.build().serviceRpcFactory());
Expand All @@ -82,12 +76,11 @@ public void testDatastore() throws Exception {

@Test
public void testToBuilder() throws Exception {
DatastoreOptions original = options.namespace("ns1").force(true).build();
DatastoreOptions original = options.namespace("ns1").build();
DatastoreOptions copy = original.toBuilder().build();
assertEquals(original.projectId(), copy.projectId());
assertEquals(original.namespace(), copy.namespace());
assertEquals(original.host(), copy.host());
assertEquals(original.force(), copy.force());
assertEquals(original.retryParams(), copy.retryParams());
assertEquals(original.authCredentials(), copy.authCredentials());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ public void testServiceOptions() throws Exception {
.namespace("ns1")
.retryParams(RetryParams.getDefaultInstance())
.authCredentials(AuthCredentials.noCredentials())
.force(true)
.build();
serializedCopy = serializeAndDeserialize(options);
assertEquals(options, serializedCopy);
Expand Down

0 comments on commit 9463c55

Please sign in to comment.