public void commit(final Collection<InternalRelation> addedRelations,
final Collection<InternalRelation> deletedRelations, final StandardTitanTx tx) {
//Setup
log.debug("Saving transaction. Added {}, removed {}", addedRelations.size(), deletedRelations.size());
final BackendTransaction mutator = tx.getTxHandle();
final boolean acquireLocks = tx.getConfiguration().hasAcquireLocks();
//1. Assign TitanVertex IDs
if (!tx.getConfiguration().hasAssignIDsImmediately())
idAssigner.assignIDs(addedRelations);
Callable<List<StaticBuffer>> persist = new Callable<List<StaticBuffer>>() {
@Override
public List<StaticBuffer> call() throws Exception {
//2. Collect deleted edges
ListMultimap<InternalVertex, InternalRelation> mutations = ArrayListMultimap.create();
if (deletedRelations != null && !deletedRelations.isEmpty()) {
for (InternalRelation del : deletedRelations) {
Preconditions.checkArgument(del.isRemoved());
for (int pos = 0; pos < del.getLen(); pos++) {
InternalVertex vertex = del.getVertex(pos);
if (pos == 0 || !del.isLoop()) mutations.put(vertex, del);
Direction dir = EdgeDirection.fromPosition(pos);
if (acquireLocks && del.getType().isUnique(dir) &&
((InternalType) del.getType()).uniqueLock(dir)) {
Entry entry = edgeSerializer.writeRelation(del, pos, tx);
mutator.acquireEdgeLock(IDHandler.getKey(vertex.getID()), entry.getColumn(), entry.getValue());
}
}
//Update Indexes
if (del.isProperty()) {
if (acquireLocks) indexSerializer.lockKeyedProperty((TitanProperty) del, mutator);
}
}
}
ListMultimap<InternalType, InternalRelation> otherEdgeTypes = ArrayListMultimap.create();
//3. Sort Added Edges
for (InternalRelation relation : addedRelations) {
Preconditions.checkArgument(relation.isNew());
TitanType type = relation.getType();
//Give special treatment to edge type definitions
if (SystemTypeManager.prepersistedSystemTypes.contains(type)) {
InternalType itype = (InternalType) relation.getVertex(0);
otherEdgeTypes.put(itype, relation);
} else { //STANDARD TitanRelation
for (int pos = 0; pos < relation.getLen(); pos++) {
InternalVertex vertex = relation.getVertex(pos);
if (pos == 0 || !relation.isLoop()) mutations.put(vertex, relation);
Direction dir = EdgeDirection.fromPosition(pos);
if (acquireLocks && relation.getType().isUnique(dir) && !vertex.isNew()
&& ((InternalType) relation.getType()).uniqueLock(dir)) {
Entry entry = edgeSerializer.writeRelation(relation, pos, tx);
mutator.acquireEdgeLock(IDHandler.getKey(vertex.getID()), entry.getColumn(), null);
}
}
}
//Update Indexes
if (relation.isProperty()) {
if (acquireLocks) indexSerializer.lockKeyedProperty((TitanProperty) relation, mutator);
}
}
//3. Persist
List<StaticBuffer> mutatedVertexKeys = new ArrayList<StaticBuffer>();
if (!otherEdgeTypes.isEmpty()) {
mutatedVertexKeys.addAll(persist(otherEdgeTypes, tx));
mutator.flush();
//Register new keys with indexprovider
for (InternalType itype : otherEdgeTypes.keySet()) {
if (itype.isPropertyKey() && itype.isNew())
indexSerializer.newPropertyKey((TitanKey) itype, mutator);
}
}
if (!mutations.isEmpty()) mutatedVertexKeys.addAll(persist(mutations, tx));
mutator.commit();
return mutatedVertexKeys;
}
@Override
public String toString() {