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);
}
}
}
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();
}
};
}
Aggregations