roryqi commented on code in PR #12234:
URL: https://github.com/apache/gravitino/pull/12234#discussion_r3703902723
##########
core/src/main/java/org/apache/gravitino/SupportsRelationOperations.java:
##########
@@ -217,7 +237,55 @@ default <E extends Entity & HasIdentifier> List<E>
updateEntityRelations(
NameIdentifier[] destEntitiesToAdd,
NameIdentifier[] destEntitiesToRemove)
throws IOException, NoSuchEntityException, EntityAlreadyExistsException {
+ return updateEntityRelations(
+ RelationUpdate.of(
+ relType,
+ srcEntityIdent,
+ srcEntityType,
+ toRelationEdgeTargets(relType, srcEntityType, destEntitiesToAdd),
+ toRelationEdgeTargets(relType, srcEntityType,
destEntitiesToRemove)));
+ }
+
+ /**
+ * Canonical relation update API. Implementations should add future
relation-edge update
+ * attributes to {@link RelationUpdate} or {@link RelationEdgeTarget}
instead of introducing new
+ * relation-specific overloads.
+ *
+ * @param <E> The type of the entity returned in the list, which represents
the final state of
+ * related entities.
+ * @param update The relation update.
+ * @return A list of entities that are related to the given entity after the
update.
+ * @throws IOException If a storage-related error occurs.
+ * @throws NoSuchEntityException If any of the specified entities does not
exist.
+ * @throws EntityAlreadyExistsException If a relation to be added already
exists.
+ */
+ default <E extends Entity & HasIdentifier> List<E>
updateEntityRelations(RelationUpdate update)
+ throws IOException, NoSuchEntityException, EntityAlreadyExistsException {
throw new UnsupportedOperationException(
- "updateEntityRelations is not supported by this implementation");
+ "updateEntityRelations with RelationUpdate is not supported by this
implementation");
+ }
+
+ private static RelationEdgeTarget[] toRelationEdgeTargets(
+ Type relType, Entity.EntityType srcEntityType, NameIdentifier[]
nameIdentifiers) {
+ if (nameIdentifiers == null) {
+ return null;
+ }
+
+ Entity.EntityType targetEntityType = relationDestinationType(relType,
srcEntityType);
+ return Arrays.stream(nameIdentifiers)
+ .map(nameIdentifier -> RelationEdgeTarget.of(nameIdentifier,
targetEntityType, null))
+ .toArray(RelationEdgeTarget[]::new);
+ }
+
+ private static Entity.EntityType relationDestinationType(
+ Type relType, Entity.EntityType srcEntityType) {
+ switch (relType) {
+ case POLICY_METADATA_OBJECT_REL:
Review Comment:
Clarified in `1e1f55001`. Reverse lookup still uses the same relation type:
querying metadata objects for a policy uses `POLICY_METADATA_OBJECT_REL` with
the policy as the `RelationQuery` anchor, and querying metadata objects for a
tag uses `TAG_METADATA_OBJECT_REL` with the tag as the anchor. The helper was
renamed to `relationUpdateTargetType` to make clear it is only for the legacy
update adapter target inference, not query result typing.
##########
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:
Clarified in `1e1f55001`. The clone is a defensive copy: `RelationUpdate` is
intended to be immutable, but Java arrays are mutable, so the constructor and
accessors copy arrays to prevent callers from mutating the stored update after
creation or through a returned array.
--
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]