Skip to content

Commit

Permalink
Remove unnecessary normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Apr 7, 2016
1 parent bdc472a commit 73ed769
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,7 +36,6 @@ public class DatastoreOptions extends ServiceOptions<Datastore, DatastoreRpc, Da
private static final Set<String> SCOPES = ImmutableSet.of(DATASTORE_SCOPE);

private final String namespace;
private final boolean normalizeDataset;

public static class DefaultDatastoreFactory implements DatastoreFactory {

Expand All @@ -64,67 +61,31 @@ public static class Builder extends
ServiceOptions.Builder<Datastore, DatastoreRpc, DatastoreOptions, Builder> {

private String namespace;
private boolean normalizeDataset = true;

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<com.google.datastore.v1beta3.EntityResult> 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(
Expand All @@ -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();
}
Expand Down Expand Up @@ -192,7 +153,7 @@ public Builder toBuilder() {

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

@Override
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 73ed769

Please sign in to comment.