Search in sources :

Example 1 with PartitionAssignment

use of com.thinkaurelius.titan.graphdb.database.idassigner.placement.PartitionAssignment in project titan by thinkaurelius.

the class VertexIDAssigner method assignIDs.

public void assignIDs(Iterable<InternalRelation> addedRelations) {
    if (!placementStrategy.supportsBulkPlacement()) {
        for (InternalRelation relation : addedRelations) {
            for (int i = 0; i < relation.getArity(); i++) {
                InternalVertex vertex = relation.getVertex(i);
                if (!vertex.hasId()) {
                    assignID(vertex);
                }
            }
            assignID(relation);
        }
    } else {
        // First, only assign idAuthorities to (real) vertices and types
        Map<InternalVertex, PartitionAssignment> assignments = new HashMap<InternalVertex, PartitionAssignment>();
        for (InternalRelation relation : addedRelations) {
            for (int i = 0; i < relation.getArity(); i++) {
                InternalVertex vertex = relation.getVertex(i);
                if (!vertex.hasId()) {
                    if (vertex instanceof TitanType) {
                        assignID(vertex, DEFAULT_PARTITION);
                    } else {
                        assignments.put(vertex, PartitionAssignment.EMPTY);
                    }
                }
            }
        }
        log.trace("Bulk id assignment for {} vertices", assignments.size());
        for (int attempt = 0; attempt < MAX_PARTITION_RENEW_ATTEMPTS && (assignments != null && !assignments.isEmpty()); attempt++) {
            placementStrategy.getPartitions(assignments);
            Map<InternalVertex, PartitionAssignment> leftOvers = null;
            Iterator<Map.Entry<InternalVertex, PartitionAssignment>> iter = assignments.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry<InternalVertex, PartitionAssignment> entry = iter.next();
                try {
                    assignID(entry.getKey(), entry.getValue().getPartitionID());
                    Preconditions.checkArgument(entry.getKey().hasId());
                } catch (IDPoolExhaustedException e) {
                    if (leftOvers == null)
                        leftOvers = new HashMap<InternalVertex, PartitionAssignment>();
                    leftOvers.put(entry.getKey(), PartitionAssignment.EMPTY);
                    break;
                }
            }
            if (leftOvers != null) {
                while (iter.hasNext()) leftOvers.put(iter.next().getKey(), PartitionAssignment.EMPTY);
                log.debug("Exhausted ID Pool in bulk assignment. Left-over vertices {}", leftOvers.size());
            }
            assignments = leftOvers;
        }
        if (assignments != null && !assignments.isEmpty())
            throw new IDPoolExhaustedException("Could not find non-exhausted partition ID Pool after " + MAX_PARTITION_RENEW_ATTEMPTS + " attempts");
        // Second, assign idAuthorities to relations
        for (InternalRelation relation : addedRelations) {
            for (int pos = 0; pos < relation.getArity(); pos++) {
                try {
                    Preconditions.checkArgument(relation.getVertex(pos).hasId());
                    assignID(relation, getPartitionID(relation.getVertex(pos)));
                    break;
                } catch (IDPoolExhaustedException e) {
                }
            }
            if (!relation.hasId())
                assignID(relation);
        }
    }
}
Also used : HashMap(java.util.HashMap) OpenIntObjectHashMap(cern.colt.map.OpenIntObjectHashMap) InternalRelation(com.thinkaurelius.titan.graphdb.internal.InternalRelation) PartitionAssignment(com.thinkaurelius.titan.graphdb.database.idassigner.placement.PartitionAssignment) InternalVertex(com.thinkaurelius.titan.graphdb.internal.InternalVertex) TitanType(com.thinkaurelius.titan.core.TitanType) HashMap(java.util.HashMap) OpenIntObjectHashMap(cern.colt.map.OpenIntObjectHashMap) Map(java.util.Map) AbstractIntObjectMap(cern.colt.map.AbstractIntObjectMap)

Aggregations

AbstractIntObjectMap (cern.colt.map.AbstractIntObjectMap)1 OpenIntObjectHashMap (cern.colt.map.OpenIntObjectHashMap)1 TitanType (com.thinkaurelius.titan.core.TitanType)1 PartitionAssignment (com.thinkaurelius.titan.graphdb.database.idassigner.placement.PartitionAssignment)1 InternalRelation (com.thinkaurelius.titan.graphdb.internal.InternalRelation)1 InternalVertex (com.thinkaurelius.titan.graphdb.internal.InternalVertex)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1