Skip to content

Commit

Permalink
[FIX] Committing of update list.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesLichtenberger committed Aug 13, 2017
1 parent 1b3d392 commit 69ce022
Showing 1 changed file with 19 additions and 10 deletions.
Expand Up @@ -3,13 +3,16 @@
import static com.google.common.base.Preconditions.checkNotNull;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

import org.brackit.xquery.QueryContext;
import org.brackit.xquery.QueryException;
import org.brackit.xquery.update.op.UpdateOp;
import org.brackit.xquery.xdm.Sequence;
import org.sirix.api.XdmNodeReadTrx;
import org.sirix.api.XdmNodeWriteTrx;
import org.sirix.xquery.node.DBNode;
import org.sirix.xquery.node.DBStore;
Expand Down Expand Up @@ -42,16 +45,22 @@ public void applyUpdates() throws QueryException {
getUpdateList() != null ? getUpdateList().list() : Collections.emptyList();

if (!updateList.isEmpty()) {
commit(updateList.get(0).getTarget());
}
}
}
final Function<Sequence, XdmNodeReadTrx> mapDBNodeToWtx = sequence -> {
if (sequence instanceof DBNode) {
final XdmNodeReadTrx trx = ((DBNode) sequence).getTrx();
return trx;
}

private void commit(Sequence item) {
if (item instanceof DBNode) {
final Optional<XdmNodeWriteTrx> trx =
((DBNode) item).getTrx().getResourceManager().getNodeWriteTrx();
trx.ifPresent(XdmNodeWriteTrx::commit);
// Can not occur (must be a write transaction).
throw new IllegalStateException();
};

final Set<Long> trxIDs = new HashSet<>();

updateList.stream().map(UpdateOp::getTarget).map(mapDBNodeToWtx)
.filter(trx -> trxIDs.add(trx.getId()))
.forEach(trx -> ((XdmNodeWriteTrx) trx).commit());
}
}
}
}

0 comments on commit 69ce022

Please sign in to comment.