|
27 | 27 | import jakarta.ws.rs.core.MultivaluedHashMap; |
28 | 28 | import jakarta.ws.rs.core.Response; |
29 | 29 | import java.net.URI; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.HashMap; |
30 | 32 | import java.util.List; |
31 | 33 | import java.util.Map; |
32 | 34 | import org.apache.iceberg.catalog.Namespace; |
| 35 | +import org.apache.iceberg.catalog.TableIdentifier; |
33 | 36 | import org.apache.iceberg.rest.RESTUtil; |
34 | 37 | import org.apache.iceberg.rest.requests.CreateNamespaceRequest; |
35 | 38 | import org.apache.iceberg.rest.responses.ListNamespacesResponse; |
| 39 | +import org.apache.iceberg.rest.responses.ListTablesResponse; |
36 | 40 | import org.apache.iceberg.rest.responses.OAuthTokenResponse; |
37 | 41 |
|
38 | 42 | public class CatalogApi extends RestApi { |
@@ -72,21 +76,88 @@ public void createNamespace(String catalogName, String namespaceName) { |
72 | 76 | } |
73 | 77 | } |
74 | 78 |
|
75 | | - public List<Namespace> listNamespaces(String catalog) { |
76 | | - try (Response response = request("v1/{cat}/namespaces", Map.of("cat", catalog)).get()) { |
| 79 | + public List<Namespace> listNamespaces(String catalog, Namespace parent) { |
| 80 | + Map<String, String> queryParams = new HashMap<>(); |
| 81 | + if (!parent.isEmpty()) { |
| 82 | + queryParams.put("parent", RESTUtil.encodeNamespace(parent)); |
| 83 | + } |
| 84 | + try (Response response = |
| 85 | + request("v1/{cat}/namespaces", Map.of("cat", catalog), queryParams).get()) { |
77 | 86 | assertThat(response.getStatus()).isEqualTo(OK.getStatusCode()); |
78 | 87 | ListNamespacesResponse res = response.readEntity(ListNamespacesResponse.class); |
79 | 88 | return res.namespaces(); |
80 | 89 | } |
81 | 90 | } |
82 | 91 |
|
83 | | - public void deleteNamespaces(String catalog, Namespace namespace) { |
| 92 | + public List<Namespace> listAllNamespacesChildFirst(String catalog) { |
| 93 | + List<Namespace> result = new ArrayList<>(); |
| 94 | + for (int idx = -1; idx < result.size(); idx++) { |
| 95 | + Namespace parent = Namespace.empty(); |
| 96 | + if (idx >= 0) { |
| 97 | + parent = result.get(idx); |
| 98 | + } |
| 99 | + |
| 100 | + result.addAll(listNamespaces(catalog, parent)); |
| 101 | + } |
| 102 | + |
| 103 | + return result.reversed(); |
| 104 | + } |
| 105 | + |
| 106 | + public void deleteNamespace(String catalog, Namespace namespace) { |
| 107 | + String ns = RESTUtil.encodeNamespace(namespace); |
84 | 108 | try (Response response = |
| 109 | + request("v1/{cat}/namespaces/" + ns, Map.of("cat", catalog)).delete()) { |
| 110 | + assertThat(response.getStatus()).isEqualTo(NO_CONTENT.getStatusCode()); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public void purge(String catalog) { |
| 115 | + listAllNamespacesChildFirst(catalog).forEach(ns -> purge(catalog, ns)); |
| 116 | + } |
| 117 | + |
| 118 | + public void purge(String catalog, Namespace ns) { |
| 119 | + listTables(catalog, ns).forEach(t -> dropTable(catalog, t)); |
| 120 | + listViews(catalog, ns).forEach(t -> dropView(catalog, t)); |
| 121 | + deleteNamespace(catalog, ns); |
| 122 | + } |
| 123 | + |
| 124 | + public List<TableIdentifier> listTables(String catalog, Namespace namespace) { |
| 125 | + String ns = RESTUtil.encodeNamespace(namespace); |
| 126 | + try (Response res = |
| 127 | + request("v1/{cat}/namespaces/" + ns + "/tables", Map.of("cat", catalog)).get()) { |
| 128 | + assertThat(res.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); |
| 129 | + return res.readEntity(ListTablesResponse.class).identifiers(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public void dropTable(String catalog, TableIdentifier id) { |
| 134 | + String ns = RESTUtil.encodeNamespace(id.namespace()); |
| 135 | + try (Response res = |
85 | 136 | request( |
86 | | - "v1/{cat}/namespaces/{ns}", |
87 | | - Map.of("cat", catalog, "ns", RESTUtil.encodeNamespace(namespace))) |
| 137 | + "v1/{cat}/namespaces/" + ns + "/tables/{table}", |
| 138 | + Map.of("cat", catalog, "table", id.name())) |
88 | 139 | .delete()) { |
89 | | - assertThat(response.getStatus()).isEqualTo(NO_CONTENT.getStatusCode()); |
| 140 | + assertThat(res.getStatus()).isEqualTo(NO_CONTENT.getStatusCode()); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public List<TableIdentifier> listViews(String catalog, Namespace namespace) { |
| 145 | + String ns = RESTUtil.encodeNamespace(namespace); |
| 146 | + try (Response res = |
| 147 | + request("v1/{cat}/namespaces/" + ns + "/views", Map.of("cat", catalog)).get()) { |
| 148 | + assertThat(res.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); |
| 149 | + return res.readEntity(ListTablesResponse.class).identifiers(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + public void dropView(String catalog, TableIdentifier id) { |
| 154 | + String ns = RESTUtil.encodeNamespace(id.namespace()); |
| 155 | + try (Response res = |
| 156 | + request( |
| 157 | + "v1/{cat}/namespaces/" + ns + "/views/{view}", |
| 158 | + Map.of("cat", catalog, "view", id.name())) |
| 159 | + .delete()) { |
| 160 | + assertThat(res.getStatus()).isEqualTo(NO_CONTENT.getStatusCode()); |
90 | 161 | } |
91 | 162 | } |
92 | 163 | } |
0 commit comments