Debian Codegraph @

net/sf/saxon/expr/flwor/WindowClausePush.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-push 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 WindowClausePush extends TuplePush {
25
26 private WindowClause windowClause;
27 private TuplePush destination;
28 /*@Nullable*/ 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 * Notify the availability of the next tuple. Before calling this method,
37 * the supplier of the tuples must set all the variables corresponding
38 * to the supplied tuple in the local stack frame associated with the context object
39 *
40 * @param context the dynamic evaluation context
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 // but still complete this time round the loop
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 // automatically end the previous window
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 // on completion, despatch unclosed windows if required
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 * Despatch a window to the output tuple stream. This essentially involves setting the values of relevant local
124 * variables to the appropriate properties of the window
125 *
126 * @param w the window to be despatched
127 * @param context the dynamic evaluation context
128 * @throws XPathException if anything goes wrong, for example if the window contents have the wrong type
129 */
130
131 private void despatch(WindowClause.Window w, XPathContext context) throws XPathException {
132
133 windowClause.checkWindowContents(w);
134
135 // In ordered mode, we must despatch windows in order of start position not in order of end position.
136 // So we don't despatch it yet if there are unfinished windows with an earlier start position
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 // if the earliest window is unfinished, we can't do anything yet
150 return;
151 } else {
152 // otherwise we can process it now
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 } // and loop round to see if there's another finished window that we can despatch
196 }
197
198 /**
199 * Close the tuple stream, indicating that no more tuples will be supplied
200 */
201 @Override
202 public void close() throws XPathException {
203 currentWindows = null;
204 destination.close();
205 }
206
207
208 }
209
210 // Copyright (c) 2011 Saxonica Limited.