Skip to content

Commit ed19b7a

Browse files
committed
Optimize the entity parent id logic
1 parent 2980674 commit ed19b7a

11 files changed

+179
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.gravitino.storage.relational.helper;
20+
21+
public class CatalogIds {
22+
private Long metalakeId;
23+
private Long catalogId;
24+
25+
public CatalogIds(Long metalakeId, Long catalogId) {
26+
this.metalakeId = metalakeId;
27+
this.catalogId = catalogId;
28+
}
29+
30+
public Long getMetalakeId() {
31+
return metalakeId;
32+
}
33+
34+
public Long getCatalogId() {
35+
return catalogId;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.gravitino.storage.relational.helper;
20+
21+
public class SchemaIds {
22+
private Long metalakeId;
23+
private Long catalogId;
24+
private Long schemaId;
25+
26+
public SchemaIds(Long metalakeId, Long catalogId, Long schemaId) {
27+
this.metalakeId = metalakeId;
28+
this.catalogId = catalogId;
29+
this.schemaId = schemaId;
30+
}
31+
32+
public Long getMetalakeId() {
33+
return metalakeId;
34+
}
35+
36+
public Long getCatalogId() {
37+
return catalogId;
38+
}
39+
40+
public Long getSchemaId() {
41+
return schemaId;
42+
}
43+
}

core/src/main/java/org/apache/gravitino/storage/relational/mapper/CatalogMetaMapper.java

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.gravitino.storage.relational.mapper;
2121

2222
import java.util.List;
23+
import org.apache.gravitino.storage.relational.helper.CatalogIds;
2324
import org.apache.gravitino.storage.relational.po.CatalogPO;
2425
import org.apache.ibatis.annotations.DeleteProvider;
2526
import org.apache.ibatis.annotations.InsertProvider;
@@ -87,4 +88,10 @@ Integer updateCatalogMeta(
8788
method = "deleteCatalogMetasByLegacyTimeline")
8889
Integer deleteCatalogMetasByLegacyTimeline(
8990
@Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit);
91+
92+
@SelectProvider(
93+
type = CatalogMetaSQLProviderFactory.class,
94+
method = "selectCatalogIdByMetalakeNameAndCatalogName")
95+
CatalogIds selectCatalogIdByMetalakeNameAndCatalogName(
96+
@Param("metalakeName") String metalakeName, @Param("catalogName") String catalogName);
9097
}

core/src/main/java/org/apache/gravitino/storage/relational/mapper/CatalogMetaSQLProviderFactory.java

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public static String selectCatalogMetaByMetalakeIdAndName(
7171
return getProvider().selectCatalogMetaByMetalakeIdAndName(metalakeId, name);
7272
}
7373

74+
public static String selectCatalogIdByMetalakeNameAndCatalogName(
75+
@Param("metalakeName") String metalakeName, @Param("catalogName") String catalogName) {
76+
return getProvider().selectCatalogIdByMetalakeNameAndCatalogName(metalakeName, catalogName);
77+
}
78+
7479
public static String selectCatalogMetaById(@Param("catalogId") Long catalogId) {
7580
return getProvider().selectCatalogMetaById(catalogId);
7681
}

core/src/main/java/org/apache/gravitino/storage/relational/mapper/SchemaMetaMapper.java

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.gravitino.storage.relational.mapper;
2121

2222
import java.util.List;
23+
import org.apache.gravitino.storage.relational.helper.SchemaIds;
2324
import org.apache.gravitino.storage.relational.po.SchemaPO;
2425
import org.apache.ibatis.annotations.DeleteProvider;
2526
import org.apache.ibatis.annotations.InsertProvider;
@@ -91,4 +92,12 @@ Integer updateSchemaMeta(
9192
method = "deleteSchemaMetasByLegacyTimeline")
9293
Integer deleteSchemaMetasByLegacyTimeline(
9394
@Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit);
95+
96+
@SelectProvider(
97+
type = SchemaMetaSQLProviderFactory.class,
98+
method = "selectSchemaIdByMetalakeNameAndCatalogNameAndSchemaName")
99+
SchemaIds selectSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
100+
@Param("metalakeName") String metalakeName,
101+
@Param("catalogName") String catalogName,
102+
@Param("schemaName") String schemaName);
94103
}

core/src/main/java/org/apache/gravitino/storage/relational/mapper/SchemaMetaSQLProviderFactory.java

