Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojiebao committed Feb 6, 2024
1 parent bc7d1e2 commit fde3c14
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 5 deletions.
2 changes: 2 additions & 0 deletions LICENSE.bin
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@
J2ObjC
SQLite JDBC Driver
Immutables
MyBatis

This product bundles various third-party components also under the
Apache Software Foundation License 1.1
Expand Down Expand Up @@ -410,6 +411,7 @@
Mozilla Public License

Javassist
H2 Database Engine


This product bundles various third-party components also placed in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import com.datastrato.gravitino.meta.BaseMetalake;
import com.datastrato.gravitino.storage.relational.RelationalBackend;
import com.datastrato.gravitino.storage.relational.mysql.mapper.MetalakeMetaMapper;
import com.datastrato.gravitino.storage.relational.mysql.orm.SqlSessionFactoryHelper;
import com.datastrato.gravitino.storage.relational.mysql.orm.SqlSessions;
import com.datastrato.gravitino.storage.relational.mysql.po.MetalakePO;
import com.datastrato.gravitino.storage.relational.mysql.session.SqlSessionFactoryHelper;
import com.datastrato.gravitino.storage.relational.mysql.session.SqlSessions;
import com.datastrato.gravitino.storage.relational.mysql.utils.POConverters;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Preconditions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
* This software is licensed under the Apache License version 2.
*/

package com.datastrato.gravitino.storage.relational.mysql.orm;
package com.datastrato.gravitino.storage.relational.mysql.session;

