| 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.value; |
| 9 | |
| 10 | import net.sf.saxon.expr.Expression; |
| 11 | import net.sf.saxon.expr.XPathContext; |
| 12 | import net.sf.saxon.om.*; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | |
| 15 | /** |
| 16 | * A MemoClosure represents a value that has not yet been evaluated: the value is represented |
| 17 | * by an expression, together with saved values of all the context variables that the |
| 18 | * expression depends on. |
| 19 | * <p>The MemoClosure is designed for use when the value is only read several times. The |
| 20 | * value is saved on the first evaluation and remembered for later use.</p> |
| 21 | * <p>The MemoClosure maintains a reservoir containing those items in the value that have |
| 22 | * already been read. When a new iterator is requested to read the value, this iterator |
| 23 | * first examines and returns any items already placed in the reservoir by previous |
| 24 | * users of the MemoClosure. When the reservoir is exhausted, it then uses an underlying |
| 25 | * Input Iterator to read further values of the underlying expression. If the value is |
| 26 | * not read to completion (for example, if the first user did exists($expr), then the |
| 27 | * Input Iterator is left positioned where this user abandoned it. The next user will read |
| 28 | * any values left in the reservoir by the first user, and then pick up iterating the |
| 29 | * base expression where the first user left off. Eventually, all the values of the |
| 30 | * expression will find their way into the reservoir, and future users simply iterate |
| 31 | * over the reservoir contents. Alternatively, of course, the values may be left unread.</p> |
| 32 | * <p>Delayed evaluation is used only for expressions with a static type that allows |
| 33 | * more than one item, so the evaluateItem() method will not normally be used, but it is |
| 34 | * supported for completeness.</p> |
| 35 | * <p>The expression may depend on local variables and on the context item; these values |
| 36 | * are held in the saved XPathContext object that is kept as part of the Closure, and they |
| 37 | * will always be read from that object. The expression may also depend on global variables; |
| 38 | * these are unchanging, so they can be read from the Bindery in the normal way. Expressions |
| 39 | * that depend on other contextual information, for example the values of position(), last(), |
| 40 | * current(), current-group(), should not be evaluated using this mechanism: they should |
| 41 | * always be evaluated eagerly. This means that the Closure does not need to keep a copy |
| 42 | * of these context variables.</p> |
| 43 | * <p>In Saxon-EE, a for-each loop can be multithreaded. If a variable declared outside |
| 44 | * the loop is evaluated as a MemoClosure, then a reference to the variable within the |
| 45 | * loop can result in concurrent attempts to evaluate the variable incrementally. This |
| 46 | * is prevented by synchronizing the evaluation methods.</p> |
| 47 | */ |
| 48 | |
| 49 | public class MemoClosure extends Closure { |
| 50 | |
| 51 | private Sequence<?> sequence; |
| 52 | |
| 53 | /** |
| 54 | * Constructor should not be called directly, instances should be made using the make() method. |
| 55 | */ |
| 56 | |
| 57 | //private static int closureCount = 0; |
| 58 | public MemoClosure() { |
| 59 | //System.err.println("************** Creating MemoClosure " + closureCount); |
| 60 | //closureCount++; if ((closureCount % 1000) == 0) System.err.println("MemoClosures: " + closureCount); |
| 61 | } |
| 62 | //private static int closureCount = 0; |
| 63 | |
| 64 | public MemoClosure(Expression expr, XPathContext context) throws XPathException { |
| 65 | setExpression(expr); |
| 66 | setSavedXPathContext(context.newContext()); |
| 67 | saveContext(expr, context); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Evaluate the expression in a given context to return an iterator over a sequence |
| 72 | */ |
| 73 | |
| 74 | /*@NotNull*/ |
| 75 | public synchronized SequenceIterator<Item<?>> iterate() throws XPathException { |
| 76 | makeSequence(); |
| 77 | return (SequenceIterator<Item<?>>)sequence.iterate(); |
| 78 | } |
| 79 | |
| 80 | private void makeSequence() throws XPathException { |
| 81 | if (sequence == null) { |
| 82 | inputIterator = expression.iterate(savedXPathContext); |
| 83 | sequence = SequenceTool.toMemoSequence(inputIterator); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get the n'th item in the sequence, zero-based |
| 89 | * |
| 90 | * @param n the index of the required item, starting from zero |
| 91 | * @return the item at the relevant position |
| 92 | * @throws XPathException if an error occurs reading the base iterator |
| 93 | */ |
| 94 | |
| 95 | public synchronized Item itemAt(int n) throws XPathException { |
| 96 | makeSequence(); |
| 97 | if (sequence instanceof GroundedValue) { |
| 98 | return ((GroundedValue)sequence).itemAt(n); |
| 99 | } else if (sequence instanceof MemoSequence) { |
| 100 | return ((MemoSequence) sequence).itemAt(n); |
| 101 | } else { |
| 102 | throw new IllegalStateException(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Return a value containing all the items in the sequence returned by this |
| 108 | * SequenceIterator |
| 109 | * |
| 110 | * @return the corresponding value |
| 111 | * @throws net.sf.saxon.trans.XPathException |
| 112 | * if a failure occurs reading the input |
| 113 | */ |
| 114 | |
| 115 | /*@Nullable*/ |
| 116 | public GroundedValue<?> reduce() throws XPathException { |
| 117 | if (sequence instanceof GroundedValue) { |
| 118 | return (GroundedValue<?>)sequence; |
| 119 | } else { |
| 120 | return iterate().materialize(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public Sequence<Item<?>> makeRepeatable() { |
| 126 | return this; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | } |
| 131 |