http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/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/0877e47c/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/0877e47c/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/0877e47c/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/0877e47c/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/0877e47c/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; - } - } - -} http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/typesystem/src/main/java/org/apache/atlas/typesystem/types/ObjectGraphWalker.java ---------------------------------------------------------------------- diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/ObjectGraphWalker.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/ObjectGraphWalker.java deleted file mode 100755 index 036d18d..0000000 --- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/ObjectGraphWalker.java +++ /dev/null @@ -1,226 +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.List; -import java.util.Map; -import java.util.Queue; -import java.util.Set; - -/** - * Given a IReferenceableInstance, a Walker will traverse the Object Graph - * reachable form the instance. It will invoke the process call on the provided NodeProcessor - * for each non-primitive attribute (Structs, Traits, References, Arrays of Non-Primitives, Maps - * of Non-Primitives) - */ -public class ObjectGraphWalker { - - final Queue<IReferenceableInstance> queue; - final TypeSystem typeSystem; - final NodeProcessor nodeProcessor; - Set<Id> processedIds; - - public ObjectGraphWalker(TypeSystem typeSystem, NodeProcessor nodeProcessor) throws AtlasException { - this(typeSystem, nodeProcessor, (IReferenceableInstance) null); - } - - public ObjectGraphWalker(TypeSystem typeSystem, NodeProcessor nodeProcessor, IReferenceableInstance start) - throws AtlasException { - this.typeSystem = typeSystem; - this.nodeProcessor = nodeProcessor; - queue = new LinkedList<>(); - processedIds = new HashSet<>(); - if (start != null) { - visitReferenceableInstance(start); - } - } - - public ObjectGraphWalker(TypeSystem typeSystem, NodeProcessor nodeProcessor, - List<? extends IReferenceableInstance> roots) throws AtlasException { - this.typeSystem = typeSystem; - this.nodeProcessor = nodeProcessor; - queue = new LinkedList<>(); - processedIds = new HashSet<>(); - for (IReferenceableInstance r : roots) { - visitReferenceableInstance(r); - } - } - - public void walk() throws AtlasException { - while (!queue.isEmpty()) { - IReferenceableInstance r = queue.poll(); - if(r != null) { - processReferenceableInstance(r); - } - } - } - - public void addRoot(IReferenceableInstance root) { - visitReferenceableInstance(root); - } - - void traverseValue(IDataType dT, Object val) throws AtlasException { - if (val != null) { - if (dT.getTypeCategory() == DataTypes.TypeCategory.ARRAY) { - IDataType elemType = ((DataTypes.ArrayType) dT).getElemType(); - visitCollection(elemType, val); - } else if (dT.getTypeCategory() == DataTypes.TypeCategory.MAP) { - IDataType keyType = ((DataTypes.MapType) dT).getKeyType(); - IDataType valueType = ((DataTypes.MapType) dT).getValueType(); - visitMap(keyType, valueType, val); - } else if (dT.getTypeCategory() == DataTypes.TypeCategory.STRUCT - || dT.getTypeCategory() == DataTypes.TypeCategory.TRAIT) { - visitStruct(val); - } else if (dT.getTypeCategory() == DataTypes.TypeCategory.CLASS) { - visitReferenceableInstance(val); - } - } - } - - void visitMap(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(); - traverseValue(keyType, e.getKey()); - traverseValue(valueType, e.getValue()); - } - } - } - } - - void visitCollection(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(); - traverseValue(elemType, elem); - } - } - } - } - - void visitStruct(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) { - Object aVal = i.get(attrName); - nodeProcessor.processNode(new Node(i, attrName, aInfo, aVal)); - traverseValue(aInfo.dataType(), aVal); - } - } - } - - void visitReferenceableInstance(Object val) { - - if (val == null || !(val instanceof IReferenceableInstance)) { - return; - } - - IReferenceableInstance ref = (IReferenceableInstance) val; - - if (!processedIds.contains(ref.getId())) { - processedIds.add(ref.getId()); - if (!(ref instanceof Id)) { - queue.add(ref); - } - } - } - - void processReferenceableInstance(IReferenceableInstance ref) throws AtlasException { - - nodeProcessor.processNode(new Node(ref, null, null, null)); - visitStruct(ref); - ImmutableList<String> traits = ref.getTraits(); - for (String trait : traits) { - visitStruct(ref.getTrait(trait)); - } - } - - public interface NodeProcessor { - - void processNode(Node nd) throws AtlasException; - } - - /** - * Represents a non-primitive value of an instance. - */ - public static class Node { - public final IStruct instance; - public final String attributeName; - public final AttributeInfo aInfo; - public final Object value; - - public Node(IStruct instance, String attributeName, AttributeInfo aInfo, Object value) { - this.instance = instance; - this.attributeName = attributeName; - this.aInfo = aInfo; - this.value = value; - } - - @Override - public String toString(){ - StringBuilder string = new StringBuilder().append(instance).append(aInfo).append(value); - return string.toString(); - } - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/typesystem/src/main/java/org/apache/atlas/typesystem/types/StructType.java ---------------------------------------------------------------------- diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/StructType.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/StructType.java deleted file mode 100755 index 57f2517..0000000 --- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/StructType.java +++ /dev/null @@ -1,280 +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.io.IOException; -import java.nio.charset.Charset; -import java.security.MessageDigest; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.apache.atlas.AtlasConstants; -import org.apache.atlas.AtlasException; -import org.apache.atlas.typesystem.IStruct; -import org.apache.atlas.typesystem.ITypedStruct; - -public class StructType extends AbstractDataType<IStruct> implements IConstructableType<IStruct, ITypedStruct> { - - public final TypeSystem typeSystem; - public final FieldMapping fieldMapping; - public final Map<AttributeInfo, List<String>> infoToNameMap; - public final int numFields; - private final TypedStructHandler handler; - - protected StructType(TypeSystem typeSystem, String name, String description, int numFields) { - this(typeSystem, name, description, AtlasConstants.DEFAULT_TYPE_VERSION, numFields); - } - - protected StructType(TypeSystem typeSystem, String name, String description, String version, int numFields) { - super(name, description, version); - this.typeSystem = typeSystem; - this.fieldMapping = null; - infoToNameMap = null; - this.numFields = numFields; - this.handler = null; - } - - protected StructType(TypeSystem typeSystem, String name, String description, AttributeInfo... fields) - throws AtlasException { - this(typeSystem, name, description, AtlasConstants.DEFAULT_TYPE_VERSION, fields); - } - - protected StructType(TypeSystem typeSystem, String name, String description, String version, AttributeInfo... fields) - throws AtlasException { - super(name, description, version); - this.typeSystem = typeSystem; - this.fieldMapping = constructFieldMapping(fields); - infoToNameMap = TypeUtils.buildAttrInfoToNameMap(this.fieldMapping); - this.numFields = this.fieldMapping.fields.size(); - this.handler = new TypedStructHandler(this); - } - - public FieldMapping fieldMapping() { - return fieldMapping; - } - - /** - * 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); - - StructType newStructType = (StructType) newType; - try { - TypeUtils.validateUpdate(fieldMapping, newStructType.fieldMapping); - } catch (TypeUpdateException e) { - throw new TypeUpdateException(newType, e); - } - } - - protected FieldMapping constructFieldMapping(AttributeInfo... fields) - throws AtlasException { - - Map<String, AttributeInfo> fieldsMap = new LinkedHashMap<>(); - Map<String, Integer> fieldPos = new HashMap<>(); - Map<String, Integer> fieldNullPos = 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; - - for (AttributeInfo i : fields) { - if (fieldsMap.containsKey(i.name)) { - throw new AtlasException( - String.format("Struct defintion cannot contain multiple fields with the same " + "name %s", - i.name)); - } - fieldsMap.put(i.name, i); - fieldNullPos.put(i.name, fieldNullPos.size()); - if (i.dataType() == DataTypes.BOOLEAN_TYPE) { - fieldPos.put(i.name, numBools); - numBools++; - } else if (i.dataType() == DataTypes.BYTE_TYPE) { - fieldPos.put(i.name, numBytes); - numBytes++; - } else if (i.dataType() == DataTypes.SHORT_TYPE) { - fieldPos.put(i.name, numShorts); - numShorts++; - } else if (i.dataType() == DataTypes.INT_TYPE) { - fieldPos.put(i.name, numInts); - numInts++; - } else if (i.dataType() == DataTypes.LONG_TYPE) { - fieldPos.put(i.name, numLongs); - numLongs++; - } else if (i.dataType() == DataTypes.FLOAT_TYPE) { - fieldPos.put(i.name, numFloats); - numFloats++; - } else if (i.dataType() == DataTypes.DOUBLE_TYPE) { - fieldPos.put(i.name, numDoubles); - numDoubles++; - } else if (i.dataType() == DataTypes.BIGINTEGER_TYPE) { - fieldPos.put(i.name, numBigInts); - numBigInts++; - } else if (i.dataType() == DataTypes.BIGDECIMAL_TYPE) { - fieldPos.put(i.name, numBigDecimals); - numBigDecimals++; - } else if (i.dataType() == DataTypes.DATE_TYPE) { - fieldPos.put(i.name, numDates); - numDates++; - } else if (i.dataType() == DataTypes.STRING_TYPE) { - fieldPos.put(i.name, 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(i.name, numArrays); - numArrays++; - } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.MAP) { - fieldPos.put(i.name, numMaps); - numMaps++; - } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.STRUCT - || i.dataType().getTypeCategory() == DataTypes.TypeCategory.TRAIT) { - fieldPos.put(i.name, numStructs); - numStructs++; - } else if (i.dataType().getTypeCategory() == DataTypes.TypeCategory.CLASS) { - fieldPos.put(i.name, numReferenceables); - numReferenceables++; - } else { - throw new AtlasException(String.format("Unknown datatype %s", i.dataType())); - } - } - - return new FieldMapping(fieldsMap, fieldPos, fieldNullPos, numBools, numBytes, numShorts, numInts, numLongs, - numFloats, numDoubles, numBigInts, numBigDecimals, numDates, numStrings, numArrays, numMaps, numStructs, - numReferenceables); - } - - - @Override - public DataTypes.TypeCategory getTypeCategory() { - return DataTypes.TypeCategory.STRUCT; - } - - @Override - public ITypedStruct convert(Object val, Multiplicity m) throws AtlasException { - return handler.convert(val, m); - } - - public ITypedStruct createInstance() { - return handler.createInstance(); - } - - @Override - public void output(IStruct s, Appendable buf, String prefix, Set<IStruct> inProcess) throws AtlasException { - handler.output(s, buf, prefix, inProcess); - } - - @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()); - buf.append("{name=").append(name); - buf.append(", description=").append(description); - buf.append(", fieldMapping.fields=["); - Iterator<AttributeInfo> it = fieldMapping.fields.values().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); - } - } - - @Override - public void updateSignatureHash(MessageDigest digester, Object val) throws AtlasException { - if( !(val instanceof ITypedStruct)) { - throw new IllegalArgumentException("Unexpected value type " + val.getClass().getSimpleName() + ". Expected instance of ITypedStruct"); - } - digester.update(getName().getBytes(Charset.forName("UTF-8"))); - - if(fieldMapping.fields != null && val != null) { - IStruct typedValue = (IStruct) val; - for (AttributeInfo aInfo : fieldMapping.fields.values()) { - Object attrVal = typedValue.get(aInfo.name); - if(attrVal != null) { - aInfo.dataType().updateSignatureHash(digester, attrVal); - } - } - } - } - - public List<String> getNames(AttributeInfo info) { - return infoToNameMap.get(info); - } - -} http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/typesystem/src/main/java/org/apache/atlas/typesystem/types/StructTypeDefinition.java ---------------------------------------------------------------------- diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/StructTypeDefinition.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/StructTypeDefinition.java deleted file mode 100755 index 4f8695b..0000000 --- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/StructTypeDefinition.java +++ /dev/null @@ -1,84 +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.AtlasConstants; -import org.apache.atlas.utils.ParamChecker; - -import java.util.Arrays; -import java.util.Objects; - -public class StructTypeDefinition { - - public final String typeName; - public final String typeDescription;//optional field - public final String typeVersion; - public final AttributeDefinition[] attributeDefinitions; - - protected StructTypeDefinition(String typeName, String typeDescription, boolean validate, - AttributeDefinition... attributeDefinitions) { - this(typeName, typeDescription, AtlasConstants.DEFAULT_TYPE_VERSION, validate, attributeDefinitions); - } - - protected StructTypeDefinition(String typeName, String typeDescription, String typeVersion, boolean validate, - AttributeDefinition... attributeDefinitions) { - this.typeName = ParamChecker.notEmpty(typeName, "Struct type name"); - this.typeDescription = typeDescription; - if (validate) { - ParamChecker.notNullElements(attributeDefinitions, "Attribute definitions"); - } - this.attributeDefinitions = attributeDefinitions; - this.typeVersion = typeVersion; - } - - public StructTypeDefinition(String typeName, AttributeDefinition[] attributeDefinitions) { - this(typeName, null, AtlasConstants.DEFAULT_TYPE_VERSION, attributeDefinitions); - } - - public StructTypeDefinition(String typeName, String typeDescription, - AttributeDefinition[] attributeDefinitions) { - - this(typeName, typeDescription, AtlasConstants.DEFAULT_TYPE_VERSION, attributeDefinitions); - } - - public StructTypeDefinition(String typeName, String typeDescription, String typeVersion, - AttributeDefinition[] attributeDefinitions) { - this.typeName = ParamChecker.notEmpty(typeName, "Struct type name"); - this.typeDescription = typeDescription; - this.typeVersion = typeVersion; - this.attributeDefinitions = ParamChecker.notNullElements(attributeDefinitions, "Attribute definitions"); - } - - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - StructTypeDefinition that = (StructTypeDefinition) o; - return Objects.equals(typeName, that.typeName) && - Objects.equals(typeDescription, that.typeDescription) && - Objects.equals(typeVersion, that.typeVersion) && - Arrays.equals(attributeDefinitions, that.attributeDefinitions); - } - - @Override - public int hashCode() { - return Objects.hash(typeName, typeDescription, typeVersion, attributeDefinitions); - } -} http://git-wip-us.apache.org/repos/asf/atlas/blob/0877e47c/typesystem/src/main/java/org/apache/atlas/typesystem/types/TraitType.java ---------------------------------------------------------------------- diff --git a/typesystem/src/main/java/org/apache/atlas/typesystem/types/TraitType.java b/typesystem/src/main/java/org/apache/atlas/typesystem/types/TraitType.java deleted file mode 100755 index bbb845a..0000000 --- a/typesystem/src/main/java/org/apache/atlas/typesystem/types/TraitType.java +++ /dev/null @@ -1,104 +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 org.apache.atlas.AtlasException; -import org.apache.atlas.typesystem.IStruct; -import org.apache.atlas.typesystem.ITypedStruct; - -import java.nio.charset.Charset; -import java.security.MessageDigest; -import java.util.List; -import java.util.Map; -import java.util.Set; - -public class TraitType extends HierarchicalType<TraitType, IStruct> - implements IConstructableType<IStruct, ITypedStruct> { - - public final Map<AttributeInfo, List<String>> infoToNameMap; - private final TypedStructHandler handler; - - TraitType(TypeSystem typeSystem, String name, String description, ImmutableSet<String> superTraits, int numFields) { - this(typeSystem, name, description, AtlasConstants.DEFAULT_TYPE_VERSION, superTraits, numFields); - } - - TraitType(TypeSystem typeSystem, String name, String description, String version, ImmutableSet<String> superTraits, int numFields) { - super(typeSystem, TraitType.class, name, description, version, superTraits, numFields); - handler = null; - infoToNameMap = null; - } - - TraitType(TypeSystem typeSystem, String name, String description, ImmutableSet<String> superTraits, AttributeInfo... fields) - throws AtlasException { - this(typeSystem, name, description, AtlasConstants.DEFAULT_TYPE_VERSION, superTraits, fields); - } - - TraitType(TypeSystem typeSystem, String name, String description, String version, ImmutableSet<String> superTraits, AttributeInfo... fields) - throws AtlasException { - super(typeSystem, TraitType.class, name, description, version, superTraits, fields); - handler = new TypedStructHandler(this); - infoToNameMap = TypeUtils.buildAttrInfoToNameMap(fieldMapping); - } - - @Override - public DataTypes.TypeCategory getTypeCategory() { - return DataTypes.TypeCategory.TRAIT; - } - - @Override - public ITypedStruct convert(Object val, Multiplicity m) throws AtlasException { - return handler.convert(val, m); - } - - public ITypedStruct createInstance() { - return handler.createInstance(); - } - - @Override - public void output(IStruct s, Appendable buf, String prefix, Set<IStruct> inProcess) throws AtlasException { - handler.output(s, buf, prefix, inProcess); - } - - @Override - public void updateSignatureHash(MessageDigest digester, Object val) throws AtlasException { - if( !(val instanceof ITypedStruct)) { - throw new IllegalArgumentException("Unexpected value type " + val.getClass().getSimpleName() + ". Expected instance of ITypedStruct"); - } - digester.update(getName().getBytes(Charset.forName("UTF-8"))); - - if(fieldMapping.fields != null && val != null) { - IStruct typedValue = (IStruct) val; - for (AttributeInfo aInfo : fieldMapping.fields.values()) { - Object attrVal = typedValue.get(aInfo.name); - if(attrVal != null) { - aInfo.dataType().updateSignatureHash(digester, attrVal); - } - } - } - } - - @Override - public List<String> getNames(AttributeInfo info) { - return infoToNameMap.get(info); - } - -}