|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Altinity Inc and/or its affiliates. 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 | +package com.altinity.ice.cli.internal.cmd; |
| 11 | + |
| 12 | +import com.altinity.ice.cli.internal.util.TreePrinter; |
| 13 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 14 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 15 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 16 | +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Comparator; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Set; |
| 25 | +import org.apache.iceberg.Table; |
| 26 | +import org.apache.iceberg.catalog.TableIdentifier; |
| 27 | +import org.apache.iceberg.rest.RESTCatalog; |
| 28 | + |
| 29 | +public final class ListSnapshots { |
| 30 | + |
| 31 | + private ListSnapshots() {} |
| 32 | + |
| 33 | + public static void run(RESTCatalog catalog, TableIdentifier tableId, boolean json, int limit) |
| 34 | + throws IOException { |
| 35 | + Table table = catalog.loadTable(tableId); |
| 36 | + Long currentSnapshotId = |
| 37 | + table.currentSnapshot() != null ? table.currentSnapshot().snapshotId() : null; |
| 38 | + |
| 39 | + List<DescribeMetadata.SnapshotInfo> rows = |
| 40 | + DescribeMetadata.extractSnapshots(table.snapshots(), currentSnapshotId); |
| 41 | + |
| 42 | + rows.sort(Comparator.comparingLong(DescribeMetadata.SnapshotInfo::timestampMillis)); |
| 43 | + |
| 44 | + if (limit > 0 && rows.size() > limit) { |
| 45 | + rows = new ArrayList<>(rows.subList(rows.size() - limit, rows.size())); |
| 46 | + } |
| 47 | + |
| 48 | + if (json) { |
| 49 | + var result = new Result(tableId.toString(), currentSnapshotId, rows); |
| 50 | + ObjectMapper mapper = new ObjectMapper(); |
| 51 | + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
| 52 | + System.out.println(mapper.writeValueAsString(result)); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + printTree(tableId.toString(), currentSnapshotId, rows); |
| 57 | + } |
| 58 | + |
| 59 | + private static void printTree( |
| 60 | + String tableName, Long currentSnapshotId, List<DescribeMetadata.SnapshotInfo> rows) |
| 61 | + throws IOException { |
| 62 | + StringBuilder rootLabel = new StringBuilder(); |
| 63 | + rootLabel.append("table: ").append(tableName); |
| 64 | + if (currentSnapshotId != null) { |
| 65 | + rootLabel.append("\ncurrentSnapshotId: ").append(currentSnapshotId); |
| 66 | + } |
| 67 | + |
| 68 | + if (rows.isEmpty()) { |
| 69 | + TreePrinter.print(new TreePrinter.Node(rootLabel.toString(), List.of())); |
| 70 | + System.out.println("(no snapshots)"); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + ObjectMapper yamlMapper = |
| 75 | + new ObjectMapper( |
| 76 | + new YAMLFactory() |
| 77 | + .enable(YAMLGenerator.Feature.MINIMIZE_QUOTES) |
| 78 | + .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)); |
| 79 | + yamlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
| 80 | + |
| 81 | + Set<Long> presentIds = new HashSet<>(rows.size()); |
| 82 | + for (DescribeMetadata.SnapshotInfo info : rows) { |
| 83 | + presentIds.add(info.snapshotId()); |
| 84 | + } |
| 85 | + |
| 86 | + Map<Long, List<DescribeMetadata.SnapshotInfo>> childrenByParent = new HashMap<>(); |
| 87 | + List<DescribeMetadata.SnapshotInfo> roots = new ArrayList<>(); |
| 88 | + for (DescribeMetadata.SnapshotInfo info : rows) { |
| 89 | + Long parentId = info.parentId(); |
| 90 | + if (parentId == null || !presentIds.contains(parentId)) { |
| 91 | + roots.add(info); |
| 92 | + } else { |
| 93 | + childrenByParent.computeIfAbsent(parentId, k -> new ArrayList<>()).add(info); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + List<TreePrinter.Node> rootChildren = new ArrayList<>(roots.size()); |
| 98 | + for (DescribeMetadata.SnapshotInfo root : roots) { |
| 99 | + rootChildren.add(buildNode(root, childrenByParent, yamlMapper)); |
| 100 | + } |
| 101 | + |
| 102 | + TreePrinter.print(new TreePrinter.Node(rootLabel.toString(), rootChildren)); |
| 103 | + } |
| 104 | + |
| 105 | + private static TreePrinter.Node buildNode( |
| 106 | + DescribeMetadata.SnapshotInfo info, |
| 107 | + Map<Long, List<DescribeMetadata.SnapshotInfo>> childrenByParent, |
| 108 | + ObjectMapper yamlMapper) |
| 109 | + throws IOException { |
| 110 | + List<DescribeMetadata.SnapshotInfo> children = |
| 111 | + childrenByParent.getOrDefault(info.snapshotId(), List.of()); |
| 112 | + List<TreePrinter.Node> childNodes = new ArrayList<>(children.size()); |
| 113 | + |
| 114 | + for (DescribeMetadata.SnapshotInfo child : children) { |
| 115 | + childNodes.add(buildNode(child, childrenByParent, yamlMapper)); |
| 116 | + } |
| 117 | + return new TreePrinter.Node(yamlMapper.writeValueAsString(info), childNodes); |
| 118 | + } |
| 119 | + |
| 120 | + @JsonInclude(JsonInclude.Include.NON_NULL) |
| 121 | + record Result( |
| 122 | + String table, Long currentSnapshotId, List<DescribeMetadata.SnapshotInfo> snapshots) {} |
| 123 | +} |
0 commit comments