Debian Codegraph @

net/sf/saxon/expr/flwor/WindowClausePull.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * Implement a sliding or tumbling window clause of a FLWOR expression in tuple-pull mode. The entire window processing
22 * is activated once for each input tuple, and it generates one output tuple for each identified window.
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 * Move on to the next tuple. Before returning, this method must set all the variables corresponding
46 * to the "returned" tuple in the local stack frame associated with the context object
47 *
48 * @param context the dynamic evaluation context
49 * @return true if another tuple has been generated; false if the tuple stream is exhausted. If the
50 * method returns false, the values of the local variables corresponding to this tuple stream
51 * are undefined.
52 */
53 @Override
54 public boolean nextTuple(XPathContext context) throws XPathException {
55
56 boolean deliver = false;
57
58 // First see if there are any windows waiting to be delivered
59
60 boolean pending = lookForEarliest();
61 if (pending) {
62 return true;
63 }
64
65 // If we're not currently processing an input sequence, try to get a new one
66
67 if (finished || baseIterator == null) {
68 // advance the previous step in the pipeline
69 if (source.nextTuple(context)) {
70 // get an iterator over the input sequence
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 // advance the input sequence
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 // but still complete this time round the loop
94 }
95 position++;
96 if (position > 0) {
97 // See if we need to start a new window
98 if ((windowClause.isSlidingWindow() || currentWindows.isEmpty() || autoclose) &&
99 windowClause.matchesStart(previous, current, next, position, context)) {
100 // See if we need to end the previous window
101 if (autoclose && !currentWindows.isEmpty()) {
102 // automatically end the previous window
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 // Create the new window
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 // Add the current item to all active windows
121 for (WindowClause.Window active : currentWindows) {
122 if (!active.isFinished()) {
123 active.contents.add(current);
124 }
125 }
126 // See if this item marks the end of any active windows
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 // if there's a window ready to be delivered, deliver it
152 if (deliver) {
153 return true;
154 }
155 }
156 }
157 return false;
158 }
159
160 /**
161 * Deliver a tuple (representing a window)
162 *
163 * @param w a Window whose end conditions have just been satisfied
164 * @param context dynamic evaluation context
165 * @return true if a Tuple has been created by writing variables to the context stack; false otherwise
166 * @throws XPathException if anything goes wrong
167 */
168
169 private boolean despatch(WindowClause.Window w, XPathContext context) throws XPathException {
170
171 windowClause.checkWindowContents(w);
172
173 // In ordered mode, we must despatch windows in order of start position not in order of end position.
174 // So we don't despatch it yet if there are unfinished windows with an earlier start position
175
176 return lookForEarliest();
177 }
178
179 /**
180 * Identity the earliest window (that is the one whose start position comes earlier in the input sequence
181 * than any other window) that is available to be despatched to the output tuple stream
182 *
183 * @return true if an earliest window was found; false if there are no windows ready to be despatched
184 * @throws XPathException if anything goes wrong
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 // if the earliest window is unfinished, we can't do anything yet
200 return false;
201 } else {
202 // otherwise we can process it now
203 processWindow(earliestWindow, context);
204 //earliestWindow.isDespatched = true;
205 return true;
206 }
207 }
208
209 /**
210 * Despatch a window to the output tuple stream. This essentially means setting the relevant local variables
211 * on the context stack from information held in the window object.
212 *
213 * @param w the window to be despatched
214 * @param context the dynamic evaluation context
215 * @throws XPathException if anything goes wrong
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 * Close the tuple stream, indicating that no more tuples will be supplied
262 */
263 @Override
264 public void close() {
265 baseIterator.close();
266 source.close();
267 }
268
269 }
270
271 // Copyright (c) 2011 Saxonica Limited.