import com.datastrato.gravitino.Config;
import com.datastrato.gravitino.Configs;
import com.datastrato.gravitino.storage.relational.mysql.mapper.MetalakeMetaMapper;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import java.time.Duration;
import org.apache.commons.dbcp2.BasicDataSource;
Expand All @@ -34,6 +35,11 @@ public static SqlSessionFactoryHelper getInstance() {

private SqlSessionFactoryHelper() {}

@VisibleForTesting
static void setSqlSessionFactory(SqlSessionFactory sessionFactory) {
sqlSessionFactory = sessionFactory;
}

/**
* Initialize the SqlSessionFactory object.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
* This software is licensed under the Apache License version 2.
*/

package com.datastrato.gravitino.storage.relational.mysql.orm;
package com.datastrato.gravitino.storage.relational.mysql.session;

import com.google.common.annotations.VisibleForTesting;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.TransactionIsolationLevel;

Expand All @@ -18,6 +19,11 @@ public final class SqlSessions {

private SqlSessions() {}

@VisibleForTesting
static ThreadLocal<SqlSession> getSessions() {
return sessions;
}

/**
* Get the SqlSession object. If the SqlSession object is not present in the thread local, then
* create a new SqlSession object and set it in the thread local.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.datastrato.gravitino.meta.AuditInfo;
import com.datastrato.gravitino.meta.BaseMetalake;
import com.datastrato.gravitino.meta.SchemaVersion;
import com.datastrato.gravitino.storage.relational.mysql.orm.SqlSessionFactoryHelper;
import com.datastrato.gravitino.storage.relational.mysql.session.SqlSessionFactoryHelper;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright 2024 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/

package com.datastrato.gravitino.storage.relational.mysql.session;

import static com.datastrato.gravitino.Configs.DEFAULT_ENTITY_RELATIONAL_STORE;
import static com.datastrato.gravitino.Configs.ENTITY_RELATIONAL_STORE;
import static com.datastrato.gravitino.Configs.ENTITY_STORE;
import static com.datastrato.gravitino.Configs.MYSQL_ENTITY_STORE_DRIVER_NAME_KEY;
import static com.datastrato.gravitino.Configs.MYSQL_ENTITY_STORE_URL_KEY;
import static com.datastrato.gravitino.Configs.MYSQL_ENTITY_STORE_USERNAME_KEY;
import static com.datastrato.gravitino.Configs.RELATIONAL_ENTITY_STORE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.datastrato.gravitino.Config;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.UUID;
import org.apache.commons.dbcp2.BasicDataSource;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class TestSqlSessionFactoryHelper {
private static final String MYSQL_STORE_PATH =
"/tmp/gravitino_test_entityStore_" + UUID.randomUUID().toString().replace("-", "");
private static final String DB_DIR = MYSQL_STORE_PATH + "/testdb";

private static Config config;

@BeforeAll
public static void setUp() {
File dir = new File(DB_DIR);
if (dir.exists() || !dir.isDirectory()) {
dir.delete();
}
dir.mkdirs();

config = Mockito.mock(Config.class);
Mockito.when(config.get(ENTITY_STORE)).thenReturn(RELATIONAL_ENTITY_STORE);
Mockito.when(config.get(ENTITY_RELATIONAL_STORE)).thenReturn(DEFAULT_ENTITY_RELATIONAL_STORE);
Mockito.when(config.getRawString(MYSQL_ENTITY_STORE_URL_KEY))
.thenReturn(String.format("jdbc:h2:%s;DB_CLOSE_DELAY=-1;MODE=MYSQL", DB_DIR));
Mockito.when(config.getRawString(MYSQL_ENTITY_STORE_USERNAME_KEY)).thenReturn("sa");
Mockito.when(config.getRawString(MYSQL_ENTITY_STORE_DRIVER_NAME_KEY))
.thenReturn("org.h2.Driver");
}

@BeforeEach
public void init() {
SqlSessionFactoryHelper.setSqlSessionFactory(null);
}

@AfterEach
public void cleanUp() {
SqlSessionFactoryHelper.setSqlSessionFactory(null);
}

@AfterAll
public static void tearDown() throws IOException {
File dir = new File(DB_DIR);
if (dir.exists()) {
dir.delete();
}
}

@Test
public void testGetInstance() {
SqlSessionFactoryHelper instance = SqlSessionFactoryHelper.getInstance();
assertNotNull(instance);
}

@Test
public void testInit() throws SQLException {
SqlSessionFactoryHelper.getInstance().init(config);
assertNotNull(SqlSessionFactoryHelper.getInstance().getSqlSessionFactory());
BasicDataSource dataSource =
(BasicDataSource)
SqlSessionFactoryHelper.getInstance()
.getSqlSessionFactory()
.getConfiguration()
.getEnvironment()
.getDataSource();
assertEquals("org.h2.Driver", dataSource.getDriverClassName());
assertEquals(config.getRawString(MYSQL_ENTITY_STORE_URL_KEY), dataSource.getUrl());
}

@Test
public void testGetSqlSessionFactoryWithoutInit() {
assertThrows(
IllegalStateException.class,
() -> SqlSessionFactoryHelper.getInstance().getSqlSessionFactory());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2024 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/

package com.datastrato.gravitino.storage.relational.mysql.session;

import static com.datastrato.gravitino.Configs.DEFAULT_ENTITY_RELATIONAL_STORE;
import static com.datastrato.gravitino.Configs.ENTITY_RELATIONAL_STORE;
import static com.datastrato.gravitino.Configs.ENTITY_STORE;
import static com.datastrato.gravitino.Configs.MYSQL_ENTITY_STORE_DRIVER_NAME_KEY;
import static com.datastrato.gravitino.Configs.MYSQL_ENTITY_STORE_URL_KEY;
import static com.datastrato.gravitino.Configs.MYSQL_ENTITY_STORE_USERNAME_KEY;
import static com.datastrato.gravitino.Configs.RELATIONAL_ENTITY_STORE;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import com.datastrato.gravitino.Config;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class TestSqlSessions {
private static final String MYSQL_STORE_PATH =
"/tmp/gravitino_test_entityStore_" + UUID.randomUUID().toString().replace("-", "");
private static final String DB_DIR = MYSQL_STORE_PATH + "/testdb";

@BeforeAll
public static void setUp() {
File dir = new File(DB_DIR);
if (dir.exists() || !dir.isDirectory()) {
dir.delete();
}
dir.mkdirs();

Config config = Mockito.mock(Config.class);
Mockito.when(config.get(ENTITY_STORE)).thenReturn(RELATIONAL_ENTITY_STORE);
Mockito.when(config.get(ENTITY_RELATIONAL_STORE)).thenReturn(DEFAULT_ENTITY_RELATIONAL_STORE);
Mockito.when(config.getRawString(MYSQL_ENTITY_STORE_URL_KEY))
.thenReturn(String.format("jdbc:h2:%s;DB_CLOSE_DELAY=-1;MODE=MYSQL", DB_DIR));
Mockito.when(config.getRawString(MYSQL_ENTITY_STORE_USERNAME_KEY)).thenReturn("sa");
Mockito.when(config.getRawString(MYSQL_ENTITY_STORE_DRIVER_NAME_KEY))
.thenReturn("org.h2.Driver");
SqlSessionFactoryHelper.getInstance().init(config);
}

@AfterAll
public static void tearDown() throws IOException {
File dir = new File(DB_DIR);
if (dir.exists()) {
dir.delete();
}
}

@Test
public void testOpenAndCloseSqlSession() {
SqlSession session = SqlSessions.getSqlSession();
assertNotNull(session);
SqlSessions.closeSqlSession();
assertNull(SqlSessions.getSessions().get());
}

@Test
public void testOpenAndCommitAndCloseSqlSession() {
SqlSession session = SqlSessions.getSqlSession();
assertNotNull(session);
SqlSessions.commitAndCloseSqlSession();
assertNull(SqlSessions.getSessions().get());
}

@Test
public void testOpenAndRollbackAndCloseSqlSession() {
SqlSession session = SqlSessions.getSqlSession();
assertNotNull(session);
SqlSessions.rollbackAndCloseSqlSession();
assertNull(SqlSessions.getSessions().get());
}
}

0 comments on commit fde3c14

Please sign in to comment.