|
| 1 | +/* |
| 2 | + * Copyright 2015 Google Inc. All Rights Reserved. |
| 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.gcloud.storage; |
| 18 | + |
| 19 | +import static com.google.common.base.MoreObjects.firstNonNull; |
| 20 | +import static com.google.gcloud.RetryHelper.runWithRetries; |
| 21 | + |
| 22 | +import com.google.api.services.storage.model.RewriteResponse; |
| 23 | +import com.google.gcloud.RetryHelper; |
| 24 | +import com.google.gcloud.spi.StorageRpc; |
| 25 | + |
| 26 | +import java.math.BigInteger; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.concurrent.Callable; |
| 29 | + |
| 30 | +/** |
| 31 | + * Google Storage blob rewriter. |
| 32 | + */ |
| 33 | +public final class BlobRewriter { |
| 34 | + |
| 35 | + private final StorageOptions serviceOptions; |
| 36 | + private final BlobId source; |
| 37 | + private final Map<StorageRpc.Option, ?> sourceOptions; |
| 38 | + private final Map<StorageRpc.Option, ?> targetOptions; |
| 39 | + private final Long maxBytesRewrittenPerCall; |
| 40 | + private BigInteger blobSize; |
| 41 | + private BlobInfo target; |
| 42 | + private Boolean isDone; |
| 43 | + private String rewriteToken; |
| 44 | + private BigInteger totalBytesRewritten; |
| 45 | + |
| 46 | + private final StorageRpc storageRpc; |
| 47 | + |
| 48 | + private BlobRewriter(Builder builder) { |
| 49 | + this.serviceOptions = builder.serviceOptions; |
| 50 | + this.source = builder.source; |
| 51 | + this.sourceOptions = builder.sourceOptions; |
| 52 | + this.target = builder.target; |
| 53 | + this.targetOptions = builder.targetOptions; |
| 54 | + this.blobSize = builder.blobSize; |
| 55 | + this.isDone = builder.isDone; |
| 56 | + this.rewriteToken = builder.rewriteToken; |
| 57 | + this.totalBytesRewritten = firstNonNull(builder.totalBytesRewritten, BigInteger.ZERO); |
| 58 | + this.maxBytesRewrittenPerCall = builder.maxBytesRewrittenPerCall; |
| 59 | + this.storageRpc = serviceOptions.storageRpc(); |
| 60 | + } |
| 61 | + |
| 62 | + static class Builder { |
| 63 | + |
| 64 | + private final StorageOptions serviceOptions; |
| 65 | + private final BlobId source; |
| 66 | + private final Map<StorageRpc.Option, ?> sourceOptions; |
| 67 | + private final BlobInfo target; |
| 68 | + private final Map<StorageRpc.Option, ?> targetOptions; |
| 69 | + private BigInteger blobSize; |
| 70 | + private Boolean isDone; |
| 71 | + private String rewriteToken; |
| 72 | + private BigInteger totalBytesRewritten; |
| 73 | + private Long maxBytesRewrittenPerCall; |
| 74 | + |
| 75 | + Builder(StorageOptions serviceOptions, BlobId source, Map<StorageRpc.Option, ?> sourceOptions, |
| 76 | + BlobInfo target, Map<StorageRpc.Option, ?> targetOptions) { |
| 77 | + this.serviceOptions = serviceOptions; |
| 78 | + this.source = source; |
| 79 | + this.sourceOptions = sourceOptions; |
| 80 | + this.target = target; |
| 81 | + this.targetOptions = targetOptions; |
| 82 | + } |
| 83 | + |
| 84 | + Builder blobSize(BigInteger blobSize) { |
| 85 | + this.blobSize = blobSize; |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + Builder isDone(Boolean isDone) { |
| 90 | + this.isDone = isDone; |
| 91 | + return this; |
| 92 | + } |
| 93 | + |
| 94 | + Builder rewriteToken(String rewriteToken) { |
| 95 | + this.rewriteToken = rewriteToken; |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + Builder totalBytesRewritten(BigInteger totalBytesRewritten) { |
| 100 | + this.totalBytesRewritten = totalBytesRewritten; |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + Builder maxBytesRewrittenPerCall(Long maxBytesRewrittenPerCall) { |
| 105 | + this.maxBytesRewrittenPerCall = maxBytesRewrittenPerCall; |
| 106 | + return this; |
| 107 | + } |
| 108 | + |
| 109 | + BlobRewriter build() { |
| 110 | + return new BlobRewriter(this); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + static Builder builder(StorageOptions options, BlobId source, |
| 115 | + Map<StorageRpc.Option, ?> sourceOpt, |
| 116 | + BlobInfo target, Map<StorageRpc.Option, ?> targetOpt) { |
| 117 | + return new Builder(options, source, sourceOpt, target, targetOpt); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Returns the id of the source blob. |
| 122 | + */ |
| 123 | + public BlobId source() { |
| 124 | + return source; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Returns the info for the target blob. When {@link #isDone} is {@code true} this method returns |
| 129 | + * the updated information for the just written blob. |
| 130 | + */ |
| 131 | + public BlobInfo target() { |
| 132 | + return target; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Size of the blob being copied. Returns {@code null} until the first copy request returns. |
| 137 | + */ |
| 138 | + public BigInteger blobSize() { |
| 139 | + return blobSize; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Returns {@code true} of blob rewrite finished, {@code false} otherwise. |
| 144 | + */ |
| 145 | + public Boolean isDone() { |
| 146 | + return isDone; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Returns the token to be used to rewrite the next chunk of the blob. |
| 151 | + */ |
| 152 | + public String rewriteToken() { |
| 153 | + return rewriteToken; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Returns the number of bytes written. |
| 158 | + */ |
| 159 | + public BigInteger totalBytesRewritten() { |
| 160 | + return totalBytesRewritten; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Returns the maximum number of bytes to be copied with each {@link #copyChunk()} call. This |
| 165 | + * parameter is ignored if source and target blob share the same location and storage class as |
| 166 | + * rewrite is made with one single RPC. |
| 167 | + */ |
| 168 | + public Long maxBytesRewrittenPerCall() { |
| 169 | + return maxBytesRewrittenPerCall; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Rewrite the next chunk of the blob. An RPC is issued only if rewrite has not finished yet |
| 174 | + * ({@link #isDone} returns {@code false}). |
| 175 | + * |
| 176 | + * @throws StorageException upon failure |
| 177 | + */ |
| 178 | + public void copyChunk() { |
| 179 | + if (!isDone) { |
| 180 | + try { |
| 181 | + RewriteResponse response = runWithRetries(new Callable<RewriteResponse>() { |
| 182 | + @Override |
| 183 | + public RewriteResponse call() { |
| 184 | + return storageRpc.rewrite( |
| 185 | + source.toPb(), |
| 186 | + sourceOptions, |
| 187 | + target.toPb(), |
| 188 | + targetOptions, |
| 189 | + rewriteToken, |
| 190 | + maxBytesRewrittenPerCall); |
| 191 | + } |
| 192 | + }, serviceOptions.retryParams(), StorageImpl.EXCEPTION_HANDLER); |
| 193 | + rewriteToken = response.getRewriteToken(); |
| 194 | + isDone = response.getDone(); |
| 195 | + blobSize = response.getObjectSize(); |
| 196 | + totalBytesRewritten = response.getTotalBytesRewritten(); |
| 197 | + target = response.getResource() != null ? BlobInfo.fromPb(response.getResource()) : target; |
| 198 | + } catch (RetryHelper.RetryHelperException e) { |
| 199 | + throw StorageException.translateAndThrow(e); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments