Skip to content

Commit 9d965f9

Browse files
github-actions[bot]yuqi1129jerryshao
authored
[#7916] improvement(core): Using entity store's relation operation to refactor tag operation (#8700)
### What changes were proposed in this pull request? Replace `SupportsTagOperations` with the entity store's relation operations ### Why are the changes needed? The entity store's relation operations are general enough, and there is no need to create a new interface specifically for tags. Fix: #7916 ### Does this PR introduce _any_ user-facing change? N/A ### How was this patch tested? Existing UTs and ITs. --------- Co-authored-by: Mini Yu <[email protected]> Co-authored-by: Jerry Shao <[email protected]>
1 parent af0ef56 commit 9d965f9

File tree

9 files changed

+110
-235
lines changed

9 files changed

+110
-235
lines changed

core/src/main/java/org/apache/gravitino/EntityStore.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.apache.commons.lang3.tuple.Pair;
2626
import org.apache.gravitino.Entity.EntityType;
2727
import org.apache.gravitino.exceptions.NoSuchEntityException;
28-
import org.apache.gravitino.tag.SupportsTagOperations;
2928
import org.apache.gravitino.utils.Executable;
3029

3130
public interface EntityStore extends Closeable {
@@ -229,16 +228,6 @@ <E extends Entity & HasIdentifier> void batchPut(List<E> entities, boolean overw
229228
<R, E extends Exception> R executeInTransaction(Executable<R, E> executable)
230229
throws E, IOException;
231230

232-
/**
233-
* Get the extra tag operations that are supported by the entity store.
234-
*
235-
* @return the tag operations object that are supported by the entity store
236-
* @throws UnsupportedOperationException if the extra operations are not supported
237-
*/
238-
default SupportsTagOperations tagOperations() {
239-
throw new UnsupportedOperationException("tag operations are not supported");
240-
}
241-
242231
/**
243232
* Get the extra relation operations that are supported by the entity store.
244233
*

core/src/main/java/org/apache/gravitino/SupportsRelationOperations.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ enum Type {
3939
/** Role and group relationship */
4040
ROLE_GROUP_REL,
4141
/** Policy and metadata object relationship */
42-
POLICY_METADATA_OBJECT_REL
42+
POLICY_METADATA_OBJECT_REL,
43+
/** Metadata object to tag relationship */
44+
TAG_METADATA_OBJECT_REL,
4345
}
4446

4547
/**
@@ -48,7 +50,7 @@ enum Type {
4850
* @param <E> The type of entities returned.
4951
* @param relType The type of relation.
5052
* @param nameIdentifier The given entity identifier.
51-
* @param identType The given entity type.
53+
* @param identType The entity type of parameter nameIdentifier represents.
5254
* @return The list of entities
5355
* @throws IOException When occurs storage issues, it will throw IOException.
5456
*/
@@ -63,13 +65,28 @@ default <E extends Entity & HasIdentifier> List<E> listEntitiesByRelation(
6365
* @param <E> the type of entities returned.
6466
* @param relType The type of relation.
6567
* @param nameIdentifier The given entity identifier
66-
* @param identType The given entity type.
68+
* @param identType The entity type of parameter nameIdentifier represents.
6769
* @param allFields Some fields may have a relatively high acquisition cost, EntityStore provide
6870
* an optional setting to avoid fetching these high-cost fields to improve the performance. If
6971
* true, the method will fetch all the fields, Otherwise, the method will fetch all the fields
7072
* except for high-cost fields.
7173
* @return The list of entities
7274
* @throws IOException When occurs storage issues, it will throw IOException.
75+
* <pre>
76+
* Let's see an example to illustrate how this method works.
77+
* If we want to list all the users who have a specific role, we can use this method as follows:
78+
*
79+
* listEntitiesByRelation(ROLE_USER_REL, name_identifier_of_role, EntityType.ROLE);
80+
*
81+
* This will return a list of User entities that are associated with the specified role.
82+
* Similarly, if we want to list all roles a user has, we can call:
83+
*
84+
* listEntitiesByRelation(ROLE_USER_REL, name_identifier_of_user, EntityType.USER);
85+
*
86+
* That is to say, this method is versatile and can be used to navigate relationships in both
87+
* directions, the start entity is determined by the identType and nameIdentifier parameters.
88+
* The end entity type is determined by the relType parameter and start entity.
89+
* </pre>
7390
*/
7491
<E extends Entity & HasIdentifier> List<E> listEntitiesByRelation(
7592
Type relType, NameIdentifier nameIdentifier, Entity.EntityType identType, boolean allFields)

core/src/main/java/org/apache/gravitino/storage/relational/JDBCBackend.java

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.apache.gravitino.Entity;
3737
import org.apache.gravitino.EntityAlreadyExistsException;
3838
import org.apache.gravitino.HasIdentifier;
39-
import org.apache.gravitino.MetadataObject;
4039
import org.apache.gravitino.NameIdentifier;
4140
import org.apache.gravitino.Namespace;
4241
import org.apache.gravitino.SupportsRelationOperations;
@@ -455,37 +454,6 @@ public void close() throws IOException {
455454
}
456455
}
457456

458-
@Override
459-
public List<MetadataObject> listAssociatedMetadataObjectsForTag(NameIdentifier tagIdent)
460-
throws IOException {
461-
return TagMetaService.getInstance().listAssociatedMetadataObjectsForTag(tagIdent);
462-
}
463-
464-
@Override
465-
public List<TagEntity> listAssociatedTagsForMetadataObject(
466-
NameIdentifier objectIdent, Entity.EntityType objectType)
467-
throws NoSuchEntityException, IOException {
468-
return TagMetaService.getInstance().listTagsForMetadataObject(objectIdent, objectType);
469-
}
470-
471-
@Override
472-
public TagEntity getTagForMetadataObject(
473-
NameIdentifier objectIdent, Entity.EntityType objectType, NameIdentifier tagIdent)
474-
throws NoSuchEntityException, IOException {
475-
return TagMetaService.getInstance().getTagForMetadataObject(objectIdent, objectType, tagIdent);
476-
}
477-
478-
@Override
479-
public List<TagEntity> associateTagsWithMetadataObject(
480-
NameIdentifier objectIdent,
481-
Entity.EntityType objectType,
482-
NameIdentifier[] tagsToAdd,
483-
NameIdentifier[] tagsToRemove)
484-
throws NoSuchEntityException, EntityAlreadyExistsException, IOException {
485-
return TagMetaService.getInstance()
486-
.associateTagsWithMetadataObject(objectIdent, objectType, tagsToAdd, tagsToRemove);
487-
}
488-
489457
@Override
490458
public int batchDelete(
491459
List<Pair<NameIdentifier, Entity.EntityType>> entitiesToDelete, boolean cascade)
@@ -597,6 +565,15 @@ public <E extends Entity & HasIdentifier> List<E> listEntitiesByRelation(
597565
PolicyMetaService.getInstance()
598566
.listPoliciesForMetadataObject(nameIdentifier, identType);
599567
}
568+
569+
case TAG_METADATA_OBJECT_REL:
570+
if (identType == Entity.EntityType.TAG) {
571+
return (List<E>)
572+
TagMetaService.getInstance().listAssociatedMetadataObjectsForTag(nameIdentifier);
573+
} else {
574+
return (List<E>)
575+
TagMetaService.getInstance().listTagsForMetadataObject(nameIdentifier, identType);
576+
}
600577
default:
601578
throw new IllegalArgumentException(
602579
String.format("Doesn't support the relation type %s", relType));
@@ -635,6 +612,11 @@ public <E extends Entity & HasIdentifier> List<E> updateEntityRelations(
635612
PolicyMetaService.getInstance()
636613
.associatePoliciesWithMetadataObject(
637614
srcEntityIdent, srcEntityType, destEntitiesToAdd, destEntitiesToRemove);
615+
case TAG_METADATA_OBJECT_REL:
616+
return (List<E>)
617+
TagMetaService.getInstance()
618+
.associateTagsWithMetadataObject(
619+
srcEntityIdent, srcEntityType, destEntitiesToAdd, destEntitiesToRemove);
638620
default:
639621
throw new IllegalArgumentException(
640622
String.format("Doesn't support the relation type %s", relType));
@@ -653,6 +635,10 @@ public <E extends Entity & HasIdentifier> E getEntityByRelation(
653635
return (E)
654636
PolicyMetaService.getInstance()
655637
.getPolicyForMetadataObject(srcIdentifier, srcType, destEntityIdent);
638+
case TAG_METADATA_OBJECT_REL:
639+
return (E)
640+
TagMetaService.getInstance()
641+
.getTagForMetadataObject(srcIdentifier, srcType, destEntityIdent);
656642
default:
657643
throw new IllegalArgumentException(
658644
String.format("Doesn't support the relation type %s", relType));

core/src/main/java/org/apache/gravitino/storage/relational/RelationalBackend.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@
3131
import org.apache.gravitino.Namespace;
3232
import org.apache.gravitino.SupportsRelationOperations;
3333
import org.apache.gravitino.exceptions.NoSuchEntityException;
34-
import org.apache.gravitino.tag.SupportsTagOperations;
3534

3635
/** Interface defining the operations for a Relation Backend. */
37-
public interface RelationalBackend
38-
extends Closeable, SupportsTagOperations, SupportsRelationOperations {
36+
public interface RelationalBackend extends Closeable, SupportsRelationOperations {
3937

4038
/**
4139
* Initializes the Relational Backend environment with the provided configuration.

core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.apache.gravitino.EntityAlreadyExistsException;
3333
import org.apache.gravitino.EntityStore;
3434
import org.apache.gravitino.HasIdentifier;
35-
import org.apache.gravitino.MetadataObject;
3635
import org.apache.gravitino.NameIdentifier;
3736
import org.apache.gravitino.Namespace;
3837
import org.apache.gravitino.SupportsRelationOperations;
@@ -41,8 +40,6 @@
4140
import org.apache.gravitino.cache.EntityCacheRelationKey;
4241
import org.apache.gravitino.cache.NoOpsCache;
4342
import org.apache.gravitino.exceptions.NoSuchEntityException;
44-
import org.apache.gravitino.meta.TagEntity;
45-
import org.apache.gravitino.tag.SupportsTagOperations;
4643
import org.apache.gravitino.utils.Executable;
4744
import org.slf4j.Logger;
4845
import org.slf4j.LoggerFactory;
@@ -52,8 +49,7 @@
5249
* MySQL, PostgreSQL, etc. If you want to use a different backend, you can implement the {@link
5350
* RelationalBackend} interface
5451
*/
55-
public class RelationalEntityStore
56-
implements EntityStore, SupportsTagOperations, SupportsRelationOperations {
52+
public class RelationalEntityStore implements EntityStore, SupportsRelationOperations {
5753
private static final Logger LOGGER = LoggerFactory.getLogger(RelationalEntityStore.class);
5854
public static final ImmutableMap<String, String> RELATIONAL_BACKENDS =
5955
ImmutableMap.of(
@@ -166,47 +162,11 @@ public void close() throws IOException {
166162
backend.close();
167163
}
168164

169-
@Override
170-
public SupportsTagOperations tagOperations() {
171-
return this;
172-
}
173-
174165
@Override
175166
public SupportsRelationOperations relationOperations() {
176167
return this;
177168
}
178169

179-
@Override
180-
public List<MetadataObject> listAssociatedMetadataObjectsForTag(NameIdentifier tagIdent)
181-
throws IOException {
182-
return backend.listAssociatedMetadataObjectsForTag(tagIdent);
183-
}
184-
185-
@Override
186-
public List<TagEntity> listAssociatedTagsForMetadataObject(
187-
NameIdentifier objectIdent, Entity.EntityType objectType)
188-
throws NoSuchEntityException, IOException {
189-
return backend.listAssociatedTagsForMetadataObject(objectIdent, objectType);
190-
}
191-
192-
@Override
193-
public TagEntity getTagForMetadataObject(
194-
NameIdentifier objectIdent, Entity.EntityType objectType, NameIdentifier tagIdent)
195-
throws NoSuchEntityException, IOException {
196-
return backend.getTagForMetadataObject(objectIdent, objectType, tagIdent);
197-
}
198-
199-
@Override
200-
public List<TagEntity> associateTagsWithMetadataObject(
201-
NameIdentifier objectIdent,
202-
Entity.EntityType objectType,
203-
NameIdentifier[] tagsToAdd,
204-
NameIdentifier[] tagsToRemove)
205-
throws NoSuchEntityException, EntityAlreadyExistsException, IOException {
206-
return backend.associateTagsWithMetadataObject(
207-
objectIdent, objectType, tagsToAdd, tagsToRemove);
208-
}
209-
210170
@Override
211171
public <E extends Entity & HasIdentifier> List<E> listEntitiesByRelation(
212172
Type relType, NameIdentifier nameIdentifier, Entity.EntityType identType, boolean allFields)

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
import org.apache.gravitino.EntityAlreadyExistsException;
3333
import org.apache.gravitino.HasIdentifier;
3434
import org.apache.gravitino.MetadataObject;
35-
import org.apache.gravitino.MetadataObjects;
3635
import org.apache.gravitino.NameIdentifier;
3736
import org.apache.gravitino.Namespace;
3837
import org.apache.gravitino.exceptions.NoSuchEntityException;
3938
import org.apache.gravitino.exceptions.NoSuchTagException;
39+
import org.apache.gravitino.meta.GenericEntity;
4040
import org.apache.gravitino.meta.TagEntity;
4141
import org.apache.gravitino.storage.relational.mapper.TagMetaMapper;
4242
import org.apache.gravitino.storage.relational.mapper.TagMetadataObjectRelMapper;
@@ -218,7 +218,7 @@ public TagEntity getTagForMetadataObject(
218218
return POConverters.fromTagPO(tagPO, NamespaceUtil.ofTag(metalake));
219219
}
220220

221-
public List<MetadataObject> listAssociatedMetadataObjectsForTag(NameIdentifier tagIdent)
221+
public List<GenericEntity> listAssociatedMetadataObjectsForTag(NameIdentifier tagIdent)
222222
throws IOException {
223223
String metalakeName = tagIdent.namespace().level(0);
224224
String tagName = tagIdent.name();
@@ -230,7 +230,7 @@ public List<MetadataObject> listAssociatedMetadataObjectsForTag(NameIdentifier t
230230
mapper ->
231231
mapper.listTagMetadataObjectRelsByMetalakeAndTagName(metalakeName, tagName));
232232

233-
List<MetadataObject> metadataObjects = Lists.newArrayList();
233+
List<GenericEntity> metadataObjects = Lists.newArrayList();
234234
Map<String, List<TagMetadataObjectRelPO>> tagMetadataObjectRelPOsByType =
235235
tagMetadataObjectRelPOs.stream()
236236
.collect(Collectors.groupingBy(TagMetadataObjectRelPO::getMetadataObjectType));
@@ -256,7 +256,11 @@ public List<MetadataObject> listAssociatedMetadataObjectsForTag(NameIdentifier t
256256
// return null, we should skip this metadata object.
257257
if (fullName != null) {
258258
metadataObjects.add(
259-
MetadataObjects.parse(fullName, MetadataObject.Type.valueOf(metadataObjectType)));
259+
GenericEntity.builder()
260+
.withName(fullName)
261+
.withEntityType(Entity.EntityType.valueOf(metadataObjectType))
262+
.withId(metadataObjectName.getKey())
263+
.build());
260264
}
261265
}
262266
}

core/src/main/java/org/apache/gravitino/tag/SupportsTagOperations.java

Lines changed: 0 additions & 95 deletions
This file was deleted.

0 commit comments

Comments
 (0)