-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Bigtable 12. Implement SampleRowKeys #2913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pongad
merged 8 commits into
googleapis:master
from
igorbernstein2:12-impl-sample-row-keys
Feb 23, 2018
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c907dcf
Bigtable 12. Implement SampleRowKeys
igorbernstein2 98ed0e5
codacy fixes
igorbernstein2 1d47bfd
static import truth
igorbernstein2 3aa1951
one more codacy fix
igorbernstein2 6047a3a
improve disable retries code
igorbernstein2 7199a84
fix imports
igorbernstein2 df70853
address feedback
igorbernstein2 c5b06fb
rename wrappers package
igorbernstein2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/SampleRowKeysCallable.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * 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.stub; | ||
|
|
||
| import com.google.api.core.ApiFunction; | ||
| import com.google.api.core.ApiFuture; | ||
| import com.google.api.core.ApiFutures; | ||
| import com.google.api.gax.rpc.ApiCallContext; | ||
| import com.google.api.gax.rpc.UnaryCallable; | ||
| import com.google.bigtable.v2.SampleRowKeysRequest; | ||
| import com.google.bigtable.v2.SampleRowKeysResponse; | ||
| import com.google.bigtable.v2.TableName; | ||
| import com.google.cloud.bigtable.data.v2.internal.RequestContext; | ||
| import com.google.cloud.bigtable.data.v2.models.KeyOffset; | ||
| import com.google.common.collect.ImmutableList; | ||
| import java.util.List; | ||
|
|
||
| /** Simple wrapper for SampleRowKeys to wrap the request and response protobufs. */ | ||
| class SampleRowKeysCallable extends UnaryCallable<String, List<KeyOffset>> { | ||
| private final RequestContext requestContext; | ||
| private final UnaryCallable<SampleRowKeysRequest, List<SampleRowKeysResponse>> inner; | ||
|
|
||
| SampleRowKeysCallable( | ||
| UnaryCallable<SampleRowKeysRequest, List<SampleRowKeysResponse>> inner, | ||
| RequestContext requestContext) { | ||
|
|
||
| this.requestContext = requestContext; | ||
| this.inner = inner; | ||
| } | ||
|
|
||
| @Override | ||
| public ApiFuture<List<KeyOffset>> futureCall(String tableId, ApiCallContext context) { | ||
| TableName tableName = | ||
| TableName.of( | ||
| requestContext.getInstanceName().getProject(), | ||
| requestContext.getInstanceName().getInstance(), | ||
| tableId); | ||
|
|
||
| SampleRowKeysRequest request = | ||
| SampleRowKeysRequest.newBuilder() | ||
| .setTableName(tableName.toString()) | ||
| .setAppProfileId(requestContext.getAppProfileId()) | ||
| .build(); | ||
|
|
||
| ApiFuture<List<SampleRowKeysResponse>> rawResponse = inner.futureCall(request, context); | ||
|
|
||
| return ApiFutures.transform( | ||
| rawResponse, | ||
| new ApiFunction<List<SampleRowKeysResponse>, List<KeyOffset>>() { | ||
| @Override | ||
| public List<KeyOffset> apply(List<SampleRowKeysResponse> rawResponse) { | ||
| return convert(rawResponse); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private static List<KeyOffset> convert(List<SampleRowKeysResponse> rawResponse) { | ||
| ImmutableList.Builder<KeyOffset> results = ImmutableList.builder(); | ||
|
|
||
| for (SampleRowKeysResponse element : rawResponse) { | ||
| results.add(KeyOffset.create(element.getRowKey(), element.getOffsetBytes())); | ||
| } | ||
|
|
||
| return results.build(); | ||
| } | ||
| } | ||
122 changes: 122 additions & 0 deletions
122
...table/src/test/java/com/google/cloud/bigtable/data/v2/stub/SampleRowKeysCallableTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * 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.stub; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
|
|
||
| import com.google.api.core.ApiFuture; | ||
| import com.google.api.core.SettableApiFuture; | ||
| import com.google.api.gax.grpc.GrpcStatusCode; | ||
| import com.google.api.gax.rpc.ApiCallContext; | ||
| import com.google.api.gax.rpc.NotFoundException; | ||
| import com.google.api.gax.rpc.UnaryCallable; | ||
| import com.google.bigtable.admin.v2.InstanceName; | ||
| import com.google.bigtable.v2.SampleRowKeysRequest; | ||
| import com.google.bigtable.v2.SampleRowKeysResponse; | ||
| import com.google.cloud.bigtable.data.v2.internal.RequestContext; | ||
| import com.google.cloud.bigtable.data.v2.models.KeyOffset; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.protobuf.ByteString; | ||
| import io.grpc.Status.Code; | ||
| import java.util.List; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class SampleRowKeysCallableTest { | ||
| private final RequestContext requestContext = | ||
| RequestContext.create(InstanceName.of("my-project", "my-instance"), "my-app-profile"); | ||
| private FakeCallable inner; | ||
| private SampleRowKeysCallable callable; | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| inner = new FakeCallable(); | ||
| callable = new SampleRowKeysCallable(inner, requestContext); | ||
| } | ||
|
|
||
| @Test | ||
| public void requestIsCorrect() { | ||
| callable.futureCall("my-table"); | ||
|
|
||
| assertThat(inner.request) | ||
| .isEqualTo( | ||
| SampleRowKeysRequest.newBuilder() | ||
| .setTableName(requestContext.getInstanceName() + "/tables/my-table") | ||
| .setAppProfileId(requestContext.getAppProfileId()) | ||
| .build()); | ||
| } | ||
|
|
||
| @Test | ||
| public void responseCorrectlyTransformed() throws Exception { | ||
| ApiFuture<List<KeyOffset>> result = callable.futureCall("my-table"); | ||
|
|
||
| inner.response.set( | ||
| ImmutableList.of( | ||
| SampleRowKeysResponse.newBuilder() | ||
| .setRowKey(ByteString.copyFromUtf8("key1")) | ||
| .setOffsetBytes(100) | ||
| .build(), | ||
| SampleRowKeysResponse.newBuilder() | ||
| .setRowKey(ByteString.copyFromUtf8("")) | ||
| .setOffsetBytes(1000) | ||
| .build())); | ||
|
|
||
| assertThat(result.get(1, TimeUnit.SECONDS)) | ||
| .isEqualTo( | ||
| ImmutableList.of( | ||
| KeyOffset.create(ByteString.copyFromUtf8("key1"), 100), | ||
| KeyOffset.create(ByteString.EMPTY, 1000))); | ||
| } | ||
|
|
||
| @Test | ||
| public void errorIsPropagated() throws Exception { | ||
| ApiFuture<List<KeyOffset>> result = callable.futureCall("my-table"); | ||
|
|
||
| Throwable expectedError = | ||
| new NotFoundException("fake error", null, GrpcStatusCode.of(Code.NOT_FOUND), false); | ||
| inner.response.setException(expectedError); | ||
|
|
||
| Throwable actualError = null; | ||
| try { | ||
| result.get(1, TimeUnit.SECONDS); | ||
| } catch (ExecutionException e) { | ||
| actualError = e.getCause(); | ||
| } | ||
|
|
||
| assertThat(actualError).isEqualTo(expectedError); | ||
| } | ||
|
|
||
| static class FakeCallable | ||
| extends UnaryCallable<SampleRowKeysRequest, List<SampleRowKeysResponse>> { | ||
| SampleRowKeysRequest request; | ||
| ApiCallContext callContext; | ||
| SettableApiFuture<List<SampleRowKeysResponse>> response = SettableApiFuture.create(); | ||
|
|
||
| @Override | ||
| public ApiFuture<List<SampleRowKeysResponse>> futureCall( | ||
| SampleRowKeysRequest request, ApiCallContext context) { | ||
| this.request = request; | ||
| this.callContext = context; | ||
|
|
||
| return response; | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.