diff --git a/api/src/main/java/com/datastrato/gravitino/exceptions/NoSuchEntityException.java b/api/src/main/java/com/datastrato/gravitino/exceptions/NoSuchEntityException.java index e3f391162fc..27ba0569e3b 100644 --- a/api/src/main/java/com/datastrato/gravitino/exceptions/NoSuchEntityException.java +++ b/api/src/main/java/com/datastrato/gravitino/exceptions/NoSuchEntityException.java @@ -9,8 +9,8 @@ /** This exception is thrown when an entity is not found. */ public class NoSuchEntityException extends RuntimeException { - /** The no such an entity message for the exception. */ - public static final String NO_SUCH_AN_ENTITY_MESSAGE = "No such an entity: %s"; + /** The no such entity message for the exception. */ + public static final String NO_SUCH_ENTITY_MESSAGE = "No such entity: %s"; /** * Constructs a new NoSuchEntityException. diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/service/CatalogMetaService.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/service/CatalogMetaService.java index 2a9f9236e54..2d329c700d1 100644 --- a/core/src/main/java/com/datastrato/gravitino/storage/relational/service/CatalogMetaService.java +++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/service/CatalogMetaService.java @@ -36,7 +36,7 @@ public static CatalogMetaService getInstance() { private CatalogMetaService() {} public CatalogEntity getCatalogByIdentifier(NameIdentifier identifier) { - checkCatalogNamespaceWithIdentifier(identifier); + NameIdentifier.checkCatalog(identifier); String metalakeName = identifier.namespace().level(0); String catalogName = identifier.name(); Long metalakeId = @@ -44,7 +44,7 @@ public CatalogEntity getCatalogByIdentifier(NameIdentifier identifier) { MetalakeMetaMapper.class, mapper -> mapper.selectMetalakeIdMetaByName(metalakeName)); if (metalakeId == null) { throw new NoSuchEntityException( - NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, identifier.namespace().toString()); + NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE, identifier.namespace().toString()); } CatalogPO catalogPO = SessionUtils.getWithoutCommit( @@ -52,20 +52,20 @@ public CatalogEntity getCatalogByIdentifier(NameIdentifier identifier) { mapper -> mapper.selectCatalogMetaByMetalakeIdAndName(metalakeId, catalogName)); if (catalogPO == null) { throw new NoSuchEntityException( - NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, identifier.toString()); + NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE, identifier.toString()); } return POConverters.fromCatalogPO(catalogPO, identifier.namespace()); } public List listCatalogsByNamespace(Namespace namespace) { - checkCatalogNamespace(namespace); + Namespace.checkCatalog(namespace); String metalakeName = namespace.level(0); Long metalakeId = SessionUtils.getWithoutCommit( MetalakeMetaMapper.class, mapper -> mapper.selectMetalakeIdMetaByName(metalakeName)); if (metalakeId == null) { throw new NoSuchEntityException( - NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, namespace.toString()); + NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE, namespace.toString()); } List catalogPOS = SessionUtils.getWithoutCommit( @@ -75,14 +75,14 @@ public List listCatalogsByNamespace(Namespace namespace) { public void insertCatalog(CatalogEntity catalogEntity, boolean overwrite) { try { - checkCatalogNamespaceWithIdentifier(catalogEntity.nameIdentifier()); + NameIdentifier.checkCatalog(catalogEntity.nameIdentifier()); Long metalakeId = SessionUtils.getWithoutCommit( MetalakeMetaMapper.class, mapper -> mapper.selectMetalakeIdMetaByName(catalogEntity.namespace().level(0))); if (metalakeId == null) { throw new NoSuchEntityException( - NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, catalogEntity.namespace().toString()); + NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE, catalogEntity.namespace().toString()); } SessionUtils.doWithCommit( CatalogMetaMapper.class, @@ -103,8 +103,7 @@ public void insertCatalog(CatalogEntity catalogEntity, boolean overwrite) { // SQL violates the constraints of `primary key` and `unique key`. // We simply think that the entity already exists at this time. throw new EntityAlreadyExistsException( - String.format( - "Catalog entity: %s already exists", catalogEntity.nameIdentifier().name())); + String.format("Catalog entity: %s already exists", catalogEntity.nameIdentifier())); } throw re; } @@ -112,7 +111,7 @@ public void insertCatalog(CatalogEntity catalogEntity, boolean overwrite) { public CatalogEntity updateCatalog( NameIdentifier identifier, Function updater) throws IOException { - checkCatalogNamespaceWithIdentifier(identifier); + NameIdentifier.checkCatalog(identifier); String metalakeName = identifier.namespace().level(0); String catalogName = identifier.name(); Long metalakeId = @@ -120,7 +119,7 @@ public CatalogEntity updateCatalog( MetalakeMetaMapper.class, mapper -> mapper.selectMetalakeIdMetaByName(metalakeName)); if (metalakeId == null) { throw new NoSuchEntityException( - NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, identifier.namespace().toString()); + NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE, identifier.namespace().toString()); } CatalogPO oldCatalogPO = @@ -129,7 +128,7 @@ public CatalogEntity updateCatalog( mapper -> mapper.selectCatalogMetaByMetalakeIdAndName(metalakeId, catalogName)); if (oldCatalogPO == null) { throw new NoSuchEntityException( - NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, identifier.toString()); + NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE, identifier.toString()); } CatalogEntity oldCatalogEntity = @@ -157,7 +156,7 @@ public CatalogEntity updateCatalog( } public boolean deleteCatalog(NameIdentifier identifier, boolean cascade) { - checkCatalogNamespaceWithIdentifier(identifier); + NameIdentifier.checkCatalog(identifier); String metalakeName = identifier.namespace().level(0); String catalogName = identifier.name(); Long metalakeId = @@ -165,7 +164,7 @@ public boolean deleteCatalog(NameIdentifier identifier, boolean cascade) { MetalakeMetaMapper.class, mapper -> mapper.selectMetalakeIdMetaByName(metalakeName)); if (metalakeId == null) { throw new NoSuchEntityException( - NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, identifier.namespace().toString()); + NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE, identifier.namespace().toString()); } Long catalogId = SessionUtils.getWithoutCommit( @@ -190,14 +189,4 @@ public boolean deleteCatalog(NameIdentifier identifier, boolean cascade) { } return true; } - - private void checkCatalogNamespaceWithIdentifier(NameIdentifier identifier) { - Preconditions.checkArgument( - identifier.hasNamespace() && identifier.namespace().levels().length == 1, - "Only support one level namespace"); - } - - private void checkCatalogNamespace(Namespace namespace) { - Preconditions.checkArgument(namespace.levels().length == 1, "Only support one level namespace"); - } } diff --git a/core/src/main/java/com/datastrato/gravitino/storage/relational/service/MetalakeMetaService.java b/core/src/main/java/com/datastrato/gravitino/storage/relational/service/MetalakeMetaService.java index 0ac0dfe7f60..46810f39514 100644 --- a/core/src/main/java/com/datastrato/gravitino/storage/relational/service/MetalakeMetaService.java +++ b/core/src/main/java/com/datastrato/gravitino/storage/relational/service/MetalakeMetaService.java @@ -46,18 +46,20 @@ public List listMetalakes() { } public BaseMetalake getMetalakeByIdentifier(NameIdentifier ident) { + NameIdentifier.checkMetalake(ident); MetalakePO metalakePO = SessionUtils.getWithoutCommit( MetalakeMetaMapper.class, mapper -> mapper.selectMetalakeMetaByName(ident.name())); if (metalakePO == null) { throw new NoSuchEntityException( - NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, ident.toString()); + NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE, ident.toString()); } return POConverters.fromMetalakePO(metalakePO); } public void insertMetalake(BaseMetalake baseMetalake, boolean overwrite) { try { + NameIdentifier.checkMetalake(baseMetalake.nameIdentifier()); SessionUtils.doWithCommit( MetalakeMetaMapper.class, mapper -> { @@ -77,8 +79,7 @@ public void insertMetalake(BaseMetalake baseMetalake, boolean overwrite) { // SQL violates the constraints of `primary key` and `unique key`. // We simply think that the entity already exists at this time. throw new EntityAlreadyExistsException( - String.format( - "Metalake entity: %s already exists", baseMetalake.nameIdentifier().name())); + String.format("Metalake entity: %s already exists", baseMetalake.nameIdentifier())); } throw re; } @@ -86,12 +87,13 @@ public void insertMetalake(BaseMetalake baseMetalake, boolean overwrite) { public BaseMetalake updateMetalake( NameIdentifier ident, Function updater) throws IOException { + NameIdentifier.checkMetalake(ident); MetalakePO oldMetalakePO = SessionUtils.getWithoutCommit( MetalakeMetaMapper.class, mapper -> mapper.selectMetalakeMetaByName(ident.name())); if (oldMetalakePO == null) { throw new NoSuchEntityException( - NoSuchEntityException.NO_SUCH_AN_ENTITY_MESSAGE, ident.toString()); + NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE, ident.toString()); } BaseMetalake oldMetalakeEntity = POConverters.fromMetalakePO(oldMetalakePO); @@ -116,6 +118,7 @@ public BaseMetalake updateMetalake( } public boolean deleteMetalake(NameIdentifier ident, boolean cascade) { + NameIdentifier.checkMetalake(ident); Long metalakeId = SessionUtils.getWithoutCommit( MetalakeMetaMapper.class, mapper -> mapper.selectMetalakeIdMetaByName(ident.name())); diff --git a/core/src/test/java/com/datastrato/gravitino/storage/relational/TestRelationalEntityStore.java b/core/src/test/java/com/datastrato/gravitino/storage/relational/TestRelationalEntityStore.java index 74271d0914e..5ad7118f7c0 100644 --- a/core/src/test/java/com/datastrato/gravitino/storage/relational/TestRelationalEntityStore.java +++ b/core/src/test/java/com/datastrato/gravitino/storage/relational/TestRelationalEntityStore.java @@ -47,9 +47,11 @@ import java.sql.Statement; import java.time.Instant; import java.util.ArrayList; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; import org.apache.ibatis.session.SqlSession; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -206,7 +208,10 @@ public void testPutAndList() throws IOException { entityStore.put(metalake1, false); entityStore.put(metalake2, false); List metalakes = - entityStore.list(metalake1.namespace(), BaseMetalake.class, Entity.EntityType.METALAKE); + entityStore.list(metalake1.namespace(), BaseMetalake.class, Entity.EntityType.METALAKE) + .stream() + .sorted(Comparator.comparing(BaseMetalake::id)) + .collect(Collectors.toList()); assertNotNull(metalakes); assertEquals(2, metalakes.size()); assertTrue(checkMetalakeEquals(metalake1, metalakes.get(0))); @@ -227,7 +232,10 @@ public void testPutAndList() throws IOException { entityStore.put(catalog1, false); entityStore.put(catalog2, false); List catalogEntities = - entityStore.list(catalog1.namespace(), CatalogEntity.class, Entity.EntityType.CATALOG); + entityStore.list(catalog1.namespace(), CatalogEntity.class, Entity.EntityType.CATALOG) + .stream() + .sorted(Comparator.comparing(CatalogEntity::id)) + .collect(Collectors.toList()); assertNotNull(catalogEntities); assertEquals(2, catalogEntities.size()); assertTrue(checkCatalogEquals(catalog1, catalogEntities.get(0)));