| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.trans; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.XPathContext; |
| 12 |
import net.sf.saxon.expr.XPathContextMajor; |
| 13 |
import net.sf.saxon.expr.instruct.SlotManager; |
| 14 |
import net.sf.saxon.expr.sort.AtomicMatchKey; |
| 15 |
import net.sf.saxon.expr.sort.CodepointMatchKey; |
| 16 |
import net.sf.saxon.expr.sort.LocalOrderComparer; |
| 17 |
import net.sf.saxon.lib.ConversionRules; |
| 18 |
import net.sf.saxon.lib.StringCollator; |
| 19 |
import net.sf.saxon.om.NodeInfo; |
| 20 |
import net.sf.saxon.om.SequenceIterator; |
| 21 |
import net.sf.saxon.om.StandardNames; |
| 22 |
import net.sf.saxon.om.TreeInfo; |
| 23 |
import net.sf.saxon.pattern.Pattern; |
| 24 |
import net.sf.saxon.tree.iter.ManualIterator; |
| 25 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 26 |
import net.sf.saxon.type.StringConverter; |
| 27 |
import net.sf.saxon.type.UType; |
| 28 |
import net.sf.saxon.value.AtomicValue; |
| 29 |
import net.sf.saxon.value.UntypedAtomicValue; |
| 30 |
|
| 31 |
import java.util.*; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class KeyIndex { |
| 51 |
|
| 52 |
public enum Status {UNDER_CONSTRUCTION, BUILT, FAILED} |
| 53 |
|
| 54 |
private Map <AtomicMatchKey, List<NodeInfo>> index; |
| 55 |
private UType keyTypesPresent = UType.VOID; |
| 56 |
private UType keyTypesConvertedFromUntyped = UType.STRING_LIKE; |
| 57 |
private List <UntypedAtomicValue> untypedKeys; |
| 58 |
private ConversionRules rules; |
| 59 |
private int implicitTimezone; |
| 60 |
private StringCollator collation; |
| 61 |
private long creatingThread; |
| 62 |
private Status status; |
| 63 |
|
| 64 |
public KeyIndex(boolean isRangeKey) { |
| 65 |
index = isRangeKey ? new TreeMap<>() : new HashMap<>(100); |
| 66 |
creatingThread = Thread.currentThread().getId(); |
| 67 |
status = Status.UNDER_CONSTRUCTION; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public Map <AtomicMatchKey, List<NodeInfo>> getUnderlyingMap() { |
| 76 |
return index; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public boolean isCreatedInThisThread() { |
| 85 |
return creatingThread == Thread.currentThread().getId(); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public Status getStatus() { |
| 94 |
return status; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public void setStatus(Status status) { |
| 103 |
this.status = status; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public void buildIndex(KeyDefinitionSet keySet, |
| 120 |
TreeInfo doc, |
| 121 |
XPathContext context) throws XPathException { |
| 122 |
|
| 123 |
List<KeyDefinition> definitions = keySet.getKeyDefinitions(); |
| 124 |
|
| 125 |
|
| 126 |
for (int k = 0; k < definitions.size(); k++) { |
| 127 |
constructIndex(doc, definitions.get(k), context, k == 0); |
| 128 |
} |
| 129 |
this.rules = context.getConfiguration().getConversionRules(); |
| 130 |
this.implicitTimezone = context.getImplicitTimezone(); |
| 131 |
this.collation = definitions.get(0).getCollation(); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
private void constructIndex(TreeInfo doc, |
| 146 |
KeyDefinition keydef, |
| 147 |
XPathContext context, |
| 148 |
boolean isFirst) throws XPathException { |
| 149 |
|
| 150 |
Pattern match = keydef.getMatch(); |
| 151 |
|
| 152 |
|
| 153 |
XPathContextMajor xc = context.newContext(); |
| 154 |
xc.setOrigin(keydef); |
| 155 |
xc.setCurrentComponent(keydef.getDeclaringComponent()); |
| 156 |
xc.setTemporaryOutputState(StandardNames.XSL_KEY); |
| 157 |
|
| 158 |
|
| 159 |
SlotManager map = keydef.getStackFrameMap(); |
| 160 |
if (map != null) { |
| 161 |
xc.openStackFrame(map); |
| 162 |
} |
| 163 |
|
| 164 |
match.selectNodes(doc, xc).forEachOrFail(node -> processNode(node, keydef, xc, isFirst)); |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
private void processNode(NodeInfo node, |
| 181 |
KeyDefinition keydef, |
| 182 |
XPathContext xc, |
| 183 |
boolean isFirst) throws XPathException { |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
ManualIterator<NodeInfo> si = new ManualIterator<>(node); |
| 190 |
xc.setCurrentIterator(si); |
| 191 |
|
| 192 |
StringCollator collation = keydef.getCollation(); |
| 193 |
int implicitTimezone = xc.getImplicitTimezone(); |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
Expression use = keydef.getUse(); |
| 198 |
SequenceIterator<?> useval = use.iterate(xc); |
| 199 |
if (keydef.isComposite()) { |
| 200 |
List<AtomicMatchKey> amks = new ArrayList<>(4); |
| 201 |
useval.forEachOrFail( |
| 202 |
keyVal -> amks.add(getCollationKey((AtomicValue)keyVal, collation, implicitTimezone)) |
| 203 |
); |
| 204 |
addEntry(new CompositeAtomicMatchKey(amks), node, isFirst); |
| 205 |
} else { |
| 206 |
AtomicValue keyVal; |
| 207 |
while ((keyVal = (AtomicValue) useval.next()) != null) { |
| 208 |
if (keyVal.isNaN()) { |
| 209 |
continue; |
| 210 |
} |
| 211 |
UType actualUType = keyVal.getUType(); |
| 212 |
if (!keyTypesPresent.subsumes(actualUType)) { |
| 213 |
keyTypesPresent = keyTypesPresent.union(actualUType); |
| 214 |
} |
| 215 |
AtomicMatchKey amk = getCollationKey(keyVal, collation, implicitTimezone); |
| 216 |
if (actualUType.equals(UType.UNTYPED_ATOMIC) && keydef.isConvertUntypedToOther()) { |
| 217 |
if (untypedKeys == null) { |
| 218 |
untypedKeys = new ArrayList<>(20); |
| 219 |
} |
| 220 |
untypedKeys.add((UntypedAtomicValue)keyVal); |
| 221 |
} |
| 222 |
addEntry(amk, node, isFirst); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
private void addEntry(AtomicMatchKey val, NodeInfo curr, boolean isFirst) { |
| 229 |
List<NodeInfo> nodes = index.get(val); |
| 230 |
if (nodes == null) { |
| 231 |
|
| 232 |
nodes = new ArrayList<>(4); |
| 233 |
index.put(val, nodes); |
| 234 |
nodes.add(curr); |
| 235 |
} else { |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
if (isFirst) { |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
if (nodes.get(nodes.size() - 1) != curr) { |
| 244 |
nodes.add(curr); |
| 245 |
} |
| 246 |
} else { |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
LocalOrderComparer comparer = LocalOrderComparer.getInstance(); |
| 254 |
boolean found = false; |
| 255 |
for (int i=nodes.size()-1; i>=0; i--) { |
| 256 |
int d = comparer.compare(curr, nodes.get(i)); |
| 257 |
if (d>=0) { |
| 258 |
if (d==0) { |
| 259 |
|
| 260 |
} else { |
| 261 |
|
| 262 |
nodes.add(i+1, curr); |
| 263 |
} |
| 264 |
found = true; |
| 265 |
break; |
| 266 |
} |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
if (!found) { |
| 271 |
nodes.add(0, curr); |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
public void reindexUntypedValues(BuiltInAtomicType type) throws XPathException { |
| 289 |
UType uType = type.getUType(); |
| 290 |
if (UType.STRING_LIKE.subsumes(uType)) { |
| 291 |
return; |
| 292 |
} |
| 293 |
if (UType.NUMERIC.subsumes(uType)) { |
| 294 |
type = BuiltInAtomicType.DOUBLE; |
| 295 |
uType = UType.DOUBLE; |
| 296 |
} |
| 297 |
StringConverter converter = type.getStringConverter(rules); |
| 298 |
for (UntypedAtomicValue v : untypedKeys) { |
| 299 |
AtomicMatchKey uk = getCollationKey(v, collation, implicitTimezone); |
| 300 |
List<NodeInfo> nodes = index.get(uk); |
| 301 |
AtomicValue convertedValue = converter.convertString(v.getStringValueCS()).asAtomic(); |
| 302 |
AtomicMatchKey amk = getCollationKey(convertedValue, collation, implicitTimezone); |
| 303 |
for (NodeInfo node : nodes) { |
| 304 |
addEntry(amk, node, false); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public boolean isEmpty() { |
| 316 |
return index.isEmpty(); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public List<NodeInfo> get(AtomicValue soughtValue) throws XPathException { |
| 329 |
if (untypedKeys != null && !keyTypesConvertedFromUntyped.subsumes(soughtValue.getUType())) { |
| 330 |
reindexUntypedValues(soughtValue.getPrimitiveType()); |
| 331 |
} |
| 332 |
return index.get(getCollationKey(soughtValue, collation, implicitTimezone)); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
public List<NodeInfo> getComposite(SequenceIterator<?> soughtValue) throws XPathException { |
| 345 |
List<AtomicMatchKey> amks = new ArrayList<>(4); |
| 346 |
soughtValue.forEachOrFail( |
| 347 |
keyVal -> amks.add(getCollationKey((AtomicValue)keyVal, collation, implicitTimezone))); |
| 348 |
return index.get(new CompositeAtomicMatchKey(amks)); |
| 349 |
} |
| 350 |
|
| 351 |
private static AtomicMatchKey getCollationKey(AtomicValue value, StringCollator collation, int implicitTimezone) |
| 352 |
throws XPathException { |
| 353 |
if (UType.STRING_LIKE.subsumes(value.getUType())) { |
| 354 |
if (collation == null) { |
| 355 |
return new CodepointMatchKey(value.getStringValue()); |
| 356 |
} else { |
| 357 |
return collation.getCollationKey(value.getStringValue()); |
| 358 |
} |
| 359 |
} else { |
| 360 |
return value.getXPathComparable(false, collation, implicitTimezone); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
private class CompositeAtomicMatchKey implements AtomicMatchKey { |
| 365 |
|
| 366 |
private List<AtomicMatchKey> keys; |
| 367 |
|
| 368 |
public CompositeAtomicMatchKey(List<AtomicMatchKey> keys) { |
| 369 |
this.keys = keys; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public AtomicValue asAtomic() { |
| 378 |
throw new UnsupportedOperationException(); |
| 379 |
} |
| 380 |
|
| 381 |
@Override |
| 382 |
public boolean equals(Object obj) { |
| 383 |
if (obj instanceof CompositeAtomicMatchKey && |
| 384 |
((CompositeAtomicMatchKey)obj).keys.size() == keys.size()) { |
| 385 |
List<AtomicMatchKey> keys2 = ((CompositeAtomicMatchKey)obj).keys; |
| 386 |
for (int i=0; i<keys.size(); i++) { |
| 387 |
if (!keys.get(i).equals(keys2.get(i))) { |
| 388 |
return false; |
| 389 |
} |
| 390 |
} |
| 391 |
return true; |
| 392 |
} |
| 393 |
return false; |
| 394 |
} |
| 395 |
|
| 396 |
@Override |
| 397 |
public int hashCode() { |
| 398 |
int h = 0x8ab27cd6; |
| 399 |
for (AtomicMatchKey amk : keys) { |
| 400 |
h ^= amk.hashCode(); |
| 401 |
h = h << 1; |
| 402 |
} |
| 403 |
return h; |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
|