| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.flwor; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.XPathContext; |
| 11 |
import net.sf.saxon.om.Item; |
| 12 |
import net.sf.saxon.om.SequenceIterator; |
| 13 |
import net.sf.saxon.trans.XPathException; |
| 14 |
import net.sf.saxon.value.Int64Value; |
| 15 |
import net.sf.saxon.value.SequenceExtent; |
| 16 |
|
| 17 |
import java.util.ArrayList; |
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class WindowClausePush extends TuplePush { |
| 25 |
|
| 26 |
private WindowClause windowClause; |
| 27 |
private TuplePush destination; |
| 28 |
List<WindowClause.Window> currentWindows = new ArrayList<>(); |
| 29 |
|
| 30 |
public WindowClausePush(TuplePush destination, WindowClause windowClause) { |
| 31 |
this.windowClause = windowClause; |
| 32 |
this.destination = destination; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
@Override |
| 43 |
public void processTuple(XPathContext context) throws XPathException { |
| 44 |
currentWindows = new ArrayList<>(); |
| 45 |
boolean autoclose = windowClause.isTumblingWindow() && windowClause.getEndCondition() == null; |
| 46 |
Item previousPrevious = null; |
| 47 |
Item previous = null; |
| 48 |
Item current = null; |
| 49 |
Item next = null; |
| 50 |
int position = -1; |
| 51 |
SequenceIterator iter = windowClause.getSequence().iterate(context); |
| 52 |
boolean finished = false; |
| 53 |
while (!finished) { |
| 54 |
previousPrevious = previous; |
| 55 |
previous = current; |
| 56 |
current = next; |
| 57 |
next = iter.next(); |
| 58 |
if (next == null) { |
| 59 |
finished = true; |
| 60 |
|
| 61 |
} |
| 62 |
position++; |
| 63 |
if (position > 0) { |
| 64 |
if ((windowClause.isSlidingWindow() || currentWindows.isEmpty() || autoclose) && |
| 65 |
windowClause.matchesStart(previous, current, next, position, context)) { |
| 66 |
if (autoclose && !currentWindows.isEmpty()) { |
| 67 |
|
| 68 |
WindowClause.Window w = currentWindows.get(0); |
| 69 |
w.endItem = previous; |
| 70 |
w.endPreviousItem = previousPrevious; |
| 71 |
w.endNextItem = current; |
| 72 |
w.endPosition = position - 1; |
| 73 |
despatch(w, context); |
| 74 |
currentWindows.clear(); |
| 75 |
} |
| 76 |
WindowClause.Window window = new WindowClause.Window(); |
| 77 |
window.startPosition = position; |
| 78 |
window.startItem = current; |
| 79 |
window.startPreviousItem = previous; |
| 80 |
window.startNextItem = next; |
| 81 |
window.contents = new ArrayList<>(); |
| 82 |
currentWindows.add(window); |
| 83 |
} |
| 84 |
for (WindowClause.Window active : currentWindows) { |
| 85 |
if (!active.isFinished()) { |
| 86 |
active.contents.add(current); |
| 87 |
} |
| 88 |
} |
| 89 |
if (windowClause.getEndCondition() != null) { |
| 90 |
List<WindowClause.Window> removals = new ArrayList<>(); |
| 91 |
for (WindowClause.Window w : currentWindows) { |
| 92 |
if (!w.isFinished() && windowClause.matchesEnd(w, previous, current, next, position, context)) { |
| 93 |
w.endItem = current; |
| 94 |
w.endPreviousItem = previous; |
| 95 |
w.endNextItem = next; |
| 96 |
w.endPosition = position; |
| 97 |
despatch(w, context); |
| 98 |
if (w.isDespatched()) { |
| 99 |
removals.add(w); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
for (WindowClause.Window w : removals) { |
| 104 |
currentWindows.remove(w); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if (windowClause.isIncludeUnclosedWindows()) { |
| 111 |
for (WindowClause.Window w : currentWindows) { |
| 112 |
w.endItem = current; |
| 113 |
w.endPreviousItem = previous; |
| 114 |
w.endNextItem = null; |
| 115 |
w.endPosition = position; |
| 116 |
despatch(w, context); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
private void despatch(WindowClause.Window w, XPathContext context) throws XPathException { |
| 132 |
|
| 133 |
windowClause.checkWindowContents(w); |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
while (true) { |
| 139 |
int earliestStart = Integer.MAX_VALUE; |
| 140 |
WindowClause.Window earliestWindow = null; |
| 141 |
for (WindowClause.Window u : currentWindows) { |
| 142 |
if (u.startPosition < earliestStart && !u.isDespatched()) { |
| 143 |
earliestStart = u.startPosition; |
| 144 |
earliestWindow = u; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if (earliestWindow == null || !earliestWindow.isFinished()) { |
| 149 |
|
| 150 |
return; |
| 151 |
} else { |
| 152 |
|
| 153 |
|
| 154 |
WindowClause clause = windowClause; |
| 155 |
LocalVariableBinding binding; |
| 156 |
binding = clause.getVariableBinding(WindowClause.WINDOW_VAR); |
| 157 |
context.setLocalVariable(binding.getLocalSlotNumber(), SequenceExtent.makeSequenceExtent(earliestWindow.contents)); |
| 158 |
|
| 159 |
binding = clause.getVariableBinding(WindowClause.START_ITEM); |
| 160 |
if (binding != null) { |
| 161 |
context.setLocalVariable(binding.getLocalSlotNumber(), WindowClause.makeValue(earliestWindow.startItem)); |
| 162 |
} |
| 163 |
binding = clause.getVariableBinding(WindowClause.START_ITEM_POSITION); |
| 164 |
if (binding != null) { |
| 165 |
context.setLocalVariable(binding.getLocalSlotNumber(), new Int64Value(earliestWindow.startPosition)); |
| 166 |
} |
| 167 |
binding = clause.getVariableBinding(WindowClause.START_NEXT_ITEM); |
| 168 |
if (binding != null) { |
| 169 |
context.setLocalVariable(binding.getLocalSlotNumber(), WindowClause.makeValue(earliestWindow.startNextItem)); |
| 170 |
} |
| 171 |
binding = clause.getVariableBinding(WindowClause.START_PREVIOUS_ITEM); |
| 172 |
if (binding != null) { |
| 173 |
context.setLocalVariable(binding.getLocalSlotNumber(), WindowClause.makeValue(earliestWindow.startPreviousItem)); |
| 174 |
} |
| 175 |
binding = clause.getVariableBinding(WindowClause.END_ITEM); |
| 176 |
if (binding != null) { |
| 177 |
context.setLocalVariable(binding.getLocalSlotNumber(), WindowClause.makeValue(earliestWindow.endItem)); |
| 178 |
} |
| 179 |
binding = clause.getVariableBinding(WindowClause.END_ITEM_POSITION); |
| 180 |
if (binding != null) { |
| 181 |
context.setLocalVariable(binding.getLocalSlotNumber(), new Int64Value(earliestWindow.endPosition)); |
| 182 |
} |
| 183 |
binding = clause.getVariableBinding(WindowClause.END_NEXT_ITEM); |
| 184 |
if (binding != null) { |
| 185 |
context.setLocalVariable(binding.getLocalSlotNumber(), WindowClause.makeValue(earliestWindow.endNextItem)); |
| 186 |
} |
| 187 |
binding = clause.getVariableBinding(WindowClause.END_PREVIOUS_ITEM); |
| 188 |
if (binding != null) { |
| 189 |
context.setLocalVariable(binding.getLocalSlotNumber(), WindowClause.makeValue(earliestWindow.endPreviousItem)); |
| 190 |
} |
| 191 |
destination.processTuple(context); |
| 192 |
earliestWindow.isDespatched = true; |
| 193 |
} |
| 194 |
|
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
@Override |
| 202 |
public void close() throws XPathException { |
| 203 |
currentWindows = null; |
| 204 |
destination.close(); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
|