/
BulkMutation.java
145 lines (126 loc) · 5.11 KB
/
BulkMutation.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
/*
* 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.MutateRowsRequest;
import com.google.cloud.bigtable.data.v2.internal.NameUtil;
import com.google.cloud.bigtable.data.v2.internal.RequestContext;
import com.google.common.base.Preconditions;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javax.annotation.Nonnull;
/**
* Represents a list of mutations for multiple rows. Each mutation contains multiple changes that
* will be atomically applied to each row. However, ordering between rows is not guaranteed.
*
* <p>This class is meant for manual batching.
*/
public final class BulkMutation implements Serializable, Cloneable {
private static final long serialVersionUID = 3522061250439399088L;
private final String tableId;
private transient MutateRowsRequest.Builder builder;
public static BulkMutation create(String tableId) {
return new BulkMutation(tableId);
}
private BulkMutation(@Nonnull String tableId) {
Preconditions.checkNotNull(tableId);
this.tableId = tableId;
this.builder = MutateRowsRequest.newBuilder();
}
private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {
input.defaultReadObject();
builder = MutateRowsRequest.newBuilder().mergeFrom(input);
}
private void writeObject(ObjectOutputStream output) throws IOException {
output.defaultWriteObject();
builder.build().writeTo(output);
}
/**
* Add mutation for a particular row. The changes in the mutation will be applied atomically.
* However there is no guarantees about the relative ordering between mutations affecting
* different rows.
*/
public BulkMutation add(@Nonnull String rowKey, @Nonnull Mutation mutation) {
Preconditions.checkNotNull(rowKey);
Preconditions.checkNotNull(mutation);
return add(ByteString.copyFromUtf8(rowKey), mutation);
}
/**
* Add mutation for a particular row. The changes in the mutation will be applied atomic. However
* there is no guarantees about the relative ordering between mutations affecting different rows.
*/
public BulkMutation add(@Nonnull ByteString rowKey, @Nonnull Mutation mutation) {
Preconditions.checkNotNull(rowKey);
Preconditions.checkNotNull(mutation);
builder.addEntries(
MutateRowsRequest.Entry.newBuilder()
.setRowKey(rowKey)
.addAllMutations(mutation.getMutations())
.build());
return this;
}
/**
* Add mutation for a particular row. The changes in the mutation will be applied atomic. However
* there is no guarantees about the relative ordering between mutations affecting different rows.
*/
public BulkMutation add(@Nonnull RowMutationEntry entry) {
Preconditions.checkNotNull(entry, "Row mutation entry can't be null");
builder.addEntries(entry.toProto());
return this;
}
public int getEntryCount() {
return builder.getEntriesCount();
}
@InternalApi
public MutateRowsRequest toProto(RequestContext requestContext) {
String tableName =
NameUtil.formatTableName(
requestContext.getProjectId(), requestContext.getInstanceId(), tableId);
return builder
.setTableName(tableName)
.setAppProfileId(requestContext.getAppProfileId())
.build();
}
/**
* Wraps the protobuf {@link MutateRowsRequest}.
*
* <p>This is meant for advanced usage only. Please ensure that the MutateRowsRequest does not use
* server side timestamps. The BigtableDataClient assumes that mutation present in BulkMutation
* are idempotent and is configured to enable retries by default. If serverside timestamps are
* enabled then that can lead to duplicate mutations.
*
* <p>WARNING: 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.
*/
@BetaApi
public static BulkMutation fromProto(@Nonnull MutateRowsRequest request) {
BulkMutation bulkMutation =
BulkMutation.create(NameUtil.extractTableIdFromTableName(request.getTableName()));
bulkMutation.builder = request.toBuilder();
return bulkMutation;
}
/** Creates a copy of {@link BulkMutation}. */
@Override
public BulkMutation clone() {
BulkMutation bulkMutation = BulkMutation.create(tableId);
bulkMutation.builder = this.builder.clone();
return bulkMutation;
}
}