-
Notifications
You must be signed in to change notification settings - Fork 350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#5070] improvement(core): Add check for the full name of the metadata object #5075
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,16 +18,22 @@ | |
*/ | ||
package org.apache.gravitino.utils; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.google.common.base.Joiner; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.BiMap; | ||
import com.google.common.collect.ImmutableBiMap; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.gravitino.Entity; | ||
import org.apache.gravitino.GravitinoEnv; | ||
import org.apache.gravitino.MetadataObject; | ||
import org.apache.gravitino.NameIdentifier; | ||
import org.apache.gravitino.authorization.AuthorizationUtils; | ||
import org.apache.gravitino.exceptions.NoSuchMetadataObjectException; | ||
import org.apache.gravitino.exceptions.NoSuchRoleException; | ||
|
||
public class MetadataObjectUtil { | ||
|
||
|
@@ -98,4 +104,77 @@ public static NameIdentifier toEntityIdent(String metalakeName, MetadataObject m | |
"Unknown metadata object type: " + metadataObject.type()); | ||
} | ||
} | ||
|
||
/** | ||
* This method will check if the entity is existed explicitly, internally this check will load the | ||
* entity from underlying sources to entity store if not stored, and will allocate an uid for this | ||
* entity, with this uid tags can be associated with this entity. This method should be called out | ||
* of the tree lock, otherwise it will cause deadlock. | ||
* | ||
* @param metalake The metalake name | ||
* @param object The metadata object | ||
* @param env The Gravitino environment | ||
* @throws NoSuchMetadataObjectException if the metadata object type doesn't exist. | ||
*/ | ||
public static void checkMetadataObject(String metalake, MetadataObject object, GravitinoEnv env) { | ||
NameIdentifier identifier = toEntityIdent(metalake, object); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we doesn't need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
|
||
Supplier<NoSuchMetadataObjectException> exceptionToThrowSupplier = | ||
() -> | ||
new NoSuchMetadataObjectException( | ||
"Metadata object %s type %s doesn't exist", object.fullName(), object.type()); | ||
|
||
switch (object.type()) { | ||
case METALAKE: | ||
NameIdentifierUtil.checkMetalake(identifier); | ||
check(env.metalakeDispatcher().metalakeExists(identifier), exceptionToThrowSupplier); | ||
break; | ||
|
||
case CATALOG: | ||
NameIdentifierUtil.checkCatalog(identifier); | ||
check(env.catalogDispatcher().catalogExists(identifier), exceptionToThrowSupplier); | ||
break; | ||
|
||
case SCHEMA: | ||
NameIdentifierUtil.checkSchema(identifier); | ||
check(env.schemaDispatcher().schemaExists(identifier), exceptionToThrowSupplier); | ||
break; | ||
|
||
case FILESET: | ||
NameIdentifierUtil.checkFileset(identifier); | ||
check(env.filesetDispatcher().filesetExists(identifier), exceptionToThrowSupplier); | ||
break; | ||
|
||
case TABLE: | ||
NameIdentifierUtil.checkTable(identifier); | ||
check(env.tableDispatcher().tableExists(identifier), exceptionToThrowSupplier); | ||
break; | ||
|
||
case TOPIC: | ||
NameIdentifierUtil.checkTopic(identifier); | ||
check(env.topicDispatcher().topicExists(identifier), exceptionToThrowSupplier); | ||
break; | ||
|
||
case ROLE: | ||
AuthorizationUtils.checkRole(identifier); | ||
try { | ||
env.accessControlDispatcher().getRole(metalake, object.fullName()); | ||
} catch (NoSuchRoleException nsr) { | ||
throw checkNotNull(exceptionToThrowSupplier).get(); | ||
} | ||
break; | ||
|
||
case COLUMN: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we support checking columns or not? If the answer is 'No', you can remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. |
||
default: | ||
throw new IllegalArgumentException( | ||
String.format("Doesn't support the type %s", object.type())); | ||
} | ||
} | ||
|
||
private static void check( | ||
final boolean expression, Supplier<? extends RuntimeException> exceptionToThrowSupplier) { | ||
if (!expression) { | ||
throw checkNotNull(exceptionToThrowSupplier).get(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
checkAndImportEntity(...)
function only have one line code.We can directly call
MetadataObjectUtil.checkMetadataObject(metalake, metadataObject, env)
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is convenient to test. Because we can spy this method easily.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.