World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#89] feat(catalog): introduce `CatalogProvider` interface and refactor catalog implementation (#93)
Jerry Shao7c7a1147cbd1
diff --git a/api/src/main/java/com/datastrato/graviton/CatalogProvider.java b/api/src/main/java/com/datastrato/graviton/CatalogProvider.javanew file mode 100644index 0000000000..b39eec3432--- /dev/null+++ b/api/src/main/java/com/datastrato/graviton/CatalogProvider.java@@ -0,0 +1,15 @@+package com.datastrato.graviton;++/**+ * A Catalog provider is a class that provides a short name for a catalog. This short is used when+ * creating a catalog, Graviton will search the short name and its related class name to create the+ * catalog.+ */+public interface CatalogProvider {++ /**+ * The string that represents the catalog that this provider uses. This is overridden by children+ * to provide a nice alias for the catalog.+ */+ String shortName();+}diff --git a/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalog.java b/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalog.javaindex e11d1b45f8..82f52a077d 100644--- a/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalog.java+++ b/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalog.java@@ -1,346 +1,26 @@package com.datastrato.graviton.catalog.hive;-import com.datastrato.graviton.*;-import com.datastrato.graviton.catalog.hive.rel.HiveSchema;-import com.datastrato.graviton.exceptions.NoSuchNamespaceException;-import com.datastrato.graviton.exceptions.NoSuchSchemaException;-import com.datastrato.graviton.exceptions.NonEmptySchemaException;-import com.datastrato.graviton.exceptions.SchemaAlreadyExistsException;-import com.datastrato.graviton.meta.AuditInfo;-import com.datastrato.graviton.meta.BaseCatalog;-import com.datastrato.graviton.rel.SchemaChange;+import com.datastrato.graviton.catalog.BaseCatalog;+import com.datastrato.graviton.catalog.CatalogOperations;import com.datastrato.graviton.rel.SupportsSchemas;-import com.google.common.annotations.VisibleForTesting;-import com.google.common.base.Preconditions;-import java.io.IOException;-import java.time.Instant;import java.util.Map;-import org.apache.hadoop.conf.Configuration;-import org.apache.hadoop.hive.conf.HiveConf;-import org.apache.hadoop.hive.metastore.api.*;-import org.apache.hadoop.security.UserGroupInformation;-import org.apache.thrift.TException;-import org.slf4j.Logger;-import org.slf4j.LoggerFactory;-public class HiveCatalog extends BaseCatalog implements SupportsSchemas {-- public static final Logger LOG = LoggerFactory.getLogger(HiveCatalog.class);-- @VisibleForTesting HiveClientPool clientPool;-- private HiveConf hiveConf;-- @Override- public void close() {- if (clientPool != null) {- clientPool.close();- clientPool = null;- }- }-- @Override- public void initialize(Config config) throws RuntimeException {- // todo(xun): add hive client pool size in config- this.clientPool = new HiveClientPool(1, hiveConf);- }-- @Override- public NameIdentifier[] listSchemas(Namespace namespace) throws NoSuchNamespaceException {- if (!isValidNamespace(namespace)) {- throw new NoSuchNamespaceException("Namespace is invalid " + namespace);- }-- try {- NameIdentifier[] schemas =- clientPool.run(- c ->- c.getAllDatabases().stream()- .map(db -> NameIdentifier.of(namespace, db))- .toArray(NameIdentifier[]::new));- return schemas;-- } catch (TException e) {- throw new RuntimeException(- "Failed to list all schemas (database) under namespace : "- + namespace- + " in Hive Metastore",- e);-- } catch (InterruptedException e) {- throw new RuntimeException(e);- }- }-- @Override- public HiveSchema createSchema(NameIdentifier ident, String comment, Map<String, String> metadata)- throws SchemaAlreadyExistsException {- Preconditions.checkArgument(- !ident.name().isEmpty(),- String.format("Cannot create schema with invalid name: %s", ident.name()));- Preconditions.checkArgument(- isValidNamespace(ident.namespace()),- String.format("Cannot support invalid namespace in Hive Metastore: %s", ident.namespace()));-- try {- HiveSchema hiveSchema =- new HiveSchema.Builder()- .withId(1L /*TODO. Use ID generator*/)- .withCatalogId(getId())- .withName(ident.name())- .withNamespace(ident.namespace())- .withComment(comment)- .withProperties(metadata)- .withAuditInfo(- new AuditInfo.Builder()- .withCreator(currentUser())- .withCreateTime(Instant.now())- .build())- .withConf(hiveConf)- .build();-- clientPool.run(- client -> {- client.createDatabase(hiveSchema.toInnerDB());- return null;- });-- // TODO. We should also store the customized HiveSchema entity fields into our own- // underlying storage, like id, auditInfo, etc.-- LOG.info("Created Hive schema (database) {} in Hive Metastore", ident.name());-- return hiveSchema;-- } catch (AlreadyExistsException e) {- throw new SchemaAlreadyExistsException(- String.format(- "Hive schema (database) '%s' already exists in Hive Metastore", ident.name()));-- } catch (TException e) {- throw new RuntimeException(- "Failed to create Hive schema (database) " + ident.name() + " in Hive Metastore", e);-- } catch (InterruptedException e) {- throw new RuntimeException(e);- }- }-- @Override- public HiveSchema loadSchema(NameIdentifier ident) throws NoSuchSchemaException {- Preconditions.checkArgument(- !ident.name().isEmpty(),- String.format("Cannot create schema with invalid name: %s", namespace));- Preconditions.checkArgument(- isValidNamespace(ident.namespace()),- String.format("Cannot support invalid namespace in Hive Metastore: %s", namespace));-- try {- Database database = clientPool.run(client -> client.getDatabase(ident.name()));- HiveSchema.Builder builder = new HiveSchema.Builder();-- // TODO. We should also fetch the customized HiveSchema entity fields from our own- // underlying storage, like id, auditInfo, etc.-- builder =- builder- .withId(1L /* TODO. Fetch id from underlying storage */)- .withCatalogId(getId())- .withNamespace(ident.namespace())- .withAuditInfo(- /* TODO. Fetch audit info from underlying storage */- new AuditInfo.Builder()- .withCreator(currentUser())- .withCreateTime(Instant.now())- .build())- .withConf(hiveConf);- HiveSchema hiveSchema = HiveSchema.fromInnerDB(database, builder);-- LOG.info("Loaded Hive schema (database) {} from Hive Metastore ", ident.name());-- return hiveSchema;-- } catch (NoSuchObjectException | UnknownDBException e) {- throw new NoSuchSchemaException(- String.format(- "Hive schema (database) does not exist: %s in Hive Metastore", ident.name()),- e);-- // TODO. We should also delete Hive schema (database) from our own underlying storage-- } catch (TException e) {- throw new RuntimeException(- "Failed to load Hive schema (database) " + ident.name() + " from Hive Metastore", e);-- } catch (InterruptedException e) {- throw new RuntimeException(e);- }- }+public class HiveCatalog extends BaseCatalog<HiveCatalog> {@Override- public HiveSchema alterSchema(NameIdentifier ident, SchemaChange... changes)- throws NoSuchSchemaException {- Preconditions.checkArgument(- !ident.name().isEmpty(),- String.format("Cannot create schema with invalid name: %s", namespace));- Preconditions.checkArgument(- isValidNamespace(ident.namespace()),- String.format("Cannot support invalid namespace in Hive Metastore: %s", namespace));-- try {- // load the database parameters- Database database = clientPool.run(client -> client.getDatabase(ident.name()));- Map<String, String> metadata = HiveSchema.convertToMetadata(database);- LOG.debug(- "Loaded metadata for Hive schema (database) {} found {}",- ident.name(),- metadata.keySet());-- for (SchemaChange change : changes) {- if (change instanceof SchemaChange.SetProperty) {- metadata.put(- ((SchemaChange.SetProperty) change).getProperty(),- ((SchemaChange.SetProperty) change).getValue());- } else if (change instanceof SchemaChange.RemoveProperty) {- metadata.remove(((SchemaChange.RemoveProperty) change).getProperty());- } else {- throw new IllegalArgumentException(- "Unsupported schema change type: " + change.getClass().getSimpleName());- }- }-- // alter the hive database parameters- Database alteredDatabase = database.deepCopy();- alteredDatabase.setParameters(metadata);-- clientPool.run(- client -> {- client.alterDatabase(ident.name(), alteredDatabase);- return null;- });-- // TODO. We should also update the customized HiveSchema entity fields into our own if- // necessary- HiveSchema.Builder builder = new HiveSchema.Builder();- builder =- builder- .withId(1L /* TODO. Fetch id from underlying storage */)- .withCatalogId(getId())- .withNamespace(ident.namespace())- .withAuditInfo(- /* TODO. Fetch audit info from underlying storage */- new AuditInfo.Builder()- .withCreator(currentUser())- .withCreateTime(Instant.now())- .withLastModifier(currentUser())- .withLastModifiedTime(Instant.now())- .build())- .withConf(hiveConf);- HiveSchema hiveSchema = HiveSchema.fromInnerDB(alteredDatabase, builder);-- LOG.info("Altered Hive schema (database) {} in Hive Metastore", ident.name());- // todo(xun): hive dose not support renaming database name directly,- // perhaps we can use namespace to mapping the database names indirectly-- return hiveSchema;-- } catch (TException | InterruptedException e) {- throw new RuntimeException(e);- }+ public String shortName() {+ return "hive";}@Override- public boolean dropSchema(NameIdentifier ident, boolean cascade) throws NonEmptySchemaException {- if (ident.name().isEmpty()) {- LOG.error("Cannot drop schema with invalid name: {}", ident.name());- return false;- }- if (!isValidNamespace(ident.namespace())) {- LOG.error("Cannot support invalid namespace in Hive Metastore: {}", ident.namespace());- return false;- }-- try {- clientPool.run(- client -> {- client.dropDatabase(ident.name(), false, false, cascade);- return null;- });-- // TODO. we should also delete the Hive schema (database) from our own underlying storage-- LOG.info("Dropped Hive schema (database) {}", ident.name());- return true;-- } catch (InvalidOperationException e) {- throw new NonEmptySchemaException(- String.format(- "Hive schema (database) %s is not empty. One or more tables exist.",- ident.name(), e));-- } catch (NoSuchObjectException e) {- LOG.warn("Hive schema (database) {} does not exist in Hive Metastore", ident.name());- return false;-- } catch (TException e) {- throw new RuntimeException(- "Failed to drop Hive schema (database) " + namespace + " in Hive Metastore", e);-- } catch (InterruptedException e) {- throw new RuntimeException(e);- }+ protected CatalogOperations newOps(Map<String, String> config) {+ HiveCatalogOperations ops = new HiveCatalogOperations(entity());+ ops.initialize(config);+ return ops;}@Overridepublic SupportsSchemas asSchemas() {- return this;- }-- public boolean isValidNamespace(Namespace namespace) {- return namespace.levels().length == 2 && namespace.level(1).equals(name());- }-- // TODO. We should figure out a better way to get the current user from servlet container.- private static String currentUser() {- String username = null;- try {- username = UserGroupInformation.getCurrentUser().getShortUserName();- } catch (IOException e) {- LOG.warn("Failed to get Hadoop user", e);- }-- if (username != null) {- return username;- } else {- LOG.warn("Hadoop user is null, defaulting to user.name");- return System.getProperty("user.name");- }- }-- public static class Builder extends BaseCatalog.BaseCatalogBuilder<Builder, HiveCatalog> {- HiveConf hiveConf;-- HiveCatalog hiveCatalog = new HiveCatalog();-- @Override- protected HiveCatalog internalBuild() {- hiveCatalog.id = id;- hiveCatalog.metalakeId = metalakeId;- hiveCatalog.name = name;- hiveCatalog.namespace = namespace;- hiveCatalog.type = type;- hiveCatalog.comment = comment;- hiveCatalog.properties = properties;- hiveCatalog.auditInfo = auditInfo;- hiveCatalog.hiveConf = hiveConf;-- return hiveCatalog;- }-- public Builder withHiveConf(Configuration conf) {- this.hiveConf = new HiveConf(conf, HiveCatalog.class);- this.hiveConf.addResource(conf);- return this;- }+ return (HiveCatalogOperations) ops();}}diff --git a/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalogOperations.java b/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalogOperations.javanew file mode 100644index 0000000000..aa12b3094c--- /dev/null+++ b/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveCatalogOperations.java@@ -0,0 +1,325 @@+package com.datastrato.graviton.catalog.hive;++import com.datastrato.graviton.NameIdentifier;+import com.datastrato.graviton.Namespace;+import com.datastrato.graviton.catalog.CatalogOperations;+import com.datastrato.graviton.exceptions.NoSuchNamespaceException;+import com.datastrato.graviton.exceptions.NoSuchSchemaException;+import com.datastrato.graviton.exceptions.NonEmptySchemaException;+import com.datastrato.graviton.exceptions.SchemaAlreadyExistsException;+import com.datastrato.graviton.meta.AuditInfo;+import com.datastrato.graviton.meta.CatalogEntity;+import com.datastrato.graviton.rel.SchemaChange;+import com.datastrato.graviton.rel.SupportsSchemas;+import com.google.common.annotations.VisibleForTesting;+import com.google.common.base.Preconditions;+import java.io.IOException;+import java.time.Instant;+import java.util.Map;+import org.apache.hadoop.conf.Configuration;+import org.apache.hadoop.hive.conf.HiveConf;+import org.apache.hadoop.hive.metastore.api.*;+import org.apache.hadoop.security.UserGroupInformation;+import org.apache.thrift.TException;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class HiveCatalogOperations implements CatalogOperations, SupportsSchemas {++ public static final Logger LOG = LoggerFactory.getLogger(HiveCatalogOperations.class);++ @VisibleForTesting HiveClientPool clientPool;++ private HiveConf hiveConf;++ private final CatalogEntity entity;++ public HiveCatalogOperations(CatalogEntity entity) {+ this.entity = entity;+ }++ @Override+ public void initialize(Map<String, String> conf) throws RuntimeException {+ Configuration hadoopConf = new Configuration();+ conf.forEach(hadoopConf::set);+ hiveConf = new HiveConf(hadoopConf, HiveCatalogOperations.class);++ // todo(xun): add hive client pool size in config+ this.clientPool = new HiveClientPool(1, hiveConf);+ }++ @Override+ public void close() {+ if (clientPool != null) {+ clientPool.close();+ clientPool = null;+ }+ }++ @Override+ public NameIdentifier[] listSchemas(Namespace namespace) throws NoSuchNamespaceException {+ if (!isValidNamespace(namespace)) {+ throw new NoSuchNamespaceException("Namespace is invalid " + namespace);+ }++ try {+ NameIdentifier[] schemas =+ clientPool.run(+ c ->+ c.getAllDatabases().stream()+ .map(db -> NameIdentifier.of(namespace, db))+ .toArray(NameIdentifier[]::new));+ return schemas;++ } catch (TException e) {+ throw new RuntimeException(+ "Failed to list all schemas (database) under namespace : "+ + namespace+ + " in Hive Metastore",+ e);++ } catch (InterruptedException e) {+ throw new RuntimeException(e);+ }+ }++ @Override+ public HiveSchema createSchema(NameIdentifier ident, String comment, Map<String, String> metadata)+ throws SchemaAlreadyExistsException {+ Preconditions.checkArgument(+ !ident.name().isEmpty(),+ String.format("Cannot create schema with invalid name: %s", ident.name()));+ Preconditions.checkArgument(+ isValidNamespace(ident.namespace()),+ String.format("Cannot support invalid namespace in Hive Metastore: %s", ident.namespace()));++ try {+ HiveSchema hiveSchema =+ new HiveSchema.Builder()+ .withId(1L /*TODO. Use ID generator*/)+ .withCatalogId(entity.getId())+ .withName(ident.name())+ .withNamespace(ident.namespace())+ .withComment(comment)+ .withProperties(metadata)+ .withAuditInfo(+ new AuditInfo.Builder()+ .withCreator(currentUser())+ .withCreateTime(Instant.now())+ .build())+ .withConf(hiveConf)+ .build();++ clientPool.run(+ client -> {+ client.createDatabase(hiveSchema.toInnerDB());+ return null;+ });++ // TODO. We should also store the customized HiveSchema entity fields into our own+ // underlying storage, like id, auditInfo, etc.++ LOG.info("Created Hive schema (database) {} in Hive Metastore", ident.name());++ return hiveSchema;++ } catch (AlreadyExistsException e) {+ throw new SchemaAlreadyExistsException(+ String.format(+ "Hive schema (database) '%s' already exists in Hive Metastore", ident.name()));++ } catch (TException e) {+ throw new RuntimeException(+ "Failed to create Hive schema (database) " + ident.name() + " in Hive Metastore", e);++ } catch (InterruptedException e) {+ throw new RuntimeException(e);+ }+ }++ @Override+ public HiveSchema loadSchema(NameIdentifier ident) throws NoSuchSchemaException {+ Preconditions.checkArgument(+ !ident.name().isEmpty(),+ String.format("Cannot create schema with invalid name: %s", ident.name()));+ Preconditions.checkArgument(+ isValidNamespace(ident.namespace()),+ String.format("Cannot support invalid namespace in Hive Metastore: %s", ident.namespace()));++ try {+ Database database = clientPool.run(client -> client.getDatabase(ident.name()));+ HiveSchema.Builder builder = new HiveSchema.Builder();++ // TODO. We should also fetch the customized HiveSchema entity fields from our own+ // underlying storage, like id, auditInfo, etc.++ builder =+ builder+ .withId(1L /* TODO. Fetch id from underlying storage */)+ .withCatalogId(entity.getId())+ .withNamespace(ident.namespace())+ .withAuditInfo(+ /* TODO. Fetch audit info from underlying storage */+ new AuditInfo.Builder()+ .withCreator(currentUser())+ .withCreateTime(Instant.now())+ .build())+ .withConf(hiveConf);+ HiveSchema hiveSchema = HiveSchema.fromInnerDB(database, builder);++ LOG.info("Loaded Hive schema (database) {} from Hive Metastore ", ident.name());++ return hiveSchema;++ } catch (NoSuchObjectException | UnknownDBException e) {+ throw new NoSuchSchemaException(+ String.format(+ "Hive schema (database) does not exist: %s in Hive Metastore", ident.name()),+ e);++ // TODO. We should also delete Hive schema (database) from our own underlying storage++ } catch (TException e) {+ throw new RuntimeException(+ "Failed to load Hive schema (database) " + ident.name() + " from Hive Metastore", e);++ } catch (InterruptedException e) {+ throw new RuntimeException(e);+ }+ }++ @Override+ public HiveSchema alterSchema(NameIdentifier ident, SchemaChange... changes)+ throws NoSuchSchemaException {+ Preconditions.checkArgument(+ !ident.name().isEmpty(),+ String.format("Cannot create schema with invalid name: %s", ident.name()));+ Preconditions.checkArgument(+ isValidNamespace(ident.namespace()),+ String.format("Cannot support invalid namespace in Hive Metastore: %s", ident.namespace()));++ try {+ // load the database parameters+ Database database = clientPool.run(client -> client.getDatabase(ident.name()));+ Map<String, String> metadata = HiveSchema.convertToMetadata(database);+ LOG.debug(+ "Loaded metadata for Hive schema (database) {} found {}",+ ident.name(),+ metadata.keySet());++ for (SchemaChange change : changes) {+ if (change instanceof SchemaChange.SetProperty) {+ metadata.put(+ ((SchemaChange.SetProperty) change).getProperty(),+ ((SchemaChange.SetProperty) change).getValue());+ } else if (change instanceof SchemaChange.RemoveProperty) {+ metadata.remove(((SchemaChange.RemoveProperty) change).getProperty());+ } else {+ throw new IllegalArgumentException(+ "Unsupported schema change type: " + change.getClass().getSimpleName());+ }+ }++ // alter the hive database parameters+ Database alteredDatabase = database.deepCopy();+ alteredDatabase.setParameters(metadata);++ clientPool.run(+ client -> {+ client.alterDatabase(ident.name(), alteredDatabase);+ return null;+ });++ // TODO. We should also update the customized HiveSchema entity fields into our own if+ // necessary+ HiveSchema.Builder builder = new HiveSchema.Builder();+ builder =+ builder+ .withId(1L /* TODO. Fetch id from underlying storage */)+ .withCatalogId(entity.getId())+ .withNamespace(ident.namespace())+ .withAuditInfo(+ /* TODO. Fetch audit info from underlying storage */+ new AuditInfo.Builder()+ .withCreator(currentUser())+ .withCreateTime(Instant.now())+ .withLastModifier(currentUser())+ .withLastModifiedTime(Instant.now())+ .build())+ .withConf(hiveConf);+ HiveSchema hiveSchema = HiveSchema.fromInnerDB(alteredDatabase, builder);++ LOG.info("Altered Hive schema (database) {} in Hive Metastore", ident.name());+ // todo(xun): hive dose not support renaming database name directly,+ // perhaps we can use namespace to mapping the database names indirectly++ return hiveSchema;++ } catch (TException | InterruptedException e) {+ throw new RuntimeException(e);+ }+ }++ @Override+ public boolean dropSchema(NameIdentifier ident, boolean cascade) throws NonEmptySchemaException {+ if (ident.name().isEmpty()) {+ LOG.error("Cannot drop schema with invalid name: {}", ident.name());+ return false;+ }+ if (!isValidNamespace(ident.namespace())) {+ LOG.error("Cannot support invalid namespace in Hive Metastore: {}", ident.namespace());+ return false;+ }++ try {+ clientPool.run(+ client -> {+ client.dropDatabase(ident.name(), false, false, cascade);+ return null;+ });++ // TODO. we should also delete the Hive schema (database) from our own underlying storage++ LOG.info("Dropped Hive schema (database) {}", ident.name());+ return true;++ } catch (InvalidOperationException e) {+ throw new NonEmptySchemaException(+ String.format(+ "Hive schema (database) %s is not empty. One or more tables exist.", ident.name()),+ e);++ } catch (NoSuchObjectException e) {+ LOG.warn("Hive schema (database) {} does not exist in Hive Metastore", ident.name());+ return false;++ } catch (TException e) {+ throw new RuntimeException(+ "Failed to drop Hive schema (database) " + ident.name() + " in Hive Metastore", e);++ } catch (InterruptedException e) {+ throw new RuntimeException(e);+ }+ }++ public boolean isValidNamespace(Namespace namespace) {+ return namespace.levels().length == 2 && namespace.level(1).equals(entity.name());+ }++ // TODO. We should figure out a better way to get the current user from servlet container.+ private static String currentUser() {+ String username = null;+ try {+ username = UserGroupInformation.getCurrentUser().getShortUserName();+ } catch (IOException e) {+ LOG.warn("Failed to get Hadoop user", e);+ }++ if (username != null) {+ return username;+ } else {+ LOG.warn("Hadoop user is null, defaulting to user.name");+ return System.getProperty("user.name");+ }+ }+}diff --git a/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/rel/HiveSchema.java b/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveSchema.javasimilarity index 98%rename from catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/rel/HiveSchema.javarename to catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveSchema.javaindex 0fad1310da..c3ac8b52df 100644--- a/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/rel/HiveSchema.java+++ b/catalog-hive/src/main/java/com/datastrato/graviton/catalog/hive/HiveSchema.java@@ -1,4 +1,4 @@-package com.datastrato.graviton.catalog.hive.rel;+package com.datastrato.graviton.catalog.hive;import com.datastrato.graviton.meta.rel.BaseSchema;import com.google.common.base.Preconditions;diff --git a/catalog-hive/src/main/resources/com.datastrato.graviton.CatalogProvider b/catalog-hive/src/main/resources/com.datastrato.graviton.CatalogProvidernew file mode 100644index 0000000000..fcd49d42cc--- /dev/null+++ b/catalog-hive/src/main/resources/com.datastrato.graviton.CatalogProvider@@ -0,0 +1 @@+com.datastrato.graviton.catalog.hive.HiveCatalog\ No newline at end of filediff --git a/catalog-hive/src/test/java/com/datastrato/graviton/catalog/hive/HiveCatalogTest.java b/catalog-hive/src/test/java/com/datastrato/graviton/catalog/hive/HiveCatalogTest.javaindex 00fd0088ec..f68ba2ddf2 100644--- a/catalog-hive/src/test/java/com/datastrato/graviton/catalog/hive/HiveCatalogTest.java+++ b/catalog-hive/src/test/java/com/datastrato/graviton/catalog/hive/HiveCatalogTest.java@@ -3,8 +3,11 @@ package com.datastrato.graviton.catalog.hive;import com.datastrato.graviton.Namespace;import com.datastrato.graviton.catalog.hive.miniHMS.MiniHiveMetastoreService;import com.datastrato.graviton.meta.AuditInfo;+import com.datastrato.graviton.meta.CatalogEntity;+import com.google.common.collect.Maps;import java.time.Instant;import java.util.List;+import java.util.Map;import org.apache.hadoop.hive.metastore.IMetaStoreClient;import org.apache.thrift.TException;import org.junit.jupiter.api.Assertions;@@ -12,26 +15,29 @@ import org.junit.jupiter.api.Test;public class HiveCatalogTest extends MiniHiveMetastoreService {@Test- public void listDatabases() throws TException, InterruptedException {+ public void testListDatabases() throws TException, InterruptedException {AuditInfo auditInfo =new AuditInfo.Builder().withCreator("creator").withCreateTime(Instant.now()).build();- HiveCatalog hiveCatalog =- new HiveCatalog.Builder()+ CatalogEntity entity =+ new CatalogEntity.Builder().withId(1L).withName("catalog").withNamespace(Namespace.of("metalake")).withType(HiveCatalog.Type.RELATIONAL).withMetalakeId(1L).withAuditInfo(auditInfo)- .withHiveConf(metastore.hiveConf()).build();- hiveCatalog.initialize(null);+ Map<String, String> conf = Maps.newHashMap();+ metastore.hiveConf().forEach(e -> conf.put(e.getKey(), e.getValue()));- List<String> dbs = hiveCatalog.clientPool.run(IMetaStoreClient::getAllDatabases);- Assertions.assertEquals(2, dbs.size());- Assertions.assertTrue(dbs.contains("default"));- Assertions.assertTrue(dbs.contains(DB_NAME));+ try (HiveCatalogOperations ops = new HiveCatalogOperations(entity)) {+ ops.initialize(conf);+ List<String> dbs = ops.clientPool.run(IMetaStoreClient::getAllDatabases);+ Assertions.assertEquals(2, dbs.size());+ Assertions.assertTrue(dbs.contains("default"));+ Assertions.assertTrue(dbs.contains(DB_NAME));+ }}}diff --git a/catalog-hive/src/test/java/com/datastrato/graviton/catalog/hive/HiveSchemaTest.java b/catalog-hive/src/test/java/com/datastrato/graviton/catalog/hive/HiveSchemaTest.javaindex a437dae40c..fcbe6c6cb2 100644--- a/catalog-hive/src/test/java/com/datastrato/graviton/catalog/hive/HiveSchemaTest.java+++ b/catalog-hive/src/test/java/com/datastrato/graviton/catalog/hive/HiveSchemaTest.java@@ -3,8 +3,9 @@ package com.datastrato.graviton.catalog.hive;import com.datastrato.graviton.NameIdentifier;import com.datastrato.graviton.Namespace;import com.datastrato.graviton.catalog.hive.miniHMS.MiniHiveMetastoreService;-import com.datastrato.graviton.catalog.hive.rel.HiveSchema;import com.datastrato.graviton.meta.AuditInfo;+import com.datastrato.graviton.meta.CatalogEntity;+import com.datastrato.graviton.rel.Schema;import com.datastrato.graviton.rel.SchemaChange;import com.google.common.collect.Maps;import java.time.Instant;@@ -19,18 +20,20 @@ public class HiveSchemaTest extends MiniHiveMetastoreService {AuditInfo auditInfo =new AuditInfo.Builder().withCreator("creator").withCreateTime(Instant.now()).build();- HiveCatalog hiveCatalog =- new HiveCatalog.Builder()+ CatalogEntity entity =+ new CatalogEntity.Builder().withId(1L).withName("catalog").withNamespace(Namespace.of("metalake")).withType(HiveCatalog.Type.RELATIONAL).withMetalakeId(1L).withAuditInfo(auditInfo)- .withHiveConf(metastore.hiveConf()).build();- hiveCatalog.initialize(null);+ Map<String, String> conf = Maps.newHashMap();+ metastore.hiveConf().iterator().forEachRemaining(e -> conf.put(e.getKey(), e.getValue()));+ HiveCatalog hiveCatalog = new HiveCatalog().withCatalogConf(conf).withCatalogEntity(entity);+return hiveCatalog;}@@ -44,15 +47,14 @@ public class HiveSchemaTest extends MiniHiveMetastoreService {properties.put("key2", "val2");String comment = "comment";- HiveSchema schema = hiveCatalog.createSchema(ident, comment, properties);+ Schema schema = hiveCatalog.asSchemas().createSchema(ident, comment, properties);Assertions.assertEquals(ident.name(), schema.name());- Assertions.assertEquals(ident.namespace(), schema.namespace());Assertions.assertEquals(comment, schema.comment());Assertions.assertEquals(properties, schema.properties());- Assertions.assertTrue(hiveCatalog.schemaExists(ident));+ Assertions.assertTrue(hiveCatalog.asSchemas().schemaExists(ident));- NameIdentifier[] idents = hiveCatalog.listSchemas(ident.namespace());+ NameIdentifier[] idents = hiveCatalog.asSchemas().listSchemas(ident.namespace());Assertions.assertTrue(Arrays.asList(idents).contains(ident));// Test illegal identifier@@ -61,7 +63,7 @@ public class HiveSchemaTest extends MiniHiveMetastoreService {Assertions.assertThrows(IllegalArgumentException.class,() -> {- hiveCatalog.createSchema(ident1, comment, properties);+ hiveCatalog.asSchemas().createSchema(ident1, comment, properties);});Assertions.assertTrue(exception.getMessage().contains("Cannot support invalid namespace in Hive Metastore"));@@ -77,22 +79,30 @@ public class HiveSchemaTest extends MiniHiveMetastoreService {properties.put("key2", "val2");String comment = "comment";- hiveCatalog.createSchema(ident, comment, properties);- Assertions.assertTrue(hiveCatalog.schemaExists(ident));+ hiveCatalog.asSchemas().createSchema(ident, comment, properties);+ Assertions.assertTrue(hiveCatalog.asSchemas().schemaExists(ident));- Map<String, String> properties1 = hiveCatalog.loadSchema(ident).properties();+ Map<String, String> properties1 = hiveCatalog.asSchemas().loadSchema(ident).properties();Assertions.assertEquals("val1", properties1.get("key1"));Assertions.assertEquals("val2", properties1.get("key2"));- hiveCatalog.alterSchema(- ident, SchemaChange.removeProperty("key1"), SchemaChange.setProperty("key2", "val2-alter"));- Map<String, String> properties2 = hiveCatalog.loadSchema(ident).properties();+ hiveCatalog+ .asSchemas()+ .alterSchema(+ ident,+ SchemaChange.removeProperty("key1"),+ SchemaChange.setProperty("key2", "val2-alter"));+ Map<String, String> properties2 = hiveCatalog.asSchemas().loadSchema(ident).properties();Assertions.assertFalse(properties2.containsKey("key1"));Assertions.assertEquals("val2-alter", properties2.get("key2"));- hiveCatalog.alterSchema(- ident, SchemaChange.setProperty("key3", "val3"), SchemaChange.setProperty("key4", "val4"));- Map<String, String> properties3 = hiveCatalog.loadSchema(ident).properties();+ hiveCatalog+ .asSchemas()+ .alterSchema(+ ident,+ SchemaChange.setProperty("key3", "val3"),+ SchemaChange.setProperty("key4", "val4"));+ Map<String, String> properties3 = hiveCatalog.asSchemas().loadSchema(ident).properties();Assertions.assertEquals("val3", properties3.get("key3"));Assertions.assertEquals("val4", properties3.get("key4"));}@@ -107,10 +117,10 @@ public class HiveSchemaTest extends MiniHiveMetastoreService {properties.put("key2", "val2");String comment = "comment";- hiveCatalog.createSchema(ident, comment, properties);- Assertions.assertTrue(hiveCatalog.schemaExists(ident));- hiveCatalog.dropSchema(ident, true);- Assertions.assertFalse(hiveCatalog.schemaExists(ident));+ hiveCatalog.asSchemas().createSchema(ident, comment, properties);+ Assertions.assertTrue(hiveCatalog.asSchemas().schemaExists(ident));+ hiveCatalog.asSchemas().dropSchema(ident, true);+ Assertions.assertFalse(hiveCatalog.asSchemas().schemaExists(ident));}private static String genRandomName() {diff --git a/core/src/main/java/com/datastrato/graviton/catalog/BaseCatalog.java b/core/src/main/java/com/datastrato/graviton/catalog/BaseCatalog.javanew file mode 100644index 0000000000..2280dfae97--- /dev/null+++ b/core/src/main/java/com/datastrato/graviton/catalog/BaseCatalog.java@@ -0,0 +1,96 @@+package com.datastrato.graviton.catalog;++import com.datastrato.graviton.Audit;+import com.datastrato.graviton.Catalog;+import com.datastrato.graviton.CatalogProvider;+import com.datastrato.graviton.meta.CatalogEntity;+import com.google.common.base.Preconditions;+import com.google.common.collect.Maps;+import java.util.Collections;+import java.util.Map;+import java.util.Optional;++/**+ * The abstract base class for Catalog implementations.+ *+ * <p>A typical catalog is combined by two objects, one is {@link CatalogEntity} which represents+ * the metadata of the catalog, the other is {@link CatalogOperations} which is used to trigger the+ * specific operations by the catalog.+ *+ * <p>For example, a hive catalog has a {@link CatalogEntity} metadata and a {@link+ * CatalogOperations} which manipulates Hive DBs and tables.+ */+public abstract class BaseCatalog<T extends BaseCatalog> implements Catalog, CatalogProvider {++ private CatalogEntity entity;++ private Map<String, String> conf;++ private volatile CatalogOperations ops;++ protected abstract CatalogOperations newOps(Map<String, String> config);++ public CatalogOperations ops() {+ if (ops == null) {+ synchronized (this) {+ if (ops == null) {+ Preconditions.checkArgument(+ entity != null && conf != null, "entity and conf must be set before calling ops()");+ ops = newOps(mergeConf(properties(), conf));+ }+ }+ }++ return ops;+ }++ public T withCatalogEntity(CatalogEntity entity) {+ this.entity = entity;+ return (T) this;+ }++ public T withCatalogConf(Map<String, String> conf) {+ this.conf = conf;+ return (T) this;+ }++ public CatalogEntity entity() {+ return entity;+ }++ @Override+ public String name() {+ Preconditions.checkArgument(entity != null, "entity is not set");+ return entity.name();+ }++ @Override+ public Type type() {+ Preconditions.checkArgument(entity != null, "entity is not set");+ return entity.getType();+ }++ @Override+ public String comment() {+ Preconditions.checkArgument(entity != null, "entity is not set");+ return entity.getComment();+ }++ @Override+ public Map<String, String> properties() {+ Preconditions.checkArgument(entity != null, "entity is not set");+ return entity.getProperties();+ }++ @Override+ public Audit auditInfo() {+ Preconditions.checkArgument(entity != null, "entity is not set");+ return entity.auditInfo();+ }++ private Map<String, String> mergeConf(Map<String, String> properties, Map<String, String> conf) {+ Map<String, String> mergedConf = conf != null ? Maps.newHashMap(conf) : Maps.newHashMap();+ Optional.ofNullable(properties).ifPresent(mergedConf::putAll);+ return Collections.unmodifiableMap(mergedConf);+ }+}diff --git a/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperations.java b/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperations.javanew file mode 100644index 0000000000..640700c290--- /dev/null+++ b/core/src/main/java/com/datastrato/graviton/catalog/CatalogOperations.java@@ -0,0 +1,25 @@+package com.datastrato.graviton.catalog;++import com.datastrato.graviton.rel.SupportsSchemas;+import com.datastrato.graviton.rel.TableCatalog;+import java.io.Closeable;+import java.util.Map;++/**+ * A catalog operation interface that is used to trigger the operations of a catalog. This interface+ * should be mixed with other Catalog interface like {@link SupportsSchemas} to provide schema+ * operation, {@link TableCatalog} to support table operations, etc.+ */+public interface CatalogOperations extends Closeable {++ /**+ * Initialize the CatalogOperation with specified configuration. This method is called after+ * CatalogOperation object is created, but before any other method is called. The method is used+ * to initialize the connection to the underlying metadata source. RuntimeException will be thrown+ * if the initialization failed.+ *+ * @param config The configuration of this Catalog.+ * @throws RuntimeException if the initialization failed.+ */+ void initialize(Map<String, String> config) throws RuntimeException;+}diff --git a/core/src/main/java/com/datastrato/graviton/meta/BaseCatalog.java b/core/src/main/java/com/datastrato/graviton/meta/BaseCatalog.javadeleted file mode 100644index a94c855558..0000000000--- a/core/src/main/java/com/datastrato/graviton/meta/BaseCatalog.java+++ /dev/null@@ -1,199 +0,0 @@-package com.datastrato.graviton.meta;--import com.datastrato.graviton.*;-import java.io.Closeable;-import java.util.Collections;-import java.util.HashMap;-import java.util.Map;-import javax.annotation.Nullable;-import lombok.EqualsAndHashCode;-import lombok.Getter;-import lombok.ToString;--/**- * This is the base class for a Catalog implementation, it defines the interface to initialize and- * uninitialize the catalog. This class inherits from Closeable, so that a determined close() method- * should be called to release any resources that the catalog holds.- */-@EqualsAndHashCode-@ToString-public abstract class BaseCatalog implements Catalog, Entity, Auditable, HasIdentifier, Closeable {-- public static final Field ID =- Field.required("id", Long.class, "The unique identifier of the catalog");- public static final Field METALAKE_ID =- Field.required("metalake_id", Long.class, "The unique identifier of the metalake");- public static final Field NAME = Field.required("name", String.class, "The name of the catalog");- public static final Field TYPE = Field.required("type", Type.class, "The type of the catalog");- public static final Field COMMENT =- Field.optional("comment", String.class, "The comment of the catalog");- public static final Field PROPERTIES =- Field.optional("properties", Map.class, "The properties of the catalog");- public static final Field AUDIT_INFO =- Field.required("audit_info", AuditInfo.class, "The audit info of the catalog");-- @Getter protected Long id;-- @Getter protected Long metalakeId;-- protected String name;-- protected Type type;-- @Nullable protected String comment;-- @Nullable protected Map<String, String> properties;-- protected AuditInfo auditInfo;-- protected Namespace namespace;-- /**- * Initialize the catalog with specified configuration. This method is called after Catalog object- * is created, but before any other method is called. The method is used to initialize the- * connection to the underlying metadata source. RuntimeException will be thrown if the- * initialization failed.- *- * @param config The configuration of this Catalog.- * @throws RuntimeException if the initialization failed.- */- public abstract void initialize(Config config) throws RuntimeException;-- @Override- public Map<Field, Object> fields() {- Map<Field, Object> fields = new HashMap<>();- fields.put(ID, id);- fields.put(METALAKE_ID, metalakeId);- fields.put(NAME, name);- fields.put(COMMENT, comment);- fields.put(TYPE, type);- fields.put(PROPERTIES, properties);- fields.put(AUDIT_INFO, auditInfo);-- return Collections.unmodifiableMap(fields);- }-- @Override- public Audit auditInfo() {- return auditInfo;- }-- @Override- public String name() {- return name;- }-- @Override- public Namespace namespace() {- return namespace;- }-- @Override- public Type type() {- return type;- }-- @Override- public String comment() {- return comment;- }-- @Override- public Map<String, String> properties() {- return properties;- }-- interface Builder<SELF extends Builder<SELF, T>, T extends BaseCatalog> {- SELF withId(Long id);-- SELF withMetalakeId(Long metalakeId);-- SELF withName(String name);-- SELF withNamespace(Namespace namespace);-- SELF withType(Type type);-- SELF withComment(String comment);-- SELF withProperties(Map<String, String> properties);-- SELF withAuditInfo(AuditInfo auditInfo);-- T build();- }-- public abstract static class BaseCatalogBuilder<- SELF extends Builder<SELF, T>, T extends BaseCatalog>- implements Builder<SELF, T> {- protected Long id;- protected Long metalakeId;- protected String name;- protected Namespace namespace;- protected Type type;- protected String comment;- protected Map<String, String> properties;- protected AuditInfo auditInfo;-- @Override- public SELF withId(Long id) {- this.id = id;- return self();- }-- @Override- public SELF withMetalakeId(Long metalakeId) {- this.metalakeId = metalakeId;- return self();- }-- @Override- public SELF withName(String name) {- this.name = name;- return self();- }-- @Override- public SELF withNamespace(Namespace namespace) {- this.namespace = namespace;- return self();- }-- @Override- public SELF withType(Type type) {- this.type = type;- return self();- }-- @Override- public SELF withComment(String comment) {- this.comment = comment;- return self();- }-- @Override- public SELF withProperties(Map<String, String> properties) {- this.properties = properties;- return self();- }-- @Override- public SELF withAuditInfo(AuditInfo auditInfo) {- this.auditInfo = auditInfo;- return self();- }-- @Override- public T build() {- T t = internalBuild();- t.validate();- return t;- }-- private SELF self() {- return (SELF) this;- }-- protected abstract T internalBuild();- }-}diff --git a/core/src/main/java/com/datastrato/graviton/meta/CatalogEntity.java b/core/src/main/java/com/datastrato/graviton/meta/CatalogEntity.javanew file mode 100644index 0000000000..c1c56585f9--- /dev/null+++ b/core/src/main/java/com/datastrato/graviton/meta/CatalogEntity.java@@ -0,0 +1,128 @@+package com.datastrato.graviton.meta;++import com.datastrato.graviton.*;+import java.util.Collections;+import java.util.HashMap;+import java.util.Map;+import javax.annotation.Nullable;+import lombok.EqualsAndHashCode;+import lombok.Getter;+import lombok.ToString;++@EqualsAndHashCode+@ToString+public class CatalogEntity implements Entity, Auditable, HasIdentifier {++ public static final Field ID =+ Field.required("id", Long.class, "The unique identifier of the catalog");+ public static final Field METALAKE_ID =+ Field.required("metalake_id", Long.class, "The unique identifier of the metalake");+ public static final Field NAME = Field.required("name", String.class, "The name of the catalog");+ public static final Field TYPE =+ Field.required("type", Catalog.Type.class, "The type of the catalog");+ public static final Field COMMENT =+ Field.optional("comment", String.class, "The comment of the catalog");+ public static final Field PROPERTIES =+ Field.optional("properties", Map.class, "The properties of the catalog");+ public static final Field AUDIT_INFO =+ Field.required("audit_info", AuditInfo.class, "The audit info of the catalog");++ @Getter private Long id;++ @Getter private Long metalakeId;++ private String name;++ @Getter private Catalog.Type type;++ @Nullable @Getter private String comment;++ @Nullable @Getter private Map<String, String> properties;++ private AuditInfo auditInfo;++ private Namespace namespace;++ @Override+ public Map<Field, Object> fields() {+ Map<Field, Object> fields = new HashMap<>();+ fields.put(ID, id);+ fields.put(METALAKE_ID, metalakeId);+ fields.put(NAME, name);+ fields.put(COMMENT, comment);+ fields.put(TYPE, type);+ fields.put(PROPERTIES, properties);+ fields.put(AUDIT_INFO, auditInfo);++ return Collections.unmodifiableMap(fields);+ }++ @Override+ public Audit auditInfo() {+ return auditInfo;+ }++ @Override+ public String name() {+ return name;+ }++ @Override+ public Namespace namespace() {+ return namespace;+ }++ public static class Builder {++ private final CatalogEntity catalog;++ public Builder() {+ catalog = new CatalogEntity();+ }++ public Builder withId(Long id) {+ catalog.id = id;+ return this;+ }++ public Builder withMetalakeId(Long metalakeId) {+ catalog.metalakeId = metalakeId;+ return this;+ }++ public Builder withName(String name) {+ catalog.name = name;+ return this;+ }++ public Builder withNamespace(Namespace namespace) {+ catalog.namespace = namespace;+ return this;+ }++ public Builder withType(Catalog.Type type) {+ catalog.type = type;+ return this;+ }++ public Builder withComment(String comment) {+ catalog.comment = comment;+ return this;+ }++ public Builder withProperties(Map<String, String> properties) {+ catalog.properties = properties;+ return this;+ }++ public Builder withAuditInfo(AuditInfo auditInfo) {+ catalog.auditInfo = auditInfo;+ return this;+ }++ public CatalogEntity build() {+ catalog.validate();+ return catalog;+ }+ }+}diff --git a/core/src/test/java/com/datastrato/graviton/TestCatalog.java b/core/src/test/java/com/datastrato/graviton/TestCatalog.javaindex 568ea8fd64..2f3decb29c 100644--- a/core/src/test/java/com/datastrato/graviton/TestCatalog.java+++ b/core/src/test/java/com/datastrato/graviton/TestCatalog.java@@ -1,98 +1,26 @@package com.datastrato.graviton;-import com.datastrato.graviton.exceptions.NoSuchSchemaException;-import com.datastrato.graviton.exceptions.NoSuchTableException;-import com.datastrato.graviton.exceptions.TableAlreadyExistsException;-import com.datastrato.graviton.meta.AuditInfo;-import com.datastrato.graviton.meta.BaseCatalog;-import com.datastrato.graviton.rel.Column;-import com.datastrato.graviton.rel.Table;+import com.datastrato.graviton.catalog.BaseCatalog;+import com.datastrato.graviton.catalog.CatalogOperations;import com.datastrato.graviton.rel.TableCatalog;-import com.datastrato.graviton.rel.TableChange;-import com.google.common.collect.Maps;-import java.io.IOException;-import java.time.Instant;import java.util.Map;-public class TestCatalog extends BaseCatalog implements TableCatalog {+public class TestCatalog extends BaseCatalog<TestCatalog> {- private final Map<NameIdentifier, TestTable> tables;-- private TestCatalog() {- tables = Maps.newHashMap();- }-- @Override- public void initialize(Config config) throws RuntimeException {}-- @Override- public void close() throws IOException {}-- @Override- public NameIdentifier[] listTables(Namespace namespace) throws NoSuchSchemaException {- return tables.keySet().stream()- .filter(testTable -> testTable.namespace().equals(namespace))- .toArray(NameIdentifier[]::new);- }+ public TestCatalog() {}@Override- public Table loadTable(NameIdentifier ident) throws NoSuchTableException {- if (tables.containsKey(ident)) {- return tables.get(ident);- } else {- throw new NoSuchTableException("Table " + ident + " does not exist");- }+ public String shortName() {+ return "test";}@Override- public Table createTable(- NameIdentifier ident, Column[] columns, String comment, Map<String, String> properties)- throws NoSuchSchemaException, TableAlreadyExistsException {- AuditInfo auditInfo =- new AuditInfo.Builder().withCreator("test").withCreateTime(Instant.now()).build();-- TestTable table =- new TestTable(ident.name(), ident.namespace(), comment, properties, auditInfo, columns);- if (tables.containsKey(ident)) {- throw new TableAlreadyExistsException("Table " + ident + " already exists");- } else {- tables.put(ident, table);- }-- return table;- }-- @Override- public Table alterTable(NameIdentifier ident, TableChange... changes)- throws NoSuchTableException, IllegalArgumentException {- throw new UnsupportedOperationException("unsupported");+ protected CatalogOperations newOps(Map config) {+ return new TestCatalogOperations();}@Override- public boolean dropTable(NameIdentifier ident) {- if (tables.containsKey(ident)) {- tables.remove(ident);- return true;- } else {- return false;- }- }-- public static class Builder extends BaseCatalog.BaseCatalogBuilder<Builder, TestCatalog> {- TestCatalog testCatalog = new TestCatalog();-- @Override- protected TestCatalog internalBuild() {- testCatalog.id = id;- testCatalog.metalakeId = metalakeId;- testCatalog.name = name;- testCatalog.namespace = namespace;- testCatalog.type = type;- testCatalog.comment = comment;- testCatalog.properties = properties;- testCatalog.auditInfo = auditInfo;-- return testCatalog;- }+ public TableCatalog asTableCatalog() {+ return (TableCatalog) ops();}}diff --git a/core/src/test/java/com/datastrato/graviton/TestCatalogOperations.java b/core/src/test/java/com/datastrato/graviton/TestCatalogOperations.javanew file mode 100644index 0000000000..8de11c8cb8--- /dev/null+++ b/core/src/test/java/com/datastrato/graviton/TestCatalogOperations.java@@ -0,0 +1,80 @@+package com.datastrato.graviton;++import com.datastrato.graviton.catalog.CatalogOperations;+import com.datastrato.graviton.exceptions.NoSuchSchemaException;+import com.datastrato.graviton.exceptions.NoSuchTableException;+import com.datastrato.graviton.exceptions.TableAlreadyExistsException;+import com.datastrato.graviton.meta.AuditInfo;+import com.datastrato.graviton.rel.Column;+import com.datastrato.graviton.rel.Table;+import com.datastrato.graviton.rel.TableCatalog;+import com.datastrato.graviton.rel.TableChange;+import com.google.common.collect.Maps;+import java.io.IOException;+import java.time.Instant;+import java.util.Map;++public class TestCatalogOperations implements CatalogOperations, TableCatalog {++ private final Map<NameIdentifier, TestTable> tables;++ public TestCatalogOperations() {+ tables = Maps.newHashMap();+ }++ @Override+ public void initialize(Map<String, String> config) throws RuntimeException {}++ @Override+ public void close() throws IOException {}++ @Override+ public NameIdentifier[] listTables(Namespace namespace) throws NoSuchSchemaException {+ return tables.keySet().stream()+ .filter(testTable -> testTable.namespace().equals(namespace))+ .toArray(NameIdentifier[]::new);+ }++ @Override+ public Table loadTable(NameIdentifier ident) throws NoSuchTableException {+ if (tables.containsKey(ident)) {+ return tables.get(ident);+ } else {+ throw new NoSuchTableException("Table " + ident + " does not exist");+ }+ }++ @Override+ public Table createTable(+ NameIdentifier ident, Column[] columns, String comment, Map<String, String> properties)+ throws NoSuchSchemaException, TableAlreadyExistsException {+ AuditInfo auditInfo =+ new AuditInfo.Builder().withCreator("test").withCreateTime(Instant.now()).build();++ TestTable table =+ new TestTable(ident.name(), ident.namespace(), comment, properties, auditInfo, columns);+ if (tables.containsKey(ident)) {+ throw new TableAlreadyExistsException("Table " + ident + " already exists");+ } else {+ tables.put(ident, table);+ }++ return table;+ }++ @Override+ public Table alterTable(NameIdentifier ident, TableChange... changes)+ throws NoSuchTableException, IllegalArgumentException {+ throw new UnsupportedOperationException("unsupported");+ }++ @Override+ public boolean dropTable(NameIdentifier ident) {+ if (tables.containsKey(ident)) {+ tables.remove(ident);+ return true;+ } else {+ return false;+ }+ }+}diff --git a/core/src/test/java/com/datastrato/graviton/TestEntityStore.java b/core/src/test/java/com/datastrato/graviton/TestEntityStore.javaindex 0105a0cd7f..fc811b5b99 100644--- a/core/src/test/java/com/datastrato/graviton/TestEntityStore.java+++ b/core/src/test/java/com/datastrato/graviton/TestEntityStore.java@@ -56,8 +56,8 @@ public class TestEntityStore {.withVersion(SchemaVersion.V_0_1).build();- TestCatalog catalog =- new TestCatalog.Builder()+ CatalogEntity catalog =+ new CatalogEntity.Builder().withId(1L).withName("catalog").withNamespace(Namespace.of("metalake"))@@ -86,7 +86,7 @@ public class TestEntityStore {Metalake retrievedMetalake = store.retrieveEntity(metalake);Assertions.assertEquals(metalake, retrievedMetalake);- BaseCatalog retrievedCatalog = store.retrieveEntity(catalog);+ CatalogEntity retrievedCatalog = store.retrieveEntity(catalog);Assertions.assertEquals(catalog, retrievedCatalog);Table retrievedTable = store.retrieveEntity(table);diff --git a/core/src/test/java/com/datastrato/graviton/meta/TestEntity.java b/core/src/test/java/com/datastrato/graviton/meta/TestEntity.javaindex f833dcae47..93d3f908ba 100644--- a/core/src/test/java/com/datastrato/graviton/meta/TestEntity.java+++ b/core/src/test/java/com/datastrato/graviton/meta/TestEntity.java@@ -1,7 +1,7 @@package com.datastrato.graviton.meta;+import com.datastrato.graviton.Catalog;import com.datastrato.graviton.Field;-import com.datastrato.graviton.TestCatalog;import com.google.common.collect.ImmutableMap;import java.time.Instant;import java.util.Map;@@ -23,8 +23,7 @@ public class TestEntity {// Catalog test dataprivate final Long catalogId = 1L;private final String catalogName = "testCatalog";- private final BaseCatalog.Type type = BaseCatalog.Type.RELATIONAL;- private final String catalogComment = "testComment";+ private final Catalog.Type type = Catalog.Type.RELATIONAL;@Testpublic void testMetalake() {@@ -48,8 +47,9 @@ public class TestEntity {@Testpublic void testCatalog() {- TestCatalog testCatalog =- new TestCatalog.Builder()+ String catalogComment = "testComment";+ CatalogEntity testCatalog =+ new CatalogEntity.Builder().withId(catalogId).withMetalakeId(metalakeId).withName(catalogName)@@ -60,12 +60,12 @@ public class TestEntity {.build();Map<Field, Object> fields = testCatalog.fields();- Assertions.assertEquals(catalogId, fields.get(TestCatalog.ID));- Assertions.assertEquals(metalakeId, fields.get(TestCatalog.METALAKE_ID));- Assertions.assertEquals(catalogName, fields.get(TestCatalog.NAME));- Assertions.assertEquals(catalogComment, fields.get(TestCatalog.COMMENT));- Assertions.assertEquals(type, fields.get(TestCatalog.TYPE));- Assertions.assertEquals(map, fields.get(TestCatalog.PROPERTIES));- Assertions.assertEquals(auditInfo, fields.get(TestCatalog.AUDIT_INFO));+ Assertions.assertEquals(catalogId, fields.get(CatalogEntity.ID));+ Assertions.assertEquals(metalakeId, fields.get(CatalogEntity.METALAKE_ID));+ Assertions.assertEquals(catalogName, fields.get(CatalogEntity.NAME));+ Assertions.assertEquals(catalogComment, fields.get(CatalogEntity.COMMENT));+ Assertions.assertEquals(type, fields.get(CatalogEntity.TYPE));+ Assertions.assertEquals(map, fields.get(CatalogEntity.PROPERTIES));+ Assertions.assertEquals(auditInfo, fields.get(CatalogEntity.AUDIT_INFO));}}diff --git a/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalog.java b/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalog.javaindex 76ee7f6c44..233e77809f 100644--- a/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalog.java+++ b/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalog.java@@ -1,31 +1,24 @@package com.datastrato.graviton.server.web.rest;-import com.datastrato.graviton.Config;-import com.datastrato.graviton.meta.BaseCatalog;+import com.datastrato.graviton.catalog.BaseCatalog;+import com.datastrato.graviton.catalog.CatalogOperations;import java.io.IOException;+import java.util.Map;-public class TestCatalog extends BaseCatalog {+public class TestCatalog extends BaseCatalog<TestCatalog> {@Override- public void initialize(Config config) throws RuntimeException {}+ public String shortName() {+ return "test";+ }@Override- public void close() throws IOException {}-- public static class Builder extends BaseCatalogBuilder<Builder, TestCatalog> {-- @Override- protected TestCatalog internalBuild() {- TestCatalog catalog = new TestCatalog();- catalog.id = id;- catalog.metalakeId = metalakeId;- catalog.name = name;- catalog.namespace = namespace;- catalog.type = type;- catalog.comment = comment;- catalog.properties = properties;- catalog.auditInfo = auditInfo;+ protected CatalogOperations newOps(Map config) {+ return new CatalogOperations() {+ @Override+ public void initialize(Map<String, String> config) throws RuntimeException {}- return catalog;- }+ @Override+ public void close() throws IOException {}+ };}}diff --git a/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalogOperations.java b/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalogOperations.javaindex 3ac4ea37c4..c6830f60a7 100644--- a/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalogOperations.java+++ b/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalogOperations.java@@ -15,9 +15,11 @@ import com.datastrato.graviton.exceptions.NoSuchCatalogException;import com.datastrato.graviton.exceptions.NoSuchMetalakeException;import com.datastrato.graviton.meta.AuditInfo;import com.datastrato.graviton.meta.BaseCatalogsOperations;+import com.datastrato.graviton.meta.CatalogEntity;import com.google.common.collect.ImmutableList;import com.google.common.collect.ImmutableMap;import java.time.Instant;+import java.util.Collections;import javax.servlet.http.HttpServletRequest;import javax.ws.rs.client.Entity;import javax.ws.rs.core.Application;@@ -358,16 +360,22 @@ public class TestCatalogOperations extends JerseyTest {}private static TestCatalog buildCatalog(String metalake, String catalogName) {- return new TestCatalog.Builder()- .withId(1L)- .withMetalakeId(1L)- .withName(catalogName)- .withComment("comment")- .withNamespace(Namespace.of(metalake))- .withProperties(ImmutableMap.of("key", "value"))- .withType(Catalog.Type.RELATIONAL)- .withAuditInfo(- new AuditInfo.Builder().withCreator("creator").withCreateTime(Instant.now()).build())- .build();+ CatalogEntity entity =+ new CatalogEntity.Builder()+ .withId(1L)+ .withMetalakeId(1L)+ .withName(catalogName)+ .withComment("comment")+ .withNamespace(Namespace.of(metalake))+ .withProperties(ImmutableMap.of("key", "value"))+ .withType(Catalog.Type.RELATIONAL)+ .withAuditInfo(+ new AuditInfo.Builder()+ .withCreator("creator")+ .withCreateTime(Instant.now())+ .build())+ .build();++ return new TestCatalog().withCatalogConf(Collections.emptyMap()).withCatalogEntity(entity);}}