You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@atlas.apache.org by ma...@apache.org on 2017/11/12 18:14:19 UTC
[10/42] atlas git commit: ATLAS-2251: Remove TypeSystem and related
implementation, to avoid unncessary duplicate of type details in cache
http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/main/java/org/apache/atlas/typesystem/types/DataTypes.java
----------------------------------------------------------------------
diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/DataTypes.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/DataTypes.java
deleted file mode 100755
index f9f4abe..0000000
--- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/DataTypes.java
+++ /dev/null
@@ -1,655 +0,0 @@
-/**
- * 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.atlas.typesystem.types;
-
-import com.google.common.collect.ImmutableCollection;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-import org.apache.atlas.AtlasException;
-import org.apache.atlas.typesystem.IReferenceableInstance;
-import org.apache.atlas.typesystem.persistence.Id;
-import org.apache.commons.lang3.StringUtils;
-import org.joda.time.DateTime;
-import org.joda.time.DateTimeZone;
-import org.joda.time.format.DateTimeFormatter;
-import org.joda.time.format.ISODateTimeFormat;
-
-import java.math.BigDecimal;
-import java.math.BigInteger;
-import java.nio.charset.Charset;
-import java.security.MessageDigest;
-import java.util.Collection;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-public class DataTypes {
-
- public static BooleanType BOOLEAN_TYPE = new BooleanType();
- public static ByteType BYTE_TYPE = new ByteType();
- public static ShortType SHORT_TYPE = new ShortType();
- public static IntType INT_TYPE = new IntType();
- public static LongType LONG_TYPE = new LongType();
- public static FloatType FLOAT_TYPE = new FloatType();
- public static DoubleType DOUBLE_TYPE = new DoubleType();
- public static BigIntegerType BIGINTEGER_TYPE = new BigIntegerType();
- public static BigDecimalType BIGDECIMAL_TYPE = new BigDecimalType();
- public static DateType DATE_TYPE = new DateType();
- public static StringType STRING_TYPE = new StringType();
- public static String ARRAY_TYPE_PREFIX = "array<";
- static String ARRAY_TYPE_SUFFIX = ">";
- public static String MAP_TYPE_PREFIX = "map<";
- static String MAP_TYPE_SUFFIX = ">";
-
- public static String arrayTypeName(String elemTypeName) {
- assert elemTypeName != null;
- return String.format("%s%s%s", ARRAY_TYPE_PREFIX, elemTypeName, ARRAY_TYPE_SUFFIX);
- }
-
- public static String arrayTypeName(IDataType elemType) {
- return arrayTypeName(elemType.getName());
- }
-
- public static String mapTypeName(String keyTypeName, String valueTypeName) {
- return String.format("%s%s,%s%s", MAP_TYPE_PREFIX, keyTypeName, valueTypeName, MAP_TYPE_SUFFIX);
- }
-
- public static String mapTypeName(IDataType keyType, IDataType valueType) {
- assert keyType != null;
- assert valueType != null;
- return mapTypeName(keyType.getName(), valueType.getName());
- }
-
- public enum TypeCategory {
- PRIMITIVE,
- ENUM,
- ARRAY,
- MAP,
- STRUCT,
- TRAIT,
- CLASS,
- RELATIONSHIP
- }
-
- public static abstract class PrimitiveType<T> extends AbstractDataType<T> {
- public PrimitiveType(String name, String description) {
- super(name, description);
- }
-
- @Override
- public TypeCategory getTypeCategory() {
- return TypeCategory.PRIMITIVE;
- }
-
- public abstract T nullValue();
-
- @Override
- protected T convertNull(Multiplicity m) throws AtlasException {
- if (!m.nullAllowed()) {
- throw new ValueConversionException.NullConversionException(m);
- }
-
- return nullValue();
- }
-
- @Override
- public void updateSignatureHash(MessageDigest digester, Object val) throws AtlasException {
- if ( val != null ) {
- digester.update(val.toString().getBytes(Charset.forName("UTF-8")));
- }
- }
-
- }
-
- public static class BooleanType extends PrimitiveType<Boolean> {
-
- private static final String name = "boolean".intern();
-
- private BooleanType() {
- super(name, null);
- }
-
- @Override
- public Boolean convert(Object val, Multiplicity m) throws AtlasException {
- if (val != null) {
- if (val instanceof Boolean) {
- return (Boolean) val;
- } else if (val instanceof String) {
- return Boolean.parseBoolean((String) val);
- } else if (val instanceof Number) {
- return ((Number) val).intValue() != 0;
- } else {
- throw new ValueConversionException(this, val);
- }
- }
- return convertNull(m);
- }
-
- public Boolean nullValue() {
- return Boolean.FALSE;
- }
- }
-
- public static class ByteType extends PrimitiveType<Byte> {
-
- private static final String name = "byte".intern();
-
- private ByteType() {
- super(name, null);
- }
-
- @Override
- public Byte convert(Object val, Multiplicity m) throws AtlasException {
- if (val != null) {
- if (val instanceof Byte) {
- return (Byte) val;
- } else if (val instanceof String) {
- return Byte.parseByte((String) val);
- } else if (val instanceof Number) {
- return ((Number) val).byteValue();
- } else {
- throw new ValueConversionException(this, val);
- }
- }
- return convertNull(m);
- }
-
- public Byte nullValue() {
- return 0;
- }
-
- @Override
- public void updateSignatureHash(MessageDigest digester, Object val) throws AtlasException {
- if ( val != null ) {
- digester.update((Byte) val);
- }
- }
- }
-
- public static class ShortType extends PrimitiveType<Short> {
-
- private static final String name = "short".intern();
-
- private ShortType() {
- super(name, null);
- }
-
- @Override
- public Short convert(Object val, Multiplicity m) throws AtlasException {
- if (val != null) {
- if (val instanceof Short) {
- return (Short) val;
- } else if (val instanceof String) {
- return Short.parseShort((String) val);
- } else if (val instanceof Number) {
- return ((Number) val).shortValue();
- } else {
- throw new ValueConversionException(this, val);
- }
- }
- return convertNull(m);
- }
-
- public Short nullValue() {
- return 0;
- }
- }
-
- public static class IntType extends PrimitiveType<Integer> {
-
- private static final String name = "int".intern();
-
- private IntType() {
- super(name, null);
- }
-
- @Override
- public Integer convert(Object val, Multiplicity m) throws AtlasException {
- if (val != null) {
- if (val instanceof Integer) {
- return (Integer) val;
- } else if (val instanceof String) {
- return Integer.parseInt((String) val);
- } else if (val instanceof Number) {
- return ((Number) val).intValue();
- } else {
- throw new ValueConversionException(this, val);
- }
- }
- return convertNull(m);
- }
-
- public Integer nullValue() {
- return 0;
- }
- }
-
- public static class LongType extends PrimitiveType<Long> {
-
- private static final String name = "long".intern();
-
- private LongType() {
- super(name, null);
- }
-
- @Override
- public Long convert(Object val, Multiplicity m) throws AtlasException {
- if (val != null) {
- if (val instanceof Long) {
- return (Long) val;
- } else if (val instanceof String) {
- return Long.parseLong((String) val);
- } else if (val instanceof Number) {
- return ((Number) val).longValue();
- } else {
- throw new ValueConversionException(this, val);
- }
- }
- return convertNull(m);
- }
-
- public Long nullValue() {
- return 0L;
- }
- }
-
- public static class FloatType extends PrimitiveType<Float> {
-
- private static final String name = "float".intern();
-
- private FloatType() {
- super(name, null);
- }
-
- @Override
- public Float convert(Object val, Multiplicity m) throws AtlasException {
- if (val != null) {
- if (val instanceof Float) {
- return (Float) val;
- } else if (val instanceof String) {
- return Float.parseFloat((String) val);
- } else if (val instanceof Number) {
- return ((Number) val).floatValue();
- } else {
- throw new ValueConversionException(this, val);
- }
- }
- return convertNull(m);
- }
-
- public Float nullValue() {
- return 0.0f;
- }
- }
-
- public static class DoubleType extends PrimitiveType<Double> {
-
- private static final String name = "double".intern();
-
- private DoubleType() {
- super(name, null);
- }
-
- @Override
- public Double convert(Object val, Multiplicity m) throws AtlasException {
- if (val != null) {
- if (val instanceof Double) {
- return (Double) val;
- } else if (val instanceof String) {
- return Double.parseDouble((String) val);
- } else if (val instanceof Number) {
- return ((Number) val).doubleValue();
- } else {
- throw new ValueConversionException(this, val);
- }
- }
- return convertNull(m);
- }
-
- public Double nullValue() {
- return 0.0;
- }
- }
-
- public static class BigIntegerType extends PrimitiveType<BigInteger> {
-
- private static final String name = "biginteger".intern();
-
- private BigIntegerType() {
- super(name, null);
- }
-
- @Override
- public BigInteger convert(Object val, Multiplicity m) throws AtlasException {
- if (val != null) {
- if (val instanceof BigInteger) {
- return (BigInteger) val;
- } else if (val instanceof String) {
- try {
- return new BigInteger((String) val);
- } catch (NumberFormatException ne) {
- throw new ValueConversionException(this, val, ne);
- }
- } else if (val instanceof Number) {
- return BigInteger.valueOf(((Number) val).longValue());
- } else if (val instanceof BigDecimal) {
- return ((BigDecimal) val).toBigInteger();
- } else {
- throw new ValueConversionException(this, val);
- }
- }
- return convertNull(m);
- }
-
- public BigInteger nullValue() {
- return null;
- }
- }
-
- public static class BigDecimalType extends PrimitiveType<BigDecimal> {
-
- private static final String name = "bigdecimal".intern();
-
- private BigDecimalType() {
- super(name, null);
- }
-
- @Override
- public BigDecimal convert(Object val, Multiplicity m) throws AtlasException {
- if (val != null) {
- if (val instanceof BigDecimal) {
- return (BigDecimal) val;
- } else if (val instanceof String) {
- try {
- return new BigDecimal((String) val);
- } catch (NumberFormatException ne) {
- throw new ValueConversionException(this, val, ne);
- }
- } else if (val instanceof Number) {
- return new BigDecimal(((Number) val).doubleValue());
- } else if (val instanceof BigInteger) {
- return new BigDecimal((BigInteger) val);
- } else {
- throw new ValueConversionException(this, val);
- }
- }
- return convertNull(m);
- }
-
- public BigDecimal nullValue() {
- return null;
- }
- }
-
- public static class DateType extends PrimitiveType<Date> {
-
- private static final String name = "date".intern();
-
- private DateType() {
- super(name, null);
- }
-
- private static final DateTimeFormatter utcDateFormat = ISODateTimeFormat.dateTime();
-
- @Override
- public Date convert(Object val, Multiplicity m) throws AtlasException {
- if (val != null) {
- if (val instanceof Date) {
- return (Date) val;
- } else if (val instanceof String) {
- try {
- return utcDateFormat.parseDateTime((String)val).toDate();
- } catch (Exception ne) {
- throw new ValueConversionException(this, val, ne);
- }
- } else if (val instanceof Number) {
- return new Date(((Number) val).longValue());
- } else {
- throw new ValueConversionException(this, val);
- }
- }
- return convertNull(m);
- }
-
- @Override
- public void output(Date val, Appendable buf, String prefix, Set<Date> inProcess) throws AtlasException {
- TypeUtils.outputVal(val == null ? "<null>" : utcDateFormat.print(new DateTime(val).withZone(DateTimeZone.UTC)), buf,
- prefix);
- }
-
- public Date nullValue() {
- return null;
- }
- }
-
- public static class StringType extends PrimitiveType<String> {
-
- private static final String name = "string".intern();
-
- private StringType() {
- super(name, null);
- }
-
- @Override
- public String convert(Object val, Multiplicity m) throws AtlasException {
- if (val != null && (!(val instanceof String) || StringUtils.isNotEmpty((CharSequence) val))) {
- return val.toString();
- }
-
- if (m.nullAllowed() && val != null){
- return val.toString();
- }
- return convertNull(m);
- }
-
- public String nullValue() {
- return null;
- }
- }
-
- public static class ArrayType extends AbstractDataType<ImmutableCollection<?>> {
- private IDataType elemType;
-
- public ArrayType(IDataType elemType) {
- super(arrayTypeName(elemType), null);
- this.elemType = elemType;
- }
-
- public IDataType getElemType() {
- return elemType;
- }
-
- protected void setElemType(IDataType elemType) {
- this.elemType = elemType;
- }
-
- @Override
- public ImmutableCollection<?> convert(Object val, Multiplicity m) throws AtlasException {
- if (val != null) {
- Iterator it = null;
- if (val instanceof Collection) {
- it = ((Collection) val).iterator();
- } else if (val instanceof Iterable) {
- it = ((Iterable) val).iterator();
- } else if (val instanceof Iterator) {
- it = (Iterator) val;
- }
-
- if (it != null) {
- ImmutableCollection.Builder b = m.isUnique ? ImmutableSet.builder() : ImmutableList.builder();
- while (it.hasNext()) {
- b.add(elemType.convert(it.next(),
- TypeSystem.getInstance().allowNullsInCollections() ? Multiplicity.OPTIONAL :
- Multiplicity.REQUIRED));
- }
- return m.isUnique ? b.build().asList() : b.build();
- } else {
- try {
- return ImmutableList.of(elemType.convert(val,
- TypeSystem.getInstance().allowNullsInCollections() ? Multiplicity.OPTIONAL :
- Multiplicity.REQUIRED));
- } catch (Exception e) {
- throw new ValueConversionException(this, val, e);
- }
- }
- }
- if (!m.nullAllowed()) {
- throw new ValueConversionException.NullConversionException(m);
- }
- return null;
- }
-
- public ImmutableCollection<?> mapIds(ImmutableCollection<?> val, Multiplicity m, Map<Id, Id> transientToNewIds)
- throws AtlasException {
-
- if (val == null || elemType.getTypeCategory() != TypeCategory.CLASS) {
- return val;
- }
- ImmutableCollection.Builder b = m.isUnique ? ImmutableSet.builder() : ImmutableList.builder();
- for (Object elem : val) {
- if (elem instanceof IReferenceableInstance) {
- Id oldId = ((IReferenceableInstance) elem).getId();
- Id newId = transientToNewIds.get(oldId);
- b.add(newId == null ? oldId : newId);
- } else {
- b.add(elem);
- }
- }
- return b.build();
- }
-
- @Override
- public TypeCategory getTypeCategory() {
- return TypeCategory.ARRAY;
- }
-
- @Override
- public void updateSignatureHash(MessageDigest digester, Object val) throws AtlasException {
- IDataType elemType = getElemType();
- List vals = (List) val;
- for (Object listElem : vals) {
- elemType.updateSignatureHash(digester, listElem);
- }
- }
- }
-
- public static class MapType extends AbstractDataType<ImmutableMap<?, ?>> {
-
- private IDataType keyType;
- private IDataType valueType;
-
- public MapType(IDataType keyType, IDataType valueType) {
- super(mapTypeName(keyType, valueType), null);
- this.keyType = keyType;
- this.valueType = valueType;
- }
-
- public IDataType getKeyType() {
- return keyType;
- }
-
- protected void setKeyType(IDataType keyType) {
- this.keyType = keyType;
- }
-
- public IDataType getValueType() {
- return valueType;
- }
-
- protected void setValueType(IDataType valueType) {
- this.valueType = valueType;
- }
-
- @Override
- public ImmutableMap<?, ?> convert(Object val, Multiplicity m) throws AtlasException {
- if (val != null) {
- Iterator<Map.Entry> it = null;
- if (Map.class.isAssignableFrom(val.getClass())) {
- it = ((Map) val).entrySet().iterator();
- ImmutableMap.Builder b = ImmutableMap.builder();
- while (it.hasNext()) {
- Map.Entry e = it.next();
- b.put(keyType.convert(e.getKey(),
- TypeSystem.getInstance().allowNullsInCollections() ? Multiplicity.OPTIONAL :
- Multiplicity.REQUIRED),
- valueType.convert(e.getValue(), Multiplicity.OPTIONAL));
- }
- return b.build();
- } else {
- throw new ValueConversionException(this, val);
- }
- }
- if (!m.nullAllowed()) {
- throw new ValueConversionException.NullConversionException(m);
- }
- return null;
- }
-
- public ImmutableMap<?, ?> mapIds(ImmutableMap val, Multiplicity m, Map<Id, Id> transientToNewIds)
- throws AtlasException {
-
- if (val == null || (keyType.getTypeCategory() != TypeCategory.CLASS
- && valueType.getTypeCategory() != TypeCategory.CLASS)) {
- return val;
- }
- ImmutableMap.Builder b = ImmutableMap.builder();
- for (Map.Entry elem : (Iterable<Map.Entry>) val.entrySet()) {
- Object oldKey = elem.getKey();
- Object oldValue = elem.getValue();
- Object newKey = oldKey;
- Object newValue = oldValue;
-
- if (oldKey instanceof IReferenceableInstance) {
- Id oldId = ((IReferenceableInstance) oldKey).getId();
- Id newId = transientToNewIds.get(oldId);
- newKey = newId == null ? oldId : newId;
- }
-
- if (oldValue instanceof IReferenceableInstance) {
- Id oldId = ((IReferenceableInstance) oldValue).getId();
- Id newId = transientToNewIds.get(oldId);
- newValue = newId == null ? oldId : newId;
- }
-
- b.put(newKey, newValue);
- }
- return b.build();
- }
-
- @Override
- public TypeCategory getTypeCategory() {
- return TypeCategory.MAP;
- }
-
- @Override
- public void updateSignatureHash(MessageDigest digester, Object val) throws AtlasException {
- IDataType keyType = getKeyType();
- IDataType valueType = getValueType();
- Map vals = (Map) val;
- for (Object key : vals.keySet()) {
- keyType.updateSignatureHash(digester, key);
- valueType.updateSignatureHash(digester, vals.get(key));
- }
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/main/java/org/apache/atlas/typesystem/types/DownCastFieldMapping.java
----------------------------------------------------------------------
diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/DownCastFieldMapping.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/DownCastFieldMapping.java
deleted file mode 100755
index 85e288e..0000000
--- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/DownCastFieldMapping.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/**
- * 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.atlas.typesystem.types;
-
-import com.google.common.collect.ImmutableMap;
-import org.apache.atlas.AtlasException;
-import org.apache.atlas.typesystem.persistence.DownCastStructInstance;
-
-public class DownCastFieldMapping {
-
- public final ImmutableMap<String, String> fieldNameMap;
-
- protected DownCastFieldMapping(ImmutableMap<String, String> fieldNameMap) {
- this.fieldNameMap = fieldNameMap;
- }
-
- public void set(DownCastStructInstance s, String attrName, Object val) throws AtlasException {
-
- String mappedNm = fieldNameMap.get(attrName);
- if (mappedNm == null) {
- throw new ValueConversionException(s.getTypeName(), val, "Unknown field " + attrName);
- }
-
- s.backingInstance.set(mappedNm, val);
- }
-
- public Object get(DownCastStructInstance s, String attrName) throws AtlasException {
-
- String mappedNm = fieldNameMap.get(attrName);
- if (mappedNm == null) {
- throw new ValueConversionException(
- String.format("Unknown field %s for Struct %s", attrName, s.getTypeName()));
- }
- return s.backingInstance.get(mappedNm);
- }
-}
http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/main/java/org/apache/atlas/typesystem/types/EnumType.java
----------------------------------------------------------------------
diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/EnumType.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/EnumType.java
deleted file mode 100755
index 82e22ce..0000000
--- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/EnumType.java
+++ /dev/null
@@ -1,121 +0,0 @@
-/**
- * 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.atlas.typesystem.types;
-
-import com.google.common.collect.ImmutableCollection;
-import com.google.common.collect.ImmutableMap;
-import org.apache.atlas.AtlasConstants;
-import org.apache.atlas.AtlasException;
-import scala.math.BigInt;
-
-import java.nio.charset.Charset;
-import java.security.MessageDigest;
-
-public class EnumType extends AbstractDataType<EnumValue> {
-
- public final TypeSystem typeSystem;
- public final ImmutableMap<String, EnumValue> valueMap;
- public final ImmutableMap<Integer, EnumValue> ordinalMap;
-
- protected EnumType(TypeSystem typeSystem, String name, EnumValue... values) {
- this(typeSystem, name, null, values);
- }
-
- protected EnumType(TypeSystem typeSystem, String name, String description, EnumValue... values) {
- this(typeSystem, name, description, AtlasConstants.DEFAULT_TYPE_VERSION, values);
- }
-
- protected EnumType(TypeSystem typeSystem, String name, String description, String version, EnumValue... values) {
- super(name, description, version);
- this.typeSystem = typeSystem;
- ImmutableMap.Builder<String, EnumValue> b1 = new ImmutableMap.Builder();
- ImmutableMap.Builder<Integer, EnumValue> b2 = new ImmutableMap.Builder();
- for (EnumValue v : values) {
- b1.put(v.value, v);
- b2.put(v.ordinal, v);
- }
- valueMap = b1.build();
- ordinalMap = b2.build();
- }
-
- @Override
- public EnumValue convert(Object val, Multiplicity m) throws AtlasException {
- if (val != null) {
- EnumValue e = null;
- if (val instanceof EnumValue) {
- e = valueMap.get(((EnumValue) val).value);
- } else if (val instanceof Integer || val instanceof BigInt) {
- e = ordinalMap.get(val);
- } else if (val instanceof String) {
- e = valueMap.get(val);
- } else if (val instanceof Number) {
- e = ordinalMap.get(((Number) val).intValue());
- }
-
- if (e == null) {
- throw new ValueConversionException(this, val);
- }
- return e;
- }
- return convertNull(m);
- }
-
- @Override
- public DataTypes.TypeCategory getTypeCategory() {
- return DataTypes.TypeCategory.ENUM;
- }
-
- @Override
- public void validateUpdate(IDataType newType) throws TypeUpdateException {
- super.validateUpdate(newType);
-
- EnumType enumType = (EnumType) newType;
- for (EnumValue enumValue : values()) {
- //The old enum value should be part of new enum definition as well
- if (!enumType.valueMap.containsKey(enumValue.value)) {
- throw new TypeUpdateException("Value " + enumValue.value + " is missing in new type");
- }
-
- //The ordinal for old enum value can't change
- EnumValue newEnumValue = enumType.valueMap.get(enumValue.value);
- if (enumValue.ordinal != newEnumValue.ordinal) {
- throw new TypeUpdateException(String.format("Ordinal mismatch %s(%s) != %s(%s)", enumValue.value,
- enumValue.ordinal, newEnumValue.value, newEnumValue.ordinal));
- }
- }
- }
-
- public void updateSignatureHash(MessageDigest digester, Object val) throws AtlasException {
- if (val != null) {
- digester.update(fromValue((String) val).toString().getBytes(Charset.forName("UTF-8")));
- }
- }
-
- public EnumValue fromOrdinal(int o) {
- return ordinalMap.get(o);
- }
-
- public EnumValue fromValue(String val) {
- return valueMap.get(val.trim());
- }
-
- public ImmutableCollection<EnumValue> values() {
- return valueMap.values();
- }
-}
http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/main/java/org/apache/atlas/typesystem/types/EnumTypeDefinition.java
----------------------------------------------------------------------
diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/EnumTypeDefinition.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/EnumTypeDefinition.java
deleted file mode 100755
index 40cb132..0000000
--- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/EnumTypeDefinition.java
+++ /dev/null
@@ -1,64 +0,0 @@
-/**
- * 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.atlas.typesystem.types;
-
-import org.apache.atlas.utils.ParamChecker;
-import org.apache.atlas.AtlasConstants;
-
-import java.util.Arrays;
-import java.util.Objects;
-
-public final class EnumTypeDefinition {
-
- public final String name;
- public final String description;
- public final String version;
- public final EnumValue[] enumValues;
-
- public EnumTypeDefinition(String name, EnumValue... enumValues) {
- this(name, null, AtlasConstants.DEFAULT_TYPE_VERSION, enumValues);
- }
-
- public EnumTypeDefinition(String name, String description, EnumValue... enumValues) {
- this(name, description, AtlasConstants.DEFAULT_TYPE_VERSION, enumValues);
- }
-
- public EnumTypeDefinition(String name, String description, String version, EnumValue... enumValues) {
- this.name = ParamChecker.notEmpty(name, "Enum type name");
- this.description = description;
- this.enumValues = ParamChecker.notNullElements(enumValues, "Enum values");
- this.version = version;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- EnumTypeDefinition that = (EnumTypeDefinition) o;
- return Objects.equals(name, that.name) &&
- Objects.equals(description, that.description) &&
- Objects.equals(version, that.version) &&
- Arrays.equals(enumValues, that.enumValues);
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(name, description, version, enumValues);
- }
-}
http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/main/java/org/apache/atlas/typesystem/types/EnumValue.java
----------------------------------------------------------------------
diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/EnumValue.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/EnumValue.java
deleted file mode 100755
index d75259b..0000000
--- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/EnumValue.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/**
- * 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.atlas.typesystem.types;
-
-import org.apache.atlas.utils.ParamChecker;
-
-public class EnumValue {
-
- public final String value;
- public final int ordinal;
-
- public EnumValue(String value, int ordinal) {
- this.value = ParamChecker.notEmpty(value, "Enum value");
- this.ordinal = ordinal;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
-
- EnumValue enumValue = (EnumValue) o;
-
- if (ordinal != enumValue.ordinal) {
- return false;
- }
- if (!value.equals(enumValue.value)) {
- return false;
- }
-
- return true;
- }
-
- @Override
- public int hashCode() {
- int result = value.hashCode();
- result = 31 * result + ordinal;
- return result;
- }
-
- @Override
- public String toString() {
- return value;
- }
-}
http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/main/java/org/apache/atlas/typesystem/types/FieldMapping.java
----------------------------------------------------------------------
diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/FieldMapping.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/FieldMapping.java
deleted file mode 100755
index a2b3db2..0000000
--- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/FieldMapping.java
+++ /dev/null
@@ -1,162 +0,0 @@
-/**
- * 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.atlas.typesystem.types;
-
-import org.apache.atlas.AtlasException;
-import org.apache.atlas.typesystem.IReferenceableInstance;
-import org.apache.atlas.typesystem.IStruct;
-import org.apache.atlas.typesystem.persistence.Id;
-
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-public class FieldMapping {
-
- public final Map<String, AttributeInfo> fields;
- public final Map<String, Integer> fieldPos;
- public 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 int numReferenceables;
-
- 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, int numReferenceables) {
- 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;
- this.numReferenceables = numReferenceables;
- }
-
- protected void outputFields(IStruct s, Appendable buf, String fieldPrefix, Set<? extends IStruct> inProcess) throws AtlasException {
- for (Map.Entry<String, AttributeInfo> e : fields.entrySet()) {
- String attrName = e.getKey();
- AttributeInfo i = e.getValue();
- Object aVal = s.get(attrName);
- TypeUtils.outputVal(attrName + " : ", buf, fieldPrefix);
- if (aVal != null && aVal instanceof Id) {
- TypeUtils.outputVal(aVal.toString(), buf, "");
- } else {
- i.dataType().output(aVal, buf, fieldPrefix, inProcess);
- }
- TypeUtils.outputVal("\n", buf, "");
- }
- }
-
- public void output(IStruct s, Appendable buf, String prefix, Set<IStruct> inProcess) throws AtlasException {
- if (s == null) {
- TypeUtils.outputVal("<null>\n", buf, "");
- return;
- }
-
- if (inProcess == null) {
- inProcess = new HashSet<>();
- }
- else if (inProcess.contains(s)) {
- // Avoid infinite recursion when structs reference each other.
- return;
- }
- inProcess.add(s);
-
- try {
- TypeUtils.outputVal("{", buf, prefix);
-
- TypeUtils.outputVal("\n", buf, "");
- String fieldPrefix = prefix + "\t";
-
- outputFields(s, buf, fieldPrefix, inProcess);
-
- TypeUtils.outputVal("}", buf, prefix);
- }
- finally {
- inProcess.remove(s);
- }
- }
-
- public void output(IReferenceableInstance s, Appendable buf, String prefix, Set<IReferenceableInstance> inProcess) throws AtlasException {
- if (s == null) {
- TypeUtils.outputVal("<null>\n", buf, "");
- return;
- }
-
- if (inProcess == null) {
- inProcess = new HashSet<>();
- }
- else if (inProcess.contains(s)) {
- // Avoid infinite recursion when structs reference each other.
- return;
- }
- inProcess.add(s);
-
- try {
- TypeUtils.outputVal("{", buf, prefix);
-
- TypeUtils.outputVal("\n", buf, "");
- String fieldPrefix = prefix + "\t";
-
- TypeUtils.outputVal("id : ", buf, fieldPrefix);
- TypeUtils.outputVal(s.getId().toString(), buf, "");
- TypeUtils.outputVal("\n", buf, "");
-
- outputFields(s, buf, fieldPrefix, inProcess);
-
- TypeSystem ts = TypeSystem.getInstance();
-
- for (String sT : s.getTraits()) {
- TraitType tt = ts.getDataType(TraitType.class, sT);
- TypeUtils.outputVal(sT + " : ", buf, fieldPrefix);
- tt.output(s.getTrait(sT), buf, fieldPrefix, null);
- }
-
- TypeUtils.outputVal("}", buf, prefix);
- }
- finally {
- inProcess.remove(s);
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/main/java/org/apache/atlas/typesystem/types/HierarchicalType.java
----------------------------------------------------------------------
diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/HierarchicalType.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/HierarchicalType.java
deleted file mode 100755
index ac7f442..0000000
--- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/HierarchicalType.java
+++ /dev/null
@@ -1,545 +0,0 @@
-/**
- * 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.atlas.typesystem.types;
-
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.UnmodifiableIterator;
-
-import org.apache.atlas.AtlasConstants;
-import org.apache.atlas.AtlasException;
-import org.apache.atlas.typesystem.IStruct;
-import org.apache.atlas.typesystem.persistence.DownCastStructInstance;
-import org.apache.atlas.typesystem.types.TypeUtils.Pair;
-
-import java.io.IOException;
-import java.util.*;
-
-/**
- * Represents a Type that can have SuperTypes. An Instance of the HierarchicalType can be
- * downcast to a SuperType.
- * @param <ST> the Type of the SuperType. TraitTypes have TraitTypes as SuperTypes, ClassTypes
- * have ClassTypes
- * as SuperTypes.
- * @param <T> the class of the Instance of this DataType.
- */
-public abstract class HierarchicalType<ST extends HierarchicalType, T> extends AbstractDataType<T> {
-
- public final TypeSystem typeSystem;
- public final Class<ST> superTypeClass;
- public final FieldMapping fieldMapping;
- public final int numFields;
- public final ImmutableSet<String> superTypes;
- public final ImmutableList<AttributeInfo> immediateAttrs;
- public final ImmutableMap<String, String> attributeNameToType;
- protected ImmutableMap<String, List<Path>> superTypePaths;
- protected ImmutableMap<String, Path> pathNameToPathMap;
-
- HierarchicalType(TypeSystem typeSystem, Class<ST> superTypeClass, String name, ImmutableSet<String> superTypes,
- int numFields) {
- this(typeSystem, superTypeClass, name, null, superTypes, numFields);
- }
-
- /**
- * Used when creating a Type, to support recursive Structs.
- */
- HierarchicalType(TypeSystem typeSystem, Class<ST> superTypeClass, String name, String description, ImmutableSet<String> superTypes,
- int numFields) {
- this( typeSystem, superTypeClass, name, description, AtlasConstants.DEFAULT_TYPE_VERSION, superTypes, numFields);
- }
-
- HierarchicalType(TypeSystem typeSystem, Class<ST> superTypeClass, String name, String description, String version, ImmutableSet<String> superTypes,
- int numFields) {
- super(name, description, version);
- this.typeSystem = typeSystem;
- this.superTypeClass = superTypeClass;
- this.fieldMapping = null;
- this.numFields = numFields;
- this.superTypes = superTypes;
- this.immediateAttrs = ImmutableList.of();
- this.attributeNameToType = null;
- }
-
- HierarchicalType(TypeSystem typeSystem, Class<ST> superTypeClass, String name, ImmutableSet<String> superTypes,
- AttributeInfo... fields) throws AtlasException {
- this(typeSystem, superTypeClass, name, null, superTypes, fields);
- }
- HierarchicalType(TypeSystem typeSystem, Class<ST> superTypeClass, String name, String description, ImmutableSet<String> superTypes,
- AttributeInfo... fields) throws AtlasException {
- this(typeSystem, superTypeClass, name, description, AtlasConstants.DEFAULT_TYPE_VERSION, superTypes, fields);
- }
-
- HierarchicalType(TypeSystem typeSystem, Class<ST> superTypeClass, String name, String description, String version, ImmutableSet<String> superTypes,
- AttributeInfo... fields) throws AtlasException {
- super(name, description, version);
- this.typeSystem = typeSystem;
- this.superTypeClass = superTypeClass;
- Pair<FieldMapping, ImmutableMap<String, String>> p = constructFieldMapping(superTypes, fields);
- this.fieldMapping = p.left;
- this.attributeNameToType = p.right;
- this.numFields = this.fieldMapping.fields.size();
- this.superTypes = superTypes == null ? ImmutableSet.<String>of() : superTypes;
- this.immediateAttrs = ImmutableList.copyOf(fields);
- }
-
- public FieldMapping fieldMapping() {
- return fieldMapping;
- }
-
- /**
- * Given type must be a SubType of this type.
- * @param typeName
- * @throws AtlasException
- */
- public boolean isSubType(String typeName) throws AtlasException {
- HierarchicalType cType = typeSystem.getDataType(HierarchicalType.class, typeName);
- return (cType == this || cType.superTypePaths.containsKey(getName()));
- }
-
- /**
- * Validate that current definition can be updated with the new definition
- * @param newType
- * @return true if the current definition can be updated with the new definition, else false
- */
- @Override
- public void validateUpdate(IDataType newType) throws TypeUpdateException {
- super.validateUpdate(newType);
-
- HierarchicalType newHierarchicalType = (HierarchicalType) newType;
-
- //validate on supertypes
-
- if ((newHierarchicalType.superTypes.size() != superTypes.size())
- || !newHierarchicalType.superTypes.containsAll(superTypes)) {
- throw new TypeUpdateException(newType, "New type cannot modify superTypes");
- }
-
- //validate on fields
- try {
- TypeUtils.validateUpdate(fieldMapping, newHierarchicalType.fieldMapping);
- } catch (TypeUpdateException e) {
- throw new TypeUpdateException(newType, e);
- }
- }
-
- protected void setupSuperTypesGraph() throws AtlasException {
- setupSuperTypesGraph(superTypes);
- }
-
- private void setupSuperTypesGraph(ImmutableSet<String> superTypes) throws AtlasException {
- Map<String, List<Path>> superTypePaths = new HashMap<>();
- Map<String, Path> pathNameToPathMap = new HashMap<>();
- Queue<Path> queue = new LinkedList<>();
- queue.add(new Node(getName()));
- while (!queue.isEmpty()) {
- Path currentPath = queue.poll();
-
- ST superType = Objects.equals(currentPath.typeName, getName()) ? (ST) this :
- typeSystem.getDataType(superTypeClass, currentPath.typeName);
-
- pathNameToPathMap.put(currentPath.pathName, currentPath);
- if (superType != this) {
- List<Path> typePaths = superTypePaths.get(superType.getName());
- if (typePaths == null) {
- typePaths = new ArrayList<>();
- superTypePaths.put(superType.getName(), typePaths);
- }
- typePaths.add(currentPath);
- }
-
- ImmutableSet<String> sTs = superType == this ? superTypes : superType.superTypes;
-
- if (sTs != null) {
- for (String sT : sTs) {
- queue.add(new Path(sT, currentPath));
- }
- }
- }
-
- this.superTypePaths = ImmutableMap.copyOf(superTypePaths);
- this.pathNameToPathMap = ImmutableMap.copyOf(pathNameToPathMap);
-
- }
-
- protected Pair<FieldMapping, ImmutableMap<String, String>> constructFieldMapping(ImmutableSet<String> superTypes,
- AttributeInfo... fields) throws AtlasException {
-
- Map<String, AttributeInfo> fieldsMap = new LinkedHashMap();
- Map<String, Integer> fieldPos = new HashMap();
- Map<String, Integer> fieldNullPos = new HashMap();
- Map<String, String> attributeNameToType = new HashMap<>();
-
- int numBools = 0;
- int numBytes = 0;
- int numShorts = 0;
- int numInts = 0;
- int numLongs = 0;
- int numFloats = 0;
- int numDoubles = 0;
- int numBigInts = 0;
- int numBigDecimals = 0;
- int numDates = 0;
- int numStrings = 0;
- int numArrays = 0;
- int numMaps = 0;
- int numStructs = 0;
- int numReferenceables = 0;
-
- setupSuperTypesGraph(superTypes);
-
- Iterator<Path> pathItr = pathIterator();
- while (pathItr.hasNext()) {
- Path currentPath = pathItr.next();
-
- ST superType = Objects.equals(currentPath.typeName, getName()) ? (ST) this :
- typeSystem.getDataType(superTypeClass, currentPath.typeName);
-
- ImmutableList<AttributeInfo> superTypeFields =
- superType == this ? ImmutableList.copyOf(fields) : superType.immediateAttrs;
-
- Set<String> immediateFields = new HashSet<>();
-
- for (AttributeInfo i : superTypeFields) {
- if (superType == this) {
- if (immediateFields.contains(i.name)) {
- throw new AtlasException(String.format(
- "Struct defintion cannot contain multiple fields with the" + " same name %s", i.name));
- }
- immediateFields.add(i.name);
- }
-
- String attrName = i.name;
- if (fieldsMap.containsKey(attrName)) {
- attrName = currentPath.addOverrideAttr(attrName);
- }
- attributeNameToType.put(attrName, superType.getName());
-
- fieldsMap.put(attrName, i);
- fieldNullPos.put(attrName, fieldNullPos.size());
- if (i.dataType() == DataTypes.BOOLEAN_TYPE) {
- fieldPos.put(attrName, numBools);
- numBools++;
- } else if (i.dataType() == DataTypes.BYTE_TYPE) {
- fieldPos.put(attrName, numBytes);
- numBytes++;
- } else if (i.dataType() == DataTypes.SHORT_TYPE) {
- fieldPos.put(attrName, numShorts);
- numShorts++;
- } else if (i.dataType() == DataTypes.INT_TYPE) {
- fieldPos.put(attrName, numInts);
- numInts++;
- } else if (i.dataType() == DataTypes.LONG_TYPE) {
- fieldPos.put(attrName, numLongs);
- numLongs++;
- } else if (i.dataType() == DataTypes.FLOAT_TYPE) {
- fieldPos.put(attrName, numFloats);
- numFloats++;
- } else if (i.dataType() == DataTypes.DOUBLE_TYPE) {
- fieldPos.put(attrName, numDoubles);
- numDoubles++;
- } else if (i.dataType() == DataTypes.BIGINTEGER_TYPE) {
- fieldPos.put(attrName, numBigInts);
- numBigInts++;
- } else if (i.dataType() == DataTypes.BIGDECIMAL_TYPE) {
- fieldPos.put(attrName, numBigDecimals);
- numBigDecimals++;
- } else if (i.dataType() == DataTypes.DATE_TYPE) {
- fieldPos.put(attrName, numDates);
- numDates++;
- } else if (i.dataType() == DataTypes.STRING_TYPE) {
- fieldPos.put(attrName, numStrings);
- numStrings++;
- } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.ENUM) {
- fieldPos.put(i.name, numInts);
- numInts++;
- } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.ARRAY) {
- fieldPos.put(attrName, numArrays);
- numArrays++;
- } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.MAP) {
- fieldPos.put(attrName, numMaps);
- numMaps++;
- } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.STRUCT
- || i.dataType().getTypeCategory() == DataTypes.TypeCategory.TRAIT) {
- fieldPos.put(attrName, numStructs);
- numStructs++;
- } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.CLASS) {
- fieldPos.put(attrName, numReferenceables);
- numReferenceables++;
- } else {
- throw new AtlasException(String.format("Unknown datatype %s", i.dataType()));
- }
- }
- }
-
- this.superTypePaths = ImmutableMap.copyOf(superTypePaths);
- this.pathNameToPathMap = ImmutableMap.copyOf(pathNameToPathMap);
-
- FieldMapping fm =
- new FieldMapping(fieldsMap, fieldPos, fieldNullPos, numBools, numBytes, numShorts, numInts, numLongs,
- numFloats, numDoubles, numBigInts, numBigDecimals, numDates, numStrings, numArrays, numMaps,
- numStructs, numReferenceables);
-
- return new Pair(fm, ImmutableMap.copyOf(attributeNameToType));
- }
-
- public IStruct castAs(IStruct s, String superTypeName) throws AtlasException {
-
- if (!superTypePaths.containsKey(superTypeName)) {
- throw new AtlasException(String.format("Cannot downcast to %s from type %s", superTypeName, getName()));
- }
-
- if (s != null) {
- if (!Objects.equals(s.getTypeName(), getName())) {
- throw new AtlasException(
- String.format("Downcast called on wrong type %s, instance type is %s", getName(),
- s.getTypeName()));
- }
-
- List<Path> pathToSuper = superTypePaths.get(superTypeName);
- if (pathToSuper.size() > 1) {
- throw new AtlasException(String.format(
- "Cannot downcast called to %s, from %s: there are multiple paths " + "to SuperType",
- superTypeName, getName()));
- }
-
- ST superType = typeSystem.getDataType(superTypeClass, superTypeName);
- Map<String, String> downCastMap = superType.constructDowncastFieldMap(this, pathToSuper.get(0));
- return new DownCastStructInstance(superTypeName, new DownCastFieldMapping(ImmutableMap.copyOf(downCastMap)),
- s);
- }
-
- return null;
- }
-
- public ST getDefinedType(String attrName) throws AtlasException {
- if (!attributeNameToType.containsKey(attrName)) {
- throw new AtlasException(String.format("Unknown attribute %s in type %s", attrName, getName()));
- }
- return typeSystem.getDataType(superTypeClass, attributeNameToType.get(attrName));
- }
-
- public String getDefinedTypeName(String attrName) throws AtlasException {
- return getDefinedType(attrName).getName();
- }
-
- public String getQualifiedName(String attrName) throws AtlasException {
- String attrTypeName = getDefinedTypeName(attrName);
- return attrName.contains(".") ? attrName : String.format("%s.%s", attrTypeName, attrName);
- }
-
- protected Map<String, String> constructDowncastFieldMap(ST subType, Path pathToSubType) {
-
- String pathToSubTypeName = pathToSubType.pathAfterThis;
- /*
- * the downcastMap;
- */
- Map<String, String> dCMap = new HashMap<>();
- Iterator<Path> itr = pathIterator();
- while (itr.hasNext()) {
- Path p = itr.next();
- Path pInSubType = (Path) subType.pathNameToPathMap.get(p.pathName + "." + pathToSubTypeName);
-
- if (pInSubType.hiddenAttributeMap != null) {
- for (Map.Entry<String, String> e : pInSubType.hiddenAttributeMap.entrySet()) {
- String mappedInThisType =
- p.hiddenAttributeMap != null ? p.hiddenAttributeMap.get(e.getKey()) : null;
- if (mappedInThisType == null) {
- dCMap.put(e.getKey(), e.getValue());
- } else {
- dCMap.put(mappedInThisType, e.getValue());
- }
- }
- }
- }
- return dCMap;
- }
-
- @Override
- public String toString() {
- StringBuilder buf = new StringBuilder();
- try {
- output(buf, new HashSet<String>());
- }
- catch (AtlasException e) {
- throw new RuntimeException(e);
- }
- return buf.toString();
- }
-
- @Override
- public void output(Appendable buf, Set<String> typesInProcess) throws AtlasException {
-
- if (typesInProcess == null) {
- typesInProcess = new HashSet<>();
- }
- else if (typesInProcess.contains(name)) {
- // Avoid infinite recursion on bi-directional reference attributes.
- try {
- buf.append(name);
- } catch (IOException e) {
- throw new AtlasException(e);
- }
- return;
- }
-
- typesInProcess.add(name);
- try {
- buf.append(getClass().getSimpleName()).append('{');
- buf.append("name=").append(name);
- buf.append(", description=").append(description);
- buf.append(", superTypes=").append(superTypes.toString());
- buf.append(", immediateAttrs=[");
- UnmodifiableIterator<AttributeInfo> it = immediateAttrs.iterator();
- while (it.hasNext()) {
- AttributeInfo attrInfo = it.next();
- attrInfo.output(buf, typesInProcess);
- if (it.hasNext()) {
- buf.append(", ");
- }
- else {
- buf.append(']');
- }
- }
- buf.append("}");
- }
- catch(IOException e) {
- throw new AtlasException(e);
- }
- finally {
- typesInProcess.remove(name);
- }
- }
-
- public Set<String> getAllSuperTypeNames() {
- return superTypePaths.keySet();
- }
-
- public Iterator<Path> pathIterator() {
- return new PathItr();
- }
-
- static class Path {
- public final String typeName;
- public final String pathName;
- public final String pathAfterThis;
- private final Path subTypePath;
- /*
- * name mapping for attributes hidden by a SubType.
- */ Map<String, String> hiddenAttributeMap;
-
- Path(String typeName, Path childPath) throws AtlasException {
- this.typeName = typeName;
- this.subTypePath = childPath;
- if (childPath.contains(typeName)) {
- throw new CyclicTypeDefinition(this);
- }
- pathName = String.format("%s.%s", typeName, childPath.pathName);
- pathAfterThis = childPath.pathName;
- }
-
- Path(String typeName) {
- assert getClass() == Node.class;
- this.typeName = typeName;
- this.subTypePath = null;
- pathName = typeName;
- pathAfterThis = null;
- }
-
- public boolean contains(String typeName) {
- return this.typeName.equals(typeName) || (subTypePath != null && subTypePath.contains(typeName));
- }
-
- public String pathString(String nodeSep) {
-
- StringBuilder b = new StringBuilder();
- Path p = this;
-
- while (p != null) {
- b.append(p.typeName);
- p = p.subTypePath;
- if (p != null) {
- b.append(nodeSep);
- }
- }
- return b.toString();
- }
-
- String addOverrideAttr(String name) {
- hiddenAttributeMap = hiddenAttributeMap == null ? new HashMap<String, String>() : hiddenAttributeMap;
- String oName = pathName + "." + name;
- hiddenAttributeMap.put(name, oName);
- return oName;
- }
- }
-
- static class Node extends Path {
- Node(String typeName) {
- super(typeName);
- }
- }
-
- static class CyclicTypeDefinition extends AtlasException {
-
- CyclicTypeDefinition(Path p) {
- super(String.format("Cycle in Type Definition %s", p.pathString(" -> ")));
- }
- }
-
- class PathItr implements Iterator<Path> {
-
- Queue<Path> pathQueue;
-
- PathItr() {
- pathQueue = new LinkedList<>();
- pathQueue.add(pathNameToPathMap.get(getName()));
- }
-
- @Override
- public boolean hasNext() {
- return !pathQueue.isEmpty();
- }
-
- @Override
- public Path next() {
- Path p = pathQueue.poll();
-
- if(p != null) {
- ST t = null;
- try {
- t = typeSystem.getDataType(superTypeClass, p.typeName);
- } catch (AtlasException me) {
- throw new RuntimeException(me);
- }
- if (t.superTypes != null) {
- for (String sT : (ImmutableSet<String>) t.superTypes) {
- String nm = sT + "." + p.pathName;
- pathQueue.add(pathNameToPathMap.get(nm));
- }
- }
- }
- return p;
- }
-
- @Override
- public void remove() {
- throw new UnsupportedOperationException();
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/main/java/org/apache/atlas/typesystem/types/HierarchicalTypeDefinition.java
----------------------------------------------------------------------
diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/HierarchicalTypeDefinition.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/HierarchicalTypeDefinition.java
deleted file mode 100755
index ab63fea..0000000
--- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/HierarchicalTypeDefinition.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/**
- * 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.atlas.typesystem.types;
-
-import com.google.common.collect.ImmutableSet;
-import org.apache.atlas.AtlasConstants;
-
-import java.util.Objects;
-
-public class HierarchicalTypeDefinition<T extends HierarchicalType> extends StructTypeDefinition {
-
- public final ImmutableSet<String> superTypes;
- public final String hierarchicalMetaTypeName;
-
- public HierarchicalTypeDefinition(Class<T> hierarchicalMetaType, String typeName, String typeDescription, ImmutableSet<String> superTypes,
- AttributeDefinition[] attributeDefinitions) {
- this(hierarchicalMetaType, typeName, typeDescription, AtlasConstants.DEFAULT_TYPE_VERSION, superTypes,
- attributeDefinitions);
- }
-
- // Used only for de-serializing JSON String to typedef.
- public HierarchicalTypeDefinition( String hierarchicalMetaTypeName, String typeName, String typeDescription, String typeVersion, String[] superTypes, AttributeDefinition[] attributeDefinitions) throws ClassNotFoundException {
- this((Class<T>) Class.forName(hierarchicalMetaTypeName), typeName, typeDescription, typeVersion, ImmutableSet.copyOf(superTypes), attributeDefinitions);
- }
- // Used only for de-serializing JSON String to typedef (no typeVersion).
- public HierarchicalTypeDefinition( String hierarchicalMetaTypeName, String typeName, String typeDescription, String[] superTypes, AttributeDefinition[] attributeDefinitions) throws ClassNotFoundException {
- this((Class<T>) Class.forName(hierarchicalMetaTypeName), typeName, typeDescription, AtlasConstants.DEFAULT_TYPE_VERSION, ImmutableSet.copyOf(superTypes), attributeDefinitions);
- }
- // Used only for serializing typedef to JSON String.
- public HierarchicalTypeDefinition( String hierarchicalMetaTypeName, String typeName, String typeDescription, String typeVersion, ImmutableSet<String> superTypes, AttributeDefinition[] attributeDefinitions, String typeDef) throws ClassNotFoundException {
- this((Class<T>) Class.forName(hierarchicalMetaTypeName), typeName, typeDescription, typeVersion, superTypes, attributeDefinitions);
- }
- // Used only for serializing typedef to JSON String (no typeVersion).
- public HierarchicalTypeDefinition( String hierarchicalMetaTypeName, String typeName, String typeDescription, ImmutableSet<String> superTypes, AttributeDefinition[] attributeDefinitions, String typeDef) throws ClassNotFoundException {
- this((Class<T>) Class.forName(hierarchicalMetaTypeName), typeName, typeDescription, AtlasConstants.DEFAULT_TYPE_VERSION, superTypes, attributeDefinitions);
- }
-
- public HierarchicalTypeDefinition(Class<T> hierarchicalMetaType, String typeName, String typeDescription, String typeVersion, ImmutableSet<String> superTypes, AttributeDefinition[] attributeDefinitions) {
- super(typeName, typeDescription, typeVersion, false, attributeDefinitions);
- this.hierarchicalMetaTypeName = hierarchicalMetaType.getName();
- this.superTypes = superTypes == null ? ImmutableSet.<String>of() : superTypes;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- if (!super.equals(o)) return false;
- HierarchicalTypeDefinition<?> that = (HierarchicalTypeDefinition<?>) o;
- return Objects.equals(superTypes, that.superTypes) &&
- Objects.equals(hierarchicalMetaTypeName, that.hierarchicalMetaTypeName);
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(super.hashCode(), superTypes, hierarchicalMetaTypeName);
- }
-}
http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/main/java/org/apache/atlas/typesystem/types/HierarchicalTypeDependencySorter.java
----------------------------------------------------------------------
diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/HierarchicalTypeDependencySorter.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/HierarchicalTypeDependencySorter.java
deleted file mode 100644
index aaec05c..0000000
--- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/HierarchicalTypeDependencySorter.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/**
- * 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.atlas.typesystem.types;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import com.google.common.collect.ImmutableSet;
-
-
-/**
- * Sorts hierarchical types by supertype dependency
- */
-public class HierarchicalTypeDependencySorter {
-
- /**
- * Sorts the specified hierarchical types by supertype dependencies,
- * such that any type A which is a supertype of type B
- * will always be located earlier in the result list; that is, the supertype
- * A would be found at some index i and subtype B would be found at some index j,
- * and i < j.
- *
- * @param types hierarchical types to be sorted
- * @return hierarchical types sorted by supertype dependency
- */
- public static <T extends HierarchicalType> List<T> sortTypes(List<T> types) {
- Map<String, T> typesByName = new HashMap<>();
- for (T type : types) {
- typesByName.put(type.name, type);
- }
- List<T> result = new ArrayList<>(types.size());
- Set<T> processed = new HashSet<>();
- for (T type : types) {
- addToResult(type, result, processed, typesByName);
- }
- return result;
- }
-
- private static <T extends HierarchicalType> void addToResult(T type, List<T> result,
- Set<T> processed, Map<String, T> typesByName) {
-
- if (processed.contains(type)) {
- return;
- }
- processed.add(type);
- ImmutableSet<String> superTypeNames = type.superTypes;
- for (String superTypeName : superTypeNames) {
- // Recursively add any supertypes first to the result.
- T superType = typesByName.get(superTypeName);
- if (superType != null) {
- addToResult(superType, result, processed, typesByName);
- }
- }
- result.add(type);
- }
-}
http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/main/java/org/apache/atlas/typesystem/types/IConstructableType.java
----------------------------------------------------------------------
diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/IConstructableType.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/IConstructableType.java
deleted file mode 100755
index d54da0a..0000000
--- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/IConstructableType.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/**
- * 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.atlas.typesystem.types;
-
-
-import org.apache.atlas.AtlasException;
-import org.apache.atlas.typesystem.ITypedInstance;
-
-import java.util.List;
-
-public interface IConstructableType<U, T extends ITypedInstance> extends IDataType<U> {
-
- T createInstance() throws AtlasException;
-
- FieldMapping fieldMapping();
-
- List<String> getNames(AttributeInfo info);
-}
http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/main/java/org/apache/atlas/typesystem/types/IDataType.java
----------------------------------------------------------------------
diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/IDataType.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/IDataType.java
deleted file mode 100755
index a7a2123..0000000
--- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/IDataType.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/**
- * 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.atlas.typesystem.types;
-
-import org.apache.atlas.AtlasException;
-
-import java.security.MessageDigest;
-import java.util.Set;
-
-public interface IDataType<T> {
- String getName();
-
- T convert(Object val, Multiplicity m) throws AtlasException;
-
- DataTypes.TypeCategory getTypeCategory();
-
- /**
- * Output a string representation of a value instance of this type.
- *
- * @param val
- * @param buf
- * @param prefix
- * @param inProcess
- * @throws AtlasException
- */
- void output(T val, Appendable buf, String prefix, Set<T> inProcess) throws AtlasException;
-
- /**
- * Output a string representation of this type.
- *
- * @param buf
- * @param typesInProcess
- * @throws AtlasException
- */
- void output(Appendable buf, Set<String> typesInProcess) throws AtlasException;
-
- void validateUpdate(IDataType newType) throws TypeUpdateException;
-
- void updateSignatureHash(MessageDigest digester, Object val) throws AtlasException;
-
- String getDescription();
-
- String getVersion();
-}
-
http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/main/java/org/apache/atlas/typesystem/types/Multiplicity.java
----------------------------------------------------------------------
diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/Multiplicity.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/Multiplicity.java
deleted file mode 100755
index c213d75..0000000
--- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/Multiplicity.java
+++ /dev/null
@@ -1,90 +0,0 @@
-/**
- * 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.atlas.typesystem.types;
-
-import org.codehaus.jettison.json.JSONException;
-import org.codehaus.jettison.json.JSONObject;
-
-import java.util.Objects;
-
-public final class Multiplicity {
-
- public static final Multiplicity OPTIONAL = new Multiplicity(0, 1, false);
- public static final Multiplicity REQUIRED = new Multiplicity(1, 1, false);
- public static final Multiplicity COLLECTION = new Multiplicity(1, Integer.MAX_VALUE, false);
- public static final Multiplicity SET = new Multiplicity(1, Integer.MAX_VALUE, true);
-
- public final int lower;
- public final int upper;
- public final boolean isUnique;
-
- public Multiplicity(int lower, int upper, boolean isUnique) {
- assert lower >= 0;
- assert upper >= 1;
- assert upper >= lower;
- this.lower = lower;
- this.upper = upper;
- this.isUnique = isUnique;
- }
-
- public boolean isMany() {
- return upper > 1;
- }
-
-
- public boolean nullAllowed() {
- return lower == 0;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Multiplicity that = (Multiplicity) o;
- return lower == that.lower &&
- upper == that.upper &&
- isUnique == that.isUnique;
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(lower, upper, isUnique);
- }
-
- @Override
- public String toString() {
- return "{lower=" + lower +
- ", upper=" + upper +
- ", isUnique=" + isUnique +
- '}';
- }
-
- public String toJson() throws JSONException {
- JSONObject json = new JSONObject();
- json.put("lower", lower);
- json.put("upper", upper);
- json.put("isUnique", isUnique);
- return json.toString();
- }
-
- public static Multiplicity fromJson(String jsonStr) throws JSONException {
- JSONObject json = new JSONObject(jsonStr);
- return new Multiplicity(json.getInt("lower"), json.getInt("upper"), json.getBoolean("isUnique"));
- }
-}
http://git-wip-us.apache.org/repos/asf/atlas/blob/435fe3fb/typesystem/src/main/java/org/apache/atlas/typesystem/types/ObjectGraphTraversal.java
----------------------------------------------------------------------
diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/ObjectGraphTraversal.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/ObjectGraphTraversal.java
deleted file mode 100755
index 9a1847c..0000000
--- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/ObjectGraphTraversal.java
+++ /dev/null
@@ -1,199 +0,0 @@
-/**
- * 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.atlas.typesystem.types;
-
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-import org.apache.atlas.AtlasException;
-import org.apache.atlas.typesystem.IReferenceableInstance;
-import org.apache.atlas.typesystem.IStruct;
-import org.apache.atlas.typesystem.persistence.Id;
-
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.Map;
-import java.util.Queue;
-import java.util.Set;
-
-public class ObjectGraphTraversal implements Iterator<ObjectGraphTraversal.InstanceTuple> {
-
- final Queue<InstanceTuple> queue;
- final TypeSystem typeSystem;
- Set<Id> processedIds;
-
- public ObjectGraphTraversal(TypeSystem typeSystem, IReferenceableInstance start) throws AtlasException {
- this.typeSystem = typeSystem;
- queue = new LinkedList<>();
- processedIds = new HashSet<>();
- processReferenceableInstance(start);
- }
-
- void processValue(IDataType dT, Object val) throws AtlasException {
- if (val != null) {
- if (dT.getTypeCategory() == DataTypes.TypeCategory.ARRAY) {
- IDataType elemType = ((DataTypes.ArrayType) dT).getElemType();
- processCollection(elemType, val);
- } else if (dT.getTypeCategory() == DataTypes.TypeCategory.MAP) {
- IDataType keyType = ((DataTypes.MapType) dT).getKeyType();
- IDataType valueType = ((DataTypes.MapType) dT).getValueType();
- processMap(keyType, valueType, val);
- } else if (dT.getTypeCategory() == DataTypes.TypeCategory.STRUCT
- || dT.getTypeCategory() == DataTypes.TypeCategory.TRAIT) {
- processStruct(val);
- } else if (dT.getTypeCategory() == DataTypes.TypeCategory.CLASS) {
- processReferenceableInstance(val);
- }
- }
- }
-
- void processMap(IDataType keyType, IDataType valueType, Object val) throws AtlasException {
- if (keyType.getTypeCategory() == DataTypes.TypeCategory.PRIMITIVE
- && valueType.getTypeCategory() == DataTypes.TypeCategory.PRIMITIVE) {
- return;
- }
-
- if (val != null) {
- Iterator<Map.Entry> it = null;
- if (Map.class.isAssignableFrom(val.getClass())) {
- it = ((Map) val).entrySet().iterator();
- ImmutableMap.Builder b = ImmutableMap.builder();
- while (it.hasNext()) {
- Map.Entry e = it.next();
- processValue(keyType, e.getKey());
- processValue(valueType, e.getValue());
- }
- }
- }
- }
-
- void processCollection(IDataType elemType, Object val) throws AtlasException {
-
- if (elemType.getTypeCategory() == DataTypes.TypeCategory.PRIMITIVE) {
- return;
- }
-
- if (val != null) {
- Iterator it = null;
- if (val instanceof Collection) {
- it = ((Collection) val).iterator();
- } else if (val instanceof Iterable) {
- it = ((Iterable) val).iterator();
- } else if (val instanceof Iterator) {
- it = (Iterator) val;
- }
- if (it != null) {
- DataTypes.TypeCategory elemCategory = elemType.getTypeCategory();
- while (it.hasNext()) {
- Object elem = it.next();
- processValue(elemType, elem);
- }
- }
- }
- }
-
- void processStruct(Object val) throws AtlasException {
-
- if (val == null || !(val instanceof IStruct)) {
- return;
- }
-
- IStruct i = (IStruct) val;
-
- IConstructableType type = typeSystem.getDataType(IConstructableType.class, i.getTypeName());
-
- for (Map.Entry<String, AttributeInfo> e : type.fieldMapping().fields.entrySet()) {
- AttributeInfo aInfo = e.getValue();
- String attrName = e.getKey();
- if (aInfo.dataType().getTypeCategory() != DataTypes.TypeCategory.PRIMITIVE) {
- processValue(aInfo.dataType(), i.get(attrName));
- }
- }
- }
-
- void processReferenceableInstance(Object val) throws AtlasException {
-
- if (val == null || !(val instanceof IReferenceableInstance || val instanceof Id)) {
- return;
- }
-
- if (val instanceof Id) {
- Id id = (Id) val;
- if (id.isUnassigned()) {
- add(id, null);
- }
- return;
- }
-
- IReferenceableInstance ref = (IReferenceableInstance) val;
- Id id = ref.getId();
- if (id.isUnassigned()) {
- add(id, ref);
- if (!processedIds.contains(id)) {
- processedIds.add(id);
- processStruct(val);
-
- ImmutableList<String> traits = ref.getTraits();
- for (String trait : traits) {
- processStruct(ref.getTrait(trait));
- }
- }
- }
- }
-
- void add(Id id, IReferenceableInstance ref) {
- queue.add(new InstanceTuple(id, ref));
- }
-
-
- @Override
- public boolean hasNext() {
- return !queue.isEmpty();
- }
-
- @Override
- public InstanceTuple next() {
- try {
- InstanceTuple t = queue.poll();
- if(t != null) {
- processReferenceableInstance(t.instance);
- }
- return t;
- } catch (AtlasException me) {
- throw new RuntimeException(me);
- }
- }
-
- @Override
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- public static class InstanceTuple {
- public final Id id;
- public final IReferenceableInstance instance;
-
- public InstanceTuple(Id id, IReferenceableInstance instance) {
- this.id = id;
- this.instance = instance;
- }
- }
-
-}