| 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.lib.StringCollator; |
| 15 |
import net.sf.saxon.om.*; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.iter.ListIterator; |
| 18 |
import net.sf.saxon.tree.iter.LookaheadIterator; |
| 19 |
import net.sf.saxon.value.AtomicValue; |
| 20 |
|
| 21 |
import java.util.ArrayList; |
| 22 |
import java.util.HashMap; |
| 23 |
import java.util.List; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class GroupByIterator implements GroupIterator, LastPositionFinder, LookaheadIterator<Item<?>> { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
private SequenceIterator<?> population; |
| 49 |
protected Expression keyExpression; |
| 50 |
private StringCollator collator; |
| 51 |
private XPathContext keyContext; |
| 52 |
private int position = 0; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
protected List<List<Item<?>>> groups = new ArrayList<>(40); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
protected List<AtomicSequence> groupKeys = new ArrayList<>(40); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
protected boolean composite; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public GroupByIterator(SequenceIterator<?> population, Expression keyExpression, |
| 80 |
XPathContext keyContext, StringCollator collator, boolean composite) |
| 81 |
throws XPathException { |
| 82 |
this.population = population; |
| 83 |
this.keyExpression = keyExpression; |
| 84 |
this.keyContext = keyContext; |
| 85 |
this.collator = collator; |
| 86 |
this.composite = composite; |
| 87 |
if (composite) { |
| 88 |
buildIndexedGroupsComposite(); |
| 89 |
} else { |
| 90 |
buildIndexedGroups(); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
public GroupByIterator() { |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private void buildIndexedGroups() throws XPathException { |
| 106 |
HashMap<AtomicMatchKey, List<Item<?>>> index = new HashMap<>(40); |
| 107 |
XPathContext c2 = keyContext.newMinorContext(); |
| 108 |
FocusIterator focus = c2.trackFocus(population); |
| 109 |
int implicitTimezone = c2.getImplicitTimezone(); |
| 110 |
Item item; |
| 111 |
while ((item = focus.next()) != null) { |
| 112 |
SequenceIterator keys = keyExpression.iterate(c2); |
| 113 |
boolean firstKey = true; |
| 114 |
while (true) { |
| 115 |
AtomicValue key = (AtomicValue) keys.next(); |
| 116 |
if (key == null) { |
| 117 |
break; |
| 118 |
} |
| 119 |
AtomicMatchKey comparisonKey; |
| 120 |
if (key.isNaN()) { |
| 121 |
comparisonKey = AtomicMatchKey.NaN_MATCH_KEY; |
| 122 |
} else { |
| 123 |
comparisonKey = key.getXPathComparable(false, collator, implicitTimezone); |
| 124 |
} |
| 125 |
List<Item<?>> g = index.get(comparisonKey); |
| 126 |
if (g == null) { |
| 127 |
List<Item<?>> newGroup = new ArrayList<>(20); |
| 128 |
newGroup.add(item); |
| 129 |
groups.add(newGroup); |
| 130 |
groupKeys.add(key); |
| 131 |
index.put(comparisonKey, newGroup); |
| 132 |
} else { |
| 133 |
if (firstKey) { |
| 134 |
g.add(item); |
| 135 |
} else { |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
if (g.get(g.size() - 1) != item) { |
| 141 |
g.add(item); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
firstKey = false; |
| 146 |
} |
| 147 |
|
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
private void buildIndexedGroupsComposite() throws XPathException { |
| 159 |
HashMap<List<AtomicMatchKey>, List<Item<?>>> index = new HashMap<>(40); |
| 160 |
XPathContext c2 = keyContext.newMinorContext(); |
| 161 |
FocusIterator focus = c2.trackFocus(population); |
| 162 |
int implicitTimezone = c2.getImplicitTimezone(); |
| 163 |
Item item; |
| 164 |
while ((item = focus.next()) != null) { |
| 165 |
SequenceIterator keys = keyExpression.iterate(c2); |
| 166 |
List<AtomicMatchKey> ckList = new ArrayList<>(); |
| 167 |
List<AtomicValue> compositeKey = new ArrayList<>(); |
| 168 |
while (true) { |
| 169 |
AtomicValue key = (AtomicValue) keys.next(); |
| 170 |
if (key == null) { |
| 171 |
break; |
| 172 |
} |
| 173 |
compositeKey.add(key); |
| 174 |
AtomicMatchKey comparisonKey; |
| 175 |
if (key.isNaN()) { |
| 176 |
comparisonKey = AtomicMatchKey.NaN_MATCH_KEY; |
| 177 |
} else { |
| 178 |
comparisonKey = key.getXPathComparable(false, collator, implicitTimezone); |
| 179 |
} |
| 180 |
ckList.add(comparisonKey); |
| 181 |
} |
| 182 |
|
| 183 |
List<Item<?>> g = index.get(ckList); |
| 184 |
if (g == null) { |
| 185 |
List<Item<?>> newGroup = new ArrayList<>(20); |
| 186 |
newGroup.add(item); |
| 187 |
groups.add(newGroup); |
| 188 |
groupKeys.add(new AtomicArray(compositeKey)); |
| 189 |
index.put(ckList, newGroup); |
| 190 |
} else { |
| 191 |
g.add(item); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public synchronized AtomicSequence getCurrentGroupingKey() { |
| 204 |
AtomicSequence val = groupKeys.get(position - 1); |
| 205 |
if (val == null) { |
| 206 |
return EmptyAtomicSequence.getInstance(); |
| 207 |
} else { |
| 208 |
return val; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public SequenceIterator<?> iterateCurrentGroup() { |
| 219 |
return new ListIterator<>(groups.get(position - 1)); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public List getCurrentGroup() { |
| 229 |
return groups.get(position - 1); |
| 230 |
} |
| 231 |
|
| 232 |
public boolean hasNext() { |
| 233 |
return position < groups.size(); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
public Item next() throws XPathException { |
| 238 |
if (position >= 0 && position < groups.size()) { |
| 239 |
position++; |
| 240 |
return current(); |
| 241 |
} else { |
| 242 |
position = -1; |
| 243 |
return null; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
private Item current() { |
| 248 |
if (position < 1) { |
| 249 |
return null; |
| 250 |
} |
| 251 |
|
| 252 |
return groups.get(position - 1).get(0); |
| 253 |
} |
| 254 |
|
| 255 |
public void close() { |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public int getProperties() { |
| 269 |
return LAST_POSITION_FINDER | LOOKAHEAD; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public int getLength() throws XPathException { |
| 277 |
return groups.size(); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public ManualGroupIterator getSnapShot(XPathContext context) { |
| 283 |
return new ManualGroupByIterator(); |
| 284 |
} |
| 285 |
|
| 286 |
public class ManualGroupByIterator extends ManualGroupIterator { |
| 287 |
|
| 288 |
List<Item<?>> currentGroup = groups.get(position - 1); |
| 289 |
AtomicSequence currentGroupingKey = groupKeys.get(position - 1); |
| 290 |
|
| 291 |
public ManualGroupByIterator() { |
| 292 |
super(groups.get(position - 1).get(0), position); |
| 293 |
setLastPositionFinder(() -> groups.size()); |
| 294 |
} |
| 295 |
|
| 296 |
public SequenceIterator<Item<?>> iterateCurrentGroup() { |
| 297 |
return new ListIterator<>(currentGroup); |
| 298 |
} |
| 299 |
|
| 300 |
public AtomicSequence getCurrentGroupingKey() { |
| 301 |
return currentGroupingKey; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
@Override |
| 306 |
public int getLength() throws XPathException { |
| 307 |
return groups.size(); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
} |
| 316 |
|