From 73ed7696bea4e6eef730552bf44bef244229bcb4 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Thu, 7 Apr 2016 15:06:21 -0700 Subject: [PATCH] Remove unnecessary normalization --- .../gcloud/datastore/DatastoreOptions.java | 48 ++----------------- .../datastore/DatastoreOptionsTest.java | 1 - .../gcloud/datastore/SerializationTest.java | 1 - 3 files changed, 4 insertions(+), 46 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java index bc0efd837755..b233754bbda7 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/DatastoreOptions.java @@ -20,14 +20,12 @@ import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Iterables; import com.google.gcloud.ServiceOptions; import com.google.gcloud.datastore.spi.DatastoreRpc; import com.google.gcloud.datastore.spi.DatastoreRpcFactory; import com.google.gcloud.datastore.spi.DefaultDatastoreRpc; import java.lang.reflect.Method; -import java.util.Iterator; import java.util.Objects; import java.util.Set; @@ -38,7 +36,6 @@ public class DatastoreOptions extends ServiceOptions SCOPES = ImmutableSet.of(DATASTORE_SCOPE); private final String namespace; - private final boolean normalizeDataset; public static class DefaultDatastoreFactory implements DatastoreFactory { @@ -64,7 +61,6 @@ public static class Builder extends ServiceOptions.Builder { private String namespace; - private boolean normalizeDataset = true; private Builder() { } @@ -72,59 +68,24 @@ private Builder() { private Builder(DatastoreOptions options) { super(options); namespace = options.namespace; - normalizeDataset = options.normalizeDataset; } @Override public DatastoreOptions build() { - DatastoreOptions options = new DatastoreOptions(this); - return normalizeDataset ? options.normalize() : options; + return new DatastoreOptions(this); } public Builder namespace(String namespace) { this.namespace = validateNamespace(namespace); return this; } - - Builder normalizeDataset(boolean normalizeDataset) { - this.normalizeDataset = normalizeDataset; - return this; - } } private DatastoreOptions(Builder builder) { super(DatastoreFactory.class, DatastoreRpcFactory.class, builder); - normalizeDataset = builder.normalizeDataset; namespace = builder.namespace != null ? builder.namespace : defaultNamespace(); } - private DatastoreOptions normalize() { - if (!normalizeDataset) { - return this; - } - - Builder builder = toBuilder(); - builder.normalizeDataset(false); - // Replace provided project-id with full project-id (s~xxx, e~xxx,...) - com.google.datastore.v1beta3.LookupRequest.Builder requestPb = - com.google.datastore.v1beta3.LookupRequest.newBuilder(); - com.google.datastore.v1beta3.Key key = com.google.datastore.v1beta3.Key.newBuilder() - .addPath(com.google.datastore.v1beta3.Key.PathElement.newBuilder() - .setKind("__foo__").setName("bar")) - .build(); - requestPb.addKeys(key); - com.google.datastore.v1beta3.LookupResponse responsePb = rpc().lookup(requestPb.build()); - if (responsePb.getDeferredCount() > 0) { - key = responsePb.getDeferred(0); - } else { - Iterator combinedIter = - Iterables.concat(responsePb.getMissingList(), responsePb.getFoundList()).iterator(); - key = combinedIter.next().getEntity().getKey(); - } - builder.projectId(key.getPartitionId().getProjectId()); - return new DatastoreOptions(builder); - } - @Override protected String defaultHost() { String host = System.getProperty( @@ -139,7 +100,7 @@ protected String defaultProject() { 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(); + projectId = appEngineProjectId(); } return projectId != null ? projectId : super.defaultProject(); } @@ -192,7 +153,7 @@ public Builder toBuilder() { @Override public int hashCode() { - return baseHashCode() ^ Objects.hash(namespace, normalizeDataset); + return baseHashCode() ^ Objects.hash(namespace); } @Override @@ -201,8 +162,7 @@ public boolean equals(Object obj) { return false; } DatastoreOptions other = (DatastoreOptions) obj; - return baseEquals(other) && Objects.equals(namespace, other.namespace) - && Objects.equals(normalizeDataset, other.normalizeDataset); + return baseEquals(other) && Objects.equals(namespace, other.namespace); } public static Builder builder() { diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java index 5b77c035a714..9178bf12098b 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/DatastoreOptionsTest.java @@ -41,7 +41,6 @@ public void setUp() { datastoreRpcFactory = EasyMock.createMock(DatastoreRpcFactory.class); datastoreRpc = EasyMock.createMock(DatastoreRpc.class); options = DatastoreOptions.builder() - .normalizeDataset(false) .serviceRpcFactory(datastoreRpcFactory) .projectId(PROJECT_ID) .host("http://localhost:" + PORT); diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index 3679aba6825c..4bc90315b5d9 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -110,7 +110,6 @@ public class SerializationTest extends BaseSerializationTest { protected java.io.Serializable[] serializableObjects() { DatastoreOptions options = DatastoreOptions.builder() .authCredentials(AuthCredentials.createForAppEngine()) - .normalizeDataset(false) .projectId("ds1") .build(); DatastoreOptions otherOptions = options.toBuilder()