/
Mutation.java
303 lines (255 loc) · 10.3 KB
/
Mutation.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Copyright 2018 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
*
* https://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.bigtable.data.v2.models;
import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.bigtable.v2.Mutation.DeleteFromColumn;
import com.google.bigtable.v2.Mutation.DeleteFromFamily;
import com.google.bigtable.v2.Mutation.DeleteFromRow;
import com.google.bigtable.v2.Mutation.SetCell;
import com.google.cloud.bigtable.data.v2.models.Range.TimestampRange;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Longs;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.List;
import javax.annotation.Nonnull;
/**
* The concrete implementation of {@link MutationApi} that can be used to create and represent a
* list of mutations. It used by {@link RowMutation} and {@link ConditionalRowMutation} to
* encapsulate a list of mutations that will to be applied to a single row.
*/
public final class Mutation implements MutationApi<Mutation>, Serializable {
private static final long serialVersionUID = 5893216644683374340L;
@InternalApi("Visible for testing")
static final int MAX_MUTATIONS = 100_000;
@InternalApi("Visible for testing")
static final int MAX_BYTE_SIZE = 200 * 1024 * 1024;
@InternalApi("Visible for testing")
static final long SERVER_SIDE_TIMESTAMP = -1;
private final boolean allowServersideTimestamp;
private transient ImmutableList.Builder<com.google.bigtable.v2.Mutation> mutations =
ImmutableList.builder();
private int numMutations;
private long byteSize;
/** Creates new instance of Mutation object. */
public static Mutation create() {
return new Mutation(false);
}
/**
* Creates new instance of Mutation object which allows setCell operation to use server side
* timestamp. This is dangerous because mutations will no longer be idempotent, which might cause
* multiple duplicate values to be stored in Bigtable. This option should only be used for
* advanced usecases with extreme care.
*/
@BetaApi
public static Mutation createUnsafe() {
return new Mutation(true);
}
/**
* Wraps the List of protobuf {@link com.google.bigtable.v2.Mutation}. This methods, like {@link
* #createUnsafe()}, allows setCell operation to use server side timestamp. This is dangerous
* because mutations will no longer be idempotent, which might cause multiple duplicate values to
* be stored in Bigtable. This option should only be used for advanced usecases with extreme care.
*/
@BetaApi
public static Mutation fromProtoUnsafe(List<com.google.bigtable.v2.Mutation> protos) {
Mutation mutation = new Mutation(true);
mutation.mutations.addAll(protos);
return mutation;
}
/**
* Constructs a row mutation from an existing protobuf object.
*
* <p>Callers must ensure that the protobuf argument is not using serverside timestamps. The
* client assumes that all mutations are idempotent and will retry in case of transient errors.
* This can lead to row duplication.
*
* <p>When applied, the resulting Mutation object will ignore the project id and instance id in
* the table_name and instead apply the configuration in the client
*/
static Mutation fromProto(List<com.google.bigtable.v2.Mutation> protos) {
Mutation mutation = new Mutation(false);
mutation.mutations.addAll(protos);
return mutation;
}
private Mutation(boolean allowServersideTimestamp) {
this.allowServersideTimestamp = allowServersideTimestamp;
}
private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {
input.defaultReadObject();
@SuppressWarnings("unchecked")
ImmutableList<com.google.bigtable.v2.Mutation> deserialized =
(ImmutableList<com.google.bigtable.v2.Mutation>) input.readObject();
this.mutations = ImmutableList.<com.google.bigtable.v2.Mutation>builder().addAll(deserialized);
}
private void writeObject(ObjectOutputStream output) throws IOException {
output.defaultWriteObject();
output.writeObject(mutations.build());
}
@Override
public Mutation setCell(
@Nonnull String familyName, @Nonnull String qualifier, @Nonnull String value) {
return setCell(familyName, wrapByteString(qualifier), wrapByteString(value));
}
@Override
public Mutation setCell(
@Nonnull String familyName,
@Nonnull String qualifier,
long timestamp,
@Nonnull String value) {
return setCell(familyName, wrapByteString(qualifier), timestamp, wrapByteString(value));
}
@Override
public Mutation setCell(
@Nonnull String familyName, @Nonnull ByteString qualifier, @Nonnull ByteString value) {
long timestamp = System.currentTimeMillis() * 1_000;
return setCell(familyName, qualifier, timestamp, value);
}
@Override
public Mutation setCell(
@Nonnull String familyName,
@Nonnull ByteString qualifier,
long timestamp,
@Nonnull ByteString value) {
Validations.validateFamily(familyName);
Preconditions.checkNotNull(qualifier, "qualifier can't be null.");
Preconditions.checkNotNull(value, "value can't be null.");
if (!allowServersideTimestamp) {
Preconditions.checkArgument(
timestamp != SERVER_SIDE_TIMESTAMP, "Serverside timestamps are not supported");
}
addMutation(
com.google.bigtable.v2.Mutation.newBuilder()
.setSetCell(
SetCell.newBuilder()
.setFamilyName(familyName)
.setColumnQualifier(qualifier)
.setTimestampMicros(timestamp)
.setValue(value)
.build())
.build());
return this;
}
@Override
public Mutation setCell(@Nonnull String familyName, @Nonnull String qualifier, long value) {
return setCell(familyName, wrapByteString(qualifier), value);
}
@Override
public Mutation setCell(
@Nonnull String familyName, @Nonnull String qualifier, long timestamp, long value) {
return setCell(familyName, wrapByteString(qualifier), timestamp, value);
}
@Override
public Mutation setCell(@Nonnull String familyName, @Nonnull ByteString qualifier, long value) {
return setCell(familyName, qualifier, ByteString.copyFrom(Longs.toByteArray(value)));
}
@Override
public Mutation setCell(
@Nonnull String familyName, @Nonnull ByteString qualifier, long timestamp, long value) {
return setCell(familyName, qualifier, timestamp, ByteString.copyFrom(Longs.toByteArray(value)));
}
@Override
public Mutation deleteCells(@Nonnull String familyName, @Nonnull String qualifier) {
return deleteCells(familyName, wrapByteString(qualifier));
}
@Override
public Mutation deleteCells(@Nonnull String familyName, @Nonnull ByteString qualifier) {
Validations.validateFamily(familyName);
Preconditions.checkNotNull(qualifier, "qualifier can't be null.");
return deleteCells(familyName, qualifier, TimestampRange.unbounded());
}
@Override
public Mutation deleteCells(
@Nonnull String familyName,
@Nonnull ByteString qualifier,
@Nonnull TimestampRange timestampRange) {
Validations.validateFamily(familyName);
Preconditions.checkNotNull(qualifier, "qualifier can't be null.");
Preconditions.checkNotNull(timestampRange, "timestampRange can't be null.");
DeleteFromColumn.Builder builder =
DeleteFromColumn.newBuilder().setFamilyName(familyName).setColumnQualifier(qualifier);
switch (timestampRange.getStartBound()) {
case CLOSED:
builder.getTimeRangeBuilder().setStartTimestampMicros(timestampRange.getStart());
break;
case OPEN:
builder.getTimeRangeBuilder().setStartTimestampMicros(timestampRange.getStart() + 1);
break;
case UNBOUNDED:
break;
default:
throw new IllegalArgumentException(
"Unknown start bound: " + timestampRange.getStartBound());
}
switch (timestampRange.getEndBound()) {
case CLOSED:
builder.getTimeRangeBuilder().setEndTimestampMicros(timestampRange.getEnd() + 1);
break;
case OPEN:
builder.getTimeRangeBuilder().setEndTimestampMicros(timestampRange.getEnd());
break;
case UNBOUNDED:
break;
default:
throw new IllegalArgumentException("Unknown end bound: " + timestampRange.getEndBound());
}
addMutation(
com.google.bigtable.v2.Mutation.newBuilder().setDeleteFromColumn(builder.build()).build());
return this;
}
@Override
public Mutation deleteFamily(@Nonnull String familyName) {
Validations.validateFamily(familyName);
addMutation(
com.google.bigtable.v2.Mutation.newBuilder()
.setDeleteFromFamily(DeleteFromFamily.newBuilder().setFamilyName(familyName).build())
.build());
return this;
}
@Override
public Mutation deleteRow() {
addMutation(
com.google.bigtable.v2.Mutation.newBuilder()
.setDeleteFromRow(DeleteFromRow.getDefaultInstance())
.build());
return this;
}
private void addMutation(com.google.bigtable.v2.Mutation mutation) {
Preconditions.checkState(numMutations + 1 <= MAX_MUTATIONS, "Too many mutations per row");
Preconditions.checkState(
byteSize + mutation.getSerializedSize() <= MAX_BYTE_SIZE,
"Byte size of mutations is too large");
numMutations++;
byteSize += mutation.getSerializedSize();
mutations.add(mutation);
}
private static ByteString wrapByteString(String str) {
if (str == null) {
return null;
} else {
return ByteString.copyFromUtf8(str);
}
}
/** Returns the mutation protos. */
List<com.google.bigtable.v2.Mutation> getMutations() {
return mutations.build();
}
}