Search in sources :

Example 1 with AttributeImpl

use of ai.grakn.kb.internal.concept.AttributeImpl in project grakn by graknlabs.

the class EmbeddedGraknTx method fixDuplicateResources.

/**
 * Merges the provided duplicate resources
 *
 * @param resourceVertexIds The resource vertex ids which need to be merged.
 * @return True if a commit is required.
 */
public boolean fixDuplicateResources(String index, Set<ConceptId> resourceVertexIds) {
    // This is done to ensure we merge into the indexed casting.
    Optional<AttributeImpl<?>> mainResourceOp = this.getConcept(Schema.VertexProperty.INDEX, index);
    if (!mainResourceOp.isPresent()) {
        LOG.debug(String.format("Could not post process concept with index {%s} due to not finding the concept", index));
        return false;
    }
    AttributeImpl<?> mainResource = mainResourceOp.get();
    Set<AttributeImpl> duplicates = getDuplicates(mainResource, resourceVertexIds);
    if (duplicates.size() > 0) {
        // Remove any resources associated with this index that are not the main resource
        for (Attribute otherAttribute : duplicates) {
            Stream<Relationship> otherRelations = otherAttribute.relationships();
            // Copy the actual relation
            otherRelations.forEach(otherRelation -> copyRelation(mainResource, otherAttribute, otherRelation));
            // Delete the node
            AttributeImpl.from(otherAttribute).deleteNode();
        }
        // Restore the index
        String newIndex = mainResource.getIndex();
        // NOTE: Vertex Element is used directly here otherwise property is not actually restored!
        // NOTE: Remove or change this line at your own peril!
        mainResource.vertex().element().property(Schema.VertexProperty.INDEX.name(), newIndex);
        return true;
    }
    return false;
}
Also used : Attribute(ai.grakn.concept.Attribute) AttributeImpl(ai.grakn.kb.internal.concept.AttributeImpl) Relationship(ai.grakn.concept.Relationship)

Aggregations

Attribute (ai.grakn.concept.Attribute)1 Relationship (ai.grakn.concept.Relationship)1 AttributeImpl (ai.grakn.kb.internal.concept.AttributeImpl)1