|
| 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.rest.catalog.internal.cmd; |
| 11 | + |
| 12 | +import com.altinity.ice.internal.strings.Strings; |
| 13 | +import com.altinity.ice.rest.catalog.internal.etcd.EtcdCatalog; |
| 14 | +import com.altinity.ice.rest.catalog.internal.rest.RESTObjectMapper; |
| 15 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 16 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 17 | +import java.time.Instant; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import org.apache.iceberg.catalog.Catalog; |
| 22 | +import org.apache.iceberg.exceptions.RuntimeIOException; |
| 23 | + |
| 24 | +/** Catalog registry export/import for etcd-backed catalogs. */ |
| 25 | +public final class CatalogAdminService { |
| 26 | + |
| 27 | + private CatalogAdminService() {} |
| 28 | + |
| 29 | + public static CatalogSnapshot export( |
| 30 | + Catalog catalog, String catalogName, String namespaceFilter) { |
| 31 | + EtcdCatalog etcdCatalog = requireEtcdCatalog(catalog); |
| 32 | + List<CatalogSnapshot.NamespaceEntry> namespaces = new ArrayList<>(); |
| 33 | + for (EtcdCatalog.CatalogKv kv : etcdCatalog.listAllNamespaceKvs()) { |
| 34 | + if (!matchesNamespaceFilter(kv.key(), namespaceFilter, etcdCatalog)) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + namespaces.add(new CatalogSnapshot.NamespaceEntry(kv.key(), parseJsonMap(kv.value()))); |
| 38 | + } |
| 39 | + |
| 40 | + List<CatalogSnapshot.TableEntry> tables = new ArrayList<>(); |
| 41 | + for (EtcdCatalog.CatalogKv kv : etcdCatalog.listAllTableKvs(namespaceFilter)) { |
| 42 | + tables.add(new CatalogSnapshot.TableEntry(kv.key(), parseJsonMap(kv.value()))); |
| 43 | + } |
| 44 | + |
| 45 | + return new CatalogSnapshot( |
| 46 | + CatalogSnapshot.CURRENT_VERSION, catalogName, Instant.now().toString(), namespaces, tables); |
| 47 | + } |
| 48 | + |
| 49 | + public static CatalogImportResult importSnapshot( |
| 50 | + Catalog catalog, CatalogSnapshot snapshot, boolean dryRun, boolean overwrite) { |
| 51 | + EtcdCatalog etcdCatalog = requireEtcdCatalog(catalog); |
| 52 | + |
| 53 | + if (snapshot.version() != CatalogSnapshot.CURRENT_VERSION) { |
| 54 | + throw new IllegalArgumentException( |
| 55 | + "Unsupported snapshot version: " |
| 56 | + + snapshot.version() |
| 57 | + + " (expected " |
| 58 | + + CatalogSnapshot.CURRENT_VERSION |
| 59 | + + ")"); |
| 60 | + } |
| 61 | + |
| 62 | + int created = 0; |
| 63 | + int skipped = 0; |
| 64 | + int overwritten = 0; |
| 65 | + |
| 66 | + if (snapshot.namespaces() != null) { |
| 67 | + for (CatalogSnapshot.NamespaceEntry entry : snapshot.namespaces()) { |
| 68 | + EtcdCatalog.PutCatalogKvResult result = |
| 69 | + etcdCatalog.putCatalogKv(entry.key(), marshal(entry.value()), overwrite, dryRun); |
| 70 | + switch (result) { |
| 71 | + case CREATED -> created++; |
| 72 | + case SKIPPED -> skipped++; |
| 73 | + case OVERWRITTEN -> overwritten++; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + if (snapshot.tables() != null) { |
| 78 | + for (CatalogSnapshot.TableEntry entry : snapshot.tables()) { |
| 79 | + EtcdCatalog.PutCatalogKvResult result = |
| 80 | + etcdCatalog.putCatalogKv(entry.key(), marshal(entry.value()), overwrite, dryRun); |
| 81 | + switch (result) { |
| 82 | + case CREATED -> created++; |
| 83 | + case SKIPPED -> skipped++; |
| 84 | + case OVERWRITTEN -> overwritten++; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return new CatalogImportResult( |
| 90 | + created, skipped, overwritten, snapshot.catalogName(), snapshot.exportedAt()); |
| 91 | + } |
| 92 | + |
| 93 | + private static EtcdCatalog requireEtcdCatalog(Catalog catalog) { |
| 94 | + if (!(catalog instanceof EtcdCatalog etcdCatalog)) { |
| 95 | + throw new IllegalArgumentException( |
| 96 | + "Catalog export/import requires an etcd-backed catalog (uri: etcd:... in config)"); |
| 97 | + } |
| 98 | + return etcdCatalog; |
| 99 | + } |
| 100 | + |
| 101 | + private static boolean matchesNamespaceFilter( |
| 102 | + String namespaceKey, String namespaceFilter, EtcdCatalog catalog) { |
| 103 | + if (Strings.isNullOrEmpty(namespaceFilter)) { |
| 104 | + return true; |
| 105 | + } |
| 106 | + String prefix = catalogPrefixForFilter(catalog) + "n/"; |
| 107 | + if (!namespaceKey.startsWith(prefix)) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + String path = namespaceKey.substring(prefix.length()); |
| 111 | + return path.equals(namespaceFilter) || path.startsWith(namespaceFilter + "/"); |
| 112 | + } |
| 113 | + |
| 114 | + private static String catalogPrefixForFilter(EtcdCatalog catalog) { |
| 115 | + if ("default".equals(catalog.name())) { |
| 116 | + return ""; |
| 117 | + } |
| 118 | + return catalog.name() + "/"; |
| 119 | + } |
| 120 | + |
| 121 | + private static Map<String, String> parseJsonMap(String json) { |
| 122 | + try { |
| 123 | + return RESTObjectMapper.mapper().readValue(json, new TypeReference<>() {}); |
| 124 | + } catch (JsonProcessingException e) { |
| 125 | + throw new RuntimeIOException(e); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static String marshal(Map<String, String> value) { |
| 130 | + try { |
| 131 | + return RESTObjectMapper.mapper().writeValueAsString(value); |
| 132 | + } catch (JsonProcessingException e) { |
| 133 | + throw new RuntimeIOException(e); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments