Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary project ID normalization #875

Merged
merged 3 commits into from
Apr 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -138,9 +99,6 @@ protected String defaultProject() {
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();
}
return projectId != null ? projectId : super.defaultProject();
}

Expand Down Expand Up @@ -192,7 +150,7 @@ public Builder toBuilder() {

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

@Override
Expand All @@ -201,8 +159,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