Commit d3e60d71 by Harish Butani

Store contracts and skeleton code

parent 2d335d55
......@@ -17,6 +17,8 @@
*/
package org.apache.metadata.storage;
import org.apache.metadata.*;
import java.text.DateFormat;
public interface IRepository {
......@@ -24,4 +26,12 @@ public interface IRepository {
DateFormat getDateFormat();
DateFormat getTimestampFormat();
boolean allowNullsInCollections();
ITypedReferenceableInstance create(IReferenceableInstance i) throws RepositoryException;
ITypedReferenceableInstance update(ITypedReferenceableInstance i) throws RepositoryException;
void delete(ITypedReferenceableInstance i) throws RepositoryException;
ITypedInstance get(Id id) throws RepositoryException;
}
package org.apache.metadata.storage;
import org.apache.metadata.MetadataException;
public class RepositoryException extends MetadataException {
public RepositoryException() {
}
public RepositoryException(String message) {
super(message);
}
public RepositoryException(String message, Throwable cause) {
super(message, cause);
}
public RepositoryException(Throwable cause) {
super(cause);
}
public RepositoryException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
......@@ -22,9 +22,8 @@ import com.google.common.collect.ImmutableMap;
import org.apache.metadata.IStruct;
import org.apache.metadata.ITypedStruct;
import org.apache.metadata.MetadataException;
import org.apache.metadata.types.AttributeInfo;
import org.apache.metadata.types.FieldMapping;
import org.apache.metadata.types.TypeUtils;
import org.apache.metadata.MetadataService;
import org.apache.metadata.types.*;
import java.math.BigDecimal;
import java.math.BigInteger;
......@@ -94,16 +93,121 @@ public class StructInstance implements ITypedStruct {
return fieldMapping;
}
@Override
public Object get(String attrName) throws MetadataException {
return fieldMapping.get(this, attrName);
public void set(String attrName, Object val) throws MetadataException {
AttributeInfo i = fieldMapping.fields.get(attrName);
if ( i == null ) {
throw new ValueConversionException(getTypeName(), val, "Unknown field " + attrName);
}
int pos = fieldMapping.fieldPos.get(attrName);
int nullPos = fieldMapping.fieldNullPos.get(attrName);
Object cVal = null;
if (val != null && val instanceof Id) {
ClassType clsType =
MetadataService.getCurrentTypeSystem().getDataType(ClassType.class, i.dataType().getName());
clsType.validateId((Id)cVal);
cVal = val;
} else {
cVal = i.dataType().convert(val, i.multiplicity);
}
if ( cVal == null ) {
nullFlags[nullPos] = true;
return;
}
nullFlags[nullPos] = false;
if ( i.dataType() == DataTypes.BOOLEAN_TYPE ) {
bools[pos] = ((Boolean)cVal).booleanValue();
} else if ( i.dataType() == DataTypes.BYTE_TYPE ) {
bytes[pos] = ((Byte)cVal).byteValue();
} else if ( i.dataType() == DataTypes.SHORT_TYPE ) {
shorts[pos] = ((Short)cVal).shortValue();
} else if ( i.dataType() == DataTypes.INT_TYPE ) {
ints[pos] = ((Integer)cVal).intValue();
} else if ( i.dataType() == DataTypes.LONG_TYPE ) {
longs[pos] = ((Long)cVal).longValue();
} else if ( i.dataType() == DataTypes.FLOAT_TYPE ) {
floats[pos] = ((Float)cVal).floatValue();
} else if ( i.dataType() == DataTypes.DOUBLE_TYPE ) {
doubles[pos] = ((Double)cVal).doubleValue();
} else if ( i.dataType() == DataTypes.BIGINTEGER_TYPE ) {
bigIntegers[pos] = (BigInteger) cVal;
} else if ( i.dataType() == DataTypes.BIGDECIMAL_TYPE ) {
bigDecimals[pos] = (BigDecimal) cVal;
} else if ( i.dataType() == DataTypes.DATE_TYPE ) {
dates[pos] = (Date) cVal;
} else if ( i.dataType() == DataTypes.STRING_TYPE ) {
strings[pos] = (String) cVal;
} else if ( i.dataType().getTypeCategory() == DataTypes.TypeCategory.ARRAY ) {
arrays[pos] = (ImmutableList) cVal;
} else if ( i.dataType().getTypeCategory() == DataTypes.TypeCategory.MAP ) {
maps[pos] = (ImmutableMap) cVal;
} else if ( i.dataType().getTypeCategory() == DataTypes.TypeCategory.STRUCT ||
i.dataType().getTypeCategory() == DataTypes.TypeCategory.TRAIT ) {
structs[pos] = (StructInstance) cVal;
} else if ( i.dataType().getTypeCategory() == DataTypes.TypeCategory.CLASS ) {
if ( cVal instanceof Id ) {
ids[pos] = (Id) cVal;
} else {
referenceables[pos] = (ReferenceableInstance) cVal;
}
} else {
throw new MetadataException(String.format("Unknown datatype %s", i.dataType()));
}
}
@Override
public void set(String attrName, Object val) throws MetadataException {
fieldMapping.set(this, attrName, val);
public Object get(String attrName) throws MetadataException {
AttributeInfo i = fieldMapping.fields.get(attrName);
if ( i == null ) {
throw new MetadataException(String.format("Unknown field %s for Struct %s", attrName, getTypeName()));
}
int pos = fieldMapping.fieldPos.get(attrName);
int nullPos = fieldMapping.fieldNullPos.get(attrName);
if ( nullFlags[nullPos]) {
return null;
}
if ( i.dataType() == DataTypes.BOOLEAN_TYPE ) {
return bools[pos];
} else if ( i.dataType() == DataTypes.BYTE_TYPE ) {
return bytes[pos];
} else if ( i.dataType() == DataTypes.SHORT_TYPE ) {
return shorts[pos];
} else if ( i.dataType() == DataTypes.INT_TYPE ) {
return ints[pos];
} else if ( i.dataType() == DataTypes.LONG_TYPE ) {
return longs[pos];
} else if ( i.dataType() == DataTypes.FLOAT_TYPE ) {
return floats[pos];
} else if ( i.dataType() == DataTypes.DOUBLE_TYPE ) {
return doubles[pos];
} else if ( i.dataType() == DataTypes.BIGINTEGER_TYPE ) {
return bigIntegers[pos];
} else if ( i.dataType() == DataTypes.BIGDECIMAL_TYPE ) {
return bigDecimals[pos];
} else if ( i.dataType() == DataTypes.DATE_TYPE ) {
return dates[pos];
} else if ( i.dataType() == DataTypes.STRING_TYPE ) {
return strings[pos];
} else if ( i.dataType().getTypeCategory() == DataTypes.TypeCategory.ARRAY ) {
return arrays[pos];
} else if ( i.dataType().getTypeCategory() == DataTypes.TypeCategory.MAP ) {
return maps[pos];
} else if ( i.dataType().getTypeCategory() == DataTypes.TypeCategory.STRUCT ||
i.dataType().getTypeCategory() == DataTypes.TypeCategory.TRAIT ) {
return structs[pos];
} else if ( i.dataType().getTypeCategory() == DataTypes.TypeCategory.CLASS ) {
if ( ids[pos] != null ) {
return ids[pos];
} else {
return referenceables[pos];
}
} else {
throw new MetadataException(String.format("Unknown datatype %s", i.dataType()));
}
}
public void output(IStruct s, Appendable buf, String prefix) throws MetadataException {
TypeUtils.outputVal("{", buf, prefix);
if ( s == null ) {
......
package org.apache.metadata.storage.memory;
import it.unimi.dsi.fastutil.booleans.BooleanArrayList;
import org.apache.metadata.ITypedInstance;
import org.apache.metadata.storage.RepositoryException;
import org.apache.metadata.storage.StructInstance;
import org.apache.metadata.types.AttributeInfo;
import org.apache.metadata.types.FieldMapping;
import org.apache.metadata.types.IConstructableType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AttributeStores {
private static final Object NULL_VAL = new Object();
static IAttributeStore createStore(AttributeInfo i) {
return null;
}
static abstract class AbstractAttributeStore {
AttributeInfo attrInfo;
final BooleanArrayList nullList;
final Map<Integer, Map<String, Object>> hiddenVals;
AbstractAttributeStore(AttributeInfo attrInfo) {
this.attrInfo = attrInfo;
this.nullList = new BooleanArrayList();
hiddenVals = new HashMap<Integer, Map<String, Object>>();
}
final void setNull(int pos, boolean flag) {
nullList.set(pos, flag);
}
final boolean getNull(int pos) {
return nullList.get(pos);
}
void storeHiddenVals(int pos, IConstructableType type, StructInstance instance) throws RepositoryException {
List<String> attrNames = type.getNames(attrInfo);
Map<String, Object> m = hiddenVals.get(pos);
if ( m == null ) {
m = new HashMap<String, Object>();
hiddenVals.put(pos, m);
}
for(int i=2; i < attrNames.size(); i++ ) {
String attrName = attrNames.get(i);
int nullPos = instance.fieldMapping().fieldNullPos.get(attrName);
int colPos = instance.fieldMapping().fieldPos.get(attrName);
if ( instance.nullFlags[nullPos] ) {
m.put(attrName, NULL_VAL);
} else{
m.put(attrName, instance.bools[colPos]);
}
}
}
}
static class BooleanAttributeStore extends AbstractAttributeStore implements IAttributeStore {
final BooleanArrayList list;
BooleanAttributeStore(AttributeInfo attrInfo) {
super(attrInfo);
this.list = new BooleanArrayList();
}
@Override
public void store(int pos, IConstructableType type, StructInstance instance) throws RepositoryException {
List<String> attrNames = type.getNames(attrInfo);
String attrName = attrNames.get(0);
int nullPos = instance.fieldMapping().fieldNullPos.get(attrName);
int colPos = instance.fieldMapping().fieldPos.get(attrName);
nullList.set(pos, instance.nullFlags[nullPos]);
list.set(pos, instance.bools[colPos]);
if ( attrNames.size() > 1 ) {
storeHiddenVals(pos, type, instance);
}
}
@Override
public void load(int pos, IConstructableType type, StructInstance instance) throws RepositoryException {
}
@Override
public void ensureCapacity(int pos) throws RepositoryException {
list.ensureCapacity(pos);
nullList.ensureCapacity(pos);
}
}
}
package org.apache.metadata.storage.memory;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.metadata.ITypedReferenceableInstance;
import org.apache.metadata.storage.Id;
import org.apache.metadata.storage.RepositoryException;
import org.apache.metadata.types.AttributeInfo;
import org.apache.metadata.types.HierarchicalType;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class HierarchicalTypeStore {
final MemRepository repository;
final HierarchicalType hierarchicalType;
final ImmutableMap<AttributeInfo, IAttributeStore> attrStores;
final ImmutableList<HierarchicalTypeStore> superTypeStores;
/**
* Map Id to position in storage lists.
*/
Map<Id, Integer> idPosMap;
List<Integer> freePositions;
/**
* Lock for each Class/Trait.
*/
ReentrantReadWriteLock lock;
HierarchicalTypeStore(MemRepository repository, HierarchicalType hierarchicalType) throws RepositoryException {
this.hierarchicalType = hierarchicalType;
this.repository = repository;
ImmutableMap.Builder<AttributeInfo, IAttributeStore> b = new ImmutableBiMap.Builder<AttributeInfo,
IAttributeStore>();
ImmutableList<AttributeInfo>l = hierarchicalType.immediateAttrs;
for(AttributeInfo i : l) {
b.put(i, AttributeStores.createStore(i) );
}
attrStores = b.build();
ImmutableList.Builder<HierarchicalTypeStore> b1 = new ImmutableList.Builder<HierarchicalTypeStore>();
Set<String> allSuperTypeNames = hierarchicalType.getAllSuperTypeNames();
for(String s : allSuperTypeNames) {
b1.add(repository.getStore(s));
}
superTypeStores = b1.build();
}
/**
* Assign a storage position to an Id.
* - try to assign from freePositions
* - ensure storage capacity.
* - add entry in idPosMap.
* @param id
* @return
* @throws RepositoryException
*/
int assignPosition(Id id) throws RepositoryException {
throw new RepositoryException("Not implemented");
}
/**
* - remove from idPosMap
* - add to freePositions.
* @throws RepositoryException
*/
void releaseId() throws RepositoryException {
throw new RepositoryException("Not implemented");
}
/**
* - store the typeName
* - store the immediate attributes in the respective IAttributeStore
* - call store on each SuperType.
* @param i
* @throws RepositoryException
*/
void store(ITypedReferenceableInstance i) throws RepositoryException {
}
/**
* - copy over the immediate attribute values from the respective IAttributeStore
* - call load on each SuperType.
* @param i
* @throws RepositoryException
*/
void load(ITypedReferenceableInstance i) throws RepositoryException {
}
}
package org.apache.metadata.storage.memory;
import org.apache.metadata.ITypedInstance;
import org.apache.metadata.storage.RepositoryException;
import org.apache.metadata.storage.StructInstance;
import org.apache.metadata.types.IConstructableType;
public interface IAttributeStore {
/**
* Store the attribute's value from the 'instance' into this store.
* @param pos
* @param instance
* @throws RepositoryException
*/
void store(int pos, IConstructableType type, StructInstance instance) throws RepositoryException;
/**
* load the Instance with the value from position 'pos' for the attribute.
* @param pos
* @param instance
* @throws RepositoryException
*/
void load(int pos, IConstructableType type, StructInstance instance) throws RepositoryException;
/**
* Ensure store have space for the given pos.
* @param pos
* @throws RepositoryException
*/
void ensureCapacity(int pos) throws RepositoryException;
}
......@@ -18,7 +18,13 @@
package org.apache.metadata.storage.memory;
import org.apache.metadata.IInstance;
import org.apache.metadata.IReferenceableInstance;
import org.apache.metadata.ITypedInstance;
import org.apache.metadata.ITypedReferenceableInstance;
import org.apache.metadata.storage.IRepository;
import org.apache.metadata.storage.Id;
import org.apache.metadata.storage.RepositoryException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
......@@ -42,4 +48,45 @@ public class MemRepository implements IRepository {
public boolean allowNullsInCollections() {
return false;
}
/**
* 1. traverse the Object Graph from i and create idToNewIdMap : Map[Id, Id],
* also create old Id to Instance Map: oldIdToInstance : Map[Id, IInstance]
* - traverse reference Attributes, List[ClassType], Maps where Key/value is ClassType
* - traverse Structs
* - traverse Traits.
* 2. Traverse oldIdToInstance map create newInstances : List[ITypedReferenceableInstance]
* - create a ITypedReferenceableInstance.
* replace any old References ( ids or object references) with new Ids.
* 3. Traverse over newInstances
* - ask ClassStore to assign a position to the Id.
* - for Instances with Traits, assign a position for each Trait
* - invoke store on the nwInstance.
*
* Recovery:
* - on each newInstance, invoke releaseId and delete on its ClassStore and Traits' Stores.
*
* @param i
* @return
* @throws RepositoryException
*/
public ITypedReferenceableInstance create(IReferenceableInstance i) throws RepositoryException {
throw new RepositoryException("not implemented");
}
public ITypedReferenceableInstance update(ITypedReferenceableInstance i) throws RepositoryException {
throw new RepositoryException("not implemented");
}
public void delete(ITypedReferenceableInstance i) throws RepositoryException {
throw new RepositoryException("not implemented");
}
public ITypedReferenceableInstance get(Id id) throws RepositoryException {
throw new RepositoryException("not implemented");
}
HierarchicalTypeStore getStore(String typeName) {
return null;
}
}
package org.apache.metadata.storage.memory;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.metadata.storage.RepositoryException;
import org.apache.metadata.storage.StructInstance;
import org.apache.metadata.types.AttributeInfo;
import org.apache.metadata.types.HierarchicalType;
import org.apache.metadata.types.IConstructableType;
import org.apache.metadata.types.StructType;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class StructStore extends AttributeStores.AbstractAttributeStore implements IAttributeStore {
final StructType structType;
final ImmutableMap<AttributeInfo, IAttributeStore> attrStores;
StructStore(AttributeInfo aInfo) throws RepositoryException {
super(aInfo);
this.structType = (StructType) aInfo.dataType();
ImmutableMap.Builder<AttributeInfo, IAttributeStore> b = new ImmutableBiMap.Builder<AttributeInfo,
IAttributeStore>();
Collection<AttributeInfo> l = structType.fieldMapping.fields.values();
for(AttributeInfo i : l) {
b.put(i, AttributeStores.createStore(i) );
}
attrStores = b.build();
}
@Override
public void store(int pos, IConstructableType type, StructInstance instance) throws RepositoryException {
List<String> attrNames = type.getNames(attrInfo);
String attrName = attrNames.get(0);
int nullPos = instance.fieldMapping().fieldNullPos.get(attrName);
int colPos = instance.fieldMapping().fieldPos.get(attrName);
nullList.set(pos, instance.nullFlags[nullPos]);
if ( !instance.nullFlags[nullPos] ) {
StructInstance s = instance.structs[colPos];
for(Map.Entry<AttributeInfo, IAttributeStore> e : attrStores.entrySet()) {
e.getValue().store(pos, structType, s);
}
}
if ( attrNames.size() > 1 ) {
storeHiddenVals(pos, type, instance);
}
}
@Override
public void load(int pos, IConstructableType type, StructInstance instance) throws RepositoryException {
}
@Override
public void ensureCapacity(int pos) throws RepositoryException {
}
}
......@@ -29,6 +29,7 @@ import org.apache.metadata.storage.StructInstance;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class ClassType extends HierarchicalType<ClassType, IReferenceableInstance>
......@@ -36,16 +37,20 @@ public class ClassType extends HierarchicalType<ClassType, IReferenceableInstanc
public static final String TRAIT_NAME_SEP = "::";
public final Map<AttributeInfo, List<String>> infoToNameMap;
/**
* Used when creating a ClassType, to support recursive Structs.
*/
ClassType(TypeSystem typeSystem, String name, ImmutableList<String> superTypes, int numFields) {
super(typeSystem, ClassType.class, name, superTypes, numFields);
infoToNameMap = null;
}
ClassType(TypeSystem typeSystem, String name, ImmutableList<String> superTraits, AttributeInfo... fields)
throws MetadataException {
super(typeSystem, ClassType.class, name, superTraits, fields);
infoToNameMap = TypeUtils.buildAttrInfoToNameMap(fieldMapping);
}
@Override
......@@ -172,4 +177,8 @@ public class ClassType extends HierarchicalType<ClassType, IReferenceableInstanc
fieldMapping.output(s, buf, prefix);
}
@Override
public List<String> getNames(AttributeInfo info) {
return infoToNameMap.get(info);
}
}
\ No newline at end of file
......@@ -34,8 +34,8 @@ import java.util.Map;
public class FieldMapping {
public final Map<String,AttributeInfo> fields;
private final Map<String, Integer> fieldPos;
private final Map<String, Integer> fieldNullPos;
public final Map<String, Integer> fieldPos;
public final Map<String, Integer> fieldNullPos;
public final int numBools;
public final int numBytes;
public final int numShorts;
......@@ -77,120 +77,6 @@ public class FieldMapping {
this.numReferenceables = numReferenceables;
}
public void set(StructInstance s, String attrName, Object val) throws MetadataException {
AttributeInfo i = fields.get(attrName);
if ( i == null ) {
throw new ValueConversionException(s.getTypeName(), val, "Unknown field " + attrName);
}
int pos = fieldPos.get(attrName);
int nullPos = fieldNullPos.get(attrName);
Object cVal = null;
if (val != null && val instanceof Id) {
ClassType clsType =
MetadataService.getCurrentTypeSystem().getDataType(ClassType.class, i.dataType().getName());
clsType.validateId((Id)cVal);
cVal = val;
} else {
cVal = i.dataType().convert(val, i.multiplicity);
}
if ( cVal == null ) {
s.nullFlags[nullPos] = true;
return;
}
s.nullFlags[nullPos] = false;
if ( i.dataType() == DataTypes.BOOLEAN_TYPE ) {
s.bools[pos] = ((Boolean)cVal).booleanValue();
} else if ( i.dataType() == DataTypes.BYTE_TYPE ) {
s.bytes[pos] = ((Byte)cVal).byteValue();
} else if ( i.dataType() == DataTypes.SHORT_TYPE ) {
s.shorts[pos] = ((Short)cVal).shortValue();
} else if ( i.dataType() == DataTypes.INT_TYPE ) {
s.ints[pos] = ((Integer)cVal).intValue();
} else if ( i.dataType() == DataTypes.LONG_TYPE ) {
s.longs[pos] = ((Long)cVal).longValue();
} else if ( i.dataType() == DataTypes.FLOAT_TYPE ) {
s.floats[pos] = ((Float)cVal).floatValue();
} else if ( i.dataType() == DataTypes.DOUBLE_TYPE ) {
s.doubles[pos] = ((Double)cVal).doubleValue();
} else if ( i.dataType() == DataTypes.BIGINTEGER_TYPE ) {
s.bigIntegers[pos] = (BigInteger) cVal;
} else if ( i.dataType() == DataTypes.BIGDECIMAL_TYPE ) {
s.bigDecimals[pos] = (BigDecimal) cVal;
} else if ( i.dataType() == DataTypes.DATE_TYPE ) {
s.dates[pos] = (Date) cVal;
} else if ( i.dataType() == DataTypes.STRING_TYPE ) {
s.strings[pos] = (String) cVal;
} else if ( i.dataType().getTypeCategory() == DataTypes.TypeCategory.ARRAY ) {
s.arrays[pos] = (ImmutableList) cVal;
} else if ( i.dataType().getTypeCategory() == DataTypes.TypeCategory.MAP ) {
s.maps[pos] = (ImmutableMap) cVal;
} else if ( i.dataType().getTypeCategory() == DataTypes.TypeCategory.STRUCT ||
i.dataType().getTypeCategory() == DataTypes.TypeCategory.TRAIT ) {
s.structs[pos] = (StructInstance) cVal;
} else if ( i.dataType().getTypeCategory() == DataTypes.TypeCategory.CLASS ) {
if ( cVal instanceof Id ) {
s.ids[pos] = (Id) cVal;
} else {
s.referenceables[pos] = (ReferenceableInstance) cVal;
}
} else {
throw new MetadataException(String.format("Unknown datatype %s", i.dataType()));
}
}
public Object get(StructInstance s, String attrName) throws MetadataException {
AttributeInfo i = fields.get(attrName);
if ( i == null ) {
throw new MetadataException(String.format("Unknown field %s for Struct %s", attrName, s.getTypeName()));
}
int pos = fieldPos.get(attrName);
int nullPos = fieldNullPos.get(attrName);
if ( s.nullFlags[nullPos]) {
return null;
}
if ( i.dataType() == DataTypes.BOOLEAN_TYPE ) {
return s.bools[pos];
} else if ( i.dataType() == DataTypes.BYTE_TYPE ) {
return s.bytes[pos];
} else if ( i.dataType() == DataTypes.SHORT_TYPE ) {
return s.shorts[pos];
} else if ( i.dataType() == DataTypes.INT_TYPE ) {
return s.ints[pos];
} else if ( i.dataType() == DataTypes.LONG_TYPE ) {
return s.longs[pos];
} else if ( i.dataType() == DataTypes.FLOAT_TYPE ) {
return s.floats[pos];
} else if ( i.dataType() == DataTypes.DOUBLE_TYPE ) {
return s.doubles[pos];
} else if ( i.dataType() == DataTypes.BIGINTEGER_TYPE ) {
return s.bigIntegers[pos];
} else if ( i.dataType() == DataTypes.BIGDECIMAL_TYPE ) {
return s.bigDecimals[pos];
} else if ( i.dataType() == DataTypes.DATE_TYPE ) {
return s.dates[pos];
} else if ( i.dataType() == DataTypes.STRING_TYPE ) {
return s.strings[pos];
} else if ( i.dataType().getTypeCategory() == DataTypes.TypeCategory.ARRAY ) {
return s.arrays[pos];
} else if ( i.dataType().getTypeCategory() == DataTypes.TypeCategory.MAP ) {
return s.maps[pos];
} else if ( i.dataType().getTypeCategory() == DataTypes.TypeCategory.STRUCT ||
i.dataType().getTypeCategory() == DataTypes.TypeCategory.TRAIT ) {
return s.structs[pos];
} else if ( i.dataType().getTypeCategory() == DataTypes.TypeCategory.CLASS ) {
if ( s.ids[pos] != null ) {
return s.ids[pos];
} else {
return s.referenceables[pos];
}
} else {
throw new MetadataException(String.format("Unknown datatype %s", i.dataType()));
}
}
protected void outputFields(IStruct s, Appendable buf, String fieldPrefix) throws MetadataException {
for(Map.Entry<String,AttributeInfo> e : fields.entrySet()) {
String attrName = e.getKey();
......
......@@ -41,7 +41,7 @@ public abstract class HierarchicalType<ST extends HierarchicalType,T> extends Ab
public final FieldMapping fieldMapping;
public final int numFields;
public final ImmutableList<String> superTypes;
protected final ImmutableList<AttributeInfo> immediateAttrs;
public final ImmutableList<AttributeInfo> immediateAttrs;
protected ImmutableMap<String, List<Path>> superTypePaths;
protected ImmutableMap<String, Path> pathNameToPathMap;
......@@ -330,6 +330,10 @@ public abstract class HierarchicalType<ST extends HierarchicalType,T> extends Ab
}
}
public Set<String> getAllSuperTypeNames() {
return superTypePaths.keySet();
}
public Iterator<Path> pathIterator() {
return new PathItr();
}
......
......@@ -4,8 +4,11 @@ package org.apache.metadata.types;
import org.apache.metadata.ITypedInstance;
import org.apache.metadata.MetadataException;
import java.util.List;
public interface IConstructableType<U, T extends ITypedInstance> extends IDataType<U> {
T createInstance() throws MetadataException;
FieldMapping fieldMapping();
List<String> getNames(AttributeInfo info);
}
......@@ -35,6 +35,7 @@ public class StructType extends AbstractDataType<IStruct>
public final TypeSystem typeSystem;
public final String name;
public final FieldMapping fieldMapping;
public final Map<AttributeInfo, List<String>> infoToNameMap;
public final int numFields;
private final TypedStructHandler handler;
......@@ -45,6 +46,7 @@ public class StructType extends AbstractDataType<IStruct>
this.typeSystem = typeSystem;
this.name = name;
this.fieldMapping = null;
infoToNameMap = null;
this.numFields = numFields;
this.handler = null;
}
......@@ -55,6 +57,7 @@ public class StructType extends AbstractDataType<IStruct>
this.name = name;
this.fieldMapping = constructFieldMapping(superTypes,
fields);
infoToNameMap = TypeUtils.buildAttrInfoToNameMap(this.fieldMapping);
this.numFields = this.fieldMapping.fields.size();
this.handler = new TypedStructHandler(this);
}
......@@ -188,4 +191,8 @@ public class StructType extends AbstractDataType<IStruct>
public void output(IStruct s, Appendable buf, String prefix) throws MetadataException {
handler.output(s, buf, prefix);
}
public List<String> getNames(AttributeInfo info) {
return infoToNameMap.get(info);
}
}
......@@ -23,10 +23,14 @@ import org.apache.metadata.IStruct;
import org.apache.metadata.ITypedStruct;
import org.apache.metadata.MetadataException;
import java.util.List;
import java.util.Map;
public class TraitType extends HierarchicalType<TraitType, IStruct>
implements IConstructableType<IStruct, ITypedStruct> {
private final TypedStructHandler handler;
public final Map<AttributeInfo, List<String>> infoToNameMap;
/**
* Used when creating a TraitType, to support recursive Structs.
......@@ -34,12 +38,14 @@ public class TraitType extends HierarchicalType<TraitType, IStruct>
TraitType(TypeSystem typeSystem, String name, ImmutableList<String> superTraits, int numFields) {
super(typeSystem, TraitType.class, name, superTraits, numFields);
handler = null;
infoToNameMap = null;
}
TraitType(TypeSystem typeSystem, String name, ImmutableList<String> superTraits, AttributeInfo... fields)
throws MetadataException {
super(typeSystem, TraitType.class, name, superTraits, fields);
handler = new TypedStructHandler(this);
infoToNameMap = TypeUtils.buildAttrInfoToNameMap(fieldMapping);
}
@Override
......@@ -61,4 +67,9 @@ public class TraitType extends HierarchicalType<TraitType, IStruct>
handler.output(s, buf, prefix);
}
@Override
public List<String> getNames(AttributeInfo info) {
return infoToNameMap.get(info);
}
}
......@@ -17,9 +17,14 @@
*/
package org.apache.metadata.types;
import com.google.common.collect.ImmutableMap;
import org.apache.metadata.MetadataException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -56,4 +61,17 @@ public class TypeUtils {
return m.matches() ? new String[] {m.group(1), m.group(2)} : null;
}
public static Map<AttributeInfo, List<String>> buildAttrInfoToNameMap(FieldMapping f) {
Map<AttributeInfo, List<String>> b = new HashMap<AttributeInfo, List<String>>();
for(Map.Entry<String, AttributeInfo> e : f.fields.entrySet()) {
List<String> names = b.get(e.getValue());
if ( names == null ) {
names = new ArrayList<String>();
b.put(e.getValue(), names);
}
names.add(e.getKey());
}
return ImmutableMap.copyOf(b);
}
}
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