forked from linkedin/datahub-gma
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
read column from aspect alias part 2 (linkedin#436)
Co-authored-by: Yang Yang <[email protected]>
- Loading branch information
1 parent
3f21e7c
commit 56c19cb
Showing
10 changed files
with
261 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
dao-impl/ebean-dao/src/main/java/com/linkedin/metadata/dao/GlobalAssetRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.linkedin.metadata.dao; | ||
|
||
import com.linkedin.data.template.RecordTemplate; | ||
import com.linkedin.metadata.dao.utils.ModelUtils; | ||
import com.linkedin.metadata.validator.AssetValidator; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import javax.annotation.Nonnull; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.reflections.Reflections; | ||
|
||
|
||
/** | ||
* This class tracks the (entityType, Asset) map. | ||
*/ | ||
@Slf4j | ||
public class GlobalAssetRegistry { | ||
private final Map<String, Class<? extends RecordTemplate>> registry = new ConcurrentHashMap<>(); | ||
|
||
private GlobalAssetRegistry() { | ||
preLoadInternalAssets(); | ||
} | ||
|
||
// thread-safe, lazy-load singleton instance. | ||
// JVM guarantees static fields is only instantiated once and in a thread-safe manner when class is first being loaded. | ||
// Putting it in the inner class makes this inner only being loaded when getInstance() is called. | ||
private static class InnerHolder { | ||
private static final GlobalAssetRegistry INSTANCE = new GlobalAssetRegistry(); | ||
} | ||
|
||
private static GlobalAssetRegistry getInstance() { | ||
return InnerHolder.INSTANCE; | ||
} | ||
|
||
public static void register(@Nonnull String assetType, @Nonnull Class<? extends RecordTemplate> assetClass) { | ||
AssetValidator.validateAssetSchema(assetClass); | ||
getInstance().registry.put(assetType, assetClass); | ||
} | ||
|
||
public static Class<? extends RecordTemplate> get(@Nonnull String assetType) { | ||
return getInstance().registry.get(assetType); | ||
} | ||
|
||
/** | ||
* TODO: moving this loading logic into internal-models. | ||
*/ | ||
private void preLoadInternalAssets() { | ||
Reflections reflections = new Reflections(INTERNAL_ASSET_PACKAGE); // Change to your package | ||
Set<Class<? extends RecordTemplate>> assetClasses = reflections.getSubTypesOf(RecordTemplate.class); | ||
for (Class<? extends RecordTemplate> assetClass : assetClasses) { | ||
try { | ||
register(ModelUtils.getUrnTypeFromAsset(assetClass), assetClass); | ||
} catch (Exception e) { | ||
log.error("failed to load asset: " + assetClass, e); | ||
} | ||
} | ||
} | ||
|
||
private static final String INTERNAL_ASSET_PACKAGE = "pegasus.com.linkedin.metadata.asset"; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
dao-impl/ebean-dao/src/test/java/com/linkedin/metadata/dao/GlobalAssetRegistryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.linkedin.metadata.dao; | ||
|
||
import com.linkedin.testing.AspectBar; | ||
import com.linkedin.testing.BarAsset; | ||
import com.linkedin.testing.urn.BarUrn; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.testng.Assert.*; | ||
|
||
|
||
public class GlobalAssetRegistryTest { | ||
@Test | ||
public void testGetInstance() { | ||
GlobalAssetRegistry.register(BarUrn.ENTITY_TYPE, BarAsset.class); | ||
try { | ||
GlobalAssetRegistry.register(BarUrn.ENTITY_TYPE, AspectBar.class); | ||
fail("should fail because of invalid aspect"); | ||
} catch (Exception e) { | ||
} | ||
|
||
assertEquals(GlobalAssetRegistry.get(BarUrn.ENTITY_TYPE), BarAsset.class); | ||
assertNull(GlobalAssetRegistry.get("unknownType")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.