World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#283] feat(server): support purge table for Restful server (#482)
mchadese732a37f26e7
diff --git a/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperationDispatcher.java b/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperationDispatcher.javaindex dcd80d790f..5db11be0e0 100644--- a/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperationDispatcher.java+++ b/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperationDispatcher.java@@ -538,6 +538,28 @@ public class CatalogOperationDispatcher implements TableCatalog, SupportsSchemasreturn true;}+ @Override+ public boolean purgeTable(NameIdentifier ident) throws UnsupportedOperationException {+ boolean purged =+ doWithCatalog(+ getCatalogIdentifier(ident),+ c -> c.doWithTableOps(t -> t.purgeTable(ident)),+ NoSuchTableException.class,+ UnsupportedOperationException.class);++ if (!purged) {+ return false;+ }++ try {+ store.delete(ident, TABLE);+ } catch (Exception e) {+ throw new RuntimeException(e);+ }++ return true;+ }+private <R, E extends Throwable> R doWithCatalog(NameIdentifier ident, ThrowableFunction<CatalogManager.CatalogWrapper, R> fn, Class<E> ex)throws E {diff --git a/server/src/main/java/com/datastrato/graviton/server/web/rest/TableOperations.java b/server/src/main/java/com/datastrato/graviton/server/web/rest/TableOperations.javaindex 0d852f7715..f9445cc7ed 100644--- a/server/src/main/java/com/datastrato/graviton/server/web/rest/TableOperations.java+++ b/server/src/main/java/com/datastrato/graviton/server/web/rest/TableOperations.java@@ -24,12 +24,14 @@ import com.datastrato.graviton.server.web.Utils;import javax.inject.Inject;import javax.servlet.http.HttpServletRequest;import javax.ws.rs.DELETE;+import javax.ws.rs.DefaultValue;import javax.ws.rs.GET;import javax.ws.rs.POST;import javax.ws.rs.PUT;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.Produces;+import javax.ws.rs.QueryParam;import javax.ws.rs.core.Context;import javax.ws.rs.core.Response;import org.slf4j.Logger;@@ -146,10 +148,11 @@ public class TableOperations {@PathParam("metalake") String metalake,@PathParam("catalog") String catalog,@PathParam("schema") String schema,- @PathParam("table") String table) {+ @PathParam("table") String table,+ @QueryParam("purge") @DefaultValue("false") boolean purge) {try {NameIdentifier ident = NameIdentifier.ofTable(metalake, catalog, schema, table);- boolean dropped = dispatcher.dropTable(ident);+ boolean dropped = purge ? dispatcher.purgeTable(ident) : dispatcher.dropTable(ident);if (!dropped) {LOG.warn("Failed to drop table {} under schema {}", table, schema);}diff --git a/server/src/test/java/com/datastrato/graviton/server/web/rest/TestTableOperations.java b/server/src/test/java/com/datastrato/graviton/server/web/rest/TestTableOperations.javaindex 7d6b4842ba..ef5db8117c 100644--- a/server/src/test/java/com/datastrato/graviton/server/web/rest/TestTableOperations.java+++ b/server/src/test/java/com/datastrato/graviton/server/web/rest/TestTableOperations.java@@ -666,6 +666,57 @@ public class TestTableOperations extends JerseyTest {Assertions.assertEquals(RuntimeException.class.getSimpleName(), errorResp2.getType());}+ @Test+ public void testPurgeTable() {+ when(dispatcher.purgeTable(any())).thenReturn(true);++ Response resp =+ target(tablePath(metalake, catalog, schema) + "table1")+ .queryParam("purge", true)+ .request(MediaType.APPLICATION_JSON_TYPE)+ .accept("application/vnd.graviton.v1+json")+ .delete();++ Assertions.assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());++ DropResponse dropResponse = resp.readEntity(DropResponse.class);+ Assertions.assertEquals(0, dropResponse.getCode());+ Assertions.assertTrue(dropResponse.dropped());++ // Test when failed to drop table+ when(dispatcher.purgeTable(any())).thenReturn(false);++ Response resp1 =+ target(tablePath(metalake, catalog, schema) + "table1")+ .queryParam("purge", true)+ .request(MediaType.APPLICATION_JSON_TYPE)+ .accept("application/vnd.graviton.v1+json")+ .delete();++ Assertions.assertEquals(Response.Status.OK.getStatusCode(), resp1.getStatus());++ DropResponse dropResponse1 = resp1.readEntity(DropResponse.class);+ Assertions.assertEquals(0, dropResponse1.getCode());+ Assertions.assertFalse(dropResponse1.dropped());++ // Test throw RuntimeException+ doThrow(new RuntimeException("mock error")).when(dispatcher).purgeTable(any());++ Response resp2 =+ target(tablePath(metalake, catalog, schema) + "table1")+ .queryParam("purge", true)+ .request(MediaType.APPLICATION_JSON_TYPE)+ .accept("application/vnd.graviton.v1+json")+ .delete();++ Assertions.assertEquals(+ Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resp2.getStatus());++ ErrorResponse errorResp2 = resp2.readEntity(ErrorResponse.class);+ Assertions.assertEquals(ErrorConstants.INTERNAL_ERROR_CODE, errorResp2.getCode());+ Assertions.assertEquals(RuntimeException.class.getSimpleName(), errorResp2.getType());+ }+private void testAlterTableRequest(TableUpdateRequest req, Table updatedTable) {TableUpdatesRequest updatesRequest = new TableUpdatesRequest(ImmutableList.of(req));