yuqi1129 commented on code in PR #11848: URL: https://github.com/apache/gravitino/pull/11848#discussion_r3527012962
########## core/src/main/java/org/apache/gravitino/SupportsExternalIdOperations.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; + +import java.io.IOException; +import java.util.function.Function; +import org.apache.gravitino.exceptions.NoSuchEntityException; + +/** + * Optional extension for entity stores that support lookup and mutation by external id within a + * namespace. + */ +public interface SupportsExternalIdOperations { + + /** + * Get the entity from the underlying storage by external id. + * + * @param ident the external id name identifier + * @param entityType the general type of the entity + * @param type the detailed type of the entity + * @param <E> the class of entity + * @return the entity retrieved from the underlying storage + * @throws NoSuchEntityException if the entity does not exist + * @throws IOException if the retrieve operation fails + */ + <E extends Entity & HasIdentifier> E getByExternalId( + NameIdentifier ident, Entity.EntityType entityType, Class<E> type) + throws NoSuchEntityException, IOException; + + /** + * Update an entity by external id. + * + * @param ident the external id name identifier + * @param entityType the general type of the entity + * @param type the detailed type of the entity + * @param updater the updater function to update the entity + * @param <E> the class of entity + * @return the updated entity + * @throws NoSuchEntityException if the entity does not exist + * @throws IOException if the update operation fails + */ + <E extends Entity & HasIdentifier> E updateByExternalId( + NameIdentifier ident, Entity.EntityType entityType, Class<E> type, Function<E, E> updater) + throws NoSuchEntityException, IOException; + + /** + * Delete an entity by external id. + * + * @param ident the external id name identifier + * @param entityType the general type of the entity + * @return true if the entity was deleted Review Comment: I noticed that `NoSuchEntityException` will be caught and return `false` instead; maybe you need to update the Java documentation. ########## core/src/main/java/org/apache/gravitino/Entity.java: ########## @@ -41,6 +41,9 @@ public interface Entity extends Serializable { /** The user schema name in the system catalog. */ String USER_SCHEMA_NAME = "user"; + /** The lock-only schema name for user operations keyed by external id. */ Review Comment: I can't get the meaning of `lock-only schema name`. Can you make it clearer? ########## api/src/main/java/org/apache/gravitino/authorization/User.java: ########## @@ -34,6 +35,25 @@ public interface User extends Auditable { */ String name(); + /** + * The external identifier assigned by an upstream system, or null if not set. Review Comment: Can you add more information here about `external identifier`? - What's it used for? - Why do we need it? ########## core/src/main/java/org/apache/gravitino/storage/relational/po/UserPO.java: ########## @@ -138,6 +162,7 @@ private void validate() { Preconditions.checkArgument(userPO.currentVersion != null, "Current version is required"); Preconditions.checkArgument(userPO.lastVersion != null, "Last version is required"); Preconditions.checkArgument(userPO.deletedAt != null, "Deleted at is required"); + Preconditions.checkArgument(userPO.enabled != null, "Enabled is required"); Review Comment: Why do we need to check the status of `enabled`? I believe it should be true if unset. ########## core/src/main/java/org/apache/gravitino/authorization/AccessControlDispatcher.java: ########## @@ -63,6 +78,19 @@ User addUser(String metalake, String user) */ boolean removeUser(String metalake, String user) throws NoSuchMetalakeException; + /** + * Removes a User by external identifier. + * + * @param metalake The Metalake of the User. + * @param externalId The external identifier of the User. + * @return True if the User was removed, false otherwise. + * @throws NoSuchUserException If the User with the given external id does not exist. Review Comment: You never throw such an exception for the removal method. ########## core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java: ########## @@ -181,6 +185,44 @@ public <E extends Entity & HasIdentifier> E get( }); } + @Override + public SupportsExternalIdOperations externalIdOperations() { + return this; + } + + @Override + public <E extends Entity & HasIdentifier> E getByExternalId( + NameIdentifier ident, Entity.EntityType entityType, Class<E> type) + throws NoSuchEntityException, IOException { + return backend.getByExternalId(ident, entityType); + } + + @Override + public <E extends Entity & HasIdentifier> E updateByExternalId( + NameIdentifier ident, Entity.EntityType entityType, Class<E> type, Function<E, E> updater) + throws NoSuchEntityException, IOException { + E updatedEntity = backend.updateByExternalId(ident, entityType, updater); + cache.invalidate(updatedEntity.nameIdentifier(), entityType); + return updatedEntity; + } + + @Override + public boolean deleteByExternalId(NameIdentifier ident, Entity.EntityType entityType) + throws NoSuchEntityException, IOException { + NameIdentifier nameIdent = null; + try { + HasIdentifier entity = backend.getByExternalId(ident, entityType); + nameIdent = entity.nameIdentifier(); + return backend.delete(nameIdent, entityType, false); + } catch (NoSuchEntityException e) { + return false; Review Comment: It's better to add a log here. -- 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]
