|
| 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.models; |
| 17 | + |
| 18 | +import com.google.api.core.InternalApi; |
| 19 | +import com.google.bigtable.v2.ReadModifyWriteRowRequest; |
| 20 | +import com.google.bigtable.v2.ReadModifyWriteRule; |
| 21 | +import com.google.bigtable.v2.TableName; |
| 22 | +import com.google.cloud.bigtable.data.v2.internal.RequestContext; |
| 23 | +import com.google.common.base.Preconditions; |
| 24 | +import com.google.protobuf.ByteString; |
| 25 | +import javax.annotation.Nonnull; |
| 26 | + |
| 27 | +/** Wraps a {@link ReadModifyWriteRowRequest}. */ |
| 28 | +public final class ReadModifyWriteRow { |
| 29 | + private final String tableId; |
| 30 | + private final ReadModifyWriteRowRequest.Builder builder = ReadModifyWriteRowRequest.newBuilder(); |
| 31 | + |
| 32 | + private ReadModifyWriteRow(@Nonnull String tableId, @Nonnull ByteString key) { |
| 33 | + Preconditions.checkNotNull(tableId, "tableId can't be null."); |
| 34 | + Preconditions.checkNotNull(key, "key can't be null."); |
| 35 | + this.tableId = tableId; |
| 36 | + |
| 37 | + builder.setRowKey(key); |
| 38 | + } |
| 39 | + |
| 40 | + public static ReadModifyWriteRow create(@Nonnull String tableId, @Nonnull String key) { |
| 41 | + Preconditions.checkNotNull(key, "key can't be null."); |
| 42 | + return new ReadModifyWriteRow(tableId, ByteString.copyFromUtf8(key)); |
| 43 | + } |
| 44 | + |
| 45 | + public static ReadModifyWriteRow create(@Nonnull String tableId, @Nonnull ByteString key) { |
| 46 | + return new ReadModifyWriteRow(tableId, key); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Appends the value to the existing value of the cell. If the targeted cell is unset, it will be |
| 51 | + * treated as containing the empty string. |
| 52 | + */ |
| 53 | + public ReadModifyWriteRow append( |
| 54 | + @Nonnull String familyName, @Nonnull String qualifier, @Nonnull String value) { |
| 55 | + return append(familyName, ByteString.copyFromUtf8(qualifier), ByteString.copyFromUtf8(value)); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Appends the value to the existing value of the cell. If the targeted cell is unset, it will be |
| 60 | + * treated as containing the empty string. |
| 61 | + */ |
| 62 | + public ReadModifyWriteRow append( |
| 63 | + @Nonnull String familyName, @Nonnull ByteString qualifier, @Nonnull ByteString value) { |
| 64 | + Validations.validateFamily(familyName); |
| 65 | + Preconditions.checkNotNull(qualifier, "Qualifier can't be null"); |
| 66 | + Preconditions.checkNotNull(value, "Value can't be null"); |
| 67 | + Preconditions.checkArgument(!value.isEmpty(), "Value can't be empty"); |
| 68 | + |
| 69 | + ReadModifyWriteRule rule = |
| 70 | + ReadModifyWriteRule.newBuilder() |
| 71 | + .setFamilyName(familyName) |
| 72 | + .setColumnQualifier(qualifier) |
| 73 | + .setAppendValue(value) |
| 74 | + .build(); |
| 75 | + |
| 76 | + builder.addRules(rule); |
| 77 | + |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Adds `amount` be added to the existing value. If the targeted cell is unset, it will be treated |
| 83 | + * as containing a zero. Otherwise, the targeted cell must contain an 8-byte value (interpreted as |
| 84 | + * a 64-bit big-endian signed integer), or the entire request will fail. |
| 85 | + */ |
| 86 | + public ReadModifyWriteRow increment( |
| 87 | + @Nonnull String familyName, @Nonnull String qualifier, long amount) { |
| 88 | + return increment(familyName, ByteString.copyFromUtf8(qualifier), amount); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Adds `amount` be added to the existing value. If the targeted cell is unset, it will be treated |
| 93 | + * as containing a zero. Otherwise, the targeted cell must contain an 8-byte value (interpreted as |
| 94 | + * a 64-bit big-endian signed integer), or the entire request will fail. |
| 95 | + */ |
| 96 | + public ReadModifyWriteRow increment( |
| 97 | + @Nonnull String familyName, @Nonnull ByteString qualifier, long amount) { |
| 98 | + Validations.validateFamily(familyName); |
| 99 | + Preconditions.checkNotNull(qualifier, "Qualifier can't be null"); |
| 100 | + |
| 101 | + ReadModifyWriteRule rule = |
| 102 | + ReadModifyWriteRule.newBuilder() |
| 103 | + .setFamilyName(familyName) |
| 104 | + .setColumnQualifier(qualifier) |
| 105 | + .setIncrementAmount(amount) |
| 106 | + .build(); |
| 107 | + |
| 108 | + builder.addRules(rule); |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + @InternalApi |
| 113 | + public ReadModifyWriteRowRequest toProto(RequestContext requestContext) { |
| 114 | + TableName tableName = |
| 115 | + TableName.of( |
| 116 | + requestContext.getInstanceName().getProject(), |
| 117 | + requestContext.getInstanceName().getInstance(), |
| 118 | + tableId); |
| 119 | + return builder |
| 120 | + .setTableName(tableName.toString()) |
| 121 | + .setAppProfileId(requestContext.getAppProfileId()) |
| 122 | + .build(); |
| 123 | + } |
| 124 | +} |
0 commit comments