Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jerqi committed Jul 18, 2024
1 parent a57e4e3 commit fba57b2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.common.collect.Lists;
import java.io.IOException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import org.apache.gravitino.Entity;
import org.apache.gravitino.EntityAlreadyExistsException;
Expand Down Expand Up @@ -113,19 +114,7 @@ User getUser(String metalake, String user) throws NoSuchUserException {
}

String[] listUserNames(String metalake) {
try {
AuthorizationUtils.checkMetalakeExists(metalake);
Namespace namespace = AuthorizationUtils.ofUserNamespace(metalake);
return store.list(namespace, UserEntity.class, Entity.EntityType.USER).stream()
.map(UserEntity::name)
.toArray(String[]::new);
} catch (NoSuchEntityException e) {
LOG.warn("Metalake {} does not exist", metalake, e);
throw new NoSuchMetalakeException(METALAKE_DOES_NOT_EXIST_MSG, metalake);
} catch (IOException ioe) {
LOG.error("Listing user under metalake {} failed due to storage issues", metalake, ioe);
throw new RuntimeException(ioe);
}
return Arrays.stream(listUsers(metalake)).map(User::name).toArray(String[]::new);
}

User[] listUsers(String metalake) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,19 @@ UserPO selectUserMetaByMetalakeIdAndName(
@Param("metalakeId") Long metalakeId, @Param("userName") String name);

@Select(
"SELECT user_id as userId, user_name as userName,"
+ " metalake_id as metalakeId,"
+ " audit_info as auditInfo,"
+ " current_version as currentVersion, last_version as lastVersion,"
+ " deleted_at as deletedAt"
"SELECT ut.user_id as userId, ut.user_name as userName,"
+ " ut.metalake_id as metalakeId,"
+ " ut.audit_info as auditInfo,"
+ " ut.current_version as currentVersion, ut.last_version as lastVersion,"
+ " ut.deleted_at as deletedAt"
+ " FROM "
+ USER_TABLE_NAME
+ " WHERE metalake_id = #{metalakeId}"
+ " AND deleted_at = 0")
List<UserPO> listUserPOsByMetalakeId(@Param("metalakeId") Long metalakeId);
+ " ut JOIN "
+ MetalakeMetaMapper.TABLE_NAME
+ " mt ON ut.metalake_id = mt.metalake_id"
+ " WHERE mt.metalake_name = #{metalakeName}"
+ " AND ut.deleted_at = 0 AND mt.deleted_at = 0")
List<UserPO> listUserPOsByMetalake(@Param("metalakeName") String metalakeName);

@Insert(
"INSERT INTO "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
import org.apache.gravitino.Entity;
import org.apache.gravitino.HasIdentifier;
Expand All @@ -35,7 +34,6 @@
import org.apache.gravitino.authorization.AuthorizationUtils;
import org.apache.gravitino.exceptions.NoSuchEntityException;
import org.apache.gravitino.meta.UserEntity;
import org.apache.gravitino.storage.relational.mapper.MetalakeMetaMapper;
import org.apache.gravitino.storage.relational.mapper.UserMetaMapper;
import org.apache.gravitino.storage.relational.mapper.UserRoleRelMapper;
import org.apache.gravitino.storage.relational.po.RolePO;
Expand Down Expand Up @@ -229,27 +227,10 @@ public List<UserEntity> listUsersByNamespace(Namespace namespace) {

List<UserEntity> userEntities = Lists.newArrayList();
String metalakeName = namespace.level(0);
List<UserPO> userPOs = Lists.newArrayList();
AtomicLong metalakeId = new AtomicLong();
SessionUtils.doMultipleWithoutCommit(
() -> {
Long id =
SessionUtils.doWithoutCommitAndFetchResult(
MetalakeMetaMapper.class,
mapper -> mapper.selectMetalakeIdMetaByName(metalakeName));
if (id == null) {
throw new NoSuchEntityException(
NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE,
Entity.EntityType.METALAKE.name().toLowerCase(),
metalakeName);
}
metalakeId.set(id);
},
() ->
userPOs.addAll(
SessionUtils.doWithoutCommitAndFetchResult(
UserMetaMapper.class,
mapper -> mapper.listUserPOsByMetalakeId(metalakeId.get()))));
List<UserPO> userPOs =
SessionUtils.getWithoutCommit(
UserMetaMapper.class, mapper -> mapper.listUserPOsByMetalake(metalakeName));

for (UserPO userPO : userPOs) {
List<RolePO> rolePOs = RoleMetaService.getInstance().listRolesByUserId(userPO.getUserId());
userEntities.add(POConverters.fromUserPO(userPO, rolePOs, namespace));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,6 @@ void testListUsers() throws IOException {
Assertions.assertEquals(
Lists.newArrayList(user1, user2),
userMetaService.listUsersByNamespace(AuthorizationUtils.ofUserNamespace(metalakeName)));

// Test NoSuchMetalakeException
Assertions.assertThrows(
NoSuchEntityException.class,
() ->
userMetaService.listUsersByNamespace(AuthorizationUtils.ofUserNamespace("not-exist")));
}

@Test
Expand Down

0 comments on commit fba57b2

Please sign in to comment.