Skip to content

Commit

Permalink
[#4868] improvement(core): Modify tag and user related mapper class t…
Browse files Browse the repository at this point in the history
…o use the new framework to support multiple JDBC backend. (#4869)

### What changes were proposed in this pull request?

Change tag, user, and role-related code to support the new framework
introduced by #4832

### Why are the changes needed?

To support multiple JDBC backend.

Fix: #4868 

### Does this PR introduce _any_ user-facing change?

N/A.

### How was this patch tested?

Existing UTs and ITs.
  • Loading branch information
yuqi1129 authored Sep 6, 2024
1 parent a0cb174 commit 0403f31
Show file tree
Hide file tree
Showing 28 changed files with 2,339 additions and 915 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.gravitino.storage.relational.mapper;

import static org.apache.gravitino.storage.relational.mapper.GroupMetaMapper.GROUP_TABLE_NAME;
import static org.apache.gravitino.storage.relational.mapper.RoleMetaMapper.GROUP_ROLE_RELATION_TABLE_NAME;

import org.apache.gravitino.storage.relational.po.GroupPO;
import org.apache.ibatis.annotations.Param;

public class GroupMetaBaseSQLProvider {

public String selectGroupIdBySchemaIdAndName(
@Param("metalakeId") Long metalakeId, @Param("groupName") String name) {
return "SELECT group_id as groupId FROM "
+ GROUP_TABLE_NAME
+ " WHERE metalake_id = #{metalakeId} AND group_name = #{groupName}"
+ " AND deleted_at = 0";
}

public String selectGroupMetaByMetalakeIdAndName(
@Param("metalakeId") Long metalakeId, @Param("groupName") String name) {
return "SELECT group_id as groupId, group_name as groupName,"
+ " metalake_id as metalakeId,"
+ " audit_info as auditInfo,"
+ " current_version as currentVersion, last_version as lastVersion,"
+ " deleted_at as deletedAt"
+ " FROM "
+ GROUP_TABLE_NAME
+ " WHERE metalake_id = #{metalakeId} AND group_name = #{groupName}"
+ " AND deleted_at = 0";
}

public String insertGroupMeta(@Param("groupMeta") GroupPO groupPO) {
return "INSERT INTO "
+ GROUP_TABLE_NAME
+ "(group_id, group_name,"
+ " metalake_id, audit_info,"
+ " current_version, last_version, deleted_at)"
+ " VALUES("
+ " #{groupMeta.groupId},"
+ " #{groupMeta.groupName},"
+ " #{groupMeta.metalakeId},"
+ " #{groupMeta.auditInfo},"
+ " #{groupMeta.currentVersion},"
+ " #{groupMeta.lastVersion},"
+ " #{groupMeta.deletedAt}"
+ " )";
}

public String insertGroupMetaOnDuplicateKeyUpdate(@Param("groupMeta") GroupPO groupPO) {
return "INSERT INTO "
+ GROUP_TABLE_NAME
+ "(group_id, group_name,"
+ "metalake_id, audit_info,"
+ " current_version, last_version, deleted_at)"
+ " VALUES("
+ " #{groupMeta.groupId},"
+ " #{groupMeta.groupName},"
+ " #{groupMeta.metalakeId},"
+ " #{groupMeta.auditInfo},"
+ " #{groupMeta.currentVersion},"
+ " #{groupMeta.lastVersion},"
+ " #{groupMeta.deletedAt}"
+ " )"
+ " ON DUPLICATE KEY UPDATE"
+ " group_name = #{groupMeta.groupName},"
+ " metalake_id = #{groupMeta.metalakeId},"
+ " audit_info = #{groupMeta.auditInfo},"
+ " current_version = #{groupMeta.currentVersion},"
+ " last_version = #{groupMeta.lastVersion},"
+ " deleted_at = #{groupMeta.deletedAt}";
}

public String softDeleteGroupMetaByGroupId(@Param("groupId") Long groupId) {
return "UPDATE "
+ GROUP_TABLE_NAME
+ " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
+ " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000"
+ " WHERE group_id = #{groupId} AND deleted_at = 0";
}

public String softDeleteGroupMetasByMetalakeId(@Param("metalakeId") Long metalakeId) {
return "UPDATE "
+ GROUP_TABLE_NAME
+ " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
+ " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000"
+ " WHERE metalake_id = #{metalakeId} AND deleted_at = 0";
}

public String updateGroupMeta(
@Param("newGroupMeta") GroupPO newGroupPO, @Param("oldGroupMeta") GroupPO oldGroupPO) {
return "UPDATE "
+ GROUP_TABLE_NAME
+ " SET group_name = #{newGroupMeta.groupName},"
+ " metalake_id = #{newGroupMeta.metalakeId},"
+ " audit_info = #{newGroupMeta.auditInfo},"
+ " current_version = #{newGroupMeta.currentVersion},"
+ " last_version = #{newGroupMeta.lastVersion},"
+ " deleted_at = #{newGroupMeta.deletedAt}"
+ " WHERE group_id = #{oldGroupMeta.groupId}"
+ " AND group_name = #{oldGroupMeta.groupName}"
+ " AND metalake_id = #{oldGroupMeta.metalakeId}"
+ " AND audit_info = #{oldGroupMeta.auditInfo}"
+ " AND current_version = #{oldGroupMeta.currentVersion}"
+ " AND last_version = #{oldGroupMeta.lastVersion}"
+ " AND deleted_at = 0";
}

public String listGroupsByRoleId(@Param("roleId") Long roleId) {
return "SELECT gr.group_id as groupId, gr.group_name as groupName,"
+ " gr.metalake_id as metalakeId,"
+ " gr.audit_info as auditInfo, gr.current_version as currentVersion,"
+ " gr.last_version as lastVersion, gr.deleted_at as deletedAt"
+ " FROM "
+ GROUP_TABLE_NAME
+ " gr JOIN "
+ GROUP_ROLE_RELATION_TABLE_NAME
+ " re ON gr.group_id = re.group_id"
+ " WHERE re.role_id = #{roleId}"
+ " AND gr.deleted_at = 0 AND re.deleted_at = 0";
}

public String deleteGroupMetasByLegacyTimeline(
@Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) {
return "DELETE FROM "
+ GROUP_TABLE_NAME
+ " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

import java.util.List;
import org.apache.gravitino.storage.relational.po.GroupPO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.UpdateProvider;

/**
* A MyBatis Mapper for table meta operation SQLs.
Expand All @@ -39,121 +39,44 @@ public interface GroupMetaMapper {
String GROUP_TABLE_NAME = "group_meta";
String GROUP_ROLE_RELATION_TABLE_NAME = "group_role_rel";

@Select(
"SELECT group_id as groupId FROM "
+ GROUP_TABLE_NAME
+ " WHERE metalake_id = #{metalakeId} AND group_name = #{groupName}"
+ " AND deleted_at = 0")
@SelectProvider(
type = GroupMetaSQLProviderFactory.class,
method = "selectGroupIdBySchemaIdAndName")
Long selectGroupIdBySchemaIdAndName(
@Param("metalakeId") Long metalakeId, @Param("groupName") String name);

@Select(
"SELECT group_id as groupId, group_name as groupName,"
+ " metalake_id as metalakeId,"
+ " audit_info as auditInfo,"
+ " current_version as currentVersion, last_version as lastVersion,"
+ " deleted_at as deletedAt"
+ " FROM "
+ GROUP_TABLE_NAME
+ " WHERE metalake_id = #{metalakeId} AND group_name = #{groupName}"
+ " AND deleted_at = 0")
@SelectProvider(
type = GroupMetaSQLProviderFactory.class,
method = "selectGroupMetaByMetalakeIdAndName")
GroupPO selectGroupMetaByMetalakeIdAndName(
@Param("metalakeId") Long metalakeId, @Param("groupName") String name);

@Insert(
"INSERT INTO "
+ GROUP_TABLE_NAME
+ "(group_id, group_name,"
+ " metalake_id, audit_info,"
+ " current_version, last_version, deleted_at)"
+ " VALUES("
+ " #{groupMeta.groupId},"
+ " #{groupMeta.groupName},"
+ " #{groupMeta.metalakeId},"
+ " #{groupMeta.auditInfo},"
+ " #{groupMeta.currentVersion},"
+ " #{groupMeta.lastVersion},"
+ " #{groupMeta.deletedAt}"
+ " )")
@InsertProvider(type = GroupMetaSQLProviderFactory.class, method = "insertGroupMeta")
void insertGroupMeta(@Param("groupMeta") GroupPO groupPO);

@Insert(
"INSERT INTO "
+ GROUP_TABLE_NAME
+ "(group_id, group_name,"
+ "metalake_id, audit_info,"
+ " current_version, last_version, deleted_at)"
+ " VALUES("
+ " #{groupMeta.groupId},"
+ " #{groupMeta.groupName},"
+ " #{groupMeta.metalakeId},"
+ " #{groupMeta.auditInfo},"
+ " #{groupMeta.currentVersion},"
+ " #{groupMeta.lastVersion},"
+ " #{groupMeta.deletedAt}"
+ " )"
+ " ON DUPLICATE KEY UPDATE"
+ " group_name = #{groupMeta.groupName},"
+ " metalake_id = #{groupMeta.metalakeId},"
+ " audit_info = #{groupMeta.auditInfo},"
+ " current_version = #{groupMeta.currentVersion},"
+ " last_version = #{groupMeta.lastVersion},"
+ " deleted_at = #{groupMeta.deletedAt}")
@InsertProvider(
type = GroupMetaSQLProviderFactory.class,
method = "insertGroupMetaOnDuplicateKeyUpdate")
void insertGroupMetaOnDuplicateKeyUpdate(@Param("groupMeta") GroupPO groupPO);

@Update(
"UPDATE "
+ GROUP_TABLE_NAME
+ " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
+ " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000"
+ " WHERE group_id = #{groupId} AND deleted_at = 0")
@UpdateProvider(type = GroupMetaSQLProviderFactory.class, method = "softDeleteGroupMetaByGroupId")
void softDeleteGroupMetaByGroupId(@Param("groupId") Long groupId);

@Update(
"UPDATE "
+ GROUP_TABLE_NAME
+ " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
+ " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000"
+ " WHERE metalake_id = #{metalakeId} AND deleted_at = 0")
@UpdateProvider(
type = GroupMetaSQLProviderFactory.class,
method = "softDeleteGroupMetasByMetalakeId")
void softDeleteGroupMetasByMetalakeId(@Param("metalakeId") Long metalakeId);

@Update(
"UPDATE "
+ GROUP_TABLE_NAME
+ " SET group_name = #{newGroupMeta.groupName},"
+ " metalake_id = #{newGroupMeta.metalakeId},"
+ " audit_info = #{newGroupMeta.auditInfo},"
+ " current_version = #{newGroupMeta.currentVersion},"
+ " last_version = #{newGroupMeta.lastVersion},"
+ " deleted_at = #{newGroupMeta.deletedAt}"
+ " WHERE group_id = #{oldGroupMeta.groupId}"
+ " AND group_name = #{oldGroupMeta.groupName}"
+ " AND metalake_id = #{oldGroupMeta.metalakeId}"
+ " AND audit_info = #{oldGroupMeta.auditInfo}"
+ " AND current_version = #{oldGroupMeta.currentVersion}"
+ " AND last_version = #{oldGroupMeta.lastVersion}"
+ " AND deleted_at = 0")
@UpdateProvider(type = GroupMetaSQLProviderFactory.class, method = "updateGroupMeta")
Integer updateGroupMeta(
@Param("newGroupMeta") GroupPO newGroupPO, @Param("oldGroupMeta") GroupPO oldGroupPO);

@Select(
"SELECT gr.group_id as groupId, gr.group_name as groupName,"
+ " gr.metalake_id as metalakeId,"
+ " gr.audit_info as auditInfo, gr.current_version as currentVersion,"
+ " gr.last_version as lastVersion, gr.deleted_at as deletedAt"
+ " FROM "
+ GROUP_TABLE_NAME
+ " gr JOIN "
+ GROUP_ROLE_RELATION_TABLE_NAME
+ " re ON gr.group_id = re.group_id"
+ " WHERE re.role_id = #{roleId}"
+ " AND gr.deleted_at = 0 AND re.deleted_at = 0")
@SelectProvider(type = GroupMetaSQLProviderFactory.class, method = "listGroupsByRoleId")
List<GroupPO> listGroupsByRoleId(@Param("roleId") Long roleId);

@Delete(
"DELETE FROM "
+ GROUP_TABLE_NAME
+ " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}")
@DeleteProvider(
type = GroupMetaSQLProviderFactory.class,
method = "deleteGroupMetasByLegacyTimeline")
Integer deleteGroupMetasByLegacyTimeline(
@Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.gravitino.storage.relational.mapper;

import com.google.common.collect.ImmutableMap;
import java.util.Map;
import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType;
import org.apache.gravitino.storage.relational.po.GroupPO;
import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
import org.apache.ibatis.annotations.Param;

public class GroupMetaSQLProviderFactory {
private static final Map<JDBCBackendType, GroupMetaBaseSQLProvider>
METALAKE_META_SQL_PROVIDER_MAP =
ImmutableMap.of(
JDBCBackendType.MYSQL, new GroupMetaMySQLProvider(),
JDBCBackendType.H2, new GroupMetaH2Provider());

public static GroupMetaBaseSQLProvider getProvider() {
String databaseId =
SqlSessionFactoryHelper.getInstance()
.getSqlSessionFactory()
.getConfiguration()
.getDatabaseId();

JDBCBackendType jdbcBackendType = JDBCBackendType.fromString(databaseId);
return METALAKE_META_SQL_PROVIDER_MAP.get(jdbcBackendType);
}

static class GroupMetaMySQLProvider extends GroupMetaBaseSQLProvider {}

static class GroupMetaH2Provider extends GroupMetaBaseSQLProvider {}

public static String selectGroupIdBySchemaIdAndName(
@Param("metalakeId") Long metalakeId, @Param("groupName") String name) {
return getProvider().selectGroupIdBySchemaIdAndName(metalakeId, name);
}

public static String selectGroupMetaByMetalakeIdAndName(
@Param("metalakeId") Long metalakeId, @Param("groupName") String name) {
return getProvider().selectGroupMetaByMetalakeIdAndName(metalakeId, name);
}

public static String insertGroupMeta(@Param("groupMeta") GroupPO groupPO) {
return getProvider().insertGroupMeta(groupPO);
}

public static String insertGroupMetaOnDuplicateKeyUpdate(@Param("groupMeta") GroupPO groupPO) {
return getProvider().insertGroupMetaOnDuplicateKeyUpdate(groupPO);
}

public static String softDeleteGroupMetaByGroupId(@Param("groupId") Long groupId) {
return getProvider().softDeleteGroupMetaByGroupId(groupId);
}

public static String softDeleteGroupMetasByMetalakeId(@Param("metalakeId") Long metalakeId) {
return getProvider().softDeleteGroupMetasByMetalakeId(metalakeId);
}

public static String updateGroupMeta(
@Param("newGroupMeta") GroupPO newGroupPO, @Param("oldGroupMeta") GroupPO oldGroupPO) {
return getProvider().updateGroupMeta(newGroupPO, oldGroupPO);
}

public static String listGroupsByRoleId(@Param("roleId") Long roleId) {
return getProvider().listGroupsByRoleId(roleId);
}

public static String deleteGroupMetasByLegacyTimeline(
@Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) {
return getProvider().deleteGroupMetasByLegacyTimeline(legacyTimeline, limit);
}
}
Loading

0 comments on commit 0403f31

Please sign in to comment.