Skip to content

Commit

Permalink
Merge branch 'main' into add-mysql-backend-ops-for-catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiebao Xiao committed Feb 27, 2024
2 parents bb5334e + a0b53d6 commit fada86d
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

/** This exception is thrown when an entity is not found. */
public class NoSuchEntityException extends RuntimeException {
/** The no such an entity message for the exception. */
public static final String NO_SUCH_AN_ENTITY_MESSAGE = "No such an entity: %s";

/**
* Constructs a new NoSuchEntityException.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

/** Utility class for working with maps. */
public class MapUtils {
private MapUtils() {}

/**
* Returns a map with all keys that start with the given prefix.
Expand Down
2 changes: 0 additions & 2 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ dependencies {
implementation(libs.caffeine)
implementation(libs.commons.dbcp2)
implementation(libs.commons.io)
implementation(libs.commons.dbcp2)
implementation(libs.commons.lang3)
implementation(libs.guava)
implementation(libs.mybatis)
Expand All @@ -27,7 +26,6 @@ dependencies {
.because("Brings in Guava for Android, which we don't want (and breaks multimaps).")
}
implementation(libs.rocksdbjni)
implementation(libs.mybatis)

annotationProcessor(libs.lombok)
compileOnly(libs.lombok)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
* A MyBatis Mapper for catalog meta operation SQLs.
*
* <p>This interface class is a specification defined by MyBatis. It requires this interface class
* to identify the corresponding SQLs for execution. We can write SQLs in an additional XML file, or
* write SQLs with annotations in this interface Mapper. See: <a
* href="https://mybatis.org/mybatis-3/getting-started.html"></a>
*/
public interface CatalogMetaMapper {
String TABLE_NAME = "catalog_meta";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.util.Objects;
import java.util.function.Function;

/**
* The service class for catalog metadata. It provides the basic database operations for catalog.
*/
public class CatalogMetaService {
private static final CatalogMetaService INSTANCE = new CatalogMetaService();

Expand All @@ -40,14 +43,16 @@ public CatalogEntity getCatalogByIdentifier(NameIdentifier identifier) {
SessionUtils.getWithoutCommit(
MetalakeMetaMapper.class, mapper -> mapper.selectMetalakeIdMetaByName(metalakeName));
if (metalakeId == null) {
throw new NoSuchEntityException("No such an entity: %s", identifier.namespace().toString());
throw new NoSuchEntityException(
NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, identifier.namespace().toString());
}
CatalogPO catalogPO =
SessionUtils.getWithoutCommit(
CatalogMetaMapper.class,
mapper -> mapper.selectCatalogMetaByMetalakeIdAndName(metalakeId, catalogName));
if (catalogPO == null) {
throw new NoSuchEntityException("No such entity: %s", identifier.toString());
throw new NoSuchEntityException(
NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, identifier.toString());
}
return POConverters.fromCatalogPO(catalogPO, identifier.namespace());
}
Expand All @@ -59,7 +64,8 @@ public List<CatalogEntity> listCatalogsByNamespace(Namespace namespace) {
SessionUtils.getWithoutCommit(
MetalakeMetaMapper.class, mapper -> mapper.selectMetalakeIdMetaByName(metalakeName));
if (metalakeId == null) {
throw new NoSuchEntityException("No such an entity: %s", namespace.toString());
throw new NoSuchEntityException(
NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, namespace.toString());
}
List<CatalogPO> catalogPOS =
SessionUtils.getWithoutCommit(
Expand All @@ -76,7 +82,7 @@ public void insertCatalog(CatalogEntity catalogEntity, boolean overwrite) {
mapper -> mapper.selectMetalakeIdMetaByName(catalogEntity.namespace().level(0)));
if (metalakeId == null) {
throw new NoSuchEntityException(
"No such an entity: %s", catalogEntity.namespace().toString());
NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, catalogEntity.namespace().toString());
}
SessionUtils.doWithCommit(
CatalogMetaMapper.class,
Expand Down Expand Up @@ -113,15 +119,17 @@ public <E extends Entity & HasIdentifier> CatalogEntity updateCatalog(
SessionUtils.getWithoutCommit(
MetalakeMetaMapper.class, mapper -> mapper.selectMetalakeIdMetaByName(metalakeName));
if (metalakeId == null) {
throw new NoSuchEntityException("No such an entity: %s", identifier.namespace().toString());
throw new NoSuchEntityException(
NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, identifier.namespace().toString());
}

CatalogPO oldCatalogPO =
SessionUtils.getWithoutCommit(
CatalogMetaMapper.class,
mapper -> mapper.selectCatalogMetaByMetalakeIdAndName(metalakeId, catalogName));
if (oldCatalogPO == null) {
throw new NoSuchEntityException("No such an entity: %s", identifier.toString());
throw new NoSuchEntityException(
NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, identifier.toString());
}

CatalogEntity oldCatalogEntity =
Expand Down Expand Up @@ -156,7 +164,8 @@ public boolean deleteCatalog(NameIdentifier identifier, boolean cascade) {
SessionUtils.getWithoutCommit(
MetalakeMetaMapper.class, mapper -> mapper.selectMetalakeIdMetaByName(metalakeName));
if (metalakeId == null) {
throw new NoSuchEntityException("No such an entity: %s", identifier.namespace().toString());
throw new NoSuchEntityException(
NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, identifier.namespace().toString());
}
Long catalogId =
SessionUtils.getWithoutCommit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.util.Objects;
import java.util.function.Function;

/**
* The service class for metalake metadata. It provides the basic database operations for metalake.
*/
public class MetalakeMetaService {
private static final MetalakeMetaService INSTANCE = new MetalakeMetaService();

Expand All @@ -47,7 +50,8 @@ public BaseMetalake getMetalakeByIdentifier(NameIdentifier ident) {
SessionUtils.getWithoutCommit(
MetalakeMetaMapper.class, mapper -> mapper.selectMetalakeMetaByName(ident.name()));
if (metalakePO == null) {
throw new NoSuchEntityException("No such an entity: %s", ident.toString());
throw new NoSuchEntityException(
NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, ident.toString());
}
return POConverters.fromMetalakePO(metalakePO);
}
Expand Down Expand Up @@ -86,7 +90,8 @@ public <E extends Entity & HasIdentifier> BaseMetalake updateMetalake(
SessionUtils.getWithoutCommit(
MetalakeMetaMapper.class, mapper -> mapper.selectMetalakeMetaByName(ident.name()));
if (oldMetalakePO == null) {
throw new NoSuchEntityException("No such an entity: %s", ident.toString());
throw new NoSuchEntityException(
NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, ident.toString());
}

BaseMetalake oldMetalakeEntity = POConverters.fromMetalakePO(oldMetalakePO);
Expand All @@ -111,36 +116,34 @@ public <E extends Entity & HasIdentifier> BaseMetalake updateMetalake(
}

public boolean deleteMetalake(NameIdentifier ident, boolean cascade) {
MetalakePO metalakePO =
Long metalakeId =
SessionUtils.getWithoutCommit(
MetalakeMetaMapper.class, mapper -> mapper.selectMetalakeMetaByName(ident.name()));
if (metalakePO != null) {
MetalakeMetaMapper.class, mapper -> mapper.selectMetalakeIdMetaByName(ident.name()));
if (metalakeId != null) {
if (cascade) {
SessionUtils.doMultipleWithCommit(
() ->
SessionUtils.doWithoutCommit(
MetalakeMetaMapper.class,
mapper ->
mapper.softDeleteMetalakeMetaByMetalakeId(metalakePO.getMetalakeId())),
mapper -> mapper.softDeleteMetalakeMetaByMetalakeId(metalakeId)),
() ->
SessionUtils.doWithoutCommit(
CatalogMetaMapper.class,
mapper ->
mapper.softDeleteCatalogMetasByMetalakeId(metalakePO.getMetalakeId())),
mapper -> mapper.softDeleteCatalogMetasByMetalakeId(metalakeId)),
() -> {
// TODO We will cascade delete the metadata of sub-resources under the metalake
});
} else {
List<CatalogEntity> catalogEntities =
CatalogMetaService.getInstance()
.listCatalogsByNamespace(Namespace.ofCatalog(metalakePO.getMetalakeName()));
.listCatalogsByNamespace(Namespace.ofCatalog(ident.name()));
if (!catalogEntities.isEmpty()) {
throw new NonEmptyEntityException(
"Entity %s has sub-entities, you should remove sub-entities first", ident);
}
SessionUtils.doWithCommit(
MetalakeMetaMapper.class,
mapper -> mapper.softDeleteMetalakeMetaByMetalakeId(metalakePO.getMetalakeId()));
mapper -> mapper.softDeleteMetalakeMetaByMetalakeId(metalakeId));
}
}
return true;
Expand Down
Loading

0 comments on commit fada86d

Please sign in to comment.