Search in sources :

Example 1 with TitanType

use of com.thinkaurelius.titan.core.TitanType 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)

Example 2 with TitanType

use of com.thinkaurelius.titan.core.TitanType in project titan by thinkaurelius.

the class VertexIterable method iterator.

@Override
public Iterator<InternalVertex> iterator() {
    return new Iterator<InternalVertex>() {

        RecordIterator<Long> iterator = graph.getVertexIDs(tx.getTxHandle());

        InternalVertex nextVertex = nextVertex();

        private InternalVertex nextVertex() {
            InternalVertex v = null;
            while (v == null && iterator.hasNext()) {
                long nextId = iterator.next().longValue();
                v = tx.getExistingVertex(nextId);
                // Filter out deleted vertices and types
                if (v.isRemoved() || (v instanceof TitanType))
                    v = null;
            }
            return v;
        }

        @Override
        public boolean hasNext() {
            return nextVertex != null;
        }

        @Override
        public InternalVertex next() {
            if (!hasNext())
                throw new NoSuchElementException();
            InternalVertex returnVertex = nextVertex;
            nextVertex = nextVertex();
            return returnVertex;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : RecordIterator(com.thinkaurelius.titan.diskstorage.util.RecordIterator) RecordIterator(com.thinkaurelius.titan.diskstorage.util.RecordIterator) Iterator(java.util.Iterator) InternalVertex(com.thinkaurelius.titan.graphdb.internal.InternalVertex) TitanType(com.thinkaurelius.titan.core.TitanType) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

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