-
Notifications
You must be signed in to change notification settings - Fork 81
/
ConditionalRowMutation.java
187 lines (164 loc) · 7.1 KB
/
ConditionalRowMutation.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
/*
* 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.CheckAndMutateRowRequest;
import com.google.cloud.bigtable.data.v2.internal.NameUtil;
import com.google.cloud.bigtable.data.v2.internal.RequestContext;
import com.google.cloud.bigtable.data.v2.models.Filters.Filter;
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;
/** Mutates a row atomically based on the output of a condition filter. */
public final class ConditionalRowMutation implements Serializable {
private static final long serialVersionUID = -3699904745621909502L;
private final TargetId targetId;
private transient CheckAndMutateRowRequest.Builder builder =
CheckAndMutateRowRequest.newBuilder();
private ConditionalRowMutation(TargetId targetId, ByteString rowKey) {
Preconditions.checkNotNull(targetId, "target id can't be null.");
this.targetId = targetId;
builder.setRowKey(rowKey);
}
/** @deprecated Please use {@link ConditionalRowMutation#create(TargetId, String)} instead. */
@Deprecated
public static ConditionalRowMutation create(String tableId, String rowKey) {
return create(tableId, ByteString.copyFromUtf8(rowKey));
}
/**
* Creates a new instance of the mutation builder for the given target with targetId.
*
* @see AuthorizedViewId
* @see TableId
*/
public static ConditionalRowMutation create(TargetId targetId, String rowKey) {
return create(targetId, ByteString.copyFromUtf8(rowKey));
}
/** @deprecated Please use {@link ConditionalRowMutation#create(TargetId, ByteString)} instead. */
@Deprecated
public static ConditionalRowMutation create(String tableId, ByteString rowKey) {
Validations.validateTableId(tableId);
return new ConditionalRowMutation(TableId.of(tableId), rowKey);
}
/**
* Creates a new instance of the mutation builder for the given target with targetId.
*
* @see AuthorizedViewId
* @see TableId
*/
public static ConditionalRowMutation create(TargetId targetId, ByteString rowKey) {
return new ConditionalRowMutation(targetId, rowKey);
}
private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {
input.defaultReadObject();
builder = CheckAndMutateRowRequest.newBuilder().mergeFrom(input);
}
private void writeObject(ObjectOutputStream output) throws IOException {
output.defaultWriteObject();
builder.build().writeTo(output);
}
/**
* The filter to be applied to the contents of the specified row. Depending on whether or not any
* results are yielded, either the mutations added via {@link #then(Mutation)} or {@link
* #otherwise(Mutation)} will be executed. If unset, checks that the row contains any values at
* all.
*
* <p>Unlike {@link #then(Mutation)} and {@link #otherwise(Mutation)}, only a single condition can
* be set. However that filter can be a {@link Filters#chain()} or {@link Filters#interleave()},
* which can wrap multiple other filters.
*
* <p>WARNING: {@link Filters#condition(Filter)} is not supported.
*/
public ConditionalRowMutation condition(@Nonnull Filter condition) {
Preconditions.checkNotNull(condition);
Preconditions.checkState(
!builder.hasPredicateFilter(),
"Can only have a single condition, please use a Filters#chain or Filters#interleave filter"
+ " instead");
// TODO: verify that the condition does not use any FILTERS.condition() filters
builder.setPredicateFilter(condition.toProto());
return this;
}
/**
* Adds changes to be atomically applied to the specified row if the condition yields at least one
* cell when applied to the row.
*
* <p>Each {@code mutation} can specify multiple changes and the changes are accumulated each time
* this method is called. Mutations are applied in order, meaning that earlier mutations can be
* masked by later ones. Must contain at least one entry if {@link #otherwise(Mutation)} is empty,
* and at most 100000.
*/
public ConditionalRowMutation then(@Nonnull Mutation mutation) {
Preconditions.checkNotNull(mutation);
builder.addAllTrueMutations(mutation.getMutations());
return this;
}
/**
* Adds changes to be atomically applied to the specified row if the condition does not yields any
* cells when applied to the row.
*
* <p>Each {@code mutation} can specify multiple changes and the changes are accumulated each time
* this method is called. Mutations are applied in order, meaning that earlier mutations can be
* masked by later ones. Must contain at least one entry if {@link #then(Mutation)} is empty, and
* at most 100000.
*/
public ConditionalRowMutation otherwise(@Nonnull Mutation mutation) {
Preconditions.checkNotNull(mutation);
builder.addAllFalseMutations(mutation.getMutations());
return this;
}
/**
* Creates the underlying {@link CheckAndMutateRowRequest} protobuf.
*
* <p>This method is considered an internal implementation detail and not meant to be used by
* applications.
*/
@InternalApi
public CheckAndMutateRowRequest toProto(RequestContext requestContext) {
Preconditions.checkState(
!builder.getTrueMutationsList().isEmpty() || !builder.getFalseMutationsList().isEmpty(),
"ConditionalRowMutations must have `then` or `otherwise` mutations.");
String resourceName =
targetId.toResourceName(requestContext.getProjectId(), requestContext.getInstanceId());
if (targetId.scopedForAuthorizedView()) {
builder.setAuthorizedViewName(resourceName);
} else {
builder.setTableName(resourceName);
}
return builder.setAppProfileId(requestContext.getAppProfileId()).build();
}
/**
* Wraps the protobuf {@link CheckAndMutateRowRequest}.
*
* <p>WARNING: Please note that the table_name will be overwritten by the configuration in the
* BigtableDataClient.
*/
@BetaApi
public static ConditionalRowMutation fromProto(@Nonnull CheckAndMutateRowRequest request) {
String tableName = request.getTableName();
String authorizedViewName = request.getAuthorizedViewName();
ConditionalRowMutation rowMutation =
ConditionalRowMutation.create(
NameUtil.extractTargetId(tableName, authorizedViewName), request.getRowKey());
rowMutation.builder = request.toBuilder();
return rowMutation;
}
}