Commit 305d16a1 by Harish Butani

extend GraphPersistenceStrategy to expose IdAttrName and materialization of internal Id StructType

parent 543f3a72
......@@ -108,8 +108,17 @@ public class DefaultGraphPersistenceStrategy implements GraphPersistenceStrategi
TitanVertex structVertex = (TitanVertex) value;
StructType structType = (StructType) dataType;
ITypedStruct structInstance = structType.createInstance();
metadataRepository.getGraphToInstanceMapper().mapVertexToInstance(
structVertex, structInstance, structType.fieldMapping().fields);
if ( dataType.getName() == TypeUtils.INSTANCE_ID_TYP().getName()) {
structInstance.set(TypeUtils.INSTANCE_ID_TYP_TYPENAME_ATTRNAME(),
structVertex.getProperty(typeAttributeName()));
structInstance.set(TypeUtils.INSTANCE_ID_TYP_ID_ATTRNAME(),
structVertex.getProperty(idAttributeName()));
} else {
metadataRepository.getGraphToInstanceMapper().mapVertexToInstance(
structVertex, structInstance, structType.fieldMapping().fields);
}
return dataType.convert(structInstance, Multiplicity.OPTIONAL);
case TRAIT:
......@@ -180,4 +189,14 @@ public class DefaultGraphPersistenceStrategy implements GraphPersistenceStrategi
public String loopObjectExpression(IDataType<?> dataType) {
return "{it.object." + typeAttributeName() + " == '" + dataType.getName() + "'}";
}
@Override
public String instanceToTraitEdgeDirection() { return "out"; }
@Override
public String traitToInstanceEdgeDirection() { return "in"; }
@Override
public String idAttributeName() { return metadataRepository.getIdAttributeName(); }
}
......@@ -96,6 +96,10 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
return Constants.ENTITY_TYPE_PROPERTY_KEY;
}
public String getIdAttributeName() {
return Constants.GUID_PROPERTY_KEY;
}
@Override
public String getTraitLabel(IDataType<?> dataType, String traitName) {
return dataType.getName() + "." + traitName;
......
......@@ -45,12 +45,24 @@ trait GraphPersistenceStrategies {
def typeAttributeName: String
/**
* Name of attribute used to store guid in vertex
*/
def idAttributeName : String
/**
* Given a dataType and a reference attribute, how is edge labeled
*/
def edgeLabel(iDataType: IDataType[_], aInfo: AttributeInfo): String
def traitLabel(cls: IDataType[_], traitName: String): String
def instanceToTraitEdgeDirection : String = "out"
def traitToInstanceEdgeDirection = instanceToTraitEdgeDirection match {
case "out" => "in"
case "in" => "out"
case x => x
}
/**
* The propertyKey used to store the attribute in a Graph Vertex.
* @param dataType
......@@ -101,6 +113,7 @@ trait GraphPersistenceStrategies {
object GraphPersistenceStrategy1 extends GraphPersistenceStrategies {
val typeAttributeName = "typeName"
val idAttributeName = "guid"
def edgeLabel(dataType: IDataType[_], aInfo: AttributeInfo) = s"${dataType.getName}.${aInfo.name}"
......@@ -125,6 +138,16 @@ object GraphPersistenceStrategy1 extends GraphPersistenceStrategies {
def constructInstance[U](dataType: IDataType[U], v: AnyRef): U = {
dataType.getTypeCategory match {
case DataTypes.TypeCategory.PRIMITIVE => dataType.convert(v, Multiplicity.OPTIONAL)
case DataTypes.TypeCategory.STRUCT if dataType.getName == TypeUtils.INSTANCE_ID_TYP.getName => {
val sType = dataType.asInstanceOf[StructType]
val sInstance = sType.createInstance()
val tV = v.asInstanceOf[TitanVertex]
sInstance.set(TypeUtils.INSTANCE_ID_TYP_TYPENAME_ATTRNAME,
tV.getProperty[java.lang.String](typeAttributeName))
sInstance.set(TypeUtils.INSTANCE_ID_TYP_ID_ATTRNAME,
tV.getProperty[java.lang.String](idAttributeName))
dataType.convert(sInstance, Multiplicity.OPTIONAL)
}
case DataTypes.TypeCategory.STRUCT => {
val sType = dataType.asInstanceOf[StructType]
val sInstance = sType.createInstance()
......
......@@ -65,6 +65,18 @@ object TypeUtils {
aDefs:_*);
}
val INSTANCE_ID_TYP_ID_ATTRNAME = "guid"
val INSTANCE_ID_TYP_TYPENAME_ATTRNAME = "typeName"
val INSTANCE_ID_TYP_NAME = TEMP_STRUCT_NAME_PREFIX + "_IdType"
val INSTANCE_ID_TYP = {
val idAttr = new AttributeDefinition(INSTANCE_ID_TYP_ID_ATTRNAME,
DataTypes.STRING_TYPE.getName, Multiplicity.REQUIRED, false, null)
val typNmAttr =
new AttributeDefinition(INSTANCE_ID_TYP_TYPENAME_ATTRNAME,
DataTypes.STRING_TYPE.getName, Multiplicity.REQUIRED, false, null)
typSystem.defineQueryResultType(INSTANCE_ID_TYP_NAME, idAttr, typNmAttr);
}
def fieldMapping(iDataType: IDataType[_]) : Option[FieldMapping] = iDataType match {
case c : ClassType => Some(c.fieldMapping())
case t : TraitType => Some(t.fieldMapping())
......
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