Skip to content

Commit 8fea43a

Browse files
committed
feat(listSecurableObjects): add support for TOPIC batch retrieval
1 parent f9b0f48 commit 8fea43a

File tree

6 files changed

+148
-6
lines changed

6 files changed

+148
-6
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public interface TopicMetaMapper {
4040
@SelectProvider(type = TopicMetaSQLProviderFactory.class, method = "listTopicPOsBySchemaId")
4141
List<TopicPO> listTopicPOsBySchemaId(@Param("schemaId") Long schemaId);
4242

43+
@SelectProvider(type = TopicMetaSQLProviderFactory.class, method = "listTopicPOsByTopicIds")
44+
List<TopicPO> listTopicPOsByTopicIds(@Param("topicIds") List<Long> topicIds);
45+
4346
@SelectProvider(
4447
type = TopicMetaSQLProviderFactory.class,
4548
method = "selectTopicMetaBySchemaIdAndName")

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

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

2222
import com.google.common.collect.ImmutableMap;
23+
import java.util.List;
2324
import java.util.Map;
2425
import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType;
2526
import org.apache.gravitino.storage.relational.mapper.provider.base.TopicMetaBaseSQLProvider;
@@ -63,6 +64,10 @@ public static String listTopicPOsBySchemaId(@Param("schemaId") Long schemaId) {
6364
return getProvider().listTopicPOsBySchemaId(schemaId);
6465
}
6566

67+
public static String listTopicPOsByTopicIds(@Param("topicIds") List<Long> topicIds) {
68+
return getProvider().listTopicPOsByTopicIds(topicIds);
69+
}
70+
6671
public static String selectTopicMetaBySchemaIdAndName(
6772
@Param("schemaId") Long schemaId, @Param("topicName") String topicName) {
6873
return getProvider().selectTopicMetaBySchemaIdAndName(schemaId, topicName);

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

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import static org.apache.gravitino.storage.relational.mapper.TopicMetaMapper.TABLE_NAME;
2323

24+
import java.util.List;
2425
import org.apache.gravitino.storage.relational.po.TopicPO;
2526
import org.apache.ibatis.annotations.Param;
2627

@@ -90,6 +91,24 @@ public String listTopicPOsBySchemaId(@Param("schemaId") Long schemaId) {
9091
+ " WHERE schema_id = #{schemaId} AND deleted_at = 0";
9192
}
9293

94+
public String listTopicPOsByTopicIds(@Param("topicIds") List<Long> topicIds) {
95+
return "<script>"
96+
+ " SELECT topic_id as topicId, topic_name as topicName, metalake_id as metalakeId,"
97+
+ " catalog_id as catalogId, schema_id as schemaId,"
98+
+ " comment as comment, properties as properties, audit_info as auditInfo,"
99+
+ " current_version as currentVersion, last_version as lastVersion,"
100+
+ " deleted_at as deletedAt"
101+
+ " FROM "
102+
+ TABLE_NAME
103+
+ " WHERE deleted_at = 0"
104+
+ " AND topic_id in ("
105+
+ "<foreach collection='topicIds' item='topicId' separator=','>"
106+
+ "#{topicId}"
107+
+ "</foreach>"
108+
+ ") "
109+
+ "</script>";
110+
}
111+
93112
public String selectTopicMetaBySchemaIdAndName(
94113
@Param("schemaId") Long schemaId, @Param("topicName") String topicName) {
95114
return "SELECT topic_id as topicId, topic_name as topicName,"

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

+111-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
import org.apache.gravitino.storage.relational.mapper.MetalakeMetaMapper;
3232
import org.apache.gravitino.storage.relational.mapper.ModelMetaMapper;
3333
import org.apache.gravitino.storage.relational.mapper.SchemaMetaMapper;
34+
import org.apache.gravitino.storage.relational.mapper.TableColumnMapper;
3435
import org.apache.gravitino.storage.relational.mapper.TableMetaMapper;
36+
import org.apache.gravitino.storage.relational.mapper.TopicMetaMapper;
3537
import org.apache.gravitino.storage.relational.po.CatalogPO;
3638
import org.apache.gravitino.storage.relational.po.ColumnPO;
3739
import org.apache.gravitino.storage.relational.po.FilesetPO;
@@ -386,6 +388,103 @@ public static Map<Long, String> getTableObjectFullNames(List<Long> ids) {
386388
return tableIdAndNameMap;
387389
}
388390

391+
public static Map<Long, String> getTopicObjectFullNames(List<Long> ids) {
392+
List<TopicPO> topicPOs =
393+
SessionUtils.getWithoutCommit(
394+
TopicMetaMapper.class, mapper -> mapper.listTopicPOsByTopicIds(ids));
395+
396+
if (topicPOs == null || topicPOs.isEmpty()) {
397+
return new HashMap<>();
398+
}
399+
400+
List<Long> catalogIds =
401+
topicPOs.stream().map(TopicPO::getCatalogId).collect(Collectors.toList());
402+
List<Long> schemaIds = topicPOs.stream().map(TopicPO::getSchemaId).collect(Collectors.toList());
403+
404+
Map<Long, String> catalogIdAndNameMap = getCatalogIdAndNameMap(catalogIds);
405+
Map<Long, String> schemaIdAndNameMap = getSchemaIdAndNameMap(schemaIds);
406+
407+
HashMap<Long, String> topicIdAndNameMap = new HashMap<>();
408+
409+
topicPOs.forEach(
410+
tablePO -> {
411+
// since the catalog or schema can be deleted, we need to check the null value,
412+
// and when catalog or schema is deleted, we will set fullName of tablePO to null
413+
String catalogName = catalogIdAndNameMap.getOrDefault(tablePO.getCatalogId(), null);
414+
if (catalogName == null) {
415+
LOG.warn("The catalog of topic {} may be deleted", tablePO.getTopicId());
416+
topicIdAndNameMap.put(tablePO.getTopicId(), null);
417+
return;
418+
}
419+
420+
String schemaName = schemaIdAndNameMap.getOrDefault(tablePO.getSchemaId(), null);
421+
if (schemaName == null) {
422+
LOG.warn("The schema of topic {} may be deleted", tablePO.getTopicId());
423+
topicIdAndNameMap.put(tablePO.getTopicId(), null);
424+
return;
425+
}
426+
427+
String fullName = DOT_JOINER.join(catalogName, schemaName, tablePO.getTopicName());
428+
topicIdAndNameMap.put(tablePO.getTopicId(), fullName);
429+
});
430+
431+
return topicIdAndNameMap;
432+
}
433+
434+
public static Map<Long, String> getColumnObjectFullNames(List<Long> ids) {
435+
List<ColumnPO> columnPOs =
436+
SessionUtils.getWithoutCommit(
437+
TableColumnMapper.class, mapper -> mapper.listColumnPOsByColumnIds(ids));
438+
439+
if (columnPOs == null || columnPOs.isEmpty()) {
440+
return new HashMap<>();
441+
}
442+
443+
List<Long> catalogIds =
444+
columnPOs.stream().map(ColumnPO::getCatalogId).collect(Collectors.toList());
445+
List<Long> schemaIds =
446+
columnPOs.stream().map(ColumnPO::getSchemaId).collect(Collectors.toList());
447+
List<Long> tableIds = columnPOs.stream().map(ColumnPO::getTableId).collect(Collectors.toList());
448+
449+
Map<Long, String> catalogIdAndNameMap = getCatalogIdAndNameMap(catalogIds);
450+
Map<Long, String> schemaIdAndNameMap = getSchemaIdAndNameMap(schemaIds);
451+
Map<Long, String> tableIdAndNameMap = getTableIdAndNameMap(tableIds);
452+
453+
HashMap<Long, String> columnIdAndNameMap = new HashMap<>();
454+
455+
columnPOs.forEach(
456+
columnPO -> {
457+
// since the catalog or schema can be deleted, we need to check the null value,
458+
// and when catalog or schema is deleted, we will set fullName of filesetPO to null
459+
String catalogName = catalogIdAndNameMap.getOrDefault(columnPO.getCatalogId(), null);
460+
if (catalogName == null) {
461+
LOG.warn("The catalog of column {} may be deleted", columnPO.getColumnId());
462+
columnIdAndNameMap.put(columnPO.getColumnId(), null);
463+
return;
464+
}
465+
466+
String schemaName = schemaIdAndNameMap.getOrDefault(columnPO.getSchemaId(), null);
467+
if (schemaName == null) {
468+
LOG.warn("The schema of column {} may be deleted", columnPO.getColumnId());
469+
columnIdAndNameMap.put(columnPO.getColumnId(), null);
470+
return;
471+
}
472+
473+
String tableName = tableIdAndNameMap.getOrDefault(columnPO.getTableId(), null);
474+
if (tableName == null) {
475+
LOG.warn("The table of column {} may be deleted", columnPO.getColumnId());
476+
columnIdAndNameMap.put(columnPO.getColumnId(), null);
477+
return;
478+
}
479+
480+
String fullName =
481+
DOT_JOINER.join(catalogName, schemaName, tableName, columnPO.getColumnName());
482+
columnIdAndNameMap.put(columnPO.getColumnId(), fullName);
483+
});
484+
485+
return columnIdAndNameMap;
486+
}
487+
389488
public static Map<Long, String> getCatalogObjectFullNames(List<Long> ids) {
390489
List<CatalogPO> catalogPOs =
391490
SessionUtils.getWithoutCommit(
@@ -409,6 +508,14 @@ public static Map<Long, String> getCatalogObjectFullNames(List<Long> ids) {
409508
return catalogIdAndNameMap;
410509
}
411510

511+
public static Map<Long, String> getCatalogIdAndNameMap(List<Long> catalogIds) {
512+
List<CatalogPO> catalogPOs =
513+
SessionUtils.getWithoutCommit(
514+
CatalogMetaMapper.class, mapper -> mapper.listCatalogPOsByCatalogIds(catalogIds));
515+
return catalogPOs.stream()
516+
.collect(Collectors.toMap(CatalogPO::getCatalogId, CatalogPO::getCatalogName));
517+
}
518+
412519
public static Map<Long, String> getSchemaIdAndNameMap(List<Long> schemaIds) {
413520
List<SchemaPO> schemaPOS =
414521
SessionUtils.getWithoutCommit(
@@ -417,11 +524,10 @@ public static Map<Long, String> getSchemaIdAndNameMap(List<Long> schemaIds) {
417524
.collect(Collectors.toMap(SchemaPO::getSchemaId, SchemaPO::getSchemaName));
418525
}
419526

420-
public static Map<Long, String> getCatalogIdAndNameMap(List<Long> catalogIds) {
421-
List<CatalogPO> catalogPOs =
527+
public static Map<Long, String> getTableIdAndNameMap(List<Long> tableIds) {
528+
List<TablePO> tablePOS =
422529
SessionUtils.getWithoutCommit(
423-
CatalogMetaMapper.class, mapper -> mapper.listCatalogPOsByCatalogIds(catalogIds));
424-
return catalogPOs.stream()
425-
.collect(Collectors.toMap(CatalogPO::getCatalogId, CatalogPO::getCatalogName));
530+
TableMetaMapper.class, mapper -> mapper.listTablePOsByTableIds(tableIds));
531+
return tablePOS.stream().collect(Collectors.toMap(TablePO::getTableId, TablePO::getTableName));
426532
}
427533
}

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ private static List<SecurableObject> listSecurableObjects(RolePO po) {
368368
case FILESET:
369369
case TABLE:
370370
case MODEL:
371+
case TOPIC:
371372
List<Long> objectIds =
372373
objects.stream()
373374
.map(SecurableObjectPO::getMetadataObjectId)
@@ -385,7 +386,9 @@ private static List<SecurableObject> listSecurableObjects(RolePO po) {
385386
MetadataObject.Type.TABLE,
386387
MetadataObjectService::getTableObjectFullNames,
387388
MetadataObject.Type.MODEL,
388-
MetadataObjectService::getModelObjectFullNames);
389+
MetadataObjectService::getModelObjectFullNames,
390+
MetadataObject.Type.TOPIC,
391+
MetadataObjectService::getTopicObjectFullNames);
389392

390393
// dynamically calling getter function based on type
391394
Map<Long, String> objectIdAndNameMap =

core/src/test/java/org/apache/gravitino/storage/relational/service/TestSecurableObjects.java

+6
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,15 @@ public void testAllTypeSecurableObjects() throws IOException {
8080
"fileset",
8181
auditInfo);
8282
backend.insert(fileset, false);
83+
8384
TableEntity table =
8485
createTableEntity(
8586
RandomIdGenerator.INSTANCE.nextId(),
8687
Namespace.of("metalake", "catalog", "schema"),
8788
"table",
8889
auditInfo);
8990
backend.insert(table, false);
91+
9092
TopicEntity topic =
9193
createTopicEntity(
9294
RandomIdGenerator.INSTANCE.nextId(),
@@ -122,15 +124,19 @@ public void testAllTypeSecurableObjects() throws IOException {
122124
SecurableObject schemaObject =
123125
SecurableObjects.ofSchema(
124126
catalogObject, "schema", Lists.newArrayList(Privileges.UseSchema.allow()));
127+
125128
SecurableObject tableObject =
126129
SecurableObjects.ofTable(
127130
schemaObject, "table", Lists.newArrayList(Privileges.SelectTable.allow()));
131+
128132
SecurableObject filesetObject =
129133
SecurableObjects.ofFileset(
130134
schemaObject, "fileset", Lists.newArrayList(Privileges.ReadFileset.allow()));
135+
131136
SecurableObject topicObject =
132137
SecurableObjects.ofTopic(
133138
schemaObject, "topic", Lists.newArrayList(Privileges.ConsumeTopic.deny()));
139+
134140
SecurableObject modelObject =
135141
SecurableObjects.ofModel(
136142
schemaObject, "model", Lists.newArrayList(Privileges.ConsumeTopic.deny()));

0 commit comments

Comments
 (0)