jerryshao commented on code in PR #11074:
URL: https://github.com/apache/gravitino/pull/11074#discussion_r3246571031


##########
core/src/main/java/org/apache/gravitino/storage/relational/service/HierarchicalConventionPOStorageOps.java:
##########
@@ -0,0 +1,195 @@
+/*
+ * 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.function.UnaryOperator;
+import java.util.stream.Collectors;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.Entity;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.Namespace;
+import org.apache.gravitino.utils.HierarchicalSchemaUtil;
+
+/**
+ * Wraps a {@link BasePOStorageOps} to bridge the hierarchical schema naming 
convention. Names that
+ * appear in API form (logical separator) are translated to storage form 
(physical separator) before
+ * delegating. Two optional PO rewriters allow callers to translate a PO field 
across the boundary:
+ * the read rewriter is applied to POs returned from read methods (typically 
physical→logical), and
+ * the write rewriter is applied to POs passed into write methods (typically 
logical→physical) so
+ * the SQL still receives storage-form values.
+ *
+ * @param <PO> persistent object type
+ * @param <Mapper> MyBatis mapper type
+ */
+public class HierarchicalConventionPOStorageOps<PO, Mapper> extends 
BasePOStorageOps<PO, Mapper> {
+
+  private final BasePOStorageOps<PO, Mapper> delegate;
+  private final UnaryOperator<PO> readRewriter;
+  private final UnaryOperator<PO> writeRewriter;
+
+  public HierarchicalConventionPOStorageOps(BasePOStorageOps<PO, Mapper> 
delegate) {
+    this(delegate, UnaryOperator.identity(), UnaryOperator.identity());
+  }
+
+  public HierarchicalConventionPOStorageOps(
+      BasePOStorageOps<PO, Mapper> delegate,
+      UnaryOperator<PO> readRewriter,
+      UnaryOperator<PO> writeRewriter) {
+    this.delegate = delegate;
+    this.readRewriter = readRewriter;
+    this.writeRewriter = writeRewriter;
+  }
+
+  @Override
+  public void insertPO(Mapper mapper, PO po, boolean overwrite) {
+    delegate.insertPO(mapper, writeRewriter.apply(po), overwrite);
+  }
+
+  @Override
+  public void batchInsertPOs(Mapper mapper, List<PO> pos, boolean overwrite) {
+    delegate.batchInsertPOs(mapper, applyWrite(pos), overwrite);
+  }
+
+  @Override
+  public Integer updatePO(Mapper mapper, PO newPO, PO oldPO) {
+    return delegate.updatePO(mapper, writeRewriter.apply(newPO), 
writeRewriter.apply(oldPO));
+  }
+
+  @Override
+  public PO getPO(Mapper mapper, Long parentId, String name) {
+    return applyRead(delegate.getPO(mapper, parentId, 
toPhysicalIfHierarchical(name)));
+  }
+
+  @Override
+  public List<PO> listPOs(Mapper mapper, Long parentId) {
+    return applyRead(delegate.listPOs(mapper, parentId));
+  }
+
+  @Override
+  public List<PO> listPOs(Mapper mapper, Namespace namespace, List<String> 
names) {
+    Namespace storageNs = apiNamespaceToStorage(namespace);
+    List<String> storageNames =
+        names.stream()
+            .map(HierarchicalConventionPOStorageOps::toPhysicalIfHierarchical)
+            .collect(Collectors.toList());
+    return applyRead(delegate.listPOs(mapper, storageNs, storageNames));
+  }
+
+  @Override
+  public List<PO> listPOs(Mapper mapper, List<Long> uuids) {
+    return applyRead(delegate.listPOs(mapper, uuids));
+  }
+
+  @Override
+  protected PO getPOByFullName(Mapper mapper, NameIdentifier identifier) {
+    return applyRead(delegate.getPOByFullName(mapper, 
apiIdentifierToStorage(identifier)));
+  }
+
+  @Override
+  protected List<PO> listPOsByNSFullName(Mapper mapper, Namespace namespace) {
+    return applyRead(delegate.listPOsByNSFullName(mapper, 
apiNamespaceToStorage(namespace)));
+  }
+
+  @Override
+  public List<Capability> capabilities() {
+    return delegate.capabilities();
+  }
+
+  @Override
+  protected Entity.EntityType entityType() {
+    return delegate.entityType();
+  }
+
+  private PO applyRead(PO po) {
+    return po == null ? null : readRewriter.apply(po);
+  }
+
+  private List<PO> applyRead(List<PO> pos) {
+    if (pos == null || pos.isEmpty()) {
+      return pos;
+    }
+    return pos.stream().map(this::applyRead).collect(Collectors.toList());
+  }
+
+  private List<PO> applyWrite(List<PO> pos) {
+    if (pos == null || pos.isEmpty()) {
+      return pos;
+    }
+    return pos.stream().map(writeRewriter).collect(Collectors.toList());
+  }
+
+  private static String toPhysicalIfHierarchical(String name) {
+    if (StringUtils.isBlank(name)) {
+      return name;
+    }
+    String sep = HierarchicalSchemaUtil.schemaSeparator();
+    if (!name.contains(sep)) {
+      return name;
+    }
+    return HierarchicalSchemaUtil.logicalToPhysical(name, sep);
+  }
+
+  private static NameIdentifier apiIdentifierToStorage(NameIdentifier 
apiIdentifier) {

Review Comment:
   What is `apiIdentifier`, can you figure out a better name ?



-- 
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]

Reply via email to