World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#48] feat(core): add entity store interface for Graviton (#49)
Jerry Shao496535576c87
diff --git a/core/src/main/java/com/datastrato/graviton/EntityStore.java b/core/src/main/java/com/datastrato/graviton/EntityStore.javanew file mode 100644index 0000000000..c729bb6289--- /dev/null+++ b/core/src/main/java/com/datastrato/graviton/EntityStore.java@@ -0,0 +1,45 @@+package com.datastrato.graviton;++import java.io.Closeable;+import java.io.IOException;++public interface EntityStore extends Closeable {++ /**+ * Initialize the entity store.+ *+ * <p>Note. This method will be called after the EntityStore object is created, and before any+ * other methods are called.+ *+ * @param config the configuration for the entity store+ * @throws RuntimeException if the initialization fails+ */+ void initialize(Config config) throws RuntimeException;++ /**+ * Store the entity into the underlying storage.+ *+ * <p>Note. The implementation should be thread-safe, and should be able to handle concurrent+ * store of entities.+ *+ * @param entity the entity to store+ * @param <E> the type of the entity+ * @throws IOException if the store operation fails+ */+ <E extends Entity & HasIdentifier> void storeEntity(E entity) throws IOException;++ /**+ * Retrieve the entity from the underlying storage.+ *+ * <p>Note. The implementation should be thread-safe, and should be able to handle concurrent+ * retrieve of entities.+ *+ * @param ident the unique identifier of the entity+ * @return the entity retrieved from the underlying storage+ * @param <E> the type of the entity+ * @throws NoSuchEntityException if the entity does not exist+ * @throws IOException if the retrieve operation fails+ */+ <E extends Entity & HasIdentifier> E retrieveEntity(HasIdentifier ident)+ throws NoSuchEntityException, IOException;+}diff --git a/core/src/main/java/com/datastrato/graviton/EntityStoreFactory.java b/core/src/main/java/com/datastrato/graviton/EntityStoreFactory.javanew file mode 100644index 0000000000..6d544ae384--- /dev/null+++ b/core/src/main/java/com/datastrato/graviton/EntityStoreFactory.java@@ -0,0 +1,33 @@+package com.datastrato.graviton;++import com.google.common.collect.ImmutableMap;+import java.util.Map;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class EntityStoreFactory {++ private static final Logger LOG = LoggerFactory.getLogger(EntityStoreFactory.class);++ // Register EntityStore's short name to its full qualified class name in the map. So that user+ // don't need to specify the full qualified class name when creating an EntityStore.+ private static final Map<String, String> ENTITY_STORES = ImmutableMap.of();++ private EntityStoreFactory() {}++ public static EntityStore createEntityStore(Config config) {+ String name = config.get(configs.ENTITY_STORE);+ return createEntityStore(name);+ }++ public static EntityStore createEntityStore(String name) {+ String className = ENTITY_STORES.getOrDefault(name, name);++ try {+ return (EntityStore) Class.forName(className).newInstance();+ } catch (Exception e) {+ LOG.error("Failed to create EntityStore by name {}.", name, e);+ throw new RuntimeException("Failed to create EntityStore: " + name, e);+ }+ }+}diff --git a/core/src/main/java/com/datastrato/graviton/HasIdentifier.java b/core/src/main/java/com/datastrato/graviton/HasIdentifier.javaindex d804c1d1f4..c4fab16663 100644--- a/core/src/main/java/com/datastrato/graviton/HasIdentifier.java+++ b/core/src/main/java/com/datastrato/graviton/HasIdentifier.java@@ -2,20 +2,17 @@ package com.datastrato.graviton;public interface HasIdentifier {- /**- * Return the name of the entity.- *- * @return A String with the name of the entity.- */+ /** Return the name of the entity. */String name();- /**- * Returns the name identifier of the entity.- *- * @return NameIdentifier of the entity.- */- default NameIdentifier nameIdentifier(Namespace namespace) {- return NameIdentifier.of(namespace, name());+ /** Returns the namespace of the entity. */+ default Namespace namespace() {+ return Namespace.empty();+ }++ /** Returns the name identifier of the entity. */+ default NameIdentifier nameIdentifier() {+ return NameIdentifier.of(namespace(), name());}// TODO. Returns a binary compact unique identifier of the entity. @Jerrydiff --git a/core/src/main/java/com/datastrato/graviton/configs.java b/core/src/main/java/com/datastrato/graviton/configs.javanew file mode 100644index 0000000000..aeeb9c11d6--- /dev/null+++ b/core/src/main/java/com/datastrato/graviton/configs.java@@ -0,0 +1,15 @@+package com.datastrato.graviton;++import com.datastrato.graviton.config.ConfigBuilder;+import com.datastrato.graviton.config.ConfigEntry;++public interface configs {++ ConfigEntry<String> ENTITY_STORE =+ new ConfigBuilder("graviton.entity.store")+ .doc("The entity store to use")+ .version("0.1.0")+ .stringConf()+ // TODO. Change this when we have a EntityStore implementation. @Jerry+ .createWithDefault("in-memory");+}diff --git a/core/src/main/java/com/datastrato/graviton/meta/BaseCatalog.java b/core/src/main/java/com/datastrato/graviton/meta/BaseCatalog.javaindex 53800d63c2..7ebf80b030 100644--- a/core/src/main/java/com/datastrato/graviton/meta/BaseCatalog.java+++ b/core/src/main/java/com/datastrato/graviton/meta/BaseCatalog.java@@ -21,7 +21,7 @@ import lombok.ToString;@ToStringpublic abstract class BaseCatalog implements Entity, Auditable, HasIdentifier, Closeable {- enum Type {+ public enum Type {RELATIONAL, // Catalog Type for Relational Data Structure, like db.table, catalog.db.table.FILE, // Catalog Type for File System (including HDFS, S3, etc.), like path/to/fileSTREAM, // Catalog Type for Streaming Data, like kafka://topic@@ -63,6 +63,8 @@ public abstract class BaseCatalog implements Entity, Auditable, HasIdentifier, C@JsonProperty("audit_info")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@@ -98,6 +100,11 @@ public abstract class BaseCatalog implements Entity, Auditable, HasIdentifier, Creturn name;}+ @Override+ public Namespace namespace() {+ return namespace;+ }+interface Builder<SELF extends Builder<SELF, T>, T extends BaseCatalog> {SELF withId(Long id);@@ -105,6 +112,8 @@ public abstract class BaseCatalog implements Entity, Auditable, HasIdentifier, CSELF withName(String name);+ SELF withNamespace(Namespace namespace);+SELF withType(Type type);SELF withComment(String comment);@@ -122,6 +131,7 @@ public abstract class BaseCatalog implements Entity, Auditable, HasIdentifier, Cprotected Long id;protected Long lakehouseId;protected String name;+ protected Namespace namespace;protected Type type;protected String comment;protected Map<String, String> properties;@@ -145,6 +155,12 @@ public abstract class BaseCatalog implements Entity, Auditable, HasIdentifier, Creturn self();}+ @Override+ public SELF withNamespace(Namespace namespace) {+ this.namespace = namespace;+ return self();+ }+@Overridepublic SELF withType(Type type) {this.type = type;diff --git a/core/src/main/java/com/datastrato/graviton/meta/catalog/rel/Column.java b/core/src/main/java/com/datastrato/graviton/meta/catalog/rel/Column.javaindex 2d570c2f5c..bbd1f0661d 100644--- a/core/src/main/java/com/datastrato/graviton/meta/catalog/rel/Column.java+++ b/core/src/main/java/com/datastrato/graviton/meta/catalog/rel/Column.java@@ -26,6 +26,7 @@ public interface Column extends Entity, Auditable, HasIdentifier {/** Returns the name of this column. */@JsonProperty("name")String name();+/** Returns the data type of this column. */@JsonProperty("type")@JsonSerialize(using = JsonUtils.TypeSerializer.class)diff --git a/core/src/main/java/com/datastrato/graviton/meta/catalog/rel/Table.java b/core/src/main/java/com/datastrato/graviton/meta/catalog/rel/Table.javaindex 5d8b8be9b8..b5af9497a3 100644--- a/core/src/main/java/com/datastrato/graviton/meta/catalog/rel/Table.java+++ b/core/src/main/java/com/datastrato/graviton/meta/catalog/rel/Table.java@@ -20,6 +20,9 @@ public interface Table extends Entity, Auditable, HasIdentifier {@JsonProperty("name")String name();+ /** return the namespace of the table. */+ Namespace namespace();+/** return the columns of the table. */@JsonProperty("columns")Column[] columns();diff --git a/core/src/test/java/com/datastrato/graviton/TestEntityStore.java b/core/src/test/java/com/datastrato/graviton/TestEntityStore.javanew file mode 100644index 0000000000..5de828b6c6--- /dev/null+++ b/core/src/test/java/com/datastrato/graviton/TestEntityStore.java@@ -0,0 +1,100 @@+package com.datastrato.graviton;++import com.datastrato.graviton.json.TestColumn;+import com.datastrato.graviton.json.TestTable;+import com.datastrato.graviton.meta.*;+import com.datastrato.graviton.meta.catalog.rel.Column;+import com.datastrato.graviton.meta.catalog.rel.Table;+import com.google.common.collect.Maps;+import io.substrait.type.TypeCreator;+import java.io.IOException;+import java.time.Instant;+import java.util.Map;+import org.junit.jupiter.api.Assertions;+import org.junit.jupiter.api.Test;++public class TestEntityStore {++ public static class InMemoryEntityStore implements EntityStore {+ private final Map<NameIdentifier, Entity> entityMap;++ public InMemoryEntityStore() {+ this.entityMap = Maps.newHashMap();+ }++ @Override+ public void initialize(Config config) throws RuntimeException {}++ @Override+ public synchronized <E extends Entity & HasIdentifier> void storeEntity(E entity)+ throws IOException {+ entityMap.put(entity.nameIdentifier(), entity);+ }++ @Override+ public synchronized <E extends Entity & HasIdentifier> E retrieveEntity(HasIdentifier ident)+ throws NoSuchEntityException, IOException {+ if (entityMap.containsKey(ident.nameIdentifier())) {+ return (E) entityMap.get(ident.nameIdentifier());+ } else {+ throw new NoSuchEntityException("Entity " + ident.nameIdentifier() + " does not exist");+ }+ }++ @Override+ public void close() throws IOException {}+ }++ @Test+ public void testEntityStoreAndRetrieve() throws Exception {+ AuditInfo auditInfo =+ new AuditInfo.Builder().withCreator("creator").withCreateTime(Instant.now()).build();++ Lakehouse lakehouse =+ new Lakehouse.Builder()+ .withId(1L)+ .withName("lakehouse")+ .withAuditInfo(auditInfo)+ .withVersion(SchemaVersion.V_0_1)+ .build();++ BaseCatalog catalog =+ new TestCatalog.Builder()+ .withId(1L)+ .withName("catalog")+ .withNamespace(Namespace.of("lakehouse"))+ .withType(TestCatalog.Type.RELATIONAL)+ .withLakehouseId(1L)+ .withAuditInfo(auditInfo)+ .build();++ Column column = new TestColumn("column", "comment", TypeCreator.NULLABLE.I8, auditInfo);++ Table table =+ new TestTable(+ "table",+ Namespace.of("lakehouse", "catalog", "db"),+ "comment",+ Maps.newHashMap(),+ auditInfo,+ new Column[] {column});++ InMemoryEntityStore store = new InMemoryEntityStore();+ store.storeEntity(lakehouse);+ store.storeEntity(catalog);+ store.storeEntity(table);+ store.storeEntity(column);++ Lakehouse retrievedLakehouse = store.retrieveEntity(lakehouse);+ Assertions.assertEquals(lakehouse, retrievedLakehouse);++ BaseCatalog retrievedCatalog = store.retrieveEntity(catalog);+ Assertions.assertEquals(catalog, retrievedCatalog);++ Table retrievedTable = store.retrieveEntity(table);+ Assertions.assertEquals(table, retrievedTable);++ Column retrievedColumn = store.retrieveEntity(column);+ Assertions.assertEquals(column, retrievedColumn);+ }+}diff --git a/core/src/test/java/com/datastrato/graviton/json/TestEntityJsonSerDe.java b/core/src/test/java/com/datastrato/graviton/json/TestEntityJsonSerDe.javaindex dd4f2f1e84..0deb9a7707 100644--- a/core/src/test/java/com/datastrato/graviton/json/TestEntityJsonSerDe.java+++ b/core/src/test/java/com/datastrato/graviton/json/TestEntityJsonSerDe.java@@ -1,5 +1,6 @@package com.datastrato.graviton.json;+import com.datastrato.graviton.Namespace;import com.datastrato.graviton.meta.*;import com.datastrato.graviton.meta.AuditInfo;import com.datastrato.graviton.meta.catalog.rel.Column;@@ -173,7 +174,9 @@ public class TestEntityJsonSerDe {Map<String, String> properties = ImmutableMap.of("k1", "v1", "k2", "v2");Column column = new TestColumn(name, comment, type, info);- Table table = new TestTable(tableName, tableComment, properties, info, new Column[] {column});+ Namespace namespace = Namespace.of("catalog", "db");+ Table table =+ new TestTable(tableName, namespace, tableComment, properties, info, new Column[] {column});String serJson = JsonUtils.objectMapper().writeValueAsString(table);String expectedJson =diff --git a/core/src/test/java/com/datastrato/graviton/json/TestTable.java b/core/src/test/java/com/datastrato/graviton/json/TestTable.javaindex 95ba9490ba..3b863ddd05 100644--- a/core/src/test/java/com/datastrato/graviton/json/TestTable.java+++ b/core/src/test/java/com/datastrato/graviton/json/TestTable.java@@ -1,6 +1,7 @@package com.datastrato.graviton.json;import com.datastrato.graviton.Field;+import com.datastrato.graviton.Namespace;import com.datastrato.graviton.meta.AuditInfo;import com.datastrato.graviton.meta.catalog.rel.Column;import com.datastrato.graviton.meta.catalog.rel.Table;@@ -8,7 +9,9 @@ import com.fasterxml.jackson.annotation.JsonProperty;import java.util.Collections;import java.util.HashMap;import java.util.Map;+import lombok.EqualsAndHashCode;+@EqualsAndHashCodepublic class TestTable implements Table {public static final Field NAME = Field.required("name", String.class, "The name of the table");@@ -21,6 +24,8 @@ public class TestTable implements Table {private String name;+ private Namespace namespace;+private String comment;private Map<String, String> properties;@@ -31,11 +36,13 @@ public class TestTable implements Table {public TestTable(String name,+ Namespace namespace,String comment,Map<String, String> properties,AuditInfo auditInfo,Column[] columns) {this.name = name;+ this.namespace = namespace;this.comment = comment;this.properties = properties;this.auditInfo = auditInfo;@@ -63,6 +70,11 @@ public class TestTable implements Table {return name;}+ @Override+ public Namespace namespace() {+ return namespace;+ }+@JsonProperty("audit_info")@Overridepublic AuditInfo auditInfo() {diff --git a/core/src/test/java/com/datastrato/graviton/meta/TestCatalog.java b/core/src/test/java/com/datastrato/graviton/meta/TestCatalog.javaindex dad2d2f4f6..620fe4b943 100644--- a/core/src/test/java/com/datastrato/graviton/meta/TestCatalog.java+++ b/core/src/test/java/com/datastrato/graviton/meta/TestCatalog.java@@ -2,7 +2,9 @@ package com.datastrato.graviton.meta;import com.datastrato.graviton.Config;import java.io.IOException;+import lombok.EqualsAndHashCode;+@EqualsAndHashCode(callSuper = true)public class TestCatalog extends BaseCatalog {@Override@@ -20,6 +22,7 @@ public class TestCatalog extends BaseCatalog {testCatalog.id = id;testCatalog.lakehouseId = lakehouseId;testCatalog.name = name;+ testCatalog.namespace = namespace;testCatalog.type = type;testCatalog.comment = comment;testCatalog.properties = properties;