jerqi commented on code in PR #7734:
URL: https://github.com/apache/gravitino/pull/7734#discussion_r2275255631


##########
core/src/main/java/org/apache/gravitino/storage/relational/JDBCBackend.java:
##########
@@ -569,6 +593,103 @@ public <E extends Entity & HasIdentifier> List<E> 
updateEntityRelations(
     }
   }
 
+  @Override
+  public int deleteEntitiesAndRelations(
+      Type relType, Relation.VertexType deleteVertexType, List<Relation> 
relations)
+      throws IOException {
+    switch (relType) {
+      case METADATA_OBJECT_STAT_REL:
+        Preconditions.checkArgument(
+            deleteVertexType == Relation.VertexType.DESTINATION,
+            "Only supports to delete relations of destination vertex");
+
+        AtomicInteger deleteCount = new AtomicInteger(0);
+        Map<Pair<NameIdentifier, Entity.EntityType>, List<Relation>> 
groupedRelations =
+            
relations.stream().collect(Collectors.groupingBy(Relation::getSourceVertex));
+
+        if (groupedRelations.isEmpty()) {
+          return 0; // No relations to delete.
+        }
+
+        Preconditions.checkArgument(
+            groupedRelations.size() == 1,
+            "The relations must have the same source identifier and type, but 
got %s groups",
+            groupedRelations.size());
+
+        groupedRelations.forEach(
+            (sourceVertex, deleteRelations) -> {
+              List<String> statsToDelete =
+                  deleteRelations.stream()
+                      .map(relation -> relation.getDestIdent().name())
+                      .collect(Collectors.toList());
+
+              deleteRelations.forEach(
+                  relation ->
+                      Preconditions.checkArgument(
+                          relation.getDestType() == STATISTIC,
+                          "The destination type of relation must be STATISTIC, 
but got %s",
+                          relation.getDestType()));
+
+              deleteCount.addAndGet(
+                  StatisticMetaService.getInstance()
+                      .batchDeleteStatisticPOs(
+                          sourceVertex.getLeft(), sourceVertex.getRight(), 
statsToDelete));
+            });
+        return deleteCount.get();
+      default:
+        throw new IllegalArgumentException(
+            String.format("Doesn't support the relation type %s", relType));
+    }
+  }
+
+  @Override
+  public <E extends Entity & HasIdentifier> Void insertEntitiesAndRelations(
+      Type relType, List<Entity.RelationalEntity<E>> entities, boolean 
overwrite)
+      throws IOException {
+    switch (relType) {
+      case METADATA_OBJECT_STAT_REL:
+        Preconditions.checkArgument(
+            overwrite, "The overwrite must be true for metadata object stats 
relation");
+
+        StatisticMetaService metaService = StatisticMetaService.getInstance();
+        List<StatisticEntity> statisticEntities = Lists.newArrayList();
+
+        Set<NameIdentifier> relatedIdents = Sets.newHashSet();
+        Set<Entity.EntityType> relatedEntityTypes = Sets.newHashSet();
+
+        for (Entity.RelationalEntity<E> relEntity : entities) {
+          Preconditions.checkArgument(
+              relEntity.relatedNameIdentifiers().size() == 1,
+              "Each entity must have exactly one related identifier");
+          NameIdentifier relatedIdent = 
relEntity.relatedNameIdentifiers().get(0);
+          relatedIdents.add(relatedIdent);
+          relatedEntityTypes.add(relEntity.relatedEntityType());
+          Preconditions.checkArgument(
+              relEntity.entity() instanceof StatisticEntity,
+              "The entity must be a StatisticEntity");
+          statisticEntities.add((StatisticEntity) relEntity.entity());
+        }
+
+        Preconditions.checkArgument(
+            relatedIdents.size() == 1,
+            "All entities must have the same related identifier, but got %s",
+            relatedIdents.size());
+
+        Preconditions.checkArgument(
+            relatedEntityTypes.size() == 1,
+            "All entities must have the same related entity type, but got %s",
+            relatedEntityTypes.size());
+
+        Entity.EntityType type = relatedEntityTypes.iterator().next();
+        metaService.batchInsertStatisticPOsOnDuplicateKeyUpdate(
+            statisticEntities, relatedIdents.iterator().next(), type);
+        return null;

Review Comment:
   The treeLock can't accept the method return `void`. So I used a method 
return `Void`. I returned null 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: commits-unsubscr...@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to