-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#4868] improvement(core): Modify tag and user related mapper class t…
…o use the new framework to support multiple JDBC backend. (#4869) ### What changes were proposed in this pull request? Change tag, user, and role-related code to support the new framework introduced by #4832 ### Why are the changes needed? To support multiple JDBC backend. Fix: #4868 ### Does this PR introduce _any_ user-facing change? N/A. ### How was this patch tested? Existing UTs and ITs.
- Loading branch information
Showing
28 changed files
with
2,339 additions
and
915 deletions.
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
...rc/main/java/org/apache/gravitino/storage/relational/mapper/GroupMetaBaseSQLProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...main/java/org/apache/gravitino/storage/relational/mapper/GroupMetaSQLProviderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.gravitino.storage.relational.mapper; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import java.util.Map; | ||
import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType; | ||
import org.apache.gravitino.storage.relational.po.GroupPO; | ||
import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper; | ||
import org.apache.ibatis.annotations.Param; | ||
|
||
public class GroupMetaSQLProviderFactory { | ||
private static final Map<JDBCBackendType, GroupMetaBaseSQLProvider> | ||
METALAKE_META_SQL_PROVIDER_MAP = | ||
ImmutableMap.of( | ||
JDBCBackendType.MYSQL, new GroupMetaMySQLProvider(), | ||
JDBCBackendType.H2, new GroupMetaH2Provider()); | ||
|
||
public static GroupMetaBaseSQLProvider getProvider() { | ||
String databaseId = | ||
SqlSessionFactoryHelper.getInstance() | ||
.getSqlSessionFactory() | ||
.getConfiguration() | ||
.getDatabaseId(); | ||
|
||
JDBCBackendType jdbcBackendType = JDBCBackendType.fromString(databaseId); | ||
return METALAKE_META_SQL_PROVIDER_MAP.get(jdbcBackendType); | ||
} | ||
|
||
static class GroupMetaMySQLProvider extends GroupMetaBaseSQLProvider {} | ||
|
||
static class GroupMetaH2Provider extends GroupMetaBaseSQLProvider {} | ||
|
||
public static String selectGroupIdBySchemaIdAndName( | ||
@Param("metalakeId") Long metalakeId, @Param("groupName") String name) { | ||
return getProvider().selectGroupIdBySchemaIdAndName(metalakeId, name); | ||
} | ||
|
||
public static String selectGroupMetaByMetalakeIdAndName( | ||
@Param("metalakeId") Long metalakeId, @Param("groupName") String name) { | ||
return getProvider().selectGroupMetaByMetalakeIdAndName(metalakeId, name); | ||
} | ||
|
||
public static String insertGroupMeta(@Param("groupMeta") GroupPO groupPO) { | ||
return getProvider().insertGroupMeta(groupPO); | ||
} | ||
|
||
public static String insertGroupMetaOnDuplicateKeyUpdate(@Param("groupMeta") GroupPO groupPO) { | ||
return getProvider().insertGroupMetaOnDuplicateKeyUpdate(groupPO); | ||
} | ||
|
||
public static String softDeleteGroupMetaByGroupId(@Param("groupId") Long groupId) { | ||
return getProvider().softDeleteGroupMetaByGroupId(groupId); | ||
} | ||
|
||
public static String softDeleteGroupMetasByMetalakeId(@Param("metalakeId") Long metalakeId) { | ||
return getProvider().softDeleteGroupMetasByMetalakeId(metalakeId); | ||
} | ||
|
||
public static String updateGroupMeta( | ||
@Param("newGroupMeta") GroupPO newGroupPO, @Param("oldGroupMeta") GroupPO oldGroupPO) { | ||
return getProvider().updateGroupMeta(newGroupPO, oldGroupPO); | ||
} | ||
|
||
public static String listGroupsByRoleId(@Param("roleId") Long roleId) { | ||
return getProvider().listGroupsByRoleId(roleId); | ||
} | ||
|
||
public static String deleteGroupMetasByLegacyTimeline( | ||
@Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) { | ||
return getProvider().deleteGroupMetasByLegacyTimeline(legacyTimeline, limit); | ||
} | ||
} |
Oops, something went wrong.