2828import org .apache .atlas .model .typedef .AtlasStructDef .AtlasAttributeDef ;
2929import org .apache .atlas .model .typedef .AtlasStructDef .AtlasConstraintDef ;
3030import org .apache .atlas .repository .Constants ;
31+ import org .apache .atlas .repository .IndexException ;
32+ import org .apache .atlas .repository .graphdb .AtlasCardinality ;
33+ import org .apache .atlas .repository .graphdb .AtlasGraphManagement ;
3134import org .apache .atlas .repository .graphdb .AtlasVertex ;
3235import org .apache .atlas .type .AtlasRelationshipType ;
3336import org .apache .atlas .type .AtlasStructType ;
@@ -64,28 +67,19 @@ public AtlasStructDefStoreV2(AtlasTypeDefGraphStoreV2 typeDefStore, AtlasTypeReg
6467 public static void updateVertexPreCreate (AtlasStructDef structDef , AtlasStructType structType , AtlasVertex vertex , AtlasTypeDefGraphStoreV2 typeDefStore ) throws AtlasBaseException {
6568 List <String > attrNames = new ArrayList <>(structDef .getAttributeDefs ().size ());
6669
67- for (AtlasAttributeDef attributeDef : structDef .getAttributeDefs ()) {
68- // Validate the mandatory features of an attribute (compatibility with legacy type system)
69- if (StringUtils .isEmpty (attributeDef .getName ())) {
70- throw new AtlasBaseException (AtlasErrorCode .MISSING_MANDATORY_ATTRIBUTE , structDef .getName (), "name" );
71- }
72-
73- if (StringUtils .isEmpty (attributeDef .getTypeName ())) {
74- throw new AtlasBaseException (AtlasErrorCode .MISSING_MANDATORY_ATTRIBUTE , structDef .getName (), "typeName" );
75- }
70+ createPropertyKeys (structDef , typeDefStore );
7671
77- String propertyKey = AtlasGraphUtilsV2 . getTypeDefPropertyKey ( structDef , attributeDef . getName ());
78- String encodedPropertyKey = AtlasGraphUtilsV2 .encodePropertyKey ( propertyKey );
72+ for ( AtlasAttributeDef attributeDef : structDef . getAttributeDefs ()) {
73+ String propertyKey = AtlasGraphUtilsV2 .getTypeDefPropertyKey ( structDef , attributeDef . getName () );
7974
80- vertex .setProperty (encodedPropertyKey , toJsonFromAttribute (structType .getAttribute (attributeDef .getName ())));
75+ vertex .setProperty (AtlasGraphUtilsV2 . encodePropertyKey ( propertyKey ) , toJsonFromAttribute (structType .getAttribute (attributeDef .getName ())));
8176
8277 attrNames .add (attributeDef .getName ());
8378 }
8479
85- String typeNamePropertyKey = AtlasGraphUtilsV2 .getTypeDefPropertyKey (structDef );
86- String encodedtypeNamePropertyKey = AtlasGraphUtilsV2 .encodePropertyKey (typeNamePropertyKey );
80+ String typeNamePropertyKey = AtlasGraphUtilsV2 .getTypeDefPropertyKey (structDef );
8781
88- vertex .setProperty (encodedtypeNamePropertyKey , attrNames );
82+ vertex .setProperty (AtlasGraphUtilsV2 . encodePropertyKey ( typeNamePropertyKey ) , attrNames );
8983 }
9084
9185 public static void updateVertexPreUpdate (AtlasStructDef structDef , AtlasStructType structType , AtlasVertex vertex , AtlasTypeDefGraphStoreV2 typeDefStore ) throws AtlasBaseException {
@@ -99,12 +93,15 @@ public static void updateVertexPreUpdate(AtlasStructDef structDef, AtlasStructTy
9993
10094 String structDefPropertyKey = AtlasGraphUtilsV2 .getTypeDefPropertyKey (structDef );
10195 String encodedStructDefPropertyKey = encodePropertyKey (structDefPropertyKey );
102- List <String > currAttrNames = vertex .getProperty (encodedStructDefPropertyKey , List .class );
96+ Object names = vertex .getProperty (encodedStructDefPropertyKey , Object .class );
97+ List <String > currAttrNames = names instanceof List ? (List <String >) names : new ArrayList <>();
10398
10499 // delete attributes that are not present in updated structDef
105100 if (CollectionUtils .isNotEmpty (currAttrNames )) {
106101 List <String > removedAttributes = null ;
107102
103+ createPropertyKeys (structDef , typeDefStore );
104+
108105 for (String currAttrName : currAttrNames ) {
109106 if (!attrNames .contains (currAttrName )) {
110107 if (RequestContext .get ().isInTypePatching ()) {
@@ -136,6 +133,10 @@ public static void updateVertexPreUpdate(AtlasStructDef structDef, AtlasStructTy
136133 vertex .removeProperty (propertyKey );
137134 }
138135 }
136+ } else {
137+ if (names == null ) {
138+ LOG .warn ("failed to load attribute names for type {}" , structDef .getName ());
139+ }
139140 }
140141
141142 typeDefStore .updateTypeVertex (structDef , vertex );
@@ -207,7 +208,8 @@ public static AtlasStructDef toStructDef(AtlasVertex vertex, AtlasStructDef stru
207208 List <AtlasAttributeDef > attributeDefs = new ArrayList <>();
208209 String typePropertyKey = AtlasGraphUtilsV2 .getTypeDefPropertyKey (ret );
209210 String encodedTypePropertyKey = AtlasGraphUtilsV2 .encodePropertyKey (typePropertyKey );
210- List <String > attrNames = vertex .getProperty (encodedTypePropertyKey , List .class );
211+ Object names = vertex .getProperty (encodedTypePropertyKey , Object .class );
212+ List <String > attrNames = names instanceof List ? (List <String >) names : new ArrayList <>();
211213
212214 if (CollectionUtils .isNotEmpty (attrNames )) {
213215 for (String attrName : attrNames ) {
@@ -223,6 +225,10 @@ public static AtlasStructDef toStructDef(AtlasVertex vertex, AtlasStructDef stru
223225
224226 attributeDefs .add (toAttributeDefFromJson (structDef , AtlasType .fromJson (attrJson , Map .class ), typeDefStore ));
225227 }
228+ } else {
229+ if (names == null ) {
230+ LOG .warn ("failed to load attribute names for type {}" , structDef );
231+ }
226232 }
227233
228234 ret .setAttributeDefs (attributeDefs );
@@ -638,4 +644,35 @@ private static void addReferencesForAttribute(AtlasVertex vertex, AtlasAttribute
638644 }
639645 }
640646 }
647+
648+ private static void createPropertyKeys (AtlasStructDef structDef , AtlasTypeDefGraphStoreV2 typeDefStore ) throws AtlasBaseException {
649+ AtlasGraphManagement management = typeDefStore .atlasGraph .getManagementSystem ();
650+
651+ for (AtlasAttributeDef attributeDef : structDef .getAttributeDefs ()) {
652+ // Validate the mandatory features of an attribute (compatibility with legacy type system)
653+ if (StringUtils .isEmpty (attributeDef .getName ())) {
654+ throw new AtlasBaseException (AtlasErrorCode .MISSING_MANDATORY_ATTRIBUTE , structDef .getName (), "name" );
655+ }
656+
657+ if (StringUtils .isEmpty (attributeDef .getTypeName ())) {
658+ throw new AtlasBaseException (AtlasErrorCode .MISSING_MANDATORY_ATTRIBUTE , structDef .getName (), "typeName" );
659+ }
660+
661+ String propertyKey = AtlasGraphUtilsV2 .getTypeDefPropertyKey (structDef , attributeDef .getName ());
662+
663+ createPropertyKey (AtlasGraphUtilsV2 .encodePropertyKey (propertyKey ), String .class , AtlasCardinality .SINGLE , management );
664+ }
665+
666+ String typeNamePropertyKey = AtlasGraphUtilsV2 .getTypeDefPropertyKey (structDef );
667+
668+ createPropertyKey (AtlasGraphUtilsV2 .encodePropertyKey (typeNamePropertyKey ), Object .class , AtlasCardinality .SINGLE , management );
669+
670+ try {
671+ management .commit ();
672+ } catch (Exception e ) {
673+ LOG .error ("PropertyKey creation failed" , e );
674+
675+ throw new AtlasBaseException (new IndexException ("Index commit failed" , e ));
676+ }
677+ }
641678}
0 commit comments