Search in sources :

Example 1 with ReassignableRelation

use of com.thinkaurelius.titan.graphdb.relations.ReassignableRelation in project titan by thinkaurelius.

the class VertexIDAssigner method assignID.

private void assignID(InternalElement element, IDManager.VertexIDType vertexIDType) {
    for (int attempt = 0; attempt < MAX_PARTITION_RENEW_ATTEMPTS; attempt++) {
        long partitionID = -1;
        if (element instanceof TitanSchemaVertex) {
            partitionID = IDManager.SCHEMA_PARTITION;
        } else if (element instanceof TitanVertex) {
            if (vertexIDType == IDManager.VertexIDType.PartitionedVertex)
                partitionID = IDManager.PARTITIONED_VERTEX_PARTITION;
            else
                partitionID = placementStrategy.getPartition(element);
        } else if (element instanceof InternalRelation) {
            InternalRelation relation = (InternalRelation) element;
            if (attempt < relation.getLen()) {
                //On the first attempts, try to use partition of incident vertices
                InternalVertex incident = relation.getVertex(attempt);
                Preconditions.checkArgument(incident.hasId());
                if (!IDManager.VertexIDType.PartitionedVertex.is(incident.longId()) || relation.isProperty()) {
                    partitionID = getPartitionID(incident);
                } else {
                    continue;
                }
            } else {
                partitionID = placementStrategy.getPartition(element);
            }
        }
        try {
            assignID(element, partitionID, vertexIDType);
        } catch (IDPoolExhaustedException e) {
            //try again on a different partition
            continue;
        }
        assert element.hasId();
        //Check if we should assign a different representative of a potential partitioned vertex
        if (element instanceof InternalRelation) {
            InternalRelation relation = (InternalRelation) element;
            if (relation.isProperty() && isPartitionedAt(relation, 0)) {
                //Always assign properties to the canonical representative of a partitioned vertex
                InternalVertex vertex = relation.getVertex(0);
                ((ReassignableRelation) relation).setVertexAt(0, vertex.tx().getInternalVertex(idManager.getCanonicalVertexId(vertex.longId())));
            } else if (relation.isEdge()) {
                for (int pos = 0; pos < relation.getArity(); pos++) {
                    if (isPartitionedAt(relation, pos)) {
                        InternalVertex incident = relation.getVertex(pos);
                        long newPartition;
                        int otherpos = (pos + 1) % 2;
                        if (((InternalRelationType) relation.getType()).multiplicity().isUnique(EdgeDirection.fromPosition(pos))) {
                            //If the relation is unique in the direction, we assign it to the canonical vertex...
                            newPartition = idManager.getPartitionId(idManager.getCanonicalVertexId(incident.longId()));
                        } else if (!isPartitionedAt(relation, otherpos)) {
                            //...else, we assign it to the partition of the non-partitioned vertex...
                            newPartition = getPartitionID(relation.getVertex(otherpos));
                        } else {
                            //...and if such does not exists (i.e. both end vertices are partitioned) we use the hash of the relation id
                            newPartition = idManager.getPartitionHashForId(relation.longId());
                        }
                        if (idManager.getPartitionId(incident.longId()) != newPartition) {
                            ((ReassignableRelation) relation).setVertexAt(pos, incident.tx().getOtherPartitionVertex(incident, newPartition));
                        }
                    }
                }
            }
        }
        return;
    }
    throw new IDPoolExhaustedException("Could not find non-exhausted partition ID Pool after " + MAX_PARTITION_RENEW_ATTEMPTS + " attempts");
}
Also used : ReassignableRelation(com.thinkaurelius.titan.graphdb.relations.ReassignableRelation) TitanSchemaVertex(com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex) InternalVertex(com.thinkaurelius.titan.graphdb.internal.InternalVertex) InternalRelationType(com.thinkaurelius.titan.graphdb.internal.InternalRelationType) InternalRelation(com.thinkaurelius.titan.graphdb.internal.InternalRelation)

Aggregations

InternalRelation (com.thinkaurelius.titan.graphdb.internal.InternalRelation)1 InternalRelationType (com.thinkaurelius.titan.graphdb.internal.InternalRelationType)1 InternalVertex (com.thinkaurelius.titan.graphdb.internal.InternalVertex)1 ReassignableRelation (com.thinkaurelius.titan.graphdb.relations.ReassignableRelation)1 TitanSchemaVertex (com.thinkaurelius.titan.graphdb.types.vertices.TitanSchemaVertex)1