Skip to content

Commit 0faa9fc

Browse files
igorbernstein2garrettjonesgoogle
authored andcommitted
---
yaml --- r: 9087 b: refs/heads/master c: 227b1fb h: refs/heads/master i: 9085: 7eef021 9083: f4357da 9079: d3024f5 9071: e9e74d5 9055: 8e4a544 9023: bf70211 8959: 830060d
1 parent d9d9344 commit 0faa9fc

4 files changed

Lines changed: 197 additions & 7 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 74fee09af9332f5a83a437839c38c833c6c26342
2+
refs/heads/master: 227b1fb65b7ea0a69bceb04438cc7ce89edc0131
33
refs/heads/travis: 47e4fee4fd5af9b2a8ce46f23c72ec95f9b195b2
44
refs/heads/gh-pages: 8e9b065ba06cd7a4af306aaea1010aade81670e0
55
refs/tags/0.0.9: 22f1839238f66c39e67ed4dfdcd273b1ae2e8444
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.stub;
17+
18+
import com.google.api.core.ApiFunction;
19+
import com.google.api.core.ApiFuture;
20+
import com.google.api.core.ApiFutures;
21+
import com.google.api.gax.rpc.ApiCallContext;
22+
import com.google.api.gax.rpc.UnaryCallable;
23+
import com.google.bigtable.v2.CheckAndMutateRowRequest;
24+
import com.google.bigtable.v2.CheckAndMutateRowResponse;
25+
import com.google.cloud.bigtable.data.v2.internal.RequestContext;
26+
import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation;
27+
28+
/** Simple wrapper for CheckAndMutateRow to wrap the request and response protobufs. */
29+
class CheckAndMutateRowCallable extends UnaryCallable<ConditionalRowMutation, Boolean> {
30+
private final UnaryCallable<CheckAndMutateRowRequest, CheckAndMutateRowResponse> inner;
31+
private final RequestContext requestContext;
32+
33+
CheckAndMutateRowCallable(
34+
UnaryCallable<CheckAndMutateRowRequest, CheckAndMutateRowResponse> inner,
35+
RequestContext requestContext) {
36+
this.inner = inner;
37+
this.requestContext = requestContext;
38+
}
39+
40+
@Override
41+
public ApiFuture<Boolean> futureCall(ConditionalRowMutation request, ApiCallContext context) {
42+
ApiFuture<CheckAndMutateRowResponse> rawResponse =
43+
inner.futureCall(request.toProto(requestContext), context);
44+
45+
return ApiFutures.transform(
46+
rawResponse,
47+
new ApiFunction<CheckAndMutateRowResponse, Boolean>() {
48+
@Override
49+
public Boolean apply(CheckAndMutateRowResponse checkAndMutateRowResponse) {
50+
return checkAndMutateRowResponse.getPredicateMatched();
51+
}
52+
});
53+
}
54+
}

trunk/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ public static EnhancedBigtableStub create(EnhancedBigtableStubSettings settings)
8989
.setRetryableCodes(settings.mutateRowSettings().getRetryableCodes())
9090
.setRetrySettings(settings.mutateRowSettings().getRetrySettings());
9191

92+
// CheckAndMutateRow is a simple passthrough
93+
baseSettingsBuilder
94+
.checkAndMutateRowSettings()
95+
.setRetryableCodes(settings.checkAndMutateRowSettings().getRetryableCodes())
96+
.setRetrySettings(settings.checkAndMutateRowSettings().getRetrySettings());
97+
9298
BigtableStubSettings baseSettings = baseSettingsBuilder.build();
9399
ClientContext clientContext = ClientContext.create(baseSettings);
94100
GrpcBigtableStub stub = new GrpcBigtableStub(baseSettings, clientContext);
@@ -194,13 +200,19 @@ public ApiFuture<Void> futureCall(RowMutation request, ApiCallContext context) {
194200
};
195201
}
196202

203+
/**
204+
* Creates a callable chain to handle CheckAndMutateRow RPCs. THe chain will:
205+
*
206+
* <ul>
207+
* <li>Convert {@link ConditionalRowMutation}s into {@link
208+
* com.google.bigtable.v2.CheckAndMutateRowRequest}s.
209+
* </ul>
210+
*/
197211
private UnaryCallable<ConditionalRowMutation, Boolean> createCheckAndMutateRowCallable() {
198-
return new UnaryCallable<ConditionalRowMutation, Boolean>() {
199-
@Override
200-
public ApiFuture<Boolean> futureCall(ConditionalRowMutation request, ApiCallContext context) {
201-
throw new UnsupportedOperationException("todo");
202-
}
203-
};
212+
CheckAndMutateRowCallable userFacing =
213+
new CheckAndMutateRowCallable(stub.checkAndMutateRowCallable(), requestContext);
214+
215+
return userFacing.withDefaultCallContext(clientContext.getDefaultCallContext());
204216
}
205217

