/
InstanceInfo.java
289 lines (244 loc) · 8.13 KB
/
InstanceInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.spanner;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.cloud.FieldSelector;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.FieldMask;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/** Represents a Cloud Spanner Instance. */
public class InstanceInfo {
/** Represent an updatable field in Cloud Spanner instance. */
public enum InstanceField implements FieldSelector {
DISPLAY_NAME("display_name"),
NODE_COUNT("node_count"),
PROCESSING_UNITS("processing_units"),
LABELS("labels");
static InstanceField[] defaultFieldsToUpdate(InstanceInfo info) {
if (info.getNodeCount() > 0) {
return new InstanceField[] {DISPLAY_NAME, NODE_COUNT, LABELS};
} else {
return new InstanceField[] {DISPLAY_NAME, PROCESSING_UNITS, LABELS};
}
}
private final String selector;
InstanceField(String selector) {
this.selector = selector;
}
@Override
public String getSelector() {
return selector;
}
static FieldMask toFieldMask(InstanceField... fields) {
FieldMask.Builder builder = FieldMask.newBuilder();
for (InstanceField field : fields) {
builder.addPaths(field.getSelector());
}
return builder.build();
}
}
/** State of the Instance. */
public enum State {
UNSPECIFIED,
CREATING,
READY
}
/** Builder for {@code InstanceInfo}. */
public abstract static class Builder {
public abstract Builder setInstanceConfigId(InstanceConfigId configId);
public abstract Builder setDisplayName(String displayName);
/**
* Sets the number of nodes for the instance. Exactly one of processing units or node count must
* be set when creating a new instance.
*/
public abstract Builder setNodeCount(int nodeCount);
/**
* Sets the number of processing units for the instance. Exactly one of processing units or node
* count must be set when creating a new instance. Processing units must be between 1 and 999
* (inclusive) when creating a new instance with node count = 0. Processing units from 1000 and
* up must always be a multiple of 1000 (that is equal to an integer number of nodes).
*/
public Builder setProcessingUnits(int processingUnits) {
throw new UnsupportedOperationException("Unimplemented");
}
public abstract Builder setState(State state);
public abstract Builder addLabel(String key, String value);
public abstract Builder putAllLabels(Map<String, String> labels);
public abstract InstanceInfo build();
}
static class BuilderImpl extends Builder {
private InstanceId id;
private InstanceConfigId configId;
private String displayName;
private int nodeCount;
private int processingUnits;
private State state;
private Map<String, String> labels;
BuilderImpl(InstanceId id) {
this.id = id;
this.labels = new HashMap<>();
}
BuilderImpl(InstanceInfo instance) {
this.id = instance.id;
this.configId = instance.configId;
this.displayName = instance.displayName;
this.nodeCount = instance.nodeCount;
this.processingUnits = instance.processingUnits;
this.state = instance.state;
this.labels = new HashMap<>(instance.labels);
}
@Override
public BuilderImpl setInstanceConfigId(InstanceConfigId configId) {
this.configId = configId;
return this;
}
@Override
public BuilderImpl setDisplayName(String displayName) {
this.displayName = displayName;
return this;
}
@Override
public BuilderImpl setNodeCount(int nodeCount) {
this.nodeCount = nodeCount;
return this;
}
@Override
public BuilderImpl setProcessingUnits(int processingUnits) {
this.processingUnits = processingUnits;
return this;
}
@Override
public BuilderImpl setState(State state) {
this.state = state;
return this;
}
@Override
public BuilderImpl addLabel(String key, String value) {
labels.put(key, value);
return this;
}
@Override
public BuilderImpl putAllLabels(Map<String, String> labels) {
this.labels.putAll(labels);
return this;
}
@Override
public InstanceInfo build() {
return new InstanceInfo(this);
}
}
private final InstanceId id;
private final InstanceConfigId configId;
private final String displayName;
private final int nodeCount;
private final int processingUnits;
private final State state;
private final ImmutableMap<String, String> labels;
InstanceInfo(BuilderImpl builder) {
this.id = builder.id;
this.configId = builder.configId;
this.displayName = builder.displayName;
this.nodeCount = builder.nodeCount;
this.processingUnits = builder.processingUnits;
this.state = builder.state;
this.labels = ImmutableMap.copyOf(builder.labels);
}
/** Returns the identifier of the instance. */
public InstanceId getId() {
return id;
}
/** Returns the identifier of the instance configuration. */
public InstanceConfigId getInstanceConfigId() {
return configId;
}
/** Returns the display name of the instance. */
public String getDisplayName() {
return displayName;
}
/** Returns the node count of the instance. */
public int getNodeCount() {
return nodeCount;
}
/** Returns the number of processing units of the instance. */
public int getProcessingUnits() {
return processingUnits;
}
/** Returns the current state of the instance. */
public State getState() {
return state;
}
/** Returns the cloud labels attached to this instance. */
public Map<String, String> getLabels() {
return labels;
}
public Builder toBuilder() {
return new BuilderImpl(this);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("name", id.getName())
.add("configName", configId == null ? null : configId.getName())
.add("displayName", displayName)
.add("nodeCount", nodeCount)
.add("processingUnits", processingUnits)
.add("state", state)
.add("labels", labels)
.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
InstanceInfo that = (InstanceInfo) o;
return id.equals(that.id)
&& Objects.equals(configId, that.configId)
&& Objects.equals(displayName, that.displayName)
&& nodeCount == that.nodeCount
&& processingUnits == that.processingUnits
&& state == that.state
&& Objects.equals(labels, that.labels);
}
@Override
public int hashCode() {
return Objects.hash(id, configId, displayName, nodeCount, processingUnits, state, labels);
}
com.google.spanner.admin.instance.v1.Instance toProto() {
com.google.spanner.admin.instance.v1.Instance.Builder builder =
com.google.spanner.admin.instance.v1.Instance.newBuilder()
.setName(getId().getName())
.setNodeCount(getNodeCount())
.setProcessingUnits(getProcessingUnits())
.putAllLabels(getLabels());
if (getDisplayName() != null) {
builder.setDisplayName(getDisplayName());
}
if (getInstanceConfigId() != null) {
builder.setConfig(getInstanceConfigId().getName());
}
return builder.build();
}
public static Builder newBuilder(InstanceId id) {
return new BuilderImpl(checkNotNull(id));
}
}