Skip to content

Commit

Permalink
[Feature] Optimize catalog controller (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
hunter-cloud09 authored Oct 25, 2023
1 parent 59fb611 commit 6ee3b98
Show file tree
Hide file tree
Showing 20 changed files with 392 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ public void createDatabase(String databaseName) {
}
}

public void createDatabase(String databaseName, boolean ignoreIfExists) {
Preconditions.checkNotNull(databaseName, "Database name cannot be null.");

try {
catalog.createDatabase(databaseName, ignoreIfExists);
} catch (Catalog.DatabaseAlreadyExistException e) {
throw new DatabaseException.DatabaseAlreadyExistsException(
String.format(
"The database '%s' already exists in the catalog.", databaseName));
}
}

public void dropDatabase(String databaseName) {
Preconditions.checkNotNull(databaseName, "Database name cannot be null.");
try {
Expand All @@ -93,6 +105,34 @@ public void dropDatabase(String databaseName) {
}
}

public void dropDatabase(String databaseName, boolean ignoreIfNotExists) {
Preconditions.checkNotNull(databaseName, "Database name cannot be null.");
try {
catalog.dropDatabase(databaseName, ignoreIfNotExists, true);
} catch (Catalog.DatabaseNotExistException e) {
throw new DatabaseException.DatabaseNotExistException(
String.format(
"The database '%s' does not exist in the catalog.", databaseName));
} catch (Catalog.DatabaseNotEmptyException e) {
throw new DatabaseException.DatabaseNotEmptyException(
String.format("The database '%s' is not empty.", databaseName));
}
}

public void dropDatabase(String databaseName, boolean ignoreIfNotExists, boolean cascade) {
Preconditions.checkNotNull(databaseName, "Database name cannot be null.");
try {
catalog.dropDatabase(databaseName, ignoreIfNotExists, cascade);
} catch (Catalog.DatabaseNotExistException e) {
throw new DatabaseException.DatabaseNotExistException(
String.format(
"The database '%s' does not exist in the catalog.", databaseName));
} catch (Catalog.DatabaseNotEmptyException e) {
throw new DatabaseException.DatabaseNotEmptyException(
String.format("The database '%s' is not empty.", databaseName));
}
}

public List<String> listTables(String databaseName) {
Preconditions.checkNotNull(databaseName, "Database name cannot be null.");
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,16 @@

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

import org.apache.paimon.web.api.catalog.PaimonServiceFactory;
import org.apache.paimon.web.server.data.enums.CatalogMode;
import org.apache.paimon.web.server.data.dto.CatalogDTO;
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 com.baomidou.mybatisplus.core.toolkit.Wrappers;
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;
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 @@ -45,40 +41,22 @@
@RequestMapping("/api/catalog")
public class CatalogController {

@Autowired private CatalogService catalogService;
private final CatalogService catalogService;

public CatalogController(CatalogService catalogService) {
this.catalogService = catalogService;
}

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

public R<Void> createCatalog(@RequestBody CatalogDTO catalogDTO) {
try {
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();
return catalogService.createCatalog(catalogDTO);
} catch (Exception e) {
log.error("Exception with creating catalog.", e);
return R.failed(Status.CATALOG_CREATE_ERROR);
Expand All @@ -97,17 +75,25 @@ public R<List<CatalogInfo>> getCatalog() {
}

/**
* Removes a catalog with given catalog name.
* Removes a catalog with given catalog name or catalog id.
*
* @param catalogName The catalog name.
* @param catalogDTO Given the catalog name or catalog id to remove catalog.
* @return A response indicating the success or failure of the operation.
*/
@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);
@PostMapping("/remove")
public R<Void> removeCatalog(@RequestBody CatalogDTO catalogDTO) {
boolean remove;
if (StringUtils.isNotBlank(catalogDTO.getName())) {
remove =
catalogService.remove(
Wrappers.lambdaQuery(CatalogInfo.class)
.eq(CatalogInfo::getCatalogName, catalogDTO.getName()));
} else {
remove =
catalogService.remove(
Wrappers.lambdaQuery(CatalogInfo.class)
.eq(CatalogInfo::getId, catalogDTO.getId()));
}
return remove ? R.succeed() : R.failed(Status.CATALOG_REMOVE_ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,58 @@
package org.apache.paimon.web.server.controller;

import org.apache.paimon.web.api.catalog.PaimonService;
import org.apache.paimon.web.server.data.dto.DatabaseDTO;
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.PaimonServiceUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
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;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/** Database api controller. */
@Slf4j
@RestController
@RequestMapping("/api/database")
public class DatabaseController {

@Autowired private CatalogService catalogService;
private final CatalogService catalogService;

public DatabaseController(CatalogService catalogService) {
this.catalogService = catalogService;
}

/**
* Creates a new database based on the provided DatabaseInfo.
*
* @param databaseInfo The DatabaseInfo object containing the details of the new database.
* @param databaseDTO The DatabaseInfo object containing the details of the new database.
* @return R<Void/> indicating the result of the operation.
*/
@PostMapping("/create")
public R<Void> createDatabase(@RequestBody DatabaseInfo databaseInfo) {
public R<Void> createDatabase(@RequestBody DatabaseDTO databaseDTO) {
try {
CatalogInfo catalogInfo = getCatalogInfo(databaseInfo.getCatalogName());
CatalogInfo catalogInfo = getCatalogInfo(databaseDTO);
PaimonService service = PaimonServiceUtils.getPaimonService(catalogInfo);
if (service.databaseExists(databaseInfo.getDatabaseName())) {
return R.failed(Status.DATABASE_NAME_IS_EXIST, databaseInfo.getDatabaseName());
if (service.databaseExists(databaseDTO.getName())) {
return R.failed(Status.DATABASE_NAME_IS_EXIST, databaseDTO.getName());
}
service.createDatabase(databaseInfo.getDatabaseName());
service.createDatabase(
databaseDTO.getName(),
BooleanUtils.toBooleanDefaultIfNull(databaseDTO.isIgnoreIfExists(), false));
return R.succeed();
} catch (Exception e) {
log.error("Exception with creating database.", e);
Expand Down Expand Up @@ -104,18 +111,19 @@ public R<List<DatabaseInfo>> getAllDatabases() {
/**
* Removes a database by its name.
*
* @param databaseName The database to be removed.
* @param catalogName The catalog to which the database to be removed belongs.
* @param databaseDTO The drop database DTO.
* @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.
* @throws RuntimeException if the database is not found, or it is not empty.
*/
@DeleteMapping("/drop/{databaseName}/{catalogName}")
public R<Void> dropDatabase(
@PathVariable String databaseName, @PathVariable String catalogName) {
@PostMapping("/drop")
public R<Void> dropDatabase(@RequestBody DatabaseDTO databaseDTO) {
try {
CatalogInfo catalogInfo = getCatalogInfo(catalogName);
CatalogInfo catalogInfo = getCatalogInfo(databaseDTO);
PaimonService service = PaimonServiceUtils.getPaimonService(catalogInfo);
service.dropDatabase(databaseName);
service.dropDatabase(
databaseDTO.getName(),
BooleanUtils.toBooleanDefaultIfNull(databaseDTO.isIgnoreIfExists(), false),
BooleanUtils.toBooleanDefaultIfNull(databaseDTO.isCascade(), true));
return R.succeed();
} catch (Exception e) {
log.error("Exception with dropping database.", e);
Expand All @@ -124,14 +132,27 @@ public R<Void> dropDatabase(
}

/**
* Retrieves the associated CatalogInfo object based on the given catalog name.
* Retrieves the associated CatalogInfo object based on the given catalog id.
*
* @param catalogName The catalog name.
* @param databaseDTO The database DTO.
* @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);
private CatalogInfo getCatalogInfo(DatabaseDTO databaseDTO) {
CatalogInfo catalogInfo;
if (StringUtils.isNotBlank(databaseDTO.getCatalogId())) {
catalogInfo =
catalogService.getOne(
Wrappers.lambdaQuery(CatalogInfo.class)
.eq(CatalogInfo::getId, databaseDTO.getCatalogId()));
} else {
catalogInfo =
catalogService.getOne(
Wrappers.lambdaQuery(CatalogInfo.class)
.eq(CatalogInfo::getCatalogName, databaseDTO.getCatalogName()));
}
Objects.requireNonNull(
catalogInfo,
String.format("CatalogName: [%s] not found.", databaseDTO.getCatalogName()));
return catalogInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

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

import org.apache.paimon.web.server.data.dto.LoginDto;
import org.apache.paimon.web.server.data.dto.LoginDTO;
import org.apache.paimon.web.server.data.result.R;
import org.apache.paimon.web.server.data.vo.UserInfoVo;
import org.apache.paimon.web.server.service.UserService;
Expand All @@ -44,12 +44,12 @@ public class LoginController {
/**
* login by username and password.
*
* @param loginDto login info
* @param loginDTO login info
* @return token string
*/
@PostMapping("/login")
public R<UserInfoVo> login(@RequestBody LoginDto loginDto) {
return R.succeed(userService.login(loginDto));
public R<UserInfoVo> login(@RequestBody LoginDTO loginDTO) {
return R.succeed(userService.login(loginDTO));
}

/**
Expand Down
Loading

0 comments on commit 6ee3b98

Please sign in to comment.