Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#228] refactor:(core) Add id generator and key encoding interface (#229)

Qi Yu3ca59beb2ce9
diff --git a/core/src/main/java/com/datastrato/graviton/storage/EntityKeyEncoder.java b/core/src/main/java/com/datastrato/graviton/storage/EntityKeyEncoder.java
new file mode 100644
index 0000000000..dd63da1dd4
--- /dev/null
+++ b/core/src/main/java/com/datastrato/graviton/storage/EntityKeyEncoder.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.graviton.storage;
+
+import com.datastrato.graviton.Entity.EntityIdentifier;
+import com.datastrato.graviton.Entity.EntityType;
+import com.datastrato.graviton.NameIdentifier;
+import java.io.IOException;
+
+/** Interface for encoding entity to storage it in underlying storage. E.g. RocksDB. */
+public interface EntityKeyEncoder<T> {
+ /**
+ * Construct the key for key-value store from the entity NameIdentifier and EntityType.
+ *
+ * @param entityIdentifier entity identifier to encode
+ * @param createIdIfNotExists create the id mapping for name if not exists
+ * @return encoded key for key-value store
+ * @throws IOException Exception if error occurs
+ */
+ byte[] encode(EntityIdentifier entityIdentifier, boolean createIdIfNotExists) throws IOException;
+
+ /**
+ * Construct the key for key-value store from the entity NameIdentifier and EntityType.
+ *
+ * <p>Note, This method is the replacement of {@link #encode(EntityIdentifier, boolean)}. When we
+ * implement this method, we should mark {@link #encode(EntityIdentifier, boolean)} as deprecated
+ * and remove it later.
+ *
+ * @param ident entity identifier to encode
+ * @param type entity type to encode
+ * @return encoded key for key-value stored
+ * @throws IOException, Exception if error occurs
+ */
+ default T encode(NameIdentifier ident, EntityType type) throws IOException {
+ throw new UnsupportedOperationException("Not implemented yet");
+ }
+}
diff --git a/core/src/main/java/com/datastrato/graviton/storage/IdGenerator.java b/core/src/main/java/com/datastrato/graviton/storage/IdGenerator.java
new file mode 100644
index 0000000000..d6e9590c49
--- /dev/null
+++ b/core/src/main/java/com/datastrato/graviton/storage/IdGenerator.java
@@ -0,0 +1,13 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.graviton.storage;
+
+/** Generate a unique id that maps to a name. */
+public interface IdGenerator {
+
+ /** Returns a unique identifier. */
+ long nextId();
+}
diff --git a/core/src/main/java/com/datastrato/graviton/storage/InMemoryNameMappingService.java b/core/src/main/java/com/datastrato/graviton/storage/InMemoryNameMappingService.java
new file mode 100644
index 0000000000..fab2a90243
--- /dev/null
+++ b/core/src/main/java/com/datastrato/graviton/storage/InMemoryNameMappingService.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.graviton.storage;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps;
+import java.util.Map;
+
+/**
+ * In memory implemention for {@link NameMappingService}
+ *
+ * <p>Note, This class is only for test usage, please do not use it in production.
+ */
+public class InMemoryNameMappingService implements NameMappingService {
+ private final Map<String, Long> nameToId = Maps.newHashMap();
+
+ private final IdGenerator idGenerator;
+
+ public static final InMemoryNameMappingService INSTANCE = new InMemoryNameMappingService();
+
+ public InMemoryNameMappingService() {
+ // Make configurable.
+ this.idGenerator = new RandomIdGenerator();
+ }
+
+ @Override
+ public synchronized Long get(String name) {
+ return nameToId.get(name);
+ }
+
+ @Override
+ public synchronized Long create(String name) {
+ // Should handle race condition.
+ long nextId = idGenerator.nextId();
+ nameToId.put(name, nextId);
+ return nextId;
+ }
+
+ @Override
+ public synchronized boolean update(String oldName, String newName) {
+ Preconditions.checkState(nameToId.containsKey(oldName), "Name %s does not exist", oldName);
+ Long id = nameToId.remove(oldName);
+ nameToId.put(newName, id);
+ return true;
+ }
+
+ @Override
+ public boolean delete(String name) {
+ return nameToId.remove(name) != null;
+ }
+}
diff --git a/core/src/main/java/com/datastrato/graviton/storage/NameMappingService.java b/core/src/main/java/com/datastrato/graviton/storage/NameMappingService.java
new file mode 100644
index 0000000000..eda966e8fb
--- /dev/null
+++ b/core/src/main/java/com/datastrato/graviton/storage/NameMappingService.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.graviton.storage;
+
+import com.datastrato.graviton.storage.kv.KvEntityStore;
+import java.io.IOException;
+
+/**
+ * {@link NameMappingService} manages name to id mappings when using {@link KvEntityStore} to store
+ * entity.
+ *
+ * <p>Note. Implementations of this interface should be thread-safe.
+ */
+public interface NameMappingService {
+
+ /**
+ * Get id from name.
+ *
+ * @param name the name of the entity
+ * @return the id of the name, or null if the name does not exist
+ * @throws IOException if the underlying storage failed
+ */
+ Long get(String name) throws IOException;
+
+ /**
+ * If we do not find the id of the name, we create a new id for the name.
+ *
+ * @param name the name of the entity
+ * @return the id of the name, or null if the name does not exist
+ * @throws IOException if the underlying storage failed
+ */
+ Long create(String name) throws IOException;
+
+ /**
+ * Update the mapping of the name to id. This method is used to update the mapping when we rename
+ * an entity. Please see the example
+ *
+ * <pre>
+ * Before:
+ * oldname -> 1
+ * 1 -> oldname
+ *
+ * After:
+ * newname -> 1
+ * 1 -> newname
+ * </pre>
+ *
+ * @param oldName name to be updated
+ * @param newName new name
+ * @throws IOException if the underlying storage failed
+ */
+ boolean update(String oldName, String newName) throws IOException;
+
+ /**
+ * Delete id mapping for name. Ignore if the name does not exist.
+ *
+ * @param name name to be deleted
+ * @return true if the name exists and is deleted successfully, false if the name does not exist
+ * @throws IOException if the underlying storage failed
+ */
+ boolean delete(String name) throws IOException;
+}
diff --git a/core/src/main/java/com/datastrato/graviton/storage/RandomIdGenerator.java b/core/src/main/java/com/datastrato/graviton/storage/RandomIdGenerator.java
new file mode 100644
index 0000000000..9fd15a0a92
--- /dev/null
+++ b/core/src/main/java/com/datastrato/graviton/storage/RandomIdGenerator.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.graviton.storage;
+
+import java.util.UUID;
+
+/**
+ * Random id generator. This is used to generate random id for entities. Please see {@link
+ * com.datastrato.graviton.meta.BaseMetalake#ID} for more details.
+ */
+public class RandomIdGenerator implements IdGenerator {
+
+ public static final RandomIdGenerator INSTACNE = new RandomIdGenerator();
+
+ @Override
+ public long nextId() {
+ return UUID.randomUUID().getLeastSignificantBits();
+ }
+}
diff --git a/core/src/main/java/com/datastrato/graviton/storage/kv/CustomEntityKeyEncoder.java b/core/src/main/java/com/datastrato/graviton/storage/kv/BinaryEntityKeyEncoder.java
similarity index 95%
rename from core/src/main/java/com/datastrato/graviton/storage/kv/CustomEntityKeyEncoder.java
rename to core/src/main/java/com/datastrato/graviton/storage/kv/BinaryEntityKeyEncoder.java
index 33744374f5..75bb5e2646 100644
--- a/core/src/main/java/com/datastrato/graviton/storage/kv/CustomEntityKeyEncoder.java
+++ b/core/src/main/java/com/datastrato/graviton/storage/kv/BinaryEntityKeyEncoder.java
@@ -13,6 +13,7 @@ import com.datastrato.graviton.Entity.EntityIdentifier;
import com.datastrato.graviton.Entity.EntityType;
import com.datastrato.graviton.NameIdentifier;
import com.datastrato.graviton.exceptions.NoSuchEntityException;
+import com.datastrato.graviton.storage.EntityKeyEncoder;
import com.datastrato.graviton.util.ByteUtils;
import com.datastrato.graviton.util.Bytes;
import com.google.common.annotations.VisibleForTesting;
@@ -45,8 +46,8 @@ import org.slf4j.LoggerFactory;
* to_{ml_id}_{ca_id}_{br_id}_{to_id} -----> topic_info
* </pre>
*/
-public class CustomEntityKeyEncoder implements EntityKeyEncoder {
- public static final Logger LOG = LoggerFactory.getLogger(CustomEntityKeyEncoder.class);
+public class BinaryEntityKeyEncoder implements EntityKeyEncoder<byte[]> {
+ public static final Logger LOG = LoggerFactory.getLogger(BinaryEntityKeyEncoder.class);
// name prefix of name in name to id mapping,
// e.g., name_metalake1 -> 1
@@ -76,7 +77,7 @@ public class CustomEntityKeyEncoder implements EntityKeyEncoder {
SCHEMA, new String[] {SCHEMA.getShortName() + "_", "_", "_"},
TABLE, new String[] {TABLE.getShortName() + "_", "_", "_", "_"});
- public CustomEntityKeyEncoder(KvBackend backend) {
+ public BinaryEntityKeyEncoder(KvBackend backend) {
this.backend = backend;
}
@@ -229,9 +230,9 @@ public class CustomEntityKeyEncoder implements EntityKeyEncoder {
}
/**
- * Encodes a Namespace object into a byte array for use as a key in a key-value store.
+ * Encodes an entity object into a byte array for use as a key in a key-value store.
*
- * @param namespace The Namespace object to be encoded.
+ * @param entityIdentifier The entity object to be encoded.
* @return The byte array representing the encoded key.
*/
@Override
diff --git a/core/src/main/java/com/datastrato/graviton/storage/kv/EntityKeyEncoder.java b/core/src/main/java/com/datastrato/graviton/storage/kv/EntityKeyEncoder.java
deleted file mode 100644
index bd083c280b..0000000000
--- a/core/src/main/java/com/datastrato/graviton/storage/kv/EntityKeyEncoder.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright 2023 Datastrato.
- * This software is licensed under the Apache License version 2.
- */
-
-package com.datastrato.graviton.storage.kv;
-
-import com.datastrato.graviton.Entity.EntityIdentifier;
-import java.io.IOException;
-
-/**
- * Interface for encoding entity key for KV backend, e.g., RocksDB. The key is used to store the
- * entity in the backend.
- */
-public interface EntityKeyEncoder {
- /**
- * Construct the key for key-value store from the entity NameIdentifier and EntityType.
- *
- * @param entityIdentifier entity identifier
- * @param createIdIfNotExists create the id mapping for name if not exists
- * @return encoded key for key-value store
- * @throws IOException
- */
- byte[] encode(EntityIdentifier entityIdentifier, boolean createIdIfNotExists) throws IOException;
-}
diff --git a/core/src/main/java/com/datastrato/graviton/storage/kv/KvEntityStore.java b/core/src/main/java/com/datastrato/graviton/storage/kv/KvEntityStore.java
index e825db687e..b3eae966c1 100644
--- a/core/src/main/java/com/datastrato/graviton/storage/kv/KvEntityStore.java
+++ b/core/src/main/java/com/datastrato/graviton/storage/kv/KvEntityStore.java
@@ -18,6 +18,7 @@ import com.datastrato.graviton.HasIdentifier;
import com.datastrato.graviton.NameIdentifier;
import com.datastrato.graviton.Namespace;
import com.datastrato.graviton.exceptions.NoSuchEntityException;
+import com.datastrato.graviton.storage.EntityKeyEncoder;
import com.datastrato.graviton.util.Bytes;
import com.datastrato.graviton.util.Executable;
import com.google.common.annotations.VisibleForTesting;
@@ -49,7 +50,7 @@ public class KvEntityStore implements EntityStore {
@Override
public void initialize(Config config) throws RuntimeException {
this.backend = createKvEntityBackend(config);
- this.entityKeyEncoder = new CustomEntityKeyEncoder(backend);
+ this.entityKeyEncoder = new BinaryEntityKeyEncoder(backend);
}
@Override
diff --git a/core/src/test/java/com/datastrato/graviton/storage/kv/TestEntityKeyEncoding.java b/core/src/test/java/com/datastrato/graviton/storage/kv/TestEntityKeyEncoding.java
index 0189241e61..34fa2ca0a8 100644
--- a/core/src/test/java/com/datastrato/graviton/storage/kv/TestEntityKeyEncoding.java
+++ b/core/src/test/java/com/datastrato/graviton/storage/kv/TestEntityKeyEncoding.java
@@ -9,8 +9,8 @@ import static com.datastrato.graviton.Configs.DEFUALT_ENTITY_KV_STORE;
import static com.datastrato.graviton.Configs.ENTITY_KV_STORE;
import static com.datastrato.graviton.Configs.ENTITY_STORE;
import static com.datastrato.graviton.Configs.ENTRY_KV_ROCKSDB_BACKEND_PATH;
-import static com.datastrato.graviton.storage.kv.CustomEntityKeyEncoder.NAMESPACE_SEPARATOR;
-import static com.datastrato.graviton.storage.kv.CustomEntityKeyEncoder.WILD_CARD;
+import static com.datastrato.graviton.storage.kv.BinaryEntityKeyEncoder.NAMESPACE_SEPARATOR;
+import static com.datastrato.graviton.storage.kv.BinaryEntityKeyEncoder.WILD_CARD;
import com.datastrato.graviton.Config;
import com.datastrato.graviton.Configs;
@@ -45,7 +45,7 @@ public class TestEntityKeyEncoding {
private static Config config;
public static EntityStore ENTITY_STORE_INSTANCE;
- private static CustomEntityKeyEncoder ENCODER;
+ private static BinaryEntityKeyEncoder ENCODER;
@BeforeEach
public void createEntityEncoderInstance() {
@@ -54,7 +54,7 @@ public class TestEntityKeyEncoding {
ENTITY_STORE_INSTANCE.initialize(config);
ENTITY_STORE_INSTANCE.setSerDe(
EntitySerDeFactory.createEntitySerDe(config.get(Configs.ENTITY_SERDE)));
- ENCODER = new CustomEntityKeyEncoder(((KvEntityStore) ENTITY_STORE_INSTANCE).getBackend());
+ ENCODER = new BinaryEntityKeyEncoder(((KvEntityStore) ENTITY_STORE_INSTANCE).getBackend());
}
@AfterEach
@@ -87,8 +87,8 @@ public class TestEntityKeyEncoding {
// Metalake
// metalake1 --> 1000000
Namespace namespace = Namespace.of();
- CustomEntityKeyEncoder mockEncoder = Mockito.spy(ENCODER);
- Field f = CustomEntityKeyEncoder.class.getDeclaredField("backend");
+ BinaryEntityKeyEncoder mockEncoder = Mockito.spy(ENCODER);
+ Field f = BinaryEntityKeyEncoder.class.getDeclaredField("backend");
f.setAccessible(true);
KvBackend backend = (KvBackend) f.get(mockEncoder);
@@ -220,7 +220,7 @@ public class TestEntityKeyEncoding {
@Test
public void testTransaction() throws IOException {
Namespace namespace = Namespace.of();
- CustomEntityKeyEncoder mockEncoder = Mockito.spy(ENCODER);
+ BinaryEntityKeyEncoder mockEncoder = Mockito.spy(ENCODER);
Mockito.doReturn(1000000L)
.when(mockEncoder)
@@ -240,9 +240,9 @@ public class TestEntityKeyEncoding {
throws IOException, IllegalAccessException, NoSuchFieldException {
// Scan all Metalake
Namespace namespace = Namespace.of();
- CustomEntityKeyEncoder mockEncoder = Mockito.spy(ENCODER);
+ BinaryEntityKeyEncoder mockEncoder = Mockito.spy(ENCODER);
- Field f = CustomEntityKeyEncoder.class.getDeclaredField("backend");
+ Field f = BinaryEntityKeyEncoder.class.getDeclaredField("backend");
f.setAccessible(true);
KvBackend backend = (KvBackend) f.get(mockEncoder);