Copilot commented on code in PR #11057: URL: https://github.com/apache/gravitino/pull/11057#discussion_r3226210665
########## plugins/idp-basic/src/main/java/org/apache/gravitino/storage/provider/IdpBasicUserMetaProvider.java: ########## @@ -0,0 +1,124 @@ +/* + * 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.provider; + +import com.google.common.base.Preconditions; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import org.apache.gravitino.storage.relational.mapper.IdpGroupUserRelMapper; +import org.apache.gravitino.storage.relational.mapper.IdpUserMetaMapper; +import org.apache.gravitino.storage.relational.po.IdpUserPO; +import org.apache.gravitino.storage.relational.utils.SessionUtils; + +/** The provider class for user metadata. It provides the basic database operations for user. */ +public class IdpBasicUserMetaProvider implements IdpUserMetaProvider { + private static final IdpBasicUserMetaProvider INSTANCE = new IdpBasicUserMetaProvider(); + + public static IdpBasicUserMetaProvider getInstance() { + return INSTANCE; + } + + public IdpBasicUserMetaProvider() {} + + @Override + public Optional<IdpUserPO> findUser(String userName) { + return Optional.ofNullable( + SessionUtils.getWithoutCommit( + IdpUserMetaMapper.class, mapper -> mapper.selectIdpUser(userName))); + } + + @Override + public List<IdpUserPO> findUsers(List<String> userNames) { + if (userNames.isEmpty()) { + return Collections.emptyList(); + } + + return SessionUtils.getWithoutCommit( + IdpUserMetaMapper.class, mapper -> mapper.selectIdpUsers(userNames)); + } Review Comment: `findUsers` calls `userNames.isEmpty()` without a null check, which will throw a NullPointerException if callers pass null. Either treat null as an empty list (return `Collections.emptyList()`) or validate with `Preconditions.checkArgument(userNames != null, ...)` before accessing it. ########## plugins/idp-basic/src/main/java/org/apache/gravitino/storage/provider/IdpBasicGroupMetaProvider.java: ########## @@ -0,0 +1,124 @@ +/* + * 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.provider; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import org.apache.gravitino.storage.relational.mapper.IdpGroupMetaMapper; +import org.apache.gravitino.storage.relational.mapper.IdpGroupUserRelMapper; +import org.apache.gravitino.storage.relational.po.IdpGroupPO; +import org.apache.gravitino.storage.relational.po.IdpGroupUserRelPO; +import org.apache.gravitino.storage.relational.utils.SessionUtils; + +/** The provider class for group metadata. It provides the basic database operations for group. */ +public class IdpBasicGroupMetaProvider implements IdpGroupMetaProvider { + private static final IdpBasicGroupMetaProvider INSTANCE = new IdpBasicGroupMetaProvider(); + + public static IdpBasicGroupMetaProvider getInstance() { + return INSTANCE; + } + + public IdpBasicGroupMetaProvider() {} + + @Override + public Optional<IdpGroupPO> findGroup(String groupName) { + return Optional.ofNullable( + SessionUtils.getWithoutCommit( + IdpGroupMetaMapper.class, mapper -> mapper.selectIdpGroup(groupName))); + } + + @Override + public List<String> listUserNames(String groupName) { + Optional<IdpGroupPO> group = findGroup(groupName); + if (!group.isPresent()) { + return Collections.emptyList(); + } + + return SessionUtils.getWithoutCommit( + IdpGroupUserRelMapper.class, + mapper -> mapper.selectUserNamesByGroupId(group.get().getGroupId())); + } + + @Override + public void createGroup(IdpGroupPO groupPO) { + SessionUtils.doWithCommit(IdpGroupMetaMapper.class, mapper -> mapper.insertIdpGroup(groupPO)); + } + + @Override + public boolean deleteGroup(IdpGroupPO groupPO, Long deletedAt) { + SessionUtils.doMultipleWithCommit( + () -> + SessionUtils.doWithoutCommit( + IdpGroupMetaMapper.class, + mapper -> mapper.softDeleteIdpGroup(groupPO.getGroupId(), deletedAt)), + () -> + SessionUtils.doWithoutCommit( + IdpGroupUserRelMapper.class, + mapper -> mapper.softDeleteGroupUsersByGroupId(groupPO.getGroupId(), deletedAt))); + return true; + } + + @Override + public List<Long> selectRelatedUserIds(Long groupId, List<Long> userIds) { + return SessionUtils.getWithoutCommit( + IdpGroupUserRelMapper.class, mapper -> mapper.selectRelatedUserIds(groupId, userIds)); + } + + @Override + public void addUsersToGroup(List<IdpGroupUserRelPO> relations) { + if (relations.isEmpty()) { + return; + } + SessionUtils.doWithCommit( + IdpGroupUserRelMapper.class, mapper -> mapper.batchInsertIdpGroupUsers(relations)); + } Review Comment: `addUsersToGroup` calls `relations.isEmpty()` without a null check, which can throw a NullPointerException if callers pass null. Consider treating null as empty or validating the input explicitly before the emptiness check. ########## plugins/idp-basic/src/main/java/org/apache/gravitino/storage/provider/IdpBasicGroupMetaProvider.java: ########## @@ -0,0 +1,124 @@ +/* + * 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.provider; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import org.apache.gravitino.storage.relational.mapper.IdpGroupMetaMapper; +import org.apache.gravitino.storage.relational.mapper.IdpGroupUserRelMapper; +import org.apache.gravitino.storage.relational.po.IdpGroupPO; +import org.apache.gravitino.storage.relational.po.IdpGroupUserRelPO; +import org.apache.gravitino.storage.relational.utils.SessionUtils; + +/** The provider class for group metadata. It provides the basic database operations for group. */ +public class IdpBasicGroupMetaProvider implements IdpGroupMetaProvider { + private static final IdpBasicGroupMetaProvider INSTANCE = new IdpBasicGroupMetaProvider(); + + public static IdpBasicGroupMetaProvider getInstance() { + return INSTANCE; + } + + public IdpBasicGroupMetaProvider() {} + + @Override + public Optional<IdpGroupPO> findGroup(String groupName) { + return Optional.ofNullable( + SessionUtils.getWithoutCommit( + IdpGroupMetaMapper.class, mapper -> mapper.selectIdpGroup(groupName))); + } + + @Override + public List<String> listUserNames(String groupName) { + Optional<IdpGroupPO> group = findGroup(groupName); + if (!group.isPresent()) { + return Collections.emptyList(); + } + + return SessionUtils.getWithoutCommit( + IdpGroupUserRelMapper.class, + mapper -> mapper.selectUserNamesByGroupId(group.get().getGroupId())); + } + + @Override + public void createGroup(IdpGroupPO groupPO) { + SessionUtils.doWithCommit(IdpGroupMetaMapper.class, mapper -> mapper.insertIdpGroup(groupPO)); + } + + @Override + public boolean deleteGroup(IdpGroupPO groupPO, Long deletedAt) { + SessionUtils.doMultipleWithCommit( + () -> + SessionUtils.doWithoutCommit( + IdpGroupMetaMapper.class, + mapper -> mapper.softDeleteIdpGroup(groupPO.getGroupId(), deletedAt)), + () -> + SessionUtils.doWithoutCommit( + IdpGroupUserRelMapper.class, + mapper -> mapper.softDeleteGroupUsersByGroupId(groupPO.getGroupId(), deletedAt))); + return true; + } + + @Override + public List<Long> selectRelatedUserIds(Long groupId, List<Long> userIds) { + return SessionUtils.getWithoutCommit( + IdpGroupUserRelMapper.class, mapper -> mapper.selectRelatedUserIds(groupId, userIds)); + } + + @Override + public void addUsersToGroup(List<IdpGroupUserRelPO> relations) { + if (relations.isEmpty()) { + return; + } + SessionUtils.doWithCommit( + IdpGroupUserRelMapper.class, mapper -> mapper.batchInsertIdpGroupUsers(relations)); + } + + @Override + public void removeUsersFromGroup(Long groupId, List<Long> userIds, Long deletedAt) { + if (userIds.isEmpty()) { + return; + } + + SessionUtils.doWithCommit( + IdpGroupUserRelMapper.class, + mapper -> mapper.softDeleteIdpGroupUsers(groupId, userIds, deletedAt)); Review Comment: `removeUsersFromGroup` calls `userIds.isEmpty()` without a null check, which can throw a NullPointerException if callers pass null. Consider treating null as empty (no-op) or validating the input explicitly. ########## core/src/main/java/org/apache/gravitino/storage/relational/service/IdpUserMetaService.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.service; + +import java.util.List; +import java.util.Optional; +import org.apache.gravitino.storage.relational.provider.IdpMetaProviderLoader; +import org.apache.gravitino.storage.relational.provider.IdpUserMetaProvider; + +/** Core facade for built-in IdP user metadata operations via plugin implementation. */ +public class IdpUserMetaService<U> implements IdpUserMetaProvider<U> { + private static final IdpUserMetaService<?> INSTANCE = new IdpUserMetaService<>(); + + /** + * Returns the singleton IdP user metadata service. + * + * @param <U> the user metadata type + * @return the singleton service + */ + @SuppressWarnings("unchecked") + public static <U> IdpUserMetaService<U> getInstance() { + return (IdpUserMetaService<U>) INSTANCE; + } + + private IdpUserMetaService() {} + + /** + * Find a built-in IdP user by name. + * + * @param userName the user name + * @return the matched user if present + */ + @Override + public Optional<U> findUser(String userName) { + return provider().findUser(userName); + } + + /** + * Find built-in IdP users by names. + * + * @param userNames the user names + * @return the matched users + */ + @Override + public List<U> findUsers(List<String> userNames) { + return provider().findUsers(userNames); + } + + /** + * List the groups of a built-in IdP user. + * + * @param userName the user name + * @return the group names + */ + @Override + public List<String> listGroupNames(String userName) { + return provider().listGroupNames(userName); + } + + /** + * Create a built-in IdP user. + * + * @param userMeta the user metadata + */ + @Override + public void createUser(U userMeta) { + provider().createUser(userMeta); + } + + /** + * Update the password of a built-in IdP user. + * + * @param userMeta the current user metadata + * @param passwordHash the new password hash + * @param nextVersion the next version + */ + @Override + public void updatePassword(U userMeta, String passwordHash, Long nextVersion) { + provider().updatePassword(userMeta, passwordHash, nextVersion); + } + + /** + * Soft delete a built-in IdP user. + * + * @param userMeta the user metadata + * @param deletedAt the deletion timestamp + * @return true if the delete succeeded + */ + @Override + public boolean deleteUser(U userMeta, Long deletedAt) { + return provider().deleteUser(userMeta, deletedAt); + } + + /** + * Hard deletes legacy built-in IdP user metadata records. + * + * @param legacyTimeline delete records older than this timeline + * @param limit maximum number of records to delete per invocation + * @return the number of deleted records + */ + @Override + public int deleteUserMetasByLegacyTimeline(long legacyTimeline, int limit) { + return provider().deleteUserMetasByLegacyTimeline(legacyTimeline, limit); + } + + @SuppressWarnings("unchecked") + private IdpUserMetaProvider<U> provider() { + return (IdpUserMetaProvider<U>) IdpMetaProviderLoader.loadService(IdpUserMetaProvider.class); + } Review Comment: `provider()` calls `ServiceLoader.load(...)` on every service method invocation, which can be relatively expensive and may repeatedly instantiate providers. Consider caching the loaded provider (e.g., lazy-initialized volatile field) since this service expects exactly one implementation on the classpath. ########## plugins/idp-basic/src/test/java/org/apache/gravitino/storage/provider/TestIdpBasicGroupMetaProvider.java: ########## @@ -0,0 +1,50 @@ +/* + * 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.provider; + +import java.util.List; +import java.util.ServiceLoader; +import java.util.stream.Collectors; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class TestIdpBasicGroupMetaProvider { + + @Test + public void testSingletonAndTypeBridge() { + IdpBasicGroupMetaProvider provider = IdpBasicGroupMetaProvider.getInstance(); + + Assertions.assertSame(provider, IdpBasicGroupMetaProvider.getInstance()); + Assertions.assertInstanceOf(IdpGroupMetaProvider.class, provider); + Assertions.assertInstanceOf( + org.apache.gravitino.storage.relational.provider.IdpGroupMetaProvider.class, provider); + } + + @Test + public void testServiceLoaderRegistration() { + List<Class<?>> providerClasses = + ServiceLoader.load( + org.apache.gravitino.storage.relational.provider.IdpGroupMetaProvider.class) + .stream() + .map(ServiceLoader.Provider::type) + .collect(Collectors.toList()); Review Comment: Avoid using fully-qualified class names inside methods/tests; add an import for `org.apache.gravitino.storage.relational.provider.IdpGroupMetaProvider` and use the simple type name for readability and consistency (both in `assertInstanceOf` and `ServiceLoader.load(...)`). ########## core/src/main/java/org/apache/gravitino/storage/relational/service/IdpGroupMetaService.java: ########## @@ -0,0 +1,139 @@ +/* + * 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.service; + +import java.util.List; +import java.util.Optional; +import org.apache.gravitino.storage.relational.provider.IdpGroupMetaProvider; +import org.apache.gravitino.storage.relational.provider.IdpMetaProviderLoader; + +/** Core facade for built-in IdP group metadata operations via plugin implementation. */ +public class IdpGroupMetaService<G, R> implements IdpGroupMetaProvider<G, R> { + private static final IdpGroupMetaService<?, ?> INSTANCE = new IdpGroupMetaService<>(); + + /** + * Returns the singleton IdP group metadata service. + * + * @param <G> the group metadata type + * @param <R> the group-user relation metadata type + * @return the singleton service + */ + @SuppressWarnings("unchecked") + public static <G, R> IdpGroupMetaService<G, R> getInstance() { + return (IdpGroupMetaService<G, R>) INSTANCE; + } + + private IdpGroupMetaService() {} + + /** + * Find a built-in IdP group by name. + * + * @param groupName the group name + * @return the matched group if present + */ + @Override + public Optional<G> findGroup(String groupName) { + return provider().findGroup(groupName); + } + + /** + * List the users of a built-in IdP group. + * + * @param groupName the group name + * @return the user names + */ + @Override + public List<String> listUserNames(String groupName) { + return provider().listUserNames(groupName); + } + + /** + * Create a built-in IdP group. + * + * @param groupMeta the group metadata + */ + @Override + public void createGroup(G groupMeta) { + provider().createGroup(groupMeta); + } + + /** + * Soft delete a built-in IdP group. + * + * @param groupMeta the group metadata + * @param deletedAt the deletion timestamp + * @return true if the delete succeeded + */ + @Override + public boolean deleteGroup(G groupMeta, Long deletedAt) { + return provider().deleteGroup(groupMeta, deletedAt); + } + + /** + * Select user ids already related to the group. + * + * @param groupId the group id + * @param userIds the candidate user ids + * @return the related user ids + */ + @Override + public List<Long> selectRelatedUserIds(Long groupId, List<Long> userIds) { + return provider().selectRelatedUserIds(groupId, userIds); + } + + /** + * Add users to a built-in IdP group. + * + * @param relations the group-user relations + */ + @Override + public void addUsersToGroup(List<R> relations) { + provider().addUsersToGroup(relations); + } + + /** + * Remove users from a built-in IdP group. + * + * @param groupId the group id + * @param userIds the user ids to remove + * @param deletedAt the deletion timestamp + */ + @Override + public void removeUsersFromGroup(Long groupId, List<Long> userIds, Long deletedAt) { + provider().removeUsersFromGroup(groupId, userIds, deletedAt); + } + + /** + * Hard deletes legacy built-in IdP group metadata records. + * + * @param legacyTimeline delete records older than this timeline + * @param limit maximum number of records to delete per invocation + * @return the number of deleted records + */ + @Override + public int deleteGroupMetasByLegacyTimeline(long legacyTimeline, int limit) { + return provider().deleteGroupMetasByLegacyTimeline(legacyTimeline, limit); + } + + @SuppressWarnings("unchecked") + private IdpGroupMetaProvider<G, R> provider() { + return (IdpGroupMetaProvider<G, R>) + IdpMetaProviderLoader.loadService(IdpGroupMetaProvider.class); + } Review Comment: `provider()` calls `ServiceLoader.load(...)` on every service method invocation, which can be relatively expensive and may repeatedly instantiate providers. Consider caching the loaded provider (e.g., lazy-initialized volatile field) since this service expects exactly one implementation on the classpath. ########## plugins/idp-basic/src/test/java/org/apache/gravitino/storage/provider/TestIdpBasicUserMetaProvider.java: ########## @@ -0,0 +1,50 @@ +/* + * 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.provider; + +import java.util.List; +import java.util.ServiceLoader; +import java.util.stream.Collectors; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class TestIdpBasicUserMetaProvider { + + @Test + public void testSingletonAndTypeBridge() { + IdpBasicUserMetaProvider provider = IdpBasicUserMetaProvider.getInstance(); + + Assertions.assertSame(provider, IdpBasicUserMetaProvider.getInstance()); + Assertions.assertInstanceOf(IdpUserMetaProvider.class, provider); + Assertions.assertInstanceOf( + org.apache.gravitino.storage.relational.provider.IdpUserMetaProvider.class, provider); + } + + @Test + public void testServiceLoaderRegistration() { + List<Class<?>> providerClasses = + ServiceLoader.load( + org.apache.gravitino.storage.relational.provider.IdpUserMetaProvider.class) + .stream() + .map(ServiceLoader.Provider::type) + .collect(Collectors.toList()); Review Comment: Avoid using fully-qualified class names inside methods/tests; it makes the code harder to read and conflicts with the repo’s usual import-based style. Add an import for `org.apache.gravitino.storage.relational.provider.IdpUserMetaProvider` and use the simple type name in both the `assertInstanceOf` and `ServiceLoader.load(...)` calls. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
