Skip to content

Commit fe19b53

Browse files
author
travis-ci
committed
Fix handling of host in DatastoreOptions
1 parent 959d67d commit fe19b53

File tree

5 files changed

+38
-40
lines changed

5 files changed

+38
-40
lines changed

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,17 @@ public List<Entity> add(FullEntity<?>... entities) {
176176
return Collections.emptyList();
177177
}
178178
List<com.google.datastore.v1beta3.Mutation> mutationsPb = new ArrayList<>();
179-
Set<Entity> completeEntities = new LinkedHashSet<>();
179+
Map<Key, Entity> completeEntities = new LinkedHashMap<>();
180180
for (FullEntity<?> entity : entities) {
181181
Entity completeEntity = null;
182182
if (entity.key() instanceof Key) {
183183
completeEntity = Entity.convert((FullEntity<Key>) entity);
184184
}
185185
if (completeEntity != null) {
186-
if (completeEntities.contains(completeEntity)) {
186+
if (completeEntities.put(completeEntity.key(), completeEntity) != null) {
187187
throw DatastoreException.throwInvalidRequest(
188188
"Duplicate entity with the key %s", entity.key());
189189
}
190-
completeEntities.add(completeEntity);
191190
} else {
192191
Preconditions.checkArgument(entity.hasKey(), "entity %s is missing a key", entity);
193192
}
@@ -197,11 +196,11 @@ public List<Entity> add(FullEntity<?>... entities) {
197196
com.google.datastore.v1beta3.CommitResponse commitResponse = commitMutation(mutationsPb);
198197
Iterator<com.google.datastore.v1beta3.MutationResult> mutationResults =
199198
commitResponse.getMutationResultsList().iterator();
200-
Iterator<Entity> completeEntitiesIt = completeEntities.iterator();
201199
ImmutableList.Builder<Entity> responseBuilder = ImmutableList.builder();
202200
for (FullEntity<?> entity : entities) {
203-
if (completeEntities.contains(entity)) {
204-
responseBuilder.add(completeEntitiesIt.next());
201+
Entity completeEntity = completeEntities.get(entity.key());
202+
if (completeEntity != null) {
203+
responseBuilder.add(completeEntity);
205204
mutationResults.next();
206205
} else {
207206
responseBuilder.add(

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

+7-19
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,25 @@
3535
public class DatastoreOptions extends ServiceOptions<DatastoreRpc, DatastoreOptions> {
3636

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

4442
private final String namespace;
45-
private final boolean force;
4643
private final boolean normalizeDataset;
4744
private transient DatastoreRpc datastoreRpc;
4845

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

5249
private String namespace;
53-
private boolean force;
5450
private boolean normalizeDataset = true;
5551

5652
private Builder() {
5753
}
5854

5955
private Builder(DatastoreOptions options) {
6056
super(options);
61-
force = options.force;
6257
namespace = options.namespace;
6358
normalizeDataset = options.normalizeDataset;
6459
}
@@ -74,11 +69,6 @@ public Builder namespace(String namespace) {
7469
return this;
7570
}
7671

77-
public Builder force(boolean force) {
78-
this.force = force;
79-
return this;
80-
}
81-
8272
Builder normalizeDataset(boolean normalizeDataset) {
8373
this.normalizeDataset = normalizeDataset;
8474
return this;
@@ -89,7 +79,6 @@ private DatastoreOptions(Builder builder) {
8979
super(builder);
9080
normalizeDataset = builder.normalizeDataset;
9181
namespace = builder.namespace != null ? builder.namespace : defaultNamespace();
92-
force = builder.force;
9382
}
9483

9584
private DatastoreOptions normalize() {
@@ -126,13 +115,17 @@ private DatastoreOptions normalize() {
126115

127116
@Override
128117
protected String defaultHost() {
129-
String host = System.getProperty(HOST_ENV_NAME, System.getenv(HOST_ENV_NAME));
118+
String host = System.getProperty(
119+
com.google.datastore.v1beta3.client.DatastoreHelper.LOCAL_HOST_ENV_VAR,
120+
System.getenv(com.google.datastore.v1beta3.client.DatastoreHelper.LOCAL_HOST_ENV_VAR));
130121
return host != null ? host : super.defaultHost();
131122
}
132123

133124
@Override
134125
protected String defaultProject() {
135-
String projectId = System.getProperty(DATASET_ENV_NAME, System.getenv(DATASET_ENV_NAME));
126+
String projectId = System.getProperty(
127+
com.google.datastore.v1beta3.client.DatastoreHelper.PROJECT_ID_ENV_VAR,
128+
System.getenv(com.google.datastore.v1beta3.client.DatastoreHelper.PROJECT_ID_ENV_VAR));
136129
if (projectId == null) {
137130
projectId = appEngineAppId();
138131
}
@@ -157,10 +150,6 @@ private static String defaultNamespace() {
157150
}
158151
}
159152

160-
public boolean force() {
161-
return force;
162-
}
163-
164153
@Override
165154
protected Set<String> scopes() {
166155
return SCOPES;
@@ -173,7 +162,7 @@ public Builder toBuilder() {
173162

174163
@Override
175164
public int hashCode() {
176-
return baseHashCode() ^ Objects.hash(namespace, force, normalizeDataset);
165+
return baseHashCode() ^ Objects.hash(namespace, normalizeDataset);
177166
}
178167

179168
@Override
@@ -183,7 +172,6 @@ public boolean equals(Object obj) {
183172
}
184173
DatastoreOptions other = (DatastoreOptions) obj;
185174
return baseEquals(other) && Objects.equals(namespace, other.namespace)
186-
&& Objects.equals(force, other.force)
187175
&& Objects.equals(normalizeDataset, other.normalizeDataset);
188176
}
189177

gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java

+25-6
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,41 @@ public DefaultDatastoreRpc(DatastoreOptions options) {
5050
new com.google.datastore.v1beta3.client.DatastoreOptions.Builder()
5151
.projectId(options.projectId())
5252
.initializer(options.httpRequestInitializer());
53-
if (options.host() != null) {
53+
if (isLocalHost(options.host())) {
54+
clientBuilder = clientBuilder.localHost(options.host());
55+
} else if (!options.host()
56+
.equals(com.google.datastore.v1beta3.client.DatastoreFactory.DEFAULT_HOST)) {
57+
String fullURL = options.host();
58+
if (fullURL.charAt(fullURL.length() - 1) != '/') {
59+
fullURL = fullURL + '/';
60+
}
61+
fullURL = fullURL + "datastore/v1beta3/projects/" + options.projectId();
62+
clientBuilder = clientBuilder.projectId(null).projectEndpoint(fullURL);
63+
}
64+
client = com.google.datastore.v1beta3.client.DatastoreFactory.get()
65+
.create(clientBuilder.build());
66+
}
67+
68+
private static boolean isLocalHost(String host) {
69+
if (host != null) {
5470
try {
55-
String normalizedHost = options.host();
56-
if (!normalizedHost.startsWith("http")) {
71+
String normalizedHost = host;
72+
if (!includesScheme(normalizedHost)) {
5773
normalizedHost = "http://" + normalizedHost;
5874
}
5975
InetAddress hostAddr = InetAddress.getByName(new URL(normalizedHost).getHost());
6076
if (hostAddr.isAnyLocalAddress() || hostAddr.isLoopbackAddress()) {
61-
clientBuilder = clientBuilder.localHost(options.host());
77+
return true;
6278
}
6379
} catch (UnknownHostException | MalformedURLException e) {
6480
// ignore
6581
}
6682
}
67-
client = com.google.datastore.v1beta3.client.DatastoreFactory.get()
68-
.create(clientBuilder.build());
83+
return false;
84+
}
85+
86+
private static boolean includesScheme(String url) {
87+
return url.startsWith("http://") || url.startsWith("https://");
6988
}
7089

7190
private static DatastoreRpcException translate(

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,6 @@ public void testNamespace() throws Exception {
6868
assertEquals("ns1", options.namespace("ns1").build().namespace());
6969
}
7070

71-
@Test
72-
public void testForce() throws Exception {
73-
assertFalse(options.build().force());
74-
assertTrue(options.force(true).build().force());
75-
}
76-
7771
@Test
7872
public void testDatastore() throws Exception {
7973
assertSame(datastoreRpcFactory, options.build().serviceRpcFactory());
@@ -82,12 +76,11 @@ public void testDatastore() throws Exception {
8276

8377
@Test
8478
public void testToBuilder() throws Exception {
85-
DatastoreOptions original = options.namespace("ns1").force(true).build();
79+
DatastoreOptions original = options.namespace("ns1").build();
8680
DatastoreOptions copy = original.toBuilder().build();
8781
assertEquals(original.projectId(), copy.projectId());
8882
assertEquals(original.namespace(), copy.namespace());
8983
assertEquals(original.host(), copy.host());
90-
assertEquals(original.force(), copy.force());
9184
assertEquals(original.retryParams(), copy.retryParams());
9285
assertEquals(original.authCredentials(), copy.authCredentials());
9386
}

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ public void testServiceOptions() throws Exception {
148148
.namespace("ns1")
149149
.retryParams(RetryParams.getDefaultInstance())
150150
.authCredentials(AuthCredentials.noCredentials())
151-
.force(true)
152151
.build();
153152
serializedCopy = serializeAndDeserialize(options);
154153
assertEquals(options, serializedCopy);

0 commit comments

Comments
 (0)