This is an automated email from the ASF dual-hosted git repository. nicholasjiang pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/paimon-webui.git
commit d2a9c8138e228bbfb90a4cf4fdb9473b14b9051c Author: s7monk <[email protected]> AuthorDate: Sun May 5 20:15:41 2024 +0800 [Improvement] Optimize return value type for table service interface (#208) --- .../web/server/controller/TableController.java | 112 +++++++++++---------- .../paimon/web/server/service/TableService.java | 104 +++++++++---------- .../web/server/service/impl/TableServiceImpl.java | 60 +++++------ 3 files changed, 138 insertions(+), 138 deletions(-) diff --git a/paimon-web-server/src/main/java/org/apache/paimon/web/server/controller/TableController.java b/paimon-web-server/src/main/java/org/apache/paimon/web/server/controller/TableController.java index ac45747..1f5af6b 100644 --- a/paimon-web-server/src/main/java/org/apache/paimon/web/server/controller/TableController.java +++ b/paimon-web-server/src/main/java/org/apache/paimon/web/server/controller/TableController.java @@ -56,33 +56,39 @@ public class TableController { /** * Creates a table in the database based on the provided TableInfo. * - * @param tableDTO The TableDTO object containing information about the table. - * @return R<Void/> indicating the success or failure of the operation. + * @param tableDTO The TableDTO object containing information about the table + * @return a {@code R<Void>} response indicating success or failure */ @PostMapping("/create") public R<Void> createTable(@RequestBody TableDTO tableDTO) { - return tableService.createTable(tableDTO); + if (tableService.tableExists(tableDTO)) { + return R.failed(Status.TABLE_NAME_IS_EXIST, tableDTO.getName()); + } + return tableService.createTable(tableDTO) + ? R.succeed() + : R.failed(Status.TABLE_CREATE_ERROR); } /** * Adds a column to the table. * - * @param tableDTO The information of the table, including the catalog name, database name, - * table name, and table columns. - * @return A response indicating the success or failure of the operation. + * @param tableDTO The TableDTO object containing information about the table + * @return a {@code R<Void>} response indicating success or failure */ @PostMapping("/column/add") public R<Void> addColumn(@RequestBody TableDTO tableDTO) { - return tableService.addColumn(tableDTO); + return tableService.addColumn(tableDTO) + ? R.succeed() + : R.failed(Status.TABLE_ADD_COLUMN_ERROR); } /** * Lists columns given a table. * - * @param catalogName The name of the catalog. - * @param databaseName The name of the database. - * @param tableName The name of the table. - * @return Response object containing {@link TableVO} representing the table. + * @param catalogName The name of the catalog + * @param databaseName The name of the database + * @param tableName The name of the table + * @return Response object containing {@link TableVO} representing the table */ @GetMapping("/column/list") public R<TableVO> listColumns( @@ -95,11 +101,11 @@ public class TableController { /** * Drops a column from a table. * - * @param catalogName The name of the catalog. - * @param databaseName The name of the database. - * @param tableName The name of the table. - * @param columnName The name of the column to be dropped. - * @return The result indicating the success or failure of the operation. + * @param catalogName The name of the catalog + * @param databaseName The name of the database + * @param tableName The name of the table + * @param columnName The name of the column to be dropped + * @return a {@code R<Void>} response indicating success or failure */ @DeleteMapping("/column/drop/{catalogName}/{databaseName}/{tableName}/{columnName}") public R<Void> dropColumn( @@ -107,43 +113,45 @@ public class TableController { @PathVariable String databaseName, @PathVariable String tableName, @PathVariable String columnName) { - return tableService.dropColumn(catalogName, databaseName, tableName, columnName); + return tableService.dropColumn(catalogName, databaseName, tableName, columnName) + ? R.succeed() + : R.failed(Status.TABLE_DROP_COLUMN_ERROR); } /** * Modify a column in a table. * - * @param alterTableDTO the DTO containing alteration details. - * @return A response indicating the success or failure of the operation. + * @param alterTableDTO the DTO containing alteration details + * @return a {@code R<Void>} response indicating success or failure */ @PostMapping("/alter") public R<Void> alterTable(@RequestBody AlterTableDTO alterTableDTO) { - return tableService.alterTable(alterTableDTO); + return tableService.alterTable(alterTableDTO) + ? R.succeed() + : R.failed(Status.TABLE_AlTER_COLUMN_ERROR); } /** * Adds options to a table. * - * @param tableDTO An object containing table information. - * @return If the options are successfully added, returns a successful result object. If an - * exception occurs, returns a result object with an error status. + * @param tableDTO The TableDTO object containing information about the table + * @return a {@code R<Void>} response indicating success or failure */ @PostMapping("/option/add") public R<Void> addOption(@RequestBody TableDTO tableDTO) { - return tableService.addOption(tableDTO); + return tableService.addOption(tableDTO) + ? R.succeed() + : R.failed(Status.TABLE_ADD_OPTION_ERROR); } /** * Removes an option from a table. * - * @param catalogName The name of the catalog. - * @param databaseName The name of the database. - * @param tableName The name of the table. - * @param key The key of the option to be removed. - * @return Returns a {@link R} object indicating the success or failure of the operation. If the - * option is successfully removed, the result will be a successful response with no data. If - * an error occurs during the operation, the result will be a failed response with an error - * code. Possible error codes: {@link Status#TABLE_REMOVE_OPTION_ERROR}. + * @param catalogName The name of the catalog + * @param databaseName The name of the database + * @param tableName The name of the table + * @param key The key of the option to be removed + * @return a {@code R<Void>} response indicating success or failure */ @PostMapping("/option/remove") public R<Void> removeOption( @@ -151,41 +159,37 @@ public class TableController { @RequestParam String databaseName, @RequestParam String tableName, @RequestParam String key) { - return tableService.removeOption(catalogName, databaseName, tableName, key); + return tableService.removeOption(catalogName, databaseName, tableName, key) + ? R.succeed() + : R.failed(Status.TABLE_REMOVE_OPTION_ERROR); } /** * Drops a table from the specified database in the given catalog. * - * @param catalogName The name of the catalog from which the table will be dropped. - * @param databaseName The name of the database from which the table will be dropped. - * @param tableName The name of the table to be dropped. - * @return A Response object indicating the success or failure of the operation. If the - * operation is successful, the response will be R.succeed(). If the operation fails, the - * response will be R.failed() with Status.TABLE_DROP_ERROR. - * @throws RuntimeException If there is an error during the operation, a RuntimeException is - * thrown with the error message. + * @param catalogName The name of the catalog from which the table will be dropped + * @param databaseName The name of the database from which the table will be dropped + * @param tableName The name of the table to be dropped + * @return a {@code R<Void>} response indicating success or failure */ @DeleteMapping("/drop/{catalogName}/{databaseName}/{tableName}") public R<Void> dropTable( @PathVariable String catalogName, @PathVariable String databaseName, @PathVariable String tableName) { - return tableService.dropTable(catalogName, databaseName, tableName); + return tableService.dropTable(catalogName, databaseName, tableName) + ? R.succeed() + : R.failed(Status.TABLE_DROP_ERROR); } /** * Renames a table in the specified database of the given catalog. * - * @param catalogName The name of the catalog where the table resides. - * @param databaseName The name of the database where the table resides. - * @param fromTableName The current name of the table to be renamed. - * @param toTableName The new name for the table. - * @return A Response object indicating the success or failure of the operation. If the - * operation is successful, the response will be R.succeed(). If the operation fails, the - * response will be R.failed() with Status.TABLE_RENAME_ERROR. - * @throws RuntimeException If there is an error during the operation, a RuntimeException is - * thrown with the error message. + * @param catalogName The name of the catalog where the table resides + * @param databaseName The name of the database where the table resides + * @param fromTableName The current name of the table to be renamed + * @param toTableName The new name for the table + * @return a {@code R<Void>} response indicating success or failure */ @PostMapping("/rename") public R<Void> renameTable( @@ -193,13 +197,15 @@ public class TableController { @RequestParam String databaseName, @RequestParam String fromTableName, @RequestParam String toTableName) { - return tableService.renameTable(catalogName, databaseName, fromTableName, toTableName); + return tableService.renameTable(catalogName, databaseName, fromTableName, toTableName) + ? R.succeed() + : R.failed(Status.TABLE_RENAME_ERROR); } /** * Lists tables given {@link TableDTO} condition. * - * @return Response object containing a list of {@link TableVO} representing the tables. + * @return Response object containing a list of {@link TableVO} representing the tables */ @PostMapping("/list") public R<Object> listTables(@RequestBody TableDTO tableDTO) { diff --git a/paimon-web-server/src/main/java/org/apache/paimon/web/server/service/TableService.java b/paimon-web-server/src/main/java/org/apache/paimon/web/server/service/TableService.java index b1d59c6..f1dbaa5 100644 --- a/paimon-web-server/src/main/java/org/apache/paimon/web/server/service/TableService.java +++ b/paimon-web-server/src/main/java/org/apache/paimon/web/server/service/TableService.java @@ -20,8 +20,6 @@ package org.apache.paimon.web.server.service; import org.apache.paimon.web.server.data.dto.AlterTableDTO; import org.apache.paimon.web.server.data.dto.TableDTO; -import org.apache.paimon.web.server.data.result.R; -import org.apache.paimon.web.server.data.result.enums.Status; import org.apache.paimon.web.server.data.vo.TableVO; import java.util.List; @@ -29,111 +27,105 @@ import java.util.List; /** Table service. */ public interface TableService { + /** + * Checks if the specified table exists. + * + * @param tableDTO The TableDTO object containing information about the table + * @return true if the table exists, false otherwise + */ + boolean tableExists(TableDTO tableDTO); + /** * Creates a table in the database given ${@link TableDTO}. * - * @param tableDTO The TableDTO object containing information about the table. - * @return R<Void/> indicating the success or failure of the operation. + * @param tableDTO The TableDTO object containing information about the table + * @return true if the operation is successful, false otherwise */ - R<Void> createTable(TableDTO tableDTO); + boolean createTable(TableDTO tableDTO); /** * Adds a column to the table. * - * @param tableDTO The information of the table, including the catalog name, database name, - * table name, and table columns. - * @return A response indicating the success or failure of the operation. + * @param tableDTO The TableDTO object containing information about the table + * @return true if the operation is successful, false otherwise */ - R<Void> addColumn(TableDTO tableDTO); + boolean addColumn(TableDTO tableDTO); /** * Drops a column from a table. * - * @param catalogName The name of the catalog. - * @param databaseName The name of the database. - * @param tableName The name of the table. - * @param columnName The name of the column to be dropped. - * @return The result indicating the success or failure of the operation. + * @param catalogName The name of the catalog + * @param databaseName The name of the database + * @param tableName The name of the table + * @param columnName The name of the column to be dropped + * @return true if the operation is successful, false otherwise */ - R<Void> dropColumn( + boolean dropColumn( String catalogName, String databaseName, String tableName, String columnName); /** * Alters a table. * - * @param alterTableDTO the DTO containing alteration details. - * @return A response indicating the success or failure of the operation. + * @param alterTableDTO the DTO containing alteration details + * @return true if the operation is successful, false otherwise */ - R<Void> alterTable(AlterTableDTO alterTableDTO); + boolean alterTable(AlterTableDTO alterTableDTO); /** * Adds options to a table. * - * @param tableDTO An object containing table information. - * @return If the options are successfully added, returns a successful result object. If an - * exception occurs, returns a result object with an error status. + * @param tableDTO The TableDTO object containing information about the table + * @return true if the operation is successful, false otherwise */ - R<Void> addOption(TableDTO tableDTO); + boolean addOption(TableDTO tableDTO); /** * Removes an option from a table. * - * @param catalogName The name of the catalog. - * @param databaseName The name of the database. - * @param tableName The name of the table. - * @param key The key of the option to be removed. - * @return Returns a {@link R} object indicating the success or failure of the operation. If the - * option is successfully removed, the result will be a successful response with no data. If - * an error occurs during the operation, the result will be a failed response with an error - * code. Possible error codes: {@link Status#TABLE_REMOVE_OPTION_ERROR}. + * @param catalogName The name of the catalog + * @param databaseName The name of the database + * @param tableName The name of the table + * @param key The key of the option to be removed + * @return true if the operation is successful, false otherwise */ - R<Void> removeOption(String catalogName, String databaseName, String tableName, String key); + boolean removeOption(String catalogName, String databaseName, String tableName, String key); /** * Drops a table from the specified database in the given catalog. * - * @param catalogName The name of the catalog from which the table will be dropped. - * @param databaseName The name of the database from which the table will be dropped. - * @param tableName The name of the table to be dropped. - * @return A Response object indicating the success or failure of the operation. If the - * operation is successful, the response will be R.succeed(). If the operation fails, the - * response will be R.failed() with Status.TABLE_DROP_ERROR. - * @throws RuntimeException If there is an error during the operation, a RuntimeException is - * thrown with the error message. + * @param catalogName The name of the catalog from which the table will be dropped + * @param databaseName The name of the database from which the table will be dropped + * @param tableName The name of the table to be dropped + * @return true if the operation is successful, false otherwise */ - R<Void> dropTable(String catalogName, String databaseName, String tableName); + boolean dropTable(String catalogName, String databaseName, String tableName); /** * Renames a table in the specified database of the given catalog. * - * @param catalogName The name of the catalog where the table resides. - * @param databaseName The name of the database where the table resides. - * @param fromTableName The current name of the table to be renamed. - * @param toTableName The new name for the table. - * @return A Response object indicating the success or failure of the operation. If the - * operation is successful, the response will be R.succeed(). If the operation fails, the - * response will be R.failed() with Status.TABLE_RENAME_ERROR. - * @throws RuntimeException If there is an error during the operation, a RuntimeException is - * thrown with the error message. + * @param catalogName The name of the catalog where the table resides + * @param databaseName The name of the database where the table resides + * @param fromTableName The current name of the table to be renamed + * @param toTableName The new name for the table + * @return true if the operation is successful, false otherwise */ - R<Void> renameTable( + boolean renameTable( String catalogName, String databaseName, String fromTableName, String toTableName); /** * Lists tables given {@link TableDTO} condition. * - * @return Response object containing a list of {@link TableVO} representing the tables. + * @return Response object containing a list of {@link TableVO} representing the tables */ List<TableVO> listTables(TableDTO tableDTO); /** * Retrieves the column details of a specific table within the specified catalog and database. * - * @param catalogName The name of the catalog where the table is located. - * @param databaseName The name of the database where the table is located. - * @param tableName The name of the table whose columns are to be retrieved. - * @return A {@link TableVO} object containing the details of the columns of the specified - * table. + * @param catalogName The name of the catalog where the table is located + * @param databaseName The name of the database where the table is located + * @param tableName The name of the table whose columns are to be retrieved + * @return A {@link TableVO} object containing the details of the columns of the specified table */ TableVO listColumns(String catalogName, String databaseName, String tableName); } diff --git a/paimon-web-server/src/main/java/org/apache/paimon/web/server/service/impl/TableServiceImpl.java b/paimon-web-server/src/main/java/org/apache/paimon/web/server/service/impl/TableServiceImpl.java index 87a0f73..71ecc7a 100644 --- a/paimon-web-server/src/main/java/org/apache/paimon/web/server/service/impl/TableServiceImpl.java +++ b/paimon-web-server/src/main/java/org/apache/paimon/web/server/service/impl/TableServiceImpl.java @@ -28,8 +28,6 @@ import org.apache.paimon.web.server.data.dto.AlterTableDTO; import org.apache.paimon.web.server.data.dto.TableDTO; import org.apache.paimon.web.server.data.model.CatalogInfo; import org.apache.paimon.web.server.data.model.TableColumn; -import org.apache.paimon.web.server.data.result.R; -import org.apache.paimon.web.server.data.result.enums.Status; import org.apache.paimon.web.server.data.vo.TableVO; import org.apache.paimon.web.server.service.CatalogService; import org.apache.paimon.web.server.service.TableService; @@ -68,7 +66,14 @@ public class TableServiceImpl implements TableService { } @Override - public R<Void> createTable(TableDTO tableDTO) { + public boolean tableExists(TableDTO tableDTO) { + PaimonService service = + PaimonServiceUtils.getPaimonService(getCatalogInfo(tableDTO.getCatalogName())); + return service.tableExists(tableDTO.getDatabaseName(), tableDTO.getName()); + } + + @Override + public boolean createTable(TableDTO tableDTO) { try { PaimonService service = PaimonServiceUtils.getPaimonService(getCatalogInfo(tableDTO.getCatalogName())); @@ -99,19 +104,16 @@ public class TableServiceImpl implements TableService { .options(tableOptions) .comment(tableDTO.getDescription()) .build(); - if (service.tableExists(tableDTO.getDatabaseName(), tableDTO.getName())) { - return R.failed(Status.TABLE_NAME_IS_EXIST, tableDTO.getName()); - } service.createTable(tableDTO.getDatabaseName(), tableDTO.getName(), tableMetadata); - return R.succeed(); + return true; } catch (Exception e) { log.error("Exception with creating table.", e); - return R.failed(Status.TABLE_CREATE_ERROR); + return false; } } @Override - public R<Void> addColumn(TableDTO tableDTO) { + public boolean addColumn(TableDTO tableDTO) { try { PaimonService service = PaimonServiceUtils.getPaimonService(getCatalogInfo(tableDTO.getCatalogName())); @@ -151,15 +153,15 @@ public class TableServiceImpl implements TableService { } } service.alterTable(tableDTO.getDatabaseName(), tableDTO.getName(), tableChanges); - return R.succeed(); + return true; } catch (Exception e) { log.error("Exception with adding column.", e); - return R.failed(Status.TABLE_ADD_COLUMN_ERROR); + return false; } } @Override - public R<Void> dropColumn( + public boolean dropColumn( String catalogName, String databaseName, String tableName, String columnName) { try { PaimonService service = @@ -168,15 +170,15 @@ public class TableServiceImpl implements TableService { TableChange.DropColumn dropColumn = TableChange.dropColumn(columnName); tableChanges.add(dropColumn); service.alterTable(databaseName, tableName, tableChanges); - return R.succeed(); + return true; } catch (Exception e) { log.error("Exception with dropping column.", e); - return R.failed(Status.TABLE_DROP_COLUMN_ERROR); + return false; } } @Override - public R<Void> alterTable(AlterTableDTO alterTableDTO) { + public boolean alterTable(AlterTableDTO alterTableDTO) { try { String databaseName = alterTableDTO.getDatabaseName(); String tableName = alterTableDTO.getTableName(); @@ -215,15 +217,15 @@ public class TableServiceImpl implements TableService { if (!tableChanges.isEmpty()) { service.alterTable(databaseName, tableName, tableChanges); } - return R.succeed(); + return true; } catch (Exception e) { log.error("Exception with altering table.", e); - return R.failed(Status.TABLE_AlTER_COLUMN_ERROR); + return false; } } @Override - public R<Void> addOption(TableDTO tableDTO) { + public boolean addOption(TableDTO tableDTO) { List<TableChange> tableChanges = new ArrayList<>(); try { PaimonService service = @@ -234,15 +236,15 @@ public class TableServiceImpl implements TableService { tableChanges.add(setOption); } service.alterTable(tableDTO.getDatabaseName(), tableDTO.getName(), tableChanges); - return R.succeed(); + return true; } catch (Exception e) { log.error("Exception with adding option.", e); - return R.failed(Status.TABLE_ADD_OPTION_ERROR); + return false; } } @Override - public R<Void> removeOption( + public boolean removeOption( String catalogName, String databaseName, String tableName, String key) { List<TableChange> tableChanges = new ArrayList<>(); try { @@ -251,37 +253,37 @@ public class TableServiceImpl implements TableService { TableChange.RemoveOption removeOption = TableChange.remove(key); tableChanges.add(removeOption); service.alterTable(databaseName, tableName, tableChanges); - return R.succeed(); + return true; } catch (Exception e) { log.error("Exception with removing option.", e); - return R.failed(Status.TABLE_REMOVE_OPTION_ERROR); + return false; } } @Override - public R<Void> dropTable(String catalogName, String databaseName, String tableName) { + public boolean dropTable(String catalogName, String databaseName, String tableName) { try { PaimonService service = PaimonServiceUtils.getPaimonService(getCatalogInfo(catalogName)); service.dropTable(databaseName, tableName); - return R.succeed(); + return true; } catch (Exception e) { log.error("Exception with dropping table.", e); - return R.failed(Status.TABLE_DROP_ERROR); + return false; } } @Override - public R<Void> renameTable( + public boolean renameTable( String catalogName, String databaseName, String fromTableName, String toTableName) { try { PaimonService service = PaimonServiceUtils.getPaimonService(getCatalogInfo(catalogName)); service.renameTable(databaseName, fromTableName, toTableName); - return R.succeed(); + return true; } catch (Exception e) { log.error("Exception with renaming table.", e); - return R.failed(Status.TABLE_RENAME_ERROR); + return false; } }
