jerqi commented on code in PR #5021:
URL: https://github.com/apache/gravitino/pull/5021#discussion_r1792695792
##########
core/src/main/java/org/apache/gravitino/storage/relational/service/RoleMetaService.java:
##########
@@ -160,6 +167,89 @@ public void insertRole(RoleEntity roleEntity, boolean
overwritten) throws IOExce
}
}
+ public <E extends Entity & HasIdentifier> RoleEntity updateRole(
+ NameIdentifier identifier, Function<E, E> updater) throws IOException {
+ AuthorizationUtils.checkRole(identifier);
+
+ try {
+ String metalake = identifier.namespace().level(0);
+ Long metalakeId =
MetalakeMetaService.getInstance().getMetalakeIdByName(metalake);
+
+ RolePO rolePO = getRolePOByMetalakeIdAndName(metalakeId,
identifier.name());
+ RoleEntity oldRoleEntity =
+ POConverters.fromRolePO(
+ rolePO, listSecurableObjects(rolePO),
AuthorizationUtils.ofRoleNamespace(metalake));
+
+ RoleEntity newRoleEntity = (RoleEntity) updater.apply((E) oldRoleEntity);
+
+ Preconditions.checkArgument(
+ Objects.equals(oldRoleEntity.id(), newRoleEntity.id()),
+ "The updated role entity id: %s should be same with the role entity
id before: %s",
+ newRoleEntity.id(),
+ oldRoleEntity.id());
+
+ Set<SecurableObject> oldObjects = new
HashSet<>(oldRoleEntity.securableObjects());
+ Set<SecurableObject> newObjects = new
HashSet<>(newRoleEntity.securableObjects());
+ Set<SecurableObject> insertObjects = Sets.difference(newObjects,
oldObjects);
+ Set<SecurableObject> deleteObjects = Sets.difference(oldObjects,
newObjects);
+
+ if (insertObjects.isEmpty() && deleteObjects.isEmpty()) {
+ return newRoleEntity;
+ }
+
+ List<SecurableObjectPO> deleteSecurableObjectPOS =
+ toSecurablePOs(deleteObjects, oldRoleEntity, metalakeId);
+
+ List<SecurableObjectPO> insertSecurableObjectPOs =
+ toSecurablePOs(insertObjects, oldRoleEntity, metalakeId);
+
+ SessionUtils.doMultipleWithCommit(
+ () ->
+ SessionUtils.doWithoutCommit(
+ RoleMetaMapper.class,
+ mapper ->
+ mapper.updateRoleMeta(
+ POConverters.updateRolePOWithVersion(rolePO,
newRoleEntity), rolePO)),
+ () -> {
+ if (deleteSecurableObjectPOS.isEmpty()) {
+ return;
+ }
+
+ SessionUtils.doWithoutCommit(
+ SecurableObjectMapper.class,
+ mapper ->
mapper.batchSoftDeleteSecurableObjects(deleteSecurableObjectPOS));
+ },
+ () -> {
+ if (insertSecurableObjectPOs.isEmpty()) {
+ return;
+ }
+
+ SessionUtils.doWithoutCommit(
+ SecurableObjectMapper.class,
+ mapper ->
mapper.batchInsertSecurableObjects(insertSecurableObjectPOs));
+ });
+
+ return newRoleEntity;
+ } catch (RuntimeException re) {
+ ExceptionUtils.checkSQLException(re, Entity.EntityType.ROLE,
identifier.toString());
+ throw re;
+ }
+ }
+
+ private List<SecurableObjectPO> toSecurablePOs(
Review Comment:
Fixed.
--
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]