xloya commented on code in PR #4323:
URL: https://github.com/apache/gravitino/pull/4323#discussion_r1701656066


##########
core/src/main/java/org/apache/gravitino/storage/relational/mapper/OwnerMetaMapper.java:
##########
@@ -0,0 +1,147 @@
+/*
+ * 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 org.apache.gravitino.storage.relational.po.OwnerRelPO;
+import org.apache.ibatis.annotations.Delete;
+import org.apache.ibatis.annotations.Insert;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.Update;
+
+/**
+ * A MyBatis Mapper for owner meta operation SQLs.
+ *
+ * <p>This interface class is a specification defined by MyBatis. It requires 
this interface class
+ * to identify the corresponding SQLs for execution. We can write SQLs in an 
additional XML file, or
+ * write SQLs with annotations in this interface Mapper. See: <a
+ * href="https://mybatis.org/mybatis-3/getting-started.html";></a>
+ */
+public interface OwnerMetaMapper {
+
+  String OWNER_TABLE_NAME = "owner_meta";
+
+  @Select(
+      "SELECT metalake_id as metalakeId,"
+          + " owner_id as ownerId,"
+          + " owner_type as ownerType,"
+          + " metadata_object_id as metadataObjectId,"
+          + " metadata_object_type as metadataObjectType,"
+          + " audit_info as auditInfo,"
+          + " current_version as currentVersion, last_version as lastVersion,"
+          + " deleted_at as deletedAt"
+          + " FROM "
+          + OWNER_TABLE_NAME
+          + " WHERE metadata_object_id = #{metadataObjectId}"
+          + " AND deleted_at = 0")
+  OwnerRelPO selectOwnerMetaByEntityIdAndType(@Param("metadataObjectId") Long 
metadataObjectId);
+
+  @Insert(
+      "INSERT INTO "
+          + OWNER_TABLE_NAME
+          + "(metalake_id, metadata_object_id, metadata_object_type, owner_id, 
owner_type,"
+          + " audit_info, current_version, last_version, deleted_at)"
+          + " VALUES ("
+          + " #{ownerRelPO.metalakeId},"
+          + " #{ownerRelPO.metadataObjectId},"
+          + " #{ownerRelPO.metadataObjectType},"
+          + " #{ownerRelPO.ownerId},"
+          + " #{ownerRelPO.ownerType},"
+          + " #{ownerRelPO.auditInfo},"
+          + " #{ownerRelPO.currentVersion},"
+          + " #{ownerRelPO.lastVersion},"
+          + " #{ownerRelPO.deletedAt}"
+          + ")")
+  void insertOwnerEntityRel(@Param("ownerRelPO") OwnerRelPO ownerRelPO);
+
+  @Update(
+      "UPDATE "
+          + OWNER_TABLE_NAME
+          + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
+          + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000"
+          + " WHERE metadata_object_id = #{metadataObjectId} AND deleted_at = 
0")
+  void softDeleteOwnerRelByEntityId(@Param("metadataObjectId") Long 
metadataObjectId);
+
+  @Update(
+      "UPDATE  "
+          + OWNER_TABLE_NAME
+          + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
+          + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000"
+          + " WHERE metalake_id = #{metalakeId} AND deleted_at =0")
+  void softDeleteOwnerRelByMetalakeId(@Param("metalakeId") Long metalakeId);
+
+  @Update(
+      "UPDATE  "
+          + OWNER_TABLE_NAME
+          + " ot SET ot.deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
+          + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000"
+          + " WHERE EXISTS ("
+          + " SELECT ct.catalog_id FROM "
+          + CatalogMetaMapper.TABLE_NAME
+          + " ct where ct.catalog_id = #{catalogId}  AND ct.deleted_at = 0 AND 
ot.deleted_at = 0 AND ct.catalog_id = ot.metadata_object_id"
+          + " UNION "
+          + " SELECT st.catalog_id FROM "
+          + SchemaMetaMapper.TABLE_NAME
+          + " st where st.catalog_id = #{catalogId} AND st.deleted_at = 0 AND 
ot.deleted_at = 0 AND st.schema_id = ot.metadata_object_id"
+          + " UNION "
+          + " SELECT tt.catalog_id FROM "
+          + TopicMetaMapper.TABLE_NAME
+          + " tt where tt.catalog_id = #{catalogId} AND tt.deleted_at = 0 AND 
ot.deleted_at = 0 AND tt.topic_id = ot.metadata_object_id"
+          + " UNION "
+          + " SELECT tat.catalog_id FROM "
+          + TableMetaMapper.TABLE_NAME
+          + " tat where tat.catalog_id = #{catalogId} AND tat.deleted_at = 0 
AND ot.deleted_at = 0 AND tat.table_id = ot.metadata_object_id"
+          + " UNION "
+          + " SELECT ft.catalog_id FROM "
+          + FilesetMetaMapper.META_TABLE_NAME
+          + " ft where ft.catalog_id = #{catalogId} AND ft.deleted_at = 0 AND 
ot.deleted_at = 0 AND ft.fileset_id = ot.metadata_object_id"
+          + ")")
+  void softDeleteOwnerRelByCatalogId(@Param("catalogId") Long catalogId);
+
+  @Update(
+      "UPDATE  "
+          + OWNER_TABLE_NAME
+          + " ot SET ot.deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
+          + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000"
+          + " WHERE EXISTS ("
+          + " SELECT st.schema_id FROM "
+          + SchemaMetaMapper.TABLE_NAME
+          + " st where st.schema_id = #{schemaId} AND st.deleted_at = 0 AND 
ot.deleted_at = 0 AND st.schema_id = ot.metadata_object_id"
+          + " UNION "
+          + " SELECT tt.schema_id FROM "
+          + TopicMetaMapper.TABLE_NAME
+          + " tt where tt.schema_id = #{schemaId} AND tt.deleted_at = 0 AND 
ot.deleted_at = 0 AND tt.topic_id = ot.metadata_object_id"
+          + " UNION "
+          + " SELECT tat.schema_id FROM "
+          + TableMetaMapper.TABLE_NAME
+          + " tat where tat.schema_id = #{schemaId} AND tat.deleted_at = 0 AND 
ot.deleted_at = 0 AND tat.table_id = ot.metadata_object_id"
+          + " UNION "
+          + " SELECT ft.schema_id FROM "
+          + FilesetMetaMapper.META_TABLE_NAME
+          + " ft where ft.schema_id = #{schemaId} AND ft.deleted_at = 0 AND 
ot.deleted_at = 0 AND ft.fileset_id = ot.metadata_object_id"
+          + ")")
+  void sotDeleteOwnerRelBySchemaId(@Param("schemaId") Long schemaId);
+
+  @Delete(
+      "DELETE FROM "
+          + OWNER_TABLE_NAME
+          + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT 
#{limit}")
+  Integer deleteOwnerMetasByLegacyTimeline(

Review Comment:
   This SQL is used in the `RelationalGarbageCollector`, maybe you need add 
some codes to call this SQL.



##########
core/src/test/resources/h2/schema-h2.sql:
##########
@@ -246,3 +246,20 @@ CREATE TABLE IF NOT EXISTS `tag_relation_meta` (
     KEY `idx_tid` (`tag_id`),
     KEY `idx_mid` (`metadata_object_id`)
     ) ENGINE=InnoDB;
+
+CREATE TABLE IF NOT EXISTS `owner_meta` (
+    `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'auto increment 
id',
+    `metalake_id` BIGINT(20) UNSIGNED NOT NULL COMMENT 'metalake id',
+    `owner_id` BIGINT(20) UNSIGNED NOT NULL COMMENT 'owner id',
+    `owner_type` VARCHAR(64) NOT NULL COMMENT 'owner type',
+    `metadata_object_id` BIGINT(20) UNSIGNED NOT NULL COMMENT 'metadata object 
id',
+    `metadata_object_type` VARCHAR(64) NOT NULL COMMENT 'metadata object type',
+    `audit_info` MEDIUMTEXT NOT NULL COMMENT 'owner relation audit info',
+    `current_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'owner relation 
current version',
+    `last_version` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'owner relation 
last version',
+    `deleted_at` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'owner 
relation deleted at',
+    PRIMARY KEY (`id`),
+    UNIQUE KEY `uk_ow_me_del` (`owner_id`, `metadata_object_id`, `deleted_at`),

Review Comment:
   I think we need to add `metadata_type` here and below to ensure the 
uniqueness of metadata object. WDYT?



##########
core/src/main/java/org/apache/gravitino/storage/relational/mapper/OwnerMetaMapper.java:
##########
@@ -0,0 +1,147 @@
+/*
+ * 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 org.apache.gravitino.storage.relational.po.OwnerRelPO;
+import org.apache.ibatis.annotations.Delete;
+import org.apache.ibatis.annotations.Insert;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.Update;
+
+/**
+ * A MyBatis Mapper for owner meta operation SQLs.
+ *
+ * <p>This interface class is a specification defined by MyBatis. It requires 
this interface class
+ * to identify the corresponding SQLs for execution. We can write SQLs in an 
additional XML file, or
+ * write SQLs with annotations in this interface Mapper. See: <a
+ * href="https://mybatis.org/mybatis-3/getting-started.html";></a>
+ */
+public interface OwnerMetaMapper {
+
+  String OWNER_TABLE_NAME = "owner_meta";
+
+  @Select(
+      "SELECT metalake_id as metalakeId,"
+          + " owner_id as ownerId,"
+          + " owner_type as ownerType,"
+          + " metadata_object_id as metadataObjectId,"
+          + " metadata_object_type as metadataObjectType,"
+          + " audit_info as auditInfo,"
+          + " current_version as currentVersion, last_version as lastVersion,"
+          + " deleted_at as deletedAt"
+          + " FROM "
+          + OWNER_TABLE_NAME
+          + " WHERE metadata_object_id = #{metadataObjectId}"
+          + " AND deleted_at = 0")
+  OwnerRelPO selectOwnerMetaByEntityIdAndType(@Param("metadataObjectId") Long 
metadataObjectId);

Review Comment:
   Maybe here need pass the `metadataType`.



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