Debian Codegraph @

net/sf/saxon/expr/sort/MergeGroupingIterator.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.sort;
9
10 import com.saxonica.ee.stream.ManualGroupIterator;
11 import net.sf.saxon.expr.LastPositionFinder;
12 import net.sf.saxon.expr.XPathContext;
13 import net.sf.saxon.om.AtomicArray;
14 import net.sf.saxon.om.AtomicSequence;
15 import net.sf.saxon.om.Item;
16 import net.sf.saxon.om.SequenceIterator;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.tree.iter.EmptyIterator;
19 import net.sf.saxon.tree.iter.ListIterator;
20 import net.sf.saxon.tree.iter.LookaheadIterator;
21 import net.sf.saxon.type.Type;
22 import net.sf.saxon.value.AtomicValue;
23 import net.sf.saxon.value.ExternalObject;
24 import net.sf.saxon.value.ObjectValue;
25
26 import java.util.*;
27
28 /**
29 * An iterator that groups the result of merging several xsl:merge input streams, identifying
30 * groups of adjacent items having the same merge key value
31 */
32
33 public class MergeGroupingIterator implements GroupIterator, LookaheadIterator<Item<?>>, LastPositionFinder {
34
35 private SequenceIterator baseItr;
36 private ObjectValue<ItemWithMergeKeys> currenti = null;
37 private ObjectValue<ItemWithMergeKeys> next;
38 private List<Item<?>> currentMembers;
39 private Map<String, List<Item<?>>> currentSourceMembers;
40 private ItemOrderComparer comparer;
41 private int position = 0;
42 List<AtomicValue> compositeMergeKey;
43 private LastPositionFinder lastPositionFinder;
44
45
46 public MergeGroupingIterator(
47 SequenceIterator p1,
48 ItemOrderComparer comp, LastPositionFinder lpf) throws XPathException {
49 this.baseItr = p1;
50 next = (ObjectValue<ItemWithMergeKeys>)p1.next();
51 if (next != null) {
52 compositeMergeKey = ((ItemWithMergeKeys) ((ObjectValue) next).getObject()).sortKeyValues;
53 }
54 this.comparer = comp;
55 this.lastPositionFinder = lpf;
56 }
57
58 //#if EE==true
59 public ManualMergeGroupingIterator getSnapShot(XPathContext context) {
60 return new ManualMergeGroupingIterator();
61 }
62 //#endif
63
64 /**
65 * The advance() method reads ahead a group of items having common merge key values. These items are
66 * placed in the variable currentMembers. The variable next is left at the next item after this group,
67 * or null if there are no more items
68 * @throws XPathException if a failure occurs reading the input, or if merge keys are out of order, or
69 * not comparable
70 */
71 private void advance() throws XPathException {
72 currentMembers = new ArrayList<>(20);
73 currentSourceMembers = new HashMap<>(20);
74 Item currentItem = currenti.getObject().baseItem;
75 String source = currenti.getObject().sourceName;
76 currentMembers.add(currentItem);
77 if (source != null) {
78 List<Item<?>> list = new ArrayList<>();
79 list.add(currentItem);
80 currentSourceMembers.put(source, list);
81 }
82 while (true) {
83 ObjectValue<ItemWithMergeKeys> nextCandidate = (ObjectValue<ItemWithMergeKeys>)baseItr.next();
84 if (nextCandidate == null) {
85 next = null;
86 return;
87 }
88
89 try {
90 int c = comparer.compare(currenti, nextCandidate);
91 if (c == 0) {
92 currentItem = nextCandidate.getObject().baseItem;
93 source = nextCandidate.getObject().sourceName;
94 currentMembers.add(currentItem);
95 if (source != null) {
96 List<Item<?>> list = currentSourceMembers.computeIfAbsent(source, k -> new ArrayList<>());
97 list.add(currentItem);
98 }
99 } else if (c > 0) {
100 List<AtomicValue> keys = nextCandidate.getObject().sortKeyValues;
101 throw new XPathException(
102 "Merge input for source " + source + " is not ordered according to merge key, detected at key value: " +
103 Arrays.toString(keys.toArray()), "XTDE2220");
104 } else {
105 next = nextCandidate;
106 return;
107 }
108 } catch (ClassCastException e) {
109 XPathException err = new XPathException("Merge key values are of non-comparable types ("
110 + Type.displayTypeName(currentItem) + " and " + Type.displayTypeName(nextCandidate.getObject().baseItem) + ')', "XTTE2230");
111 err.setIsTypeError(true);
112 throw err;
113 }
114
115 }
116 }
117
118 public boolean hasNext() {
119 return next != null;
120 }
121
122 public Item next() throws XPathException {
123 if (next == null) {
124 currenti = null;
125 position = -1;
126 return null;
127 }
128 currenti = next;
129 position++;
130 compositeMergeKey = ((ItemWithMergeKeys) ((ExternalObject) next).getObject()).sortKeyValues;
131 advance();
132 return currenti.getObject().baseItem;
133 }
134
135 public void close() {
136 baseItr.close();
137 }
138
139 @Override
140 public int getLength() throws XPathException {
141 return lastPositionFinder.getLength();
142 }
143
144 public int getProperties() {
145 return LOOKAHEAD | LAST_POSITION_FINDER;
146 }
147
148 public AtomicSequence getCurrentGroupingKey() {
149 return new AtomicArray(compositeMergeKey);
150 }
151
152 public SequenceIterator<?> iterateCurrentGroup() {
153 return new ListIterator<>(currentMembers);
154 }
155
156 public SequenceIterator<?> iterateCurrentGroup(String source) {
157 List<Item<?>> sourceMembers = currentSourceMembers.get(source);
158 if (sourceMembers == null) {
159 return EmptyIterator.emptyIterator();
160 } else {
161 return new ListIterator<>(sourceMembers);
162 }
163 }
164
165 //#if EE==true
166 public class ManualMergeGroupingIterator extends ManualGroupIterator {
167
168 List<Item<?>> currentGroup = currentMembers;
169 AtomicSequence currentGroupingKey = getCurrentGroupingKey();
170
171 public ManualMergeGroupingIterator() {
172 super(currenti.getObject().baseItem, position);
173 setLastPositionFinder(() -> currentMembers.size());
174 }
175
176 public SequenceIterator<?> iterateCurrentGroup() {
177 return new ListIterator<>(currentGroup);
178 }
179
180 public AtomicSequence getCurrentGroupingKey() {
181 return currentGroupingKey;
182 }
183
184
185 @Override
186 public int getLength() throws XPathException {
187 return currentMembers.size();
188 }
189
190
191 }
192 //#endif
193
194 }
195
196