From 4e4da09be7e2b2937eb020e6595a2057e30bc741 Mon Sep 17 00:00:00 2001 From: Qi Yu Date: Fri, 6 Sep 2024 13:48:40 +0800 Subject: [PATCH] [#4868] improvement(core): Modify tag and user related mapper class to 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. --- .../mapper/GroupMetaBaseSQLProvider.java | 146 +++++++++++++ .../relational/mapper/GroupMetaMapper.java | 123 ++--------- .../mapper/GroupMetaSQLProviderFactory.java | 89 ++++++++ .../mapper/GroupRoleRelBaseSQLProvider.java | 125 +++++++++++ .../relational/mapper/GroupRoleRelMapper.java | 105 ++-------- .../GroupRoleRelSQLProviderFactory.java | 83 ++++++++ .../mapper/OwnerMetaBaseSQLProvider.java | 181 ++++++++++++++++ .../relational/mapper/OwnerMetaMapper.java | 161 +++----------- .../mapper/OwnerMetaSQLProviderFactory.java | 97 +++++++++ .../mapper/RoleMetaBaseSQLProvider.java | 160 ++++++++++++++ .../relational/mapper/RoleMetaMapper.java | 136 +++--------- .../mapper/RoleMetaSQLProviderFactory.java | 93 +++++++++ .../SecurableObjectBaseSQLProvider.java | 86 ++++++++ .../mapper/SecurableObjectMapper.java | 69 ++---- .../SecurableObjectSQLProviderFactory.java | 73 +++++++ .../mapper/TagMetaBaseSQLProvider.java | 197 ++++++++++++++++++ .../relational/mapper/TagMetaMapper.java | 167 ++------------- .../mapper/TagMetaSQLProviderFactory.java | 96 +++++++++ .../TagMetadataObjectRelBaseSQLProvider.java | 154 ++++++++++++++ .../mapper/TagMetadataObjectRelMapper.java | 132 +++--------- ...agMetadataObjectRelSQLProviderFactory.java | 99 +++++++++ .../mapper/UserMetaBaseSQLProvider.java | 147 +++++++++++++ .../relational/mapper/UserMetaMapper.java | 123 ++--------- .../mapper/UserMetaSQLProviderFactory.java | 91 ++++++++ .../mapper/UserRoleRelBaseSQLProvider.java | 126 +++++++++++ .../relational/mapper/UserRoleRelMapper.java | 108 ++-------- .../mapper/UserRoleRelSQLProviderFactory.java | 84 ++++++++ .../relational/service/SchemaMetaService.java | 3 +- 28 files changed, 2339 insertions(+), 915 deletions(-) create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupMetaBaseSQLProvider.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupMetaSQLProviderFactory.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupRoleRelBaseSQLProvider.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupRoleRelSQLProviderFactory.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/OwnerMetaBaseSQLProvider.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/OwnerMetaSQLProviderFactory.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/RoleMetaBaseSQLProvider.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/RoleMetaSQLProviderFactory.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/SecurableObjectBaseSQLProvider.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/SecurableObjectSQLProviderFactory.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetaBaseSQLProvider.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetaSQLProviderFactory.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetadataObjectRelBaseSQLProvider.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetadataObjectRelSQLProviderFactory.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserMetaBaseSQLProvider.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserMetaSQLProviderFactory.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserRoleRelBaseSQLProvider.java create mode 100644 core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserRoleRelSQLProviderFactory.java diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupMetaBaseSQLProvider.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupMetaBaseSQLProvider.java new file mode 100644 index 00000000000..686bc42c565 --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupMetaBaseSQLProvider.java @@ -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}"; + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupMetaMapper.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupMetaMapper.java index 6250c2a9a6c..5743095dd72 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupMetaMapper.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupMetaMapper.java @@ -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. @@ -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 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); } diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupMetaSQLProviderFactory.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupMetaSQLProviderFactory.java new file mode 100644 index 00000000000..59e45a6dc6b --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupMetaSQLProviderFactory.java @@ -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 + 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); + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupRoleRelBaseSQLProvider.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupRoleRelBaseSQLProvider.java new file mode 100644 index 00000000000..ea883681570 --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupRoleRelBaseSQLProvider.java @@ -0,0 +1,125 @@ +/* + * 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.GroupRoleRelMapper.GROUP_ROLE_RELATION_TABLE_NAME; +import static org.apache.gravitino.storage.relational.mapper.GroupRoleRelMapper.GROUP_TABLE_NAME; + +import java.util.List; +import org.apache.gravitino.storage.relational.po.GroupRoleRelPO; +import org.apache.ibatis.annotations.Param; + +public class GroupRoleRelBaseSQLProvider { + public String batchInsertGroupRoleRel( + @Param("groupRoleRels") List groupRoleRelPOS) { + return ""; + } + + public String batchInsertGroupRoleRelOnDuplicateKeyUpdate( + @Param("groupRoleRels") List groupRoleRelPOS) { + return ""; + } + + public String softDeleteGroupRoleRelByGroupId(@Param("groupId") Long groupId) { + return "UPDATE " + + GROUP_ROLE_RELATION_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 softDeleteGroupRoleRelByGroupAndRoles( + @Param("groupId") Long groupId, @Param("roleIds") List roleIds) { + return ""; + } + + public String softDeleteGroupRoleRelByMetalakeId(Long metalakeId) { + return "UPDATE " + + GROUP_ROLE_RELATION_TABLE_NAME + + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" + + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" + + " WHERE group_id IN (SELECT group_id FROM " + + GROUP_TABLE_NAME + + " WHERE metalake_id = #{metalakeId} AND deleted_at = 0)" + + " AND deleted_at = 0"; + } + + public String softDeleteGroupRoleRelByRoleId(Long roleId) { + return "UPDATE " + + GROUP_ROLE_RELATION_TABLE_NAME + + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" + + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" + + " WHERE role_id = #{roleId} AND deleted_at = 0"; + } + + public String deleteGroupRoleRelMetasByLegacyTimeline( + @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { + return "DELETE FROM " + + GROUP_ROLE_RELATION_TABLE_NAME + + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}"; + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupRoleRelMapper.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupRoleRelMapper.java index c428341d7a4..a40ee8daae9 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupRoleRelMapper.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupRoleRelMapper.java @@ -21,10 +21,9 @@ import java.util.List; import org.apache.gravitino.storage.relational.po.GroupRoleRelPO; -import org.apache.ibatis.annotations.Delete; -import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.InsertProvider; import org.apache.ibatis.annotations.Param; -import org.apache.ibatis.annotations.Update; +import org.apache.ibatis.annotations.UpdateProvider; /** * A MyBatis Mapper for table meta operation SQLs. @@ -38,101 +37,39 @@ public interface GroupRoleRelMapper { String GROUP_TABLE_NAME = "group_meta"; String GROUP_ROLE_RELATION_TABLE_NAME = "group_role_rel"; - @Insert({ - "" - }) + @InsertProvider(type = GroupRoleRelSQLProviderFactory.class, method = "batchInsertGroupRoleRel") void batchInsertGroupRoleRel(@Param("groupRoleRels") List groupRoleRelPOS); - @Insert({ - "" - }) + @InsertProvider( + type = GroupRoleRelSQLProviderFactory.class, + method = "batchInsertGroupRoleRelOnDuplicateKeyUpdate") void batchInsertGroupRoleRelOnDuplicateKeyUpdate( @Param("groupRoleRels") List groupRoleRelPOS); - @Update( - "UPDATE " - + GROUP_ROLE_RELATION_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 = GroupRoleRelSQLProviderFactory.class, + method = "softDeleteGroupRoleRelByGroupId") void softDeleteGroupRoleRelByGroupId(@Param("groupId") Long groupId); - @Update({ - "" - }) + @UpdateProvider( + type = GroupRoleRelSQLProviderFactory.class, + method = "softDeleteGroupRoleRelByGroupAndRoles") void softDeleteGroupRoleRelByGroupAndRoles( @Param("groupId") Long groupId, @Param("roleIds") List roleIds); - @Update( - "UPDATE " - + GROUP_ROLE_RELATION_TABLE_NAME - + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" - + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" - + " WHERE group_id IN (SELECT group_id FROM " - + GROUP_TABLE_NAME - + " WHERE metalake_id = #{metalakeId} AND deleted_at = 0)" - + " AND deleted_at = 0") + @UpdateProvider( + type = GroupRoleRelSQLProviderFactory.class, + method = "softDeleteGroupRoleRelByMetalakeId") void softDeleteGroupRoleRelByMetalakeId(Long metalakeId); - @Update( - "UPDATE " - + GROUP_ROLE_RELATION_TABLE_NAME - + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" - + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" - + " WHERE role_id = #{roleId} AND deleted_at = 0") + @UpdateProvider( + type = GroupRoleRelSQLProviderFactory.class, + method = "softDeleteGroupRoleRelByRoleId") void softDeleteGroupRoleRelByRoleId(Long roleId); - @Delete( - "DELETE FROM " - + GROUP_ROLE_RELATION_TABLE_NAME - + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}") + @UpdateProvider( + type = GroupRoleRelSQLProviderFactory.class, + method = "deleteGroupRoleRelMetasByLegacyTimeline") Integer deleteGroupRoleRelMetasByLegacyTimeline( @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit); } diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupRoleRelSQLProviderFactory.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupRoleRelSQLProviderFactory.java new file mode 100644 index 00000000000..4fd047abeb1 --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/GroupRoleRelSQLProviderFactory.java @@ -0,0 +1,83 @@ +/* + * 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.List; +import java.util.Map; +import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType; +import org.apache.gravitino.storage.relational.po.GroupRoleRelPO; +import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper; +import org.apache.ibatis.annotations.Param; + +public class GroupRoleRelSQLProviderFactory { + + private static final Map + METALAKE_META_SQL_PROVIDER_MAP = + ImmutableMap.of( + JDBCBackendType.MYSQL, new GroupRoleRelMySQLProvider(), + JDBCBackendType.H2, new GroupRoleRelH2Provider()); + + public static GroupRoleRelBaseSQLProvider getProvider() { + String databaseId = + SqlSessionFactoryHelper.getInstance() + .getSqlSessionFactory() + .getConfiguration() + .getDatabaseId(); + + JDBCBackendType jdbcBackendType = JDBCBackendType.fromString(databaseId); + return METALAKE_META_SQL_PROVIDER_MAP.get(jdbcBackendType); + } + + static class GroupRoleRelMySQLProvider extends GroupRoleRelBaseSQLProvider {} + + static class GroupRoleRelH2Provider extends GroupRoleRelBaseSQLProvider {} + + public static String batchInsertGroupRoleRel( + @Param("groupRoleRels") List groupRoleRelPOS) { + return getProvider().batchInsertGroupRoleRel(groupRoleRelPOS); + } + + public static String batchInsertGroupRoleRelOnDuplicateKeyUpdate( + @Param("groupRoleRels") List groupRoleRelPOS) { + return getProvider().batchInsertGroupRoleRelOnDuplicateKeyUpdate(groupRoleRelPOS); + } + + public static String softDeleteGroupRoleRelByGroupId(@Param("groupId") Long groupId) { + return getProvider().softDeleteGroupRoleRelByGroupId(groupId); + } + + public static String softDeleteGroupRoleRelByGroupAndRoles( + @Param("groupId") Long groupId, @Param("roleIds") List roleIds) { + return getProvider().softDeleteGroupRoleRelByGroupAndRoles(groupId, roleIds); + } + + public static String softDeleteGroupRoleRelByMetalakeId(Long metalakeId) { + return getProvider().softDeleteGroupRoleRelByMetalakeId(metalakeId); + } + + public static String softDeleteGroupRoleRelByRoleId(Long roleId) { + return getProvider().softDeleteGroupRoleRelByRoleId(roleId); + } + + public static String deleteGroupRoleRelMetasByLegacyTimeline( + @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { + return getProvider().deleteGroupRoleRelMetasByLegacyTimeline(legacyTimeline, limit); + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/OwnerMetaBaseSQLProvider.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/OwnerMetaBaseSQLProvider.java new file mode 100644 index 00000000000..0d46034a5db --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/OwnerMetaBaseSQLProvider.java @@ -0,0 +1,181 @@ +/* + * 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.OwnerMetaMapper.OWNER_TABLE_NAME; + +import org.apache.gravitino.storage.relational.po.OwnerRelPO; +import org.apache.ibatis.annotations.Param; + +public class OwnerMetaBaseSQLProvider { + public String selectUserOwnerMetaByMetadataObjectIdAndType( + @Param("metadataObjectId") Long metadataObjectId, + @Param("metadataObjectType") String metadataObjectType) { + return "SELECT ut.user_id as userId," + + " ut.user_name as userName," + + " ut.metalake_id as metalakeId," + + " ut.audit_info as auditInfo," + + " ut.current_version as currentVersion," + + " ut.last_version as lastVersion," + + " ut.deleted_at as deletedAt" + + " FROM " + + OWNER_TABLE_NAME + + " ot JOIN " + + UserMetaMapper.USER_TABLE_NAME + + " ut ON ut.user_id = ot.owner_id" + + " WHERE ot.metadata_object_id = #{metadataObjectId} AND" + + " ot.metadata_object_type = #{metadataObjectType} AND" + + " ot.owner_type = 'USER' AND" + + " ot.deleted_at = 0 AND ut.deleted_at = 0"; + } + + public String selectGroupOwnerMetaByMetadataObjectIdAndType( + @Param("metadataObjectId") Long metadataObjectId, + @Param("metadataObjectType") String metadataObjectType) { + return "SELECT gt.group_id as groupId," + + " gt.group_name as groupName," + + " gt.metalake_id as metalakeId," + + " gt.audit_info as auditInfo," + + " gt.current_version as currentVersion," + + " gt.last_version as lastVersion," + + " gt.deleted_at as deletedAt" + + " FROM " + + OWNER_TABLE_NAME + + " ot JOIN " + + GroupMetaMapper.GROUP_TABLE_NAME + + " gt ON gt.group_id = ot.owner_id" + + " WHERE ot.metadata_object_id = #{metadataObjectId} AND" + + " ot.metadata_object_type = #{metadataObjectType} AND" + + " ot.owner_type = 'GROUP' AND" + + " ot.deleted_at = 0 AND gt.deleted_at = 0"; + } + + public String insertOwnerRel(@Param("ownerRelPO") OwnerRelPO ownerRelPO) { + return "INSERT INTO " + + OWNER_TABLE_NAME + + "(metalake_id, metadata_object_id, metadata_object_type, owner_id, owner_type," + + " audit_info, current_version, last_version, deleted_at)" + + " VALUES (" + + " #{ownerRelPO.metalakeId}," + + " #{ownerRelPO.metadataObjectId}," + + " #{ownerRelPO.metadataObjectType}," + + " #{ownerRelPO.ownerId}," + + " #{ownerRelPO.ownerType}," + + " #{ownerRelPO.auditInfo}," + + " #{ownerRelPO.currentVersion}," + + " #{ownerRelPO.lastVersion}," + + " #{ownerRelPO.deletedAt}" + + ")"; + } + + public String softDeleteOwnerRelByMetadataObjectIdAndType( + @Param("metadataObjectId") Long metadataObjectId, + @Param("metadataObjectType") String metadataObjectType) { + return "UPDATE " + + OWNER_TABLE_NAME + + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" + + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" + + " WHERE metadata_object_id = #{metadataObjectId} AND metadata_object_type = #{metadataObjectType} AND deleted_at = 0"; + } + + public String softDeleteOwnerRelByOwnerIdAndType( + @Param("ownerId") Long ownerId, @Param("ownerType") String ownerType) { + return "UPDATE " + + OWNER_TABLE_NAME + + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" + + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" + + " WHERE owner_id = #{ownerId} AND owner_type = #{ownerType} AND deleted_at = 0"; + } + + public String softDeleteOwnerRelByMetalakeId(@Param("metalakeId") Long metalakeId) { + return "UPDATE " + + OWNER_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 softDeleteOwnerRelByCatalogId(@Param("catalogId") Long catalogId) { + return "UPDATE " + + OWNER_TABLE_NAME + + " ot SET ot.deleted_at = (UNIX_TIMESTAMP() * 1000.0)" + + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" + + " WHERE EXISTS (" + + " SELECT ct.catalog_id FROM " + + CatalogMetaMapper.TABLE_NAME + + " ct WHERE ct.catalog_id = #{catalogId} AND ct.deleted_at = 0 AND ot.deleted_at = 0 AND " + + "ct.catalog_id = ot.metadata_object_id AND ot.metadata_object_type = 'CATALOG'" + + " UNION " + + " SELECT st.catalog_id FROM " + + SchemaMetaMapper.TABLE_NAME + + " st WHERE st.catalog_id = #{catalogId} AND st.deleted_at = 0 AND ot.deleted_at = 0 AND " + + "st.schema_id = ot.metadata_object_id AND ot.metadata_object_type = 'SCHEMA'" + + " UNION " + + " SELECT tt.catalog_id FROM " + + TopicMetaMapper.TABLE_NAME + + " tt WHERE tt.catalog_id = #{catalogId} AND tt.deleted_at = 0 AND ot.deleted_at = 0 AND " + + "tt.topic_id = ot.metadata_object_id AND ot.metadata_object_type = 'TOPIC'" + + " UNION " + + " SELECT tat.catalog_id FROM " + + TableMetaMapper.TABLE_NAME + + " tat WHERE tat.catalog_id = #{catalogId} AND tat.deleted_at = 0 AND ot.deleted_at = 0 AND " + + "tat.table_id = ot.metadata_object_id AND ot.metadata_object_type = 'TABLE'" + + " UNION " + + " SELECT ft.catalog_id FROM " + + FilesetMetaMapper.META_TABLE_NAME + + " ft WHERE ft.catalog_id = #{catalogId} AND ft.deleted_at = 0 AND ot.deleted_at = 0 AND" + + " ft.fileset_id = ot.metadata_object_id AND ot.metadata_object_type = 'FILESET'" + + ")"; + } + + public String sotDeleteOwnerRelBySchemaId(@Param("schemaId") Long schemaId) { + return "UPDATE " + + OWNER_TABLE_NAME + + " ot SET ot.deleted_at = (UNIX_TIMESTAMP() * 1000.0)" + + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" + + " WHERE EXISTS (" + + " SELECT st.schema_id FROM " + + SchemaMetaMapper.TABLE_NAME + + " st WHERE st.schema_id = #{schemaId} AND st.deleted_at = 0 AND ot.deleted_at = 0 " + + "AND st.schema_id = ot.metadata_object_id AND ot.metadata_object_type = 'SCHEMA'" + + " UNION " + + " SELECT tt.schema_id FROM " + + TopicMetaMapper.TABLE_NAME + + " tt WHERE tt.schema_id = #{schemaId} AND tt.deleted_at = 0 AND ot.deleted_at = 0 AND " + + "tt.topic_id = ot.metadata_object_id AND ot.metadata_object_type = 'TOPIC'" + + " UNION " + + " SELECT tat.schema_id FROM " + + TableMetaMapper.TABLE_NAME + + " tat WHERE tat.schema_id = #{schemaId} AND tat.deleted_at = 0 AND ot.deleted_at = 0 AND " + + "tat.table_id = ot.metadata_object_id AND ot.metadata_object_type = 'TABLE'" + + " UNION " + + " SELECT ft.schema_id FROM " + + FilesetMetaMapper.META_TABLE_NAME + + " ft WHERE ft.schema_id = #{schemaId} AND ft.deleted_at = 0 AND ot.deleted_at = 0 AND " + + "ft.fileset_id = ot.metadata_object_id AND ot.metadata_object_type = 'FILESET'" + + ")"; + } + + public String deleteOwnerMetasByLegacyTimeline( + @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { + return "DELETE FROM " + + OWNER_TABLE_NAME + + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}"; + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/OwnerMetaMapper.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/OwnerMetaMapper.java index 11c288324bf..455d78fb598 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/OwnerMetaMapper.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/OwnerMetaMapper.java @@ -21,11 +21,10 @@ import org.apache.gravitino.storage.relational.po.GroupPO; import org.apache.gravitino.storage.relational.po.OwnerRelPO; import org.apache.gravitino.storage.relational.po.UserPO; -import org.apache.ibatis.annotations.Delete; -import org.apache.ibatis.annotations.Insert; +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 owner meta operation SQLs. @@ -39,158 +38,52 @@ public interface OwnerMetaMapper { String OWNER_TABLE_NAME = "owner_meta"; - @Select( - "SELECT ut.user_id as userId," - + " ut.user_name as userName," - + " ut.metalake_id as metalakeId," - + " ut.audit_info as auditInfo," - + " ut.current_version as currentVersion," - + " ut.last_version as lastVersion," - + " ut.deleted_at as deletedAt" - + " FROM " - + OWNER_TABLE_NAME - + " ot JOIN " - + UserMetaMapper.USER_TABLE_NAME - + " ut ON ut.user_id = ot.owner_id" - + " WHERE ot.metadata_object_id = #{metadataObjectId} AND" - + " ot.metadata_object_type = #{metadataObjectType} AND" - + " ot.owner_type = 'USER' AND" - + " ot.deleted_at = 0 AND ut.deleted_at = 0") + @SelectProvider( + type = OwnerMetaSQLProviderFactory.class, + method = "selectUserOwnerMetaByMetadataObjectIdAndType") UserPO selectUserOwnerMetaByMetadataObjectIdAndType( @Param("metadataObjectId") Long metadataObjectId, @Param("metadataObjectType") String metadataObjectType); - @Select( - "SELECT gt.group_id as groupId," - + " gt.group_name as groupName," - + " gt.metalake_id as metalakeId," - + " gt.audit_info as auditInfo," - + " gt.current_version as currentVersion," - + " gt.last_version as lastVersion," - + " gt.deleted_at as deletedAt" - + " FROM " - + OWNER_TABLE_NAME - + " ot JOIN " - + GroupMetaMapper.GROUP_TABLE_NAME - + " gt ON gt.group_id = ot.owner_id" - + " WHERE ot.metadata_object_id = #{metadataObjectId} AND" - + " ot.metadata_object_type = #{metadataObjectType} AND" - + " ot.owner_type = 'GROUP' AND" - + " ot.deleted_at = 0 AND gt.deleted_at = 0") + @SelectProvider( + type = OwnerMetaSQLProviderFactory.class, + method = "selectGroupOwnerMetaByMetadataObjectIdAndType") GroupPO selectGroupOwnerMetaByMetadataObjectIdAndType( @Param("metadataObjectId") Long metadataObjectId, @Param("metadataObjectType") String metadataObjectType); - @Insert( - "INSERT INTO " - + OWNER_TABLE_NAME - + "(metalake_id, metadata_object_id, metadata_object_type, owner_id, owner_type," - + " audit_info, current_version, last_version, deleted_at)" - + " VALUES (" - + " #{ownerRelPO.metalakeId}," - + " #{ownerRelPO.metadataObjectId}," - + " #{ownerRelPO.metadataObjectType}," - + " #{ownerRelPO.ownerId}," - + " #{ownerRelPO.ownerType}," - + " #{ownerRelPO.auditInfo}," - + " #{ownerRelPO.currentVersion}," - + " #{ownerRelPO.lastVersion}," - + " #{ownerRelPO.deletedAt}" - + ")") + @InsertProvider(type = OwnerMetaSQLProviderFactory.class, method = "insertOwnerRel") void insertOwnerRel(@Param("ownerRelPO") OwnerRelPO ownerRelPO); - @Update( - "UPDATE " - + OWNER_TABLE_NAME - + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" - + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" - + " WHERE metadata_object_id = #{metadataObjectId} AND metadata_object_type = #{metadataObjectType} AND deleted_at = 0") + @UpdateProvider( + type = OwnerMetaSQLProviderFactory.class, + method = "softDeleteOwnerRelByMetadataObjectIdAndType") void softDeleteOwnerRelByMetadataObjectIdAndType( @Param("metadataObjectId") Long metadataObjectId, @Param("metadataObjectType") String metadataObjectType); - @Update( - "UPDATE " - + OWNER_TABLE_NAME - + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" - + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" - + " WHERE owner_id = #{ownerId} AND owner_type = #{ownerType} AND deleted_at = 0") + @UpdateProvider( + type = OwnerMetaSQLProviderFactory.class, + method = "softDeleteOwnerRelByOwnerIdAndType") void softDeleteOwnerRelByOwnerIdAndType( @Param("ownerId") Long ownerId, @Param("ownerType") String ownerType); - @Update( - "UPDATE " - + OWNER_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 = OwnerMetaSQLProviderFactory.class, + method = "softDeleteOwnerRelByMetalakeId") void softDeleteOwnerRelByMetalakeId(@Param("metalakeId") Long metalakeId); - @Update( - "UPDATE " - + OWNER_TABLE_NAME - + " ot SET ot.deleted_at = (UNIX_TIMESTAMP() * 1000.0)" - + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" - + " WHERE EXISTS (" - + " SELECT ct.catalog_id FROM " - + CatalogMetaMapper.TABLE_NAME - + " ct WHERE ct.catalog_id = #{catalogId} AND ct.deleted_at = 0 AND ot.deleted_at = 0 AND " - + "ct.catalog_id = ot.metadata_object_id AND ot.metadata_object_type = 'CATALOG'" - + " UNION " - + " SELECT st.catalog_id FROM " - + SchemaMetaMapper.TABLE_NAME - + " st WHERE st.catalog_id = #{catalogId} AND st.deleted_at = 0 AND ot.deleted_at = 0 AND " - + "st.schema_id = ot.metadata_object_id AND ot.metadata_object_type = 'SCHEMA'" - + " UNION " - + " SELECT tt.catalog_id FROM " - + TopicMetaMapper.TABLE_NAME - + " tt WHERE tt.catalog_id = #{catalogId} AND tt.deleted_at = 0 AND ot.deleted_at = 0 AND " - + "tt.topic_id = ot.metadata_object_id AND ot.metadata_object_type = 'TOPIC'" - + " UNION " - + " SELECT tat.catalog_id FROM " - + TableMetaMapper.TABLE_NAME - + " tat WHERE tat.catalog_id = #{catalogId} AND tat.deleted_at = 0 AND ot.deleted_at = 0 AND " - + "tat.table_id = ot.metadata_object_id AND ot.metadata_object_type = 'TABLE'" - + " UNION " - + " SELECT ft.catalog_id FROM " - + FilesetMetaMapper.META_TABLE_NAME - + " ft WHERE ft.catalog_id = #{catalogId} AND ft.deleted_at = 0 AND ot.deleted_at = 0 AND" - + " ft.fileset_id = ot.metadata_object_id AND ot.metadata_object_type = 'FILESET'" - + ")") + @UpdateProvider( + type = OwnerMetaSQLProviderFactory.class, + method = "softDeleteOwnerRelByCatalogId") void softDeleteOwnerRelByCatalogId(@Param("catalogId") Long catalogId); - @Update( - "UPDATE " - + OWNER_TABLE_NAME - + " ot SET ot.deleted_at = (UNIX_TIMESTAMP() * 1000.0)" - + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" - + " WHERE EXISTS (" - + " SELECT st.schema_id FROM " - + SchemaMetaMapper.TABLE_NAME - + " st WHERE st.schema_id = #{schemaId} AND st.deleted_at = 0 AND ot.deleted_at = 0 " - + "AND st.schema_id = ot.metadata_object_id AND ot.metadata_object_type = 'SCHEMA'" - + " UNION " - + " SELECT tt.schema_id FROM " - + TopicMetaMapper.TABLE_NAME - + " tt WHERE tt.schema_id = #{schemaId} AND tt.deleted_at = 0 AND ot.deleted_at = 0 AND " - + "tt.topic_id = ot.metadata_object_id AND ot.metadata_object_type = 'TOPIC'" - + " UNION " - + " SELECT tat.schema_id FROM " - + TableMetaMapper.TABLE_NAME - + " tat WHERE tat.schema_id = #{schemaId} AND tat.deleted_at = 0 AND ot.deleted_at = 0 AND " - + "tat.table_id = ot.metadata_object_id AND ot.metadata_object_type = 'TABLE'" - + " UNION " - + " SELECT ft.schema_id FROM " - + FilesetMetaMapper.META_TABLE_NAME - + " ft WHERE ft.schema_id = #{schemaId} AND ft.deleted_at = 0 AND ot.deleted_at = 0 AND " - + "ft.fileset_id = ot.metadata_object_id AND ot.metadata_object_type = 'FILESET'" - + ")") - void sotDeleteOwnerRelBySchemaId(@Param("schemaId") Long schemaId); + @UpdateProvider(type = OwnerMetaSQLProviderFactory.class, method = "softDeleteOwnerRelBySchemaId") + void softDeleteOwnerRelBySchemaId(@Param("schemaId") Long schemaId); - @Delete( - "DELETE FROM " - + OWNER_TABLE_NAME - + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}") + @UpdateProvider( + type = OwnerMetaSQLProviderFactory.class, + method = "deleteOwnerMetasByLegacyTimeline") Integer deleteOwnerMetasByLegacyTimeline( @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit); } diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/OwnerMetaSQLProviderFactory.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/OwnerMetaSQLProviderFactory.java new file mode 100644 index 00000000000..e07fd269b77 --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/OwnerMetaSQLProviderFactory.java @@ -0,0 +1,97 @@ +/* + * 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.OwnerRelPO; +import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper; +import org.apache.ibatis.annotations.Param; + +public class OwnerMetaSQLProviderFactory { + + private static final Map + METALAKE_META_SQL_PROVIDER_MAP = + ImmutableMap.of( + JDBCBackendType.MYSQL, new OwnerMetaMySQLProvider(), + JDBCBackendType.H2, new OwnerMetaH2Provider()); + + public static OwnerMetaBaseSQLProvider getProvider() { + String databaseId = + SqlSessionFactoryHelper.getInstance() + .getSqlSessionFactory() + .getConfiguration() + .getDatabaseId(); + + JDBCBackendType jdbcBackendType = JDBCBackendType.fromString(databaseId); + return METALAKE_META_SQL_PROVIDER_MAP.get(jdbcBackendType); + } + + static class OwnerMetaMySQLProvider extends OwnerMetaBaseSQLProvider {} + + static class OwnerMetaH2Provider extends OwnerMetaBaseSQLProvider {} + + public static String selectUserOwnerMetaByMetadataObjectIdAndType( + @Param("metadataObjectId") Long metadataObjectId, + @Param("metadataObjectType") String metadataObjectType) { + return getProvider() + .selectUserOwnerMetaByMetadataObjectIdAndType(metadataObjectId, metadataObjectType); + } + + public static String selectGroupOwnerMetaByMetadataObjectIdAndType( + @Param("metadataObjectId") Long metadataObjectId, + @Param("metadataObjectType") String metadataObjectType) { + return getProvider() + .selectGroupOwnerMetaByMetadataObjectIdAndType(metadataObjectId, metadataObjectType); + } + + public static String insertOwnerRel(@Param("ownerRelPO") OwnerRelPO ownerRelPO) { + return getProvider().insertOwnerRel(ownerRelPO); + } + + public static String softDeleteOwnerRelByMetadataObjectIdAndType( + @Param("metadataObjectId") Long metadataObjectId, + @Param("metadataObjectType") String metadataObjectType) { + return getProvider() + .softDeleteOwnerRelByMetadataObjectIdAndType(metadataObjectId, metadataObjectType); + } + + public static String softDeleteOwnerRelByOwnerIdAndType( + @Param("ownerId") Long ownerId, @Param("ownerType") String ownerType) { + return getProvider().softDeleteOwnerRelByOwnerIdAndType(ownerId, ownerType); + } + + public static String softDeleteOwnerRelByMetalakeId(@Param("metalakeId") Long metalakeId) { + return getProvider().softDeleteOwnerRelByMetalakeId(metalakeId); + } + + public static String softDeleteOwnerRelByCatalogId(@Param("catalogId") Long catalogId) { + return getProvider().softDeleteOwnerRelByCatalogId(catalogId); + } + + public static String softDeleteOwnerRelBySchemaId(@Param("schemaId") Long schemaId) { + return getProvider().sotDeleteOwnerRelBySchemaId(schemaId); + } + + public static String deleteOwnerMetasByLegacyTimeline( + @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { + return getProvider().deleteOwnerMetasByLegacyTimeline(legacyTimeline, limit); + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/RoleMetaBaseSQLProvider.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/RoleMetaBaseSQLProvider.java new file mode 100644 index 00000000000..dad878842a3 --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/RoleMetaBaseSQLProvider.java @@ -0,0 +1,160 @@ +/* + * 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.RoleMetaMapper.GROUP_ROLE_RELATION_TABLE_NAME; +import static org.apache.gravitino.storage.relational.mapper.RoleMetaMapper.ROLE_TABLE_NAME; +import static org.apache.gravitino.storage.relational.mapper.RoleMetaMapper.USER_ROLE_RELATION_TABLE_NAME; + +import org.apache.gravitino.storage.relational.po.RolePO; +import org.apache.ibatis.annotations.Param; + +public class RoleMetaBaseSQLProvider { + + public String selectRoleMetaByMetalakeIdAndName( + @Param("metalakeId") Long metalakeId, @Param("roleName") String roleName) { + return "SELECT role_id as roleId, role_name as roleName," + + " metalake_id as metalakeId, properties as properties," + + " audit_info as auditInfo, current_version as currentVersion," + + " last_version as lastVersion, deleted_at as deletedAt" + + " FROM " + + ROLE_TABLE_NAME + + " WHERE metalake_id = #{metalakeId} AND role_name = #{roleName}" + + " AND deleted_at = 0"; + } + + public String selectRoleIdByMetalakeIdAndName( + @Param("metalakeId") Long metalakeId, @Param("roleName") String name) { + return "SELECT role_id as roleId FROM " + + ROLE_TABLE_NAME + + " WHERE metalake_id = #{metalakeId} AND role_name = #{roleName}" + + " AND deleted_at = 0"; + } + + public String listRolesByUserId(@Param("userId") Long userId) { + return "SELECT ro.role_id as roleId, ro.role_name as roleName," + + " ro.metalake_id as metalakeId, ro.properties as properties," + + " ro.audit_info as auditInfo, ro.current_version as currentVersion," + + " ro.last_version as lastVersion, ro.deleted_at as deletedAt" + + " FROM " + + ROLE_TABLE_NAME + + " ro JOIN " + + USER_ROLE_RELATION_TABLE_NAME + + " re ON ro.role_id = re.role_id" + + " WHERE re.user_id = #{userId}" + + " AND ro.deleted_at = 0 AND re.deleted_at = 0"; + } + + public String listRolesByGroupId(Long groupId) { + return "SELECT ro.role_id as roleId, ro.role_name as roleName," + + " ro.metalake_id as metalakeId, ro.properties as properties," + + " ro.audit_info as auditInfo, ro.current_version as currentVersion," + + " ro.last_version as lastVersion, ro.deleted_at as deletedAt" + + " FROM " + + ROLE_TABLE_NAME + + " ro JOIN " + + GROUP_ROLE_RELATION_TABLE_NAME + + " ge ON ro.role_id = ge.role_id" + + " WHERE ge.group_id = #{groupId}" + + " AND ro.deleted_at = 0 AND ge.deleted_at = 0"; + } + + public String listRolesByMetadataObjectIdAndType( + @Param("metadataObjectId") Long metadataObjectId, + @Param("metadataObjectType") String metadataObjectType) { + return "SELECT DISTINCT ro.role_id as roleId, ro.role_name as roleName," + + " ro.metalake_id as metalakeId, ro.properties as properties," + + " ro.audit_info as auditInfo, ro.current_version as currentVersion," + + " ro.last_version as lastVersion, ro.deleted_at as deletedAt" + + " FROM " + + ROLE_TABLE_NAME + + " ro JOIN " + + SecurableObjectMapper.SECURABLE_OBJECT_TABLE_NAME + + " se ON ro.role_id = se.role_id" + + " WHERE se.metadata_object_id = #{metadataObjectId}" + + " AND se.type = #{metadataObjectType}" + + " AND ro.deleted_at = 0 AND se.deleted_at = 0"; + } + + public String insertRoleMeta(@Param("roleMeta") RolePO rolePO) { + return "INSERT INTO " + + ROLE_TABLE_NAME + + "(role_id, role_name," + + " metalake_id, properties," + + " audit_info, current_version, last_version, deleted_at)" + + " VALUES(" + + " #{roleMeta.roleId}," + + " #{roleMeta.roleName}," + + " #{roleMeta.metalakeId}," + + " #{roleMeta.properties}," + + " #{roleMeta.auditInfo}," + + " #{roleMeta.currentVersion}," + + " #{roleMeta.lastVersion}," + + " #{roleMeta.deletedAt}" + + " )"; + } + + public String insertRoleMetaOnDuplicateKeyUpdate(@Param("roleMeta") RolePO rolePO) { + return "INSERT INTO " + + ROLE_TABLE_NAME + + "(role_id, role_name," + + " metalake_id, properties," + + " audit_info, current_version, last_version, deleted_at)" + + " VALUES(" + + " #{roleMeta.roleId}," + + " #{roleMeta.roleName}," + + " #{roleMeta.metalakeId}," + + " #{roleMeta.properties}," + + " #{roleMeta.auditInfo}," + + " #{roleMeta.currentVersion}," + + " #{roleMeta.lastVersion}," + + " #{roleMeta.deletedAt}" + + " ) ON DUPLICATE KEY UPDATE" + + " role_name = #{roleMeta.roleName}," + + " metalake_id = #{roleMeta.metalakeId}," + + " properties = #{roleMeta.properties}," + + " audit_info = #{roleMeta.auditInfo}," + + " current_version = #{roleMeta.currentVersion}," + + " last_version = #{roleMeta.lastVersion}," + + " deleted_at = #{roleMeta.deletedAt}"; + } + + public String softDeleteRoleMetaByRoleId(Long roleId) { + return "UPDATE " + + ROLE_TABLE_NAME + + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" + + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" + + " WHERE role_id = #{roleId} AND deleted_at = 0"; + } + + public String softDeleteRoleMetasByMetalakeId(@Param("metalakeId") Long metalakeId) { + return "UPDATE " + + ROLE_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 deleteRoleMetasByLegacyTimeline( + @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { + return "DELETE FROM " + + ROLE_TABLE_NAME + + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}"; + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/RoleMetaMapper.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/RoleMetaMapper.java index 2c9190d69d6..d7478c3b7f4 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/RoleMetaMapper.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/RoleMetaMapper.java @@ -21,11 +21,11 @@ import java.util.List; import org.apache.gravitino.storage.relational.po.RolePO; -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. @@ -40,134 +40,50 @@ public interface RoleMetaMapper { String USER_ROLE_RELATION_TABLE_NAME = "user_role_rel"; String GROUP_ROLE_RELATION_TABLE_NAME = "group_role_rel"; - @Select( - "SELECT role_id as roleId, role_name as roleName," - + " metalake_id as metalakeId, properties as properties," - + " audit_info as auditInfo, current_version as currentVersion," - + " last_version as lastVersion, deleted_at as deletedAt" - + " FROM " - + ROLE_TABLE_NAME - + " WHERE metalake_id = #{metalakeId} AND role_name = #{roleName}" - + " AND deleted_at = 0") + @SelectProvider( + type = RoleMetaSQLProviderFactory.class, + method = "selectRoleMetaByMetalakeIdAndName") RolePO selectRoleMetaByMetalakeIdAndName( @Param("metalakeId") Long metalakeId, @Param("roleName") String roleName); - @Select( - "SELECT role_id as roleId FROM " - + ROLE_TABLE_NAME - + " WHERE metalake_id = #{metalakeId} AND role_name = #{roleName}" - + " AND deleted_at = 0") + @SelectProvider( + type = RoleMetaSQLProviderFactory.class, + method = "selectRoleIdByMetalakeIdAndName") Long selectRoleIdByMetalakeIdAndName( @Param("metalakeId") Long metalakeId, @Param("roleName") String name); - @Select( - "SELECT ro.role_id as roleId, ro.role_name as roleName," - + " ro.metalake_id as metalakeId, ro.properties as properties," - + " ro.audit_info as auditInfo, ro.current_version as currentVersion," - + " ro.last_version as lastVersion, ro.deleted_at as deletedAt" - + " FROM " - + ROLE_TABLE_NAME - + " ro JOIN " - + USER_ROLE_RELATION_TABLE_NAME - + " re ON ro.role_id = re.role_id" - + " WHERE re.user_id = #{userId}" - + " AND ro.deleted_at = 0 AND re.deleted_at = 0") + @SelectProvider(type = RoleMetaSQLProviderFactory.class, method = "listRolesByUserId") List listRolesByUserId(@Param("userId") Long userId); - @Select( - "SELECT ro.role_id as roleId, ro.role_name as roleName," - + " ro.metalake_id as metalakeId, ro.properties as properties," - + " ro.audit_info as auditInfo, ro.current_version as currentVersion," - + " ro.last_version as lastVersion, ro.deleted_at as deletedAt" - + " FROM " - + ROLE_TABLE_NAME - + " ro JOIN " - + GROUP_ROLE_RELATION_TABLE_NAME - + " ge ON ro.role_id = ge.role_id" - + " WHERE ge.group_id = #{groupId}" - + " AND ro.deleted_at = 0 AND ge.deleted_at = 0") + @SelectProvider(type = RoleMetaSQLProviderFactory.class, method = "listRolesByGroupId") List listRolesByGroupId(Long groupId); - @Select( - "SELECT DISTINCT ro.role_id as roleId, ro.role_name as roleName," - + " ro.metalake_id as metalakeId, ro.properties as properties," - + " ro.audit_info as auditInfo, ro.current_version as currentVersion," - + " ro.last_version as lastVersion, ro.deleted_at as deletedAt" - + " FROM " - + ROLE_TABLE_NAME - + " ro JOIN " - + SecurableObjectMapper.SECURABLE_OBJECT_TABLE_NAME - + " se ON ro.role_id = se.role_id" - + " WHERE se.metadata_object_id = #{metadataObjectId}" - + " AND se.type = #{metadataObjectType}" - + " AND ro.deleted_at = 0 AND se.deleted_at = 0") + @SelectProvider( + type = RoleMetaSQLProviderFactory.class, + method = "listRolesByMetadataObjectIdAndType") List listRolesByMetadataObjectIdAndType( @Param("metadataObjectId") Long metadataObjectId, @Param("metadataObjectType") String metadataObjectType); - @Insert( - "INSERT INTO " - + ROLE_TABLE_NAME - + "(role_id, role_name," - + " metalake_id, properties," - + " audit_info, current_version, last_version, deleted_at)" - + " VALUES(" - + " #{roleMeta.roleId}," - + " #{roleMeta.roleName}," - + " #{roleMeta.metalakeId}," - + " #{roleMeta.properties}," - + " #{roleMeta.auditInfo}," - + " #{roleMeta.currentVersion}," - + " #{roleMeta.lastVersion}," - + " #{roleMeta.deletedAt}" - + " )") + @InsertProvider(type = RoleMetaSQLProviderFactory.class, method = "insertRoleMeta") void insertRoleMeta(@Param("roleMeta") RolePO rolePO); - @Insert( - "INSERT INTO " - + ROLE_TABLE_NAME - + "(role_id, role_name," - + " metalake_id, properties," - + " audit_info, current_version, last_version, deleted_at)" - + " VALUES(" - + " #{roleMeta.roleId}," - + " #{roleMeta.roleName}," - + " #{roleMeta.metalakeId}," - + " #{roleMeta.properties}," - + " #{roleMeta.auditInfo}," - + " #{roleMeta.currentVersion}," - + " #{roleMeta.lastVersion}," - + " #{roleMeta.deletedAt}" - + " ) ON DUPLICATE KEY UPDATE" - + " role_name = #{roleMeta.roleName}," - + " metalake_id = #{roleMeta.metalakeId}," - + " properties = #{roleMeta.properties}," - + " audit_info = #{roleMeta.auditInfo}," - + " current_version = #{roleMeta.currentVersion}," - + " last_version = #{roleMeta.lastVersion}," - + " deleted_at = #{roleMeta.deletedAt}") + @InsertProvider( + type = RoleMetaSQLProviderFactory.class, + method = "insertRoleMetaOnDuplicateKeyUpdate") void insertRoleMetaOnDuplicateKeyUpdate(@Param("roleMeta") RolePO rolePO); - @Update( - "UPDATE " - + ROLE_TABLE_NAME - + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" - + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" - + " WHERE role_id = #{roleId} AND deleted_at = 0") + @UpdateProvider(type = RoleMetaSQLProviderFactory.class, method = "softDeleteRoleMetaByRoleId") void softDeleteRoleMetaByRoleId(Long roleId); - @Update( - "UPDATE " - + ROLE_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 = RoleMetaSQLProviderFactory.class, + method = "softDeleteRoleMetasByMetalakeId") void softDeleteRoleMetasByMetalakeId(@Param("metalakeId") Long metalakeId); - @Delete( - "DELETE FROM " - + ROLE_TABLE_NAME - + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}") + @DeleteProvider( + type = RoleMetaSQLProviderFactory.class, + method = "deleteRoleMetasByLegacyTimeline") Integer deleteRoleMetasByLegacyTimeline( @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit); } diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/RoleMetaSQLProviderFactory.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/RoleMetaSQLProviderFactory.java new file mode 100644 index 00000000000..bdcb45749da --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/RoleMetaSQLProviderFactory.java @@ -0,0 +1,93 @@ +/* + * 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.RolePO; +import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper; +import org.apache.ibatis.annotations.Param; + +public class RoleMetaSQLProviderFactory { + private static final Map + METALAKE_META_SQL_PROVIDER_MAP = + ImmutableMap.of( + JDBCBackendType.MYSQL, new RoleMetaMySQLProvider(), + JDBCBackendType.H2, new RoleMetaH2Provider()); + + public static RoleMetaBaseSQLProvider getProvider() { + String databaseId = + SqlSessionFactoryHelper.getInstance() + .getSqlSessionFactory() + .getConfiguration() + .getDatabaseId(); + + JDBCBackendType jdbcBackendType = JDBCBackendType.fromString(databaseId); + return METALAKE_META_SQL_PROVIDER_MAP.get(jdbcBackendType); + } + + static class RoleMetaMySQLProvider extends RoleMetaBaseSQLProvider {} + + static class RoleMetaH2Provider extends RoleMetaBaseSQLProvider {} + + public static String selectRoleMetaByMetalakeIdAndName( + @Param("metalakeId") Long metalakeId, @Param("roleName") String roleName) { + return getProvider().selectRoleMetaByMetalakeIdAndName(metalakeId, roleName); + } + + public static String selectRoleIdByMetalakeIdAndName( + @Param("metalakeId") Long metalakeId, @Param("roleName") String name) { + return getProvider().selectRoleIdByMetalakeIdAndName(metalakeId, name); + } + + public static String listRolesByUserId(@Param("userId") Long userId) { + return getProvider().listRolesByUserId(userId); + } + + public static String listRolesByGroupId(Long groupId) { + return getProvider().listRolesByGroupId(groupId); + } + + public static String listRolesByMetadataObjectIdAndType( + @Param("metadataObjectId") Long metadataObjectId, @Param("metadataObjectType") String type) { + return getProvider().listRolesByMetadataObjectIdAndType(metadataObjectId, type); + } + + public static String insertRoleMeta(@Param("roleMeta") RolePO rolePO) { + return getProvider().insertRoleMeta(rolePO); + } + + public static String insertRoleMetaOnDuplicateKeyUpdate(@Param("roleMeta") RolePO rolePO) { + return getProvider().insertRoleMetaOnDuplicateKeyUpdate(rolePO); + } + + public static String softDeleteRoleMetaByRoleId(Long roleId) { + return getProvider().softDeleteRoleMetaByRoleId(roleId); + } + + public static String softDeleteRoleMetasByMetalakeId(@Param("metalakeId") Long metalakeId) { + return getProvider().softDeleteRoleMetasByMetalakeId(metalakeId); + } + + public static String deleteRoleMetasByLegacyTimeline( + @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { + return getProvider().deleteRoleMetasByLegacyTimeline(legacyTimeline, limit); + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SecurableObjectBaseSQLProvider.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SecurableObjectBaseSQLProvider.java new file mode 100644 index 00000000000..7e0a3674642 --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SecurableObjectBaseSQLProvider.java @@ -0,0 +1,86 @@ +/* + * 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.SecurableObjectMapper.ROLE_TABLE_NAME; +import static org.apache.gravitino.storage.relational.mapper.SecurableObjectMapper.SECURABLE_OBJECT_TABLE_NAME; + +import java.util.List; +import org.apache.gravitino.storage.relational.po.SecurableObjectPO; +import org.apache.ibatis.annotations.Param; + +public class SecurableObjectBaseSQLProvider { + + public String batchInsertSecurableObjects( + @Param("securableObjects") List securableObjectPOs) { + return ""; + } + + public String softDeleteSecurableObjectsByRoleId(@Param("roleId") Long roleId) { + return "UPDATE " + + SECURABLE_OBJECT_TABLE_NAME + + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" + + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" + + " WHERE role_id = #{roleId} AND deleted_at = 0"; + } + + public String softDeleteRoleMetasByMetalakeId(@Param("metalakeId") Long metalakeId) { + return "UPDATE " + + SECURABLE_OBJECT_TABLE_NAME + + " ob SET ob.deleted_at = (UNIX_TIMESTAMP() * 1000.0)" + + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" + + " WHERE exists (SELECT * from " + + ROLE_TABLE_NAME + + " ro WHERE ro.metalake_id = #{metalakeId} AND ro.role_id = ob.role_id" + + " AND ro.deleted_at = 0) AND ob.deleted_at = 0"; + } + + public String listSecurableObjectsByRoleId(@Param("roleId") Long roleId) { + return "SELECT role_id as roleId, metadata_object_id as metadataObjectId," + + " type as type, privilege_names as privilegeNames," + + " privilege_conditions as privilegeConditions, current_version as currentVersion," + + " last_version as lastVersion, deleted_at as deletedAt" + + " FROM " + + SECURABLE_OBJECT_TABLE_NAME + + " WHERE role_id = #{roleId} AND deleted_at = 0"; + } + + public String deleteSecurableObjectsByLegacyTimeline( + @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { + return "DELETE FROM " + + SECURABLE_OBJECT_TABLE_NAME + + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}"; + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SecurableObjectMapper.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SecurableObjectMapper.java index 9a8ae52c309..e5fa90cc914 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SecurableObjectMapper.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SecurableObjectMapper.java @@ -20,11 +20,11 @@ import java.util.List; import org.apache.gravitino.storage.relational.po.SecurableObjectPO; -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. @@ -39,61 +39,30 @@ public interface SecurableObjectMapper { String SECURABLE_OBJECT_TABLE_NAME = "role_meta_securable_object"; String ROLE_TABLE_NAME = "role_meta"; - @Insert({ - "" - }) + @InsertProvider( + type = SecurableObjectSQLProviderFactory.class, + method = "batchInsertSecurableObjects") void batchInsertSecurableObjects( @Param("securableObjects") List securableObjectPOs); - @Update( - "UPDATE " - + SECURABLE_OBJECT_TABLE_NAME - + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" - + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" - + " WHERE role_id = #{roleId} AND deleted_at = 0") + @UpdateProvider( + type = SecurableObjectSQLProviderFactory.class, + method = "softDeleteSecurableObjectsByRoleId") void softDeleteSecurableObjectsByRoleId(@Param("roleId") Long roleId); - @Update( - "UPDATE " - + SECURABLE_OBJECT_TABLE_NAME - + " ob SET ob.deleted_at = (UNIX_TIMESTAMP() * 1000.0)" - + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" - + " WHERE exists (SELECT * from " - + ROLE_TABLE_NAME - + " ro WHERE ro.metalake_id = #{metalakeId} AND ro.role_id = ob.role_id" - + " AND ro.deleted_at = 0) AND ob.deleted_at = 0") + @UpdateProvider( + type = SecurableObjectSQLProviderFactory.class, + method = "softDeleteRoleMetasByMetalakeId") void softDeleteRoleMetasByMetalakeId(@Param("metalakeId") Long metalakeId); - @Select( - "SELECT role_id as roleId, metadata_object_id as metadataObjectId," - + " type as type, privilege_names as privilegeNames," - + " privilege_conditions as privilegeConditions, current_version as currentVersion," - + " last_version as lastVersion, deleted_at as deletedAt" - + " FROM " - + SECURABLE_OBJECT_TABLE_NAME - + " WHERE role_id = #{roleId} AND deleted_at = 0") + @SelectProvider( + type = SecurableObjectSQLProviderFactory.class, + method = "listSecurableObjectsByRoleId") List listSecurableObjectsByRoleId(@Param("roleId") Long roleId); - @Delete( - "DELETE FROM " - + SECURABLE_OBJECT_TABLE_NAME - + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}") + @DeleteProvider( + type = SecurableObjectSQLProviderFactory.class, + method = "deleteSecurableObjectsByLegacyTimeline") Integer deleteSecurableObjectsByLegacyTimeline( @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit); } diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SecurableObjectSQLProviderFactory.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SecurableObjectSQLProviderFactory.java new file mode 100644 index 00000000000..6508d7db39c --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SecurableObjectSQLProviderFactory.java @@ -0,0 +1,73 @@ +/* + * 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.List; +import java.util.Map; +import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType; +import org.apache.gravitino.storage.relational.po.SecurableObjectPO; +import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper; +import org.apache.ibatis.annotations.Param; + +public class SecurableObjectSQLProviderFactory { + + private static final Map + METALAKE_META_SQL_PROVIDER_MAP = + ImmutableMap.of( + JDBCBackendType.MYSQL, new SecurableObjectMySQLProvider(), + JDBCBackendType.H2, new SecurableObjectH2Provider()); + + public static SecurableObjectBaseSQLProvider getProvider() { + String databaseId = + SqlSessionFactoryHelper.getInstance() + .getSqlSessionFactory() + .getConfiguration() + .getDatabaseId(); + + JDBCBackendType jdbcBackendType = JDBCBackendType.fromString(databaseId); + return METALAKE_META_SQL_PROVIDER_MAP.get(jdbcBackendType); + } + + static class SecurableObjectMySQLProvider extends SecurableObjectBaseSQLProvider {} + + static class SecurableObjectH2Provider extends SecurableObjectBaseSQLProvider {} + + public static String batchInsertSecurableObjects( + @Param("securableObjects") List securableObjectPOs) { + return getProvider().batchInsertSecurableObjects(securableObjectPOs); + } + + public static String softDeleteSecurableObjectsByRoleId(@Param("roleId") Long roleId) { + return getProvider().softDeleteSecurableObjectsByRoleId(roleId); + } + + public static String softDeleteRoleMetasByMetalakeId(@Param("metalakeId") Long metalakeId) { + return getProvider().softDeleteRoleMetasByMetalakeId(metalakeId); + } + + public static String listSecurableObjectsByRoleId(@Param("roleId") Long roleId) { + return getProvider().listSecurableObjectsByRoleId(roleId); + } + + public static String deleteSecurableObjectsByLegacyTimeline( + @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { + return getProvider().deleteSecurableObjectsByLegacyTimeline(legacyTimeline, limit); + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetaBaseSQLProvider.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetaBaseSQLProvider.java new file mode 100644 index 00000000000..4c9ee54bfff --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetaBaseSQLProvider.java @@ -0,0 +1,197 @@ +/* + * 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.TagMetaMapper.TAG_TABLE_NAME; + +import java.util.List; +import org.apache.gravitino.storage.relational.po.TagPO; +import org.apache.ibatis.annotations.Param; + +public class TagMetaBaseSQLProvider { + + public String listTagPOsByMetalake(@Param("metalakeName") String metalakeName) { + return "SELECT tm.tag_id as tagId, tm.tag_name as tagName," + + " tm.metalake_id as metalakeId," + + " tm.tag_comment as comment," + + " tm.properties as properties," + + " tm.audit_info as auditInfo," + + " tm.current_version as currentVersion," + + " tm.last_version as lastVersion," + + " tm.deleted_at as deletedAt" + + " FROM " + + TAG_TABLE_NAME + + " tm JOIN " + + MetalakeMetaMapper.TABLE_NAME + + " mm ON tm.metalake_id = mm.metalake_id" + + " WHERE mm.metalake_name = #{metalakeName} AND tm.deleted_at = 0 AND mm.deleted_at = 0"; + } + + public String listTagPOsByMetalakeAndTagNames( + @Param("metalakeName") String metalakeName, @Param("tagNames") List tagNames) { + return ""; + } + + public String selectTagIdByMetalakeAndName( + @Param("metalakeName") String metalakeName, @Param("tagName") String tagName) { + return "SELECT tm.tag_id as tagId FROM " + + TAG_TABLE_NAME + + " tm JOIN " + + MetalakeMetaMapper.TABLE_NAME + + " mm ON tm.metalake_id = mm.metalake_id" + + " WHERE mm.metalake_name = #{metalakeName} AND tm.tag_name = #{tagName}" + + " AND tm.deleted_at = 0 AND mm.deleted_at = 0"; + } + + public String selectTagMetaByMetalakeAndName( + @Param("metalakeName") String metalakeName, @Param("tagName") String tagName) { + return "SELECT tm.tag_id as tagId, tm.tag_name as tagName," + + " tm.metalake_id as metalakeId," + + " tm.tag_comment as comment," + + " tm.properties as properties," + + " tm.audit_info as auditInfo," + + " tm.current_version as currentVersion," + + " tm.last_version as lastVersion," + + " tm.deleted_at as deletedAt" + + " FROM " + + TAG_TABLE_NAME + + " tm JOIN " + + MetalakeMetaMapper.TABLE_NAME + + " mm ON tm.metalake_id = mm.metalake_id" + + " WHERE mm.metalake_name = #{metalakeName} AND tm.tag_name = #{tagName}" + + " AND tm.deleted_at = 0 AND mm.deleted_at = 0"; + } + + public String insertTagMeta(@Param("tagMeta") TagPO tagPO) { + return "INSERT INTO " + + TAG_TABLE_NAME + + " (tag_id, tag_name," + + " metalake_id, tag_comment, properties, audit_info," + + " current_version, last_version, deleted_at)" + + " VALUES(" + + " #{tagMeta.tagId}," + + " #{tagMeta.tagName}," + + " #{tagMeta.metalakeId}," + + " #{tagMeta.comment}," + + " #{tagMeta.properties}," + + " #{tagMeta.auditInfo}," + + " #{tagMeta.currentVersion}," + + " #{tagMeta.lastVersion}," + + " #{tagMeta.deletedAt}" + + " )"; + } + + public String insertTagMetaOnDuplicateKeyUpdate(@Param("tagMeta") TagPO tagPO) { + return "INSERT INTO " + + TAG_TABLE_NAME + + "(tag_id, tag_name," + + " metalake_id, tag_comment, properties, audit_info," + + " current_version, last_version, deleted_at)" + + " VALUES(" + + " #{tagMeta.tagId}," + + " #{tagMeta.tagName}," + + " #{tagMeta.metalakeId}," + + " #{tagMeta.comment}," + + " #{tagMeta.properties}," + + " #{tagMeta.auditInfo}," + + " #{tagMeta.currentVersion}," + + " #{tagMeta.lastVersion}," + + " #{tagMeta.deletedAt}" + + " )" + + " ON DUPLICATE KEY UPDATE" + + " tag_name = #{tagMeta.tagName}," + + " metalake_id = #{tagMeta.metalakeId}," + + " tag_comment = #{tagMeta.comment}," + + " properties = #{tagMeta.properties}," + + " audit_info = #{tagMeta.auditInfo}," + + " current_version = #{tagMeta.currentVersion}," + + " last_version = #{tagMeta.lastVersion}," + + " deleted_at = #{tagMeta.deletedAt}"; + } + + public String updateTagMeta( + @Param("newTagMeta") TagPO newTagPO, @Param("oldTagMeta") TagPO oldTagPO) { + return "UPDATE " + + TAG_TABLE_NAME + + " SET tag_name = #{newTagMeta.tagName}," + + " tag_comment = #{newTagMeta.comment}," + + " properties = #{newTagMeta.properties}," + + " audit_info = #{newTagMeta.auditInfo}," + + " current_version = #{newTagMeta.currentVersion}," + + " last_version = #{newTagMeta.lastVersion}," + + " deleted_at = #{newTagMeta.deletedAt}" + + " WHERE tag_id = #{oldTagMeta.tagId}" + + " AND metalake_id = #{oldTagMeta.metalakeId}" + + " AND tag_name = #{oldTagMeta.tagName}" + + " AND (tag_comment IS NULL OR tag_comment = #{oldTagMeta.comment})" + + " AND properties = #{oldTagMeta.properties}" + + " AND audit_info = #{oldTagMeta.auditInfo}" + + " AND current_version = #{oldTagMeta.currentVersion}" + + " AND last_version = #{oldTagMeta.lastVersion}" + + " AND deleted_at = 0"; + } + + public String softDeleteTagMetaByMetalakeAndTagName( + @Param("metalakeName") String metalakeName, @Param("tagName") String tagName) { + return "UPDATE " + + TAG_TABLE_NAME + + " tm SET tm.deleted_at = (UNIX_TIMESTAMP() * 1000.0)" + + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" + + " WHERE tm.metalake_id IN (" + + " SELECT mm.metalake_id FROM " + + MetalakeMetaMapper.TABLE_NAME + + " mm WHERE mm.metalake_name = #{metalakeName} AND mm.deleted_at = 0)" + + " AND tm.tag_name = #{tagName} AND tm.deleted_at = 0"; + } + + public String softDeleteTagMetasByMetalakeId(@Param("metalakeId") Long metalakeId) { + return "UPDATE " + + TAG_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 deleteTagMetasByLegacyTimeline( + @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { + return "DELETE FROM " + + TAG_TABLE_NAME + + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}"; + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetaMapper.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetaMapper.java index ad8afcfa366..a76a459f42d 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetaMapper.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetaMapper.java @@ -20,181 +20,54 @@ import java.util.List; import org.apache.gravitino.storage.relational.po.TagPO; -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; public interface TagMetaMapper { String TAG_TABLE_NAME = "tag_meta"; - @Select( - "SELECT tm.tag_id as tagId, tm.tag_name as tagName," - + " tm.metalake_id as metalakeId," - + " tm.tag_comment as comment," - + " tm.properties as properties," - + " tm.audit_info as auditInfo," - + " tm.current_version as currentVersion," - + " tm.last_version as lastVersion," - + " tm.deleted_at as deletedAt" - + " FROM " - + TAG_TABLE_NAME - + " tm JOIN " - + MetalakeMetaMapper.TABLE_NAME - + " mm ON tm.metalake_id = mm.metalake_id" - + " WHERE mm.metalake_name = #{metalakeName} AND tm.deleted_at = 0 AND mm.deleted_at = 0") + @SelectProvider(type = TagMetaSQLProviderFactory.class, method = "listTagPOsByMetalake") List listTagPOsByMetalake(@Param("metalakeName") String metalakeName); - @Select( - "") + @SelectProvider( + type = TagMetaSQLProviderFactory.class, + method = "listTagPOsByMetalakeAndTagNames") List listTagPOsByMetalakeAndTagNames( @Param("metalakeName") String metalakeName, @Param("tagNames") List tagNames); - @Select( - "SELECT tm.tag_id as tagId FROM " - + TAG_TABLE_NAME - + " tm JOIN " - + MetalakeMetaMapper.TABLE_NAME - + " mm ON tm.metalake_id = mm.metalake_id" - + " WHERE mm.metalake_name = #{metalakeName} AND tm.tag_name = #{tagName}" - + " AND tm.deleted_at = 0 AND mm.deleted_at = 0") + @SelectProvider(type = TagMetaSQLProviderFactory.class, method = "selectTagIdByMetalakeAndName") Long selectTagIdByMetalakeAndName( @Param("metalakeName") String metalakeName, @Param("tagName") String tagName); - @Select( - "SELECT tm.tag_id as tagId, tm.tag_name as tagName," - + " tm.metalake_id as metalakeId," - + " tm.tag_comment as comment," - + " tm.properties as properties," - + " tm.audit_info as auditInfo," - + " tm.current_version as currentVersion," - + " tm.last_version as lastVersion," - + " tm.deleted_at as deletedAt" - + " FROM " - + TAG_TABLE_NAME - + " tm JOIN " - + MetalakeMetaMapper.TABLE_NAME - + " mm ON tm.metalake_id = mm.metalake_id" - + " WHERE mm.metalake_name = #{metalakeName} AND tm.tag_name = #{tagName}" - + " AND tm.deleted_at = 0 AND mm.deleted_at = 0") + @SelectProvider(type = TagMetaSQLProviderFactory.class, method = "selectTagMetaByMetalakeAndName") TagPO selectTagMetaByMetalakeAndName( @Param("metalakeName") String metalakeName, @Param("tagName") String tagName); - @Insert( - "INSERT INTO " - + TAG_TABLE_NAME - + " (tag_id, tag_name," - + " metalake_id, tag_comment, properties, audit_info," - + " current_version, last_version, deleted_at)" - + " VALUES(" - + " #{tagMeta.tagId}," - + " #{tagMeta.tagName}," - + " #{tagMeta.metalakeId}," - + " #{tagMeta.comment}," - + " #{tagMeta.properties}," - + " #{tagMeta.auditInfo}," - + " #{tagMeta.currentVersion}," - + " #{tagMeta.lastVersion}," - + " #{tagMeta.deletedAt}" - + " )") + @InsertProvider(type = TagMetaSQLProviderFactory.class, method = "insertTagMeta") void insertTagMeta(@Param("tagMeta") TagPO tagPO); - @Insert( - "INSERT INTO " - + TAG_TABLE_NAME - + "(tag_id, tag_name," - + " metalake_id, tag_comment, properties, audit_info," - + " current_version, last_version, deleted_at)" - + " VALUES(" - + " #{tagMeta.tagId}," - + " #{tagMeta.tagName}," - + " #{tagMeta.metalakeId}," - + " #{tagMeta.comment}," - + " #{tagMeta.properties}," - + " #{tagMeta.auditInfo}," - + " #{tagMeta.currentVersion}," - + " #{tagMeta.lastVersion}," - + " #{tagMeta.deletedAt}" - + " )" - + " ON DUPLICATE KEY UPDATE" - + " tag_name = #{tagMeta.tagName}," - + " metalake_id = #{tagMeta.metalakeId}," - + " tag_comment = #{tagMeta.comment}," - + " properties = #{tagMeta.properties}," - + " audit_info = #{tagMeta.auditInfo}," - + " current_version = #{tagMeta.currentVersion}," - + " last_version = #{tagMeta.lastVersion}," - + " deleted_at = #{tagMeta.deletedAt}") + @InsertProvider( + type = TagMetaSQLProviderFactory.class, + method = "insertTagMetaOnDuplicateKeyUpdate") void insertTagMetaOnDuplicateKeyUpdate(@Param("tagMeta") TagPO tagPO); - @Update( - "UPDATE " - + TAG_TABLE_NAME - + " SET tag_name = #{newTagMeta.tagName}," - + " tag_comment = #{newTagMeta.comment}," - + " properties = #{newTagMeta.properties}," - + " audit_info = #{newTagMeta.auditInfo}," - + " current_version = #{newTagMeta.currentVersion}," - + " last_version = #{newTagMeta.lastVersion}," - + " deleted_at = #{newTagMeta.deletedAt}" - + " WHERE tag_id = #{oldTagMeta.tagId}" - + " AND metalake_id = #{oldTagMeta.metalakeId}" - + " AND tag_name = #{oldTagMeta.tagName}" - + " AND (tag_comment IS NULL OR tag_comment = #{oldTagMeta.comment})" - + " AND properties = #{oldTagMeta.properties}" - + " AND audit_info = #{oldTagMeta.auditInfo}" - + " AND current_version = #{oldTagMeta.currentVersion}" - + " AND last_version = #{oldTagMeta.lastVersion}" - + " AND deleted_at = 0") + @UpdateProvider(type = TagMetaSQLProviderFactory.class, method = "updateTagMeta") Integer updateTagMeta(@Param("newTagMeta") TagPO newTagPO, @Param("oldTagMeta") TagPO oldTagPO); - @Update( - "UPDATE " - + TAG_TABLE_NAME - + " tm SET tm.deleted_at = (UNIX_TIMESTAMP() * 1000.0)" - + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" - + " WHERE tm.metalake_id IN (" - + " SELECT mm.metalake_id FROM " - + MetalakeMetaMapper.TABLE_NAME - + " mm WHERE mm.metalake_name = #{metalakeName} AND mm.deleted_at = 0)" - + " AND tm.tag_name = #{tagName} AND tm.deleted_at = 0") + @UpdateProvider( + type = TagMetaSQLProviderFactory.class, + method = "softDeleteTagMetaByMetalakeAndTagName") Integer softDeleteTagMetaByMetalakeAndTagName( @Param("metalakeName") String metalakeName, @Param("tagName") String tagName); - @Update( - "UPDATE " - + TAG_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 = TagMetaSQLProviderFactory.class, method = "softDeleteTagMetasByMetalakeId") void softDeleteTagMetasByMetalakeId(@Param("metalakeId") Long metalakeId); - @Delete( - "DELETE FROM " - + TAG_TABLE_NAME - + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}") + @DeleteProvider(type = TagMetaSQLProviderFactory.class, method = "deleteTagMetasByLegacyTimeline") Integer deleteTagMetasByLegacyTimeline( @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit); } diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetaSQLProviderFactory.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetaSQLProviderFactory.java new file mode 100644 index 00000000000..aaa92b038ef --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetaSQLProviderFactory.java @@ -0,0 +1,96 @@ +/* + * 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.List; +import java.util.Map; +import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType; +import org.apache.gravitino.storage.relational.po.TagPO; +import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper; +import org.apache.ibatis.annotations.Param; + +public class TagMetaSQLProviderFactory { + + private static final Map METALAKE_META_SQL_PROVIDER_MAP = + ImmutableMap.of( + JDBCBackendType.MYSQL, new TagMetaMySQLProvider(), + JDBCBackendType.H2, new TagMetaH2Provider()); + + public static TagMetaBaseSQLProvider getProvider() { + String databaseId = + SqlSessionFactoryHelper.getInstance() + .getSqlSessionFactory() + .getConfiguration() + .getDatabaseId(); + + JDBCBackendType jdbcBackendType = JDBCBackendType.fromString(databaseId); + return METALAKE_META_SQL_PROVIDER_MAP.get(jdbcBackendType); + } + + static class TagMetaMySQLProvider extends TagMetaBaseSQLProvider {} + + static class TagMetaH2Provider extends TagMetaBaseSQLProvider {} + + public static String listTagPOsByMetalake(@Param("metalakeName") String metalakeName) { + return getProvider().listTagPOsByMetalake(metalakeName); + } + + public static String listTagPOsByMetalakeAndTagNames( + @Param("metalakeName") String metalakeName, @Param("tagNames") List tagNames) { + return getProvider().listTagPOsByMetalakeAndTagNames(metalakeName, tagNames); + } + + public static String selectTagIdByMetalakeAndName( + @Param("metalakeName") String metalakeName, @Param("tagName") String tagName) { + return getProvider().selectTagIdByMetalakeAndName(metalakeName, tagName); + } + + public static String selectTagMetaByMetalakeAndName( + @Param("metalakeName") String metalakeName, @Param("tagName") String tagName) { + return getProvider().selectTagMetaByMetalakeAndName(metalakeName, tagName); + } + + public static String insertTagMeta(@Param("tagMeta") TagPO tagPO) { + return getProvider().insertTagMeta(tagPO); + } + + public static String insertTagMetaOnDuplicateKeyUpdate(@Param("tagMeta") TagPO tagPO) { + return getProvider().insertTagMetaOnDuplicateKeyUpdate(tagPO); + } + + public static String updateTagMeta( + @Param("newTagMeta") TagPO newTagPO, @Param("oldTagMeta") TagPO oldTagPO) { + return getProvider().updateTagMeta(newTagPO, oldTagPO); + } + + public static String softDeleteTagMetaByMetalakeAndTagName( + @Param("metalakeName") String metalakeName, @Param("tagName") String tagName) { + return getProvider().softDeleteTagMetaByMetalakeAndTagName(metalakeName, tagName); + } + + public static String softDeleteTagMetasByMetalakeId(@Param("metalakeId") Long metalakeId) { + return getProvider().softDeleteTagMetasByMetalakeId(metalakeId); + } + + public static String deleteTagMetasByLegacyTimeline( + @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { + return getProvider().deleteTagMetasByLegacyTimeline(legacyTimeline, limit); + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetadataObjectRelBaseSQLProvider.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetadataObjectRelBaseSQLProvider.java new file mode 100644 index 00000000000..9bcea1cd95e --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetadataObjectRelBaseSQLProvider.java @@ -0,0 +1,154 @@ +/* + * 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.TagMetadataObjectRelMapper.TAG_METADATA_OBJECT_RELATION_TABLE_NAME; + +import java.util.List; +import org.apache.gravitino.storage.relational.po.TagMetadataObjectRelPO; +import org.apache.ibatis.annotations.Param; + +public class TagMetadataObjectRelBaseSQLProvider { + + public String listTagPOsByMetadataObjectIdAndType( + @Param("metadataObjectId") Long metadataObjectId, + @Param("metadataObjectType") String metadataObjectType) { + return "SELECT tm.tag_id as tagId, tm.tag_name as tagName," + + " tm.metalake_id as metalakeId, tm.tag_comment as comment, tm.properties as properties," + + " tm.audit_info as auditInfo," + + " tm.current_version as currentVersion," + + " tm.last_version as lastVersion," + + " tm.deleted_at as deletedAt" + + " FROM " + + TagMetaMapper.TAG_TABLE_NAME + + " tm JOIN " + + TAG_METADATA_OBJECT_RELATION_TABLE_NAME + + " te ON tm.tag_id = te.tag_id" + + " WHERE te.metadata_object_id = #{metadataObjectId}" + + " AND te.metadata_object_type = #{metadataObjectType} AND te.deleted_at = 0" + + " AND tm.deleted_at = 0"; + } + + public String getTagPOsByMetadataObjectAndTagName( + @Param("metadataObjectId") Long metadataObjectId, + @Param("metadataObjectType") String metadataObjectType, + @Param("tagName") String tagName) { + return "SELECT tm.tag_id as tagId, tm.tag_name as tagName," + + " tm.metalake_id as metalakeId, tm.tag_comment as comment, tm.properties as properties," + + " tm.audit_info as auditInfo," + + " tm.current_version as currentVersion," + + " tm.last_version as lastVersion," + + " tm.deleted_at as deletedAt" + + " FROM " + + TagMetaMapper.TAG_TABLE_NAME + + " tm JOIN " + + TAG_METADATA_OBJECT_RELATION_TABLE_NAME + + " te ON tm.tag_id = te.tag_id" + + " WHERE te.metadata_object_id = #{metadataObjectId}" + + " AND te.metadata_object_type = #{metadataObjectType} AND tm.tag_name = #{tagName}" + + " AND te.deleted_at = 0 AND tm.deleted_at = 0"; + } + + public String listTagMetadataObjectRelsByMetalakeAndTagName( + @Param("metalakeName") String metalakeName, @Param("tagName") String tagName) { + return "SELECT te.tag_id as tagId, te.metadata_object_id as metadataObjectId," + + " te.metadata_object_type as metadataObjectType, te.audit_info as auditInfo," + + " te.current_version as currentVersion, te.last_version as lastVersion," + + " te.deleted_at as deletedAt" + + " FROM " + + TAG_METADATA_OBJECT_RELATION_TABLE_NAME + + " te JOIN " + + TagMetaMapper.TAG_TABLE_NAME + + " tm JOIN " + + MetalakeMetaMapper.TABLE_NAME + + " mm ON te.tag_id = tm.tag_id AND tm.metalake_id = mm.metalake_id" + + " WHERE mm.metalake_name = #{metalakeName} AND tm.tag_name = #{tagName}" + + " AND te.deleted_at = 0 AND tm.deleted_at = 0 AND mm.deleted_at = 0"; + } + + public String batchInsertTagMetadataObjectRels( + @Param("tagRels") List tagRelPOs) { + return ""; + } + + public String batchDeleteTagMetadataObjectRelsByTagIdsAndMetadataObject( + @Param("metadataObjectId") Long metadataObjectId, + @Param("metadataObjectType") String metadataObjectType, + @Param("tagIds") List tagIds) { + return ""; + } + + public String softDeleteTagMetadataObjectRelsByMetalakeAndTagName( + @Param("metalakeName") String metalakeName, @Param("tagName") String tagName) { + return "UPDATE " + + TAG_METADATA_OBJECT_RELATION_TABLE_NAME + + " te SET te.deleted_at = (UNIX_TIMESTAMP() * 1000.0)" + + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" + + " WHERE te.tag_id IN (SELECT tm.tag_id FROM " + + TagMetaMapper.TAG_TABLE_NAME + + " tm WHERE tm.metalake_id IN (SELECT mm.metalake_id FROM " + + MetalakeMetaMapper.TABLE_NAME + + " mm WHERE mm.metalake_name = #{metalakeName} AND mm.deleted_at = 0)" + + " AND tm.deleted_at = 0) AND te.deleted_at = 0"; + } + + public String softDeleteTagMetadataObjectRelsByMetalakeId(@Param("metalakeId") Long metalakeId) { + return "UPDATE " + + TAG_METADATA_OBJECT_RELATION_TABLE_NAME + + " te SET te.deleted_at = (UNIX_TIMESTAMP() * 1000.0)" + + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" + + " WHERE EXISTS (SELECT * FROM " + + TagMetaMapper.TAG_TABLE_NAME + + " tm WHERE tm.metalake_id = #{metalakeId} AND tm.tag_id = te.tag_id" + + " AND tm.deleted_at = 0) AND te.deleted_at = 0"; + } + + public String deleteTagEntityRelsByLegacyTimeline( + @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { + return "DELETE FROM " + + TAG_METADATA_OBJECT_RELATION_TABLE_NAME + + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}"; + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetadataObjectRelMapper.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetadataObjectRelMapper.java index 1ade27d8ffc..40b62955881 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetadataObjectRelMapper.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetadataObjectRelMapper.java @@ -21,139 +21,63 @@ import java.util.List; import org.apache.gravitino.storage.relational.po.TagMetadataObjectRelPO; import org.apache.gravitino.storage.relational.po.TagPO; -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; public interface TagMetadataObjectRelMapper { String TAG_METADATA_OBJECT_RELATION_TABLE_NAME = "tag_relation_meta"; - @Select( - "SELECT tm.tag_id as tagId, tm.tag_name as tagName," - + " tm.metalake_id as metalakeId, tm.tag_comment as comment, tm.properties as properties," - + " tm.audit_info as auditInfo," - + " tm.current_version as currentVersion," - + " tm.last_version as lastVersion," - + " tm.deleted_at as deletedAt" - + " FROM " - + TagMetaMapper.TAG_TABLE_NAME - + " tm JOIN " - + TAG_METADATA_OBJECT_RELATION_TABLE_NAME - + " te ON tm.tag_id = te.tag_id" - + " WHERE te.metadata_object_id = #{metadataObjectId}" - + " AND te.metadata_object_type = #{metadataObjectType} AND te.deleted_at = 0" - + " AND tm.deleted_at = 0") + @SelectProvider( + type = TagMetadataObjectRelSQLProviderFactory.class, + method = "listTagPOsByMetadataObjectIdAndType") List listTagPOsByMetadataObjectIdAndType( @Param("metadataObjectId") Long metadataObjectId, @Param("metadataObjectType") String metadataObjectType); - @Select( - "SELECT tm.tag_id as tagId, tm.tag_name as tagName," - + " tm.metalake_id as metalakeId, tm.tag_comment as comment, tm.properties as properties," - + " tm.audit_info as auditInfo," - + " tm.current_version as currentVersion," - + " tm.last_version as lastVersion," - + " tm.deleted_at as deletedAt" - + " FROM " - + TagMetaMapper.TAG_TABLE_NAME - + " tm JOIN " - + TAG_METADATA_OBJECT_RELATION_TABLE_NAME - + " te ON tm.tag_id = te.tag_id" - + " WHERE te.metadata_object_id = #{metadataObjectId}" - + " AND te.metadata_object_type = #{metadataObjectType} AND tm.tag_name = #{tagName}" - + " AND te.deleted_at = 0 AND tm.deleted_at = 0") + @SelectProvider( + type = TagMetadataObjectRelSQLProviderFactory.class, + method = "getTagPOsByMetadataObjectAndTagName") TagPO getTagPOsByMetadataObjectAndTagName( @Param("metadataObjectId") Long metadataObjectId, @Param("metadataObjectType") String metadataObjectType, @Param("tagName") String tagName); - @Select( - "SELECT te.tag_id as tagId, te.metadata_object_id as metadataObjectId," - + " te.metadata_object_type as metadataObjectType, te.audit_info as auditInfo," - + " te.current_version as currentVersion, te.last_version as lastVersion," - + " te.deleted_at as deletedAt" - + " FROM " - + TAG_METADATA_OBJECT_RELATION_TABLE_NAME - + " te JOIN " - + TagMetaMapper.TAG_TABLE_NAME - + " tm JOIN " - + MetalakeMetaMapper.TABLE_NAME - + " mm ON te.tag_id = tm.tag_id AND tm.metalake_id = mm.metalake_id" - + " WHERE mm.metalake_name = #{metalakeName} AND tm.tag_name = #{tagName}" - + " AND te.deleted_at = 0 AND tm.deleted_at = 0 AND mm.deleted_at = 0") + @SelectProvider( + type = TagMetadataObjectRelSQLProviderFactory.class, + method = "listTagMetadataObjectRelsByMetalakeAndTagName") List listTagMetadataObjectRelsByMetalakeAndTagName( @Param("metalakeName") String metalakeName, @Param("tagName") String tagName); - @Insert({ - "" - }) + @InsertProvider( + type = TagMetadataObjectRelSQLProviderFactory.class, + method = "batchInsertTagMetadataObjectRels") void batchInsertTagMetadataObjectRels(@Param("tagRels") List tagRelPOs); - @Update({ - "" - }) + @UpdateProvider( + type = TagMetadataObjectRelSQLProviderFactory.class, + method = "batchDeleteTagMetadataObjectRelsByTagIdsAndMetadataObject") void batchDeleteTagMetadataObjectRelsByTagIdsAndMetadataObject( @Param("metadataObjectId") Long metadataObjectId, @Param("metadataObjectType") String metadataObjectType, @Param("tagIds") List tagIds); - @Update( - "UPDATE " - + TAG_METADATA_OBJECT_RELATION_TABLE_NAME - + " te SET te.deleted_at = (UNIX_TIMESTAMP() * 1000.0)" - + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" - + " WHERE te.tag_id IN (SELECT tm.tag_id FROM " - + TagMetaMapper.TAG_TABLE_NAME - + " tm WHERE tm.metalake_id IN (SELECT mm.metalake_id FROM " - + MetalakeMetaMapper.TABLE_NAME - + " mm WHERE mm.metalake_name = #{metalakeName} AND mm.deleted_at = 0)" - + " AND tm.deleted_at = 0) AND te.deleted_at = 0") + @UpdateProvider( + type = TagMetadataObjectRelSQLProviderFactory.class, + method = "softDeleteTagMetadataObjectRelsByMetalakeAndTagName") Integer softDeleteTagMetadataObjectRelsByMetalakeAndTagName( @Param("metalakeName") String metalakeName, @Param("tagName") String tagName); - @Update( - "UPDATE " - + TAG_METADATA_OBJECT_RELATION_TABLE_NAME - + " te SET te.deleted_at = (UNIX_TIMESTAMP() * 1000.0)" - + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" - + " WHERE EXISTS (SELECT * FROM " - + TagMetaMapper.TAG_TABLE_NAME - + " tm WHERE tm.metalake_id = #{metalakeId} AND tm.tag_id = te.tag_id" - + " AND tm.deleted_at = 0) AND te.deleted_at = 0") + @UpdateProvider( + type = TagMetadataObjectRelSQLProviderFactory.class, + method = "softDeleteTagMetadataObjectRelsByMetalakeId") void softDeleteTagMetadataObjectRelsByMetalakeId(@Param("metalakeId") Long metalakeId); - @Delete( - "DELETE FROM " - + TAG_METADATA_OBJECT_RELATION_TABLE_NAME - + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}") + @DeleteProvider( + type = TagMetadataObjectRelSQLProviderFactory.class, + method = "deleteTagEntityRelsByLegacyTimeline") Integer deleteTagEntityRelsByLegacyTimeline( @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit); } diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetadataObjectRelSQLProviderFactory.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetadataObjectRelSQLProviderFactory.java new file mode 100644 index 00000000000..b074349e817 --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetadataObjectRelSQLProviderFactory.java @@ -0,0 +1,99 @@ +/* + * 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.List; +import java.util.Map; +import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType; +import org.apache.gravitino.storage.relational.po.TagMetadataObjectRelPO; +import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper; +import org.apache.ibatis.annotations.Param; + +public class TagMetadataObjectRelSQLProviderFactory { + + private static final Map + METALAKE_META_SQL_PROVIDER_MAP = + ImmutableMap.of( + JDBCBackendType.MYSQL, new TagMetadataObjectRelMySQLProvider(), + JDBCBackendType.H2, new TagMetadataObjectRelH2Provider()); + + public static TagMetadataObjectRelBaseSQLProvider getProvider() { + String databaseId = + SqlSessionFactoryHelper.getInstance() + .getSqlSessionFactory() + .getConfiguration() + .getDatabaseId(); + + JDBCBackendType jdbcBackendType = JDBCBackendType.fromString(databaseId); + return METALAKE_META_SQL_PROVIDER_MAP.get(jdbcBackendType); + } + + static class TagMetadataObjectRelMySQLProvider extends TagMetadataObjectRelBaseSQLProvider {} + + static class TagMetadataObjectRelH2Provider extends TagMetadataObjectRelBaseSQLProvider {} + + public static String listTagPOsByMetadataObjectIdAndType( + @Param("metadataObjectId") Long metadataObjectId, + @Param("metadataObjectType") String metadataObjectType) { + return getProvider().listTagPOsByMetadataObjectIdAndType(metadataObjectId, metadataObjectType); + } + + public static String getTagPOsByMetadataObjectAndTagName( + @Param("metadataObjectId") Long metadataObjectId, + @Param("metadataObjectType") String metadataObjectType, + @Param("tagName") String tagName) { + return getProvider() + .getTagPOsByMetadataObjectAndTagName(metadataObjectId, metadataObjectType, tagName); + } + + public static String listTagMetadataObjectRelsByMetalakeAndTagName( + @Param("metalakeName") String metalakeName, @Param("tagName") String tagName) { + return getProvider().listTagMetadataObjectRelsByMetalakeAndTagName(metalakeName, tagName); + } + + public static String batchInsertTagMetadataObjectRels( + @Param("tagRels") List tagRelPOs) { + return getProvider().batchInsertTagMetadataObjectRels(tagRelPOs); + } + + public static String batchDeleteTagMetadataObjectRelsByTagIdsAndMetadataObject( + @Param("metadataObjectId") Long metadataObjectId, + @Param("metadataObjectType") String metadataObjectType, + @Param("tagIds") List tagIds) { + return getProvider() + .batchDeleteTagMetadataObjectRelsByTagIdsAndMetadataObject( + metadataObjectId, metadataObjectType, tagIds); + } + + public static String softDeleteTagMetadataObjectRelsByMetalakeAndTagName( + @Param("metalakeName") String metalakeName, @Param("tagName") String tagName) { + return getProvider().softDeleteTagMetadataObjectRelsByMetalakeAndTagName(metalakeName, tagName); + } + + public static String softDeleteTagMetadataObjectRelsByMetalakeId( + @Param("metalakeId") Long metalakeId) { + return getProvider().softDeleteTagMetadataObjectRelsByMetalakeId(metalakeId); + } + + public static String deleteTagEntityRelsByLegacyTimeline( + @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { + return getProvider().deleteTagEntityRelsByLegacyTimeline(legacyTimeline, limit); + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserMetaBaseSQLProvider.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserMetaBaseSQLProvider.java new file mode 100644 index 00000000000..a5db8e0f943 --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserMetaBaseSQLProvider.java @@ -0,0 +1,147 @@ +/* + * 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.UserMetaMapper.USER_ROLE_RELATION_TABLE_NAME; +import static org.apache.gravitino.storage.relational.mapper.UserRoleRelMapper.USER_TABLE_NAME; + +import org.apache.gravitino.storage.relational.po.UserPO; +import org.apache.ibatis.annotations.Param; + +public class UserMetaBaseSQLProvider { + + public String selectUserIdByMetalakeIdAndName( + @Param("metalakeId") Long metalakeId, @Param("userName") String name) { + return "SELECT user_id as userId FROM " + + USER_TABLE_NAME + + " WHERE metalake_id = #{metalakeId} AND user_name = #{userName}" + + " AND deleted_at = 0"; + } + + public String selectUserMetaByMetalakeIdAndName( + @Param("metalakeId") Long metalakeId, @Param("userName") String name) { + return "SELECT user_id as userId, user_name as userName," + + " metalake_id as metalakeId," + + " audit_info as auditInfo," + + " current_version as currentVersion, last_version as lastVersion," + + " deleted_at as deletedAt" + + " FROM " + + USER_TABLE_NAME + + " WHERE metalake_id = #{metalakeId} AND user_name = #{userName}" + + " AND deleted_at = 0"; + } + + public String insertUserMeta(@Param("userMeta") UserPO userPO) { + return "INSERT INTO " + + USER_TABLE_NAME + + "(user_id, user_name," + + " metalake_id, audit_info," + + " current_version, last_version, deleted_at)" + + " VALUES(" + + " #{userMeta.userId}," + + " #{userMeta.userName}," + + " #{userMeta.metalakeId}," + + " #{userMeta.auditInfo}," + + " #{userMeta.currentVersion}," + + " #{userMeta.lastVersion}," + + " #{userMeta.deletedAt}" + + " )"; + } + + public String insertUserMetaOnDuplicateKeyUpdate(@Param("userMeta") UserPO userPO) { + return "INSERT INTO " + + USER_TABLE_NAME + + "(user_id, user_name," + + "metalake_id, audit_info," + + " current_version, last_version, deleted_at)" + + " VALUES(" + + " #{userMeta.userId}," + + " #{userMeta.userName}," + + " #{userMeta.metalakeId}," + + " #{userMeta.auditInfo}," + + " #{userMeta.currentVersion}," + + " #{userMeta.lastVersion}," + + " #{userMeta.deletedAt}" + + " )" + + " ON DUPLICATE KEY UPDATE" + + " user_name = #{userMeta.userName}," + + " metalake_id = #{userMeta.metalakeId}," + + " audit_info = #{userMeta.auditInfo}," + + " current_version = #{userMeta.currentVersion}," + + " last_version = #{userMeta.lastVersion}," + + " deleted_at = #{userMeta.deletedAt}"; + } + + public String softDeleteUserMetaByUserId(@Param("userId") Long userId) { + return "UPDATE " + + USER_TABLE_NAME + + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" + + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" + + " WHERE user_id = #{userId} AND deleted_at = 0"; + } + + public String softDeleteUserMetasByMetalakeId(@Param("metalakeId") Long metalakeId) { + return "UPDATE " + + USER_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 updateUserMeta( + @Param("newUserMeta") UserPO newUserPO, @Param("oldUserMeta") UserPO oldUserPO) { + return "UPDATE " + + USER_TABLE_NAME + + " SET user_name = #{newUserMeta.userName}," + + " metalake_id = #{newUserMeta.metalakeId}," + + " audit_info = #{newUserMeta.auditInfo}," + + " current_version = #{newUserMeta.currentVersion}," + + " last_version = #{newUserMeta.lastVersion}," + + " deleted_at = #{newUserMeta.deletedAt}" + + " WHERE user_id = #{oldUserMeta.userId}" + + " AND user_name = #{oldUserMeta.userName}" + + " AND metalake_id = #{oldUserMeta.metalakeId}" + + " AND audit_info = #{oldUserMeta.auditInfo}" + + " AND current_version = #{oldUserMeta.currentVersion}" + + " AND last_version = #{oldUserMeta.lastVersion}" + + " AND deleted_at = 0"; + } + + public String listUsersByRoleId(@Param("roleId") Long roleId) { + return "SELECT us.user_id as userId, us.user_name as userName," + + " us.metalake_id as metalakeId," + + " us.audit_info as auditInfo, us.current_version as currentVersion," + + " us.last_version as lastVersion, us.deleted_at as deletedAt" + + " FROM " + + USER_TABLE_NAME + + " us JOIN " + + USER_ROLE_RELATION_TABLE_NAME + + " re ON us.user_id = re.user_id" + + " WHERE re.role_id = #{roleId}" + + " AND us.deleted_at = 0 AND re.deleted_at = 0"; + } + + public String deleteUserMetasByLegacyTimeline( + @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { + return "DELETE FROM " + + USER_TABLE_NAME + + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}"; + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserMetaMapper.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserMetaMapper.java index e7b442c099a..ad794c39530 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserMetaMapper.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserMetaMapper.java @@ -21,11 +21,11 @@ import java.util.List; import org.apache.gravitino.storage.relational.po.UserPO; -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. @@ -39,121 +39,44 @@ public interface UserMetaMapper { String USER_TABLE_NAME = "user_meta"; String USER_ROLE_RELATION_TABLE_NAME = "user_role_rel"; - @Select( - "SELECT user_id as userId FROM " - + USER_TABLE_NAME - + " WHERE metalake_id = #{metalakeId} AND user_name = #{userName}" - + " AND deleted_at = 0") + @SelectProvider( + type = UserMetaSQLProviderFactory.class, + method = "selectUserIdByMetalakeIdAndName") Long selectUserIdByMetalakeIdAndName( @Param("metalakeId") Long metalakeId, @Param("userName") String name); - @Select( - "SELECT user_id as userId, user_name as userName," - + " metalake_id as metalakeId," - + " audit_info as auditInfo," - + " current_version as currentVersion, last_version as lastVersion," - + " deleted_at as deletedAt" - + " FROM " - + USER_TABLE_NAME - + " WHERE metalake_id = #{metalakeId} AND user_name = #{userName}" - + " AND deleted_at = 0") + @SelectProvider( + type = UserMetaSQLProviderFactory.class, + method = "selectUserMetaByMetalakeIdAndName") UserPO selectUserMetaByMetalakeIdAndName( @Param("metalakeId") Long metalakeId, @Param("userName") String name); - @Insert( - "INSERT INTO " - + USER_TABLE_NAME - + "(user_id, user_name," - + " metalake_id, audit_info," - + " current_version, last_version, deleted_at)" - + " VALUES(" - + " #{userMeta.userId}," - + " #{userMeta.userName}," - + " #{userMeta.metalakeId}," - + " #{userMeta.auditInfo}," - + " #{userMeta.currentVersion}," - + " #{userMeta.lastVersion}," - + " #{userMeta.deletedAt}" - + " )") + @InsertProvider(type = UserMetaSQLProviderFactory.class, method = "insertUserMeta") void insertUserMeta(@Param("userMeta") UserPO userPO); - @Insert( - "INSERT INTO " - + USER_TABLE_NAME - + "(user_id, user_name," - + "metalake_id, audit_info," - + " current_version, last_version, deleted_at)" - + " VALUES(" - + " #{userMeta.userId}," - + " #{userMeta.userName}," - + " #{userMeta.metalakeId}," - + " #{userMeta.auditInfo}," - + " #{userMeta.currentVersion}," - + " #{userMeta.lastVersion}," - + " #{userMeta.deletedAt}" - + " )" - + " ON DUPLICATE KEY UPDATE" - + " user_name = #{userMeta.userName}," - + " metalake_id = #{userMeta.metalakeId}," - + " audit_info = #{userMeta.auditInfo}," - + " current_version = #{userMeta.currentVersion}," - + " last_version = #{userMeta.lastVersion}," - + " deleted_at = #{userMeta.deletedAt}") + @InsertProvider( + type = UserMetaSQLProviderFactory.class, + method = "insertUserMetaOnDuplicateKeyUpdate") void insertUserMetaOnDuplicateKeyUpdate(@Param("userMeta") UserPO userPO); - @Update( - "UPDATE " - + USER_TABLE_NAME - + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)" - + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" - + " WHERE user_id = #{userId} AND deleted_at = 0") + @UpdateProvider(type = UserMetaSQLProviderFactory.class, method = "softDeleteUserMetaByUserId") void softDeleteUserMetaByUserId(@Param("userId") Long userId); - @Update( - "UPDATE " - + USER_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 = UserMetaSQLProviderFactory.class, + method = "softDeleteUserMetasByMetalakeId") void softDeleteUserMetasByMetalakeId(@Param("metalakeId") Long metalakeId); - @Update( - "UPDATE " - + USER_TABLE_NAME - + " SET user_name = #{newUserMeta.userName}," - + " metalake_id = #{newUserMeta.metalakeId}," - + " audit_info = #{newUserMeta.auditInfo}," - + " current_version = #{newUserMeta.currentVersion}," - + " last_version = #{newUserMeta.lastVersion}," - + " deleted_at = #{newUserMeta.deletedAt}" - + " WHERE user_id = #{oldUserMeta.userId}" - + " AND user_name = #{oldUserMeta.userName}" - + " AND metalake_id = #{oldUserMeta.metalakeId}" - + " AND audit_info = #{oldUserMeta.auditInfo}" - + " AND current_version = #{oldUserMeta.currentVersion}" - + " AND last_version = #{oldUserMeta.lastVersion}" - + " AND deleted_at = 0") + @UpdateProvider(type = UserMetaSQLProviderFactory.class, method = "updateUserMeta") Integer updateUserMeta( @Param("newUserMeta") UserPO newUserPO, @Param("oldUserMeta") UserPO oldUserPO); - @Select( - "SELECT us.user_id as userId, us.user_name as userName," - + " us.metalake_id as metalakeId," - + " us.audit_info as auditInfo, us.current_version as currentVersion," - + " us.last_version as lastVersion, us.deleted_at as deletedAt" - + " FROM " - + USER_TABLE_NAME - + " us JOIN " - + USER_ROLE_RELATION_TABLE_NAME - + " re ON us.user_id = re.user_id" - + " WHERE re.role_id = #{roleId}" - + " AND us.deleted_at = 0 AND re.deleted_at = 0") + @SelectProvider(type = UserMetaSQLProviderFactory.class, method = "listUsersByRoleId") List listUsersByRoleId(@Param("roleId") Long roleId); - @Delete( - "DELETE FROM " - + USER_TABLE_NAME - + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}") + @DeleteProvider( + type = UserMetaSQLProviderFactory.class, + method = "deleteUserMetasByLegacyTimeline") Integer deleteUserMetasByLegacyTimeline( @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit); } diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserMetaSQLProviderFactory.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserMetaSQLProviderFactory.java new file mode 100644 index 00000000000..3c64f510c27 --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserMetaSQLProviderFactory.java @@ -0,0 +1,91 @@ +/* + * 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.UserPO; +import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper; +import org.apache.ibatis.annotations.Param; + +public class UserMetaSQLProviderFactory { + + private static final Map + METALAKE_META_SQL_PROVIDER_MAP = + ImmutableMap.of( + JDBCBackendType.MYSQL, new UserMetaMySQLProvider(), + JDBCBackendType.H2, new UserMetaH2Provider()); + + public static UserMetaBaseSQLProvider getProvider() { + String databaseId = + SqlSessionFactoryHelper.getInstance() + .getSqlSessionFactory() + .getConfiguration() + .getDatabaseId(); + + JDBCBackendType jdbcBackendType = JDBCBackendType.fromString(databaseId); + return METALAKE_META_SQL_PROVIDER_MAP.get(jdbcBackendType); + } + + static class UserMetaMySQLProvider extends UserMetaBaseSQLProvider {} + + static class UserMetaH2Provider extends UserMetaBaseSQLProvider {} + + public static String selectUserIdByMetalakeIdAndName( + @Param("metalakeId") Long metalakeId, @Param("userName") String userName) { + return getProvider().selectUserIdByMetalakeIdAndName(metalakeId, userName); + } + + public static String selectUserMetaByMetalakeIdAndName( + @Param("metalakeId") Long metalakeId, @Param("userName") String name) { + return getProvider().selectUserMetaByMetalakeIdAndName(metalakeId, name); + } + + public static String insertUserMeta(@Param("userMeta") UserPO userPO) { + return getProvider().insertUserMeta(userPO); + } + + public static String insertUserMetaOnDuplicateKeyUpdate(@Param("userMeta") UserPO userPO) { + return getProvider().insertUserMetaOnDuplicateKeyUpdate(userPO); + } + + public static String softDeleteUserMetaByUserId(@Param("userId") Long userId) { + return getProvider().softDeleteUserMetaByUserId(userId); + } + + public static String softDeleteUserMetasByMetalakeId(@Param("metalakeId") Long metalakeId) { + return getProvider().softDeleteUserMetasByMetalakeId(metalakeId); + } + + public static String updateUserMeta( + @Param("newUserMeta") UserPO newUserPO, @Param("oldUserMeta") UserPO oldUserPO) { + return getProvider().updateUserMeta(newUserPO, oldUserPO); + } + + public static String listUsersByRoleId(@Param("roleId") Long roleId) { + return getProvider().listUsersByRoleId(roleId); + } + + public static String deleteUserMetasByLegacyTimeline( + @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { + return getProvider().deleteUserMetasByLegacyTimeline(legacyTimeline, limit); + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserRoleRelBaseSQLProvider.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserRoleRelBaseSQLProvider.java new file mode 100644 index 00000000000..a1e6659182d --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserRoleRelBaseSQLProvider.java @@ -0,0 +1,126 @@ +/* + * 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.UserRoleRelMapper.USER_ROLE_RELATION_TABLE_NAME; +import static org.apache.gravitino.storage.relational.mapper.UserRoleRelMapper.USER_TABLE_NAME; + +import java.util.List; +import org.apache.gravitino.storage.relational.po.UserRoleRelPO; +import org.apache.ibatis.annotations.Param; + +public class UserRoleRelBaseSQLProvider { + + public String batchInsertUserRoleRel( + @Param("userRoleRelList") List userRoleRelList) { + return ""; + } + + public String batchInsertUserRoleRelOnDuplicateKeyUpdate( + @Param("userRoleRels") List userRoleRelPOs) { + return ""; + } + + public String softDeleteUserRoleRelByUserId(@Param("userId") Long userId) { + return "UPDATE " + + USER_ROLE_RELATION_TABLE_NAME + + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0) " + + "+ EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" + + " WHERE user_id = #{userId} AND deleted_at = 0"; + } + + public String softDeleteUserRoleRelByUserAndRoles( + @Param("userId") Long userId, @Param("roleIds") List roleIds) { + return ""; + } + + public String softDeleteUserRoleRelByMetalakeId(Long metalakeId) { + return "UPDATE " + + USER_ROLE_RELATION_TABLE_NAME + + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0) " + + "+ EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" + + " WHERE user_id IN (SELECT user_id FROM " + + USER_TABLE_NAME + + " WHERE metalake_id = #{metalakeId} AND deleted_at = 0)" + + " AND deleted_at = 0"; + } + + public String softDeleteUserRoleRelByRoleId(@Param("roleId") Long roleId) { + return "UPDATE " + + USER_ROLE_RELATION_TABLE_NAME + + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0) " + + "+ EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" + + " WHERE role_id = #{roleId} AND deleted_at = 0"; + } + + public String deleteUserRoleRelMetasByLegacyTimeline( + @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { + return "DELETE FROM " + + USER_ROLE_RELATION_TABLE_NAME + + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}"; + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserRoleRelMapper.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserRoleRelMapper.java index 23c9d6702cb..5e540de8fe2 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserRoleRelMapper.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserRoleRelMapper.java @@ -21,10 +21,10 @@ import java.util.List; import org.apache.gravitino.storage.relational.po.UserRoleRelPO; -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.Update; +import org.apache.ibatis.annotations.UpdateProvider; /** * A MyBatis Mapper for table meta operation SQLs. @@ -38,101 +38,39 @@ public interface UserRoleRelMapper { String USER_TABLE_NAME = "user_meta"; String USER_ROLE_RELATION_TABLE_NAME = "user_role_rel"; - @Insert({ - "" - }) + @InsertProvider(type = UserRoleRelSQLProviderFactory.class, method = "batchInsertUserRoleRel") void batchInsertUserRoleRel(@Param("userRoleRels") List userRoleRelPOs); - @Insert({ - "" - }) + @InsertProvider( + type = UserRoleRelSQLProviderFactory.class, + method = "batchInsertUserRoleRelOnDuplicateKeyUpdate") void batchInsertUserRoleRelOnDuplicateKeyUpdate( @Param("userRoleRels") List userRoleRelPOs); - @Update( - "UPDATE " - + USER_ROLE_RELATION_TABLE_NAME - + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0) " - + "+ EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" - + " WHERE user_id = #{userId} AND deleted_at = 0") + @UpdateProvider( + type = UserRoleRelSQLProviderFactory.class, + method = "softDeleteUserRoleRelByUserId") void softDeleteUserRoleRelByUserId(@Param("userId") Long userId); - @Update({ - "" - }) + @UpdateProvider( + type = UserRoleRelSQLProviderFactory.class, + method = "softDeleteUserRoleRelByUserAndRoles") void softDeleteUserRoleRelByUserAndRoles( @Param("userId") Long userId, @Param("roleIds") List roleIds); - @Update( - "UPDATE " - + USER_ROLE_RELATION_TABLE_NAME - + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0) " - + "+ EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" - + " WHERE user_id IN (SELECT user_id FROM " - + USER_TABLE_NAME - + " WHERE metalake_id = #{metalakeId} AND deleted_at = 0)" - + " AND deleted_at = 0") - void softDeleteUserRoleRelByMetalakeId(Long metalakeId); + @UpdateProvider( + type = UserRoleRelSQLProviderFactory.class, + method = "softDeleteUserRoleRelByMetalakeId") + void softDeleteUserRoleRelByMetalakeId(@Param("metalakeId") Long metalakeId); - @Update( - "UPDATE " - + USER_ROLE_RELATION_TABLE_NAME - + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0) " - + "+ EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000" - + " WHERE role_id = #{roleId} AND deleted_at = 0") + @UpdateProvider( + type = UserRoleRelSQLProviderFactory.class, + method = "softDeleteUserRoleRelByRoleId") void softDeleteUserRoleRelByRoleId(@Param("roleId") Long roleId); - @Delete( - "DELETE FROM " - + USER_ROLE_RELATION_TABLE_NAME - + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}") + @DeleteProvider( + type = UserRoleRelSQLProviderFactory.class, + method = "deleteUserRoleRelMetasByLegacyTimeline") Integer deleteUserRoleRelMetasByLegacyTimeline( @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit); } diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserRoleRelSQLProviderFactory.java b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserRoleRelSQLProviderFactory.java new file mode 100644 index 00000000000..3d52c6aa02c --- /dev/null +++ b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/UserRoleRelSQLProviderFactory.java @@ -0,0 +1,84 @@ +/* + * 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.List; +import java.util.Map; +import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType; +import org.apache.gravitino.storage.relational.po.UserRoleRelPO; +import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper; +import org.apache.ibatis.annotations.Param; + +public class UserRoleRelSQLProviderFactory { + + private static final Map + METALAKE_META_SQL_PROVIDER_MAP = + ImmutableMap.of( + JDBCBackendType.MYSQL, new UserRoleRelMySQLProvider(), + JDBCBackendType.H2, new UserRoleRelH2Provider()); + + public static UserRoleRelBaseSQLProvider getProvider() { + String databaseId = + SqlSessionFactoryHelper.getInstance() + .getSqlSessionFactory() + .getConfiguration() + .getDatabaseId(); + + JDBCBackendType jdbcBackendType = JDBCBackendType.fromString(databaseId); + return METALAKE_META_SQL_PROVIDER_MAP.get(jdbcBackendType); + } + + static class UserRoleRelMySQLProvider extends UserRoleRelBaseSQLProvider {} + + static class UserRoleRelH2Provider extends UserRoleRelBaseSQLProvider {} + + public static String batchInsertUserRoleRel( + @Param("userRoleRels") List userRoleRelPOs) { + return getProvider().batchInsertUserRoleRel(userRoleRelPOs); + } + + public static String batchInsertUserRoleRelOnDuplicateKeyUpdate( + @Param("userRoleRels") List userRoleRelPOs) { + return getProvider().batchInsertUserRoleRelOnDuplicateKeyUpdate(userRoleRelPOs); + } + + public static String softDeleteUserRoleRelByUserId(@Param("userId") Long userId) { + return getProvider().softDeleteUserRoleRelByUserId(userId); + } + + public static String softDeleteUserRoleRelByUserAndRoles( + @Param("userId") Long userId, @Param("roleIds") List roleIds) { + return getProvider().softDeleteUserRoleRelByUserAndRoles(userId, roleIds); + } + + public static String softDeleteUserRoleRelByMetalakeId(@Param("metalakeId") Long metalakeId) { + return getProvider().softDeleteUserRoleRelByMetalakeId(metalakeId); + } + + public static String softDeleteUserRoleRelByRoleId(@Param("roleId") Long roleId) { + return getProvider().softDeleteUserRoleRelByRoleId(roleId); + } + + public static String deleteUserRoleRelMetasByLegacyTimeline( + @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { + return getProvider().deleteUserRoleRelMetasByLegacyTimeline(legacyTimeline, limit); + } +} diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/service/SchemaMetaService.java b/core/src/main/java/org/apache/gravitino/storage/relational/service/SchemaMetaService.java index f89c4c5f0f0..3e116781162 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/service/SchemaMetaService.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/service/SchemaMetaService.java @@ -211,7 +211,8 @@ public boolean deleteSchema(NameIdentifier identifier, boolean cascade) { mapper -> mapper.softDeleteTopicMetasBySchemaId(schemaId)), () -> SessionUtils.doWithoutCommit( - OwnerMetaMapper.class, mapper -> mapper.sotDeleteOwnerRelBySchemaId(schemaId))); + OwnerMetaMapper.class, + mapper -> mapper.softDeleteOwnerRelBySchemaId(schemaId))); } else { List tableEntities = TableMetaService.getInstance()