roryqi commented on code in PR #12234: URL: https://github.com/apache/gravitino/pull/12234#discussion_r3704227853
########## core/src/main/java/org/apache/gravitino/RelationUpdate.java: ########## @@ -0,0 +1,129 @@ +/* + * 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 com.google.common.base.Preconditions; +import java.util.Arrays; + +/** + * Describes a relation update from a source entity to target endpoints. Target endpoint identity + * and relation-edge attributes are carried by {@link RelationEdgeTarget}. + */ +public final class RelationUpdate { + + private static final RelationEdgeTarget[] EMPTY_TARGETS = new RelationEdgeTarget[0]; + + private final SupportsRelationOperations.Type relationType; + private final NameIdentifier sourceIdentifier; + private final Entity.EntityType sourceEntityType; + private final RelationEdgeTarget[] targetsToAdd; + private final RelationEdgeTarget[] targetsToRemove; + + private RelationUpdate( + SupportsRelationOperations.Type relationType, + NameIdentifier sourceIdentifier, + Entity.EntityType sourceEntityType, + RelationEdgeTarget[] targetsToAdd, + RelationEdgeTarget[] targetsToRemove) { + this.relationType = relationType; + this.sourceIdentifier = sourceIdentifier; + this.sourceEntityType = sourceEntityType; + this.targetsToAdd = copyTargets(targetsToAdd, "targetsToAdd"); + this.targetsToRemove = copyTargets(targetsToRemove, "targetsToRemove"); + } + + /** + * Creates a relation update. + * + * @param relationType The type of relation. + * @param sourceIdentifier The identifier of the source entity whose relations are being updated. + * @param sourceEntityType The source entity type. + * @param targetsToAdd Target endpoints to associate with the source entity. + * @param targetsToRemove Target endpoints to disassociate from the source entity. + * @return A relation update. + */ + public static RelationUpdate of( + SupportsRelationOperations.Type relationType, + NameIdentifier sourceIdentifier, + Entity.EntityType sourceEntityType, + RelationEdgeTarget[] targetsToAdd, + RelationEdgeTarget[] targetsToRemove) { + Preconditions.checkArgument(relationType != null, "relationType must not be null"); + Preconditions.checkArgument(sourceIdentifier != null, "sourceIdentifier must not be null"); + Preconditions.checkArgument(sourceEntityType != null, "sourceEntityType must not be null"); + return new RelationUpdate( + relationType, sourceIdentifier, sourceEntityType, targetsToAdd, targetsToRemove); + } + + /** + * @return The type of relation. + */ + public SupportsRelationOperations.Type relationType() { + return relationType; + } + + /** + * @return The source entity identifier. + */ + public NameIdentifier sourceIdentifier() { + return sourceIdentifier; + } + + /** + * @return The source entity type. + */ + public Entity.EntityType sourceEntityType() { + return sourceEntityType; + } + + /** + * @return Target endpoints to associate with the source entity. + */ + public RelationEdgeTarget[] targetsToAdd() { + return targetsToAdd.clone(); + } + + /** + * @return Target endpoints to disassociate from the source entity. + */ + public RelationEdgeTarget[] targetsToRemove() { + return targetsToRemove.clone(); Review Comment: Correct, this is a shallow array copy. For this API that is sufficient because `RelationEdgeTarget` is immutable: it is final, has private final fields, a private constructor, no mutators, and only contains immutable endpoint/value data. I clarified the Javadocs in `0662d7171` to say the copy protects the array container from external mutation, and added a unit test that mutating the input arrays or returned arrays does not affect the stored `RelationUpdate`. -- 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]
