Commit b8feeebf by Harish Butani

unify defining structs, traits and class types

parent 040ee99c
......@@ -40,12 +40,12 @@ public class ClassType extends HierarchicalType<ClassType, IReferenceableInstanc
* Used when creating a ClassType, to support recursive Structs.
*/
ClassType(TypeSystem typeSystem, String name, ImmutableList<String> superTypes, int numFields) {
super(typeSystem, name, superTypes, numFields);
super(typeSystem, ClassType.class, name, superTypes, numFields);
}
ClassType(TypeSystem typeSystem, String name, ImmutableList<String> superTraits, AttributeInfo... fields)
throws MetadataException {
super(typeSystem, name, superTraits, fields);
super(typeSystem, ClassType.class, name, superTraits, fields);
}
@Override
......
......@@ -36,6 +36,7 @@ public abstract class HierarchicalType<ST extends HierarchicalType,T> extends Ab
implements Comparable<ST> {
public final TypeSystem typeSystem;
public final Class<ST> superTypeClass;
public final String name;
public final FieldMapping fieldMapping;
public final int numFields;
......@@ -47,8 +48,10 @@ public abstract class HierarchicalType<ST extends HierarchicalType,T> extends Ab
/**
* Used when creating a Type, to support recursive Structs.
*/
HierarchicalType(TypeSystem typeSystem, String name, ImmutableList<String> superTypes, int numFields) {
HierarchicalType(TypeSystem typeSystem, Class<ST> superTypeClass,
String name, ImmutableList<String> superTypes, int numFields) {
this.typeSystem = typeSystem;
this.superTypeClass = superTypeClass;
this.name = name;
this.fieldMapping = null;
this.numFields = numFields;
......@@ -56,9 +59,11 @@ public abstract class HierarchicalType<ST extends HierarchicalType,T> extends Ab
this.immediateAttrs = null;
}
HierarchicalType(TypeSystem typeSystem, String name, ImmutableList<String> superTypes, AttributeInfo... fields)
HierarchicalType(TypeSystem typeSystem, Class<ST> superTypeClass,
String name, ImmutableList<String> superTypes, AttributeInfo... fields)
throws MetadataException {
this.typeSystem = typeSystem;
this.superTypeClass = superTypeClass;
this.name = name;
this.fieldMapping = constructFieldMapping(superTypes,
fields);
......@@ -115,8 +120,9 @@ public abstract class HierarchicalType<ST extends HierarchicalType,T> extends Ab
queue.add(new Node(getName()));
while(!queue.isEmpty()) {
Path currentPath = queue.poll();
ST superType = currentPath.typeName == getName() ? (ST) this :
(ST) typeSystem.dataType(currentPath.typeName);
(ST) typeSystem.getDataType(superTypeClass, currentPath.typeName);
pathNameToPathMap.put(currentPath.pathName, currentPath);
if ( superType != this ) {
......@@ -248,7 +254,7 @@ public abstract class HierarchicalType<ST extends HierarchicalType,T> extends Ab
superTypeName, getName()));
}
ST superType = (ST) typeSystem.dataType(superTypeName);
ST superType = (ST) typeSystem.getDataType(superTypeClass, superTypeName);
Map<String, String> downCastMap = superType.constructDowncastFieldMap(this, pathToSuper.get(0));
return new DownCastStructInstance(superTypeName,
new DownCastFieldMapping(ImmutableMap.copyOf(downCastMap)),
......@@ -318,7 +324,12 @@ public abstract class HierarchicalType<ST extends HierarchicalType,T> extends Ab
@Override
public Path next() {
Path p = pathQueue.poll();
ST t = (ST) typeSystem.dataType(p.typeName);
ST t = null;
try {
t = (ST) typeSystem.getDataType(superTypeClass, p.typeName);
} catch(MetadataException me) {
throw new RuntimeException(me);
}
if ( t.superTypes != null ) {
ImmutableList<String> sTs = t.superTypes;
for(String sT : sTs) {
......
......@@ -20,16 +20,14 @@ package org.apache.metadata.types;
import com.google.common.collect.ImmutableList;
public class ClassTypeDefinition {
public class HierarchicalTypeDefinition<T extends HierarchicalType> extends StructTypeDefinition {
public final String typeName;
public final ImmutableList<String> superTraits;
public final AttributeDefinition[] attributeDefinitions;
public final ImmutableList<String> superTypes;
public ClassTypeDefinition(String typeName, ImmutableList<String> superTraits,
public HierarchicalTypeDefinition(Class<T> hierarchicalMetaType,
String typeName, ImmutableList<String> superTypes,
AttributeDefinition[] attributeDefinitions) {
this.typeName = typeName;
this.superTraits = superTraits == null ? ImmutableList.<String>of() : superTraits;
this.attributeDefinitions = attributeDefinitions;
super(typeName, attributeDefinitions);
this.superTypes = superTypes == null ? ImmutableList.<String>of() : superTypes;
}
}
package org.apache.metadata.types;
import com.google.common.collect.ImmutableList;
public class StructTypeDefinition {
public final String typeName;
public final AttributeDefinition[] attributeDefinitions;
public StructTypeDefinition(String typeName,
AttributeDefinition[] attributeDefinitions) {
this.typeName = typeName;
this.attributeDefinitions = attributeDefinitions;
}
}
......@@ -32,13 +32,13 @@ public class TraitType extends HierarchicalType<TraitType, IStruct>
* Used when creating a TraitType, to support recursive Structs.
*/
TraitType(TypeSystem typeSystem, String name, ImmutableList<String> superTraits, int numFields) {
super(typeSystem, name, superTraits, numFields);
super(typeSystem, TraitType.class, name, superTraits, numFields);
handler = null;
}
TraitType(TypeSystem typeSystem, String name, ImmutableList<String> superTraits, AttributeInfo... fields)
throws MetadataException {
super(typeSystem, name, superTraits, fields);
super(typeSystem, TraitType.class, name, superTraits, fields);
handler = new TypedStructHandler(this);
}
......
/**
* 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.metadata.types;
import com.google.common.collect.ImmutableList;
public class TraitTypeDefinition {
public final String typeName;
public final ImmutableList<String> superTraits;
public final AttributeDefinition[] attributeDefinitions;
public TraitTypeDefinition(String typeName, ImmutableList<String> superTraits,
AttributeDefinition[] attributeDefinitions) {
this.typeName = typeName;
this.superTraits = superTraits == null ? ImmutableList.<String>of() : superTraits;
this.attributeDefinitions = attributeDefinitions;
}
}
......@@ -18,9 +18,15 @@
package org.apache.metadata.types;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.apache.metadata.MetadataException;
import javax.annotation.Nullable;
import java.lang.reflect.Constructor;
import java.util.*;
public class TypeSystem {
......@@ -32,7 +38,8 @@ public class TypeSystem {
registerPrimitiveTypes();
}
private TypeSystem(TypeSystem ts) {}
private TypeSystem(TypeSystem ts) {
}
public ImmutableList<String> getTypeNames() {
return ImmutableList.copyOf(types.keySet());
......@@ -52,12 +59,8 @@ public class TypeSystem {
types.put(DataTypes.STRING_TYPE.getName(), DataTypes.STRING_TYPE);
}
public IDataType dataType(String name) {
return types.get(name);
}
public <T> T getDataType(Class<T> cls, String name) throws MetadataException {
if ( types.containsKey(name) ) {
if (types.containsKey(name)) {
return cls.cast(types.get(name));
}
......@@ -65,7 +68,7 @@ public class TypeSystem {
* is this an Array Type?
*/
String arrElemType = TypeUtils.parseAsArrayType(name);
if ( arrElemType != null ) {
if (arrElemType != null) {
IDataType dT = defineArrayType(getDataType(IDataType.class, arrElemType));
return cls.cast(dT);
}
......@@ -74,7 +77,7 @@ public class TypeSystem {
* is this a Map Type?
*/
String[] mapType = TypeUtils.parseAsMapType(name);
if ( mapType != null ) {
if (mapType != null) {
IDataType dT = defineMapType(getDataType(IDataType.class, mapType[0]),
getDataType(IDataType.class, mapType[1]));
return cls.cast(dT);
......@@ -86,187 +89,320 @@ public class TypeSystem {
public StructType defineStructType(String name,
boolean errorIfExists,
AttributeDefinition... attrDefs) throws MetadataException {
if ( types.containsKey(name) ) {
throw new MetadataException(String.format("Cannot redefine type %s", name));
StructTypeDefinition structDef = new StructTypeDefinition(name, attrDefs);
Map<String, IDataType> newTypes = defineTypes(ImmutableList.<StructTypeDefinition>of(structDef),
ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(),
ImmutableList.<HierarchicalTypeDefinition<ClassType>>of());
return getDataType(StructType.class, structDef.typeName);
}
assert name != null;
AttributeInfo[] infos = new AttributeInfo[attrDefs.length];
Map<Integer, AttributeDefinition> recursiveRefs = new HashMap<Integer, AttributeDefinition>();
try {
types.put(name, new StructType(this, name, attrDefs.length));
for (int i = 0; i < attrDefs.length; i++) {
infos[i] = new AttributeInfo(this, attrDefs[i]);
if ( attrDefs[i].dataTypeName == name ) {
recursiveRefs.put(i, attrDefs[i]);
public TraitType defineTraitType(HierarchicalTypeDefinition<TraitType> traitDef
) throws MetadataException {
Map<String, IDataType> newTypes = defineTypes(ImmutableList.<StructTypeDefinition>of(),
ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(traitDef),
ImmutableList.<HierarchicalTypeDefinition<ClassType>>of());
return getDataType(TraitType.class, traitDef.typeName);
}
public ClassType defineClassType(HierarchicalTypeDefinition<ClassType> classDef
) throws MetadataException {
Map<String, IDataType> newTypes = defineTypes(ImmutableList.<StructTypeDefinition>of(),
ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(),
ImmutableList.<HierarchicalTypeDefinition<ClassType>>of());
return getDataType(ClassType.class, classDef.typeName);
}
} catch(MetadataException me) {
types.remove(name);
throw me;
} catch(RuntimeException re) {
types.remove(name);
throw re;
public Map<String, IDataType> defineTraitTypes(HierarchicalTypeDefinition<TraitType>... traitDefs)
throws MetadataException {
TransientTypeSystem transientTypes = new TransientTypeSystem(ImmutableList.<StructTypeDefinition>of(),
ImmutableList.<HierarchicalTypeDefinition<TraitType>>copyOf(traitDefs),
ImmutableList.<HierarchicalTypeDefinition<ClassType>>of());
return transientTypes.defineTypes();
}
StructType sT = new StructType(this, name, null, infos);
types.put(name, sT);
for(Map.Entry<Integer, AttributeDefinition> e : recursiveRefs.entrySet()) {
infos[e.getKey()].setDataType(sT);
public Map<String, IDataType> defineTypes(ImmutableList<StructTypeDefinition> structDefs,
ImmutableList<HierarchicalTypeDefinition<TraitType>> traitDefs,
ImmutableList<HierarchicalTypeDefinition<ClassType>> classDefs)
throws MetadataException {
TransientTypeSystem transientTypes = new TransientTypeSystem(structDefs,
traitDefs,
classDefs);
return transientTypes.defineTypes();
}
return sT;
public DataTypes.ArrayType defineArrayType(IDataType elemType) throws MetadataException {
assert elemType != null;
DataTypes.ArrayType dT = new DataTypes.ArrayType(elemType);
types.put(dT.getName(), dT);
return dT;
}
public TraitType defineTraitType(boolean errorIfExists,
TraitTypeDefinition traitDef
) throws MetadataException {
Map<String, TraitType> m = defineTraitTypes(errorIfExists, traitDef);
return m.values().iterator().next();
public DataTypes.MapType defineMapType(IDataType keyType, IDataType valueType) throws MetadataException {
assert keyType != null;
assert valueType != null;
DataTypes.MapType dT = new DataTypes.MapType(keyType, valueType);
types.put(dT.getName(), dT);
return dT;
}
public Map<String, TraitType> defineTraitTypes(boolean errorIfExists,
TraitTypeDefinition... traitDefs
) throws MetadataException {
TransientTypeSystem transientTypes = new TransientTypeSystem();
Map<String,TraitTypeDefinition> traitDefMap = new HashMap<String, TraitTypeDefinition>();
class TransientTypeSystem extends TypeSystem {
final ImmutableList<StructTypeDefinition> structDefs;
final ImmutableList<HierarchicalTypeDefinition<TraitType>> traitDefs;
final ImmutableList<HierarchicalTypeDefinition<ClassType>> classDefs;
Map<String, StructTypeDefinition> structNameToDefMap = new HashMap<String, StructTypeDefinition>();
Map<String, HierarchicalTypeDefinition<TraitType>> traitNameToDefMap =
new HashMap<String, HierarchicalTypeDefinition<TraitType>>();
Map<String, HierarchicalTypeDefinition<ClassType>> classNameToDefMap =
new HashMap<String, HierarchicalTypeDefinition<ClassType>>();
Set<String> transientTypes;
List<AttributeInfo> recursiveRefs;
List<DataTypes.ArrayType> recursiveArrayTypes;
List<DataTypes.MapType> recursiveMapTypes;
TransientTypeSystem(ImmutableList<StructTypeDefinition> structDefs,
ImmutableList<HierarchicalTypeDefinition<TraitType>> traitDefs,
ImmutableList<HierarchicalTypeDefinition<ClassType>> classDefs) {
super(TypeSystem.this);
this.structDefs = structDefs;
this.traitDefs = traitDefs;
this.classDefs = classDefs;
structNameToDefMap = new HashMap<String, StructTypeDefinition>();
traitNameToDefMap =
new HashMap<String, HierarchicalTypeDefinition<TraitType>>();
classNameToDefMap =
new HashMap<String, HierarchicalTypeDefinition<ClassType>>();
recursiveRefs = new ArrayList<AttributeInfo>();
recursiveArrayTypes = new ArrayList<DataTypes.ArrayType>();
recursiveMapTypes = new ArrayList<DataTypes.MapType>();
transientTypes = new LinkedHashSet<String>();
}
private IDataType dataType(String name) {
return TypeSystem.this.types.get(name);
}
/*
* Step 1:
* - validate cannot redefine types
* - setup an empty TraitType to allow for recursive type graphs.
* - for Hierarchical Types setup an empty TraitType to allow for recursive type graphs.
*/
for(TraitTypeDefinition traitDef : traitDefs) {
private void step1() throws MetadataException {
for (StructTypeDefinition sDef : structDefs) {
assert sDef.typeName != null;
TypeUtils.validateName(sDef.typeName);
if (dataType(sDef.typeName) != null) {
throw new MetadataException(String.format("Cannot redefine type %s", sDef.typeName));
}
TypeSystem.this.types.put(sDef.typeName,
new StructType(this, sDef.typeName, sDef.attributeDefinitions.length));
structNameToDefMap.put(sDef.typeName, sDef);
transientTypes.add(sDef.typeName);
}
for (HierarchicalTypeDefinition<TraitType> traitDef : traitDefs) {
assert traitDef.typeName != null;
if ( types.containsKey(traitDef.typeName) ) {
TypeUtils.validateName(traitDef.typeName);
if (types.containsKey(traitDef.typeName)) {
throw new MetadataException(String.format("Cannot redefine type %s", traitDef.typeName));
}
transientTypes.traitTypes.put(traitDef.typeName,
new TraitType(transientTypes, traitDef.typeName, traitDef.superTraits,
TypeSystem.this.types.put(traitDef.typeName,
new TraitType(this, traitDef.typeName, traitDef.superTypes,
traitDef.attributeDefinitions.length));
traitDefMap.put(traitDef.typeName, traitDef);
traitNameToDefMap.put(traitDef.typeName, traitDef);
transientTypes.add(traitDef.typeName);
}
/*
* Step 2:
* - validate SuperTypes.
*/
for(TraitTypeDefinition traitDef : traitDefs) {
for (HierarchicalTypeDefinition<ClassType> classDef : classDefs) {
assert classDef.typeName != null;
TypeUtils.validateName(classDef.typeName);
if (types.containsKey(classDef.typeName)) {
throw new MetadataException(String.format("Cannot redefine type %s", classDef.typeName));
}
TypeSystem.this.types.put(classDef.typeName,
new ClassType(this, classDef.typeName, classDef.superTypes,
classDef.attributeDefinitions.length));
classNameToDefMap.put(classDef.typeName, classDef);
transientTypes.add(classDef.typeName);
}
}
private <U extends HierarchicalType> void validateSuperTypes(Class<U> cls, HierarchicalTypeDefinition<U> def)
throws MetadataException {
Set<String> s = new HashSet<String>();
for(String superTraitName : traitDef.superTraits ) {
ImmutableList<String> superTypes = def.superTypes;
for (String superTypeName : superTypes) {
if (s.contains(superTraitName) ) {
throw new MetadataException(String.format("Trait %s extends superTrait %s multiple times",
traitDef.typeName, superTraitName));
if (s.contains(superTypeName)) {
throw new MetadataException(String.format("Type %s extends superType %s multiple times",
def.typeName, superTypeName));
}
IDataType dT = types.get(superTraitName);
dT = dT == null ? transientTypes.traitTypes.get(superTraitName) : dT;
IDataType dT = dataType(superTypeName);
if ( dT == null ) {
if (dT == null) {
throw new MetadataException(String.format("Unknown superType %s in definition of type %s",
superTraitName, traitDef.typeName));
superTypeName, def.typeName));
}
if ( dT.getTypeCategory() != DataTypes.TypeCategory.TRAIT ) {
throw new MetadataException(String.format("SuperType %s must be a Trait, in definition of type %s",
superTraitName, traitDef.typeName));
if (!cls.isAssignableFrom(dT.getClass())) {
throw new MetadataException(String.format("SuperType %s must be a %s, in definition of type %s",
superTypeName, cls.getName(), def.typeName));
}
s.add(superTraitName);
s.add(superTypeName);
}
}
/*
* Step 3:
* - Construct TraitTypes in order of SuperType before SubType.
* Step 2:
* - for Hierarchical Types, validate SuperTypes.
*/
List<TraitType> l = new ArrayList<TraitType>(transientTypes.traitTypes.values());
Collections.sort(l);
List<AttributeInfo> recursiveRefs = new ArrayList<AttributeInfo>();
List<DataTypes.ArrayType> recursiveArrayTypes = new ArrayList<DataTypes.ArrayType>();
List<DataTypes.MapType> recursiveMapTypes = new ArrayList<DataTypes.MapType>();
private void step2() throws MetadataException {
for (HierarchicalTypeDefinition<TraitType> traitDef : traitDefs) {
validateSuperTypes(TraitType.class, traitDef);
}
for (HierarchicalTypeDefinition<ClassType> classDef : classDefs) {
validateSuperTypes(ClassType.class, classDef);
}
}
try {
for (TraitType ttO : l) {
TraitTypeDefinition traitDef = traitDefMap.get(ttO.getName());
AttributeInfo[] infos = new AttributeInfo[traitDef.attributeDefinitions.length];
for (int i = 0; i < traitDef.attributeDefinitions.length; i++) {
infos[i] = new AttributeInfo(this, traitDef.attributeDefinitions[i]);
if (transientTypes.traitTypes.containsKey(traitDef.attributeDefinitions[i].dataTypeName)) {
recursiveRefs.add(infos[i]);
}
if ( infos[i].dataType().getTypeCategory() == DataTypes.TypeCategory.ARRAY ) {
DataTypes.ArrayType arrType = (DataTypes.ArrayType) infos[i].dataType();
if (transientTypes.traitTypes.containsKey(arrType.getElemType().getName())) {
private AttributeInfo constructAttributeInfo(AttributeDefinition attrDef) throws MetadataException {
AttributeInfo info = new AttributeInfo(this, attrDef);
if (transientTypes.contains(attrDef.dataTypeName)) {
recursiveRefs.add(info);
}
if (info.dataType().getTypeCategory() == DataTypes.TypeCategory.ARRAY) {
DataTypes.ArrayType arrType = (DataTypes.ArrayType) info.dataType();
if (transientTypes.contains(arrType.getElemType().getName())) {
recursiveArrayTypes.add(arrType);
}
}
if ( infos[i].dataType().getTypeCategory() == DataTypes.TypeCategory.MAP ) {
DataTypes.MapType mapType = (DataTypes.MapType) infos[i].dataType();
if (transientTypes.traitTypes.containsKey(mapType.getKeyType().getName())) {
if (info.dataType().getTypeCategory() == DataTypes.TypeCategory.MAP) {
DataTypes.MapType mapType = (DataTypes.MapType) info.dataType();
if (transientTypes.contains(mapType.getKeyType().getName())) {
recursiveMapTypes.add(mapType);
} else if (transientTypes.traitTypes.containsKey(mapType.getValueType().getName())) {
} else if (transientTypes.contains(mapType.getValueType().getName())) {
recursiveMapTypes.add(mapType);
}
}
return info;
}
private StructType constructStructureType(StructTypeDefinition def)
throws MetadataException {
AttributeInfo[] infos = new AttributeInfo[def.attributeDefinitions.length];
for (int i = 0; i < def.attributeDefinitions.length; i++) {
infos[i] = constructAttributeInfo(def.attributeDefinitions[i]);
}
StructType type = new StructType(TypeSystem.this, def.typeName, null, infos);
TypeSystem.this.types.put(def.typeName, type);
return type;
}
private <U extends HierarchicalType> U constructHierarchicalType(Class<U> cls,
HierarchicalTypeDefinition<U> def)
throws MetadataException {
AttributeInfo[] infos = new AttributeInfo[def.attributeDefinitions.length];
for (int i = 0; i < def.attributeDefinitions.length; i++) {
infos[i] = constructAttributeInfo(def.attributeDefinitions[i]);
}
try {
Constructor<U> cons = cls.getDeclaredConstructor(new Class[]{
TypeSystem.class,
String.class,
ImmutableList.class,
AttributeInfo[].class});
U type = cons.newInstance(TypeSystem.this, def.typeName, def.superTypes, infos);
TypeSystem.this.types.put(def.typeName, type);
return type;
} catch (Exception e) {
throw new MetadataException(String.format("Cannot construct Type of MetaType %s", cls.getName()), e);
}
}
/*
* Step 3:
* - Order Hierarchical Types in order of SuperType before SubType.
* - Construct all the Types
*/
private void step3() throws MetadataException {
List<TraitType> traitTypes = new ArrayList<TraitType>();
for (String traitTypeName : traitNameToDefMap.keySet()) {
traitTypes.add(getDataType(TraitType.class, traitTypeName));
}
Collections.sort(traitTypes);
List<ClassType> classTypes = new ArrayList<ClassType>();
for (String classTypeName : classNameToDefMap.keySet()) {
classTypes.add(getDataType(ClassType.class, classTypeName));
}
Collections.sort(classTypes);
for (StructTypeDefinition structDef : structDefs) {
constructStructureType(structDef);
}
for (TraitType traitType : traitTypes) {
constructHierarchicalType(TraitType.class, traitNameToDefMap.get(traitType.getName()));
}
for (ClassType classType : classTypes) {
constructHierarchicalType(ClassType.class, classNameToDefMap.get(classType.getName()));
}
TraitType tt = new TraitType(this, traitDef.typeName, traitDef.superTraits, infos);
types.put(tt.getName(), tt);
}
/*
* Step 4:
* - fix up references in recursive AttrInfo and recursive Collection Types.
*/
private void step4() throws MetadataException {
for (AttributeInfo info : recursiveRefs) {
info.setDataType(dataType(info.dataType().getName()));
}
for(DataTypes.ArrayType arrType : recursiveArrayTypes ) {
for (DataTypes.ArrayType arrType : recursiveArrayTypes) {
arrType.setElemType(dataType(arrType.getElemType().getName()));
}
for(DataTypes.MapType mapType : recursiveMapTypes ) {
for (DataTypes.MapType mapType : recursiveMapTypes) {
mapType.setKeyType(dataType(mapType.getKeyType().getName()));
mapType.setValueType(dataType(mapType.getValueType().getName()));
}
} catch(MetadataException me) {
for(String sT : transientTypes.traitTypes.keySet()) {
types.remove(sT);
}
throw me;
}
return transientTypes.traitTypes;
}
public DataTypes.ArrayType defineArrayType(IDataType elemType) throws MetadataException {
assert elemType != null;
DataTypes.ArrayType dT = new DataTypes.ArrayType(elemType);
types.put(dT.getName(), dT);
return dT;
Map<String, IDataType> defineTypes() throws MetadataException {
step1();
step2();
try {
step3();
step4();
} catch (MetadataException me) {
for (String sT : transientTypes) {
types.remove(sT);
}
public DataTypes.MapType defineMapType(IDataType keyType, IDataType valueType) throws MetadataException {
assert keyType != null;
assert valueType != null;
DataTypes.MapType dT = new DataTypes.MapType(keyType, valueType);
types.put(dT.getName(), dT);
return dT;
throw me;
}
class TransientTypeSystem extends TypeSystem {
Map<String, TraitType> traitTypes = new HashMap<String, TraitType>();
Map<String, IDataType> newTypes = new HashMap<String, IDataType>();
TransientTypeSystem() {
super(TypeSystem.this);
for (String tName : transientTypes) {
newTypes.put(tName, dataType(tName));
}
public IDataType dataType(String name) {
IDataType dT = TypeSystem.this.dataType(name);
dT = dT == null ? traitTypes.get(name) : dT;
return dT;
return newTypes;
}
@Override
......@@ -282,28 +418,37 @@ public class TypeSystem {
@Override
public StructType defineStructType(String name, boolean errorIfExists, AttributeDefinition... attrDefs)
throws MetadataException {
return TypeSystem.this.defineStructType(name, errorIfExists, attrDefs);
throw new MetadataException("Internal Error: define type called on TrasientTypeSystem");
}
@Override
public TraitType defineTraitType(boolean errorIfExists, TraitTypeDefinition traitDef) throws MetadataException {
return TypeSystem.this.defineTraitType(errorIfExists, traitDef);
public TraitType defineTraitType(HierarchicalTypeDefinition traitDef) throws MetadataException {
throw new MetadataException("Internal Error: define type called on TrasientTypeSystem");
}
@Override
public Map<String, TraitType> defineTraitTypes(boolean errorIfExists, TraitTypeDefinition... traitDefs)
public ClassType defineClassType(HierarchicalTypeDefinition<ClassType> classDef
) throws MetadataException {
throw new MetadataException("Internal Error: define type called on TrasientTypeSystem");
}
@Override
public Map<String, IDataType> defineTypes(ImmutableList<StructTypeDefinition> structDefs,
ImmutableList<HierarchicalTypeDefinition<TraitType>> traitDefs,
ImmutableList<HierarchicalTypeDefinition<ClassType>> classDefs)
throws MetadataException {
return TypeSystem.this.defineTraitTypes(errorIfExists, traitDefs);
throw new MetadataException("Internal Error: define type called on TrasientTypeSystem");
}
@Override
public DataTypes.ArrayType defineArrayType(IDataType elemType) throws MetadataException {
return TypeSystem.this.defineArrayType(elemType);
throw new MetadataException("Internal Error: define type called on TrasientTypeSystem");
}
@Override
public DataTypes.MapType defineMapType(IDataType keyType, IDataType valueType) throws MetadataException {
return TypeSystem.this.defineMapType(keyType, valueType);
throw new MetadataException("Internal Error: define type called on TrasientTypeSystem");
}
}
}
......@@ -21,13 +21,9 @@ package org.apache.metadata;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import junit.framework.TestCase;
import org.apache.metadata.storage.IRepository;
import org.apache.metadata.storage.memory.MemRepository;
import org.apache.metadata.types.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
......@@ -126,18 +122,18 @@ public abstract class BaseTest {
return new AttributeDefinition(name, dataType, Multiplicity.REQUIRED, false, null);
}
protected Map<String, TraitType> defineTraits(TraitTypeDefinition... tDefs) throws MetadataException {
return ms.getTypeSystem().defineTraitTypes(true, tDefs);
protected Map<String, IDataType> defineTraits(HierarchicalTypeDefinition... tDefs) throws MetadataException {
return ms.getTypeSystem().defineTraitTypes(tDefs);
}
protected TraitTypeDefinition createTraitTypeDef(String name, ImmutableList<String> superTypes,
protected HierarchicalTypeDefinition<TraitType> createTraitTypeDef(String name, ImmutableList<String> superTypes,
AttributeDefinition... attrDefs) {
return new TraitTypeDefinition(name, superTypes, attrDefs);
return new HierarchicalTypeDefinition(TraitType.class, name, superTypes, attrDefs);
}
protected ClassTypeDefinition createClassTypeDef(String name, ImmutableList<String> superTypes,
protected HierarchicalTypeDefinition<ClassType> createClassTypeDef(String name, ImmutableList<String> superTypes,
AttributeDefinition... attrDefs) {
return new ClassTypeDefinition(name, superTypes, attrDefs);
return new HierarchicalTypeDefinition(ClassType.class, name, superTypes, attrDefs);
}
}
......@@ -2,13 +2,10 @@ package org.apache.metadata;
import com.google.common.collect.ImmutableList;
import org.junit.Assert;
import org.apache.metadata.storage.StructInstance;
import org.apache.metadata.types.*;
import org.junit.Before;
import org.junit.Test;
import java.util.Map;
public class TraitTest extends BaseTest {
......@@ -34,16 +31,16 @@ public class TraitTest extends BaseTest {
*/
@Test
public void test1() throws MetadataException {
TraitTypeDefinition A = createTraitTypeDef("A", null,
HierarchicalTypeDefinition A = createTraitTypeDef("A", null,
createRequiredAttrDef("a", DataTypes.INT_TYPE),
createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE),
createOptionalAttrDef("c", DataTypes.BYTE_TYPE),
createOptionalAttrDef("d", DataTypes.SHORT_TYPE));
TraitTypeDefinition B = createTraitTypeDef("B", ImmutableList.<String>of("A"),
HierarchicalTypeDefinition B = createTraitTypeDef("B", ImmutableList.<String>of("A"),
createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE));
TraitTypeDefinition C = createTraitTypeDef("C", ImmutableList.<String>of("A"),
HierarchicalTypeDefinition C = createTraitTypeDef("C", ImmutableList.<String>of("A"),
createOptionalAttrDef("c", DataTypes.BYTE_TYPE));
TraitTypeDefinition D = createTraitTypeDef("D", ImmutableList.<String>of("B", "C"),
HierarchicalTypeDefinition D = createTraitTypeDef("D", ImmutableList.<String>of("B", "C"),
createOptionalAttrDef("d", DataTypes.SHORT_TYPE));
defineTraits(A, B, C, D);
......
......@@ -78,16 +78,16 @@ class SerializationTest extends BaseTest {
}
@Test def testTrait {
val A: TraitTypeDefinition = createTraitTypeDef("A", null,
val A: HierarchicalTypeDefinition[TraitType] = createTraitTypeDef("A", null,
BaseTest.createRequiredAttrDef("a", DataTypes.INT_TYPE),
BaseTest.createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE),
BaseTest.createOptionalAttrDef("c", DataTypes.BYTE_TYPE),
BaseTest.createOptionalAttrDef("d", DataTypes.SHORT_TYPE))
val B: TraitTypeDefinition = createTraitTypeDef("B", ImmutableList.of[String]("A"),
val B: HierarchicalTypeDefinition[TraitType] = createTraitTypeDef("B", ImmutableList.of[String]("A"),
BaseTest.createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE))
val C: TraitTypeDefinition = createTraitTypeDef("C", ImmutableList.of[String]("A"),
val C: HierarchicalTypeDefinition[TraitType] = createTraitTypeDef("C", ImmutableList.of[String]("A"),
BaseTest.createOptionalAttrDef("c", DataTypes.BYTE_TYPE))
val D: TraitTypeDefinition = createTraitTypeDef("D", ImmutableList.of[String]("B", "C"),
val D: HierarchicalTypeDefinition[TraitType] = createTraitTypeDef("D", ImmutableList.of[String]("B", "C"),
BaseTest.createOptionalAttrDef("d", DataTypes.SHORT_TYPE))
defineTraits(A, B, C, D)
......
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