From bec35a6f81dd52d321f4d8e8d071580c9198f407 Mon Sep 17 00:00:00 2001 From: luoshipeng <806855059@qq.com> Date: Tue, 7 Jan 2025 11:34:35 +0800 Subject: [PATCH] [#4305] improvement(core): Improved the way of fill parentEntityId in POBuilder (#6114) ### What changes were proposed in this pull request? remove the for each statement, and get parentEntityId directly. ### Why are the changes needed? issue: https://github.com/apache/gravitino/issues/4305 ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? ut --------- Co-authored-by: luoshipeng --- .../relational/service/CommonMetaService.java | 25 ++++++++++++++++++ .../service/FilesetMetaService.java | 26 ++++--------------- .../relational/service/ModelMetaService.java | 18 +++---------- .../relational/service/SchemaMetaService.java | 20 +++----------- .../relational/service/TableMetaService.java | 26 ++++--------------- .../relational/service/TopicMetaService.java | 26 ++++--------------- 6 files changed, 48 insertions(+), 93 deletions(-) diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/service/CommonMetaService.java b/core/src/main/java/org/apache/gravitino/storage/relational/service/CommonMetaService.java index f990e94fdcc..bdab2ad9fe5 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/service/CommonMetaService.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/service/CommonMetaService.java @@ -57,4 +57,29 @@ public Long getParentEntityIdByNamespace(Namespace namespace) { "Parent entity id should not be null and should be greater than 0."); return parentEntityId; } + + public Long[] getParentEntityIdsByNamespace(Namespace namespace) { + Preconditions.checkArgument( + !namespace.isEmpty() && namespace.levels().length <= 3, + "Namespace should not be empty and length should be less than or equal to 3."); + Long[] parentEntityIds = new Long[namespace.levels().length]; + if (namespace.levels().length >= 1) { + parentEntityIds[0] = + MetalakeMetaService.getInstance().getMetalakeIdByName(namespace.level(0)); + } + + if (namespace.levels().length >= 2) { + parentEntityIds[1] = + CatalogMetaService.getInstance() + .getCatalogIdByMetalakeIdAndName(parentEntityIds[0], namespace.level(1)); + } + + if (namespace.levels().length >= 3) { + parentEntityIds[2] = + SchemaMetaService.getInstance() + .getSchemaIdByCatalogIdAndName(parentEntityIds[1], namespace.level(2)); + } + + return parentEntityIds; + } } diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/service/FilesetMetaService.java b/core/src/main/java/org/apache/gravitino/storage/relational/service/FilesetMetaService.java index e049f436406..9233005c34a 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/service/FilesetMetaService.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/service/FilesetMetaService.java @@ -314,26 +314,10 @@ public int deleteFilesetVersionsByRetentionCount(Long versionRetentionCount, int private void fillFilesetPOBuilderParentEntityId(FilesetPO.Builder builder, Namespace namespace) { NamespaceUtil.checkFileset(namespace); - Long parentEntityId = null; - for (int level = 0; level < namespace.levels().length; level++) { - String name = namespace.level(level); - switch (level) { - case 0: - parentEntityId = MetalakeMetaService.getInstance().getMetalakeIdByName(name); - builder.withMetalakeId(parentEntityId); - continue; - case 1: - parentEntityId = - CatalogMetaService.getInstance() - .getCatalogIdByMetalakeIdAndName(parentEntityId, name); - builder.withCatalogId(parentEntityId); - continue; - case 2: - parentEntityId = - SchemaMetaService.getInstance().getSchemaIdByCatalogIdAndName(parentEntityId, name); - builder.withSchemaId(parentEntityId); - break; - } - } + Long[] parentEntityIds = + CommonMetaService.getInstance().getParentEntityIdsByNamespace(namespace); + builder.withMetalakeId(parentEntityIds[0]); + builder.withCatalogId(parentEntityIds[1]); + builder.withSchemaId(parentEntityIds[2]); } } diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/service/ModelMetaService.java b/core/src/main/java/org/apache/gravitino/storage/relational/service/ModelMetaService.java index 2da43755c51..0197dfdd2dd 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/service/ModelMetaService.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/service/ModelMetaService.java @@ -172,20 +172,10 @@ ModelPO getModelPOById(Long modelId) { private void fillModelPOBuilderParentEntityId(ModelPO.Builder builder, Namespace ns) { NamespaceUtil.checkModel(ns); - String metalake = ns.level(0); - String catalog = ns.level(1); - String schema = ns.level(2); - - Long metalakeId = MetalakeMetaService.getInstance().getMetalakeIdByName(metalake); - builder.withMetalakeId(metalakeId); - - Long catalogId = - CatalogMetaService.getInstance().getCatalogIdByMetalakeIdAndName(metalakeId, catalog); - builder.withCatalogId(catalogId); - - Long schemaId = - SchemaMetaService.getInstance().getSchemaIdByCatalogIdAndName(catalogId, schema); - builder.withSchemaId(schemaId); + Long[] parentEntityIds = CommonMetaService.getInstance().getParentEntityIdsByNamespace(ns); + builder.withMetalakeId(parentEntityIds[0]); + builder.withCatalogId(parentEntityIds[1]); + builder.withSchemaId(parentEntityIds[2]); } ModelPO getModelPOByIdentifier(NameIdentifier ident) { diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/service/SchemaMetaService.java b/core/src/main/java/org/apache/gravitino/storage/relational/service/SchemaMetaService.java index 4c9c828cb9c..f300e70cae3 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/service/SchemaMetaService.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/service/SchemaMetaService.java @@ -316,21 +316,9 @@ public int deleteSchemaMetasByLegacyTimeline(Long legacyTimeline, int limit) { private void fillSchemaPOBuilderParentEntityId(SchemaPO.Builder builder, Namespace namespace) { NamespaceUtil.checkSchema(namespace); - Long parentEntityId = null; - for (int level = 0; level < namespace.levels().length; level++) { - String name = namespace.level(level); - switch (level) { - case 0: - parentEntityId = MetalakeMetaService.getInstance().getMetalakeIdByName(name); - builder.withMetalakeId(parentEntityId); - continue; - case 1: - parentEntityId = - CatalogMetaService.getInstance() - .getCatalogIdByMetalakeIdAndName(parentEntityId, name); - builder.withCatalogId(parentEntityId); - break; - } - } + Long[] parentEntityIds = + CommonMetaService.getInstance().getParentEntityIdsByNamespace(namespace); + builder.withMetalakeId(parentEntityIds[0]); + builder.withCatalogId(parentEntityIds[1]); } } diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/service/TableMetaService.java b/core/src/main/java/org/apache/gravitino/storage/relational/service/TableMetaService.java index 248dedd8a73..bc44ac43a92 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/service/TableMetaService.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/service/TableMetaService.java @@ -253,27 +253,11 @@ public int deleteTableMetasByLegacyTimeline(Long legacyTimeline, int limit) { private void fillTablePOBuilderParentEntityId(TablePO.Builder builder, Namespace namespace) { NamespaceUtil.checkTable(namespace); - Long parentEntityId = null; - for (int level = 0; level < namespace.levels().length; level++) { - String name = namespace.level(level); - switch (level) { - case 0: - parentEntityId = MetalakeMetaService.getInstance().getMetalakeIdByName(name); - builder.withMetalakeId(parentEntityId); - continue; - case 1: - parentEntityId = - CatalogMetaService.getInstance() - .getCatalogIdByMetalakeIdAndName(parentEntityId, name); - builder.withCatalogId(parentEntityId); - continue; - case 2: - parentEntityId = - SchemaMetaService.getInstance().getSchemaIdByCatalogIdAndName(parentEntityId, name); - builder.withSchemaId(parentEntityId); - break; - } - } + Long[] parentEntityIds = + CommonMetaService.getInstance().getParentEntityIdsByNamespace(namespace); + builder.withMetalakeId(parentEntityIds[0]); + builder.withCatalogId(parentEntityIds[1]); + builder.withSchemaId(parentEntityIds[2]); } private TablePO getTablePOBySchemaIdAndName(Long schemaId, String tableName) { diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/service/TopicMetaService.java b/core/src/main/java/org/apache/gravitino/storage/relational/service/TopicMetaService.java index 7bc933824aa..66a12aa9de1 100644 --- a/core/src/main/java/org/apache/gravitino/storage/relational/service/TopicMetaService.java +++ b/core/src/main/java/org/apache/gravitino/storage/relational/service/TopicMetaService.java @@ -154,27 +154,11 @@ public TopicPO getTopicPOById(Long topicId) { private void fillTopicPOBuilderParentEntityId(TopicPO.Builder builder, Namespace namespace) { NamespaceUtil.checkTopic(namespace); - Long parentEntityId = null; - for (int level = 0; level < namespace.levels().length; level++) { - String name = namespace.level(level); - switch (level) { - case 0: - parentEntityId = MetalakeMetaService.getInstance().getMetalakeIdByName(name); - builder.withMetalakeId(parentEntityId); - continue; - case 1: - parentEntityId = - CatalogMetaService.getInstance() - .getCatalogIdByMetalakeIdAndName(parentEntityId, name); - builder.withCatalogId(parentEntityId); - continue; - case 2: - parentEntityId = - SchemaMetaService.getInstance().getSchemaIdByCatalogIdAndName(parentEntityId, name); - builder.withSchemaId(parentEntityId); - break; - } - } + Long[] parentEntityIds = + CommonMetaService.getInstance().getParentEntityIdsByNamespace(namespace); + builder.withMetalakeId(parentEntityIds[0]); + builder.withCatalogId(parentEntityIds[1]); + builder.withSchemaId(parentEntityIds[2]); } public TopicEntity getTopicByIdentifier(NameIdentifier identifier) {