LeftTupleSets stagedLeftTuples) {
boolean tupleMemory = true;
LeftTupleMemory ltm = bm.getLeftTupleMemory();
RightTupleMemory rtm = bm.getRightTupleMemory();
ContextEntry[] contextEntry = bm.getContext();
BetaConstraints constraints = existsNode.getRawConstraints();
boolean leftUpdateOptimizationAllowed = existsNode.isLeftUpdateOptimizationAllowed();
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
FastIterator rightIt = existsNode.getRightIterator(rtm);
RightTuple firstRightTuple = existsNode.getFirstRightTuple(leftTuple, rtm, null, rightIt);
// If in memory, remove it, because we'll need to add it anyway if it's not blocked, to ensure iteration order
RightTuple blocker = leftTuple.getBlocker();
if (blocker == null) {
if (leftTuple.getMemory() != null) { // memory can be null, if blocker was deleted in same do loop
ltm.remove(leftTuple);
}
} else {
// check if we changed bucket
if (rtm.isIndexed() && !rightIt.isFullIterator()) {
// if newRightTuple is null, we assume there was a bucket change and that bucket is empty
if (firstRightTuple == null || firstRightTuple.getMemory() != blocker.getMemory()) {
// we changed bucket, so blocker no longer blocks
blocker.removeBlocked(leftTuple);
blocker = null;
}
}
}
constraints.updateFromTuple(contextEntry,
wm,
leftTuple);
if ( !leftUpdateOptimizationAllowed && blocker != null ) {
blocker.removeBlocked(leftTuple);
blocker = null;
}
// if we where not blocked before (or changed buckets), or the previous blocker no longer blocks, then find the next blocker
if (blocker == null || !constraints.isAllowedCachedLeft(contextEntry,
blocker.getFactHandle())) {
if (blocker != null) {
// remove previous blocker if it exists, as we know it doesn't block any more
blocker.removeBlocked(leftTuple);
}
// find first blocker, because it's a modify, we need to start from the beginning again
for (RightTuple newBlocker = firstRightTuple; newBlocker != null; newBlocker = (RightTuple) rightIt.next(newBlocker)) {
if (constraints.isAllowedCachedLeft(contextEntry,
newBlocker.getFactHandle())) {
leftTuple.setBlocker(newBlocker);
newBlocker.addBlocked(leftTuple);
break;
}
}
}
if (leftTuple.getBlocker() == null) {
// not blocked
ltm.add(leftTuple); // add to memory so other fact handles can attempt to match
if (leftTuple.getFirstChild() != null) {
// with previous children, delete
if (leftTuple.getFirstChild() != null) {
LeftTuple childLeftTuple = leftTuple.getFirstChild();
if (childLeftTuple != null) {
// no need to update pctx, as no right available, and pctx will exist on a parent LeftTuple anyway
childLeftTuple = RuleNetworkEvaluator.deleteLeftChild(childLeftTuple, trgLeftTuples, stagedLeftTuples);
}
}
}
// with no previous children. do nothing.
} else if (leftTuple.getFirstChild() == null) {
// blocked, with no previous children, insert
trgLeftTuples.addInsert(sink.createLeftTuple(leftTuple,
sink,
leftTuple.getBlocker().getPropagationContext(), tupleMemory));
} else {
// blocked, with previous children, modify
if (leftTuple.getFirstChild() != null) {
LeftTuple childLeftTuple = leftTuple.getFirstChild();
while (childLeftTuple != null) {
childLeftTuple.setPropagationContext(leftTuple.getBlocker().getPropagationContext());
updateChildLeftTuple(childLeftTuple, stagedLeftTuples, trgLeftTuples);
childLeftTuple.reAddRight();
childLeftTuple = childLeftTuple.getLeftParentNext();
}
}
}
leftTuple.clearStaged();
leftTuple = next;
}
constraints.resetTuple(contextEntry);
}