This is an automated email from the ASF dual-hosted git repository.

madhan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/atlas.git


The following commit(s) were added to refs/heads/master by this push:
     new 25305c0dd ATLAS-5139: updated AtlasTypeRegistry with methods to 
add/update types (#446)
25305c0dd is described below

commit 25305c0dde397188866fe2fbf6e634a9dd8d7637
Author: Madhan Neethiraj <[email protected]>
AuthorDate: Fri Sep 19 15:07:34 2025 -0700

    ATLAS-5139: updated AtlasTypeRegistry with methods to add/update types 
(#446)
---
 .../org/apache/atlas/type/AtlasTypeRegistry.java   |  36 ++++
 .../apache/atlas/type/TestAtlasTypeRegistry.java   | 208 +++++----------------
 2 files changed, 81 insertions(+), 163 deletions(-)

diff --git a/intg/src/main/java/org/apache/atlas/type/AtlasTypeRegistry.java 
b/intg/src/main/java/org/apache/atlas/type/AtlasTypeRegistry.java
index 590ebda68..7c4af6c35 100644
--- a/intg/src/main/java/org/apache/atlas/type/AtlasTypeRegistry.java
+++ b/intg/src/main/java/org/apache/atlas/type/AtlasTypeRegistry.java
@@ -80,6 +80,12 @@ public class AtlasTypeRegistry {
         resolveIndexFieldNamesForRootTypes();
     }
 
+    public AtlasTypeRegistry(AtlasTypesDef typesDef) throws AtlasBaseException 
{
+        this();
+
+        updateTypes(typesDef);
+    }
+
     // used only by AtlasTransientTypeRegistry
     protected AtlasTypeRegistry(AtlasTypeRegistry other) {
         registryData              = new RegistryData();
@@ -297,6 +303,36 @@ public class AtlasTypeRegistry {
         return registryData.relationshipDefs.getTypeByName(name);
     }
 
+    public void updateTypes(AtlasTypesDef typesDef) throws AtlasBaseException {
+        if (typesDef != null) {
+            AtlasTransientTypeRegistry transientTypeRegistry = 
lockTypeRegistryForUpdate();
+            boolean                    commitUpdates         = false;
+
+            try {
+                transientTypeRegistry.updateTypes(typesDef);
+
+                commitUpdates = true;
+            } finally {
+                releaseTypeRegistryForUpdate(transientTypeRegistry, 
commitUpdates);
+            }
+        }
+    }
+
+    public void addType(AtlasBaseTypeDef typeDef) throws AtlasBaseException {
+        if (typeDef != null) {
+            AtlasTransientTypeRegistry transientTypeRegistry = 
lockTypeRegistryForUpdate();
+            boolean                    commitUpdates         = false;
+
+            try {
+                transientTypeRegistry.addType(typeDef);
+
+                commitUpdates = true;
+            } finally {
+                releaseTypeRegistryForUpdate(transientTypeRegistry, 
commitUpdates);
+            }
+        }
+    }
+
     public AtlasTransientTypeRegistry lockTypeRegistryForUpdate() throws 
AtlasBaseException {
         return 
lockTypeRegistryForUpdate(DEFAULT_LOCK_MAX_WAIT_TIME_IN_SECONDS);
     }
diff --git 
a/intg/src/test/java/org/apache/atlas/type/TestAtlasTypeRegistry.java 
b/intg/src/test/java/org/apache/atlas/type/TestAtlasTypeRegistry.java
index cffd31da9..84948ef27 100644
--- a/intg/src/test/java/org/apache/atlas/type/TestAtlasTypeRegistry.java
+++ b/intg/src/test/java/org/apache/atlas/type/TestAtlasTypeRegistry.java
@@ -44,6 +44,7 @@ import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
 
 public class TestAtlasTypeRegistry {
     /*
@@ -91,23 +92,14 @@ public class TestAtlasTypeRegistry {
         typesDef.getClassificationDefs().add(classifiL2d3);
         typesDef.getClassificationDefs().add(classifiL2d4);
 
-        AtlasTypeRegistry          typeRegistry = new AtlasTypeRegistry();
-        AtlasTransientTypeRegistry ttr          = null;
-        boolean                    commit       = false;
-        String                     failureMsg   = null;
+        AtlasTypeRegistry typeRegistry = null;
 
         try {
-            ttr = typeRegistry.lockTypeRegistryForUpdate();
-
-            ttr.addTypes(typesDef);
-
-            commit = true;
+            typeRegistry = new AtlasTypeRegistry(typesDef);
         } catch (AtlasBaseException excp) {
-            failureMsg = excp.getMessage();
-        } finally {
-            typeRegistry.releaseTypeRegistryForUpdate(ttr, commit);
+            fail("unexpected failure", excp);
         }
-        assertNull(failureMsg);
+        assertNotNull(typeRegistry);
 
         validateAllSuperTypes(typeRegistry, "L0", new HashSet<>());
         validateAllSuperTypes(typeRegistry, "L1-1", new 
HashSet<>(Collections.singletonList("L0")));
@@ -148,23 +140,13 @@ public class TestAtlasTypeRegistry {
 
         classifiDef1.addSuperType(classifiDef1.getName());
 
-        AtlasTypeRegistry          typeRegistry = new AtlasTypeRegistry();
-        AtlasTransientTypeRegistry ttr          = null;
-        boolean                    commit       = false;
-        String                     failureMsg   = null;
-
         try {
-            ttr = typeRegistry.lockTypeRegistryForUpdate();
-
-            ttr.addType(classifiDef1);
+            AtlasTypeRegistry ignored = new AtlasTypeRegistry(new 
AtlasTypesDef(null, null, Collections.singletonList(classifiDef1), null, null));
 
-            commit = true;
+            fail("expected invalid supertype failure");
         } catch (AtlasBaseException excp) {
-            failureMsg = excp.getMessage();
-        } finally {
-            typeRegistry.releaseTypeRegistryForUpdate(ttr, commit);
+            assertEquals(excp.getAtlasErrorCode(), 
AtlasErrorCode.CIRCULAR_REFERENCE);
         }
-        assertNotNull(failureMsg, "expected invalid supertype failure");
     }
 
     /*
@@ -207,23 +189,13 @@ public class TestAtlasTypeRegistry {
         typesDef.getClassificationDefs().add(classifiL2d3);
         typesDef.getClassificationDefs().add(classifiL2d4);
 
-        AtlasTypeRegistry          typeRegistry = new AtlasTypeRegistry();
-        AtlasTransientTypeRegistry ttr          = null;
-        boolean                    commit       = false;
-        String                     failureMsg   = null;
-
         try {
-            ttr = typeRegistry.lockTypeRegistryForUpdate();
+            AtlasTypeRegistry ignored = new AtlasTypeRegistry(typesDef);
 
-            ttr.addTypes(typesDef);
-
-            commit = true;
+            fail("expected invalid supertype failure");
         } catch (AtlasBaseException excp) {
-            failureMsg = excp.getMessage();
-        } finally {
-            typeRegistry.releaseTypeRegistryForUpdate(ttr, commit);
+            assertEquals(excp.getAtlasErrorCode(), 
AtlasErrorCode.CIRCULAR_REFERENCE);
         }
-        assertNotNull(failureMsg, "expected invalid supertype failure");
     }
 
     /*
@@ -279,23 +251,14 @@ public class TestAtlasTypeRegistry {
         typesDef.getEntityDefs().add(entL2d3);
         typesDef.getEntityDefs().add(entL2d4);
 
-        AtlasTypeRegistry          typeRegistry = new AtlasTypeRegistry();
-        AtlasTransientTypeRegistry ttr          = null;
-        boolean                    commit       = false;
-        String                     failureMsg   = null;
+        AtlasTypeRegistry typeRegistry = null;
 
         try {
-            ttr = typeRegistry.lockTypeRegistryForUpdate();
-
-            ttr.addTypes(typesDef);
-
-            commit = true;
+            typeRegistry = new AtlasTypeRegistry(typesDef);
         } catch (AtlasBaseException excp) {
-            failureMsg = excp.getMessage();
-        } finally {
-            typeRegistry.releaseTypeRegistryForUpdate(ttr, commit);
+            fail("unexpected failure", excp);
         }
-        assertNull(failureMsg);
+        assertNotNull(typeRegistry);
 
         validateAllSuperTypes(typeRegistry, "L0", new HashSet<>());
         validateAllSuperTypes(typeRegistry, "L1-1", new 
HashSet<>(Collections.singletonList("L0")));
@@ -345,23 +308,13 @@ public class TestAtlasTypeRegistry {
 
         entDef1.addSuperType(entDef1.getName());
 
-        AtlasTypeRegistry          typeRegistry = new AtlasTypeRegistry();
-        AtlasTransientTypeRegistry ttr          = null;
-        boolean                    commit       = false;
-        String                     failureMsg   = null;
-
         try {
-            ttr = typeRegistry.lockTypeRegistryForUpdate();
+            AtlasTypeRegistry ignored = new AtlasTypeRegistry(new 
AtlasTypesDef(null, null, null, Collections.singletonList(entDef1), null));
 
-            ttr.addType(entDef1);
-
-            commit = true;
+            fail("expected invalid supertype failure");
         } catch (AtlasBaseException excp) {
-            failureMsg = excp.getMessage();
-        } finally {
-            typeRegistry.releaseTypeRegistryForUpdate(ttr, commit);
+            assertEquals(excp.getAtlasErrorCode(), 
AtlasErrorCode.CIRCULAR_REFERENCE);
         }
-        assertNotNull(failureMsg, "expected invalid supertype failure");
     }
 
     /*
@@ -404,23 +357,13 @@ public class TestAtlasTypeRegistry {
         typesDef.getEntityDefs().add(entL2d3);
         typesDef.getEntityDefs().add(entL2d4);
 
-        AtlasTypeRegistry          typeRegistry = new AtlasTypeRegistry();
-        AtlasTransientTypeRegistry ttr          = null;
-        boolean                    commit       = false;
-        String                     failureMsg   = null;
-
         try {
-            ttr = typeRegistry.lockTypeRegistryForUpdate();
-
-            ttr.addTypes(typesDef);
+            AtlasTypeRegistry ignored = new AtlasTypeRegistry(typesDef);
 
-            commit = true;
+            fail("expected invalid supertype failure");
         } catch (AtlasBaseException excp) {
-            failureMsg = excp.getMessage();
-        } finally {
-            typeRegistry.releaseTypeRegistryForUpdate(ttr, commit);
+            assertEquals(excp.getAtlasErrorCode(), 
AtlasErrorCode.CIRCULAR_REFERENCE);
         }
-        assertNotNull(failureMsg, "expected invalid supertype failure");
     }
 
     @Test
@@ -428,7 +371,6 @@ public class TestAtlasTypeRegistry {
         AtlasTypeRegistry          typeRegistry = new AtlasTypeRegistry();
         AtlasTransientTypeRegistry ttr          = null;
         boolean                    commit       = false;
-        String                     failureMsg   = null;
         AtlasClassificationDef     testTag1     = new 
AtlasClassificationDef("testTag1");
         AtlasClassificationDef     testTag2     = new 
AtlasClassificationDef("testTag2");
 
@@ -451,11 +393,10 @@ public class TestAtlasTypeRegistry {
 
             commit = true;
         } catch (AtlasBaseException excp) {
-            failureMsg = excp.getMessage();
+            fail("unexpected failure", excp);
         } finally {
             typeRegistry.releaseTypeRegistryForUpdate(ttr, commit);
         }
-        assertNull(failureMsg);
         assertTrue(typeRegistry.isRegisteredType(testTag1.getName()));
         assertTrue(typeRegistry.isRegisteredType(testTag2.getName()));
     }
@@ -539,23 +480,14 @@ public class TestAtlasTypeRegistry {
         typesDef.getEntityDefs().add(entL0);
         typesDef.getEntityDefs().add(entL1);
 
-        AtlasTypeRegistry          typeRegistry = new AtlasTypeRegistry();
-        AtlasTransientTypeRegistry ttr          = null;
-        boolean                    commit       = false;
-        String                     failureMsg   = null;
+        AtlasTypeRegistry typeRegistry = null;
 
         try {
-            ttr = typeRegistry.lockTypeRegistryForUpdate();
-
-            ttr.addTypes(typesDef);
-
-            commit = true;
+            typeRegistry = new AtlasTypeRegistry(typesDef);
         } catch (AtlasBaseException excp) {
-            failureMsg = excp.getMessage();
-        } finally {
-            typeRegistry.releaseTypeRegistryForUpdate(ttr, commit);
+            fail("unexpected failure", excp);
         }
-        assertNull(failureMsg);
+        assertNotNull(typeRegistry);
 
         validateAllSuperTypes(typeRegistry, "L0", new HashSet<>());
         validateAllSubTypes(typeRegistry, "L0", new 
HashSet<>(Collections.singletonList("L1")));
@@ -573,19 +505,12 @@ public class TestAtlasTypeRegistry {
         typesDef.getEntityDefs().add(entL2);
 
         try {
-            commit = false;
-
-            ttr = typeRegistry.lockTypeRegistryForUpdate();
+            typeRegistry.updateTypes(typesDef);
 
-            ttr.updateTypes(typesDef);
-
-            commit = true;
+            fail("type update should have failed");
         } catch (AtlasBaseException excp) {
-            failureMsg = excp.getMessage();
-        } finally {
-            typeRegistry.releaseTypeRegistryForUpdate(ttr, commit);
+            assertEquals(excp.getAtlasErrorCode(), 
AtlasErrorCode.CIRCULAR_REFERENCE);
         }
-        assertNotNull(failureMsg);
 
         assertNull(typeRegistry.getEntityTypeByName("L2"));
 
@@ -619,23 +544,13 @@ public class TestAtlasTypeRegistry {
         typesDef.getEntityDefs().add(entL1);
         typesDef.getEntityDefs().add(entL2);
 
-        AtlasTypeRegistry          typeRegistry = new AtlasTypeRegistry();
-        AtlasTransientTypeRegistry ttr          = null;
-        boolean                    commit       = false;
-        String                     failureMsg   = null;
+        AtlasTypeRegistry typeRegistry = null;
 
         try {
-            ttr = typeRegistry.lockTypeRegistryForUpdate();
-
-            ttr.addTypes(typesDef);
-
-            commit = true;
+            typeRegistry = new AtlasTypeRegistry(typesDef);
         } catch (AtlasBaseException excp) {
-            failureMsg = excp.getMessage();
-        } finally {
-            typeRegistry.releaseTypeRegistryForUpdate(ttr, commit);
+            fail("unexpected failure", excp);
         }
-        assertNull(failureMsg);
 
         validateAllSuperTypes(typeRegistry, "L1", new 
HashSet<>(Collections.singletonList("L0")));
         validateAllSubTypes(typeRegistry, "L1", new HashSet<>());
@@ -644,19 +559,12 @@ public class TestAtlasTypeRegistry {
         entL1.addSuperType(entL2.getName());
 
         try {
-            commit = false;
+            typeRegistry.updateTypes(typesDef);
 
-            ttr = typeRegistry.lockTypeRegistryForUpdate();
-
-            ttr.updateTypes(typesDef);
-
-            commit = true;
+            fail("type update should have failed");
         } catch (AtlasBaseException excp) {
-            failureMsg = excp.getMessage();
-        } finally {
-            typeRegistry.releaseTypeRegistryForUpdate(ttr, commit);
+            assertEquals(excp.getAtlasErrorCode(), 
AtlasErrorCode.ATTRIBUTE_NAME_ALREADY_EXISTS_IN_ANOTHER_PARENT_TYPE);
         }
-        assertNotNull(failureMsg);
 
         validateAllSuperTypes(typeRegistry, "L1", new 
HashSet<>(Collections.singletonList("L0")));
         validateAllSubTypes(typeRegistry, "L1", new HashSet<>());
@@ -680,23 +588,14 @@ public class TestAtlasTypeRegistry {
         typesDef.getClassificationDefs().add(class1);
         typesDef.getClassificationDefs().add(class2);
 
-        AtlasTypeRegistry          typeRegistry = new AtlasTypeRegistry();
-        AtlasTransientTypeRegistry ttr          = null;
-        boolean                    commit       = false;
-        String                     failureMsg   = null;
+        AtlasTypeRegistry typeRegistry = null;
 
         try {
-            ttr = typeRegistry.lockTypeRegistryForUpdate();
-
-            ttr.addTypes(typesDef);
-
-            commit = true;
+            typeRegistry = new AtlasTypeRegistry(typesDef);
         } catch (AtlasBaseException excp) {
-            failureMsg = excp.getMessage();
-        } finally {
-            typeRegistry.releaseTypeRegistryForUpdate(ttr, commit);
+            fail("unexpected failure", excp);
         }
-        assertNull(failureMsg);
+        assertNotNull(typeRegistry);
 
         validateAllSuperTypes(typeRegistry, "class1", new 
HashSet<>(Collections.singletonList("class0")));
         validateAllSubTypes(typeRegistry, "class1", new HashSet<>());
@@ -704,45 +603,28 @@ public class TestAtlasTypeRegistry {
         //Add class2 as supertype for class1
         class1.addSuperType(class2.getName());
 
-        AtlasErrorCode atlasErrorCode = null;
         try {
-            commit = false;
-
-            ttr = typeRegistry.lockTypeRegistryForUpdate();
-
-            ttr.updateTypes(typesDef);
+            typeRegistry.updateTypes(typesDef);
 
-            commit = true;
+            fail("type update should have failed");
         } catch (AtlasBaseException excp) {
-            failureMsg     = excp.getMessage();
-            atlasErrorCode = excp.getAtlasErrorCode();
-        } finally {
-            typeRegistry.releaseTypeRegistryForUpdate(ttr, commit);
+            assertEquals(excp.getAtlasErrorCode(), 
AtlasErrorCode.ATTRIBUTE_NAME_ALREADY_EXISTS_IN_ANOTHER_PARENT_TYPE);
         }
-        assertNotNull(failureMsg);
-        assertEquals(atlasErrorCode, 
AtlasErrorCode.ATTRIBUTE_NAME_ALREADY_EXISTS_IN_ANOTHER_PARENT_TYPE);
 
         validateAllSuperTypes(typeRegistry, "class1", new 
HashSet<>(Collections.singletonList("class0")));
         validateAllSubTypes(typeRegistry, "class1", new HashSet<>());
     }
 
     private boolean addType(AtlasTypeRegistry typeRegistry, AtlasBaseTypeDef 
typeDef) {
-        boolean                    ret = false;
-        AtlasTransientTypeRegistry ttr = null;
-
         try {
-            ttr = typeRegistry.lockTypeRegistryForUpdate();
-
-            ttr.addType(typeDef);
+            typeRegistry.addType(typeDef);
 
-            ret = true;
+            return true;
         } catch (AtlasBaseException excp) {
             // ignore
-        } finally {
-            typeRegistry.releaseTypeRegistryForUpdate(ttr, ret);
         }
 
-        return ret;
+        return false;
     }
 
     private void validateAllSuperTypes(AtlasTypeRegistry typeRegistry, String 
typeName, Set<String> expectedSuperTypes) {

Reply via email to