Commit 398286a9 by Harish Butani

add IdType as an OOB type in TypeSystem; use it when exposing Trait instance…

add IdType as an OOB type in TypeSystem; use it when exposing Trait instance info; introduce PathExpression
parent 3718af6b
...@@ -29,11 +29,7 @@ import org.apache.hadoop.metadata.repository.graph.GraphBackedMetadataRepository ...@@ -29,11 +29,7 @@ import org.apache.hadoop.metadata.repository.graph.GraphBackedMetadataRepository
import org.apache.hadoop.metadata.typesystem.ITypedReferenceableInstance; import org.apache.hadoop.metadata.typesystem.ITypedReferenceableInstance;
import org.apache.hadoop.metadata.typesystem.ITypedStruct; import org.apache.hadoop.metadata.typesystem.ITypedStruct;
import org.apache.hadoop.metadata.typesystem.persistence.Id; import org.apache.hadoop.metadata.typesystem.persistence.Id;
import org.apache.hadoop.metadata.typesystem.types.AttributeInfo; import org.apache.hadoop.metadata.typesystem.types.*;
import org.apache.hadoop.metadata.typesystem.types.IDataType;
import org.apache.hadoop.metadata.typesystem.types.Multiplicity;
import org.apache.hadoop.metadata.typesystem.types.StructType;
import org.apache.hadoop.metadata.typesystem.types.TraitType;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -109,10 +105,12 @@ public class DefaultGraphPersistenceStrategy implements GraphPersistenceStrategi ...@@ -109,10 +105,12 @@ public class DefaultGraphPersistenceStrategy implements GraphPersistenceStrategi
StructType structType = (StructType) dataType; StructType structType = (StructType) dataType;
ITypedStruct structInstance = structType.createInstance(); ITypedStruct structInstance = structType.createInstance();
if ( dataType.getName() == TypeUtils.INSTANCE_ID_TYP().getName()) { TypeSystem.IdType idType = TypeSystem.getInstance().getIdType();
structInstance.set(TypeUtils.INSTANCE_ID_TYP_TYPENAME_ATTRNAME(),
if ( dataType.getName() == idType.getName()) {
structInstance.set(idType.typeNameAttrName(),
structVertex.getProperty(typeAttributeName())); structVertex.getProperty(typeAttributeName()));
structInstance.set(TypeUtils.INSTANCE_ID_TYP_ID_ATTRNAME(), structInstance.set(idType.idAttrName(),
structVertex.getProperty(idAttributeName())); structVertex.getProperty(idAttributeName()));
} else { } else {
......
...@@ -324,6 +324,8 @@ object Expressions { ...@@ -324,6 +324,8 @@ object Expressions {
def traitInstance() = new TraitInstanceExpression(this) def traitInstance() = new TraitInstanceExpression(this)
def instance() = new InstanceExpression(this) def instance() = new InstanceExpression(this)
def path() = new PathExpression(this)
} }
trait BinaryNode { trait BinaryNode {
...@@ -703,7 +705,7 @@ object Expressions { ...@@ -703,7 +705,7 @@ object Expressions {
throw new ExpressionException(this, throw new ExpressionException(this,
s"Cannot apply instance on ${child.dataType.getName}, it is not a TraitType") s"Cannot apply instance on ${child.dataType.getName}, it is not a TraitType")
} }
TypeUtils.INSTANCE_ID_TYP typSystem.getIdType.getStructType
} }
override def toString = s"$child traitInstance" override def toString = s"$child traitInstance"
...@@ -716,9 +718,22 @@ object Expressions { ...@@ -716,9 +718,22 @@ object Expressions {
throw new UnresolvedException(this, throw new UnresolvedException(this,
s"datatype. Can not resolve due to unresolved child") s"datatype. Can not resolve due to unresolved child")
} }
TypeUtils.INSTANCE_ID_TYP typSystem.getIdType.getStructType
} }
override def toString = s"$child instance" override def toString = s"$child instance"
} }
case class PathExpression(child: Expression)
extends Expression with UnaryNode {
lazy val dataType = {
if (!resolved) {
throw new UnresolvedException(this,
s"datatype. Can not resolve due to unresolved child")
}
TypeUtils.ResultWithPathStruct.createType(child.dataType)
}
override def toString = s"$child withPath"
}
} }
...@@ -138,13 +138,14 @@ object GraphPersistenceStrategy1 extends GraphPersistenceStrategies { ...@@ -138,13 +138,14 @@ object GraphPersistenceStrategy1 extends GraphPersistenceStrategies {
def constructInstance[U](dataType: IDataType[U], v: AnyRef): U = { def constructInstance[U](dataType: IDataType[U], v: AnyRef): U = {
dataType.getTypeCategory match { dataType.getTypeCategory match {
case DataTypes.TypeCategory.PRIMITIVE => dataType.convert(v, Multiplicity.OPTIONAL) case DataTypes.TypeCategory.PRIMITIVE => dataType.convert(v, Multiplicity.OPTIONAL)
case DataTypes.TypeCategory.STRUCT if dataType.getName == TypeUtils.INSTANCE_ID_TYP.getName => { case DataTypes.TypeCategory.STRUCT
if dataType.getName == TypeSystem.getInstance().getIdType.getName => {
val sType = dataType.asInstanceOf[StructType] val sType = dataType.asInstanceOf[StructType]
val sInstance = sType.createInstance() val sInstance = sType.createInstance()
val tV = v.asInstanceOf[TitanVertex] val tV = v.asInstanceOf[TitanVertex]
sInstance.set(TypeUtils.INSTANCE_ID_TYP_TYPENAME_ATTRNAME, sInstance.set(TypeSystem.getInstance().getIdType.typeNameAttrName,
tV.getProperty[java.lang.String](typeAttributeName)) tV.getProperty[java.lang.String](typeAttributeName))
sInstance.set(TypeUtils.INSTANCE_ID_TYP_ID_ATTRNAME, sInstance.set(TypeSystem.getInstance().getIdType.idAttrName,
tV.getProperty[java.lang.String](idAttributeName)) tV.getProperty[java.lang.String](idAttributeName))
dataType.convert(sInstance, Multiplicity.OPTIONAL) dataType.convert(sInstance, Multiplicity.OPTIONAL)
} }
......
...@@ -20,6 +20,7 @@ package org.apache.hadoop.metadata.query ...@@ -20,6 +20,7 @@ package org.apache.hadoop.metadata.query
import org.apache.hadoop.metadata.query.Expressions._ import org.apache.hadoop.metadata.query.Expressions._
import org.apache.hadoop.metadata.typesystem.types.DataTypes.TypeCategory import org.apache.hadoop.metadata.typesystem.types.DataTypes.TypeCategory
import org.apache.hadoop.metadata.typesystem.types.TypeSystem
import scala.collection.mutable import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer
...@@ -174,9 +175,8 @@ class GremlinTranslator(expr: Expression, ...@@ -174,9 +175,8 @@ class GremlinTranslator(expr: Expression,
val theTrait = te.as("theTrait") val theTrait = te.as("theTrait")
val theInstance = theTrait.traitInstance().as("theInstance") val theInstance = theTrait.traitInstance().as("theInstance")
val outE = val outE =
theInstance.select(id("theTrait"), theInstance.select(id("theTrait").as("traitDetails"),
id("theInstance").field(TypeUtils.INSTANCE_ID_TYP_TYPENAME_ATTRNAME).as("instanceTypeName"), id("theInstance").as("instanceInfo"))
id("theInstance").field(TypeUtils.INSTANCE_ID_TYP_ID_ATTRNAME).as("instanceId"))
QueryProcessor.validate(outE) QueryProcessor.validate(outE)
} }
} }
......
...@@ -65,16 +65,18 @@ object TypeUtils { ...@@ -65,16 +65,18 @@ object TypeUtils {
aDefs:_*); aDefs:_*);
} }
val INSTANCE_ID_TYP_ID_ATTRNAME = "guid" object ResultWithPathStruct {
val INSTANCE_ID_TYP_TYPENAME_ATTRNAME = "typeName" val pathAttrName = "path"
val INSTANCE_ID_TYP_NAME = TEMP_STRUCT_NAME_PREFIX + "_IdType" val resultAttrName = "result"
val INSTANCE_ID_TYP = { val pathAttrType = DataTypes.arrayTypeName(typSystem.getIdType.getStructType)
val idAttr = new AttributeDefinition(INSTANCE_ID_TYP_ID_ATTRNAME,
DataTypes.STRING_TYPE.getName, Multiplicity.REQUIRED, false, null) val pathAttr = new AttributeDefinition(pathAttrName, pathAttrType, Multiplicity.COLLECTION, false, null)
val typNmAttr =
new AttributeDefinition(INSTANCE_ID_TYP_TYPENAME_ATTRNAME, def createType(resultType : IDataType[_]) : StructType = {
DataTypes.STRING_TYPE.getName, Multiplicity.REQUIRED, false, null) val resultAttr = new AttributeDefinition(resultAttrName, pathAttrType, Multiplicity.REQUIRED, false, null)
typSystem.defineQueryResultType(INSTANCE_ID_TYP_NAME, idAttr, typNmAttr); val typName = s"${TEMP_STRUCT_NAME_PREFIX}${tempStructCounter.getAndIncrement}"
typSystem.defineQueryResultType(typName, pathAttr, resultAttr);
}
} }
def fieldMapping(iDataType: IDataType[_]) : Option[FieldMapping] = iDataType match { def fieldMapping(iDataType: IDataType[_]) : Option[FieldMapping] = iDataType match {
......
...@@ -41,6 +41,7 @@ public class TypeSystem { ...@@ -41,6 +41,7 @@ public class TypeSystem {
private static final TypeSystem INSTANCE = new TypeSystem(); private static final TypeSystem INSTANCE = new TypeSystem();
public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
private Map<String, IDataType> types; private Map<String, IDataType> types;
private IdType idType;
private TypeSystem() { private TypeSystem() {
initialize(); initialize();
...@@ -61,6 +62,7 @@ public class TypeSystem { ...@@ -61,6 +62,7 @@ public class TypeSystem {
private void initialize() { private void initialize() {
types = new HashMap<>(); types = new HashMap<>();
registerPrimitiveTypes(); registerPrimitiveTypes();
registerCoreTypes();
} }
public ImmutableList<String> getTypeNames() { public ImmutableList<String> getTypeNames() {
...@@ -81,6 +83,18 @@ public class TypeSystem { ...@@ -81,6 +83,18 @@ public class TypeSystem {
types.put(DataTypes.STRING_TYPE.getName(), DataTypes.STRING_TYPE); types.put(DataTypes.STRING_TYPE.getName(), DataTypes.STRING_TYPE);
} }
/*
* The only core OOB type we will define is the Struct to represent the Identity of an Instance.
*/
private void registerCoreTypes() {
idType = new IdType();
}
public IdType getIdType() {
return idType;
}
public boolean isRegistered(String typeName) { public boolean isRegistered(String typeName) {
return types.containsKey(typeName); return types.containsKey(typeName);
} }
...@@ -562,4 +576,37 @@ public class TypeSystem { ...@@ -562,4 +576,37 @@ public class TypeSystem {
throw new MetadataException("Internal Error: define type called on TrasientTypeSystem"); throw new MetadataException("Internal Error: define type called on TrasientTypeSystem");
} }
} }
public class IdType {
private static final String ID_ATTRNAME = "guid";
private static final String TYPENAME_ATTRNAME = "typeName";
private static final String TYP_NAME = "__IdType";
private IdType() {
AttributeDefinition idAttr = new AttributeDefinition(ID_ATTRNAME,
DataTypes.STRING_TYPE.getName(), Multiplicity.REQUIRED, false, null);
AttributeDefinition typNmAttr =
new AttributeDefinition(TYPENAME_ATTRNAME,
DataTypes.STRING_TYPE.getName(), Multiplicity.REQUIRED, false, null);
try {
AttributeInfo[] infos = new AttributeInfo[2];
infos[0] = new AttributeInfo(TypeSystem.this, idAttr);
infos[1] = new AttributeInfo(TypeSystem.this, typNmAttr);
StructType type = new StructType(TypeSystem.this, TYP_NAME, null, infos);
TypeSystem.this.types.put(TYP_NAME, type);
} catch (MetadataException me) {
throw new RuntimeException(me);
}
}
public StructType getStructType() throws MetadataException {
return getDataType(StructType.class, TYP_NAME);
}
public String getName() { return TYP_NAME; }
public String idAttrName() { return ID_ATTRNAME;}
public String typeNameAttrName() { return TYPENAME_ATTRNAME;}
}
} }
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