|
| 1 | +/* |
| 2 | + * Copyright 2019 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 | + * http://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 | + |
| 17 | +package com.google.cloud.spanner; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkState; |
| 20 | + |
| 21 | +import com.google.cloud.spanner.SpannerImpl.SessionImpl; |
| 22 | +import com.google.cloud.spanner.SpannerImpl.SessionTransaction; |
| 23 | +import com.google.cloud.spanner.spi.v1.SpannerRpc; |
| 24 | +import com.google.protobuf.ByteString; |
| 25 | +import com.google.spanner.v1.BeginTransactionRequest; |
| 26 | +import com.google.spanner.v1.ExecuteSqlRequest; |
| 27 | +import com.google.spanner.v1.ExecuteSqlRequest.QueryMode; |
| 28 | +import com.google.spanner.v1.Transaction; |
| 29 | +import com.google.spanner.v1.TransactionOptions; |
| 30 | +import com.google.spanner.v1.TransactionSelector; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.concurrent.Callable; |
| 33 | + |
| 34 | +/** Partitioned DML transaction for bulk updates and deletes. */ |
| 35 | +class PartitionedDMLTransaction implements SessionTransaction { |
| 36 | + private final ByteString transactionId; |
| 37 | + private final SessionImpl session; |
| 38 | + private final SpannerRpc rpc; |
| 39 | + private volatile boolean isValid = true; |
| 40 | + |
| 41 | + PartitionedDMLTransaction(SessionImpl session, SpannerRpc rpc) { |
| 42 | + this.session = session; |
| 43 | + this.rpc = rpc; |
| 44 | + this.transactionId = initTransaction(); |
| 45 | + } |
| 46 | + |
| 47 | + private ByteString initTransaction() { |
| 48 | + final BeginTransactionRequest request = |
| 49 | + BeginTransactionRequest.newBuilder() |
| 50 | + .setSession(session.getName()) |
| 51 | + .setOptions( |
| 52 | + TransactionOptions.newBuilder() |
| 53 | + .setPartitionedDml(TransactionOptions.PartitionedDml.getDefaultInstance())) |
| 54 | + .build(); |
| 55 | + Transaction txn = |
| 56 | + SpannerImpl.runWithRetries( |
| 57 | + new Callable<Transaction>() { |
| 58 | + @Override |
| 59 | + public Transaction call() throws Exception { |
| 60 | + return rpc.beginTransaction(request, session.getOptions()); |
| 61 | + } |
| 62 | + }); |
| 63 | + if (txn.getId().isEmpty()) { |
| 64 | + throw SpannerExceptionFactory.newSpannerException( |
| 65 | + ErrorCode.INTERNAL, |
| 66 | + "Failed to init transaction, missing transaction id\n" + session.getName()); |
| 67 | + } |
| 68 | + return txn.getId(); |
| 69 | + } |
| 70 | + |
| 71 | + long executePartitionedUpdate(Statement statement) { |
| 72 | + checkState(isValid, "Partitioned DML has been invalidated by a new operation on the session"); |
| 73 | + final ExecuteSqlRequest.Builder builder = |
| 74 | + ExecuteSqlRequest.newBuilder() |
| 75 | + .setSql(statement.getSql()) |
| 76 | + .setQueryMode(QueryMode.NORMAL) |
| 77 | + .setSession(session.getName()) |
| 78 | + .setTransaction(TransactionSelector.newBuilder().setId(transactionId).build()); |
| 79 | + Map<String, Value> stmtParameters = statement.getParameters(); |
| 80 | + if (!stmtParameters.isEmpty()) { |
| 81 | + com.google.protobuf.Struct.Builder paramsBuilder = builder.getParamsBuilder(); |
| 82 | + for (Map.Entry<String, Value> param : stmtParameters.entrySet()) { |
| 83 | + paramsBuilder.putFields(param.getKey(), param.getValue().toProto()); |
| 84 | + builder.putParamTypes(param.getKey(), param.getValue().getType().toProto()); |
| 85 | + } |
| 86 | + } |
| 87 | + com.google.spanner.v1.ResultSet resultSet = |
| 88 | + SpannerImpl.runWithRetries( |
| 89 | + new Callable<com.google.spanner.v1.ResultSet>() { |
| 90 | + @Override |
| 91 | + public com.google.spanner.v1.ResultSet call() throws Exception { |
| 92 | + return rpc.executeQuery(builder.build(), session.getOptions()); |
| 93 | + } |
| 94 | + }); |
| 95 | + if (!resultSet.hasStats()) { |
| 96 | + throw new IllegalArgumentException( |
| 97 | + "Partitioned DML response missing stats possibly due to non-DML statement as input"); |
| 98 | + } |
| 99 | + // For partitioned DML, using the row count lower bound. |
| 100 | + return resultSet.getStats().getRowCountLowerBound(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void invalidate() { |
| 105 | + isValid = false; |
| 106 | + } |
| 107 | +} |
0 commit comments