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


##########
core/src/main/java/org/apache/gravitino/authorization/OwnerManager.java:
##########
@@ -0,0 +1,193 @@
+/*
+ * 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.authorization;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Optional;
+import org.apache.gravitino.Entity;
+import org.apache.gravitino.EntityStore;
+import org.apache.gravitino.MetadataObject;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.SupportsRelationOperations;
+import org.apache.gravitino.exceptions.NoSuchEntityException;
+import org.apache.gravitino.exceptions.NotFoundException;
+import org.apache.gravitino.lock.LockType;
+import org.apache.gravitino.lock.TreeLockUtils;
+import org.apache.gravitino.meta.GroupEntity;
+import org.apache.gravitino.meta.UserEntity;
+import org.apache.gravitino.storage.kv.KvEntityStore;
+import org.apache.gravitino.utils.MetadataObjectUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * OwnerManager is used for manage the owner of metadata object. The user and 
group don't have an
+ * owner
+ */
+public class OwnerManager {
+  private static final Logger LOG = 
LoggerFactory.getLogger(OwnerManager.class);
+  private final EntityStore store;
+
+  public OwnerManager(EntityStore store) {
+    if (store instanceof KvEntityStore) {
+      String errorMsg =
+          "OwnerManager cannot run with kv entity store, please configure the 
entity "
+              + "store to use relational entity store and restart the 
Gravitino server";
+      LOG.error(errorMsg);
+      throw new RuntimeException(errorMsg);
+    }
+
+    if (!(store instanceof SupportsRelationOperations)) {
+      String errorMsg =
+          "OwnerManager cannot run with entity store that does not support 
relation operations, "
+              + "please configure the entity store to use relational entity 
store and restart the Gravitino server";
+      LOG.error(errorMsg);
+      throw new RuntimeException(errorMsg);
+    }
+    this.store = store;
+  }
+
+  public void setOwner(
+      String metalake, MetadataObject metadataObject, String ownerName, 
Owner.Type ownerType) {
+    try {
+      NameIdentifier objectIdent = MetadataObjectUtil.toEntityIdent(metalake, 
metadataObject);
+      if (ownerType == Owner.Type.USER) {
+        NameIdentifier ownerIdent = AuthorizationUtils.ofUser(metalake, 
ownerName);
+        TreeLockUtils.doWithTreeLock(
+            objectIdent,
+            LockType.READ,
+            () ->
+                TreeLockUtils.doWithTreeLock(
+                    ownerIdent,
+                    LockType.READ,
+                    () -> {
+                      store
+                          .relationOperations()
+                          .insertRelation(
+                              SupportsRelationOperations.Type.OWNER_REL,
+                              objectIdent,
+                              MetadataObjectUtil.toEntityType(metadataObject),
+                              ownerIdent,
+                              Entity.EntityType.USER,
+                              true);
+                      return null;
+                    }));
+      } else if (ownerType == Owner.Type.GROUP) {
+        NameIdentifier ownerIdent = AuthorizationUtils.ofGroup(metalake, 
ownerName);
+        TreeLockUtils.doWithTreeLock(
+            objectIdent,
+            LockType.READ,
+            () ->
+                TreeLockUtils.doWithTreeLock(
+                    ownerIdent,
+                    LockType.READ,
+                    () -> {
+                      store
+                          .relationOperations()
+                          .insertRelation(
+                              SupportsRelationOperations.Type.OWNER_REL,
+                              objectIdent,
+                              MetadataObjectUtil.toEntityType(metadataObject),
+                              ownerIdent,
+                              Entity.EntityType.GROUP,
+                              true);
+                      return null;
+                    }));
+      }
+    } catch (NoSuchEntityException nse) {
+      LOG.warn(
+          "Metadata object {} or owner {} is not found", 
metadataObject.fullName(), ownerName, nse);
+      throw new NotFoundException(
+          nse, "Metadata object %s or owner %s is not found", 
metadataObject.fullName(), ownerName);
+    } catch (IOException ioe) {
+      LOG.info(
+          "Fail to set the owner {} of metadata object {}",
+          ownerName,
+          metadataObject.fullName(),
+          ioe);
+      throw new RuntimeException(ioe);
+    }
+  }
+
+  public Optional<Owner> getOwner(String metalake, MetadataObject 
metadataObject) {
+    try {
+      OwnerImpl owner = new OwnerImpl();
+      NameIdentifier ident = MetadataObjectUtil.toEntityIdent(metalake, 
metadataObject);
+      List entities =

Review Comment:
   Why do we use List without type, I think it has compile warning.



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