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


##########
core/src/main/java/org/apache/gravitino/storage/relational/mapper/StatisticSQLProviderFactory.java:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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.mapper;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.gravitino.storage.relational.JDBCBackend;
+import 
org.apache.gravitino.storage.relational.mapper.provider.base.StatisticBaseSQLProvider;
+import org.apache.gravitino.storage.relational.po.StatisticPO;
+import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper;
+import org.apache.ibatis.annotations.Param;
+
+public class StatisticSQLProviderFactory {
+
+  static class StatisticMySQLProvider extends StatisticBaseSQLProvider {}
+
+  static class StatisticH2Provider extends StatisticBaseSQLProvider {}
+
+  static class StatisticPostgresSQLProvider extends StatisticBaseSQLProvider {
+    @Override
+    protected String softDeleteSQL() {
+      return " SET deleted_at = floor(extract(epoch from((current_timestamp -"
+          + " timestamp '1970-01-01 00:00:00')*1000))) ";
+    }
+  }
+
+  private static final Map<JDBCBackend.JDBCBackendType, 
StatisticBaseSQLProvider>
+      STATISTIC_SQL_PROVIDERS =
+          ImmutableMap.of(
+              JDBCBackend.JDBCBackendType.H2,
+              new StatisticH2Provider(),
+              JDBCBackend.JDBCBackendType.MYSQL,
+              new StatisticMySQLProvider(),
+              JDBCBackend.JDBCBackendType.POSTGRESQL,
+              new StatisticPostgresSQLProvider());
+
+  public static StatisticBaseSQLProvider getProvider() {
+    String databaseId =
+        SqlSessionFactoryHelper.getInstance()
+            .getSqlSessionFactory()
+            .getConfiguration()
+            .getDatabaseId();
+    JDBCBackend.JDBCBackendType jdbcBackendType =
+        JDBCBackend.JDBCBackendType.fromString(databaseId);
+    return STATISTIC_SQL_PROVIDERS.get(jdbcBackendType);
+  }
+
+  public static String batchInsertStatisticPOs(
+      @Param("statisticPOs") List<StatisticPO> statisticPOs) {
+    return getProvider().batchInsertStatisticPOs(statisticPOs);
+  }
+
+  public static String batchDeleteStatisticPOs(
+      @Param("objectId") Long objectId, @Param("statisticNames") List<String> 
statisticNames) {
+    return getProvider().batchDeleteStatisticPOs(objectId, statisticNames);
+  }
+
+  public static String softDeleteStatisticsByObjectId(@Param("objectId") Long 
objectId) {
+    return getProvider().softDeleteStatisticsByObjectId(objectId);
+  }
+
+  public static String listStatisticPOsByObjectId(@Param("objectId") Long 
objectId) {
+    return getProvider().listStatisticPOsByObjectId(objectId);
+  }

Review Comment:
   For table, this is the table id. For fileset, this is the fileset id. The 
object id is the unique identifier. 



##########
common/src/main/java/org/apache/gravitino/json/JsonUtils.java:
##########
@@ -1343,6 +1349,94 @@ public Expression deserialize(JsonParser p, 
DeserializationContext ctxt) throws
     }
   }
 
