Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improvement] Refactor server module #48

Merged
merged 6 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions paimon-web-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ under the License.
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@

package org.apache.paimon.web.server.controller;

import org.apache.paimon.web.api.catalog.CatalogCreator;
import org.apache.paimon.web.api.catalog.PaimonServiceFactory;
import org.apache.paimon.web.server.data.enums.CatalogMode;
import org.apache.paimon.web.server.data.model.CatalogInfo;
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.service.CatalogService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -45,46 +48,39 @@ public class CatalogController {
@Autowired private CatalogService catalogService;

/**
* Create a filesystem catalog.
* Create a catalog.
*
* @param catalogInfo The catalogInfo for the filesystem catalog.
* @param catalogInfo The catalogInfo for the catalog.
* @return The created catalog.
*/
@PostMapping("/createFilesystemCatalog")
public R<Void> createFilesystemCatalog(@RequestBody CatalogInfo catalogInfo) {
@PostMapping("/create")
public R<Void> createCatalog(@RequestBody CatalogInfo catalogInfo) {
if (!catalogService.checkCatalogNameUnique(catalogInfo)) {
return R.failed(Status.CATALOG_NAME_IS_EXIST, catalogInfo.getCatalogName());
}

try {
CatalogCreator.createFilesystemCatalog(catalogInfo.getWarehouse());
if (catalogInfo.getCatalogType().equalsIgnoreCase(CatalogMode.FILESYSTEM.getMode())) {
PaimonServiceFactory.createFileSystemCatalogService(
catalogInfo.getCatalogName(), catalogInfo.getWarehouse());
} else if (catalogInfo.getCatalogType().equalsIgnoreCase(CatalogMode.HIVE.getMode())) {
if (StringUtils.isNotBlank(catalogInfo.getHiveConfDir())) {
PaimonServiceFactory.createHiveCatalogService(
catalogInfo.getCatalogName(),
catalogInfo.getWarehouse(),
catalogInfo.getHiveUri(),
catalogInfo.getHiveConfDir());
} else {
PaimonServiceFactory.createHiveCatalogService(
catalogInfo.getCatalogName(),
catalogInfo.getWarehouse(),
catalogInfo.getHiveUri(),
null);
}
}
return catalogService.save(catalogInfo) ? R.succeed() : R.failed();
} catch (Exception e) {
e.printStackTrace();
return R.failed(Status.CATALOG_CREATE_ERROR);
}
}

/**
* Create a hive catalog.
*
* @param catalogInfo The information for the hive catalog.
* @return The created catalog.
*/
@PostMapping("/createHiveCatalog")
public R<Void> createHiveCatalog(@RequestBody CatalogInfo catalogInfo) {
if (!catalogService.checkCatalogNameUnique(catalogInfo)) {
return R.failed(Status.CATALOG_NAME_IS_EXIST, catalogInfo.getCatalogName());
}

try {
CatalogCreator.createHiveCatalog(
catalogInfo.getWarehouse(),
catalogInfo.getHiveUri(),
catalogInfo.getHiveConfDir());
return catalogService.save(catalogInfo) ? R.succeed() : R.failed();
} catch (Exception e) {
e.printStackTrace();
log.error("Exception with creating catalog.", e);
return R.failed(Status.CATALOG_CREATE_ERROR);
}
}
Expand All @@ -101,13 +97,17 @@ public R<List<CatalogInfo>> getCatalog() {
}

/**
* Removes a catalog by its ID.
* Removes a catalog with given catalog name.
*
* @param catalogId The ID of the catalog to be removed.
* @return A response indicating the success or failure of the removal operation.
* @param catalogName The catalog name.
* @return A response indicating the success or failure of the operation.
*/
@DeleteMapping("/{catalogId}")
public R<Void> remove(@PathVariable Integer catalogId) {
return catalogService.removeById(catalogId) ? R.succeed() : R.failed();
@DeleteMapping("/remove/{catalogName}")
public R<Void> removeCatalog(@PathVariable String catalogName) {
QueryWrapper<CatalogInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("catalog_name", catalogName);
return catalogService.remove(queryWrapper)
? R.succeed()
: R.failed(Status.CATALOG_REMOVE_ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@

package org.apache.paimon.web.server.controller;

import org.apache.paimon.catalog.Catalog;
import org.apache.paimon.web.api.database.DatabaseManager;
import org.apache.paimon.web.api.catalog.PaimonService;
import org.apache.paimon.web.server.data.model.CatalogInfo;
import org.apache.paimon.web.server.data.model.DatabaseInfo;
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.service.CatalogService;
import org.apache.paimon.web.server.util.CatalogUtils;
import org.apache.paimon.web.server.util.PaimonServiceUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -54,18 +55,18 @@ public class DatabaseController {
* @param databaseInfo The DatabaseInfo object containing the details of the new database.
* @return R<Void/> indicating the result of the operation.
*/
@PostMapping("/createDatabase")
@PostMapping("/create")
public R<Void> createDatabase(@RequestBody DatabaseInfo databaseInfo) {
try {
CatalogInfo catalogInfo = getCatalogInfo(databaseInfo);
Catalog catalog = CatalogUtils.getCatalog(catalogInfo);
if (DatabaseManager.databaseExists(catalog, databaseInfo.getDatabaseName())) {
CatalogInfo catalogInfo = getCatalogInfo(databaseInfo.getCatalogName());
PaimonService service = PaimonServiceUtils.getPaimonService(catalogInfo);
if (service.databaseExists(databaseInfo.getDatabaseName())) {
return R.failed(Status.DATABASE_NAME_IS_EXIST, databaseInfo.getDatabaseName());
}
DatabaseManager.createDatabase(catalog, databaseInfo.getDatabaseName());
service.createDatabase(databaseInfo.getDatabaseName());
return R.succeed();
} catch (Exception e) {
e.printStackTrace();
log.error("Exception with creating database.", e);
return R.failed(Status.DATABASE_CREATE_ERROR);
}
}
Expand All @@ -79,17 +80,18 @@ public R<Void> createDatabase(@RequestBody DatabaseInfo databaseInfo) {
public R<List<DatabaseInfo>> getAllDatabases() {
List<DatabaseInfo> databaseInfoList = new ArrayList<>();
List<CatalogInfo> catalogInfoList = catalogService.list();
if (catalogInfoList.size() > 0) {
if (!CollectionUtils.isEmpty(catalogInfoList)) {
catalogInfoList.forEach(
item -> {
Catalog catalog = CatalogUtils.getCatalog(item);
List<String> list = DatabaseManager.listDatabase(catalog);
PaimonService service = PaimonServiceUtils.getPaimonService(item);
List<String> list = service.listDatabases();
list.forEach(
databaseName -> {
DatabaseInfo info =
DatabaseInfo.builder()
.databaseName(databaseName)
.catalogId(item.getId())
.catalogName(item.getCatalogName())
.description("")
.build();
databaseInfoList.add(info);
Expand All @@ -99,34 +101,37 @@ public R<List<DatabaseInfo>> getAllDatabases() {
return R.succeed(databaseInfoList);
}

/**
* Retrieves the associated CatalogInfo object based on the given DatabaseInfo object.
*
* @param databaseInfo The DatabaseInfo object for which to retrieve the associated CatalogInfo.
* @return The associated CatalogInfo object, or null if it doesn't exist.
*/
private CatalogInfo getCatalogInfo(DatabaseInfo databaseInfo) {
LambdaQueryWrapper<CatalogInfo> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CatalogInfo::getId, databaseInfo.getCatalogId());
return catalogService.getOne(queryWrapper);
}

/**
* Removes a database by its name.
*
* @param databaseInfo The information of the database to be removed.
* @param databaseName The database to be removed.
SteNicholas marked this conversation as resolved.
Show resolved Hide resolved
* @param catalogName The catalog to which the database to be removed belongs.
* @return A response indicating the success or failure of the removal operation.
* @throws RuntimeException if the database is not found or it is not empty.
*/
@DeleteMapping("/delete")
public R<Void> remove(@RequestBody DatabaseInfo databaseInfo) {
@DeleteMapping("/drop/{databaseName}/{catalogName}")
public R<Void> dropDatabase(
@PathVariable String databaseName, @PathVariable String catalogName) {
try {
CatalogInfo catalogInfo = getCatalogInfo(databaseInfo);
Catalog catalog = CatalogUtils.getCatalog(catalogInfo);
DatabaseManager.dropDatabase(catalog, databaseInfo.getDatabaseName());
CatalogInfo catalogInfo = getCatalogInfo(catalogName);
PaimonService service = PaimonServiceUtils.getPaimonService(catalogInfo);
service.dropDatabase(databaseName);
return R.succeed();
} catch (Catalog.DatabaseNotEmptyException | Catalog.DatabaseNotExistException e) {
throw new RuntimeException(e);
} catch (Exception e) {
log.error("Exception with dropping database.", e);
return R.failed(Status.DATABASE_DROP_ERROR);
}
}

/**
* Retrieves the associated CatalogInfo object based on the given catalog name.
*
* @param catalogName The catalog name.
* @return The associated CatalogInfo object, or null if it doesn't exist.
*/
private CatalogInfo getCatalogInfo(String catalogName) {
LambdaQueryWrapper<CatalogInfo> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CatalogInfo::getCatalogName, catalogName);
return catalogService.getOne(queryWrapper);
}
}
Loading
Loading