Commit e1df983c by Harish Butani

Introduce FieldMapping to get ready for Trait and Class Types

parent 1a3dc0e2
......@@ -5,14 +5,17 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.metadata.IStruct;
import org.apache.metadata.MetadataException;
import org.apache.metadata.types.StructType;
import org.apache.metadata.types.AttributeInfo;
import org.apache.metadata.types.FieldMapping;
import org.apache.metadata.types.TypeUtils;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
public class TypedStruct implements IStruct {
public final StructType dataType;
public class StructInstance implements IStruct {
public final String dataTypeName;
public final FieldMapping fieldMapping;
public final boolean nullFlags[];
public final boolean[] bools;
public final byte[] bytes;
......@@ -27,14 +30,16 @@ public class TypedStruct implements IStruct {
public final String[] strings;
public final ImmutableList<Object>[] arrays;
public final ImmutableMap<Object,Object>[] maps;
public final TypedStruct[] structs;
public final StructInstance[] structs;
public TypedStruct(StructType dataType, boolean[] nullFlags, boolean[] bools, byte[] bytes, short[] shorts, int[] ints,
public StructInstance(String dataTypeName, FieldMapping fieldMapping,
boolean[] nullFlags, boolean[] bools, byte[] bytes, short[] shorts, int[] ints,
long[] longs, float[] floats, double[] doubles,
BigDecimal[] bigDecimals, BigInteger[] bigIntegers, Date[] dates, String[] strings,
ImmutableList<Object>[] arrays, ImmutableMap<Object, Object>[] maps, TypedStruct[] structs) {
assert dataType != null;
this.dataType = dataType;
ImmutableList<Object>[] arrays, ImmutableMap<Object, Object>[] maps, StructInstance[] structs) {
assert dataTypeName != null;
this.dataTypeName = dataTypeName;
this.fieldMapping = fieldMapping;
this.nullFlags = nullFlags;
this.bools = bools;
this.bytes = bytes;
......@@ -58,25 +63,54 @@ public class TypedStruct implements IStruct {
@Override
public String getTypeName() {
return dataType.getName();
return dataTypeName;
}
@Override
public Object get(String attrName) throws MetadataException {
return dataType.get(this, attrName);
return fieldMapping.get(this, attrName);
}
@Override
public void set(String attrName, Object val) throws MetadataException {
dataType.set(this, attrName, val);
fieldMapping.set(this, attrName, val);
}
public void output(IStruct s, Appendable buf, String prefix) throws MetadataException {
TypeUtils.outputVal("{", buf, prefix);
if ( s == null ) {
TypeUtils.outputVal("<null>\n", buf, "");
return;
}
TypeUtils.outputVal("\n", buf, "");
String fieldPrefix = prefix + "\t";
for(AttributeInfo i : fieldMapping.fields.values()) {
Object aVal = s.get(i.name);
TypeUtils.outputVal(i.name + " : ", buf, fieldPrefix);
i.dataType().output(aVal, buf, "");
TypeUtils.outputVal("\n", buf, "");
}
TypeUtils.outputVal("\n}\n", buf, "");
}
@Override
public String toString() {
try {
StringBuilder b = new StringBuilder();
dataType.output(this, b, "");
return b.toString();
StringBuilder buf = new StringBuilder();
String prefix = "";
TypeUtils.outputVal("{", buf, prefix);
TypeUtils.outputVal("\n", buf, "");
String fieldPrefix = prefix + "\t";
for(AttributeInfo i : fieldMapping.fields.values()) {
Object aVal = get(i.name);
TypeUtils.outputVal(i.name + " : ", buf, fieldPrefix);
i.dataType().output(aVal, buf, "");
TypeUtils.outputVal("\n", buf, "");
}
TypeUtils.outputVal("\n}\n", buf, "");
return buf.toString();
} catch(MetadataException me) {
throw new RuntimeException(me);
}
......
......@@ -13,18 +13,9 @@ abstract class AbstractDataType<T> implements IDataType<T> {
return null;
}
protected void outputVal(String val, Appendable buf, String prefix) throws MetadataException {
try {
buf.append(prefix).append(val);
} catch(IOException ie) {
throw new MetadataException(ie);
}
}
@Override
public void output(T val, Appendable buf, String prefix) throws MetadataException {
outputVal(val == null ? "<null>" : val.toString(), buf, prefix);
TypeUtils.outputVal(val == null ? "<null>" : val.toString(), buf, prefix);
}
}
package org.apache.metadata.types;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.metadata.MetadataException;
import org.apache.metadata.storage.StructInstance;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
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 int numBools;
public final int numBytes;
public final int numShorts;
public final int numInts;
public final int numLongs;
public final int numFloats;
public final int numDoubles;
public final int numBigInts;
public final int numBigDecimals;
public final int numDates;
public final int numStrings;
public final int numArrays;
public final int numMaps;
public final int numStructs;
public FieldMapping(Map<String, AttributeInfo> fields, Map<String, Integer> fieldPos,
Map<String, Integer> fieldNullPos, int numBools, int numBytes, int numShorts,
int numInts, int numLongs, int numFloats, int numDoubles, int numBigInts, int numBigDecimals,
int numDates, int numStrings, int numArrays, int numMaps, int numStructs) {
this.fields = fields;
this.fieldPos = fieldPos;
this.fieldNullPos = fieldNullPos;
this.numBools = numBools;
this.numBytes = numBytes;
this.numShorts = numShorts;
this.numInts = numInts;
this.numLongs = numLongs;
this.numFloats = numFloats;
this.numDoubles = numDoubles;
this.numBigInts = numBigInts;
this.numBigDecimals = numBigDecimals;
this.numDates = numDates;
this.numStrings = numStrings;
this.numArrays = numArrays;
this.numMaps = numMaps;
this.numStructs = numStructs;
}
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 = 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 ) {
s.structs[pos] = (StructInstance) 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 ) {
return s.structs[pos];
} else {
throw new MetadataException(String.format("Unknown datatype %s", i.dataType()));
}
}
}
......@@ -4,11 +4,9 @@ package org.apache.metadata.types;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.metadata.IStruct;
import org.apache.metadata.storage.IRepository;
import org.apache.metadata.MetadataException;
import org.apache.metadata.MetadataService;
import org.apache.metadata.Struct;
import org.apache.metadata.storage.TypedStruct;
import org.apache.metadata.storage.StructInstance;
import java.math.BigDecimal;
import java.math.BigInteger;
......@@ -17,53 +15,32 @@ import java.util.*;
public class StructType extends AbstractDataType<IStruct> {
public final String name;
public final Map<String,AttributeInfo> fields;
private final Map<String, Integer> fieldPos;
private final Map<String, Integer> fieldNullPos;
public final int numBools;
public final int numBytes;
public final int numShorts;
public final int numInts;
public final int numLongs;
public final int numFloats;
public final int numDoubles;
public final int numBigInts;
public final int numBigDecimals;
public final int numDates;
public final int numStrings;
public final int numArrays;
public final int numMaps;
public final int numStructs;
public final FieldMapping fieldMapping;
/**
* Used when creating a StructType, to support recursive Structs.
*/
StructType(String name) {
this.name = name;
this.fields = new LinkedHashMap<String, AttributeInfo>();
this.fieldPos = new HashMap<String, Integer>();
fieldNullPos = new HashMap<String, Integer>();
numBools = 0;
numBytes = 0;
numShorts = 0;
numInts = 0;
numLongs = 0;
numFloats = 0;
numDoubles = 0;
numBigInts = 0;
numBigDecimals = 0;
numDates = 0;
numStrings = 0;
numArrays = 0;
numMaps = 0;
numStructs = 0;
this.fieldMapping = null;
}
StructType(String name, AttributeInfo... fields) throws MetadataException {
this.name = name;
this.fields = new LinkedHashMap<String, AttributeInfo>();
this.fieldPos = new HashMap<String, Integer>();
fieldNullPos = new HashMap<String, Integer>();
this.fieldMapping = constructFieldMapping(fields);
}
@Override
public String getName() {
return name;
}
protected FieldMapping constructFieldMapping(AttributeInfo... fields)
throws MetadataException {
Map<String,AttributeInfo> fieldsMap = new LinkedHashMap<String, AttributeInfo>();
Map<String, Integer> fieldPos = new HashMap<String, Integer>();
Map<String, Integer> fieldNullPos = new HashMap<String, Integer>();
int numBools = 0;
int numBytes = 0;
int numShorts = 0;
......@@ -78,13 +55,14 @@ public class StructType extends AbstractDataType<IStruct> {
int numArrays = 0;
int numMaps = 0;
int numStructs = 0;
for(AttributeInfo i : fields) {
if ( this.fields.containsKey(i.name) ) {
if ( fieldsMap.containsKey(i.name) ) {
throw new MetadataException(
String.format("Struct defintion cannot contain multiple fields with the same name %s", i.name));
}
this.fields.put(i.name, i);
this.fieldNullPos.put(i.name, fieldNullPos.size());
fieldsMap.put(i.name, i);
fieldNullPos.put(i.name, fieldNullPos.size());
if ( i.dataType() == DataTypes.BOOLEAN_TYPE ) {
fieldPos.put(i.name, numBools);
numBools++;
......@@ -131,47 +109,47 @@ public class StructType extends AbstractDataType<IStruct> {
throw new MetadataException(String.format("Unknown datatype %s", i.dataType()));
}
}
this.numBools = numBools;
this.numBytes = numBytes;
this.numShorts = numShorts;
this.numInts = numInts;
this.numLongs = numLongs;
this.numFloats = numFloats;
this.numDoubles = numDoubles;
this.numBigInts = numBigInts;
this.numBigDecimals = numBigDecimals;
this.numDates = numDates;
this.numStrings = numStrings;
this.numArrays = numArrays;
this.numMaps = numMaps;
this.numStructs = numStructs;
}
@Override
public String getName() {
return name;
return new FieldMapping(fieldsMap,
fieldPos,
fieldNullPos,
numBools,
numBytes,
numShorts,
numInts,
numLongs,
numFloats,
numDoubles,
numBigInts,
numBigDecimals,
numDates,
numStrings,
numArrays,
numMaps,
numStructs);
}
@Override
public TypedStruct convert(Object val, Multiplicity m) throws MetadataException {
public StructInstance convert(Object val, Multiplicity m) throws MetadataException {
if ( val != null ) {
if ( val instanceof Struct ) {
Struct s = (Struct) val;
if ( s.typeName != name ) {
throw new ValueConversionException(this, val);
}
TypedStruct ts = createInstance();
for(AttributeInfo i : fields.values()) {
StructInstance ts = createInstance();
for(AttributeInfo i : fieldMapping.fields.values()) {
Object aVal = s.get(i.name);
try {
set(ts, i.name, aVal);
ts.set(i.name, aVal);
} catch(ValueConversionException ve) {
throw new ValueConversionException(this, val, ve);
}
}
return ts;
} else if ( val instanceof TypedStruct && ((TypedStruct)val).dataType == this ) {
return (TypedStruct) val;
} else if ( val instanceof StructInstance && ((StructInstance)val).getTypeName() == getName() ) {
return (StructInstance) val;
} else {
throw new ValueConversionException(this, val);
}
......@@ -187,131 +165,41 @@ public class StructType extends AbstractDataType<IStruct> {
return DataTypes.TypeCategory.STRUCT;
}
public TypedStruct createInstance() {
return new TypedStruct(this,
new boolean[fields.size()],
numBools == 0 ? null : new boolean[numBools],
numBytes == 0 ? null : new byte[numBytes],
numShorts == 0 ? null : new short[numShorts],
numInts == 0 ? null : new int[numInts],
numLongs == 0 ? null : new long[numLongs],
numFloats == 0 ? null : new float[numFloats],
numDoubles == 0 ? null : new double[numDoubles],
numBigDecimals == 0 ? null : new BigDecimal[numBigDecimals],
numBigInts == 0 ? null : new BigInteger[numBigInts],
numDates == 0 ? null : new Date[numDates],
numStrings == 0 ? null : new String[numStrings],
numArrays == 0 ? null : new ImmutableList[numArrays],
numMaps == 0 ? null : new ImmutableMap[numMaps],
numStructs == 0 ? null : new TypedStruct[numStructs]);
}
public void set(TypedStruct s, String attrName, Object val) throws MetadataException {
AttributeInfo i = fields.get(attrName);
if ( i == null ) {
throw new ValueConversionException(this, val, "Unknown field " + attrName);
}
int pos = fieldPos.get(attrName);
int nullPos = fieldNullPos.get(attrName);
Object 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 ) {
s.structs[pos] = (TypedStruct) cVal;
} else {
throw new MetadataException(String.format("Unknown datatype %s", i.dataType()));
}
}
public Object get(TypedStruct 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, this.getName()));
}
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 ) {
return s.structs[pos];
} else {
throw new MetadataException(String.format("Unknown datatype %s", i.dataType()));
}
public StructInstance createInstance() {
return new StructInstance(getName(),
fieldMapping,
new boolean[fieldMapping.fields.size()],
fieldMapping.numBools == 0 ? null : new boolean[fieldMapping.numBools],
fieldMapping.numBytes == 0 ? null : new byte[fieldMapping.numBytes],
fieldMapping.numShorts == 0 ? null : new short[fieldMapping.numShorts],
fieldMapping.numInts == 0 ? null : new int[fieldMapping.numInts],
fieldMapping.numLongs == 0 ? null : new long[fieldMapping.numLongs],
fieldMapping.numFloats == 0 ? null : new float[fieldMapping.numFloats],
fieldMapping.numDoubles == 0 ? null : new double[fieldMapping.numDoubles],
fieldMapping.numBigDecimals == 0 ? null : new BigDecimal[fieldMapping.numBigDecimals],
fieldMapping.numBigInts == 0 ? null : new BigInteger[fieldMapping.numBigInts],
fieldMapping.numDates == 0 ? null : new Date[fieldMapping.numDates],
fieldMapping.numStrings == 0 ? null : new String[fieldMapping.numStrings],
fieldMapping.numArrays == 0 ? null : new ImmutableList[fieldMapping.numArrays],
fieldMapping.numMaps == 0 ? null : new ImmutableMap[fieldMapping.numMaps],
fieldMapping.numStructs == 0 ? null : new StructInstance[fieldMapping.numStructs]);
}
@Override
public void output(IStruct s, Appendable buf, String prefix) throws MetadataException {
outputVal("{", buf, prefix);
TypeUtils.outputVal("{", buf, prefix);
if ( s == null ) {
outputVal("<null>\n", buf, "");
TypeUtils.outputVal("<null>\n", buf, "");
return;
}
outputVal("\n", buf, "");
TypeUtils.outputVal("\n", buf, "");
String fieldPrefix = prefix + "\t";
for(AttributeInfo i : fields.values()) {
for(AttributeInfo i : fieldMapping.fields.values()) {
Object aVal = s.get(i.name);
outputVal(i.name + " : ", buf, fieldPrefix);
TypeUtils.outputVal(i.name + " : ", buf, fieldPrefix);
i.dataType().output(aVal, buf, "");
outputVal("\n", buf, "");
TypeUtils.outputVal("\n", buf, "");
}
outputVal("\n}\n", buf, "");
TypeUtils.outputVal("\n}\n", buf, "");
}
}
package org.apache.metadata.types;
import org.apache.metadata.MetadataException;
import java.io.IOException;
public class TypeUtils {
public static void outputVal(String val, Appendable buf, String prefix) throws MetadataException {
try {
buf.append(prefix).append(val);
} catch(IOException ie) {
throw new MetadataException(ie);
}
}
}
......@@ -17,6 +17,11 @@ public class ValueConversionException extends MetadataException {
val.toString(), typ.getName(), msg));
}
public ValueConversionException(String typeName, Object val, String msg) {
super(String.format("Cannot convert value '%s' to datatype %s because: %s",
val.toString(), typeName, msg));
}
protected ValueConversionException(String msg) {
super(msg);
}
......
package org.apache.metadata.dsl
import org.apache.metadata.storage.TypedStruct
import org.apache.metadata.MetadataService
import org.apache.metadata.storage.StructInstance
import org.apache.metadata.types.TypeSystem
import scala.language.dynamics
class DynamicTypedStruct(val ts : TypedStruct) extends Dynamic {
class DynamicTypedStruct(val ts : StructInstance) extends Dynamic {
def selectDynamic(name: String) = ts.get(name)
def updateDynamic(name: String)(value: Any) {
var value1 = value
......@@ -12,5 +14,5 @@ class DynamicTypedStruct(val ts : TypedStruct) extends Dynamic {
}
ts.set(name, value1)
}
def dataType = ts.dataType
def dataType = MetadataService.getCurrentTypeSystem.getDataType(ts.getTypeName)
}
package org.apache.metadata
import org.apache.metadata.json.{BigIntegerSerializer, BigDecimalSerializer, TypedStructSerializer, Serialization}
import org.apache.metadata.storage.TypedStruct
import org.apache.metadata.storage.StructInstance
import org.apache.metadata.types._
import scala.collection.JavaConverters._
......@@ -59,7 +59,7 @@ package object dsl {
assert(j.isInstanceOf[JObject])
var j1 = j.asInstanceOf[JObject]
j1 = JObject(JField(Serialization.STRUCT_TYPE_FIELD_NAME, JString(typeName)) :: j1.obj)
new DynamicTypedStruct(Extraction.extract[TypedStruct](j1))
new DynamicTypedStruct(Extraction.extract[StructInstance](j1))
}
def createInstance(typeName : String) = {
......
......@@ -9,7 +9,7 @@ import org.json4s.native.Serialization.{read, write => swrite}
import org.json4s.reflect.{ScalaType, Reflector}
import java.util.regex.Pattern
import java.util.Date
import org.apache.metadata.storage.TypedStruct
import org.apache.metadata.storage.StructInstance
import collection.JavaConversions._
import scala.collection.JavaConverters._
......@@ -27,7 +27,7 @@ class BigIntegerSerializer extends CustomSerializer[java.math.BigInteger](format
}
))
class TypedStructSerializer extends Serializer[TypedStruct] {
class TypedStructSerializer extends Serializer[StructInstance] {
def extractList(lT : ArrayType, value : JArray)(implicit format: Formats) : Any = {
val dT = lT.getElemType
......@@ -51,11 +51,11 @@ class TypedStructSerializer extends Serializer[TypedStruct] {
case value : JObject if dT.getTypeCategory eq TypeCategory.MAP =>
extractMap(dT.asInstanceOf[MapType], value.asInstanceOf[JObject])
case value : JObject =>
Extraction.extract[TypedStruct](value)
Extraction.extract[StructInstance](value)
}
def deserialize(implicit format: Formats) = {
case (TypeInfo(clazz, ptype), json) if classOf[TypedStruct].isAssignableFrom(clazz) => json match {
case (TypeInfo(clazz, ptype), json) if classOf[StructInstance].isAssignableFrom(clazz) => json match {
case JObject(fs) =>
val(typ, fields) = fs.partition(f => f._1 == Serialization.STRUCT_TYPE_FIELD_NAME)
val typName = typ(0)._2.asInstanceOf[JString].s
......@@ -63,7 +63,7 @@ class TypedStructSerializer extends Serializer[TypedStruct] {
val s = sT.createInstance()
fields.foreach { f =>
val fName = f._1
val fInfo = sT.fields(fName)
val fInfo = sT.fieldMapping.fields(fName)
if ( fInfo != null ) {
//println(fName)
var v = f._2
......@@ -91,8 +91,8 @@ class TypedStructSerializer extends Serializer[TypedStruct] {
//implicit def javaBigInteger2bigInt(x: java.math.BigInteger): BigInt = new BigInt(x)
def serialize(implicit format: Formats) = {
case e: TypedStruct =>
val fields = e.dataType.fields.map {
case e: StructInstance =>
val fields = e.fieldMapping.fields.map {
case (fName, info) => {
var v = e.get(fName)
if ( v != null && (info.dataType().getTypeCategory eq TypeCategory.MAP) ) {
......@@ -101,7 +101,7 @@ class TypedStructSerializer extends Serializer[TypedStruct] {
JField(fName, Extraction.decompose(v))
}
}.toList.map(_.asInstanceOf[JField])
JObject(JField(Serialization.STRUCT_TYPE_FIELD_NAME, JString(e.dataType.getName)) :: fields)
JObject(JField(Serialization.STRUCT_TYPE_FIELD_NAME, JString(e.dataTypeName)) :: fields)
}
}
......
package org.apache.metadata;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import junit.framework.TestCase;
import org.apache.metadata.storage.TypedStruct;
import org.apache.metadata.storage.StructInstance;
import org.apache.metadata.types.*;
import org.junit.Before;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.Map;
public class StructTest extends BaseTest {
StructType structType;
......@@ -28,7 +20,7 @@ public class StructTest extends BaseTest {
@Test
public void test1() throws MetadataException {
Struct s = createStruct(ms);
TypedStruct ts = structType.convert(s, Multiplicity.REQUIRED);
StructInstance ts = structType.convert(s, Multiplicity.REQUIRED);
System.out.println(ts);
}
......@@ -39,7 +31,7 @@ public class StructTest extends BaseTest {
Struct s2 = new Struct(recursiveStructType.getName());
s2.set("a", 1);
s2.set("s", s1);
TypedStruct ts = recursiveStructType.convert(s2, Multiplicity.REQUIRED);
StructInstance ts = recursiveStructType.convert(s2, Multiplicity.REQUIRED);
System.out.println(ts);
}
......
......@@ -2,7 +2,7 @@ package org.apache.metadata.dsl
import org.apache.metadata.hive.HiveMockMetadataService
import org.apache.metadata.json.{BigIntegerSerializer, BigDecimalSerializer, TypedStructSerializer}
import org.apache.metadata.storage.TypedStruct
import org.apache.metadata.storage.StructInstance
import org.apache.metadata.{Struct, BaseTest}
import org.apache.metadata.types.{IDataType, Multiplicity, StructType}
import org.json4s.NoTypeHints
......
package org.apache.metadata.json
import org.apache.metadata.Struct
import org.apache.metadata.storage.TypedStruct
import org.apache.metadata.storage.TypedStruct
import org.apache.metadata.storage.StructInstance
import org.apache.metadata.storage.StructInstance
import org.apache.metadata.types.Multiplicity
import org.apache.metadata.types.StructType
import org.apache.metadata.{Struct, BaseTest}
......@@ -29,7 +29,7 @@ class SerializationTest extends BaseTest {
@Test def test1 {
val s: Struct = BaseTest.createStruct(ms)
val ts: TypedStruct = structType.convert(s, Multiplicity.REQUIRED)
val ts: StructInstance = structType.convert(s, Multiplicity.REQUIRED)
println("Typed Struct :")
println(ts)
......@@ -41,19 +41,19 @@ class SerializationTest extends BaseTest {
println("Json representation :")
println(ser)
val ts1 = read[TypedStruct](ser)
val ts1 = read[StructInstance](ser)
println("Typed Struct read back:")
println(ts1)
}
@Test def test2 {
val s: Struct = BaseTest.createStruct(ms)
val ts: TypedStruct = structType.convert(s, Multiplicity.REQUIRED)
val ts: StructInstance = structType.convert(s, Multiplicity.REQUIRED)
implicit val formats = org.json4s.native.Serialization.formats(NoTypeHints) + new TypedStructSerializer +
new BigDecimalSerializer + new BigIntegerSerializer
val ts1 = read[TypedStruct](
val ts1 = read[StructInstance](
"""
{"$typeName$":"t1","e":1,"n":[1.1,1.1],"h":1.0,"b":true,"k":1,"j":1,"d":2,"m":[1,1],"g":1,"a":1,"i":1.0,
"c":1,"l":"2014-12-03T19:38:55.053Z","f":1,"o":{"b":2.0,"a":1.0}}""")
......
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