Debian Codegraph @

net/sf/saxon/expr/flwor/OrderByClausePush.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 OrderByClausePush extends TuplePush implements Sortable {
22
23 private TuplePush destination;
24 private OrderByClause orderByClause;
25 private TupleExpression tupleExpr;
26 protected AtomicComparer[] comparers;
27 XPathContext context;
28 int position = 0;
29 private ArrayList<ItemToBeSorted> tupleArray = new ArrayList<ItemToBeSorted>(100);
30
31 public OrderByClausePush(TuplePush destination, TupleExpression tupleExpr, OrderByClause orderBy, XPathContext context) {
32 this.destination = destination;
33 this.tupleExpr = tupleExpr;
34 this.orderByClause = orderBy;
35 this.context = context;
36
37 AtomicComparer[] suppliedComparers = orderBy.getAtomicComparers();
38 comparers = new AtomicComparer[suppliedComparers.length];
39 for (int n = 0; n < comparers.length; n++) {
40 this.comparers[n] = suppliedComparers[n].provideContext(context);
41 }
42 }
43
44 /**
45 * Process the next tuple.
46 */
47 @Override
48 public void processTuple(XPathContext context) throws XPathException {
49
50 Tuple tuple = tupleExpr.evaluateItem(context);
51 SortKeyDefinitionList sortKeyDefinitions = orderByClause.getSortKeyDefinitions();
52 ItemToBeSorted itbs = new ItemToBeSorted(sortKeyDefinitions.size());
53 itbs.value = tuple;
54 for (int i = 0; i < sortKeyDefinitions.size(); i++) {
55 itbs.sortKeyValues[i] = orderByClause.evaluateSortKey(i, context);
56 }
57 itbs.originalPosition = ++position;
58 tupleArray.add(itbs);
59
60 }
61
62 /**
63 * Compare two objects within this Sortable, identified by their position.
64 *
65 * @return &lt;0 if obj[a]&lt;obj[b], 0 if obj[a]=obj[b], &gt;0 if obj[a]&gt;obj[b]
66 */
67 public int compare(int a, int b) {
68 try {
69 for (int i = 0; i < comparers.length; i++) {
70 int comp = comparers[i].compareAtomicValues(
71 tupleArray.get(a).sortKeyValues[i], tupleArray.get(b).sortKeyValues[i]);
72 if (comp != 0) {
73 // we have found a difference, so we can return
74 return comp;
75 }
76 }
77 } catch (NoDynamicContextException e) {
78 throw new AssertionError("Sorting without dynamic context: " + e.getMessage());
79 }
80
81 // all sort keys equal: return the items in their original order
82
83 return tupleArray.get(a).originalPosition - tupleArray.get(b).originalPosition;
84 }
85
86 /**
87 * Swap two objects within this Sortable, identified by their position.
88 */
89 public void swap(int a, int b) {
90 ItemToBeSorted temp = tupleArray.get(a);
91 tupleArray.set(a, tupleArray.get(b));
92 tupleArray.set(b, temp);
93 }
94
95 /**
96 * Close the tuple stream, indicating that no more tuples will be delivered
97 */
98 @Override
99 public void close() throws XPathException {
100 try {
101 GenericSorter.quickSort(0, position, this);
102 } catch (ClassCastException e) {
103 XPathException err = new XPathException("Non-comparable types found while sorting: " + e.getMessage());
104 err.setErrorCode("XPTY0004");
105 throw err;
106 }
107
108 for (ItemToBeSorted itbs : tupleArray) {
109 tupleExpr.setCurrentTuple(context, (Tuple) itbs.value);
110 destination.processTuple(context);
111 }
112 destination.close();
113 }
114 }
115