@Override public RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws StorageException { String sourceKey = fullname(rewriteRequest.source); // a little hackish, just good enough for the tests to work. if (!contents.containsKey(sourceKey)) { throw new StorageException(404, "File not found: " + sourceKey); } // if non-null, then we check the file's at that generation. Long generationMatch = null; for (Option option : rewriteRequest.targetOptions.keySet()) { // this is a bit of a hack, since we don't implement generations. if (option == Option.IF_GENERATION_MATCH) { generationMatch = (Long) rewriteRequest.targetOptions.get(option); } } String destKey = fullname(rewriteRequest.target); checkGeneration(destKey, generationMatch); metadata.put(destKey, rewriteRequest.target); byte[] data = contents.get(sourceKey); contents.put(destKey, Arrays.copyOf(data, data.length)); return new RewriteResponse( rewriteRequest, rewriteRequest.target, data.length, true, "rewriteToken goes here", data.length); }
setEncryptionHeaders(requestHeaders, ENCRYPTION_KEY_PREFIX, req.targetOptions); com.google.api.services.storage.model.RewriteResponse rewriteResponse = rewrite.execute(); return new RewriteResponse( req, rewriteResponse.getResource(),
@Override public RewriteResponse openRewrite(RewriteRequest rewriteRequest) throws StorageException { String sourceKey = fullname(rewriteRequest.source); // a little hackish, just good enough for the tests to work. if (!contents.containsKey(sourceKey)) { throw new StorageException(404, "File not found: " + sourceKey); } boolean mustNotExist = false; for (Option option : rewriteRequest.targetOptions.keySet()) { // this is a bit of a hack, since we don't implement generations. if (option == Option.IF_GENERATION_MATCH && (Long) rewriteRequest.targetOptions.get(option) == 0L) { mustNotExist = true; } } String destKey = fullname(rewriteRequest.target); if (mustNotExist && contents.containsKey(destKey)) { throw new StorageException(new FileAlreadyExistsException(destKey)); } metadata.put(destKey, rewriteRequest.target); byte[] data = contents.get(sourceKey); contents.put(destKey, Arrays.copyOf(data, data.length)); return new RewriteResponse(rewriteRequest, rewriteRequest.target, data.length, true, "rewriteToken goes here", data.length); }
@Override public CopyWriter restore() { RewriteRequest rewriteRequest = new RewriteRequest( source.toPb(), sourceOptions, overrideInfo, target.toPb(), targetOptions, megabytesCopiedPerChunk); RewriteResponse rewriteResponse = new RewriteResponse( rewriteRequest, result != null ? result.toPb() : null, blobSize, isDone, rewriteToken, totalBytesCopied); return new CopyWriter(serviceOptions, rewriteResponse); }
setEncryptionHeaders(requestHeaders, ENCRYPTION_KEY_PREFIX, req.targetOptions); com.google.api.services.storage.model.RewriteResponse rewriteResponse = rewrite.execute(); return new RewriteResponse( req, rewriteResponse.getResource(),
null); StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse).times(2); EasyMock.replay(storageRpcMock);
null); StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse).times(2); EasyMock.replay(storageRpcMock);
@Test public void testCopyWithOptionsFromBlobId() { CopyRequest request = Storage.CopyRequest.newBuilder() .setSource(BLOB_INFO1.getBlobId()) .setSourceOptions(BLOB_SOURCE_GENERATION_FROM_BLOB_ID, BLOB_SOURCE_METAGENERATION) .setTarget(BLOB_INFO1, BLOB_TARGET_GENERATION, BLOB_TARGET_METAGENERATION) .build(); StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest( request.getSource().toPb(), BLOB_SOURCE_OPTIONS_COPY, true, request.getTarget().toPb(), BLOB_TARGET_OPTIONS_COMPOSE, null); StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse); EasyMock.replay(storageRpcMock); initializeService(); CopyWriter writer = storage.copy(request); assertEquals(42L, writer.getBlobSize()); assertEquals(21L, writer.getTotalBytesCopied()); assertTrue(!writer.isDone()); }
@Test public void testCopyMultipleRequests() { CopyRequest request = Storage.CopyRequest.of(BLOB_INFO1.getBlobId(), BLOB_INFO2.getBlobId()); StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest( request.getSource().toPb(), EMPTY_RPC_OPTIONS, false, BLOB_INFO2.toPb(), EMPTY_RPC_OPTIONS, null); StorageRpc.RewriteResponse rpcResponse1 = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); StorageRpc.RewriteResponse rpcResponse2 = new StorageRpc.RewriteResponse(rpcRequest, BLOB_INFO1.toPb(), 42L, true, "token", 42L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse1); EasyMock.expect(storageRpcMock.continueRewrite(rpcResponse1)).andReturn(rpcResponse2); EasyMock.replay(storageRpcMock); initializeService(); CopyWriter writer = storage.copy(request); assertEquals(42L, writer.getBlobSize()); assertEquals(21L, writer.getTotalBytesCopied()); assertTrue(!writer.isDone()); assertEquals(expectedBlob1, writer.getResult()); assertTrue(writer.isDone()); assertEquals(42L, writer.getTotalBytesCopied()); assertEquals(42L, writer.getBlobSize()); }
@Test public void testCopyWithOptions() { CopyRequest request = Storage.CopyRequest.newBuilder() .setSource(BLOB_INFO2.getBlobId()) .setSourceOptions(BLOB_SOURCE_GENERATION, BLOB_SOURCE_METAGENERATION) .setTarget(BLOB_INFO1, BLOB_TARGET_GENERATION, BLOB_TARGET_METAGENERATION) .build(); StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest( request.getSource().toPb(), BLOB_SOURCE_OPTIONS_COPY, true, request.getTarget().toPb(), BLOB_TARGET_OPTIONS_COMPOSE, null); StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse); EasyMock.replay(storageRpcMock); initializeService(); CopyWriter writer = storage.copy(request); assertEquals(42L, writer.getBlobSize()); assertEquals(21L, writer.getTotalBytesCopied()); assertTrue(!writer.isDone()); }
@Test public void testCopy() { CopyRequest request = Storage.CopyRequest.of(BLOB_INFO1.getBlobId(), BLOB_INFO2.getBlobId()); StorageRpc.RewriteRequest rpcRequest = new StorageRpc.RewriteRequest( request.getSource().toPb(), EMPTY_RPC_OPTIONS, false, BLOB_INFO2.toPb(), EMPTY_RPC_OPTIONS, null); StorageRpc.RewriteResponse rpcResponse = new StorageRpc.RewriteResponse(rpcRequest, null, 42L, false, "token", 21L); EasyMock.expect(storageRpcMock.openRewrite(rpcRequest)).andReturn(rpcResponse); EasyMock.replay(storageRpcMock); initializeService(); CopyWriter writer = storage.copy(request); assertEquals(42L, writer.getBlobSize()); assertEquals(21L, writer.getTotalBytesCopied()); assertTrue(!writer.isDone()); }
@Override public CopyWriter restore() { RewriteRequest rewriteRequest = new RewriteRequest( source.toPb(), sourceOptions, overrideInfo, target.toPb(), targetOptions, megabytesCopiedPerChunk); RewriteResponse rewriteResponse = new RewriteResponse( rewriteRequest, result != null ? result.toPb() : null, blobSize, isDone, rewriteToken, totalBytesCopied); return new CopyWriter(serviceOptions, rewriteResponse); }