206218
private UnaryCallable<ReadModifyWriteRow, Row> createReadModifyWriteRowCallable() {
@@ -230,6 +242,10 @@ public UnaryCallable<RowMutation, Void> mutateRowsCallable() {
230242
return mutateRowsCallable;
231243
}
232244

245+
/**
246+
* Returns the callable chain created in {@link #createCheckAndMutateRowCallable()} during stub
247+
* construction.
248+
*/
233249
public UnaryCallable<ConditionalRowMutation, Boolean> checkAndMutateRowCallable() {
234250
return checkAndMutateRowCallable;
235251
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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.stub;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
20+
import com.google.api.core.ApiFuture;
21+
import com.google.api.core.SettableApiFuture;
22+
import com.google.api.gax.grpc.GrpcStatusCode;
23+
import com.google.api.gax.rpc.ApiCallContext;
24+
import com.google.api.gax.rpc.NotFoundException;
25+
import com.google.api.gax.rpc.UnaryCallable;
26+
import com.google.bigtable.admin.v2.InstanceName;
27+
import com.google.bigtable.v2.CheckAndMutateRowRequest;
28+
import com.google.bigtable.v2.CheckAndMutateRowResponse;
29+
import com.google.bigtable.v2.Mutation.DeleteFromRow;
30+
import com.google.cloud.bigtable.data.v2.internal.RequestContext;
31+
import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation;
32+
import com.google.cloud.bigtable.data.v2.models.Mutation;
33+
import com.google.protobuf.ByteString;
34+
import io.grpc.Status.Code;
35+
import java.util.concurrent.ExecutionException;
36+
import java.util.concurrent.TimeUnit;
37+
import org.junit.Before;
38+
import org.junit.Test;
39+
import org.junit.runner.RunWith;
40+
import org.junit.runners.JUnit4;
41+
42+
@RunWith(JUnit4.class)
43+
public class CheckAndMutateRowCallableTest {
44+
private final RequestContext requestContext =
45+
RequestContext.create(InstanceName.of("my-project", "my-instance"), "my-app-profile");
46+
private FakeCallable inner;
47+
private CheckAndMutateRowCallable callable;
48+
49+
@Before
50+
public void setUp() {
51+
inner = new FakeCallable();
52+
callable = new CheckAndMutateRowCallable(inner, requestContext);
53+
}
54+
55+
@Test
56+
public void requestIsCorrect() {
57+
callable.futureCall(
58+
ConditionalRowMutation.create("my-table", "row-key").then(Mutation.create().deleteRow()));
59+
60+
assertThat(inner.request)
61+
.isEqualTo(
62+
CheckAndMutateRowRequest.newBuilder()
63+
.setTableName(requestContext.getInstanceName() + "/tables/my-table")
64+
.setRowKey(ByteString.copyFromUtf8("row-key"))
65+
.setAppProfileId(requestContext.getAppProfileId())
66+
.addTrueMutations(
67+
com.google.bigtable.v2.Mutation.newBuilder()
68+
.setDeleteFromRow(DeleteFromRow.getDefaultInstance()))
69+
.build());
70+
}
71+
72+
@Test
73+
public void responseCorrectlyTransformed() throws Exception {
74+
ApiFuture<Boolean> result =
75+
callable.futureCall(
76+
ConditionalRowMutation.create("my-table", "row-key")
77+
.then(Mutation.create().deleteRow()));
78+
79+
inner.response.set(CheckAndMutateRowResponse.newBuilder().setPredicateMatched(true).build());
80+
81+
assertThat(result.get(1, TimeUnit.SECONDS)).isEqualTo(true);
82+
}
83+
84+
@Test
85+
public void errorIsPropagated() throws Exception {
86+
ApiFuture<Boolean> result =
87+
callable.futureCall(
88+
ConditionalRowMutation.create("my-table", "row-key")
89+
.then(Mutation.create().deleteRow()));
90+
91+
Throwable expectedError =
92+
new NotFoundException("fake error", null, GrpcStatusCode.of(Code.NOT_FOUND), false);
93+
inner.response.setException(expectedError);
94+
95+
Throwable actualError = null;
96+
try {
97+
result.get(1, TimeUnit.SECONDS);
98+
} catch (ExecutionException e) {
99+
actualError = e.getCause();
100+
}
101+
102+
assertThat(actualError).isEqualTo(expectedError);
103+
}
104+
105+
static class FakeCallable
106+
extends UnaryCallable<CheckAndMutateRowRequest, CheckAndMutateRowResponse> {
107+
CheckAndMutateRowRequest request;
108+
ApiCallContext callContext;
109+
SettableApiFuture<CheckAndMutateRowResponse> response = SettableApiFuture.create();
110+
111+
@Override
112+
public ApiFuture<CheckAndMutateRowResponse> futureCall(
113+
CheckAndMutateRowRequest request, ApiCallContext context) {
114+
this.request = request;
115+
this.callContext = context;
116+
117+
return response;
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)