Commit 5a4dd2e7 by Madhan Neethiraj

ATLAS-1235: TypesREST.update() does not update fields like description

parent a96424a1
...@@ -65,22 +65,13 @@ public class AtlasClassificationType extends AtlasStructType { ...@@ -65,22 +65,13 @@ public class AtlasClassificationType extends AtlasStructType {
super.resolveReferences(typeRegistry); super.resolveReferences(typeRegistry);
List<AtlasClassificationType> s = new ArrayList<AtlasClassificationType>(); List<AtlasClassificationType> s = new ArrayList<AtlasClassificationType>();
Set<String> allS = new HashSet<String>(); Set<String> allS = getAllSuperTypes(typeRegistry);
for (String superTypeName : classificationDef.getSuperTypes()) { for (String superTypeName : classificationDef.getSuperTypes()) {
AtlasType superType = typeRegistry.getType(superTypeName); AtlasType superType = typeRegistry.getType(superTypeName);
if (superType instanceof AtlasClassificationType) { if (superType instanceof AtlasClassificationType) {
AtlasClassificationType superClassificationType = (AtlasClassificationType)superType; s.add((AtlasClassificationType)superType);
superClassificationType.resolveReferences(typeRegistry);
s.add(superClassificationType);
allS.add(superTypeName);
if (CollectionUtils.isNotEmpty(superClassificationType.getAllSuperTypes())) {
allS.addAll(superClassificationType.getAllSuperTypes());
}
} else { } else {
throw new AtlasBaseException(superTypeName + ": incompatible supertype in classification " throw new AtlasBaseException(superTypeName + ": incompatible supertype in classification "
+ classificationDef.getName()); + classificationDef.getName());
...@@ -88,7 +79,7 @@ public class AtlasClassificationType extends AtlasStructType { ...@@ -88,7 +79,7 @@ public class AtlasClassificationType extends AtlasStructType {
} }
this.superTypes = Collections.unmodifiableList(s); this.superTypes = Collections.unmodifiableList(s);
this.allSuperTypes = allS; this.allSuperTypes = Collections.unmodifiableSet(allS);
} }
public Set<String> getSuperTypes() { public Set<String> getSuperTypes() {
...@@ -193,4 +184,41 @@ public class AtlasClassificationType extends AtlasStructType { ...@@ -193,4 +184,41 @@ public class AtlasClassificationType extends AtlasStructType {
super.populateDefaultValues(classification); super.populateDefaultValues(classification);
} }
} }
private Set<String> getAllSuperTypes(AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
Set<String> superTypes = new HashSet<>();
List<String> subTypes = new ArrayList<>();
collectAllSuperTypes(subTypes, superTypes, typeRegistry);
return superTypes;
}
/*
* This method should not assume that resolveReferences() has been called on all superTypes.
* this.classificationDef is the only safe member to reference here
*/
private void collectAllSuperTypes(List<String> subTypes, Set<String> superTypes, AtlasTypeRegistry typeRegistry)
throws AtlasBaseException {
if (subTypes.contains(classificationDef.getName())) {
throw new AtlasBaseException(classificationDef.getName()
+ ": invalid supertypes - circular reference back to self " + subTypes);
}
if (CollectionUtils.isNotEmpty(classificationDef.getSuperTypes())) {
superTypes.addAll(classificationDef.getSuperTypes());
subTypes.add(classificationDef.getName());
for (String superTypeName : classificationDef.getSuperTypes()) {
AtlasType type = typeRegistry.getType(superTypeName);
if (type instanceof AtlasClassificationType) {
AtlasClassificationType superType = (AtlasClassificationType) type;
superType.collectAllSuperTypes(subTypes, superTypes, typeRegistry);
}
}
subTypes.remove(classificationDef.getName());
}
}
} }
...@@ -63,22 +63,13 @@ public class AtlasEntityType extends AtlasStructType { ...@@ -63,22 +63,13 @@ public class AtlasEntityType extends AtlasStructType {
super.resolveReferences(typeRegistry); super.resolveReferences(typeRegistry);
List<AtlasEntityType> s = new ArrayList<AtlasEntityType>(); List<AtlasEntityType> s = new ArrayList<AtlasEntityType>();
Set<String> allS = new HashSet<String>(); Set<String> allS = getAllSuperTypes(typeRegistry);
for (String superTypeName : entityDef.getSuperTypes()) { for (String superTypeName : entityDef.getSuperTypes()) {
AtlasType superType = typeRegistry.getType(superTypeName); AtlasType superType = typeRegistry.getType(superTypeName);
if (superType instanceof AtlasEntityType) { if (superType instanceof AtlasEntityType) {
AtlasEntityType superEntityType = (AtlasEntityType)superType; s.add((AtlasEntityType)superType);
superEntityType.resolveReferences(typeRegistry);
s.add(superEntityType);
allS.add(superTypeName);
if (CollectionUtils.isNotEmpty(superEntityType.getAllSuperTypes())) {
allS.addAll(superEntityType.getAllSuperTypes());
}
} else { } else {
throw new AtlasBaseException(superTypeName + ": incompatible supertype in entity " throw new AtlasBaseException(superTypeName + ": incompatible supertype in entity "
+ entityDef.getName()); + entityDef.getName());
...@@ -193,4 +184,41 @@ public class AtlasEntityType extends AtlasStructType { ...@@ -193,4 +184,41 @@ public class AtlasEntityType extends AtlasStructType {
super.populateDefaultValues(ent); super.populateDefaultValues(ent);
} }
} }
private Set<String> getAllSuperTypes(AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
Set<String> superTypes = new HashSet<>();
List<String> subTypes = new ArrayList<>();
collectAllSuperTypes(subTypes, superTypes, typeRegistry);
return superTypes;
}
/*
* This method should not assume that resolveReferences() has been called on all superTypes.
* this.entityDef is the only safe member to reference here
*/
private void collectAllSuperTypes(List<String> subTypes, Set<String> superTypes, AtlasTypeRegistry typeRegistry)
throws AtlasBaseException {
if (subTypes.contains(entityDef.getName())) {
throw new AtlasBaseException(entityDef.getName()
+ ": invalid supertypes - circular reference back to self " + subTypes);
}
if (CollectionUtils.isNotEmpty(entityDef.getSuperTypes())) {
superTypes.addAll(entityDef.getSuperTypes());
subTypes.add(entityDef.getName());
for (String superTypeName : entityDef.getSuperTypes()) {
AtlasType type = typeRegistry.getType(superTypeName);
if (type instanceof AtlasEntityType) {
AtlasEntityType superType = (AtlasEntityType) type;
superType.collectAllSuperTypes(subTypes, superTypes, typeRegistry);
}
}
subTypes.remove(entityDef.getName());
}
}
} }
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.type;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.typedef.AtlasClassificationDef;
import org.apache.atlas.model.typedef.AtlasEntityDef;
import org.apache.atlas.model.typedef.AtlasTypesDef;
import org.apache.atlas.type.AtlasTypeRegistry.AtlasTransientTypeRegistry;
import org.testng.annotations.Test;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
public class TestAtlasTypeRegistry {
/*
* L0
* / \
* / \
* L1_1---- L1_2
* / \ \ / \
* / \ \ / \
* L2_1 L2_2 L2_3 L2_4
*/
@Test
public void testClassificationDefValidSuperTypes() {
AtlasClassificationDef classifiL0 = new AtlasClassificationDef("L0");
AtlasClassificationDef classifiL1_1 = new AtlasClassificationDef("L1-1");
AtlasClassificationDef classifiL1_2 = new AtlasClassificationDef("L1-2");
AtlasClassificationDef classifiL2_1 = new AtlasClassificationDef("L2-1");
AtlasClassificationDef classifiL2_2 = new AtlasClassificationDef("L2-2");
AtlasClassificationDef classifiL2_3 = new AtlasClassificationDef("L2-3");
AtlasClassificationDef classifiL2_4 = new AtlasClassificationDef("L2-4");
classifiL1_1.addSuperType(classifiL0.getName());
classifiL1_2.addSuperType(classifiL0.getName());
classifiL2_1.addSuperType(classifiL1_1.getName());
classifiL2_2.addSuperType(classifiL1_1.getName());
classifiL2_3.addSuperType(classifiL1_1.getName());
classifiL2_3.addSuperType(classifiL1_2.getName());
classifiL2_4.addSuperType(classifiL1_2.getName());
AtlasTypesDef typesDef = new AtlasTypesDef();
typesDef.getClassificationDefs().add(classifiL0);
typesDef.getClassificationDefs().add(classifiL1_1);
typesDef.getClassificationDefs().add(classifiL1_2);
typesDef.getClassificationDefs().add(classifiL2_1);
typesDef.getClassificationDefs().add(classifiL2_2);
typesDef.getClassificationDefs().add(classifiL2_3);
typesDef.getClassificationDefs().add(classifiL2_4);
AtlasTypeRegistry typeRegistry = new AtlasTypeRegistry();
AtlasTransientTypeRegistry ttr = typeRegistry.createTransientTypeRegistry();
String failureMsg = null;
try {
ttr.addTypes(typesDef);
} catch (AtlasBaseException excp) {
failureMsg = excp.getMessage();
}
assertNull(failureMsg);
}
@Test
public void testClassificationDefInvalidSuperTypes_Self() {
AtlasClassificationDef classifiDef1 = new AtlasClassificationDef("classifiDef-1");
classifiDef1.addSuperType(classifiDef1.getName());
AtlasTypeRegistry typeRegistry = new AtlasTypeRegistry();
AtlasTransientTypeRegistry ttr = typeRegistry.createTransientTypeRegistry();
String failureMsg = null;
try {
ttr.addType(classifiDef1);
} catch (AtlasBaseException excp) {
failureMsg = excp.getMessage();
}
assertNotNull(failureMsg, "expected invalid supertype failure");
}
/*
* L2_3
* \
* L0
* / \
* / \
* L1_1---- L1_2
* / \ \ / \
* / \ \ / \
* L2_1 L2_2 L2_3 L2_4
*/
@Test
public void testClassificationDefInvalidSuperTypes_CircularRef() {
AtlasClassificationDef classifiL0 = new AtlasClassificationDef("L0");
AtlasClassificationDef classifiL1_1 = new AtlasClassificationDef("L1-1");
AtlasClassificationDef classifiL1_2 = new AtlasClassificationDef("L1-2");
AtlasClassificationDef classifiL2_1 = new AtlasClassificationDef("L2-1");
AtlasClassificationDef classifiL2_2 = new AtlasClassificationDef("L2-2");
AtlasClassificationDef classifiL2_3 = new AtlasClassificationDef("L2-3");
AtlasClassificationDef classifiL2_4 = new AtlasClassificationDef("L2-4");
classifiL1_1.addSuperType(classifiL0.getName());
classifiL1_2.addSuperType(classifiL0.getName());
classifiL2_1.addSuperType(classifiL1_1.getName());
classifiL2_2.addSuperType(classifiL1_1.getName());
classifiL2_3.addSuperType(classifiL1_1.getName());
classifiL2_3.addSuperType(classifiL1_2.getName());
classifiL2_4.addSuperType(classifiL1_2.getName());
classifiL0.addSuperType(classifiL2_3.getName()); // circular-ref
AtlasTypesDef typesDef = new AtlasTypesDef();
typesDef.getClassificationDefs().add(classifiL0);
typesDef.getClassificationDefs().add(classifiL1_1);
typesDef.getClassificationDefs().add(classifiL1_2);
typesDef.getClassificationDefs().add(classifiL2_1);
typesDef.getClassificationDefs().add(classifiL2_2);
typesDef.getClassificationDefs().add(classifiL2_3);
typesDef.getClassificationDefs().add(classifiL2_4);
AtlasTypeRegistry typeRegistry = new AtlasTypeRegistry();
AtlasTransientTypeRegistry ttr = typeRegistry.createTransientTypeRegistry();
String failureMsg = null;
try {
ttr.addTypes(typesDef);
} catch (AtlasBaseException excp) {
failureMsg = excp.getMessage();
}
assertNotNull(failureMsg, "expected invalid supertype failure");
}
/*
* L0
* / \
* / \
* L1_1---- L1_2
* / \ \ / \
* / \ \ / \
* L2_1 L2_2 L2_3 L2_4
*/
@Test
public void testEntityDefValidSuperTypes() {
AtlasEntityDef entL0 = new AtlasEntityDef("L0");
AtlasEntityDef entL1_1 = new AtlasEntityDef("L1-1");
AtlasEntityDef entL1_2 = new AtlasEntityDef("L1-2");
AtlasEntityDef entL2_1 = new AtlasEntityDef("L2-1");
AtlasEntityDef entL2_2 = new AtlasEntityDef("L2-2");
AtlasEntityDef entL2_3 = new AtlasEntityDef("L2-3");
AtlasEntityDef entL2_4 = new AtlasEntityDef("L2-4");
entL1_1.addSuperType(entL0.getName());
entL1_2.addSuperType(entL0.getName());
entL2_1.addSuperType(entL1_1.getName());
entL2_2.addSuperType(entL1_1.getName());
entL2_3.addSuperType(entL1_1.getName());
entL2_3.addSuperType(entL1_2.getName());
entL2_4.addSuperType(entL1_2.getName());
AtlasTypesDef typesDef = new AtlasTypesDef();
typesDef.getEntityDefs().add(entL0);
typesDef.getEntityDefs().add(entL1_1);
typesDef.getEntityDefs().add(entL1_2);
typesDef.getEntityDefs().add(entL2_1);
typesDef.getEntityDefs().add(entL2_2);
typesDef.getEntityDefs().add(entL2_3);
typesDef.getEntityDefs().add(entL2_4);
AtlasTypeRegistry typeRegistry = new AtlasTypeRegistry();
AtlasTransientTypeRegistry ttr = typeRegistry.createTransientTypeRegistry();
String failureMsg = null;
try {
ttr.addTypes(typesDef);
} catch (AtlasBaseException excp) {
failureMsg = excp.getMessage();
}
assertNull(failureMsg);
}
@Test
public void testEntityDefInvalidSuperTypes_Self() {
AtlasEntityDef entDef1 = new AtlasEntityDef("entDef-1");
entDef1.addSuperType(entDef1.getName());
AtlasTypeRegistry typeRegistry = new AtlasTypeRegistry();
AtlasTransientTypeRegistry ttr = typeRegistry.createTransientTypeRegistry();
String failureMsg = null;
try {
ttr.addType(entDef1);
} catch (AtlasBaseException excp) {
failureMsg = excp.getMessage();
}
assertNotNull(failureMsg, "expected invalid supertype failure");
}
/*
* L2_3
* \
* L0
* / \
* / \
* L1_1---- L1_2
* / \ \ / \
* / \ \ / \
* L2_1 L2_2 L2_3 L2_4
*/
@Test
public void testEntityDefInvalidSuperTypes_CircularRef() {
AtlasEntityDef entL0 = new AtlasEntityDef("L0");
AtlasEntityDef entL1_1 = new AtlasEntityDef("L1-1");
AtlasEntityDef entL1_2 = new AtlasEntityDef("L1-2");
AtlasEntityDef entL2_1 = new AtlasEntityDef("L2-1");
AtlasEntityDef entL2_2 = new AtlasEntityDef("L2-2");
AtlasEntityDef entL2_3 = new AtlasEntityDef("L2-3");
AtlasEntityDef entL2_4 = new AtlasEntityDef("L2-4");
entL1_1.addSuperType(entL0.getName());
entL1_2.addSuperType(entL0.getName());
entL2_1.addSuperType(entL1_1.getName());
entL2_2.addSuperType(entL1_1.getName());
entL2_3.addSuperType(entL1_1.getName());
entL2_3.addSuperType(entL1_2.getName());
entL2_4.addSuperType(entL1_2.getName());
entL0.addSuperType(entL2_3.getName()); // circular-ref
AtlasTypesDef typesDef = new AtlasTypesDef();
typesDef.getEntityDefs().add(entL0);
typesDef.getEntityDefs().add(entL1_1);
typesDef.getEntityDefs().add(entL1_2);
typesDef.getEntityDefs().add(entL2_1);
typesDef.getEntityDefs().add(entL2_2);
typesDef.getEntityDefs().add(entL2_3);
typesDef.getEntityDefs().add(entL2_4);
AtlasTypeRegistry typeRegistry = new AtlasTypeRegistry();
AtlasTransientTypeRegistry ttr = typeRegistry.createTransientTypeRegistry();
String failureMsg = null;
try {
ttr.addTypes(typesDef);
} catch (AtlasBaseException excp) {
failureMsg = excp.getMessage();
}
assertNotNull(failureMsg, "expected invalid supertype failure");
}
}
...@@ -173,8 +173,8 @@ public class AtlasClassificationDefStoreV1 extends AtlasAbstractDefStoreV1 imple ...@@ -173,8 +173,8 @@ public class AtlasClassificationDefStoreV1 extends AtlasAbstractDefStoreV1 imple
LOG.debug("==> AtlasClassificationDefStoreV1.update({})", classifiDef); LOG.debug("==> AtlasClassificationDefStoreV1.update({})", classifiDef);
} }
AtlasClassificationDef ret = StringUtils.isNotBlank(classifiDef.getName()) AtlasClassificationDef ret = StringUtils.isNotBlank(classifiDef.getGuid())
? updateByName(classifiDef.getName(), classifiDef) : updateByGuid(classifiDef.getGuid(), classifiDef); ? updateByGuid(classifiDef.getGuid(), classifiDef) : updateByName(classifiDef.getName(), classifiDef);
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
LOG.debug("<== AtlasClassificationDefStoreV1.update({}): {}", classifiDef, ret); LOG.debug("<== AtlasClassificationDefStoreV1.update({}): {}", classifiDef, ret);
...@@ -360,13 +360,13 @@ public class AtlasClassificationDefStoreV1 extends AtlasAbstractDefStoreV1 imple ...@@ -360,13 +360,13 @@ public class AtlasClassificationDefStoreV1 extends AtlasAbstractDefStoreV1 imple
private void updateVertexPreCreate(AtlasClassificationDef classificationDef, private void updateVertexPreCreate(AtlasClassificationDef classificationDef,
AtlasClassificationType classificationType, AtlasClassificationType classificationType,
AtlasVertex vertex) { AtlasVertex vertex) {
AtlasStructDefStoreV1.updateVertexPreCreate(classificationDef, classificationType, vertex); AtlasStructDefStoreV1.updateVertexPreCreate(classificationDef, classificationType, vertex, typeDefStore);
} }
private void updateVertexPreUpdate(AtlasClassificationDef classificationDef, private void updateVertexPreUpdate(AtlasClassificationDef classificationDef,
AtlasClassificationType classificationType, AtlasClassificationType classificationType,
AtlasVertex vertex) { AtlasVertex vertex) throws AtlasBaseException {
AtlasStructDefStoreV1.updateVertexPreUpdate(classificationDef, classificationType, vertex); AtlasStructDefStoreV1.updateVertexPreUpdate(classificationDef, classificationType, vertex, typeDefStore);
} }
private void updateVertexAddReferences(AtlasClassificationDef classificationDef, AtlasVertex vertex) throws AtlasBaseException { private void updateVertexAddReferences(AtlasClassificationDef classificationDef, AtlasVertex vertex) throws AtlasBaseException {
......
...@@ -172,8 +172,8 @@ public class AtlasEntityDefStoreV1 extends AtlasAbstractDefStoreV1 implements At ...@@ -172,8 +172,8 @@ public class AtlasEntityDefStoreV1 extends AtlasAbstractDefStoreV1 implements At
LOG.debug("==> AtlasEntityDefStoreV1.update({})", entityDef); LOG.debug("==> AtlasEntityDefStoreV1.update({})", entityDef);
} }
AtlasEntityDef ret = StringUtils.isNotBlank(entityDef.getName()) ? updateByName(entityDef.getName(), entityDef) AtlasEntityDef ret = StringUtils.isNotBlank(entityDef.getGuid()) ? updateByGuid(entityDef.getGuid(), entityDef)
: updateByGuid(entityDef.getGuid(), entityDef); : updateByName(entityDef.getName(), entityDef);
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
LOG.debug("<== AtlasEntityDefStoreV1.update({}): {}", entityDef, ret); LOG.debug("<== AtlasEntityDefStoreV1.update({}): {}", entityDef, ret);
...@@ -356,11 +356,12 @@ public class AtlasEntityDefStoreV1 extends AtlasAbstractDefStoreV1 implements At ...@@ -356,11 +356,12 @@ public class AtlasEntityDefStoreV1 extends AtlasAbstractDefStoreV1 implements At
} }
private void updateVertexPreCreate(AtlasEntityDef entityDef, AtlasEntityType entityType, AtlasVertex vertex) { private void updateVertexPreCreate(AtlasEntityDef entityDef, AtlasEntityType entityType, AtlasVertex vertex) {
AtlasStructDefStoreV1.updateVertexPreCreate(entityDef, entityType, vertex); AtlasStructDefStoreV1.updateVertexPreCreate(entityDef, entityType, vertex, typeDefStore);
} }
private void updateVertexPreUpdate(AtlasEntityDef entityDef, AtlasEntityType entityType, AtlasVertex vertex) { private void updateVertexPreUpdate(AtlasEntityDef entityDef, AtlasEntityType entityType, AtlasVertex vertex)
AtlasStructDefStoreV1.updateVertexPreUpdate(entityDef, entityType, vertex); throws AtlasBaseException {
AtlasStructDefStoreV1.updateVertexPreUpdate(entityDef, entityType, vertex, typeDefStore);
} }
private void updateVertexAddReferences(AtlasEntityDef entityDef, AtlasVertex vertex) throws AtlasBaseException { private void updateVertexAddReferences(AtlasEntityDef entityDef, AtlasVertex vertex) throws AtlasBaseException {
......
...@@ -81,7 +81,7 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At ...@@ -81,7 +81,7 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At
ret = typeDefStore.createTypeVertex(structDef); ret = typeDefStore.createTypeVertex(structDef);
AtlasStructDefStoreV1.updateVertexPreCreate(structDef, (AtlasStructType)type, ret); AtlasStructDefStoreV1.updateVertexPreCreate(structDef, (AtlasStructType)type, ret, typeDefStore);
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
LOG.debug("<== AtlasStructDefStoreV1.preCreate({}): {}", structDef, ret); LOG.debug("<== AtlasStructDefStoreV1.preCreate({}): {}", structDef, ret);
...@@ -184,8 +184,8 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At ...@@ -184,8 +184,8 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At
LOG.debug("==> AtlasStructDefStoreV1.update({})", structDef); LOG.debug("==> AtlasStructDefStoreV1.update({})", structDef);
} }
AtlasStructDef ret = StringUtils.isNotBlank(structDef.getName()) ? updateByName(structDef.getName(), structDef) AtlasStructDef ret = StringUtils.isNotBlank(structDef.getGuid()) ? updateByGuid(structDef.getGuid(), structDef)
: updateByGuid(structDef.getGuid(), structDef); : updateByName(structDef.getName(), structDef);
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
LOG.debug("<== AtlasStructDefStoreV1.update({}): {}", structDef, ret); LOG.debug("<== AtlasStructDefStoreV1.update({}): {}", structDef, ret);
...@@ -212,7 +212,7 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At ...@@ -212,7 +212,7 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At
throw new AtlasBaseException("no structDef exists with name " + name); throw new AtlasBaseException("no structDef exists with name " + name);
} }
AtlasStructDefStoreV1.updateVertexPreUpdate(structDef, (AtlasStructType)type, vertex); AtlasStructDefStoreV1.updateVertexPreUpdate(structDef, (AtlasStructType)type, vertex, typeDefStore);
AtlasStructDefStoreV1.updateVertexAddReferences(structDef, vertex, typeDefStore); AtlasStructDefStoreV1.updateVertexAddReferences(structDef, vertex, typeDefStore);
AtlasStructDef ret = toStructDef(vertex); AtlasStructDef ret = toStructDef(vertex);
...@@ -242,7 +242,7 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At ...@@ -242,7 +242,7 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At
throw new AtlasBaseException("no structDef exists with guid " + guid); throw new AtlasBaseException("no structDef exists with guid " + guid);
} }
AtlasStructDefStoreV1.updateVertexPreUpdate(structDef, (AtlasStructType)type, vertex); AtlasStructDefStoreV1.updateVertexPreUpdate(structDef, (AtlasStructType)type, vertex, typeDefStore);
AtlasStructDefStoreV1.updateVertexAddReferences(structDef, vertex, typeDefStore); AtlasStructDefStoreV1.updateVertexAddReferences(structDef, vertex, typeDefStore);
AtlasStructDef ret = toStructDef(vertex); AtlasStructDef ret = toStructDef(vertex);
...@@ -377,7 +377,8 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At ...@@ -377,7 +377,8 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At
return ret; return ret;
} }
public static void updateVertexPreCreate(AtlasStructDef structDef, AtlasStructType structType, AtlasVertex vertex) { public static void updateVertexPreCreate(AtlasStructDef structDef, AtlasStructType structType,
AtlasVertex vertex, AtlasTypeDefGraphStoreV1 typeDefStore) {
List<String> attrNames = new ArrayList<>(structDef.getAttributeDefs().size()); List<String> attrNames = new ArrayList<>(structDef.getAttributeDefs().size());
for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) { for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
...@@ -390,8 +391,49 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At ...@@ -390,8 +391,49 @@ public class AtlasStructDefStoreV1 extends AtlasAbstractDefStoreV1 implements At
AtlasGraphUtilsV1.setProperty(vertex, AtlasGraphUtilsV1.getPropertyKey(structDef), attrNames); AtlasGraphUtilsV1.setProperty(vertex, AtlasGraphUtilsV1.getPropertyKey(structDef), attrNames);
} }
public static void updateVertexPreUpdate(AtlasStructDef structDef, AtlasStructType structType, AtlasVertex vertex) { public static void updateVertexPreUpdate(AtlasStructDef structDef, AtlasStructType structType,
AtlasStructDefStoreV1.updateVertexPreCreate(structDef, structType, vertex); AtlasVertex vertex, AtlasTypeDefGraphStoreV1 typeDefStore)
throws AtlasBaseException {
List<String> attrNames = new ArrayList<>();
if (CollectionUtils.isNotEmpty(structDef.getAttributeDefs())) {
for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
attrNames.add(attributeDef.getName());
}
}
List<String> currAttrNames = vertex.getProperty(AtlasGraphUtilsV1.getPropertyKey(structDef), List.class);
// delete attributes that are not present in updated structDef
if (CollectionUtils.isNotEmpty(currAttrNames)) {
for (String currAttrName : currAttrNames) {
if (!attrNames.contains(currAttrName)) {
throw new AtlasBaseException(structDef.getName() + "." + currAttrName +
": attribute delete not supported");
}
}
}
typeDefStore.updateTypeVertex(structDef, vertex);
// add/update attributes that are present in updated structDef
if (CollectionUtils.isNotEmpty(structDef.getAttributeDefs())) {
for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
if (CollectionUtils.isEmpty(currAttrNames) || !currAttrNames.contains(attributeDef.getName())) {
// new attribute - only allow if optional
if (!attributeDef.isOptional()) {
throw new AtlasBaseException(structDef.getName() + "." + attributeDef.getName()
+ ": can not add mandatory attribute");
}
}
String propertyKey = AtlasGraphUtilsV1.getPropertyKey(structDef, attributeDef.getName());
AtlasGraphUtilsV1.setProperty(vertex, propertyKey, toJsonFromAttributeDef(attributeDef, structType));
}
}
AtlasGraphUtilsV1.setProperty(vertex, AtlasGraphUtilsV1.getPropertyKey(structDef), attrNames);
} }
public static void updateVertexAddReferences(AtlasStructDef structDef, AtlasVertex vertex, public static void updateVertexAddReferences(AtlasStructDef structDef, AtlasVertex vertex,
......
...@@ -162,8 +162,6 @@ public class AtlasTypeDefGraphStoreV1 extends AtlasTypeDefGraphStore { ...@@ -162,8 +162,6 @@ public class AtlasTypeDefGraphStoreV1 extends AtlasTypeDefGraphStore {
public AtlasVertex createTypeVertex(AtlasBaseTypeDef typeDef) { public AtlasVertex createTypeVertex(AtlasBaseTypeDef typeDef) {
// Validate all the required checks // Validate all the required checks
Preconditions.checkArgument(StringUtils.isNotBlank(typeDef.getName()), "Type name can't be null/empty"); Preconditions.checkArgument(StringUtils.isNotBlank(typeDef.getName()), "Type name can't be null/empty");
Preconditions.checkArgument(StringUtils.isNotBlank(typeDef.getTypeVersion()), "Type version can't be null/empty");
Preconditions.checkArgument(typeDef.getVersion() != null, "Version can't be null");
AtlasVertex ret = atlasGraph.addVertex(); AtlasVertex ret = atlasGraph.addVertex();
...@@ -171,6 +169,10 @@ public class AtlasTypeDefGraphStoreV1 extends AtlasTypeDefGraphStore { ...@@ -171,6 +169,10 @@ public class AtlasTypeDefGraphStoreV1 extends AtlasTypeDefGraphStore {
typeDef.setTypeVersion("1.0"); typeDef.setTypeVersion("1.0");
} }
if (typeDef.getVersion() == null) {
typeDef.setVersion(Long.valueOf(1l));
}
if (StringUtils.isBlank(typeDef.getGuid())) { if (StringUtils.isBlank(typeDef.getGuid())) {
typeDef.setGuid(UUID.randomUUID().toString()); typeDef.setGuid(UUID.randomUUID().toString());
} }
...@@ -198,6 +200,26 @@ public class AtlasTypeDefGraphStoreV1 extends AtlasTypeDefGraphStore { ...@@ -198,6 +200,26 @@ public class AtlasTypeDefGraphStoreV1 extends AtlasTypeDefGraphStore {
return ret; return ret;
} }
public void updateTypeVertex(AtlasBaseTypeDef typeDef, AtlasVertex vertex) {
if (!isTypeVertex(vertex)) {
LOG.warn("updateTypeVertex(): not a type-vertex - {}", vertex);
return;
}
updateVertexProperty(vertex, Constants.GUID_PROPERTY_KEY, typeDef.getGuid());
/*
* rename of a type is supported yet - as the typename is used to in the name of the edges from this vertex
* To support rename of types, he edge names should be derived from an internal-name - not directly the typename
*
updateVertexProperty(vertex, Constants.TYPENAME_PROPERTY_KEY, typeDef.getName());
*/
updateVertexProperty(vertex, Constants.TYPEDESCRIPTION_PROPERTY_KEY, typeDef.getDescription());
updateVertexProperty(vertex, Constants.TYPEVERSION_PROPERTY_KEY, typeDef.getTypeVersion());
markVertexUpdated(vertex);
}
public void deleteTypeVertexOutEdges(AtlasVertex vertex) throws AtlasBaseException { public void deleteTypeVertexOutEdges(AtlasVertex vertex) throws AtlasBaseException {
Iterable<AtlasEdge> edges = vertex.getEdges(AtlasEdgeDirection.OUT); Iterable<AtlasEdge> edges = vertex.getEdges(AtlasEdgeDirection.OUT);
...@@ -311,15 +333,23 @@ public class AtlasTypeDefGraphStoreV1 extends AtlasTypeDefGraphStore { ...@@ -311,15 +333,23 @@ public class AtlasTypeDefGraphStoreV1 extends AtlasTypeDefGraphStore {
return ret; return ret;
} }
public void createSuperTypeEdges(AtlasVertex vertex, Set<String> superTypes, TypeCategory typeCategory) { public void createSuperTypeEdges(AtlasVertex vertex, Set<String> superTypes, TypeCategory typeCategory)
throws AtlasBaseException {
Set<String> currentSuperTypes = getSuperTypeNames(vertex);
if (CollectionUtils.isNotEmpty(superTypes)) { if (CollectionUtils.isNotEmpty(superTypes)) {
if (! superTypes.containsAll(currentSuperTypes)) {
throw new AtlasBaseException("superType remove not supported");
}
for (String superType : superTypes) { for (String superType : superTypes) {
AtlasVertex superTypeVertex = findTypeVertexByNameAndCategory(superType, typeCategory); AtlasVertex superTypeVertex = findTypeVertexByNameAndCategory(superType, typeCategory);
getOrCreateEdge(vertex, superTypeVertex, AtlasGraphUtilsV1.SUPERTYPE_EDGE_LABEL); getOrCreateEdge(vertex, superTypeVertex, AtlasGraphUtilsV1.SUPERTYPE_EDGE_LABEL);
} }
} else if (CollectionUtils.isNotEmpty(currentSuperTypes)) {
throw new AtlasBaseException("superType remove not supported");
} }
// TODO: remove any other superType edges, if any exists
} }
public Set<String> getSuperTypeNames(AtlasVertex vertex) { public Set<String> getSuperTypeNames(AtlasVertex vertex) {
...@@ -348,4 +378,42 @@ public class AtlasTypeDefGraphStoreV1 extends AtlasTypeDefGraphStore { ...@@ -348,4 +378,42 @@ public class AtlasTypeDefGraphStoreV1 extends AtlasTypeDefGraphStore {
return ret; return ret;
} }
/*
* update the given vertex property, if the new value is not-blank
*/
private void updateVertexProperty(AtlasVertex vertex, String propertyName, String newValue) {
if (StringUtils.isNotBlank(newValue)) {
String currValue = vertex.getProperty(propertyName, String.class);
if (!StringUtils.equals(currValue, newValue)) {
vertex.setProperty(propertyName, newValue);
}
}
}
/*
* update the given vertex property, if the new value is not-null
*/
private void updateVertexProperty(AtlasVertex vertex, String propertyName, Date newValue) {
if (newValue != null) {
Number currValue = vertex.getProperty(propertyName, Number.class);
if (currValue == null || !currValue.equals(newValue.getTime())) {
vertex.setProperty(propertyName, newValue.getTime());
}
}
}
/*
* increment the version value for this vertex
*/
private void markVertexUpdated(AtlasVertex vertex) {
Date now = new Date();
Number currVersion = vertex.getProperty(Constants.VERSION_PROPERTY_KEY, Number.class);
long newVersion = currVersion == null ? 1 : (currVersion.longValue() + 1);
vertex.setProperty(Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, now.getTime());
vertex.setProperty(Constants.VERSION_PROPERTY_KEY, newVersion);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment