|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iceberg; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 23 | +import com.fasterxml.jackson.databind.JsonNode; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.StringWriter; |
| 26 | +import java.io.UncheckedIOException; |
| 27 | +import java.util.Locale; |
| 28 | +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; |
| 29 | +import org.apache.iceberg.util.JsonUtil; |
| 30 | + |
| 31 | +public class SnapshotRefParser { |
| 32 | + |
| 33 | + private SnapshotRefParser() { |
| 34 | + } |
| 35 | + |
| 36 | + private static final String SNAPSHOT_ID = "snapshot-id"; |
| 37 | + private static final String TYPE = "type"; |
| 38 | + private static final String MIN_SNAPSHOTS_TO_KEEP = "min-snapshots-to-keep"; |
| 39 | + private static final String MAX_SNAPSHOT_AGE_MS = "max-snapshot-age-ms"; |
| 40 | + private static final String MAX_REF_AGE_MS = "max-ref-age-ms"; |
| 41 | + |
| 42 | + public static String toJson(SnapshotRef ref) { |
| 43 | + return toJson(ref, false); |
| 44 | + } |
| 45 | + |
| 46 | + public static String toJson(SnapshotRef ref, boolean pretty) { |
| 47 | + try { |
| 48 | + StringWriter writer = new StringWriter(); |
| 49 | + JsonGenerator generator = JsonUtil.factory().createGenerator(writer); |
| 50 | + if (pretty) { |
| 51 | + generator.useDefaultPrettyPrinter(); |
| 52 | + } |
| 53 | + |
| 54 | + toJson(ref, generator); |
| 55 | + generator.flush(); |
| 56 | + return writer.toString(); |
| 57 | + } catch (IOException e) { |
| 58 | + throw new UncheckedIOException(e); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public static void toJson(SnapshotRef ref, JsonGenerator generator) throws IOException { |
| 63 | + generator.writeStartObject(); |
| 64 | + generator.writeNumberField(SNAPSHOT_ID, ref.snapshotId()); |
| 65 | + generator.writeStringField(TYPE, ref.type().name().toLowerCase(Locale.ENGLISH)); |
| 66 | + JsonUtil.writeIntegerFieldIf(ref.minSnapshotsToKeep() != null, MIN_SNAPSHOTS_TO_KEEP, ref.minSnapshotsToKeep(), |
| 67 | + generator); |
| 68 | + JsonUtil.writeLongFieldIf(ref.maxSnapshotAgeMs() != null, MAX_SNAPSHOT_AGE_MS, ref.maxSnapshotAgeMs(), generator); |
| 69 | + JsonUtil.writeLongFieldIf(ref.maxRefAgeMs() != null, MAX_REF_AGE_MS, ref.maxRefAgeMs(), generator); |
| 70 | + generator.writeEndObject(); |
| 71 | + } |
| 72 | + |
| 73 | + public static SnapshotRef fromJson(String json) { |
| 74 | + Preconditions.checkArgument(json != null && !json.isEmpty(), "Cannot parse snapshot ref from invalid JSON: %s", |
| 75 | + json); |
| 76 | + try { |
| 77 | + return fromJson(JsonUtil.mapper().readValue(json, JsonNode.class)); |
| 78 | + } catch (IOException e) { |
| 79 | + throw new UncheckedIOException("Failed to parse snapshot ref: " + json, e); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public static SnapshotRef fromJson(JsonNode node) { |
| 84 | + Preconditions.checkArgument(node.isObject(), "Cannot parse snapshot reference from a non-object: %s", node); |
| 85 | + long snapshotId = JsonUtil.getLong(SNAPSHOT_ID, node); |
| 86 | + SnapshotRefType type = SnapshotRefType.valueOf(JsonUtil.getString(TYPE, node).toUpperCase(Locale.ENGLISH)); |
| 87 | + Integer minSnapshotsToKeep = JsonUtil.getIntOrNull(MIN_SNAPSHOTS_TO_KEEP, node); |
| 88 | + Long maxSnapshotAgeMs = JsonUtil.getLongOrNull(MAX_SNAPSHOT_AGE_MS, node); |
| 89 | + Long maxRefAgeMs = JsonUtil.getLongOrNull(MAX_REF_AGE_MS, node); |
| 90 | + return SnapshotRef.builderFor(snapshotId, type) |
| 91 | + .minSnapshotsToKeep(minSnapshotsToKeep) |
| 92 | + .maxSnapshotAgeMs(maxSnapshotAgeMs) |
| 93 | + .maxRefAgeMs(maxRefAgeMs) |
| 94 | + .build(); |
| 95 | + } |
| 96 | +} |
0 commit comments