Skip to content
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
4 changes: 0 additions & 4 deletions google-cloud-datastore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
</dependency>
<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.datastore;

import static com.google.cloud.datastore.Validator.validateNamespace;
import static com.google.cloud.datastore.spi.v1.DatastoreRpc.Transport.GRPC;

import com.google.api.core.BetaApi;
import com.google.cloud.ServiceDefaults;
Expand All @@ -25,9 +26,12 @@
import com.google.cloud.TransportOptions;
import com.google.cloud.datastore.spi.DatastoreRpcFactory;
import com.google.cloud.datastore.spi.v1.DatastoreRpc;
import com.google.cloud.datastore.spi.v1.DatastoreRpc.Transport;
import com.google.cloud.datastore.spi.v1.GrpcDatastoreRpc;
import com.google.cloud.datastore.spi.v1.HttpDatastoreRpc;
import com.google.cloud.datastore.v1.DatastoreSettings;
import com.google.cloud.http.HttpTransportOptions;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableSet;
import java.io.IOException;
Expand All @@ -45,6 +49,7 @@ public class DatastoreOptions extends ServiceOptions<Datastore, DatastoreOptions

private final String namespace;
private final String databaseId;
private final Transport transport;

public static class DefaultDatastoreFactory implements DatastoreFactory {

Expand All @@ -63,7 +68,9 @@ public static class DefaultDatastoreRpcFactory implements DatastoreRpcFactory {
@Override
public ServiceRpc create(DatastoreOptions options) {
try {
return new GrpcDatastoreRpc(options);
return options.transport == GRPC
? new GrpcDatastoreRpc(options)
: new HttpDatastoreRpc(options);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -74,13 +81,15 @@ public static class Builder extends ServiceOptions.Builder<Datastore, DatastoreO

private String namespace;
private String databaseId;
private Transport transport = GRPC;

private Builder() {}

private Builder(DatastoreOptions options) {
super(options);
namespace = options.namespace;
databaseId = options.databaseId;
transport = options.transport;
}

@Override
Expand Down Expand Up @@ -108,12 +117,18 @@ public Builder setDatabaseId(String databaseId) {
this.databaseId = databaseId;
return this;
}

public Builder setTransport(Transport transport) {
this.transport = transport;
return this;
}
}

private DatastoreOptions(Builder builder) {
super(DatastoreFactory.class, DatastoreRpcFactory.class, builder, new DatastoreDefaults());
namespace = MoreObjects.firstNonNull(builder.namespace, defaultNamespace());
databaseId = MoreObjects.firstNonNull(builder.databaseId, DEFAULT_DATABASE_ID);
transport = builder.transport;
}

@Override
Expand Down Expand Up @@ -166,6 +181,12 @@ public String getDatabaseId() {
return this.databaseId;
}

/** Returns the current transport */
@VisibleForTesting
Transport getTransport() {
return this.transport;
}

/** Returns a default {@code DatastoreOptions} instance. */
public static DatastoreOptions getDefaultInstance() {
return newBuilder().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

package com.google.cloud.datastore.spi.v1;

import com.google.api.core.InternalApi;
import com.google.api.gax.rpc.HeaderProvider;
import com.google.cloud.ServiceRpc;
import com.google.cloud.datastore.DatastoreException;
import com.google.cloud.datastore.v1.DatastoreSettings;
import com.google.datastore.v1.AllocateIdsRequest;
import com.google.datastore.v1.AllocateIdsResponse;
import com.google.datastore.v1.BeginTransactionRequest;
Expand Down Expand Up @@ -99,7 +102,26 @@ default RunAggregationQueryResponse runAggregationQuery(RunAggregationQueryReque

@Override
void close() throws Exception;

/** Returns true if this background resource has been shut down. */
boolean isClosed();

// This class is needed solely to get access to protected method setInternalHeaderProvider()
class DatastoreSettingsBuilder extends DatastoreSettings.Builder {
DatastoreSettingsBuilder(DatastoreSettings settings) {
super(settings);
}

@Override
protected DatastoreSettings.Builder setInternalHeaderProvider(
HeaderProvider internalHeaderProvider) {
return super.setInternalHeaderProvider(internalHeaderProvider);
}
}

/** Transport used to sending requests. */
@InternalApi
enum Transport {
GRPC,
HTTP
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import static com.google.cloud.datastore.DatastoreUtils.isLocalHost;
import static com.google.cloud.datastore.DatastoreUtils.removeScheme;
import static com.google.cloud.datastore.spi.v1.RpcUtils.retrySettingSetter;
import static java.util.concurrent.TimeUnit.SECONDS;

import com.google.api.core.ApiFunction;
import com.google.api.core.InternalApi;
import com.google.api.gax.core.BackgroundResource;
import com.google.api.gax.core.GaxProperties;
Expand All @@ -30,7 +30,6 @@
import com.google.api.gax.rpc.HeaderProvider;
import com.google.api.gax.rpc.NoHeaderProvider;
import com.google.api.gax.rpc.TransportChannel;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.cloud.NoCredentials;
import com.google.cloud.ServiceOptions;
import com.google.cloud.datastore.DatastoreException;
Expand Down Expand Up @@ -76,14 +75,10 @@ public GrpcDatastoreRpc(DatastoreOptions datastoreOptions) throws IOException {
isEmulator(datastoreOptions)
? getClientContextForEmulator(datastoreOptions)
: getClientContext(datastoreOptions);
ApiFunction<UnaryCallSettings.Builder<?, ?>, Void> retrySettingsSetter =
builder -> {
builder.setRetrySettings(datastoreOptions.getRetrySettings());
return null;
};

DatastoreStubSettings datastoreStubSettings =
DatastoreStubSettings.newBuilder(clientContext)
.applyToAllUnaryMethods(retrySettingsSetter)
.applyToAllUnaryMethods(retrySettingSetter(datastoreOptions))
.build();
datastoreStub = GrpcDatastoreStub.create(datastoreStubSettings);
} catch (IOException e) {
Expand Down Expand Up @@ -202,18 +197,4 @@ private String getResourceToken(DatastoreOptions datastoreOptions) {
}
return builder.toString();
}

// This class is needed solely to get access to protected method setInternalHeaderProvider()
private static class DatastoreSettingsBuilder extends DatastoreSettings.Builder {

private DatastoreSettingsBuilder(DatastoreSettings settings) {
super(settings);
}

@Override
protected DatastoreSettings.Builder setInternalHeaderProvider(
HeaderProvider internalHeaderProvider) {
return super.setInternalHeaderProvider(internalHeaderProvider);
}
}
}
Loading