Skip to content

Commit 30309ba

Browse files
committed
Improve the code
1 parent 33a87aa commit 30309ba

File tree

6 files changed

+82
-18
lines changed

6 files changed

+82
-18
lines changed

api/src/main/java/org/apache/gravitino/Catalog.java

+28-5
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,42 @@ public interface Catalog extends Auditable {
4040
/** The type of the catalog. */
4141
enum Type {
4242
/** Catalog Type for Relational Data Structure, like db.table, catalog.db.table. */
43-
RELATIONAL,
43+
RELATIONAL(false),
4444

4545
/** Catalog Type for Fileset System (including HDFS, S3, etc.), like path/to/file */
46-
FILESET,
46+
FILESET(false),
4747

4848
/** Catalog Type for Message Queue, like Kafka://topic */
49-
MESSAGING,
49+
MESSAGING(false),
5050

5151
/** Catalog Type for ML model */
52-
MODEL,
52+
MODEL(true),
5353

5454
/** Catalog Type for test only. */
55-
UNSUPPORTED;
55+
UNSUPPORTED(false);
56+
57+
private final boolean supportsManagedCatalog;
58+
59+
Type(boolean supportsManagedCatalog) {
60+
this.supportsManagedCatalog = supportsManagedCatalog;
61+
}
62+
63+
/**
64+
* A flag to indicate whether the catalog type supports managed catalog. Managed catalog is a
65+
* concept in Gravitino, for the details of managed catalog, please refer to the class comment
66+
* of {@link CatalogProvider}. If the catalog type supports managed catalog, users can create
67+
* managed catalog of this type without specifying the provider, Gravitino will use the type as
68+
* the provider to create the managed catalog. If the catalog type does not support managed
69+
* catalog, users need to specify the provider when creating the catalog.
70+
*
71+
* <p>Currently, only the model catalog supports managed catalog.
72+
*
73+
* @return Whether the catalog type supports managed catalog. Returns true if the catalog type
74+
* supports managed catalog.
75+
*/
76+
public boolean supportsManagedCatalog() {
77+
return supportsManagedCatalog;
78+
}
5679

5780
/**
5881
* Convert the string (case-insensitive) to the catalog type.

api/src/main/java/org/apache/gravitino/CatalogProvider.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,32 @@
2424
/**
2525
* A Catalog provider is a class that provides a short name for a catalog. This short name is used
2626
* when creating a catalog.
27+
*
28+
* <p>There are two kinds of catalogs in Gravitino, one is managed catalog and another is external
29+
* catalog.
30+
*
31+
* <p>Managed catalog: A catalog and its subsidiary objects are all managed by Gravitino. Gravitino
32+
* takes care of the lifecycle of the catalog and its objects. For those catalogs, Gravitino uses
33+
* the type of the catalog as the provider short name. Note that for each catalog type, there is
34+
* only one implementation of managed catalog for that type. Currently, Gravitino only has model
35+
* catalog that is a managed catalog.
36+
*
37+
* <p>External catalog: A catalog its subsidiary objects are stored by an external sources, such as
38+
* Hive catalog, the DB and tables are stored in HMS. For those catalogs, Gravitino uses a unique
39+
* name as the provider short name to load the catalog. For example, Hive catalog uses "hive" as the
40+
* provider short name.
2741
*/
2842
@Evolving
2943
public interface CatalogProvider {
3044

3145
/**
32-
* Form the provider short name for a builtin catalog. The provider short name for a builtin
46+
* Form the provider short name for a managed catalog. The provider short name for a managed
3347
* catalog is the catalog type in lowercase.
3448
*
3549
* @param type The catalog type.
36-
* @return The provider short name for the builtin catalog.
50+
* @return The provider short name for the managed catalog.
3751
*/
38-
static String builtinShortName(Catalog.Type type) {
52+
static String shortNameForManagedCatalog(Catalog.Type type) {
3953
return type.name().toLowerCase(Locale.ROOT);
4054
}
4155

api/src/main/java/org/apache/gravitino/SupportsCatalogs.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ default boolean catalogExists(String catalogName) {
8181
* catalog should be created. The short name:
8282
*
8383
* <p>1) should be the same as the {@link CatalogProvider} interface provided. 2) can be "null" if
84-
* the created catalog is a built-in catalog, like model catalog.
84+
* the created catalog is the managed catalog, like model catalog. For the details of the provider
85+
* definition, see {@link CatalogProvider}.
8586
*
8687
* @param catalogName the name of the catalog.
8788
* @param type the type of the catalog.
88-
* @param provider the provider of the catalog, or null if the catalog is a built-in catalog.
89+
* @param provider the provider of the catalog, or null if the catalog is a managed catalog.
8990
* @param comment the comment of the catalog.
9091
* @param properties the properties of the catalog.
9192
* @return The created catalog.
@@ -101,7 +102,7 @@ Catalog createCatalog(
101102
throws NoSuchMetalakeException, CatalogAlreadyExistsException;
102103

103104
/**
104-
* Create a built-in catalog with specified catalog name, type, comment, and properties.
105+
* Create a managed catalog with specified catalog name, type, comment, and properties.
105106
*
106107
* @param catalogName the name of the catalog.
107108
* @param type the type of the catalog.

catalogs/catalog-model/src/main/java/org/apache/gravitino/catalog/model/ModelCatalog.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class ModelCatalog extends BaseCatalog<ModelCatalog> {
3838

3939
@Override
4040
public String shortName() {
41-
return CatalogProvider.builtinShortName(super.type());
41+
return CatalogProvider.shortNameForManagedCatalog(super.type());
4242
}
4343

4444
@Override

common/src/main/java/org/apache/gravitino/dto/requests/CatalogCreateRequest.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,25 @@ public CatalogCreateRequest(
8282
}
8383

8484
/**
85-
* Sets the provider of the catalog.
85+
* Sets the provider of the catalog if it is null. The value of provider in the request can be
86+
* null if the catalog is a managed catalog. For such request, the value will be set when it is
87+
* deserialized.
8688
*
8789
* @param provider The provider of the catalog.
8890
*/
8991
@JsonSetter(value = "provider")
9092
public void setProvider(String provider) {
91-
this.provider =
92-
StringUtils.isNotBlank(provider) ? provider : CatalogProvider.builtinShortName(type);
93+
if (StringUtils.isNotBlank(provider)) {
94+
this.provider = provider;
95+
} else if (type != null && type.supportsManagedCatalog()) {
96+
this.provider = CatalogProvider.shortNameForManagedCatalog(type);
97+
} else {
98+
throw new IllegalStateException(
99+
"Provider cannot be null for catalog type "
100+
+ type
101+
+ " that doesn't support managed "
102+
+ "catalog");
103+
}
93104
}
94105

95106
/**
@@ -102,5 +113,10 @@ public void validate() throws IllegalArgumentException {
102113
Preconditions.checkArgument(
103114
StringUtils.isNotBlank(name), "\"name\" field is required and cannot be empty");
104115
Preconditions.checkArgument(type != null, "\"type\" field is required and cannot be empty");
116+
Preconditions.checkArgument(
117+
StringUtils.isNotBlank(provider) || type.supportsManagedCatalog(),
118+
"\"provider\" field is required and cannot be empty for catalog type "
119+
+ type
120+
+ " that doesn't support managed catalog");
105121
}
106122
}

common/src/test/java/org/apache/gravitino/dto/requests/TestCatalogCreateRequest.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.gravitino.dto.requests;
2020

2121
import com.fasterxml.jackson.core.JsonProcessingException;
22+
import com.fasterxml.jackson.databind.JsonMappingException;
2223
import com.google.common.collect.ImmutableMap;
2324
import java.util.Locale;
2425
import org.apache.gravitino.Catalog;
@@ -33,7 +34,7 @@ public void testCatalogCreateRequestSerDe() throws JsonProcessingException {
3334
CatalogCreateRequest request =
3435
new CatalogCreateRequest(
3536
"catalog_test",
36-
Catalog.Type.RELATIONAL,
37+
Catalog.Type.MODEL,
3738
"provider_test",
3839
"catalog comment",
3940
ImmutableMap.of("key", "value"));
@@ -44,14 +45,14 @@ public void testCatalogCreateRequestSerDe() throws JsonProcessingException {
4445

4546
Assertions.assertEquals(request, deserRequest);
4647
Assertions.assertEquals("catalog_test", deserRequest.getName());
47-
Assertions.assertEquals(Catalog.Type.RELATIONAL, deserRequest.getType());
48+
Assertions.assertEquals(Catalog.Type.MODEL, deserRequest.getType());
4849
Assertions.assertEquals("provider_test", deserRequest.getProvider());
4950
Assertions.assertEquals("catalog comment", deserRequest.getComment());
5051
Assertions.assertEquals(ImmutableMap.of("key", "value"), deserRequest.getProperties());
5152

5253
// Test with null provider, comment and properties
5354
CatalogCreateRequest request1 =
54-
new CatalogCreateRequest("catalog_test", Catalog.Type.RELATIONAL, null, null, null);
55+
new CatalogCreateRequest("catalog_test", Catalog.Type.MODEL, null, null, null);
5556

5657
String serJson1 = JsonUtils.objectMapper().writeValueAsString(request1);
5758
CatalogCreateRequest deserRequest1 =
@@ -61,5 +62,14 @@ public void testCatalogCreateRequestSerDe() throws JsonProcessingException {
6162
deserRequest1.getType().name().toLowerCase(Locale.ROOT), deserRequest1.getProvider());
6263
Assertions.assertNull(deserRequest1.getComment());
6364
Assertions.assertNull(deserRequest1.getProperties());
65+
66+
// Test using null provider with catalog type doesn't support managed catalog
67+
CatalogCreateRequest request2 =
68+
new CatalogCreateRequest("catalog_test", Catalog.Type.RELATIONAL, null, null, null);
69+
70+
String serJson2 = JsonUtils.objectMapper().writeValueAsString(request2);
71+
Assertions.assertThrows(
72+
JsonMappingException.class,
73+
() -> JsonUtils.objectMapper().readValue(serJson2, CatalogCreateRequest.class));
6474
}
6575
}

0 commit comments

Comments
 (0)