Commit 9c45d2e8 by Madhan Neethiraj

ATLAS-2836: Remove redundant encoding of vertex property keys - #2

parent 0ed9f0e2
...@@ -646,83 +646,6 @@ public final class GraphHelper { ...@@ -646,83 +646,6 @@ public final class GraphHelper {
+ edge.getInVertex() + "]"; + edge.getInVertex() + "]";
} }
public static <T extends AtlasElement> void setProperty(T element, String propertyName, Object value) {
String actualPropertyName = AtlasGraphUtilsV2.encodePropertyKey(propertyName);
String elementStr = null;
if (LOG.isDebugEnabled()) {
elementStr = string(element);
LOG.debug("Setting property {} = \"{}\" to {}", actualPropertyName, value, elementStr);
}
Object existValue = element.getProperty(actualPropertyName, Object.class);
if(value == null || (value instanceof Collection && ((Collection) value).isEmpty())) {
if(existValue != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Removing property - {} value from {}", actualPropertyName, elementStr);
}
element.removeProperty(actualPropertyName);
}
} else {
if (!value.equals(existValue)) {
element.setProperty(actualPropertyName, value);
if (LOG.isDebugEnabled()) {
LOG.debug("Set property {} = \"{}\" to {}", actualPropertyName, value, elementStr);
}
}
}
}
/**
* Gets the value of a property that is stored in the graph as a single property value. If
* a multi-property such as {@link Constants#TRAIT_NAMES_PROPERTY_KEY} or {@link Constants#SUPER_TYPES_PROPERTY_KEY}
* is used, an exception will be thrown.
*
* @param element
* @param propertyName
* @param clazz
* @return
*/
public static <T> T getSingleValuedProperty(AtlasElement element, String propertyName, Class<T> clazz) {
String actualPropertyName = AtlasGraphUtilsV2.encodePropertyKey(propertyName);
if (LOG.isTraceEnabled()) {
LOG.trace("Reading property {} from {}", actualPropertyName, string(element));
}
return element.getProperty(actualPropertyName, clazz);
}
public static Object getProperty(AtlasVertex<?,?> vertex, String propertyName) {
String actualPropertyName = AtlasGraphUtilsV2.encodePropertyKey(propertyName);
if (LOG.isTraceEnabled()) {
LOG.trace("Reading property {} from {}", actualPropertyName, string(vertex));
}
if(AtlasGraphProvider.getGraphInstance().isMultiProperty(actualPropertyName)) {
return vertex.getPropertyValues(actualPropertyName, String.class);
}
else {
return vertex.getProperty(actualPropertyName, Object.class);
}
}
public static Object getProperty(AtlasEdge<?,?> edge, String propertyName) {
String actualPropertyName = AtlasGraphUtilsV2.encodePropertyKey(propertyName);
if (LOG.isTraceEnabled()) {
LOG.trace("Reading property {} from {}", actualPropertyName, string(edge));
}
return edge.getProperty(actualPropertyName, Object.class);
}
private static <T extends AtlasElement> String string(T element) { private static <T extends AtlasElement> String string(T element) {
if (element instanceof AtlasVertex) { if (element instanceof AtlasVertex) {
return string((AtlasVertex) element); return string((AtlasVertex) element);
...@@ -732,23 +655,6 @@ public final class GraphHelper { ...@@ -732,23 +655,6 @@ public final class GraphHelper {
return element.toString(); return element.toString();
} }
/**
* Adds an additional value to a multi-property (SET).
*
* @param vertex
* @param propertyName
* @param value
*/
public static void addProperty(AtlasVertex vertex, String propertyName, Object value) {
String actualPropertyName = AtlasGraphUtilsV2.encodePropertyKey(propertyName);
if (LOG.isDebugEnabled()) {
LOG.debug("Adding property {} = \"{}\" to vertex {}", actualPropertyName, value, string(vertex));
}
vertex.addProperty(actualPropertyName, value);
}
public static void addToPropagatedTraitNames(AtlasVertex entityVertex, String classificationName) { public static void addToPropagatedTraitNames(AtlasVertex entityVertex, String classificationName) {
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
LOG.debug("Adding property {} = \"{}\" to vertex {}", PROPAGATED_TRAIT_NAMES_PROPERTY_KEY, classificationName, string(entityVertex)); LOG.debug("Adding property {} = \"{}\" to vertex {}", PROPAGATED_TRAIT_NAMES_PROPERTY_KEY, classificationName, string(entityVertex));
...@@ -757,20 +663,6 @@ public final class GraphHelper { ...@@ -757,20 +663,6 @@ public final class GraphHelper {
entityVertex.addListProperty(PROPAGATED_TRAIT_NAMES_PROPERTY_KEY, classificationName); entityVertex.addListProperty(PROPAGATED_TRAIT_NAMES_PROPERTY_KEY, classificationName);
} }
public static void removeFromPropagatedTraitNames(AtlasVertex entityVertex, String classificationName) {
if (entityVertex != null && StringUtils.isNotEmpty(classificationName)) {
List<String> propagatedTraitNames = getTraitNames(entityVertex, true);
propagatedTraitNames.remove(classificationName);
entityVertex.removeProperty(PROPAGATED_TRAIT_NAMES_PROPERTY_KEY);
for (String propagatedTraitName : propagatedTraitNames) {
addToPropagatedTraitNames(entityVertex, propagatedTraitName);
}
}
}
/** /**
* Remove the specified edge from the graph. * Remove the specified edge from the graph.
* *
......
...@@ -928,7 +928,7 @@ public class EntityGraphMapper { ...@@ -928,7 +928,7 @@ public class EntityGraphMapper {
Object element = newElementsCreated.get(index); Object element = newElementsCreated.get(index);
if (element instanceof AtlasEdge) { if (element instanceof AtlasEdge) {
AtlasGraphUtilsV2.setProperty((AtlasEdge) element, ATTRIBUTE_INDEX_PROPERTY_KEY, index); AtlasGraphUtilsV2.setEncodedProperty((AtlasEdge) element, ATTRIBUTE_INDEX_PROPERTY_KEY, index);
} }
} }
......
...@@ -597,7 +597,7 @@ public final class EntityGraphRetriever { ...@@ -597,7 +597,7 @@ public final class EntityGraphRetriever {
ret.setRelationGuid(relationGuid); ret.setRelationGuid(relationGuid);
} }
Object displayName = GraphHelper.getProperty(termVertex, GLOSSARY_TERM_DISPLAY_NAME_ATTR); Object displayName = AtlasGraphUtilsV2.getEncodedProperty(termVertex, GLOSSARY_TERM_DISPLAY_NAME_ATTR, Object.class);
if (displayName instanceof String) { if (displayName instanceof String) {
ret.setDisplayText((String) displayName); ret.setDisplayText((String) displayName);
} }
...@@ -660,8 +660,7 @@ public final class EntityGraphRetriever { ...@@ -660,8 +660,7 @@ public final class EntityGraphRetriever {
private Object mapVertexToAttribute(AtlasVertex entityVertex, AtlasAttribute attribute, AtlasEntityExtInfo entityExtInfo, final boolean isMinExtInfo) throws AtlasBaseException { private Object mapVertexToAttribute(AtlasVertex entityVertex, AtlasAttribute attribute, AtlasEntityExtInfo entityExtInfo, final boolean isMinExtInfo) throws AtlasBaseException {
Object ret = null; Object ret = null;
AtlasType attrType = attribute.getAttributeType(); AtlasType attrType = attribute.getAttributeType();
String vertexPropertyName = attribute.getQualifiedName(); String edgeLabel = EDGE_LABEL_PREFIX + attribute.getQualifiedName();
String edgeLabel = EDGE_LABEL_PREFIX + vertexPropertyName;
boolean isOwnedAttribute = attribute.isOwnedRef(); boolean isOwnedAttribute = attribute.isOwnedRef();
AtlasRelationshipEdgeDirection edgeDirection = attribute.getRelationshipEdgeDirection(); AtlasRelationshipEdgeDirection edgeDirection = attribute.getRelationshipEdgeDirection();
...@@ -671,10 +670,10 @@ public final class EntityGraphRetriever { ...@@ -671,10 +670,10 @@ public final class EntityGraphRetriever {
switch (attrType.getTypeCategory()) { switch (attrType.getTypeCategory()) {
case PRIMITIVE: case PRIMITIVE:
ret = mapVertexToPrimitive(entityVertex, vertexPropertyName, attribute.getAttributeDef()); ret = mapVertexToPrimitive(entityVertex, attribute.getVertexPropertyName(), attribute.getAttributeDef());
break; break;
case ENUM: case ENUM:
ret = GraphHelper.getProperty(entityVertex, vertexPropertyName); ret = AtlasGraphUtilsV2.getEncodedProperty(entityVertex, attribute.getVertexPropertyName(), Object.class);
break; break;
case STRUCT: case STRUCT:
ret = mapVertexToStruct(entityVertex, edgeLabel, null, entityExtInfo, isMinExtInfo); ret = mapVertexToStruct(entityVertex, edgeLabel, null, entityExtInfo, isMinExtInfo);
...@@ -686,7 +685,7 @@ public final class EntityGraphRetriever { ...@@ -686,7 +685,7 @@ public final class EntityGraphRetriever {
ret = mapVertexToArray(entityVertex, entityExtInfo, isOwnedAttribute, attribute, isMinExtInfo); ret = mapVertexToArray(entityVertex, entityExtInfo, isOwnedAttribute, attribute, isMinExtInfo);
break; break;
case MAP: case MAP:
ret = mapVertexToMap(entityVertex, vertexPropertyName, entityExtInfo, isOwnedAttribute, attribute, isMinExtInfo); ret = mapVertexToMap(entityVertex, entityExtInfo, isOwnedAttribute, attribute, isMinExtInfo);
break; break;
case CLASSIFICATION: case CLASSIFICATION:
// do nothing // do nothing
...@@ -696,7 +695,7 @@ public final class EntityGraphRetriever { ...@@ -696,7 +695,7 @@ public final class EntityGraphRetriever {
return ret; return ret;
} }
private Map<String, Object> mapVertexToMap(AtlasVertex entityVertex, final String propertyName, AtlasEntityExtInfo entityExtInfo, private Map<String, Object> mapVertexToMap(AtlasVertex entityVertex, AtlasEntityExtInfo entityExtInfo,
boolean isOwnedAttribute, AtlasAttribute attribute, final boolean isMinExtInfo) throws AtlasBaseException { boolean isOwnedAttribute, AtlasAttribute attribute, final boolean isMinExtInfo) throws AtlasBaseException {
Map<String, Object> ret = null; Map<String, Object> ret = null;
...@@ -724,7 +723,7 @@ public final class EntityGraphRetriever { ...@@ -724,7 +723,7 @@ public final class EntityGraphRetriever {
} }
} }
} else { } else {
ret = getPrimitiveMap(entityVertex, propertyName); ret = getPrimitiveMap(entityVertex, attribute.getVertexPropertyName());
} }
if (MapUtils.isEmpty(ret)) { if (MapUtils.isEmpty(ret)) {
...@@ -806,43 +805,43 @@ public final class EntityGraphRetriever { ...@@ -806,43 +805,43 @@ public final class EntityGraphRetriever {
public Object mapVertexToPrimitive(AtlasElement entityVertex, final String vertexPropertyName, AtlasAttributeDef attrDef) { public Object mapVertexToPrimitive(AtlasElement entityVertex, final String vertexPropertyName, AtlasAttributeDef attrDef) {
Object ret = null; Object ret = null;
if (GraphHelper.getSingleValuedProperty(entityVertex, vertexPropertyName, Object.class) == null) { if (AtlasGraphUtilsV2.getEncodedProperty(entityVertex, vertexPropertyName, Object.class) == null) {
return null; return null;
} }
switch (attrDef.getTypeName().toLowerCase()) { switch (attrDef.getTypeName().toLowerCase()) {
case ATLAS_TYPE_STRING: case ATLAS_TYPE_STRING:
ret = GraphHelper.getSingleValuedProperty(entityVertex, vertexPropertyName, String.class); ret = AtlasGraphUtilsV2.getEncodedProperty(entityVertex, vertexPropertyName, String.class);
break; break;
case ATLAS_TYPE_SHORT: case ATLAS_TYPE_SHORT:
ret = GraphHelper.getSingleValuedProperty(entityVertex, vertexPropertyName, Short.class); ret = AtlasGraphUtilsV2.getEncodedProperty(entityVertex, vertexPropertyName, Short.class);
break; break;
case ATLAS_TYPE_INT: case ATLAS_TYPE_INT:
ret = GraphHelper.getSingleValuedProperty(entityVertex, vertexPropertyName, Integer.class); ret = AtlasGraphUtilsV2.getEncodedProperty(entityVertex, vertexPropertyName, Integer.class);
break; break;
case ATLAS_TYPE_BIGINTEGER: case ATLAS_TYPE_BIGINTEGER:
ret = GraphHelper.getSingleValuedProperty(entityVertex, vertexPropertyName, BigInteger.class); ret = AtlasGraphUtilsV2.getEncodedProperty(entityVertex, vertexPropertyName, BigInteger.class);
break; break;
case ATLAS_TYPE_BOOLEAN: case ATLAS_TYPE_BOOLEAN:
ret = GraphHelper.getSingleValuedProperty(entityVertex, vertexPropertyName, Boolean.class); ret = AtlasGraphUtilsV2.getEncodedProperty(entityVertex, vertexPropertyName, Boolean.class);
break; break;
case ATLAS_TYPE_BYTE: case ATLAS_TYPE_BYTE:
ret = GraphHelper.getSingleValuedProperty(entityVertex, vertexPropertyName, Byte.class); ret = AtlasGraphUtilsV2.getEncodedProperty(entityVertex, vertexPropertyName, Byte.class);
break; break;
case ATLAS_TYPE_LONG: case ATLAS_TYPE_LONG:
ret = GraphHelper.getSingleValuedProperty(entityVertex, vertexPropertyName, Long.class); ret = AtlasGraphUtilsV2.getEncodedProperty(entityVertex, vertexPropertyName, Long.class);
break; break;
case ATLAS_TYPE_FLOAT: case ATLAS_TYPE_FLOAT:
ret = GraphHelper.getSingleValuedProperty(entityVertex, vertexPropertyName, Float.class); ret = AtlasGraphUtilsV2.getEncodedProperty(entityVertex, vertexPropertyName, Float.class);
break; break;
case ATLAS_TYPE_DOUBLE: case ATLAS_TYPE_DOUBLE:
ret = GraphHelper.getSingleValuedProperty(entityVertex, vertexPropertyName, Double.class); ret = AtlasGraphUtilsV2.getEncodedProperty(entityVertex, vertexPropertyName, Double.class);
break; break;
case ATLAS_TYPE_BIGDECIMAL: case ATLAS_TYPE_BIGDECIMAL:
ret = GraphHelper.getSingleValuedProperty(entityVertex, vertexPropertyName, BigDecimal.class); ret = AtlasGraphUtilsV2.getEncodedProperty(entityVertex, vertexPropertyName, BigDecimal.class);
break; break;
case ATLAS_TYPE_DATE: case ATLAS_TYPE_DATE:
ret = new Date(GraphHelper.getSingleValuedProperty(entityVertex, vertexPropertyName, Long.class)); ret = new Date(AtlasGraphUtilsV2.getEncodedProperty(entityVertex, vertexPropertyName, Long.class));
break; break;
default: default:
break; break;
...@@ -1166,7 +1165,7 @@ public final class EntityGraphRetriever { ...@@ -1166,7 +1165,7 @@ public final class EntityGraphRetriever {
for (AtlasAttribute attribute : relationshipType.getAllAttributes().values()) { for (AtlasAttribute attribute : relationshipType.getAllAttributes().values()) {
// mapping only primitive attributes // mapping only primitive attributes
Object attrValue = mapVertexToPrimitive(edge, attribute.getQualifiedName(), attribute.getAttributeDef()); Object attrValue = mapVertexToPrimitive(edge, attribute.getVertexPropertyName(), attribute.getAttributeDef());
relationship.setAttribute(attribute.getName(), attrValue); relationship.setAttribute(attribute.getName(), attrValue);
} }
......
...@@ -25,6 +25,7 @@ import org.apache.atlas.repository.graph.GraphBackedSearchIndexer; ...@@ -25,6 +25,7 @@ import org.apache.atlas.repository.graph.GraphBackedSearchIndexer;
import org.apache.atlas.repository.graph.GraphHelper; import org.apache.atlas.repository.graph.GraphHelper;
import org.apache.atlas.repository.graphdb.*; import org.apache.atlas.repository.graphdb.*;
import org.apache.atlas.repository.store.bootstrap.AtlasTypeDefStoreInitializer; import org.apache.atlas.repository.store.bootstrap.AtlasTypeDefStoreInitializer;
import org.apache.atlas.repository.store.graph.v2.AtlasGraphUtilsV2;
import org.apache.atlas.runner.LocalSolrRunner; import org.apache.atlas.runner.LocalSolrRunner;
import org.apache.atlas.store.AtlasTypeDefStore; import org.apache.atlas.store.AtlasTypeDefStore;
import org.apache.atlas.type.AtlasTypeRegistry; import org.apache.atlas.type.AtlasTypeRegistry;
...@@ -143,11 +144,11 @@ public class MigrationBaseAsserts { ...@@ -143,11 +144,11 @@ public class MigrationBaseAsserts {
e = it.next(); e = it.next();
} }
assertNotNull(GraphHelper.getProperty(e, R_GUID_PROPERTY_NAME)); assertNotNull(AtlasGraphUtilsV2.getEncodedProperty(e, R_GUID_PROPERTY_NAME, Object.class));
assertNotNull(GraphHelper.getProperty(e, "tagPropagation")); assertNotNull(AtlasGraphUtilsV2.getEncodedProperty(e, "tagPropagation", Object.class));
if(StringUtils.isNotEmpty(edgeTypeName)) { if(StringUtils.isNotEmpty(edgeTypeName)) {
assertEquals(GraphHelper.getProperty(e, TYPE_NAME_PROPERTY), edgeTypeName, edgeTypeName); assertEquals(AtlasGraphUtilsV2.getEncodedProperty(e, TYPE_NAME_PROPERTY, Object.class), edgeTypeName, edgeTypeName);
} }
assertEquals(count, expectedItems, String.format("%s", edgeTypeName)); assertEquals(count, expectedItems, String.format("%s", edgeTypeName));
...@@ -160,8 +161,8 @@ public class MigrationBaseAsserts { ...@@ -160,8 +161,8 @@ public class MigrationBaseAsserts {
e = it.next(); e = it.next();
} }
assertNotNull(GraphHelper.getProperty(e, R_GUID_PROPERTY_NAME)); assertNotNull(AtlasGraphUtilsV2.getEncodedProperty(e, R_GUID_PROPERTY_NAME, Object.class));
assertNotNull(GraphHelper.getProperty(e, "tagPropagation")); assertNotNull(AtlasGraphUtilsV2.getEncodedProperty(e, "tagPropagation", Object.class));
if(StringUtils.isNotEmpty(edgeTypeName)) { if(StringUtils.isNotEmpty(edgeTypeName)) {
assertEquals(e.getLabel(), edgeTypeName, edgeTypeName); assertEquals(e.getLabel(), edgeTypeName, edgeTypeName);
...@@ -182,7 +183,7 @@ public class MigrationBaseAsserts { ...@@ -182,7 +183,7 @@ public class MigrationBaseAsserts {
} }
if(StringUtils.isNotEmpty(name)) { if(StringUtils.isNotEmpty(name)) {
assertEquals(GraphHelper.getProperty(v, ASSERT_NAME_PROPERTY), name, name); assertEquals(AtlasGraphUtilsV2.getEncodedProperty(v, ASSERT_NAME_PROPERTY, String.class), name, name);
} }
count++; count++;
...@@ -193,6 +194,7 @@ public class MigrationBaseAsserts { ...@@ -193,6 +194,7 @@ public class MigrationBaseAsserts {
protected void assertMigrationStatus(int expectedTotalCount) { protected void assertMigrationStatus(int expectedTotalCount) {
AtlasVertex v = getVertex("__MigrationStatus", ""); AtlasVertex v = getVertex("__MigrationStatus", "");
assertEquals((long) GraphHelper.getProperty(v, "currentIndex"), expectedTotalCount);
assertEquals(AtlasGraphUtilsV2.getEncodedProperty(v, "currentIndex", Number.class).intValue(), expectedTotalCount);
} }
} }
...@@ -24,6 +24,7 @@ import org.apache.atlas.repository.graph.GraphHelper; ...@@ -24,6 +24,7 @@ import org.apache.atlas.repository.graph.GraphHelper;
import org.apache.atlas.repository.graphdb.AtlasGraph; import org.apache.atlas.repository.graphdb.AtlasGraph;
import org.apache.atlas.repository.graphdb.AtlasVertex; import org.apache.atlas.repository.graphdb.AtlasVertex;
import org.apache.atlas.repository.graphdb.GraphDBMigrator; import org.apache.atlas.repository.graphdb.GraphDBMigrator;
import org.apache.atlas.repository.store.graph.v2.AtlasGraphUtilsV2;
import org.apache.atlas.type.AtlasBuiltInTypes; import org.apache.atlas.type.AtlasBuiltInTypes;
import org.testng.annotations.Guice; import org.testng.annotations.Guice;
import org.testng.annotations.Test; import org.testng.annotations.Test;
...@@ -64,8 +65,8 @@ public class PathTest extends MigrationBaseAsserts { ...@@ -64,8 +65,8 @@ public class PathTest extends MigrationBaseAsserts {
BigInteger bitExpected = bitRef.getNormalizedValue(612361213421234L); BigInteger bitExpected = bitRef.getNormalizedValue(612361213421234L);
BigDecimal bdtExpected = bdtRef.getNormalizedValue(125353); BigDecimal bdtExpected = bdtRef.getNormalizedValue(125353);
BigInteger bit = GraphHelper.getSingleValuedProperty(v, HASH_CODE_PROPERTY, BigInteger.class); BigInteger bit = AtlasGraphUtilsV2.getEncodedProperty(v, HASH_CODE_PROPERTY, BigInteger.class);
BigDecimal bdt = GraphHelper.getSingleValuedProperty(v, RETENTION_PROPERTY, BigDecimal.class); BigDecimal bdt = AtlasGraphUtilsV2.getEncodedProperty(v, RETENTION_PROPERTY, BigDecimal.class);
assertEquals(bit, bitExpected); assertEquals(bit, bitExpected);
assertEquals(bdt.compareTo(bdtExpected), 0); assertEquals(bdt.compareTo(bdtExpected), 0);
......
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