+9
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,13 @@ public static String deleteSchemaMetasByLegacyTimeline(
103103
@Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) {
104104
return getProvider().deleteSchemaMetasByLegacyTimeline(legacyTimeline, limit);
105105
}
106+
107+
public static String selectSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
108+
@Param("metalakeName") String metalakeName,
109+
@Param("catalogName") String catalogName,
110+
@Param("schemaName") String schemaName) {
111+
return getProvider()
112+
.selectSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
113+
metalakeName, catalogName, schemaName);
114+
}
106115
}

core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/CatalogMetaBaseSQLProvider.java

+9
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ public String selectCatalogMetaByMetalakeIdAndName(
7474
+ " WHERE metalake_id = #{metalakeId} AND catalog_name = #{catalogName} AND deleted_at = 0";
7575
}
7676

77+
public String selectCatalogIdByMetalakeNameAndCatalogName(
78+
@Param("metalakeName") String metalakeName, @Param("catalogName") String catalogName) {
79+
return "SELECT me.metalake_id as metalakeId, ca.catalog_id as catalogId FROM "
80+
+ TABLE_NAME
81+
+ " ca INNER JOIN metalake_meta me ON ca.metalake_id = me.metalake_id"
82+
+ " WHERE me.metalake_name = #{metalakeName} AND ca.catalog_name = #{catalogName} "
83+
+ " AND ca.deleted_at = 0 AND me.deleted_at = 0";
84+
}
85+
7786
public String selectCatalogMetaById(@Param("catalogId") Long catalogId) {
7887
return "SELECT catalog_id as catalogId, catalog_name as catalogName,"
7988
+ " metalake_id as metalakeId, type, provider,"

core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/SchemaMetaBaseSQLProvider.java

+17
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,21 @@ public String deleteSchemaMetasByLegacyTimeline(
190190
+ TABLE_NAME
191191
+ " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT #{limit}";
192192
}
193+
194+
public String selectSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
195+
@Param("metalakeName") String metalakeName,
196+
@Param("catalogName") String catalogName,
197+
@Param("schemaName") String schemaName) {
198+
return "SELECT metalake_meta.metalake_id as metalakeId, catalog_meta.catalog_id as catalogId, "
199+
+ " schema_id as schemaId"
200+
+ " FROM metalake_meta"
201+
+ " JOIN catalog_meta ON metalake_meta.metalake_id = catalog_meta.metalake_id"
202+
+ " JOIN schema_meta ON catalog_meta.catalog_id = schema_meta.catalog_id"
203+
+ " WHERE metalake_name = #{metalakeName}"
204+
+ " AND catalog_name = #{catalogName}"
205+
+ " AND schema_name = #{schemaName}"
206+
+ " AND schema_meta.deleted_at = 0"
207+
+ " AND catalog_meta.deleted_at = 0"
208+
+ " AND metalake_meta.deleted_at = 0";
209+
}
193210
}

core/src/main/java/org/apache/gravitino/storage/relational/service/CatalogMetaService.java

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.gravitino.exceptions.NonEmptyEntityException;
3434
import org.apache.gravitino.meta.CatalogEntity;
3535
import org.apache.gravitino.meta.SchemaEntity;
36+
import org.apache.gravitino.storage.relational.helper.CatalogIds;
3637
import org.apache.gravitino.storage.relational.mapper.CatalogMetaMapper;
3738
import org.apache.gravitino.storage.relational.mapper.FilesetMetaMapper;
3839
import org.apache.gravitino.storage.relational.mapper.FilesetVersionMapper;
@@ -80,6 +81,12 @@ public CatalogPO getCatalogPOByMetalakeIdAndName(Long metalakeId, String catalog
8081
return catalogPO;
8182
}
8283

