Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojiebao committed Feb 5, 2024
1 parent 1c7021a commit 4594fe2
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 31 deletions.
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ dependencies {
testImplementation(libs.junit.jupiter.params)
testRuntimeOnly(libs.junit.jupiter.engine)
testImplementation(libs.mockito.core)
testImplementation(libs.h2db)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
Expand Down Expand Up @@ -50,17 +51,18 @@ public <E extends Entity & HasIdentifier> List<E> list(
List<MetalakePO> metalakePOS =
((MetalakeMetaMapper) SqlSessions.getMapper(MetalakeMetaMapper.class))
.listMetalakePOs();
return (List<E>)
metalakePOS.stream()
return metalakePOS != null
? metalakePOS.stream()
.map(
metalakePO -> {
try {
return POConverters.fromMetalakePO(metalakePO);
return (E) POConverters.fromMetalakePO(metalakePO);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toList());
.collect(Collectors.toList())
: new ArrayList<>();
case CATALOG:
case SCHEMA:
case TABLE:
Expand Down Expand Up @@ -102,16 +104,24 @@ public <E extends Entity & HasIdentifier> void insert(E e, boolean overwritten)
try (SqlSession session = SqlSessions.getSqlSession()) {
try {
if (e instanceof BaseMetalake) {
MetalakePO metalakePO = POConverters.toMetalakePO((BaseMetalake) e);
MetalakePO metalakePO =
((MetalakeMetaMapper) SqlSessions.getMapper(MetalakeMetaMapper.class))
.selectMetalakeMetaByName(e.nameIdentifier().name());
if (!overwritten && metalakePO != null) {
throw new EntityAlreadyExistsException(
String.format("Metalake entity: %s already exists", e.nameIdentifier().name()));
}

if (overwritten) {
((MetalakeMetaMapper) SqlSessions.getMapper(MetalakeMetaMapper.class))
.insertMetalakeMetaWithUpdate(metalakePO);
.insertMetalakeMetaWithUpdate(POConverters.toMetalakePO((BaseMetalake) e));
} else {
((MetalakeMetaMapper) SqlSessions.getMapper(MetalakeMetaMapper.class))
.insertMetalakeMeta(metalakePO);
.insertMetalakeMeta(POConverters.toMetalakePO((BaseMetalake) e));
}
SqlSessions.commitAndCloseSqlSession();
} else {
SqlSessions.closeSqlSession();
throw new IllegalArgumentException(
String.format("Unsupported entity type: %s for put operation", e.getClass()));
}
Expand All @@ -138,7 +148,7 @@ public <E extends Entity & HasIdentifier> E update(
Preconditions.checkArgument(
Objects.equals(oldMetalakeEntity.id(), newMetalakeEntity.id()),
String.format(
"The updated metalake entity id: %s is not same with the metalake entity id before: %s",
"The updated metalake entity id: %s should same with the metalake entity id before: %s",
newMetalakeEntity.id(), oldMetalakeEntity.id()));
((MetalakeMetaMapper) SqlSessions.getMapper(MetalakeMetaMapper.class))
.updateMetalakeMeta(POConverters.toMetalakePO(newMetalakeEntity), oldMetalakePO);
Expand Down Expand Up @@ -169,6 +179,9 @@ public <E extends Entity & HasIdentifier> E get(
MetalakePO metalakePO =
((MetalakeMetaMapper) SqlSessions.getMapper(MetalakeMetaMapper.class))
.selectMetalakeMetaByName(ident.name());
if (metalakePO == null) {
return null;
}
return (E) POConverters.fromMetalakePO(metalakePO);
case CATALOG:
case SCHEMA:
Expand Down Expand Up @@ -197,7 +210,7 @@ public boolean delete(NameIdentifier ident, Entity.EntityType entityType, boolea
((MetalakeMetaMapper) SqlSessions.getMapper(MetalakeMetaMapper.class))
.deleteMetalakeMetaById(metalakeId);
if (cascade) {
// TODO We will cascade delete the metadata of sub-resources under metalake
// TODO We will cascade delete the metadata of sub-resources under the metalake
}
SqlSessions.commitAndCloseSqlSession();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public interface MetalakeMetaMapper {
+ " audit_info = #{newMetalakeMeta.auditInfo},"
+ " schema_version = #{newMetalakeMeta.schemaVersion}"
+ " WHERE id = #{oldMetalakeMeta.id}"
+ " and metalake_name = #{oldMetalakeMeta.metalakeComment}"
+ " and metalake_name = #{oldMetalakeMeta.metalakeName}"
+ " and metalake_comment = #{oldMetalakeMeta.metalakeComment}"
+ " and properties = #{oldMetalakeMeta.properties}"
+ " and audit_info = #{oldMetalakeMeta.auditInfo}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

package com.datastrato.gravitino.storage.relation.mysql.orm;

import java.io.Closeable;
import java.io.IOException;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.TransactionIsolationLevel;

public final class SqlSessions implements Closeable {
public final class SqlSessions {
private static final ThreadLocal<SqlSession> sessions = new ThreadLocal<>();

private SqlSessions() {}
Expand Down Expand Up @@ -57,9 +55,4 @@ public static void closeSqlSession() {
public static <T> T getMapper(Class className) {
return (T) getSqlSession().getMapper(className);
}

@Override
public void close() throws IOException {
sessions.remove();
}
}
11 changes: 11 additions & 0 deletions core/src/main/resources/mysql/mysql_init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS `metalake_meta`
(
`id` BIGINT(20) UNSIGNED NOT NULL COMMENT 'metalake id',
`metalake_name` VARCHAR(128) NOT NULL COMMENT 'metalake name',
`metalake_comment` VARCHAR(256) DEFAULT '' COMMENT 'metalake comment',
`properties` MEDIUMTEXT DEFAULT NULL COMMENT 'metalake properties',
`audit_info` MEDIUMTEXT NOT NULL COMMENT 'metalake audit info',
`schema_version` TEXT NOT NULL COMMENT 'metalake schema version info',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_mn` (`metalake_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT 'metalake metadata';
13 changes: 0 additions & 13 deletions core/src/main/resources/mysql_init/mysql_backend_init.sql

This file was deleted.

Loading

0 comments on commit 4594fe2

Please sign in to comment.