+  /** Custom JSON deserializer for StatisticValue objects. */
+  public static class StatisticValueDeserializer extends 
JsonDeserializer<StatisticValue> {
+    @Override
+    public StatisticValue<?> deserialize(JsonParser p, DeserializationContext 
ctxt)
+        throws IOException {
+      JsonNode node = p.getCodec().readTree(p);
+      return getStatisticValue(node);
+    }
+  }
+
+  private static StatisticValue<?> getStatisticValue(JsonNode node) throws 
IOException {
+    Preconditions.checkArgument(
+        node != null && !node.isNull(), "Cannot parse statistic value from 
invalid JSON: %s", node);
+    if (node.isIntegralNumber()) {
+      return StatisticValues.longValue(node.asLong());
+    } else if (node.isFloatingPointNumber()) {
+      return StatisticValues.doubleValue(node.asDouble());
+    } else if (node.isTextual()) {
+      return StatisticValues.stringValue(node.asText());
+    } else if (node.isBoolean()) {
+      return StatisticValues.booleanValue(node.asBoolean());
+    } else if (node.isArray()) {
+      ArrayNode arrayNode = (ArrayNode) node;
+      List<StatisticValue<Object>> values = 
Lists.newArrayListWithCapacity(arrayNode.size());
+      for (JsonNode arrayElement : arrayNode) {
+        StatisticValue<?> value = getStatisticValue(arrayElement);
+        if (value != null) {
+          values.add((StatisticValue<Object>) value);
+        }
+      }
+      return StatisticValues.listValue(values);
+    } else if (node.isObject()) {
+      ObjectNode objectNode = (ObjectNode) node;
+      Map<String, StatisticValue<?>> map = Maps.newHashMap();
+      objectNode
+          .fields()
+          .forEachRemaining(
+              entry -> {
+                try {
+                  StatisticValue<?> value = 
getStatisticValue(entry.getValue());
+                  if (value != null) {
+                    map.put(entry.getKey(), value);
+                  }
+                } catch (IOException e) {
+                  throw new RuntimeException(e);
+                }
+              });
+      return StatisticValues.objectValue(map);
+    } else {
+      throw new UnsupportedEncodingException(
+          String.format("Don't support json node type %s", 
node.getNodeType()));
+    }
+  }
+
+  /** Custom JSON serializer for StatisticValue objects. */
+  public static class StatisticValueSerializer extends 
JsonSerializer<StatisticValue> {
+
+    @Override
+    public void serialize(StatisticValue value, JsonGenerator gen, 
SerializerProvider serializers)
+        throws IOException {
+      if (value.dataType().name() == Type.Name.BOOLEAN) {
+        gen.writeBoolean((Boolean) value.value());
+      } else if (value.dataType().name() == Type.Name.STRING) {
+        gen.writeString((String) value.value());
+      } else if (value.dataType().name() == Type.Name.DOUBLE) {
+        gen.writeNumber((Double) value.value());
+      } else if (value.dataType().name() == Type.Name.LONG) {
+        gen.writeNumber((Long) value.value());
+      } else if (value.dataType().name() == Type.Name.LIST) {
+        gen.writeStartArray();
+        for (StatisticValue<?> element : (List<StatisticValue<?>>) 
value.value()) {
+          serialize(element, gen, serializers);
+        }
+        gen.writeEndArray();
+      } else if (value.dataType().name() == Type.Name.STRUCT) {
+        gen.writeStartObject();
+        for (Map.Entry<String, StatisticValue<?>> entry :
+            ((Map<String, StatisticValue<?>>) value.value()).entrySet()) {
+          gen.writeFieldName(entry.getKey());
+          serialize(entry.getValue(), gen, serializers);
+        }
+        gen.writeEndObject();
+      } else {
+        throw new IOException("Unsupported statistic value type: " + 
value.dataType());
+      }
+    }
+  }

Review Comment:
   I can try to reuse partial code.



##########
core/src/main/java/org/apache/gravitino/meta/StatisticEntity.java:
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.meta;
+
+import com.google.common.collect.Maps;
+import java.util.Map;
+import org.apache.gravitino.Audit;
+import org.apache.gravitino.Auditable;
+import org.apache.gravitino.Entity;
+import org.apache.gravitino.Field;
+import org.apache.gravitino.HasIdentifier;
+import org.apache.gravitino.Namespace;
+import org.apache.gravitino.stats.StatisticValue;
+
+public class StatisticEntity implements Entity, HasIdentifier, Auditable {
+  public static final Field ID =
+      Field.required("id", Long.class, "The unique identifier of the statistic 
entity.");
+  public static final Field NAME =
+      Field.required("name", String.class, "The name of the statistic 
entity.");
+  public static final Field VALUE =
+      Field.required("value", StatisticValue.class, "The value of the 
statistic entity.");
+  public static final Field AUDIT_INFO =
+      Field.required("audit_info", Audit.class, "The audit details of the 
statistic entity.");
+
+  private Long id;
+  private String name;
+  private StatisticValue<?> value;
+  private AuditInfo auditInfo;
+  private Namespace namespace;
+
+  @Override
+  public Audit auditInfo() {
+    return auditInfo;
+  }
+
+  @Override
+  public Map<Field, Object> fields() {
+    Map<Field, Object> fields = Maps.newHashMap();
+    fields.put(ID, id);
+    fields.put(NAME, name);
+    fields.put(VALUE, value);
+    fields.put(AUDIT_INFO, auditInfo);
+    return fields;
+  }
+
+  @Override
+  public EntityType type() {
+    return EntityType.STATISTIC;
+  }
+
+  @Override
+  public String name() {
+    return name;
+  }
+
+  @Override
+  public Long id() {
+    return id;
+  }
+
+  @Override
+  public Namespace namespace() {
+    return namespace;
+  }
+
+  public StatisticValue<?> value() {
+    return value;
+  }
+
+  public static Builder builder() {
+    return new Builder();
+  }
+
+  public static class Builder {
+    private final StatisticEntity statisticEntity;
+
+    private Builder() {
+      statisticEntity = new StatisticEntity();
+    }
+
+    public Builder withId(Long id) {
+      statisticEntity.id = id;
+      return this;
+    }
+
+    public Builder withName(String name) {
+      statisticEntity.name = name;
+      return this;
+    }
+
+    public Builder withValue(StatisticValue<?> value) {
+      statisticEntity.value = value;
+      return this;
+    }
+
+    public Builder withAuditInfo(AuditInfo auditInfo) {
+      statisticEntity.auditInfo = auditInfo;
+      return this;
+    }
+
+    public Builder withNamespace(Namespace namespace) {
+      statisticEntity.namespace = namespace;
+      return this;
+    }
+
+    public Builder withNamespace(String namespace) {
+      statisticEntity.namespace = Namespace.of(namespace);
+      return this;
+    }

Review Comment:
   Removed.



##########
core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/StatisticBaseSQLProvider.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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.mapper.provider.base;
+
+import static 
org.apache.gravitino.storage.relational.mapper.StatisticMetaMapper.STATISTIC_META_TABLE_NAME;
+
+import java.util.List;
+import org.apache.gravitino.storage.relational.mapper.CatalogMetaMapper;
+import org.apache.gravitino.storage.relational.mapper.FilesetMetaMapper;
+import org.apache.gravitino.storage.relational.mapper.ModelMetaMapper;
+import org.apache.gravitino.storage.relational.mapper.SchemaMetaMapper;
+import org.apache.gravitino.storage.relational.mapper.TableMetaMapper;
+import org.apache.gravitino.storage.relational.mapper.TopicMetaMapper;
+import org.apache.gravitino.storage.relational.po.StatisticPO;
+import org.apache.ibatis.annotations.Param;
+
+public class StatisticBaseSQLProvider {
+
+  public String batchInsertStatisticPOs(@Param("statisticPOs") 
List<StatisticPO> statisticPOs) {
+    return "<script>"
+        + "INSERT INTO "
+        + STATISTIC_META_TABLE_NAME
+        + " (statistic_id, statistic_name, statistic_value, metalake_id, 
metadata_object_id, metadata_object_type, audit_info, current_version, 
last_version, deleted_at) VALUES "
+        + "<foreach collection='statisticPOs' item='item' separator=','>"
+        + "(#{item.statisticId}, "
+        + "#{item.statisticName}, "
+        + "#{item.statisticValue}, "
+        + "#{item.metalakeId}, "
+        + "#{item.metadataObjectId}, "
+        + "#{item.metadataObjectType}, "
+        + "#{item.auditInfo}, "
+        + "#{item.currentVersion}, "
+        + "#{item.lastVersion}, "
+        + "#{item.deletedAt})"
+        + "</foreach>"
+        + "</script>";
+  }

Review Comment:
   Done.



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