http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/f8fe0945/repository/src/main/java/org/apache/atlas/repository/store/graph/AtlasTypeDefGraphStore.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/AtlasTypeDefGraphStore.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/AtlasTypeDefGraphStore.java new file mode 100644 index 0000000..9c0e569 --- /dev/null +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/AtlasTypeDefGraphStore.java @@ -0,0 +1,451 @@ +/** + * 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.repository.store.graph; + +import org.apache.atlas.GraphTransaction; +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.SearchFilter; +import org.apache.atlas.model.typedef.AtlasBaseTypeDef; +import org.apache.atlas.model.typedef.AtlasClassificationDef; +import org.apache.atlas.model.typedef.AtlasClassificationDef.AtlasClassificationDefs; +import org.apache.atlas.model.typedef.AtlasEntityDef; +import org.apache.atlas.model.typedef.AtlasEntityDef.AtlasEntityDefs; +import org.apache.atlas.model.typedef.AtlasEnumDef; +import org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumDefs; +import org.apache.atlas.model.typedef.AtlasStructDef; +import org.apache.atlas.model.typedef.AtlasStructDef.AtlasStructDefs; +import org.apache.atlas.model.typedef.AtlasTypesDef; +import org.apache.atlas.repository.util.FilterUtil; +import org.apache.atlas.store.AtlasTypeDefStore; +import org.apache.atlas.util.TypeDefSorter; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.Predicate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collections; +import java.util.List; + + +/** + * Abstract class for graph persistence store for TypeDef + */ +public abstract class AtlasTypeDefGraphStore implements AtlasTypeDefStore { + + private static final Logger LOG = LoggerFactory.getLogger(AtlasTypeDefGraphStore.class); + + protected AtlasEnumDefStore enumDefStore; + protected AtlasStructDefStore structDefStore; + protected AtlasClassificationDefStore classificationDefStore; + protected AtlasEntityDefStore entityDefStore; + + protected AtlasTypeDefGraphStore() { + } + + @Override + public void init() throws AtlasBaseException { + + } + + @Override + @GraphTransaction + public AtlasEnumDef createEnumDef(AtlasEnumDef enumDef) throws AtlasBaseException { + return enumDefStore.create(enumDef); + } + + @Override + @GraphTransaction + public List<AtlasEnumDef> createEnumDefs(List<AtlasEnumDef> atlasEnumDefs) throws AtlasBaseException { + return enumDefStore.create(atlasEnumDefs); + } + + @Override + @GraphTransaction + public List<AtlasEnumDef> getAllEnumDefs() throws AtlasBaseException { + return enumDefStore.getAll(); + } + + @Override + @GraphTransaction + public AtlasEnumDef getEnumDefByName(String name) throws AtlasBaseException { + return enumDefStore.getByName(name); + } + + @Override + @GraphTransaction + public AtlasEnumDef getEnumDefByGuid(String guid) throws AtlasBaseException { + return enumDefStore.getByGuid(guid); + } + + @Override + @GraphTransaction + public AtlasEnumDef updateEnumDefByName(String name, AtlasEnumDef enumDef) throws AtlasBaseException { + return enumDefStore.updateByName(name, enumDef); + } + + @Override + @GraphTransaction + public AtlasEnumDef updateEnumDefByGuid(String guid, AtlasEnumDef enumDef) throws AtlasBaseException { + return enumDefStore.updateByGuid(guid, enumDef); + } + + @Override + @GraphTransaction + public void deleteEnumDefByName(String name) throws AtlasBaseException { + enumDefStore.deleteByName(name); + } + + @Override + @GraphTransaction + public void deleteEnumDefByGuid(String guid) throws AtlasBaseException { + enumDefStore.deleteByGuid(guid); + } + + @Override + @GraphTransaction + public AtlasEnumDefs searchEnumDefs(SearchFilter filter) throws AtlasBaseException { + return enumDefStore.search(filter); + } + + @Override + @GraphTransaction + public AtlasStructDef createStructDef(AtlasStructDef structDef) throws AtlasBaseException { + return structDefStore.create(structDef); + } + + @Override + @GraphTransaction + public List<AtlasStructDef> createStructDefs(List<AtlasStructDef> structDefs) throws AtlasBaseException { + return structDefStore.create(structDefs); + } + + @Override + @GraphTransaction + public List<AtlasStructDef> getAllStructDefs() throws AtlasBaseException { + return structDefStore.getAll(); + } + + @Override + @GraphTransaction + public AtlasStructDef getStructDefByName(String name) throws AtlasBaseException { + return structDefStore.getByName(name); + } + + @Override + @GraphTransaction + public AtlasStructDef getStructDefByGuid(String guid) throws AtlasBaseException { + return structDefStore.getByGuid(guid); + } + + @Override + @GraphTransaction + public AtlasStructDef updateStructDefByName(String name, AtlasStructDef structDef) throws AtlasBaseException { + return structDefStore.updateByName(name, structDef); + } + + @Override + @GraphTransaction + public AtlasStructDef updateStructDefByGuid(String guid, AtlasStructDef structDef) throws AtlasBaseException { + return structDefStore.updateByGuid(guid, structDef); + } + + @Override + @GraphTransaction + public void deleteStructDefByName(String name) throws AtlasBaseException { + structDefStore.deleteByName(name); + } + + @Override + @GraphTransaction + public void deleteStructDefByGuid(String guid) throws AtlasBaseException { + structDefStore.deleteByGuid(guid); + } + + @Override + @GraphTransaction + public AtlasStructDefs searchStructDefs(SearchFilter filter) throws AtlasBaseException { + return structDefStore.search(filter); + } + + @Override + @GraphTransaction + public AtlasClassificationDef createClassificationDef(AtlasClassificationDef classificationDef) throws AtlasBaseException { + return classificationDefStore.create(classificationDef); + } + + @Override + @GraphTransaction + public List<AtlasClassificationDef> createClassificationDefs(List<AtlasClassificationDef> classificationDefs) throws AtlasBaseException { + return classificationDefStore.create(classificationDefs); + } + + @Override + @GraphTransaction + public List<AtlasClassificationDef> getAllClassificationDefs() throws AtlasBaseException { + return classificationDefStore.getAll(); + } + + @Override + @GraphTransaction + public AtlasClassificationDef getClassificationDefByName(String name) throws AtlasBaseException { + return classificationDefStore.getByName(name); + } + + @Override + @GraphTransaction + public AtlasClassificationDef getClassificationDefByGuid(String guid) throws AtlasBaseException { + return classificationDefStore.getByGuid(guid); + } + + @Override + @GraphTransaction + public AtlasClassificationDef updateClassificationDefByName(String name, AtlasClassificationDef classificationDef) throws AtlasBaseException { + return classificationDefStore.updateByName(name, classificationDef); + } + + @Override + @GraphTransaction + public AtlasClassificationDef updateClassificationDefByGuid(String guid, AtlasClassificationDef classificationDef) throws AtlasBaseException { + return classificationDefStore.updateByGuid(guid, classificationDef); + } + + @Override + @GraphTransaction + public void deleteClassificationDefByName(String name) throws AtlasBaseException { + classificationDefStore.deleteByName(name); + } + + @Override + @GraphTransaction + public void deleteClassificationDefByGuid(String guid) throws AtlasBaseException { + classificationDefStore.deleteByGuid(guid); + } + + @Override + @GraphTransaction + public AtlasClassificationDefs searchClassificationDefs(SearchFilter filter) throws AtlasBaseException { + return classificationDefStore.search(filter); + } + + @Override + @GraphTransaction + public AtlasEntityDef createEntityDefs(AtlasEntityDef entityDef) throws AtlasBaseException { + return entityDefStore.create(entityDef); + } + + @Override + @GraphTransaction + public List<AtlasEntityDef> createEntityDefs(List<AtlasEntityDef> entityDefs) throws AtlasBaseException { + return entityDefStore.create(entityDefs); + } + + @Override + @GraphTransaction + public List<AtlasEntityDef> getAllEntityDefs() throws AtlasBaseException { + return entityDefStore.getAll(); + } + + @Override + @GraphTransaction + public AtlasEntityDef getEntityDefByName(String name) throws AtlasBaseException { + return entityDefStore.getByName(name); + } + + @Override + @GraphTransaction + public AtlasEntityDef getEntityDefByGuid(String guid) throws AtlasBaseException { + return entityDefStore.getByGuid(guid); + } + + @Override + @GraphTransaction + public AtlasEntityDef updateEntityDefByName(String name, AtlasEntityDef entityDef) throws AtlasBaseException { + return entityDefStore.updateByName(name, entityDef); + } + + @Override + @GraphTransaction + public AtlasEntityDef updateEntityDefByGuid(String guid, AtlasEntityDef entityDef) throws AtlasBaseException { + return entityDefStore.updateByGuid(guid, entityDef); + } + + @Override + @GraphTransaction + public void deleteEntityDefByName(String name) throws AtlasBaseException { + entityDefStore.deleteByName(name); + } + + @Override + @GraphTransaction + public void deleteEntityDefByGuid(String guid) throws AtlasBaseException { + entityDefStore.deleteByGuid(guid); + } + + @Override + @GraphTransaction + public AtlasEntityDefs searchEntityDefs(SearchFilter filter) throws AtlasBaseException { + return entityDefStore.search(filter); + } + + private List<? extends AtlasBaseTypeDef> createOrUpdateTypeDefs(List<? extends AtlasBaseTypeDef> typeDefs, boolean isUpdate) { + List<AtlasBaseTypeDef> ret = Collections.emptyList(); + + if (CollectionUtils.isNotEmpty(typeDefs)) { + AtlasBaseTypeDef typeDef = typeDefs.get(0); + if (LOG.isDebugEnabled()) { + if (isUpdate) { + LOG.debug("Updating {} {}", typeDefs.size(), typeDef.getClass().getSimpleName()); + } else { + LOG.debug("Creating {} {}", typeDefs.size(), typeDef.getClass().getSimpleName()); + } + } + + if (typeDef instanceof AtlasEntityDef) { + List<AtlasEntityDef> entityDefs = TypeDefSorter.sortTypes((List<AtlasEntityDef>) typeDefs); + try { + if (isUpdate) { + return entityDefStore.update((List<AtlasEntityDef>) typeDefs); + } else { + return entityDefStore.create((List<AtlasEntityDef>) typeDefs); + } + } catch (AtlasBaseException ex) { + LOG.error("Failed to " + (isUpdate ? "update" : "create") + " EntityDefs", ex); + } + } else if (typeDef instanceof AtlasClassificationDef) { + List<AtlasClassificationDef> classificationDefs = + TypeDefSorter.sortTypes((List<AtlasClassificationDef>) typeDefs); + try { + if (isUpdate) { + return classificationDefStore.update((List<AtlasClassificationDef>) typeDefs); + } else { + return classificationDefStore.create((List<AtlasClassificationDef>) typeDefs); + } + } catch (AtlasBaseException ex) { + LOG.error("Failed to " + (isUpdate ? "update" : "create") + " ClassificationDefs", ex); + } + + } else if (typeDef instanceof AtlasStructDef) { + try { + if (isUpdate) { + return structDefStore.update((List<AtlasStructDef>) typeDefs); + } else { + return structDefStore.create((List<AtlasStructDef>) typeDefs); + } + } catch (AtlasBaseException ex) { + LOG.error("Failed to " + (isUpdate ? "update" : "create") + " StructDefs", ex); + } + } else if (typeDef instanceof AtlasEnumDef) { + try { + if (isUpdate) { + return enumDefStore.update((List<AtlasEnumDef>) typeDefs); + } else { + return enumDefStore.create((List<AtlasEnumDef>) typeDefs); + } + } catch (AtlasBaseException ex) { + LOG.error("Failed to " + (isUpdate ? "update" : "create") + " EnumDefs", ex); + } + } + } + return ret; + } + + @Override + @GraphTransaction + public AtlasTypesDef createTypesDef(AtlasTypesDef typesDef) throws AtlasBaseException { + AtlasTypesDef createdTypeDefs = new AtlasTypesDef(); + + LOG.info("Creating EnumDefs"); + List<? extends AtlasBaseTypeDef> createdEnumDefs = createOrUpdateTypeDefs(typesDef.getEnumDefs(), false); + LOG.info("Creating StructDefs"); + List<? extends AtlasBaseTypeDef> createdStructDefs = createOrUpdateTypeDefs(typesDef.getStructDefs(), false); + LOG.info("Creating ClassificationDefs"); + List<? extends AtlasBaseTypeDef> createdClassificationDefs = createOrUpdateTypeDefs(typesDef.getClassificationDefs(), false); + LOG.info("Creating EntityDefs"); + List<? extends AtlasBaseTypeDef> createdEntityDefs = createOrUpdateTypeDefs(typesDef.getEntityDefs(), false); + + typesDef.setEnumDefs((List<AtlasEnumDef>) createdEnumDefs); + typesDef.setStructDefs((List<AtlasStructDef>) createdStructDefs); + typesDef.setClassificationDefs((List<AtlasClassificationDef>) createdClassificationDefs); + typesDef.setEntityDefs((List<AtlasEntityDef>) createdEntityDefs); + + return typesDef; + } + + @Override + @GraphTransaction + public AtlasTypesDef updateTypesDef(AtlasTypesDef typesDef) throws AtlasBaseException { + AtlasTypesDef createdTypeDefs = new AtlasTypesDef(); + + LOG.info("Updating EnumDefs"); + List<? extends AtlasBaseTypeDef> updatedEnumDefs = createOrUpdateTypeDefs(typesDef.getEnumDefs(), true); + LOG.info("Updating StructDefs"); + List<? extends AtlasBaseTypeDef> updatedStructDefs = createOrUpdateTypeDefs(typesDef.getStructDefs(), true); + LOG.info("Updating ClassificationDefs"); + List<? extends AtlasBaseTypeDef> updatedClassficationDefs = createOrUpdateTypeDefs(typesDef.getClassificationDefs(), true); + LOG.info("Updating EntityDefs"); + List<? extends AtlasBaseTypeDef> updatedEntityDefs = createOrUpdateTypeDefs(typesDef.getEntityDefs(), true); + + typesDef.setEnumDefs((List<AtlasEnumDef>) updatedEnumDefs); + typesDef.setStructDefs((List<AtlasStructDef>) updatedStructDefs); + typesDef.setClassificationDefs((List<AtlasClassificationDef>) updatedClassficationDefs); + typesDef.setEntityDefs((List<AtlasEntityDef>) updatedEntityDefs); + + return typesDef; + + } + + @Override + @GraphTransaction + public AtlasTypesDef searchTypesDef(SearchFilter searchFilter) throws AtlasBaseException { + AtlasTypesDef typesDef = new AtlasTypesDef(); + Predicate searchPredicates = FilterUtil.getPredicateFromSearchFilter(searchFilter); + try { + List<AtlasEnumDef> enumDefs = enumDefStore.getAll(); + CollectionUtils.filter(enumDefs, searchPredicates); + typesDef.setEnumDefs(enumDefs); + } catch (AtlasBaseException ex) { + LOG.error("Failed to retrieve the EnumDefs", ex); + } + + try { + List<AtlasStructDef> structDefs = structDefStore.getAll(); + CollectionUtils.filter(structDefs, searchPredicates); + typesDef.setStructDefs(structDefs); + } catch (AtlasBaseException ex) { + LOG.error("Failed to retrieve the StructDefs", ex); + } + + try { + List<AtlasClassificationDef> classificationDefs = classificationDefStore.getAll(); + CollectionUtils.filter(classificationDefs, searchPredicates); + typesDef.setClassificationDefs(classificationDefs); + } catch (AtlasBaseException ex) { + LOG.error("Failed to retrieve the ClassificationDefs", ex); + } + + try { + List<AtlasEntityDef> entityDefs = entityDefStore.getAll(); + CollectionUtils.filter(entityDefs, searchPredicates); + typesDef.setEntityDefs(entityDefs); + } catch (AtlasBaseException ex) { + LOG.error("Failed to retrieve the EntityDefs", ex); + } + + return typesDef; + } +}
http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/f8fe0945/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasClassificationDefStoreV1.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasClassificationDefStoreV1.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasClassificationDefStoreV1.java new file mode 100644 index 0000000..9afccbe --- /dev/null +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasClassificationDefStoreV1.java @@ -0,0 +1,363 @@ +/** + * 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.repository.store.graph.v1; + + +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.SearchFilter; +import org.apache.atlas.model.typedef.AtlasClassificationDef; +import org.apache.atlas.model.typedef.AtlasClassificationDef.AtlasClassificationDefs; +import org.apache.atlas.repository.Constants; +import org.apache.atlas.repository.graphdb.AtlasVertex; +import org.apache.atlas.repository.store.graph.AtlasClassificationDefStore; +import org.apache.atlas.repository.util.FilterUtil; +import org.apache.atlas.typesystem.types.DataTypes.TypeCategory; +import org.apache.commons.collections.CollectionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import sun.security.provider.certpath.Vertex; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +/** + * ClassificationDef store in v1 format. + */ +public class AtlasClassificationDefStoreV1 implements AtlasClassificationDefStore { + private static final Logger LOG = LoggerFactory.getLogger(AtlasClassificationDefStoreV1.class); + + private final AtlasTypeDefGraphStoreV1 typeDefStore; + + public AtlasClassificationDefStoreV1(AtlasTypeDefGraphStoreV1 typeDefStore) { + super(); + + this.typeDefStore = typeDefStore; + } + + @Override + public AtlasClassificationDef create(AtlasClassificationDef classificationDef) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasClassificationDefStoreV1.create({})", classificationDef); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByName(classificationDef.getName()); + + if (vertex != null) { + throw new AtlasBaseException(classificationDef.getName() + ": type already exists"); + } + + vertex = typeDefStore.createTypeVertex(classificationDef); + + toVertex(classificationDef, vertex); + + AtlasClassificationDef ret = toClassificationDef(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasClassificationDefStoreV1.create({}): {}", classificationDef, ret); + } + + return ret; + } + + @Override + public List<AtlasClassificationDef> create(List<AtlasClassificationDef> classificationDefs) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasClassificationDefStoreV1.create({})", classificationDefs); + } + List<AtlasClassificationDef> classificationDefList = new LinkedList<>(); + for (AtlasClassificationDef structDef : classificationDefs) { + try { + AtlasClassificationDef atlasClassificationDef = create(structDef); + classificationDefList.add(atlasClassificationDef); + } catch (AtlasBaseException baseException) { + LOG.error("Failed to create {}", structDef); + LOG.error("Exception: {}", baseException); + } + } + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasClassificationDefStoreV1.create({}, {})", classificationDefs, classificationDefList); + } + return classificationDefList; + } + + @Override + public List<AtlasClassificationDef> getAll() throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasClassificationDefStoreV1.getAll()"); + } + + List<AtlasClassificationDef> classificationDefs = new LinkedList<>(); + Iterator<AtlasVertex> verticesByCategory = typeDefStore.findTypeVerticesByCategory(TypeCategory.TRAIT); + while (verticesByCategory.hasNext()) { + AtlasClassificationDef classificationDef = toClassificationDef(verticesByCategory.next()); + classificationDefs.add(classificationDef); + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasClassificationDefStoreV1.getAll()"); + } + return classificationDefs; + } + + @Override + public AtlasClassificationDef getByName(String name) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasClassificationDefStoreV1.getByName({})", name); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByNameAndCategory(name, TypeCategory.TRAIT); + + if (vertex == null) { + throw new AtlasBaseException("no classificationDef exists with name " + name); + } + + vertex.getProperty(Constants.TYPE_CATEGORY_PROPERTY_KEY, TypeCategory.class); + + AtlasClassificationDef ret = toClassificationDef(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasClassificationDefStoreV1.getByName({}): {}", name, ret); + } + + return ret; + } + + @Override + public AtlasClassificationDef getByGuid(String guid) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasClassificationDefStoreV1.getByGuid({})", guid); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByGuidAndCategory(guid, TypeCategory.TRAIT); + + if (vertex == null) { + throw new AtlasBaseException("no classificationDef exists with guid " + guid); + } + + AtlasClassificationDef ret = toClassificationDef(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasClassificationDefStoreV1.getByGuid({}): {}", guid, ret); + } + + return ret; + } + + @Override + public AtlasClassificationDef updateByName(String name, AtlasClassificationDef classificationDef) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasClassificationDefStoreV1.updateByName({}, {})", name, classificationDef); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByNameAndCategory(name, TypeCategory.TRAIT); + + if (vertex == null) { + throw new AtlasBaseException("no classificationDef exists with name " + name); + } + + toVertex(classificationDef, vertex); + + AtlasClassificationDef ret = toClassificationDef(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasClassificationDefStoreV1.updateByName({}, {}): {}", name, classificationDef, ret); + } + + return ret; + } + + @Override + public AtlasClassificationDef updateByGuid(String guid, AtlasClassificationDef classificationDef) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasClassificationDefStoreV1.updateByGuid({})", guid); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByGuidAndCategory(guid, TypeCategory.TRAIT); + + if (vertex == null) { + throw new AtlasBaseException("no classificationDef exists with guid " + guid); + } + + toVertex(classificationDef, vertex); + + AtlasClassificationDef ret = toClassificationDef(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasClassificationDefStoreV1.updateByGuid({}): {}", guid, ret); + } + + return ret; + } + + @Override + public List<AtlasClassificationDef> update(List<AtlasClassificationDef> classificationDefs) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasClassificationDefStoreV1.update({})", classificationDefs); + } + + List<AtlasClassificationDef> updatedClassificationDefs = new ArrayList<>(); + + for (AtlasClassificationDef classificationDef : classificationDefs) { + try { + AtlasClassificationDef updatedDef = updateByName(classificationDef.getName(), classificationDef); + updatedClassificationDefs.add(updatedDef); + } catch (AtlasBaseException ex) { + LOG.error("Failed to update {}", classificationDef); + } + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasClassificationDefStoreV1.update({}): {}", classificationDefs, updatedClassificationDefs); + } + + return updatedClassificationDefs; + } + + @Override + public void deleteByName(String name) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasClassificationDefStoreV1.deleteByName({})", name); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByNameAndCategory(name, TypeCategory.TRAIT); + + if (vertex == null) { + throw new AtlasBaseException("no classificationDef exists with name " + name); + } + + typeDefStore.deleteTypeVertex(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasClassificationDefStoreV1.deleteByName({})", name); + } + } + + @Override + public void deleteByNames(List<String> names) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasClassificationDefStoreV1.deleteByNames({})", names); + } + + for (String name : names) { + try { + deleteByName(name); + } catch (AtlasBaseException ex) { + LOG.error("Failed to delete {}", name); + } + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasClassificationDefStoreV1.deleteByNames({})", names); + } + } + + @Override + public void deleteByGuid(String guid) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasClassificationDefStoreV1.deleteByGuid({})", guid); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByGuidAndCategory(guid, TypeCategory.TRAIT); + + if (vertex == null) { + throw new AtlasBaseException("no classificationDef exists with guid " + guid); + } + + typeDefStore.deleteTypeVertex(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasClassificationDefStoreV1.deleteByGuid({})", guid); + } + } + + @Override + public void deleteByGuids(List<String> guids) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasClassificationDefStoreV1.deleteByGuids({})", guids); + } + + for (String guid : guids) { + try { + deleteByGuid(guid); + } catch (AtlasBaseException ex) { + LOG.error("Failed to delete {}", guid); + } + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasClassificationDefStoreV1.deleteByGuids({})", guids); + } + + } + + @Override + public AtlasClassificationDefs search(SearchFilter filter) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasClassificationDefStoreV1.search({})", filter); + } + + List<AtlasClassificationDef> classificationDefs = new ArrayList<AtlasClassificationDef>(); + + Iterator<AtlasVertex> vertices = typeDefStore.findTypeVerticesByCategory(TypeCategory.TRAIT); + + while(vertices.hasNext()) { + AtlasVertex vertex = vertices.next(); + AtlasClassificationDef classificationDef = toClassificationDef(vertex); + + if (classificationDef != null) { + classificationDefs.add(classificationDef); // TODO: add only if this passes filter + } + } + + if (CollectionUtils.isNotEmpty(classificationDefs)) { + CollectionUtils.filter(classificationDefs, FilterUtil.getPredicateFromSearchFilter(filter)); + } + + + AtlasClassificationDefs ret = new AtlasClassificationDefs(classificationDefs); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasClassificationDefStoreV1.search({}): {}", filter, ret); + } + + return ret; + } + + private void toVertex(AtlasClassificationDef classificationDef, AtlasVertex vertex) { + AtlasStructDefStoreV1.toVertex(classificationDef, vertex, typeDefStore); + + typeDefStore.createSuperTypeEdges(vertex, classificationDef.getSuperTypes()); + } + + private AtlasClassificationDef toClassificationDef(AtlasVertex vertex) { + AtlasClassificationDef ret = null; + + if (vertex != null && typeDefStore.isTypeVertex(vertex, TypeCategory.TRAIT)) { + ret = new AtlasClassificationDef(); + + AtlasStructDefStoreV1.toStructDef(vertex, ret, typeDefStore); + + ret.setSuperTypes(typeDefStore.getSuperTypeNames(vertex)); + } + + return ret; + } +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/f8fe0945/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityDefStoreV1.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityDefStoreV1.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityDefStoreV1.java new file mode 100644 index 0000000..f36c0d6 --- /dev/null +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityDefStoreV1.java @@ -0,0 +1,360 @@ +/** + * 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.repository.store.graph.v1; + +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.SearchFilter; +import org.apache.atlas.model.typedef.AtlasEntityDef; +import org.apache.atlas.model.typedef.AtlasEntityDef.AtlasEntityDefs; +import org.apache.atlas.model.typedef.AtlasStructDef; +import org.apache.atlas.repository.Constants; +import org.apache.atlas.repository.graphdb.AtlasVertex; +import org.apache.atlas.repository.store.graph.AtlasEntityDefStore; +import org.apache.atlas.repository.util.FilterUtil; +import org.apache.atlas.typesystem.types.DataTypes.TypeCategory; +import org.apache.commons.collections.CollectionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +/** + * EntityDef store in v1 format. + */ +public class AtlasEntityDefStoreV1 implements AtlasEntityDefStore { + private static final Logger LOG = LoggerFactory.getLogger(AtlasEntityDefStoreV1.class); + + private final AtlasTypeDefGraphStoreV1 typeDefStore; + + public AtlasEntityDefStoreV1(AtlasTypeDefGraphStoreV1 typeDefStore) { + super(); + + this.typeDefStore = typeDefStore; + } + + @Override + public AtlasEntityDef create(AtlasEntityDef entityDef) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEntityDefStoreV1.create({})", entityDef); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByName(entityDef.getName()); + + if (vertex != null) { + throw new AtlasBaseException(entityDef.getName() + ": type already exists"); + } + + vertex = typeDefStore.createTypeVertex(entityDef); + + toVertex(entityDef, vertex); + + AtlasEntityDef ret = toEntityDef(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEntityDefStoreV1.create({}): {}", entityDef, ret); + } + + return ret; + } + + @Override + public List<AtlasEntityDef> create(List<AtlasEntityDef> entityDefs) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEntityDefStoreV1.create({})", entityDefs); + } + List<AtlasEntityDef> entityDefList = new LinkedList<>(); + for (AtlasEntityDef structDef : entityDefs) { + try { + AtlasEntityDef atlasEntityDef = create(structDef); + entityDefList.add(atlasEntityDef); + } catch (AtlasBaseException baseException) { + LOG.error("Failed to create {}", structDef); + LOG.error("Exception: {}", baseException); + } + } + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEntityDefStoreV1.create({}, {})", entityDefs, entityDefList); + } + return entityDefList; + } + + @Override + public List<AtlasEntityDef> getAll() throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEntityDefStoreV1.getAll()"); + } + + List<AtlasEntityDef> entityDefs = new LinkedList<>(); + Iterator<AtlasVertex> verticesByCategory = typeDefStore.findTypeVerticesByCategory(TypeCategory.CLASS); + while (verticesByCategory.hasNext()) { + AtlasEntityDef atlasEntityDef = toEntityDef(verticesByCategory.next()); + entityDefs.add(atlasEntityDef); + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEntityDefStoreV1.getAll()"); + } + return entityDefs; + } + + @Override + public AtlasEntityDef getByName(String name) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEntityDefStoreV1.getByName({})", name); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByNameAndCategory(name, TypeCategory.CLASS); + + if (vertex == null) { + throw new AtlasBaseException("no entityDef exists with name " + name); + } + + vertex.getProperty(Constants.TYPE_CATEGORY_PROPERTY_KEY, TypeCategory.class); + + AtlasEntityDef ret = toEntityDef(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEntityDefStoreV1.getByName({}): {}", name, ret); + } + + return ret; + } + + @Override + public AtlasEntityDef getByGuid(String guid) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEntityDefStoreV1.getByGuid({})", guid); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByGuidAndCategory(guid, TypeCategory.CLASS); + + if (vertex == null) { + throw new AtlasBaseException("no entityDef exists with guid " + guid); + } + + AtlasEntityDef ret = toEntityDef(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEntityDefStoreV1.getByGuid({}): {}", guid, ret); + } + + return ret; + } + + @Override + public AtlasEntityDef updateByName(String name, AtlasEntityDef entityDef) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEntityDefStoreV1.updateByName({}, {})", name, entityDef); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByNameAndCategory(name, TypeCategory.CLASS); + + if (vertex == null) { + throw new AtlasBaseException("no entityDef exists with name " + name); + } + + toVertex(entityDef, vertex); + + AtlasEntityDef ret = toEntityDef(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEntityDefStoreV1.updateByName({}, {}): {}", name, entityDef, ret); + } + + return ret; + } + + @Override + public AtlasEntityDef updateByGuid(String guid, AtlasEntityDef entityDef) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEntityDefStoreV1.updateByGuid({})", guid); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByGuidAndCategory(guid, TypeCategory.CLASS); + + if (vertex == null) { + throw new AtlasBaseException("no entityDef exists with guid " + guid); + } + + toVertex(entityDef, vertex); + + AtlasEntityDef ret = toEntityDef(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEntityDefStoreV1.updateByGuid({}): {}", guid, ret); + } + + return ret; + } + + @Override + public List<AtlasEntityDef> update(List<AtlasEntityDef> entityDefs) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEntityDefStoreV1.update({})", entityDefs); + } + + List<AtlasEntityDef> updatedEntityDefs = new ArrayList<>(); + + for (AtlasEntityDef entityDef : entityDefs) { + try { + AtlasEntityDef atlasEntityDef = updateByName(entityDef.getName(), entityDef); + updatedEntityDefs.add(atlasEntityDef); + } catch (AtlasBaseException ex) { + LOG.error("Failed to update {}", entityDef); + } + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEntityDefStoreV1.update({}): {}", entityDefs, updatedEntityDefs); + } + + return updatedEntityDefs; + } + + @Override + public void deleteByName(String name) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEntityDefStoreV1.deleteByName({})", name); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByNameAndCategory(name, TypeCategory.CLASS); + + if (vertex == null) { + throw new AtlasBaseException("no entityDef exists with name " + name); + } + + typeDefStore.deleteTypeVertex(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEntityDefStoreV1.deleteByName({})", name); + } + } + + @Override + public void deleteByNames(List<String> names) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEntityDefStoreV1.deleteByNames({})", names); + } + + List<AtlasStructDef> updatedDefs = new ArrayList<>(); + + for (String name : names) { + try { + deleteByName(name); + } catch (AtlasBaseException ex) {} + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEntityDefStoreV1.deleteByNames({})", names); + } + } + + @Override + public void deleteByGuid(String guid) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEntityDefStoreV1.deleteByGuid({})", guid); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByGuidAndCategory(guid, TypeCategory.CLASS); + + if (vertex == null) { + throw new AtlasBaseException("no entityDef exists with guid " + guid); + } + + typeDefStore.deleteTypeVertex(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEntityDefStoreV1.deleteByGuid({})", guid); + } + } + + @Override + public void deleteByGuids(List<String> guids) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEntityDefStoreV1.deleteByGuids({})", guids); + } + + List<AtlasStructDef> updatedDefs = new ArrayList<>(); + + for (String guid : guids) { + try { + deleteByGuid(guid); + } catch (AtlasBaseException ex) {} + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEntityDefStoreV1.deleteByGuids({})", guids); + } + } + + @Override + public AtlasEntityDefs search(SearchFilter filter) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEntityDefStoreV1.search({})", filter); + } + + List<AtlasEntityDef> entityDefs = new ArrayList<AtlasEntityDef>(); + + Iterator<AtlasVertex> vertices = typeDefStore.findTypeVerticesByCategory(TypeCategory.CLASS); + + while(vertices.hasNext()) { + AtlasVertex vertex = vertices.next(); + AtlasEntityDef entityDef = toEntityDef(vertex); + + if (entityDef != null) { + entityDefs.add(entityDef); + } + } + + if (CollectionUtils.isNotEmpty(entityDefs)) { + CollectionUtils.filter(entityDefs, FilterUtil.getPredicateFromSearchFilter(filter)); + } + + + AtlasEntityDefs ret = new AtlasEntityDefs(entityDefs); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEntityDefStoreV1.search({}): {}", filter, ret); + } + + return ret; + } + + private void toVertex(AtlasEntityDef entityDef, AtlasVertex vertex) { + AtlasStructDefStoreV1.toVertex(entityDef, vertex, typeDefStore); + + typeDefStore.createSuperTypeEdges(vertex, entityDef.getSuperTypes()); + } + + private AtlasEntityDef toEntityDef(AtlasVertex vertex) { + AtlasEntityDef ret = null; + + if (vertex != null && typeDefStore.isTypeVertex(vertex, TypeCategory.CLASS)) { + ret = new AtlasEntityDef(); + + AtlasStructDefStoreV1.toStructDef(vertex, ret, typeDefStore); + + ret.setSuperTypes(typeDefStore.getSuperTypeNames(vertex)); + } + + return ret; + } +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/f8fe0945/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEnumDefStoreV1.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEnumDefStoreV1.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEnumDefStoreV1.java new file mode 100644 index 0000000..67ff1a0 --- /dev/null +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEnumDefStoreV1.java @@ -0,0 +1,390 @@ +/** + * 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.repository.store.graph.v1; + +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.SearchFilter; +import org.apache.atlas.model.typedef.AtlasEnumDef; +import org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumDefs; +import org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef; +import org.apache.atlas.repository.Constants; +import org.apache.atlas.repository.graphdb.AtlasVertex; +import org.apache.atlas.repository.store.graph.AtlasEnumDefStore; +import org.apache.atlas.repository.util.FilterUtil; +import org.apache.atlas.typesystem.types.DataTypes.TypeCategory; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +/** + * EnumDef store in v1 format. + */ +public class AtlasEnumDefStoreV1 implements AtlasEnumDefStore { + private static final Logger LOG = LoggerFactory.getLogger(AtlasEnumDefStoreV1.class); + + private final AtlasTypeDefGraphStoreV1 typeDefStore; + + public AtlasEnumDefStoreV1(AtlasTypeDefGraphStoreV1 typeDefStore) { + super(); + + this.typeDefStore = typeDefStore; + } + + @Override + public AtlasEnumDef create(AtlasEnumDef enumDef) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEnumDefStoreV1.create({})", enumDef); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByName(enumDef.getName()); + + if (vertex != null) { + throw new AtlasBaseException(enumDef.getName() + ": type already exists"); + } + + vertex = typeDefStore.createTypeVertex(enumDef); + + toVertex(enumDef, vertex); + + AtlasEnumDef ret = toEnumDef(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEnumDefStoreV1.create({}): {}", enumDef, ret); + } + + return ret; + } + + @Override + public List<AtlasEnumDef> create(List<AtlasEnumDef> atlasEnumDefs) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEnumDefStoreV1.create({})", atlasEnumDefs); + } + List<AtlasEnumDef> enumDefList = new LinkedList<>(); + for (AtlasEnumDef enumDef : atlasEnumDefs) { + try { + AtlasEnumDef atlasEnumDef = create(enumDef); + enumDefList.add(atlasEnumDef); + } catch (AtlasBaseException baseException) { + LOG.error("Failed to create {}", enumDef); + LOG.error("Exception: {}", baseException); + } + } + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEnumDefStoreV1.create({}, {})", atlasEnumDefs, enumDefList); + } + return enumDefList; + } + + @Override + public List<AtlasEnumDef> getAll() throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEnumDefStoreV1.getAll()"); + } + + List<AtlasEnumDef> enumDefs = new LinkedList<>(); + Iterator<AtlasVertex> verticesByCategory = typeDefStore.findTypeVerticesByCategory(TypeCategory.ENUM); + while (verticesByCategory.hasNext()) { + AtlasEnumDef enumDef = toEnumDef(verticesByCategory.next()); + enumDefs.add(enumDef); + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEnumDefStoreV1.getAll()"); + } + return enumDefs; + } + + @Override + public AtlasEnumDef getByName(String name) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEnumDefStoreV1.getByName({})", name); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByNameAndCategory(name, TypeCategory.ENUM); + + if (vertex == null) { + throw new AtlasBaseException("no enumdef exists with name " + name); + } + + vertex.getProperty(Constants.TYPE_CATEGORY_PROPERTY_KEY, TypeCategory.class); + + AtlasEnumDef ret = toEnumDef(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEnumDefStoreV1.getByName({}): {}", name, ret); + } + + return ret; + } + + @Override + public AtlasEnumDef getByGuid(String guid) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEnumDefStoreV1.getByGuid({})", guid); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByGuidAndCategory(guid, TypeCategory.ENUM); + + if (vertex == null) { + throw new AtlasBaseException("no enumdef exists with guid " + guid); + } + + AtlasEnumDef ret = toEnumDef(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEnumDefStoreV1.getByGuid({}): {}", guid, ret); + } + + return ret; + } + + @Override + public AtlasEnumDef updateByName(String name, AtlasEnumDef enumDef) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEnumDefStoreV1.updateByName({}, {})", name, enumDef); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByNameAndCategory(name, TypeCategory.ENUM); + + if (vertex == null) { + throw new AtlasBaseException("no enumdef exists with name " + name); + } + + toVertex(enumDef, vertex); + + AtlasEnumDef ret = toEnumDef(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEnumDefStoreV1.updateByName({}, {}): {}", name, enumDef, ret); + } + + return ret; + } + + @Override + public AtlasEnumDef updateByGuid(String guid, AtlasEnumDef enumDef) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEnumDefStoreV1.updateByGuid({})", guid); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByGuidAndCategory(guid, TypeCategory.ENUM); + + if (vertex == null) { + throw new AtlasBaseException("no enumdef exists with guid " + guid); + } + + toVertex(enumDef, vertex); + + AtlasEnumDef ret = toEnumDef(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEnumDefStoreV1.updateByGuid({}): {}", guid, ret); + } + + return ret; + } + + @Override + public List<AtlasEnumDef> update(List<AtlasEnumDef> enumDefs) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEnumDefStoreV1.update({})", enumDefs); + } + + List<AtlasEnumDef> updatedEnumDefs = new ArrayList<>(); + + for (AtlasEnumDef enumDef : enumDefs) { + try { + AtlasEnumDef updatedDef = updateByName(enumDef.getName(), enumDef); + updatedEnumDefs.add(updatedDef); + } catch (AtlasBaseException ex) { + LOG.error("Failed to update {}", enumDef); + } + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEnumDefStoreV1.update({}): {}", enumDefs, updatedEnumDefs); + } + + return updatedEnumDefs; + } + + @Override + public void deleteByName(String name) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEnumDefStoreV1.deleteByName({})", name); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByNameAndCategory(name, TypeCategory.ENUM); + + if (vertex == null) { + throw new AtlasBaseException("no enumdef exists with name " + name); + } + + typeDefStore.deleteTypeVertex(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEnumDefStoreV1.deleteByName({})", name); + } + } + + @Override + public void deleteByNames(List<String> names) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEnumDefStoreV1.deleteByNames({})", names); + } + + for (String name : names) { + try { + deleteByName(name); + } catch (AtlasBaseException ex) { + LOG.error("Failed to delete {}", name); + } + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEnumDefStoreV1.deleteByName({})", names); + } + } + + @Override + public void deleteByGuid(String guid) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEnumDefStoreV1.deleteByGuid({})", guid); + } + + AtlasVertex vertex = typeDefStore.findTypeVertexByGuidAndCategory(guid, TypeCategory.ENUM); + + if (vertex == null) { + throw new AtlasBaseException("no enumdef exists with guid " + guid); + } + + typeDefStore.deleteTypeVertex(vertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEnumDefStoreV1.deleteByGuid({})", guid); + } + } + + @Override + public void deleteByGuids(List<String> guids) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEnumDefStoreV1.deleteByGuids({})", guids); + } + + for (String guid : guids) { + try { + deleteByGuid(guid); + } catch (AtlasBaseException ex) { + LOG.error("Failed to delete {}", guid); + } + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEnumDefStoreV1.deleteByGuids({})", guids); + } + } + + @Override + public AtlasEnumDefs search(SearchFilter filter) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasEnumDefStoreV1.search({})", filter); + } + + List<AtlasEnumDef> enumDefs = new ArrayList<AtlasEnumDef>(); + + Iterator<AtlasVertex> vertices = typeDefStore.findTypeVerticesByCategory(TypeCategory.ENUM); + + while(vertices.hasNext()) { + AtlasVertex vertex = vertices.next(); + AtlasEnumDef enumDef = toEnumDef(vertex); + + if (enumDef != null) { + enumDefs.add(enumDef); + } + } + + if (CollectionUtils.isNotEmpty(enumDefs)) { + CollectionUtils.filter(enumDefs, FilterUtil.getPredicateFromSearchFilter(filter)); + } + + AtlasEnumDefs ret = new AtlasEnumDefs(enumDefs); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasEnumDefStoreV1.search({}): {}", filter, ret); + } + + return ret; + } + + private void toVertex(AtlasEnumDef enumDef, AtlasVertex vertex) { + List<String> values = new ArrayList<>(enumDef.getElementDefs().size()); + + for (AtlasEnumElementDef element : enumDef.getElementDefs()) { + String elemKey = AtlasGraphUtilsV1.getPropertyKey(enumDef, element.getValue()); + + AtlasGraphUtilsV1.setProperty(vertex, elemKey, element.getOrdinal()); + + if (StringUtils.isNoneBlank(element.getDescription())) { + String descKey = AtlasGraphUtilsV1.getPropertyKey(elemKey, "description"); + + AtlasGraphUtilsV1.setProperty(vertex, descKey, element.getDescription()); + } + + values.add(element.getValue()); + } + AtlasGraphUtilsV1.setProperty(vertex, AtlasGraphUtilsV1.getPropertyKey(enumDef), values); + } + + private AtlasEnumDef toEnumDef(AtlasVertex vertex) { + AtlasEnumDef ret = null; + + if (vertex != null && typeDefStore.isTypeVertex(vertex, TypeCategory.ENUM)) { + ret = toEnumDef(vertex, new AtlasEnumDef(), typeDefStore); + } + + return ret; + } + + private static AtlasEnumDef toEnumDef(AtlasVertex vertex, AtlasEnumDef enumDef, AtlasTypeDefGraphStoreV1 typeDefStore) { + AtlasEnumDef ret = enumDef != null ? enumDef : new AtlasEnumDef(); + + typeDefStore.vertexToTypeDef(vertex, ret); + + List<AtlasEnumElementDef> elements = new ArrayList<>(); + List<String> elemValues = vertex.getProperty(AtlasGraphUtilsV1.getPropertyKey(ret), List.class); + for (String elemValue : elemValues) { + String elemKey = AtlasGraphUtilsV1.getPropertyKey(ret, elemValue); + String descKey = AtlasGraphUtilsV1.getPropertyKey(elemKey, "description"); + + Integer ordinal = AtlasGraphUtilsV1.getProperty(vertex, elemKey, Integer.class); + String desc = AtlasGraphUtilsV1.getProperty(vertex, descKey, String.class); + + elements.add(new AtlasEnumElementDef(elemValue, desc, ordinal)); + } + ret.setElementDefs(elements); + + return ret; + } +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/f8fe0945/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasGraphUtilsV1.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasGraphUtilsV1.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasGraphUtilsV1.java new file mode 100644 index 0000000..cb389a9 --- /dev/null +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasGraphUtilsV1.java @@ -0,0 +1,189 @@ +/** + * 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.repository.store.graph.v1; + +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; + +import org.apache.atlas.model.typedef.AtlasBaseTypeDef; +import org.apache.atlas.repository.Constants; +import org.apache.atlas.repository.graphdb.AtlasEdge; +import org.apache.atlas.repository.graphdb.AtlasElement; +import org.apache.atlas.repository.graphdb.AtlasVertex; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collection; +import java.util.HashMap; + +/** + * Utility methods for Graph. + */ +public class AtlasGraphUtilsV1 { + private static final Logger LOG = LoggerFactory.getLogger(AtlasGraphUtilsV1.class); + + public static final String PROPERTY_PREFIX = Constants.INTERNAL_PROPERTY_KEY_PREFIX + "type."; + public static final String SUPERTYPE_EDGE_LABEL = PROPERTY_PREFIX + ".supertype"; + public static final String VERTEX_TYPE = "typeSystem"; + + public static final BiMap<String, String> RESERVED_CHARS_ENCODE_MAP = + HashBiMap.create(new HashMap<String, String>() {{ + put("{", "_o"); + put("}", "_c"); + put("\"", "_q"); + put("$", "_d"); + put("%", "_p"); + }}); + + + public static String getPropertyKey(AtlasBaseTypeDef typeDef) { + return getPropertyKey(typeDef.getName()); + } + + public static String getPropertyKey(AtlasBaseTypeDef typeDef, String child) { + return getPropertyKey(typeDef.getName(), child); + } + + public static String getPropertyKey(String typeName) { + return PROPERTY_PREFIX + typeName; + } + + public static String getPropertyKey(String typeName, String child) { + return PROPERTY_PREFIX + typeName + "." + child; + } + + public static String getIdFromVertex(AtlasVertex vertex) { + return vertex.<String>getProperty(Constants.GUID_PROPERTY_KEY, String.class); + } + + public static String getTypeName(AtlasVertex instanceVertex) { + return instanceVertex.getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY, String.class); + } + + public static String getEdgeLabel(String fromNode, String toNode) { + return PROPERTY_PREFIX + "edge." + fromNode + "." + toNode; + } + + public static String encodePropertyKey(String key) { + String ret = key; + + if (StringUtils.isNotBlank(key)) { + for (String str : RESERVED_CHARS_ENCODE_MAP.keySet()) { + ret = ret.replace(str, RESERVED_CHARS_ENCODE_MAP.get(str)); + } + } + + return ret; + } + + public static String decodePropertyKey(String key) { + String ret = key; + + if (StringUtils.isNotBlank(key)) { + for (String encodedStr : RESERVED_CHARS_ENCODE_MAP.values()) { + ret = ret.replace(encodedStr, RESERVED_CHARS_ENCODE_MAP.inverse().get(encodedStr)); + } + } + + return ret; + } + + public static <T extends AtlasElement> void setProperty(T element, String propertyName, Object value) { + if (LOG.isDebugEnabled()) { + LOG.debug("==> setProperty({}, {}, {})", toString(element), propertyName, value); + } + + propertyName = encodePropertyKey(propertyName); + + Object existingValue = element.getProperty(propertyName, Object.class); + + if (value == null || (value instanceof Collection && ((Collection)value).isEmpty())) { + if (existingValue != null) { + if (LOG.isDebugEnabled()) { + LOG.debug("Removing property {} from {}", propertyName, toString(element)); + } + + element.removeProperty(propertyName); + } + } else { + if (!value.equals(existingValue)) { + if (LOG.isDebugEnabled()) { + LOG.debug("Setting property {} in {}", propertyName, toString(element)); + } + + element.setProperty(propertyName, value); + } + } + } + + public static <T extends AtlasElement, O> O getProperty(T element, String propertyName, Class<O> returnType) { + Object property = element.getProperty(encodePropertyKey(propertyName), returnType); + + if (LOG.isDebugEnabled()) { + LOG.debug("getProperty({}, {}) ==> {}", toString(element), propertyName, returnType.cast(property)); + } + + return returnType.cast(property); + } + + private static <T extends AtlasElement> String toString(T element) { + if (element instanceof AtlasVertex) { + return toString((AtlasVertex) element); + } else if (element instanceof AtlasEdge) { + return toString((AtlasEdge)element); + } + + return element.toString(); + } + + public static String toString(AtlasVertex vertex) { + if(vertex == null) { + return "vertex[null]"; + } else { + if (LOG.isDebugEnabled()) { + return getVertexDetails(vertex); + } else { + return String.format("vertex[id=%s]", vertex.getId().toString()); + } + } + } + + + public static String toString(AtlasEdge edge) { + if(edge == null) { + return "edge[null]"; + } else { + if (LOG.isDebugEnabled()) { + return getEdgeDetails(edge); + } else { + return String.format("edge[id=%s]", edge.getId().toString()); + } + } + } + + public static String getVertexDetails(AtlasVertex vertex) { + return String.format("vertex[id=%s type=%s guid=%s]", + vertex.getId().toString(), getTypeName(vertex), getIdFromVertex(vertex)); + } + + public static String getEdgeDetails(AtlasEdge edge) { + return String.format("edge[id=%s label=%s from %s -> to %s]", edge.getId(), edge.getLabel(), + toString(edge.getOutVertex()), toString(edge.getInVertex())); + } +} http://git-wip-us.apache.org/repos/asf/incubator-atlas/blob/f8fe0945/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasStructDefStoreV1.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasStructDefStoreV1.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasStructDefStoreV1.java new file mode 100644 index 0000000..c4e7dcd --- /dev/null +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasStructDefStoreV1.java @@ -0,0 +1,474 @@ +/** + * 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.repository.store.graph.v1; + +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.SearchFilter; +import org.apache.atlas.model.typedef.AtlasStructDef; +import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; +import org.apache.atlas.model.typedef.AtlasStructDef.AtlasStructDefs; +import org.apache.atlas.repository.Constants; +import org.apache.atlas.repository.graphdb.AtlasVertex; +import org.apache.atlas.repository.store.graph.AtlasStructDefStore; +import org.apache.atlas.repository.util.FilterUtil; +import org.apache.atlas.type.AtlasType; +import org.apache.atlas.type.AtlasTypeUtil; +import org.apache.atlas.typesystem.types.DataTypes.TypeCategory; +import org.apache.commons.collections.CollectionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * StructDef store in v1 format. + */ +public class AtlasStructDefStoreV1 implements AtlasStructDefStore { + private static final Logger LOG = LoggerFactory.getLogger(AtlasStructDefStoreV1.class); + + private final AtlasTypeDefGraphStoreV1 typeDefStore; + + public AtlasStructDefStoreV1(AtlasTypeDefGraphStoreV1 typeDefStore) { + super(); + + this.typeDefStore = typeDefStore; + } + + @Override + public AtlasStructDef create(AtlasStructDef structDef) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasStructDefStoreV1.create({})", structDef); + } + + AtlasVertex AtlasVertex = typeDefStore.findTypeVertexByName(structDef.getName()); + + if (AtlasVertex != null) { + throw new AtlasBaseException(structDef.getName() + ": type already exists"); + } + + AtlasVertex = typeDefStore.createTypeVertex(structDef); + + toVertex(structDef, AtlasVertex, typeDefStore); + + AtlasStructDef ret = toStructDef(AtlasVertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasStructDefStoreV1.create({}): {}", structDef, ret); + } + + return ret; + } + + @Override + public List<AtlasStructDef> create(List<AtlasStructDef> structDefs) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasStructDefStoreV1.create({})", structDefs); + } + List<AtlasStructDef> structDefList = new LinkedList<>(); + for (AtlasStructDef structDef : structDefs) { + try { + AtlasStructDef atlasStructDef = create(structDef); + structDefList.add(atlasStructDef); + } catch (AtlasBaseException baseException) { + LOG.error("Failed to create {}", structDef); + LOG.error("Exception: {}", baseException); + } + } + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasStructDefStoreV1.create({}, {})", structDefs, structDefList); + } + return structDefList; + } + + @Override + public List<AtlasStructDef> getAll() throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasStructDefStoreV1.getAll()"); + } + + List<AtlasStructDef> structDefs = new LinkedList<>(); + Iterator<AtlasVertex> verticesByCategory = typeDefStore.findTypeVerticesByCategory(TypeCategory.STRUCT); + while (verticesByCategory.hasNext()) { + AtlasStructDef atlasStructDef = toStructDef(verticesByCategory.next()); + structDefs.add(atlasStructDef); + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasStructDefStoreV1.getAll()"); + } + return structDefs; + } + + @Override + public AtlasStructDef getByName(String name) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasStructDefStoreV1.getByName({})", name); + } + + AtlasVertex atlasVertex = typeDefStore.findTypeVertexByNameAndCategory(name, TypeCategory.STRUCT); + + if (atlasVertex == null) { + throw new AtlasBaseException("no structDef exists with name " + name); + } + + atlasVertex.getProperty(Constants.TYPE_CATEGORY_PROPERTY_KEY, String.class); + + AtlasStructDef ret = toStructDef(atlasVertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasStructDefStoreV1.getByName({}): {}", name, ret); + } + + return ret; + } + + @Override + public AtlasStructDef getByGuid(String guid) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasStructDefStoreV1.getByGuid({})", guid); + } + + AtlasVertex AtlasVertex = typeDefStore.findTypeVertexByGuidAndCategory(guid, TypeCategory.STRUCT); + + if (AtlasVertex == null) { + throw new AtlasBaseException("no structDef exists with guid " + guid); + } + + AtlasStructDef ret = toStructDef(AtlasVertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasStructDefStoreV1.getByGuid({}): {}", guid, ret); + } + + return ret; + } + + @Override + public AtlasStructDef updateByName(String name, AtlasStructDef structDef) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasStructDefStoreV1.updateByName({}, {})", name, structDef); + } + + AtlasVertex atlasVertex = typeDefStore.findTypeVertexByNameAndCategory(name, TypeCategory.STRUCT); + + if (atlasVertex == null) { + throw new AtlasBaseException("no structDef exists with name " + name); + } + + toVertex(structDef, atlasVertex); + + AtlasStructDef ret = toStructDef(atlasVertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasStructDefStoreV1.updateByName({}, {}): {}", name, structDef, ret); + } + + return ret; + } + + @Override + public AtlasStructDef updateByGuid(String guid, AtlasStructDef structDef) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasStructDefStoreV1.updateByGuid({})", guid); + } + + AtlasVertex atlasVertex = typeDefStore.findTypeVertexByGuidAndCategory(guid, TypeCategory.STRUCT); + + if (atlasVertex == null) { + throw new AtlasBaseException("no structDef exists with guid " + guid); + } + + toVertex(structDef, atlasVertex); + + AtlasStructDef ret = toStructDef(atlasVertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasStructDefStoreV1.updateByGuid({}): {}", guid, ret); + } + + return ret; + } + + @Override + public List<AtlasStructDef> update(List<AtlasStructDef> structDefs) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasStructDefStoreV1.update({})", structDefs); + } + + List<AtlasStructDef> updatedDefs = new ArrayList<>(); + + for (AtlasStructDef structDef : structDefs) { + try { + AtlasStructDef updatedDef = updateByName(structDef.getName(), structDef); + updatedDefs.add(updatedDef); + } catch (AtlasBaseException ex) {} + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasStructDefStoreV1.update({}): {}", structDefs, updatedDefs); + } + + return updatedDefs; + } + + @Override + public void deleteByName(String name) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasStructDefStoreV1.deleteByName({})", name); + } + + AtlasVertex AtlasVertex = typeDefStore.findTypeVertexByNameAndCategory(name, TypeCategory.STRUCT); + + if (AtlasVertex == null) { + throw new AtlasBaseException("no structDef exists with name " + name); + } + + typeDefStore.deleteTypeVertex(AtlasVertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasStructDefStoreV1.deleteByName({})", name); + } + } + + @Override + public void deleteByNames(List<String> names) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasStructDefStoreV1.deleteByNames({})", names); + } + + List<AtlasStructDef> updatedDefs = new ArrayList<>(); + + for (String name : names) { + try { + deleteByName(name); + } catch (AtlasBaseException ex) {} + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasStructDefStoreV1.deleteByNames({})", names); + } + } + + @Override + public void deleteByGuid(String guid) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasStructDefStoreV1.deleteByGuid({})", guid); + } + + AtlasVertex AtlasVertex = typeDefStore.findTypeVertexByGuidAndCategory(guid, TypeCategory.STRUCT); + + if (AtlasVertex == null) { + throw new AtlasBaseException("no structDef exists with guid " + guid); + } + + typeDefStore.deleteTypeVertex(AtlasVertex); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasStructDefStoreV1.deleteByGuid({})", guid); + } + } + + @Override + public void deleteByGuids(List<String> guids) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasStructDefStoreV1.deleteByGuids({})", guids); + } + + List<AtlasStructDef> updatedDefs = new ArrayList<>(); + + for (String guid : guids) { + try { + deleteByGuid(guid); + } catch (AtlasBaseException ex) {} + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasStructDefStoreV1.deleteByGuids({})", guids); + } + } + + @Override + public AtlasStructDefs search(SearchFilter filter) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasStructDefStoreV1.search({})", filter); + } + + List<AtlasStructDef> structDefs = new ArrayList<AtlasStructDef>(); + + Iterator<AtlasVertex> vertices = typeDefStore.findTypeVerticesByCategory(TypeCategory.STRUCT); + + while(vertices.hasNext()) { + AtlasVertex AtlasVertex = vertices.next(); + AtlasStructDef structDef = toStructDef(AtlasVertex); + + if (structDef != null) { + structDefs.add(structDef); + } + } + + if (CollectionUtils.isNotEmpty(structDefs)) { + CollectionUtils.filter(structDefs, FilterUtil.getPredicateFromSearchFilter(filter)); + } + + AtlasStructDefs ret = new AtlasStructDefs(structDefs); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasStructDefStoreV1.search({}): {}", filter, ret); + } + + return ret; + } + + private void toVertex(AtlasStructDef structDef, AtlasVertex AtlasVertex) { + toVertex(structDef, AtlasVertex, typeDefStore); + } + + private AtlasStructDef toStructDef(AtlasVertex AtlasVertex) { + AtlasStructDef ret = null; + + if (AtlasVertex != null && typeDefStore.isTypeVertex(AtlasVertex, TypeCategory.STRUCT)) { + ret = toStructDef(AtlasVertex, new AtlasStructDef(), typeDefStore); + } + + return ret; + } + + public static void toVertex(AtlasStructDef structDef, AtlasVertex atlasVertex, AtlasTypeDefGraphStoreV1 typeDefStore) { + List<String> attrNames = new ArrayList<>(structDef.getAttributeDefs().size()); + + for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) { + String propertyKey = AtlasGraphUtilsV1.getPropertyKey(structDef, attributeDef.getName()); + + AtlasGraphUtilsV1.setProperty(atlasVertex, propertyKey, toJsonFromAttributeDef(attributeDef)); + + attrNames.add(attributeDef.getName()); + addReferencesForAttribute(atlasVertex, attributeDef, typeDefStore); + } + AtlasGraphUtilsV1.setProperty(atlasVertex, AtlasGraphUtilsV1.getPropertyKey(structDef), attrNames); + } + + public static AtlasStructDef toStructDef(AtlasVertex atlasVertex, AtlasStructDef structDef, AtlasTypeDefGraphStoreV1 typeDefStore) { + AtlasStructDef ret = (structDef != null) ? structDef :new AtlasStructDef(); + + typeDefStore.vertexToTypeDef(atlasVertex, ret); + + List<AtlasAttributeDef> attributeDefs = new ArrayList<>(); + List<String> attrNames = atlasVertex.getProperty(AtlasGraphUtilsV1.getPropertyKey(ret), List.class); + + if (CollectionUtils.isNotEmpty(attrNames)) { + for (String attrName : attrNames) { + String propertyKey = AtlasGraphUtilsV1.getPropertyKey(ret, attrName); + String attribJson = atlasVertex.getProperty(propertyKey, String.class); + + attributeDefs.add(toAttributeDefFromJson(attribJson)); + } + } + ret.setAttributeDefs(attributeDefs); + + return ret; + } + + private static void addReferencesForAttribute(AtlasVertex atlasVertex, AtlasAttributeDef attributeDef, AtlasTypeDefGraphStoreV1 typeDefStore) { + Set<String> referencedTypeNames = AtlasTypeUtil.getReferencedTypeNames(attributeDef.getTypeName()); + + String AtlasVertexTypeName = atlasVertex.getProperty(Constants.TYPENAME_PROPERTY_KEY, String.class); + + for (String referencedTypeName : referencedTypeNames) { + if (!AtlasTypeUtil.isBuiltInType(referencedTypeName)) { + AtlasVertex referencedTypeAtlasVertex = typeDefStore.findTypeVertexByName(referencedTypeName); + + if (referencedTypeAtlasVertex == null) { + // create atlasVertex? + } + + if (referencedTypeAtlasVertex != null) { + String label = AtlasGraphUtilsV1.getEdgeLabel(AtlasVertexTypeName, attributeDef.getName()); + + typeDefStore.getOrCreateEdge(atlasVertex, referencedTypeAtlasVertex, label); + } + } + } + } + + private static String toJsonFromAttributeDef(AtlasAttributeDef attributeDef) { + Map<String, Object> attribInfo = new HashMap<String, Object>(); + + attribInfo.put("name", attributeDef.getName()); + attribInfo.put("dataType", attributeDef.getTypeName()); + attribInfo.put("isUnique", attributeDef.isUnique()); + attribInfo.put("isIndexable", attributeDef.isIndexable()); + attribInfo.put("isComposite", Boolean.FALSE); + attribInfo.put("reverseAttributeName", ""); + Map<String, Object> multiplicity = new HashMap<String, Object>(); + multiplicity.put("lower", attributeDef.getValuesMinCount()); + multiplicity.put("upper", attributeDef.getValuesMaxCount()); + multiplicity.put("isUnique", AtlasAttributeDef.Cardinality.SET.equals(attributeDef.getCardinality())); + + attribInfo.put("multiplicity", AtlasType.toJson(multiplicity)); + + return AtlasType.toJson(attribInfo); + } + + private static AtlasAttributeDef toAttributeDefFromJson(String json) { + Map attribInfo = AtlasType.fromJson(json, Map.class); + + AtlasAttributeDef ret = new AtlasAttributeDef(); + + ret.setName((String) attribInfo.get("name")); + ret.setTypeName((String) attribInfo.get("dataType")); + ret.setUnique((Boolean) attribInfo.get("isUnique")); + ret.setIndexable((Boolean) attribInfo.get("isIndexable")); + /* + attributeMap.put("isComposite", isComposite); + attributeMap.put("reverseAttributeName", reverseAttributeName); + */ + Map multiplicity = AtlasType.fromJson((String) attribInfo.get("multiplicity"), Map.class); + Number minCount = (Number) multiplicity.get("lower"); + Number maxCount = (Number) multiplicity.get("upper"); + Boolean isUnique = (Boolean) multiplicity.get("isUnique"); + + if (minCount == null || minCount.intValue() == 0) { + ret.setOptional(true); + ret.setValuesMinCount(0); + } else { + ret.setOptional(false); + ret.setValuesMinCount(minCount.intValue()); + } + + if (maxCount == null || maxCount.intValue() < 2) { + ret.setCardinality(AtlasAttributeDef.Cardinality.SINGLE); + ret.setValuesMaxCount(1); + } else { + if (isUnique == null || isUnique == Boolean.FALSE) { + ret.setCardinality(AtlasAttributeDef.Cardinality.LIST); + } else { + ret.setCardinality(AtlasAttributeDef.Cardinality.SET); + } + + ret.setValuesMaxCount(maxCount.intValue()); + } + + return ret; + } +}