Do not instantiate transient replicas
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / InferredStatementContext.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
9
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.VerifyException;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.Streams;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Objects;
23 import java.util.Optional;
24 import java.util.stream.Collectors;
25 import java.util.stream.Stream;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.opendaylight.yangtools.concepts.Immutable;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.QNameModule;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
32 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
33 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
35 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
36 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.OnDemandSchemaTreeStorageNode;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementFactory;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
45 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * A statement which has been inferred to exist. Functionally it is equivalent to a SubstatementContext, but it is not
51  * backed by a declaration (and declared statements). It is backed by a prototype StatementContextBase and has only
52  * effective substatements, which are either transformed from that prototype or added by inference.
53  */
54 final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
55         extends StatementContextBase<A, D, E> implements OnDemandSchemaTreeStorageNode {
56     // An effective copy view, with enough information to decide what to do next
57     private static final class EffectiveCopy implements Immutable {
58         // Original statement
59         private final ReactorStmtCtx<?, ?, ?> orig;
60         // Effective view, if the statement is to be reused it equals to orig
61         private final ReactorStmtCtx<?, ?, ?> copy;
62
63         EffectiveCopy(final ReactorStmtCtx<?, ?, ?> orig, final ReactorStmtCtx<?, ?, ?> copy) {
64             this.orig = requireNonNull(orig);
65             this.copy = requireNonNull(copy);
66         }
67
68         boolean isReused() {
69             return orig == copy;
70         }
71
72         ReactorStmtCtx<?, ?, ?> toChildContext(final @NonNull InferredStatementContext<?, ?, ?> parent) {
73             return isReused() ? orig.replicaAsChildOf(parent) : copy;
74         }
75
76         ReactorStmtCtx<?, ?, ?> toReusedChild(final @NonNull InferredStatementContext<?, ?, ?> parent) {
77             verify(isReused(), "Attempted to discard copy %s", copy);
78             return orig.replicaAsChildOf(parent);
79         }
80     }
81
82     private static final Logger LOG = LoggerFactory.getLogger(InferredStatementContext.class);
83
84     // Sentinel objects for 'substatements', String is a good enough type
85     private static final @NonNull String REUSED_SUBSTATEMENTS = "reused";
86     private static final @NonNull String SWEPT_SUBSTATEMENTS = "swept";
87
88     private final @NonNull StatementContextBase<A, D, E> prototype;
89     private final @NonNull StatementContextBase<?, ?, ?> parent;
90     private final @NonNull StmtContext<A, D, E> originalCtx;
91     private final @NonNull CopyType childCopyType;
92     private final QNameModule targetModule;
93     private final A argument;
94
95     /**
96      * Effective substatements, lazily materialized. This field can have four states:
97      * <ul>
98      *   <li>it can be {@code null}, in which case no materialization has taken place</li>
99      *   <li>it can be a {@link HashMap}, in which case partial materialization has taken place</li>
100      *   <li>it can be a {@link List}, in which case full materialization has taken place</li>
101      *   <li>it can be {@link SWEPT_SUBSTATEMENTS}, in which case materialized state is no longer available</li>
102      * </ul>
103      */
104     private Object substatements;
105
106     private InferredStatementContext(final InferredStatementContext<A, D, E> original,
107             final StatementContextBase<?, ?, ?> parent) {
108         super(original);
109         this.parent = requireNonNull(parent);
110         this.childCopyType = original.childCopyType;
111         this.targetModule = original.targetModule;
112         this.prototype = original.prototype;
113         this.originalCtx = original.originalCtx;
114         this.argument = original.argument;
115         // Substatements are initialized here
116         this.substatements = ImmutableList.of();
117     }
118
119     InferredStatementContext(final StatementContextBase<?, ?, ?> parent, final StatementContextBase<A, D, E> prototype,
120             final CopyType myCopyType, final CopyType childCopyType, final QNameModule targetModule) {
121         super(prototype.definition(), CopyHistory.of(myCopyType, prototype.history()));
122         this.parent = requireNonNull(parent);
123         this.prototype = requireNonNull(prototype);
124         this.argument = targetModule == null ? prototype.argument()
125                 : prototype.definition().adaptArgumentValue(prototype, targetModule);
126         this.childCopyType = requireNonNull(childCopyType);
127         this.targetModule = targetModule;
128         this.originalCtx = prototype.getOriginalCtx().orElse(prototype);
129
130         // Mark prototype as blocking statement cleanup
131         prototype.incRef();
132     }
133
134     @Override
135     public Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements() {
136         return ImmutableList.of();
137     }
138
139     @Override
140     public Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements() {
141         return mutableEffectiveSubstatements(ensureEffectiveSubstatements());
142     }
143
144     @Override
145     public Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
146         // No need to concat with declared
147         return effectiveSubstatements();
148     }
149
150     @Override
151     public Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
152         // No need to concat with declared
153         return effectiveSubstatements().stream();
154     }
155
156     @Override
157     public StatementSourceReference sourceReference() {
158         return originalCtx.sourceReference();
159     }
160
161     @Override
162     public String rawArgument() {
163         return originalCtx.rawArgument();
164     }
165
166     @Override
167     public Optional<StmtContext<A, D, E>> getOriginalCtx() {
168         return Optional.of(originalCtx);
169     }
170
171     @Override
172     public Optional<StmtContext<A, D, E>> getPreviousCopyCtx() {
173         return Optional.of(prototype);
174     }
175
176     @Override
177     public D declared() {
178         /*
179          * Share original instance of declared statement between all effective statements which have been copied or
180          * derived from this original declared statement.
181          */
182         return originalCtx.declared();
183     }
184
185     @Override
186     public void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef) {
187         substatements = removeStatementFromEffectiveSubstatements(ensureEffectiveSubstatements(), statementDef);
188     }
189
190     @Override
191     public void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef,
192             final String statementArg) {
193         substatements = removeStatementFromEffectiveSubstatements(ensureEffectiveSubstatements(), statementDef,
194             statementArg);
195     }
196
197     @Override
198     public void addEffectiveSubstatement(final Mutable<?, ?, ?> substatement) {
199         substatements = addEffectiveSubstatement(ensureEffectiveSubstatements(), substatement);
200     }
201
202     @Override
203     void addEffectiveSubstatementsImpl(final Collection<? extends Mutable<?, ?, ?>> statements) {
204         substatements = addEffectiveSubstatementsImpl(ensureEffectiveSubstatements(), statements);
205     }
206
207     @Override
208     InferredStatementContext<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
209         return new InferredStatementContext<>(this, newParent);
210     }
211
212     @Override
213     E createEffective(final StatementFactory<A, D, E> factory) {
214         // If we have not materialized we do not have a difference in effective substatements, hence we can forward
215         // towards the source of the statement.
216         accessSubstatements();
217         return substatements == null ? tryToReusePrototype(factory) : super.createEffective(factory);
218     }
219
220     private @NonNull E tryToReusePrototype(final StatementFactory<A, D, E> factory) {
221         final E origEffective = prototype.buildEffective();
222         final Collection<? extends @NonNull EffectiveStatement<?, ?>> origSubstatements =
223             origEffective.effectiveSubstatements();
224
225         // First check if we can reuse the entire prototype
226         if (!factory.canReuseCurrent(this, prototype, origSubstatements)) {
227             return tryToReuseSubstatements(factory, origEffective);
228         }
229
230         // No substatements to deal with, we can freely reuse the original
231         if (origSubstatements.isEmpty()) {
232             LOG.debug("Reusing empty: {}", origEffective);
233             substatements = ImmutableList.of();
234             prototype.decRef();
235             return origEffective;
236         }
237
238         // We can reuse this statement let's see if all the statements agree
239         final List<EffectiveCopy> declCopy = prototype.streamDeclared()
240             .map(sub -> effectiveCopy((ReactorStmtCtx<?, ?, ?>) sub))
241             .filter(Objects::nonNull)
242             .collect(Collectors.toUnmodifiableList());
243         final List<EffectiveCopy> effCopy = prototype.streamEffective()
244             .map(sub -> effectiveCopy((ReactorStmtCtx<?, ?, ?>) sub))
245             .filter(Objects::nonNull)
246             .collect(Collectors.toUnmodifiableList());
247
248         if (allReused(declCopy) && allReused(effCopy)) {
249             LOG.debug("Reusing after substatement check: {}", origEffective);
250             substatements = noRefs() ? REUSED_SUBSTATEMENTS
251                 : reusePrototypeReplicas(Streams.concat(declCopy.stream(), effCopy.stream())
252                     .map(copy -> copy.toReusedChild(this)));
253             prototype.decRef();
254             return origEffective;
255         }
256
257         final List<ReactorStmtCtx<?, ?, ?>> declared = declCopy.stream()
258             .map(copy -> copy.toChildContext(this))
259             .collect(ImmutableList.toImmutableList());
260         final List<ReactorStmtCtx<?, ?, ?>> effective = effCopy.stream()
261             .map(copy -> copy.toChildContext(this))
262             .collect(ImmutableList.toImmutableList());
263         substatements = declared.isEmpty() ? effective
264             : Streams.concat(declared.stream(), effective.stream()).collect(ImmutableList.toImmutableList());
265         prototype.decRef();
266
267         // Values are the effective copies, hence this efficiently deals with recursion.
268         return factory.createEffective(this, declared.stream(), effective.stream());
269     }
270
271     private @NonNull E tryToReuseSubstatements(final StatementFactory<A, D, E> factory, final @NonNull E original) {
272         if (allSubstatementsContextIndependent()) {
273             LOG.debug("Reusing substatements of: {}", prototype);
274             substatements = noRefs() ? REUSED_SUBSTATEMENTS : reusePrototypeReplicas();
275             prototype.decRef();
276             return factory.copyEffective(this, original);
277         }
278
279         // Fall back to full instantiation, which populates our substatements. Then check if we should be reusing
280         // the substatement list, as this operation turned out to not affect them.
281         final E effective = super.createEffective(factory);
282         if (sameSubstatements(original.effectiveSubstatements(), effective)) {
283             LOG.debug("Reusing unchanged substatements of: {}", prototype);
284             return factory.copyEffective(this, original);
285         }
286         return effective;
287     }
288
289     private List<ReactorStmtCtx<?, ?, ?>> reusePrototypeReplicas() {
290         return reusePrototypeReplicas(Streams.concat(
291             prototype.streamDeclared(), prototype.streamEffective()));
292     }
293
294     private List<ReactorStmtCtx<?, ?, ?>> reusePrototypeReplicas(final Stream<StmtContext<?, ?, ?>> stream) {
295         return stream
296             .map(stmt -> {
297                 final ReplicaStatementContext<?, ?, ?> ret = ((ReactorStmtCtx<?, ?, ?>) stmt).replicaAsChildOf(this);
298                 ret.buildEffective();
299                 return ret;
300             })
301             .collect(Collectors.toUnmodifiableList());
302     }
303
304     private static boolean sameSubstatements(final Collection<?> original, final EffectiveStatement<?, ?> effective) {
305         final Collection<?> copied = effective.effectiveSubstatements();
306         if (copied != effective.effectiveSubstatements() || original.size() != copied.size()) {
307             // Do not bother if result is treating substatements as transient
308             return false;
309         }
310
311         final Iterator<?> oit = original.iterator();
312         final Iterator<?> cit = copied.iterator();
313         while (oit.hasNext()) {
314             verify(cit.hasNext());
315             // Identity comparison on purpose to side-step whatever equality there might be. We want to reuse instances
316             // after all.
317             if (oit.next() != cit.next()) {
318                 return false;
319             }
320         }
321         verify(!cit.hasNext());
322         return true;
323     }
324
325     private static boolean allReused(final List<EffectiveCopy> entries) {
326         return entries.stream().allMatch(EffectiveCopy::isReused);
327     }
328
329     @Override
330     boolean hasEmptySubstatements() {
331         if (substatements == null) {
332             return prototype.hasEmptySubstatements();
333         }
334         return substatements instanceof HashMap ? false : ((List<?>) substatements).isEmpty();
335     }
336
337     @Override
338     boolean noSensitiveSubstatements() {
339         accessSubstatements();
340         if (substatements == null) {
341             // No difference, defer to prototype
342             return prototype.allSubstatementsContextIndependent();
343         }
344         if (substatements instanceof List) {
345             // Fully materialized, walk all statements
346             return noSensitiveSubstatements(castEffective(substatements));
347         }
348
349         // Partially-materialized. This case has three distinct outcomes:
350         // - prototype does not have a sensitive statement (1)
351         // - protype has a sensitive substatement, and
352         //   - we have not marked is as unsupported (2)
353         //   - we have marked it as unsupported (3)
354         //
355         // Determining the outcome between (2) and (3) is a bother, this check errs on the side of false negative side
356         // and treats (3) as (2) -- i.e. even if we marked a sensitive statement as unsupported, we still consider it
357         // as affecting the result.
358         return prototype.allSubstatementsContextIndependent()
359             && noSensitiveSubstatements(castMaterialized(substatements).values());
360     }
361
362     @Override
363     <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgumentImpl(
364             final @NonNull Class<Z> type) {
365         if (substatements instanceof List) {
366             return super.findSubstatementArgumentImpl(type);
367         }
368
369         final Optional<X> templateArg = prototype.findSubstatementArgument(type);
370         if (templateArg.isEmpty()) {
371             return templateArg;
372         }
373         if (SchemaTreeEffectiveStatement.class.isAssignableFrom(type)) {
374             // X is known to be QName
375             return (Optional<X>) templateArg.map(template -> ((QName) template).bindTo(targetModule));
376         }
377         return templateArg;
378     }
379
380     @Override
381     boolean hasSubstatementImpl(final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
382         return substatements instanceof List ? super.hasSubstatementImpl(type)
383             // We do not allow deletion of partially-materialized statements, hence this is accurate
384             : prototype.hasSubstatement(type);
385     }
386
387     @Override
388     public <Y extends DeclaredStatement<QName>, Z extends SchemaTreeEffectiveStatement<Y>>
389             StmtContext<QName, Y, Z> requestSchemaTreeChild(final QName qname) {
390         if (substatements instanceof List) {
391             // We have performed materialization, hence we have triggered creation of all our schema tree child
392             // statements.
393             return null;
394         }
395
396         final QName templateQName = qname.bindTo(StmtContextUtils.getRootModuleQName(prototype));
397         LOG.debug("Materializing child {} from {}", qname, templateQName);
398
399         final StmtContext<?, ?, ?> template;
400         if (prototype instanceof InferredStatementContext) {
401             // Note: we need to access namespace here, as the target statement may have already been populated, in which
402             //       case we want to obtain the statement in local namespace storage.
403             template = (StmtContext) ((InferredStatementContext<?, ?, ?>) prototype).getFromNamespace(
404                 SchemaTreeNamespace.class, templateQName);
405         } else {
406             template = prototype.allSubstatementsStream()
407                 .filter(stmt -> stmt.producesEffective(SchemaTreeEffectiveStatement.class)
408                     && templateQName.equals(stmt.argument()))
409                 .findAny()
410                 .orElse(null);
411         }
412
413         if (template == null) {
414             // We do not have a template, this child does not exist. It may be added later, but that is someone else's
415             // responsibility.
416             LOG.debug("Child {} does not have a template", qname);
417             return null;
418         }
419
420         @SuppressWarnings("unchecked")
421         final Mutable<QName, Y, Z> ret = (Mutable<QName, Y, Z>) copySubstatement((Mutable<?, ?, ?>) template)
422             .orElseThrow(
423                 () -> new InferenceException(this, "Failed to materialize child %s template %s", qname, template));
424         ensureCompletedPhase(ret);
425         addMaterialized(template, ret);
426
427         LOG.debug("Child {} materialized", qname);
428         return ret;
429     }
430
431     // Instantiate this statement's effective substatements. Note this method has side-effects in namespaces and overall
432     // BuildGlobalContext, hence it must be called at most once.
433     private List<ReactorStmtCtx<?, ?, ?>> ensureEffectiveSubstatements() {
434         accessSubstatements();
435         return substatements instanceof List ? castEffective(substatements)
436             : initializeSubstatements(castMaterialized(substatements));
437     }
438
439     @Override
440     Iterable<ReactorStmtCtx<?, ?, ?>> effectiveChildrenToComplete() {
441         // When we have not initialized, there are no statements to catch up: we will catch up when we are copying
442         // from prototype (which is already at ModelProcessingPhase.EFFECTIVE_MODEL).
443         if (substatements == null) {
444             return ImmutableList.of();
445         }
446         accessSubstatements();
447         if (substatements instanceof HashMap) {
448             return castMaterialized(substatements).values();
449         } else {
450             return castEffective(substatements);
451         }
452     }
453
454     @Override
455     Stream<? extends @NonNull StmtContext<?, ?, ?>> streamDeclared() {
456         return Stream.empty();
457     }
458
459     @Override
460     Stream<? extends @NonNull StmtContext<?, ?, ?>> streamEffective() {
461         return ensureEffectiveSubstatements().stream().filter(StmtContext::isSupportedToBuildEffective);
462     }
463
464     private void accessSubstatements() {
465         if (substatements instanceof String) {
466             throw new VerifyException("Access to " + substatements + " substatements of " + this);
467         }
468     }
469
470     @Override
471     void markNoParentRef() {
472         final Object local = substatements;
473         if (local != null) {
474             markNoParentRef(castEffective(local));
475         }
476     }
477
478     @Override
479     int sweepSubstatements() {
480         final Object local = substatements;
481         substatements = SWEPT_SUBSTATEMENTS;
482         int count = 0;
483         if (local instanceof List) {
484             final List<ReactorStmtCtx<?, ?, ?>> list = castEffective(local);
485             sweep(list);
486             count = countUnswept(list);
487         }
488         return count;
489     }
490
491     private List<ReactorStmtCtx<?, ?, ?>> initializeSubstatements(
492             final Map<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree) {
493         final Collection<? extends StatementContextBase<?, ?, ?>> declared = prototype.mutableDeclaredSubstatements();
494         final Collection<? extends Mutable<?, ?, ?>> effective = prototype.mutableEffectiveSubstatements();
495
496         final List<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
497         for (final Mutable<?, ?, ?> stmtContext : declared) {
498             if (stmtContext.isSupportedByFeatures()) {
499                 copySubstatement(stmtContext, buffer, materializedSchemaTree);
500             }
501         }
502         for (final Mutable<?, ?, ?> stmtContext : effective) {
503             copySubstatement(stmtContext, buffer, materializedSchemaTree);
504         }
505
506         final List<ReactorStmtCtx<?, ?, ?>> ret = beforeAddEffectiveStatementUnsafe(ImmutableList.of(), buffer.size());
507         ret.addAll((Collection) buffer);
508         substatements = ret;
509
510         prototype.decRef();
511         return ret;
512     }
513
514     //
515     // Statement copy mess starts here. As it turns out, it's not that much of a mess, but it does make your head spin
516     // sometimes. Tread softly because you tread on my dreams.
517     //
518
519     private EffectiveCopy effectiveCopy(final ReactorStmtCtx<?, ?, ?> stmt) {
520         final ReactorStmtCtx<?, ?, ?> effective = stmt.asEffectiveChildOf(this, childCopyType, targetModule);
521         return effective == null ? null : new EffectiveCopy(stmt, effective);
522     }
523
524     private void copySubstatement(final Mutable<?, ?, ?> substatement, final Collection<Mutable<?, ?, ?>> buffer,
525             final Map<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree) {
526         // Consult materialized substatements. We are in a copy operation and will end up throwing materialized
527         // statements away -- hence we do not perform Map.remove() to save ourselves a mutation operation.
528         //
529         // We could also perform a Map.containsKey() and perform a bulk add, but that would mean the statement order
530         // against parent would change -- and we certainly do not want that to happen.
531         final ReactorStmtCtx<?, ?, ?> materialized = findMaterialized(materializedSchemaTree, substatement);
532         if (materialized == null) {
533             copySubstatement(substatement).ifPresent(copy -> {
534                 ensureCompletedPhase(copy);
535                 buffer.add(copy);
536             });
537         } else {
538             buffer.add(materialized);
539         }
540     }
541
542     private Optional<? extends Mutable<?, ?, ?>> copySubstatement(final Mutable<?, ?, ?> substatement) {
543         return substatement.copyAsChildOf(this, childCopyType, targetModule);
544     }
545
546     private void addMaterialized(final StmtContext<?, ?, ?> template, final Mutable<?, ?, ?> copy) {
547         final HashMap<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree;
548         if (substatements == null) {
549             // Lazy initialization of backing map. We do not expect this to be used often or multiple times -- each hit
550             // here means an inference along schema tree, such as deviate/augment. HashMap requires power-of-two and
551             // defaults to 0.75 load factor -- we therefore size it to 4, i.e. next two inserts will not cause a
552             // resizing operation.
553             materializedSchemaTree = new HashMap<>(4);
554             substatements = materializedSchemaTree;
555         } else {
556             verify(substatements instanceof HashMap, "Unexpected substatements %s", substatements);
557             materializedSchemaTree = castMaterialized(substatements);
558         }
559
560         final StmtContext<?, ?, ?> existing = materializedSchemaTree.put(template,
561             (StatementContextBase<?, ?, ?>) copy);
562         if (existing != null) {
563             throw new VerifyException(
564                 "Unexpected duplicate request for " + copy.argument() + " previous result was " + existing);
565         }
566     }
567
568     private static @Nullable ReactorStmtCtx<?, ?, ?> findMaterialized(
569             final Map<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree,
570             final StmtContext<?, ?, ?> template) {
571         return materializedSchemaTree == null ? null : materializedSchemaTree.get(template);
572     }
573
574     @SuppressWarnings("unchecked")
575     private static List<ReactorStmtCtx<?, ?, ?>> castEffective(final Object substatements) {
576         return (List<ReactorStmtCtx<?, ?, ?>>) substatements;
577     }
578
579     @SuppressWarnings("unchecked")
580     private static HashMap<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> castMaterialized(final Object substatements) {
581         return (HashMap<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>>) substatements;
582     }
583
584     // Statement copy mess ends here
585
586     /*
587      * KEEP THINGS ORGANIZED!
588      *
589      * below methods exist in the same form in SubstatementContext. If any adjustment is made here, make sure it is
590      * properly updated there.
591      */
592     @Override
593     @Deprecated
594     public SchemaPath schemaPath() {
595         return substatementGetSchemaPath();
596     }
597
598     @Override
599     public A argument() {
600         return argument;
601     }
602
603     @Override
604     public StatementContextBase<?, ?, ?> getParentContext() {
605         return parent;
606     }
607
608     @Override
609     public StorageNodeType getStorageNodeType() {
610         return StorageNodeType.STATEMENT_LOCAL;
611     }
612
613     @Override
614     public StatementContextBase<?, ?, ?> getParentNamespaceStorage() {
615         return parent;
616     }
617
618     @Override
619     public RootStatementContext<?, ?, ?> getRoot() {
620         return parent.getRoot();
621     }
622
623     @Override
624     public EffectiveConfig effectiveConfig() {
625         return effectiveConfig(parent);
626     }
627
628     @Override
629     protected boolean isIgnoringIfFeatures() {
630         return isIgnoringIfFeatures(parent);
631     }
632
633     @Override
634     protected boolean isIgnoringConfig() {
635         return isIgnoringConfig(parent);
636     }
637
638     @Override
639     protected boolean isParentSupportedByFeatures() {
640         return parent.isSupportedByFeatures();
641     }
642 }