forked from apache/gravitino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[apache#2851] feat(core): Add relational backend for Group Entity (ap…
…ache#3031) ### What changes were proposed in this pull request? add relational backend for Group Entity [note]: wait for apache#2850 merged first ### Why are the changes needed? Fix: apache#2851 ### Does this PR introduce _any_ user-facing change? N/A ### How was this patch tested? ut --------- Co-authored-by: yangliwei <[email protected]>
- Loading branch information
Showing
19 changed files
with
1,750 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
core/src/main/java/com/datastrato/gravitino/storage/relational/mapper/GroupMetaMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright 2024 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
|
||
package com.datastrato.gravitino.storage.relational.mapper; | ||
|
||
import com.datastrato.gravitino.storage.relational.po.GroupPO; | ||
import org.apache.ibatis.annotations.Insert; | ||
import org.apache.ibatis.annotations.Param; | ||
import org.apache.ibatis.annotations.Select; | ||
import org.apache.ibatis.annotations.Update; | ||
|
||
/** | ||
* A MyBatis Mapper for table 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 GroupMetaMapper { | ||
String TABLE_NAME = "group_meta"; | ||
|
||
@Select( | ||
"SELECT group_id as groupId FROM " | ||
+ TABLE_NAME | ||
+ " WHERE metalake_id = #{metalakeId} AND group_name = #{groupName}" | ||
+ " AND deleted_at = 0") | ||
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 " | ||
+ TABLE_NAME | ||
+ " WHERE metalake_id = #{metalakeId} AND group_name = #{groupName}" | ||
+ " AND deleted_at = 0") | ||
GroupPO selectGroupMetaByMetalakeIdAndName( | ||
@Param("metalakeId") Long metalakeId, @Param("groupName") String name); | ||
|
||
@Insert( | ||
"INSERT INTO " | ||
+ 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}" | ||
+ " )") | ||
void insertGroupMeta(@Param("groupMeta") GroupPO groupPO); | ||
|
||
@Insert( | ||
"INSERT INTO " | ||
+ 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}") | ||
void insertGroupMetaOnDuplicateKeyUpdate(@Param("groupMeta") GroupPO groupPO); | ||
|
||
@Update( | ||
"UPDATE " | ||
+ TABLE_NAME | ||
+ " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0" | ||
+ " WHERE group_id = #{groupId} AND deleted_at = 0") | ||
void softDeleteGroupMetaByGroupId(@Param("groupId") Long groupId); | ||
|
||
@Update( | ||
"UPDATE " | ||
+ TABLE_NAME | ||
+ " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0" | ||
+ " WHERE metalake_id = #{metalakeId} AND deleted_at = 0") | ||
void softDeleteGroupMetasByMetalakeId(@Param("metalakeId") Long metalakeId); | ||
|
||
@Update( | ||
"UPDATE " | ||
+ 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") | ||
Integer updateGroupMeta( | ||
@Param("newGroupMeta") GroupPO newGroupPO, @Param("oldGroupMeta") GroupPO oldGroupPO); | ||
} |
105 changes: 105 additions & 0 deletions
105
.../src/main/java/com/datastrato/gravitino/storage/relational/mapper/GroupRoleRelMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 2024 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
|
||
package com.datastrato.gravitino.storage.relational.mapper; | ||
|
||
import com.datastrato.gravitino.storage.relational.po.GroupRoleRelPO; | ||
import java.util.List; | ||
import org.apache.ibatis.annotations.Insert; | ||
import org.apache.ibatis.annotations.Param; | ||
import org.apache.ibatis.annotations.Update; | ||
|
||
/** | ||
* A MyBatis Mapper for table 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 GroupRoleRelMapper { | ||
String RELATION_TABLE_NAME = "group_role_rel"; | ||
String GROUP_TABLE_NAME = "group_meta"; | ||
|
||
@Insert({ | ||
"<script>", | ||
"INSERT INTO " | ||
+ RELATION_TABLE_NAME | ||
+ "(group_id, role_id," | ||
+ " audit_info," | ||
+ " current_version, last_version, deleted_at)" | ||
+ " VALUES ", | ||
"<foreach collection='groupRoleRels' item='item' separator=','>", | ||
"(#{item.groupId}," | ||
+ " #{item.roleId}," | ||
+ " #{item.auditInfo}," | ||
+ " #{item.currentVersion}," | ||
+ " #{item.lastVersion}," | ||
+ " #{item.deletedAt})", | ||
"</foreach>", | ||
"</script>" | ||
}) | ||
void batchInsertGroupRoleRel(@Param("groupRoleRels") List<GroupRoleRelPO> groupRoleRelPOS); | ||
|
||
@Insert({ | ||
"<script>", | ||
"INSERT INTO " | ||
+ RELATION_TABLE_NAME | ||
+ "(group_id, role_id," | ||
+ " audit_info," | ||
+ " current_version, last_version, deleted_at)" | ||
+ " VALUES ", | ||
"<foreach collection='groupRoleRels' item='item' separator=','>", | ||
"(#{item.groupId}," | ||
+ " #{item.roleId}," | ||
+ " #{item.auditInfo}," | ||
+ " #{item.currentVersion}," | ||
+ " #{item.lastVersion}," | ||
+ " #{item.deletedAt})", | ||
"</foreach>", | ||
" ON DUPLICATE KEY UPDATE" | ||
+ " group_id = VALUES(group_id)," | ||
+ " role_id = VALUES(role_id)," | ||
+ " audit_info = VALUES(audit_info)," | ||
+ " current_version = VALUES(current_version)," | ||
+ " last_version = VALUES(last_version)," | ||
+ " deleted_at = VALUES(deleted_at)", | ||
"</script>" | ||
}) | ||
void batchInsertGroupRoleRelOnDuplicateKeyUpdate( | ||
@Param("groupRoleRels") List<GroupRoleRelPO> groupRoleRelPOS); | ||
|
||
@Update( | ||
"UPDATE " | ||
+ RELATION_TABLE_NAME | ||
+ " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0" | ||
+ " WHERE group_id = #{groupId} AND deleted_at = 0") | ||
void softDeleteGroupRoleRelByGroupId(@Param("groupId") Long groupId); | ||
|
||
@Update({ | ||
"<script>", | ||
"UPDATE " | ||
+ RELATION_TABLE_NAME | ||
+ " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0" | ||
+ " WHERE group_id = #{groupId} AND role_id in (", | ||
"<foreach collection='roleIds' item='roleId' separator=','>", | ||
"#{roleId}", | ||
"</foreach>", | ||
") " + "AND deleted_at = 0", | ||
"</script>" | ||
}) | ||
void softDeleteGroupRoleRelByGroupAndRoles( | ||
@Param("groupId") Long groupId, @Param("roleIds") List<Long> roleIds); | ||
|
||
@Update( | ||
"UPDATE " | ||
+ RELATION_TABLE_NAME | ||
+ " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0" | ||
+ " WHERE group_id IN (SELECT group_id FROM " | ||
+ GROUP_TABLE_NAME | ||
+ " WHERE metalake_id = #{metalakeId} AND deleted_at = 0)" | ||
+ " AND deleted_at = 0") | ||
void softDeleteGroupRoleRelByMetalakeId(Long metalakeId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.