| 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 WindowClausePull extends TuplePull { |
| 25 |
|
| 26 |
private WindowClause windowClause; |
| 27 |
private TuplePull source; |
| 28 |
private SequenceIterator baseIterator; |
| 29 |
private boolean finished = false; |
| 30 |
private XPathContext context; |
| 31 |
private Item previous = null; |
| 32 |
private Item current = null; |
| 33 |
private Item next = null; |
| 34 |
private int position = -1; |
| 35 |
private List<WindowClause.Window> currentWindows = new ArrayList<>(); |
| 36 |
|
| 37 |
|
| 38 |
public WindowClausePull(TuplePull source, WindowClause windowClause, XPathContext context) { |
| 39 |
this.windowClause = windowClause; |
| 40 |
this.source = source; |
| 41 |
this.context = context; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
@Override |
| 54 |
public boolean nextTuple(XPathContext context) throws XPathException { |
| 55 |
|
| 56 |
boolean deliver = false; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
boolean pending = lookForEarliest(); |
| 61 |
if (pending) { |
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
if (finished || baseIterator == null) { |
| 68 |
|
| 69 |
if (source.nextTuple(context)) { |
| 70 |
|
| 71 |
baseIterator = windowClause.getSequence().iterate(context); |
| 72 |
finished = false; |
| 73 |
previous = null; |
| 74 |
position = -1; |
| 75 |
current = null; |
| 76 |
next = null; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
while (!finished) { |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
boolean autoclose = windowClause.isTumblingWindow() && windowClause.getEndCondition() == null; |
| 85 |
deliver = false; |
| 86 |
|
| 87 |
Item oldPrevious = previous; |
| 88 |
previous = current; |
| 89 |
current = next; |
| 90 |
next = baseIterator.next(); |
| 91 |
if (next == null) { |
| 92 |
finished = true; |
| 93 |
|
| 94 |
} |
| 95 |
position++; |
| 96 |
if (position > 0) { |
| 97 |
|
| 98 |
if ((windowClause.isSlidingWindow() || currentWindows.isEmpty() || autoclose) && |
| 99 |
windowClause.matchesStart(previous, current, next, position, context)) { |
| 100 |
|
| 101 |
if (autoclose && !currentWindows.isEmpty()) { |
| 102 |
|
| 103 |
WindowClause.Window w = currentWindows.get(0); |
| 104 |
w.endItem = previous; |
| 105 |
w.endPreviousItem = oldPrevious; |
| 106 |
w.endNextItem = current; |
| 107 |
w.endPosition = position - 1; |
| 108 |
deliver = despatch(w, context); |
| 109 |
currentWindows.clear(); |
| 110 |
} |
| 111 |
|
| 112 |
WindowClause.Window window = new WindowClause.Window(); |
| 113 |
window.startPosition = position; |
| 114 |
window.startItem = current; |
| 115 |
window.startPreviousItem = previous; |
| 116 |
window.startNextItem = next; |
| 117 |
window.contents = new ArrayList<>(); |
| 118 |
currentWindows.add(window); |
| 119 |
} |
| 120 |
|
| 121 |
for (WindowClause.Window active : currentWindows) { |
| 122 |
if (!active.isFinished()) { |
| 123 |
active.contents.add(current); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
boolean explicitEndCondition = windowClause.getEndCondition() != null; |
| 128 |
boolean implicitEndCondition = finished && windowClause.isIncludeUnclosedWindows(); |
| 129 |
if (explicitEndCondition || implicitEndCondition) { |
| 130 |
List<WindowClause.Window> removals = new ArrayList<>(); |
| 131 |
for (WindowClause.Window w : currentWindows) { |
| 132 |
if (!w.isFinished() && |
| 133 |
(implicitEndCondition || |
| 134 |
windowClause.matchesEnd(w, previous, current, next, position, context))) { |
| 135 |
w.endItem = current; |
| 136 |
w.endPreviousItem = previous; |
| 137 |
w.endNextItem = next; |
| 138 |
w.endPosition = position; |
| 139 |
if (!deliver) { |
| 140 |
deliver = despatch(w, context); |
| 141 |
if (w.isDespatched()) { |
| 142 |
removals.add(w); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
for (WindowClause.Window w : removals) { |
| 148 |
currentWindows.remove(w); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if (deliver) { |
| 153 |
return true; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
private boolean despatch(WindowClause.Window w, XPathContext context) throws XPathException { |
| 170 |
|
| 171 |
windowClause.checkWindowContents(w); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
return lookForEarliest(); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
private boolean lookForEarliest() throws XPathException { |
| 188 |
|
| 189 |
int earliestStart = Integer.MAX_VALUE; |
| 190 |
WindowClause.Window earliestWindow = null; |
| 191 |
for (WindowClause.Window u : currentWindows) { |
| 192 |
if (u.startPosition < earliestStart && !u.isDespatched()) { |
| 193 |
earliestStart = u.startPosition; |
| 194 |
earliestWindow = u; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
if (earliestWindow == null || !earliestWindow.isFinished()) { |
| 199 |
|
| 200 |
return false; |
| 201 |
} else { |
| 202 |
|
| 203 |
processWindow(earliestWindow, context); |
| 204 |
|
| 205 |
return true; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public void processWindow(WindowClause.Window w, XPathContext context) throws XPathException { |
| 219 |
|
| 220 |
WindowClause clause = windowClause; |
| 221 |
LocalVariableBinding binding; |
| 222 |
binding = clause.getVariableBinding(WindowClause.WINDOW_VAR); |
| 223 |
context.setLocalVariable(binding.getLocalSlotNumber(), SequenceExtent.makeSequenceExtent(w.contents)); |
| 224 |
|
| 225 |
binding = clause.getVariableBinding(WindowClause.START_ITEM); |
| 226 |
if (binding != null) { |
| 227 |
context.setLocalVariable(binding.getLocalSlotNumber(), WindowClause.makeValue(w.startItem)); |
| 228 |
} |
| 229 |
binding = clause.getVariableBinding(WindowClause.START_ITEM_POSITION); |
| 230 |
if (binding != null) { |
| 231 |
context.setLocalVariable(binding.getLocalSlotNumber(), new Int64Value(w.startPosition)); |
| 232 |
} |
| 233 |
binding = clause.getVariableBinding(WindowClause.START_NEXT_ITEM); |
| 234 |
if (binding != null) { |
| 235 |
context.setLocalVariable(binding.getLocalSlotNumber(), WindowClause.makeValue(w.startNextItem)); |
| 236 |
} |
| 237 |
binding = clause.getVariableBinding(WindowClause.START_PREVIOUS_ITEM); |
| 238 |
if (binding != null) { |
| 239 |
context.setLocalVariable(binding.getLocalSlotNumber(), WindowClause.makeValue(w.startPreviousItem)); |
| 240 |
} |
| 241 |
binding = clause.getVariableBinding(WindowClause.END_ITEM); |
| 242 |
if (binding != null) { |
| 243 |
context.setLocalVariable(binding.getLocalSlotNumber(), WindowClause.makeValue(w.endItem)); |
| 244 |
} |
| 245 |
binding = clause.getVariableBinding(WindowClause.END_ITEM_POSITION); |
| 246 |
if (binding != null) { |
| 247 |
context.setLocalVariable(binding.getLocalSlotNumber(), new Int64Value(w.endPosition)); |
| 248 |
} |
| 249 |
binding = clause.getVariableBinding(WindowClause.END_NEXT_ITEM); |
| 250 |
if (binding != null) { |
| 251 |
context.setLocalVariable(binding.getLocalSlotNumber(), WindowClause.makeValue(w.endNextItem)); |
| 252 |
} |
| 253 |
binding = clause.getVariableBinding(WindowClause.END_PREVIOUS_ITEM); |
| 254 |
if (binding != null) { |
| 255 |
context.setLocalVariable(binding.getLocalSlotNumber(), WindowClause.makeValue(w.endPreviousItem)); |
| 256 |
} |
| 257 |
w.isDespatched = true; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
@Override |
| 264 |
public void close() { |
| 265 |
baseIterator.close(); |
| 266 |
source.close(); |
| 267 |
} |
| 268 |
|
| 269 |
} |
| 270 |
|
| 271 |
|