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,
AttributeDefinition[] attributeDefinitions) {
this.typeName = typeName;
this.superTraits = superTraits == null ? ImmutableList.<String>of() : superTraits;
this.attributeDefinitions = attributeDefinitions;
public HierarchicalTypeDefinition(Class<T> hierarchicalMetaType,
String typeName, ImmutableList<String> superTypes,
AttributeDefinition[] attributeDefinitions) {
super(typeName, attributeDefinitions);
this.superTypes = superTypes == null ? ImmutableList.<String>of() : superTypes;
}
}
\ No newline at end of file
}
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;
}
}
......@@ -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