|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.bigtable.data.v2; |
| 17 | + |
| 18 | +import com.google.api.gax.rpc.ClientSettings; |
| 19 | +import com.google.bigtable.admin.v2.InstanceName; |
| 20 | +import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings; |
| 21 | +import java.io.IOException; |
| 22 | +import javax.annotation.Nonnull; |
| 23 | + |
| 24 | +/** |
| 25 | + * Settings class to configure an instance of {@link BigtableDataClient}. |
| 26 | + * |
| 27 | + * <p>Sane defaults are provided for most settings: |
| 28 | + * |
| 29 | + * <ul> |
| 30 | + * <li>The default service address (bigtable.googleapis.com) and default port (443) are used. |
| 31 | + * <li>Credentials are acquired automatically through Application Default Credentials. |
| 32 | + * <li>Retries are configured for idempotent methods but not for non-idempotent methods. |
| 33 | + * </ul> |
| 34 | + * |
| 35 | + * <p>The only required setting is the instance name. |
| 36 | + * |
| 37 | + * <p>The builder of this class is recursive, so contained classes are themselves builders. When |
| 38 | + * build() is called, the tree of builders is called to create the complete settings object. |
| 39 | + * |
| 40 | + * <pre>{@code |
| 41 | + * BigtableDataSettings bigtableDataSettings = BigtableDataSettings.newBuilder() |
| 42 | + * .setInstanceName(InstanceName.of("my-project", "my-instance-id")) |
| 43 | + * .setAppProfileId("default") |
| 44 | + * .build(); |
| 45 | + * }</pre> |
| 46 | + */ |
| 47 | +public class BigtableDataSettings extends ClientSettings<BigtableDataSettings> { |
| 48 | + private BigtableDataSettings(Builder builder) throws IOException { |
| 49 | + super(builder); |
| 50 | + } |
| 51 | + |
| 52 | + /** Create a new builder. */ |
| 53 | + public static Builder newBuilder() { |
| 54 | + return new Builder(); |
| 55 | + } |
| 56 | + |
| 57 | + /** Returns the target instance */ |
| 58 | + public InstanceName getInstanceName() { |
| 59 | + return getTypedStubSettings().getInstanceName(); |
| 60 | + } |
| 61 | + |
| 62 | + /** Returns the configured AppProfile to use */ |
| 63 | + public String getAppProfileId() { |
| 64 | + return getTypedStubSettings().getAppProfileId(); |
| 65 | + } |
| 66 | + |
| 67 | + @SuppressWarnings("unchecked") |
| 68 | + EnhancedBigtableStubSettings getTypedStubSettings() { |
| 69 | + return (EnhancedBigtableStubSettings) getStubSettings(); |
| 70 | + } |
| 71 | + |
| 72 | + /** Returns a builder containing all the values of this settings class. */ |
| 73 | + @SuppressWarnings("unchecked") |
| 74 | + public Builder toBuilder() { |
| 75 | + return new Builder(this); |
| 76 | + } |
| 77 | + |
| 78 | + /** Builder for BigtableDataSettings. */ |
| 79 | + public static class Builder extends ClientSettings.Builder<BigtableDataSettings, Builder> { |
| 80 | + /** |
| 81 | + * Initializes a new Builder with sane defaults for all settings. |
| 82 | + * |
| 83 | + * <p>Most defaults are extracted from BaseBigtableDataSettings, however some of the more |
| 84 | + * complex defaults are configured explicitly here. Once the overlayed defaults are configured, |
| 85 | + * the base settings are augmented to work with overlayed functionality (like disabling retries |
| 86 | + * in the underlying GAPIC client for batching). |
| 87 | + */ |
| 88 | + private Builder() { |
| 89 | + super(EnhancedBigtableStubSettings.newBuilder()); |
| 90 | + } |
| 91 | + |
| 92 | + private Builder(BigtableDataSettings settings) { |
| 93 | + super(settings); |
| 94 | + } |
| 95 | + |
| 96 | + // <editor-fold desc="Public API"> |
| 97 | + /** |
| 98 | + * Sets the target instance. This setting is required. All RPCs will be made in the context of |
| 99 | + * this setting. |
| 100 | + */ |
| 101 | + public Builder setInstanceName(@Nonnull InstanceName instanceName) { |
| 102 | + getTypedStubSettings().setInstanceName(instanceName); |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + /** Gets the {@link InstanceName} that was previously set on this Builder. */ |
| 107 | + public InstanceName getInstanceName() { |
| 108 | + return getTypedStubSettings().getInstanceName(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Sets the AppProfile to use. An application profile (sometimes also shortened to "app |
| 113 | + * profile") is a group of configuration parameters for an individual use case. A client will |
| 114 | + * identify itself with an application profile ID at connection time, and the requests will be |
| 115 | + * handled according to that application profile. |
| 116 | + */ |
| 117 | + public Builder setAppProfileId(@Nonnull String appProfileId) { |
| 118 | + getTypedStubSettings().setAppProfileId(appProfileId); |
| 119 | + return this; |
| 120 | + } |
| 121 | + |
| 122 | + /** Gets the app profile id that was previously set on this Builder. */ |
| 123 | + public String getAppProfileId() { |
| 124 | + return getTypedStubSettings().getAppProfileId(); |
| 125 | + } |
| 126 | + |
| 127 | + @SuppressWarnings("unchecked") |
| 128 | + private EnhancedBigtableStubSettings.Builder getTypedStubSettings() { |
| 129 | + return (EnhancedBigtableStubSettings.Builder) getStubSettings(); |
| 130 | + } |
| 131 | + |
| 132 | + public BigtableDataSettings build() throws IOException { |
| 133 | + return new BigtableDataSettings(this); |
| 134 | + } |
| 135 | + // </editor-fold> |
| 136 | + } |
| 137 | +} |
0 commit comments