| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.sort; |
| 9 |
|
| 10 |
import com.saxonica.ee.stream.ManualGroupIterator; |
| 11 |
import net.sf.saxon.expr.Expression; |
| 12 |
import net.sf.saxon.expr.LastPositionFinder; |
| 13 |
import net.sf.saxon.expr.XPathContext; |
| 14 |
import net.sf.saxon.functions.Count; |
| 15 |
import net.sf.saxon.lib.StringCollator; |
| 16 |
import net.sf.saxon.om.*; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.tree.iter.ListIterator; |
| 19 |
import net.sf.saxon.tree.iter.LookaheadIterator; |
| 20 |
import net.sf.saxon.value.AtomicValue; |
| 21 |
|
| 22 |
import java.util.ArrayList; |
| 23 |
import java.util.List; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class GroupAdjacentIterator implements GroupIterator, LastPositionFinder, LookaheadIterator<Item<?>> { |
| 34 |
|
| 35 |
private Expression select; |
| 36 |
private FocusIterator population; |
| 37 |
private Expression keyExpression; |
| 38 |
private StringCollator collator; |
| 39 |
private XPathContext baseContext; |
| 40 |
private XPathContext runningContext; |
| 41 |
private List<AtomicMatchKey> currentComparisonKey; |
| 42 |
private AtomicSequence currentKey; |
| 43 |
private List<Item<?>> currentMembers; |
| 44 |
private List<AtomicMatchKey> nextComparisonKey; |
| 45 |
private List<AtomicValue> nextKey = null; |
| 46 |
private Item next; |
| 47 |
private Item current = null; |
| 48 |
private int position = 0; |
| 49 |
private boolean composite = false; |
| 50 |
|
| 51 |
public GroupAdjacentIterator(Expression select, Expression keyExpression, |
| 52 |
XPathContext baseContext, StringCollator collator, boolean composite) |
| 53 |
throws XPathException { |
| 54 |
this.select = select; |
| 55 |
this.keyExpression = keyExpression; |
| 56 |
this.baseContext = baseContext; |
| 57 |
this.runningContext = baseContext.newMinorContext(); |
| 58 |
this.population = runningContext.trackFocus(select.iterate(baseContext)); |
| 59 |
this.collator = collator; |
| 60 |
this.composite = composite; |
| 61 |
next = population.next(); |
| 62 |
if (next != null) { |
| 63 |
nextKey = getKey(runningContext); |
| 64 |
nextComparisonKey = getComparisonKey(nextKey, baseContext); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public int getLength() throws XPathException { |
| 70 |
GroupAdjacentIterator another = new GroupAdjacentIterator(select, keyExpression, baseContext, collator, composite); |
| 71 |
return Count.steppingCount(another); |
| 72 |
} |
| 73 |
|
| 74 |
private List<AtomicValue> getKey(XPathContext context) throws XPathException { |
| 75 |
List<AtomicValue> key = new ArrayList<>(); |
| 76 |
SequenceIterator iter = keyExpression.iterate(context); |
| 77 |
while (true) { |
| 78 |
AtomicValue val = (AtomicValue) iter.next(); |
| 79 |
if (val == null) { |
| 80 |
break; |
| 81 |
} |
| 82 |
key.add(val); |
| 83 |
} |
| 84 |
return key; |
| 85 |
} |
| 86 |
|
| 87 |
private List<AtomicMatchKey> getComparisonKey(List<AtomicValue> key, XPathContext keyContext) throws XPathException { |
| 88 |
List<AtomicMatchKey> ckey = new ArrayList<>(key.size()); |
| 89 |
for (AtomicValue aKey : key) { |
| 90 |
AtomicMatchKey comparisonKey; |
| 91 |
if (aKey.isNaN()) { |
| 92 |
comparisonKey = AtomicMatchKey.NaN_MATCH_KEY; |
| 93 |
} else { |
| 94 |
comparisonKey = aKey.getXPathComparable(false, collator, keyContext.getImplicitTimezone()); |
| 95 |
} |
| 96 |
ckey.add(comparisonKey); |
| 97 |
} |
| 98 |
return ckey; |
| 99 |
} |
| 100 |
|
| 101 |
private void advance() throws XPathException { |
| 102 |
currentMembers = new ArrayList<>(20); |
| 103 |
currentMembers.add(current); |
| 104 |
while (true) { |
| 105 |
Item nextCandidate = population.next(); |
| 106 |
if (nextCandidate == null) { |
| 107 |
break; |
| 108 |
} |
| 109 |
List<AtomicValue> newKey = getKey(runningContext); |
| 110 |
List<AtomicMatchKey> newComparisonKey = getComparisonKey(newKey, baseContext); |
| 111 |
|
| 112 |
try { |
| 113 |
if (currentComparisonKey.equals(newComparisonKey)) { |
| 114 |
currentMembers.add(nextCandidate); |
| 115 |
} else { |
| 116 |
next = nextCandidate; |
| 117 |
nextComparisonKey = newComparisonKey; |
| 118 |
nextKey = newKey; |
| 119 |
return; |
| 120 |
} |
| 121 |
} catch (ClassCastException e) { |
| 122 |
String message = "Grouping key values are of non-comparable types"; |
| 123 |
XPathException err = new XPathException(message); |
| 124 |
err.setIsTypeError(true); |
| 125 |
err.setXPathContext(runningContext); |
| 126 |
throw err; |
| 127 |
} |
| 128 |
} |
| 129 |
next = null; |
| 130 |
nextKey = null; |
| 131 |
} |
| 132 |
|
| 133 |
public AtomicSequence getCurrentGroupingKey() { |
| 134 |
return currentKey; |
| 135 |
} |
| 136 |
|
| 137 |
public SequenceIterator<?> iterateCurrentGroup() { |
| 138 |
return new ListIterator<>(currentMembers); |
| 139 |
} |
| 140 |
|
| 141 |
public boolean hasNext() { |
| 142 |
return next != null; |
| 143 |
} |
| 144 |
|
| 145 |
public Item next() throws XPathException { |
| 146 |
if (next == null) { |
| 147 |
current = null; |
| 148 |
position = -1; |
| 149 |
return null; |
| 150 |
} |
| 151 |
current = next; |
| 152 |
if (nextKey.size() == 1) { |
| 153 |
currentKey = nextKey.get(0); |
| 154 |
} else { |
| 155 |
currentKey = new AtomicArray(nextKey); |
| 156 |
} |
| 157 |
currentComparisonKey = nextComparisonKey; |
| 158 |
position++; |
| 159 |
advance(); |
| 160 |
return current; |
| 161 |
} |
| 162 |
|
| 163 |
public void close() { |
| 164 |
population.close(); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public int getProperties() { |
| 178 |
return LOOKAHEAD | LAST_POSITION_FINDER; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public ManualGroupIterator getSnapShot(XPathContext context) { |
| 185 |
return new ManualGroupAdjacentIterator(); |
| 186 |
} |
| 187 |
|
| 188 |
private class ManualGroupAdjacentIterator extends ManualGroupIterator { |
| 189 |
|
| 190 |
List<Item<?>> cMembers = currentMembers; |
| 191 |
XPathContext savedcontext = runningContext.newMinorContext(); |
| 192 |
|
| 193 |
ManualGroupAdjacentIterator() { |
| 194 |
super(current, position); |
| 195 |
setCurrentGroupingKey(currentKey); |
| 196 |
setLastPositionFinder(() -> savedcontext.getLast()); |
| 197 |
} |
| 198 |
|
| 199 |
public SequenceIterator<Item<?>> iterateCurrentGroup() { |
| 200 |
return new ListIterator<>(cMembers); |
| 201 |
} |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
} |
| 207 |
|