Debian Codegraph @

net/sf/saxon/expr/flwor/OrderByClausePull.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.expr.sort.*;
12 import net.sf.saxon.trans.NoDynamicContextException;
13 import net.sf.saxon.trans.XPathException;
14
15 import java.util.ArrayList;
16
17 /**
18 * Represents the tuple stream delivered by an "order by" clause. This sorts the tuple stream supplied
19 * as its input, and outputs the same tuples but in sorted order.
20 */
21 public class OrderByClausePull extends TuplePull implements Sortable {
22
23 private TuplePull base;
24 private OrderByClause orderByClause;
25 private TupleExpression tupleExpr;
26 private int currentPosition = -1;
27 protected AtomicComparer[] comparers;
28 private ArrayList<ItemToBeSorted> tupleArray = new ArrayList<ItemToBeSorted>(100);
29
30 public OrderByClausePull(TuplePull base, TupleExpression tupleExpr, OrderByClause orderBy, XPathContext context) {
31 this.base = base;
32 this.tupleExpr = tupleExpr;
33 this.orderByClause = orderBy;
34
35 AtomicComparer[] suppliedComparers = orderBy.getAtomicComparers();
36 comparers = new AtomicComparer[suppliedComparers.length];
37 for (int n = 0; n < comparers.length; n++) {
38 this.comparers[n] = suppliedComparers[n].provideContext(context);
39 }
40 }
41
42 /**
43 * Move on to the next tuple. Before returning, this method must set all the variables corresponding
44 * to the "returned" tuple in the local stack frame associated with the context object
45 *
46 * @param context the dynamic evaluation context
47 * @return true if another tuple has been generated; false if the tuple stream is exhausted. If the
48 * method returns false, the values of the local variables corresponding to this tuple stream
49 * are undefined.
50 */
51 @Override
52 public boolean nextTuple(XPathContext context) throws XPathException {
53 if (currentPosition < 0) {
54 currentPosition = 0;
55 int position = 0;
56
57 while (base.nextTuple(context)) {
58 Tuple tuple = tupleExpr.evaluateItem(context);
59 SortKeyDefinitionList sortKeyDefinitions = orderByClause.getSortKeyDefinitions();
60 ItemToBeSorted itbs = new ItemToBeSorted(sortKeyDefinitions.size());
61 itbs.value = tuple;
62 for (int i = 0; i < sortKeyDefinitions.size(); i++) {
63 itbs.sortKeyValues[i] = orderByClause.evaluateSortKey(i, context);
64 }
65 itbs.originalPosition = ++position;
66 tupleArray.add(itbs);
67 }
68
69 try {
70 GenericSorter.quickSort(0, position, this);
71 } catch (ClassCastException e) {
72 XPathException err = new XPathException("Non-comparable types found while sorting: " + e.getMessage());
73 err.setErrorCode("XPTY0004");
74 throw err;
75 }
76 }
77
78 if (currentPosition < tupleArray.size()) {
79 tupleExpr.setCurrentTuple(context, (Tuple) tupleArray.get(currentPosition++).value);
80 return true;
81 } else {
82 return false;
83 }
84
85 }
86
87 /**
88 * Compare two objects within this Sortable, identified by their position.
89 *
90 * @return &lt;0 if obj[a]&lt;obj[b], 0 if obj[a]=obj[b], &gt;0 if obj[a]&gt;obj[b]
91 */
92 public int compare(int a, int b) {
93 try {
94 for (int i = 0; i < comparers.length; i++) {
95 int comp = comparers[i].compareAtomicValues(
96 tupleArray.get(a).sortKeyValues[i], tupleArray.get(b).sortKeyValues[i]);
97 if (comp != 0) {
98 // we have found a difference, so we can return
99 return comp;
100 }
101 }
102 } catch (NoDynamicContextException e) {
103 throw new AssertionError("Sorting without dynamic context: " + e.getMessage());
104 }
105
106 // all sort keys equal: return the items in their original order
107
108 return tupleArray.get(a).originalPosition - tupleArray.get(b).originalPosition;
109 }
110
111 /**
112 * Swap two objects within this Sortable, identified by their position.
113 */
114 public void swap(int a, int b) {
115 ItemToBeSorted temp = tupleArray.get(a);
116 tupleArray.set(a, tupleArray.get(b));
117 tupleArray.set(b, temp);
118 }
119
120 /**
121 * Close the tuple stream, indicating that although not all tuples have been read,
122 * no further tuples are required and resources can be released
123 */
124 @Override
125 public void close() {
126 base.close();
127 }
128 }
129