84+
public CatalogIds getCatalogIdByMetalakeAndCatalogName(String metalakeName, String catalogName) {
85+
return SessionUtils.getWithoutCommit(
86+
CatalogMetaMapper.class,
87+
mapper -> mapper.selectCatalogIdByMetalakeNameAndCatalogName(metalakeName, catalogName));
88+
}
89+
8390
// Catalog may be deleted, so the CatalogPO may be null.
8491
@Nullable
8592
public CatalogPO getCatalogPOById(Long catalogId) {

core/src/main/java/org/apache/gravitino/storage/relational/service/CommonMetaService.java

+26-16
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import com.google.common.base.Preconditions;
2323
import org.apache.gravitino.Namespace;
24+
import org.apache.gravitino.storage.relational.helper.CatalogIds;
25+
import org.apache.gravitino.storage.relational.helper.SchemaIds;
2426

2527
/** The service class for common metadata operations. */
2628
public class CommonMetaService {
@@ -36,22 +38,27 @@ public Long getParentEntityIdByNamespace(Namespace namespace) {
3638
Preconditions.checkArgument(
3739
!namespace.isEmpty() && namespace.levels().length <= 3,
3840
"Namespace should not be empty and length should be less than or equal to 3.");
41+
3942
Long parentEntityId = null;
40-
if (namespace.levels().length >= 1) {
43+
if (namespace.levels().length == 1) {
4144
parentEntityId = MetalakeMetaService.getInstance().getMetalakeIdByName(namespace.level(0));
4245
}
4346

44-
if (namespace.levels().length >= 2) {
47+
if (namespace.levels().length == 2) {
4548
parentEntityId =
4649
CatalogMetaService.getInstance()
47-
.getCatalogIdByMetalakeIdAndName(parentEntityId, namespace.level(1));
50+
.getCatalogIdByMetalakeAndCatalogName(namespace.level(0), namespace.level(1))
51+
.getCatalogId();
4852
}
4953

50-
if (namespace.levels().length >= 3) {
54+
if (namespace.levels().length == 3) {
5155
parentEntityId =
5256
SchemaMetaService.getInstance()
53-
.getSchemaIdByCatalogIdAndName(parentEntityId, namespace.level(2));
57+
.getSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
58+
namespace.level(0), namespace.level(1), namespace.level(2))
59+
.getSchemaId();
5460
}
61+
5562
Preconditions.checkState(
5663
parentEntityId != null && parentEntityId > 0,
5764
"Parent entity id should not be null and should be greater than 0.");
@@ -63,21 +70,24 @@ public Long[] getParentEntityIdsByNamespace(Namespace namespace) {
6370
!namespace.isEmpty() && namespace.levels().length <= 3,
6471
"Namespace should not be empty and length should be less than or equal to 3.");
6572
Long[] parentEntityIds = new Long[namespace.levels().length];
66-
if (namespace.levels().length >= 1) {
73+
74+
if (namespace.levels().length == 1) {
6775
parentEntityIds[0] =
6876
MetalakeMetaService.getInstance().getMetalakeIdByName(namespace.level(0));
69-
}
70-
71-
if (namespace.levels().length >= 2) {
72-
parentEntityIds[1] =
77+
} else if (namespace.levels().length == 2) {
78+
CatalogIds catalogIds =
7379
CatalogMetaService.getInstance()
74-
.getCatalogIdByMetalakeIdAndName(parentEntityIds[0], namespace.level(1));
75-
}
76-
77-
if (namespace.levels().length >= 3) {
78-
parentEntityIds[2] =
80+
.getCatalogIdByMetalakeAndCatalogName(namespace.level(0), namespace.level(1));
81+
parentEntityIds[0] = catalogIds.getMetalakeId();
82+
parentEntityIds[1] = catalogIds.getCatalogId();
83+
} else if (namespace.levels().length == 3) {
84+
SchemaIds schemaIds =
7985
SchemaMetaService.getInstance()
80-
.getSchemaIdByCatalogIdAndName(parentEntityIds[1], namespace.level(2));
86+
.getSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
87+
namespace.level(0), namespace.level(1), namespace.level(2));
88+
parentEntityIds[0] = schemaIds.getMetalakeId();
89+
parentEntityIds[1] = schemaIds.getCatalogId();
90+
parentEntityIds[2] = schemaIds.getSchemaId();
8191
}
8292

8393
return parentEntityIds;

core/src/main/java/org/apache/gravitino/storage/relational/service/SchemaMetaService.java

+10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.gravitino.meta.ModelEntity;
3535
import org.apache.gravitino.meta.SchemaEntity;
3636
import org.apache.gravitino.meta.TableEntity;
37+
import org.apache.gravitino.storage.relational.helper.SchemaIds;
3738
import org.apache.gravitino.storage.relational.mapper.FilesetMetaMapper;
3839
import org.apache.gravitino.storage.relational.mapper.FilesetVersionMapper;
3940
import org.apache.gravitino.storage.relational.mapper.ModelMetaMapper;
@@ -78,6 +79,15 @@ public SchemaPO getSchemaPOByCatalogIdAndName(Long catalogId, String schemaName)
7879
return schemaPO;
7980
}
8081

82+
public SchemaIds getSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
83+
String metalakeName, String catalogName, String schemaName) {
84+
return SessionUtils.getWithoutCommit(
85+
SchemaMetaMapper.class,
86+
mapper ->
87+
mapper.selectSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
88+
metalakeName, catalogName, schemaName));
89+
}
90+
8191
// Schema may be deleted, so the SchemaPO may be null.
8292
public SchemaPO getSchemaPOById(Long schemaId) {
8393
return SessionUtils.getWithoutCommit(

0 commit comments

Comments
 (0)