|
| 1 | +/* |
| 2 | + * Copyright 2022-2025 Google LLC |
| 3 | + * Copyright 2013-2021 CompilerWorks |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.google.edwmigration.dumper.application.dumper.connector.snowflake; |
| 18 | + |
| 19 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 20 | +import static java.util.Objects.requireNonNull; |
| 21 | + |
| 22 | +import com.google.common.io.ByteSink; |
| 23 | +import com.google.common.io.CharSink; |
| 24 | +import com.google.edwmigration.dumper.application.dumper.ConnectorArguments; |
| 25 | +import com.google.edwmigration.dumper.application.dumper.handle.Handle; |
| 26 | +import com.google.edwmigration.dumper.application.dumper.task.AbstractTask; |
| 27 | +import com.google.edwmigration.dumper.application.dumper.task.TaskRunContext; |
| 28 | +import com.google.edwmigration.dumper.plugin.lib.dumper.spi.CoreMetadataDumpFormat; |
| 29 | +import com.google.edwmigration.dumper.plugin.lib.dumper.spi.CoreMetadataDumpFormat.CompilerWorksDumpMetadataTaskFormat; |
| 30 | +import com.google.edwmigration.dumper.plugin.lib.dumper.spi.CoreMetadataDumpFormat.CompilerWorksDumpMetadataTaskFormat.Product; |
| 31 | +import com.google.edwmigration.dumper.plugin.lib.dumper.spi.CoreMetadataDumpFormat.CompilerWorksDumpMetadataTaskFormat.Root; |
| 32 | +import java.io.IOException; |
| 33 | +import java.io.Writer; |
| 34 | +import javax.annotation.Nonnull; |
| 35 | +import javax.annotation.Nullable; |
| 36 | +import javax.annotation.ParametersAreNonnullByDefault; |
| 37 | +import org.anarres.jdiagnostics.ProductMetadata; |
| 38 | + |
| 39 | +/** A {@link Task} that creates YAML with extraction metadata. */ |
| 40 | +@ParametersAreNonnullByDefault |
| 41 | +abstract class SnowflakeYamlSummaryTask extends AbstractTask<Void> { |
| 42 | + |
| 43 | + private static final String zipEntryName = CompilerWorksDumpMetadataTaskFormat.ZIP_ENTRY_NAME; |
| 44 | + |
| 45 | + @Nonnull private final String zipFormat; |
| 46 | + |
| 47 | + @Override |
| 48 | + public final String describeSourceData() { |
| 49 | + return "containing dump metadata."; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected final Void doRun(@Nullable TaskRunContext context, ByteSink sink, Handle handle) |
| 54 | + throws IOException { |
| 55 | + Product product = new Product(); |
| 56 | + product.version = String.valueOf(new ProductMetadata()); |
| 57 | + product.arguments = serializedArguments(context); |
| 58 | + |
| 59 | + CharSink streamSupplier = sink.asCharSink(UTF_8); |
| 60 | + try (Writer writer = streamSupplier.openBufferedStream()) { |
| 61 | + |
| 62 | + Root root = new Root(); |
| 63 | + root.format = zipFormat; |
| 64 | + root.timestamp = System.currentTimeMillis(); |
| 65 | + root.product = product; |
| 66 | + CoreMetadataDumpFormat.MAPPER.writeValue(writer, root); |
| 67 | + return null; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Nonnull |
| 72 | + static SnowflakeYamlSummaryTask create(String zipFormat) { |
| 73 | + return new ContextArgumentsTask(zipFormat); |
| 74 | + } |
| 75 | + |
| 76 | + @Nonnull |
| 77 | + static SnowflakeYamlSummaryTask create(String zipFormat, ConnectorArguments arguments) { |
| 78 | + return new FixedArgumentsTask(zipFormat, arguments); |
| 79 | + } |
| 80 | + |
| 81 | + private SnowflakeYamlSummaryTask(String format) { |
| 82 | + super(zipEntryName); |
| 83 | + this.zipFormat = format; |
| 84 | + } |
| 85 | + |
| 86 | + @Nonnull |
| 87 | + abstract String serializedArguments(@Nullable TaskRunContext context); |
| 88 | + |
| 89 | + /** A task that takes arguments from {@link TaskRunContext}. */ |
| 90 | + private static class ContextArgumentsTask extends SnowflakeYamlSummaryTask { |
| 91 | + |
| 92 | + ContextArgumentsTask(String zipFormat) { |
| 93 | + super(zipFormat); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + @Nonnull |
| 98 | + String serializedArguments(@Nullable TaskRunContext context) { |
| 99 | + context = requireNonNull(context); |
| 100 | + return String.valueOf(context.getArguments()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** A task that stores its own {@link ConnectorArguments}. */ |
| 105 | + private static class FixedArgumentsTask extends SnowflakeYamlSummaryTask { |
| 106 | + |
| 107 | + @Nonnull final ConnectorArguments arguments; |
| 108 | + |
| 109 | + FixedArgumentsTask(String zipFormat, ConnectorArguments arguments) { |
| 110 | + super(zipFormat); |
| 111 | + this.arguments = arguments; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + @Nonnull |
| 116 | + String serializedArguments(@Nullable TaskRunContext unused) { |
| 117 | + return String.valueOf(arguments); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments