b371a067447893b18662223174a9ea5372f21866
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / ReactorStmtCtx.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
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.base.VerifyException;
15 import java.util.Collection;
16 import java.util.Map;
17 import java.util.Optional;
18 import java.util.Set;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.YangVersion;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
27 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
28 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.ConfigEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.DeviationStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
33 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
34 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
43 import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace;
44 import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace.SupportedFeatures;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * Real "core" reactor statement implementation of {@link Mutable}, supporting basic reactor lifecycle.
50  *
51  * @param <A> Argument type
52  * @param <D> Declared Statement representation
53  * @param <E> Effective Statement representation
54  */
55 abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
56         extends NamespaceStorageSupport implements Mutable<A, D, E> {
57     private static final Logger LOG = LoggerFactory.getLogger(ReactorStmtCtx.class);
58
59     /**
60      * Substatement refcount tracking. This mechanics deals with retaining substatements for the purposes of
61      * instantiating their lazy copies in InferredStatementContext. It works in concert with {@link #buildEffective()}
62      * and {@link #buildDeclared()}: declared/effective statement views hold an implicit reference and refcount-based
63      * sweep is not activated until they are done (or this statement is not {@link #isSupportedToBuildEffective}).
64      *
65      * <p>
66      * Reference count is hierarchical in that parent references also pin down their child statements and do not allow
67      * them to be swept.
68      *
69      * <p>
70      * The counter's positive values are tracking incoming references via {@link #incRef()}/{@link #decRef()} methods.
71      * Once we transition to sweeping, this value becomes negative counting upwards to {@link #REFCOUNT_NONE} based on
72      * {@link #sweepOnChildDone()}. Once we reach that, we transition to {@link #REFCOUNT_SWEPT}.
73      */
74     private int refcount = REFCOUNT_NONE;
75     /**
76      * No outstanding references, this statement is a potential candidate for sweeping, provided it has populated its
77      * declared and effective views and {@link #parentRef} is known to be absent.
78      */
79     private static final int REFCOUNT_NONE = 0;
80     /**
81      * Reference count overflow or some other recoverable logic error. Do not rely on refcounts and do not sweep
82      * anything.
83      *
84      * <p>
85      * Note on value assignment:
86      * This allow our incRef() to naturally progress to being saturated. Others jump there directly.
87      * It also makes it  it impossible to observe {@code Interger.MAX_VALUE} children, which we take advantage of for
88      * {@link #REFCOUNT_SWEEPING}.
89      */
90     private static final int REFCOUNT_DEFUNCT = Integer.MAX_VALUE;
91     /**
92      * This statement is being actively swept. This is a transient value set when we are sweeping our children, so that
93      * we prevent re-entering this statement.
94      *
95      * <p>
96      * Note on value assignment:
97      * The value is lower than any legal child refcount due to {@link #REFCOUNT_DEFUNCT} while still being higher than
98      * {@link #REFCOUNT_SWEPT}.
99      */
100     private static final int REFCOUNT_SWEEPING = -Integer.MAX_VALUE;
101     /**
102      * This statement, along with its entire subtree has been swept and we positively know all our children have reached
103      * this state. We {@link #sweepNamespaces()} upon reaching this state.
104      *
105      * <p>
106      * Note on value assignment:
107      * This is the lowest value observable, making it easier on checking others on equality.
108      */
109     private static final int REFCOUNT_SWEPT = Integer.MIN_VALUE;
110
111     private @Nullable E effectiveInstance;
112
113     // Master flag controlling whether this context can yield an effective statement
114     // FIXME: investigate the mechanics that are being supported by this, as it would be beneficial if we can get rid
115     //        of this flag -- eliminating the initial alignment shadow used by below gap-filler fields.
116     private boolean isSupportedToBuildEffective = true;
117
118     // Flag bit assignments
119     private static final int IS_SUPPORTED_BY_FEATURES    = 0x01;
120     private static final int HAVE_SUPPORTED_BY_FEATURES  = 0x02;
121     private static final int IS_IGNORE_IF_FEATURE        = 0x04;
122     private static final int HAVE_IGNORE_IF_FEATURE      = 0x08;
123     // Note: these four are related
124     private static final int IS_IGNORE_CONFIG            = 0x10;
125     private static final int HAVE_IGNORE_CONFIG          = 0x20;
126     private static final int IS_CONFIGURATION            = 0x40;
127     private static final int HAVE_CONFIGURATION          = 0x80;
128
129     // Have-and-set flag constants, also used as masks
130     private static final int SET_SUPPORTED_BY_FEATURES = HAVE_SUPPORTED_BY_FEATURES | IS_SUPPORTED_BY_FEATURES;
131     private static final int SET_CONFIGURATION = HAVE_CONFIGURATION | IS_CONFIGURATION;
132     // Note: implies SET_CONFIGURATION, allowing fewer bit operations to be performed
133     private static final int SET_IGNORE_CONFIG = HAVE_IGNORE_CONFIG | IS_IGNORE_CONFIG | SET_CONFIGURATION;
134     private static final int SET_IGNORE_IF_FEATURE = HAVE_IGNORE_IF_FEATURE | IS_IGNORE_IF_FEATURE;
135
136     // Flags for use with SubstatementContext. These are hiding in the alignment shadow created by above boolean and
137     // hence improve memory layout.
138     private byte flags;
139
140     // Flag for use with AbstractResumedStatement. This is hiding in the alignment shadow created by above boolean
141     // FIXME: move this out once we have JDK15+
142     private boolean fullyDefined;
143
144     // SchemaPath cache for use with SubstatementContext and InferredStatementContext. This hurts RootStatementContext
145     // a bit in terms of size -- but those are only a few and SchemaPath is on its way out anyway.
146     @Deprecated
147     private volatile SchemaPath schemaPath;
148
149     ReactorStmtCtx() {
150         // Empty on purpose
151     }
152
153     ReactorStmtCtx(final ReactorStmtCtx<A, D, E> original) {
154         isSupportedToBuildEffective = original.isSupportedToBuildEffective;
155         fullyDefined = original.fullyDefined;
156         flags = original.flags;
157     }
158
159     //
160     //
161     // Common public interface contracts with simple mechanics. Please keep this in one logical block, so we do not end
162     // up mixing concerns and simple details with more complex logic.
163     //
164     //
165
166     @Override
167     public abstract StatementContextBase<?, ?, ?> getParentContext();
168
169     @Override
170     public abstract RootStatementContext<?, ?, ?> getRoot();
171
172     @Override
173     public abstract Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements();
174
175     @Override
176     public final @NonNull Registry getBehaviourRegistry() {
177         return getRoot().getBehaviourRegistryImpl();
178     }
179
180     @Override
181     public final YangVersion getRootVersion() {
182         return getRoot().getRootVersionImpl();
183     }
184
185     @Override
186     public final void setRootVersion(final YangVersion version) {
187         getRoot().setRootVersionImpl(version);
188     }
189
190     @Override
191     public final void addMutableStmtToSeal(final MutableStatement mutableStatement) {
192         getRoot().addMutableStmtToSealImpl(mutableStatement);
193     }
194
195     @Override
196     public final void addRequiredSource(final SourceIdentifier dependency) {
197         getRoot().addRequiredSourceImpl(dependency);
198     }
199
200     @Override
201     public final void setRootIdentifier(final SourceIdentifier identifier) {
202         getRoot().setRootIdentifierImpl(identifier);
203     }
204
205     @Override
206     public final boolean isEnabledSemanticVersioning() {
207         return getRoot().isEnabledSemanticVersioningImpl();
208     }
209
210     @Override
211     public final ModelActionBuilder newInferenceAction(final ModelProcessingPhase phase) {
212         return getRoot().getSourceContext().newInferenceAction(phase);
213     }
214
215     @Override
216     public final StatementDefinition publicDefinition() {
217         return definition().getPublicView();
218     }
219
220     @Override
221     public final String toString() {
222         return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
223     }
224
225     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
226         return toStringHelper.add("definition", definition()).add("rawArgument", rawArgument());
227     }
228
229     /**
230      * Return the context in which this statement was defined.
231      *
232      * @return statement definition
233      */
234     abstract @NonNull StatementDefinitionContext<A, D, E> definition();
235
236     //
237     //
238     // NamespaceStorageSupport/Mutable integration methods. Keep these together.
239     //
240     //
241
242     @Override
243     public final <K, V, T extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(
244             final Class<@NonNull N> type, final T key) {
245         return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this, key);
246     }
247
248     @Override
249     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(final Class<N> type) {
250         return getNamespace(type);
251     }
252
253     @Override
254     public final <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(
255             final Class<N> type) {
256         return getLocalNamespace(type);
257     }
258
259     @Override
260     protected final void checkLocalNamespaceAllowed(final Class<? extends IdentifierNamespace<?, ?>> type) {
261         definition().checkNamespaceAllowed(type);
262     }
263
264     @Override
265     protected <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceElementAdded(final Class<N> type, final K key,
266             final V value) {
267         // definition().onNamespaceElementAdded(this, type, key, value);
268     }
269
270     //
271     //
272     // Statement build entry points -- both public and package-private.
273     //
274     //
275
276     @Override
277     public final E buildEffective() {
278         final E existing;
279         return (existing = effectiveInstance) != null ? existing : loadEffective();
280     }
281
282     private E loadEffective() {
283         // Creating an effective statement does not strictly require a declared instance -- there are statements like
284         // 'input', which are implicitly defined.
285         // Our implementation design makes an invariant assumption that buildDeclared() has been called by the time
286         // we attempt to create effective statement:
287         buildDeclared();
288
289         final E ret = effectiveInstance = createEffective();
290         // we have called createEffective(), substatements are no longer guarded by us. Let's see if we can clear up
291         // some residue.
292         if (refcount == REFCOUNT_NONE) {
293             sweepOnDecrement();
294         }
295         return ret;
296     }
297
298     abstract @NonNull E createEffective();
299
300     //
301     //
302     // Flags-based mechanics. These include public interfaces as well as all the crud we have lurking in our alignment
303     // shadow.
304     //
305     //
306
307     @Override
308     public final boolean isSupportedToBuildEffective() {
309         return isSupportedToBuildEffective;
310     }
311
312     @Override
313     public final void setIsSupportedToBuildEffective(final boolean isSupportedToBuildEffective) {
314         this.isSupportedToBuildEffective = isSupportedToBuildEffective;
315     }
316
317     @Override
318     public final boolean isSupportedByFeatures() {
319         final int fl = flags & SET_SUPPORTED_BY_FEATURES;
320         if (fl != 0) {
321             return fl == SET_SUPPORTED_BY_FEATURES;
322         }
323         if (isIgnoringIfFeatures()) {
324             flags |= SET_SUPPORTED_BY_FEATURES;
325             return true;
326         }
327
328         /*
329          * If parent is supported, we need to check if-features statements of this context.
330          */
331         if (isParentSupportedByFeatures()) {
332             // If the set of supported features has not been provided, all features are supported by default.
333             final Set<QName> supportedFeatures = getFromNamespace(SupportedFeaturesNamespace.class,
334                     SupportedFeatures.SUPPORTED_FEATURES);
335             if (supportedFeatures == null || StmtContextUtils.checkFeatureSupport(this, supportedFeatures)) {
336                 flags |= SET_SUPPORTED_BY_FEATURES;
337                 return true;
338             }
339         }
340
341         // Either parent is not supported or this statement is not supported
342         flags |= HAVE_SUPPORTED_BY_FEATURES;
343         return false;
344     }
345
346     protected abstract boolean isParentSupportedByFeatures();
347
348     /**
349      * Config statements are not all that common which means we are performing a recursive search towards the root
350      * every time {@link #isConfiguration()} is invoked. This is quite expensive because it causes a linear search
351      * for the (usually non-existent) config statement.
352      *
353      * <p>
354      * This method maintains a resolution cache, so once we have returned a result, we will keep on returning the same
355      * result without performing any lookups, solely to support {@link SubstatementContext#isConfiguration()}.
356      *
357      * <p>
358      * Note: use of this method implies that {@link #isIgnoringConfig()} is realized with
359      *       {@link #isIgnoringConfig(StatementContextBase)}.
360      */
361     final boolean isConfiguration(final StatementContextBase<?, ?, ?> parent) {
362         final int fl = flags & SET_CONFIGURATION;
363         if (fl != 0) {
364             return fl == SET_CONFIGURATION;
365         }
366         if (isIgnoringConfig(parent)) {
367             // Note: SET_CONFIGURATION has been stored in flags
368             return true;
369         }
370
371         final boolean isConfig;
372         final Optional<Boolean> optConfig = findSubstatementArgument(ConfigEffectiveStatement.class);
373         if (optConfig.isPresent()) {
374             isConfig = optConfig.orElseThrow();
375             if (isConfig) {
376                 // Validity check: if parent is config=false this cannot be a config=true
377                 InferenceException.throwIf(!parent.isConfiguration(), sourceReference(),
378                         "Parent node has config=false, this node must not be specifed as config=true");
379             }
380         } else {
381             // If "config" statement is not specified, the default is the same as the parent's "config" value.
382             isConfig = parent.isConfiguration();
383         }
384
385         // Resolved, make sure we cache this return
386         flags |= isConfig ? SET_CONFIGURATION : HAVE_CONFIGURATION;
387         return isConfig;
388     }
389
390     protected abstract boolean isIgnoringConfig();
391
392     /**
393      * This method maintains a resolution cache for ignore config, so once we have returned a result, we will
394      * keep on returning the same result without performing any lookups. Exists only to support
395      * {@link SubstatementContext#isIgnoringConfig()}.
396      *
397      * <p>
398      * Note: use of this method implies that {@link #isConfiguration()} is realized with
399      *       {@link #isConfiguration(StatementContextBase)}.
400      */
401     final boolean isIgnoringConfig(final StatementContextBase<?, ?, ?> parent) {
402         final int fl = flags & SET_IGNORE_CONFIG;
403         if (fl != 0) {
404             return fl == SET_IGNORE_CONFIG;
405         }
406         if (definition().support().isIgnoringConfig() || parent.isIgnoringConfig()) {
407             flags |= SET_IGNORE_CONFIG;
408             return true;
409         }
410
411         flags |= HAVE_IGNORE_CONFIG;
412         return false;
413     }
414
415     protected abstract boolean isIgnoringIfFeatures();
416
417     /**
418      * This method maintains a resolution cache for ignore if-feature, so once we have returned a result, we will
419      * keep on returning the same result without performing any lookups. Exists only to support
420      * {@link SubstatementContext#isIgnoringIfFeatures()}.
421      */
422     final boolean isIgnoringIfFeatures(final StatementContextBase<?, ?, ?> parent) {
423         final int fl = flags & SET_IGNORE_IF_FEATURE;
424         if (fl != 0) {
425             return fl == SET_IGNORE_IF_FEATURE;
426         }
427         if (definition().support().isIgnoringIfFeatures() || parent.isIgnoringIfFeatures()) {
428             flags |= SET_IGNORE_IF_FEATURE;
429             return true;
430         }
431
432         flags |= HAVE_IGNORE_IF_FEATURE;
433         return false;
434     }
435
436     // These two exists only due to memory optimization, should live in AbstractResumedStatement. We are also reusing
437     // this for ReplicaStatementContext's refcount tracking.
438     final boolean fullyDefined() {
439         return fullyDefined;
440     }
441
442     final void setFullyDefined() {
443         fullyDefined = true;
444     }
445
446     //
447     //
448     // Common SchemaPath cache. All of this is bound to be removed once YANGTOOLS-1066 is done.
449     //
450     //
451
452     abstract @NonNull Optional<SchemaPath> schemaPath();
453
454     // Exists only to support {SubstatementContext,InferredStatementContext}.schemaPath()
455     @Deprecated
456     final @NonNull Optional<SchemaPath> substatementGetSchemaPath() {
457         SchemaPath local = schemaPath;
458         if (local == null) {
459             synchronized (this) {
460                 local = schemaPath;
461                 if (local == null) {
462                     schemaPath = local = createSchemaPath((StatementContextBase<?, ?, ?>) coerceParentContext());
463                 }
464             }
465         }
466
467         return Optional.ofNullable(local);
468     }
469
470     @Deprecated
471     private SchemaPath createSchemaPath(final StatementContextBase<?, ?, ?> parent) {
472         final Optional<SchemaPath> maybeParentPath = parent.schemaPath();
473         verify(maybeParentPath.isPresent(), "Parent %s does not have a SchemaPath", parent);
474         final SchemaPath parentPath = maybeParentPath.get();
475
476         if (StmtContextUtils.isUnknownStatement(this)) {
477             return parentPath.createChild(publicDefinition().getStatementName());
478         }
479         final Object argument = argument();
480         if (argument instanceof QName) {
481             final QName qname = (QName) argument;
482             if (producesDeclared(UsesStatement.class)) {
483                 return maybeParentPath.orElse(null);
484             }
485
486             return parentPath.createChild(qname);
487         }
488         if (argument instanceof String) {
489             // FIXME: This may yield illegal argument exceptions
490             final Optional<StmtContext<A, D, E>> originalCtx = getOriginalCtx();
491             final QName qname = StmtContextUtils.qnameFromArgument(originalCtx.orElse(this), (String) argument);
492             return parentPath.createChild(qname);
493         }
494         if (argument instanceof SchemaNodeIdentifier
495                 && (producesDeclared(AugmentStatement.class) || producesDeclared(RefineStatement.class)
496                         || producesDeclared(DeviationStatement.class))) {
497
498             return parentPath.createChild(((SchemaNodeIdentifier) argument).getNodeIdentifiers());
499         }
500
501         // FIXME: this does not look right
502         return maybeParentPath.orElse(null);
503     }
504
505     //
506     //
507     // Reference counting mechanics start. Please keep these methods in one block for clarity. Note this does not
508     // contribute to state visible outside of this package.
509     //
510     //
511
512     /**
513      * Acquire a reference on this context. As long as there is at least one reference outstanding,
514      * {@link #buildEffective()} will not result in {@link #effectiveSubstatements()} being discarded.
515      *
516      * @throws VerifyException if {@link #effectiveSubstatements()} has already been discarded
517      */
518     final void incRef() {
519         final int current = refcount;
520         verify(current >= REFCOUNT_NONE, "Attempted to access reference count of %s", this);
521         if (current != REFCOUNT_DEFUNCT) {
522             // Note: can end up becoming REFCOUNT_DEFUNCT on overflow
523             refcount = current + 1;
524         } else {
525             LOG.debug("Disabled refcount increment of {}", this);
526         }
527     }
528
529     /**
530      * Release a reference on this context. This call may result in {@link #effectiveSubstatements()} becoming
531      * unavailable.
532      */
533     final void decRef() {
534         final int current = refcount;
535         if (current == REFCOUNT_DEFUNCT) {
536             // no-op
537             LOG.debug("Disabled refcount decrement of {}", this);
538             return;
539         }
540         if (current <= REFCOUNT_NONE) {
541             // Underflow, become defunct
542             LOG.warn("Statement refcount underflow, reference counting disabled for {}", this, new Throwable());
543             refcount = REFCOUNT_DEFUNCT;
544             return;
545         }
546
547         refcount = current - 1;
548         LOG.trace("Refcount {} on {}", refcount, this);
549         if (isSweepable()) {
550             // We are no longer guarded by effective instance
551             sweepOnDecrement();
552         }
553     }
554
555     /**
556      * Sweep this statement context as a result of {@link #sweepSubstatements()}, i.e. when parent is also being swept.
557      */
558     private void sweep() {
559         if (isSweepable()) {
560             LOG.trace("Releasing {}", this);
561             sweepState();
562         }
563     }
564
565     static final void sweep(final Collection<? extends ReactorStmtCtx<?, ?, ?>> substatements) {
566         for (ReactorStmtCtx<?, ?, ?> stmt : substatements) {
567             stmt.sweep();
568         }
569     }
570
571     static final int countUnswept(final Collection<? extends ReactorStmtCtx<?, ?, ?>> substatements) {
572         int result = 0;
573         for (ReactorStmtCtx<?, ?, ?> stmt : substatements) {
574             if (stmt.refcount > REFCOUNT_NONE || !stmt.noImplictRef()) {
575                 result++;
576             }
577         }
578         return result;
579     }
580
581     /**
582      * Implementation-specific sweep action. This is expected to perform a recursive {@link #sweep(Collection)} on all
583      * {@link #declaredSubstatements()} and {@link #effectiveSubstatements()} and report the result of the sweep
584      * operation.
585      *
586      * <p>
587      * {@link #effectiveSubstatements()} as well as namespaces may become inoperable as a result of this operation.
588      *
589      * @return True if the entire tree has been completely swept, false otherwise.
590      */
591     abstract int sweepSubstatements();
592
593     // Called when this statement does not have an implicit reference and have reached REFCOUNT_NONE
594     private void sweepOnDecrement() {
595         LOG.trace("Sweeping on decrement {}", this);
596         if (noParentRefcount()) {
597             // No further parent references, sweep our state.
598             sweepState();
599         }
600
601         // Propagate towards parent if there is one
602         final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
603         if (parent != null) {
604             parent.sweepOnChildDecrement();
605         }
606     }
607
608     // Called from child when it has lost its final reference
609     private void sweepOnChildDecrement() {
610         if (isAwaitingChildren()) {
611             // We are a child for which our parent is waiting. Notify it and we are done.
612             sweepOnChildDone();
613             return;
614         }
615
616         // Check parent reference count
617         final int refs = refcount;
618         if (refs > REFCOUNT_NONE || refs <= REFCOUNT_SWEEPING || !noImplictRef()) {
619             // No-op
620             return;
621         }
622
623         // parent is potentially reclaimable
624         if (noParentRefcount()) {
625             LOG.trace("Cleanup {} of parent {}", refcount, this);
626             if (sweepState()) {
627                 final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
628                 if (parent != null) {
629                     parent.sweepOnChildDecrement();
630                 }
631             }
632         }
633     }
634
635     private boolean noImplictRef() {
636         return effectiveInstance != null || !isSupportedToBuildEffective();
637     }
638
639     // FIXME: cache the resolution of this
640     private boolean noParentRefcount() {
641         final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
642         if (parent != null) {
643             // There are three possibilities:
644             // - REFCOUNT_NONE, in which case we need to search next parent
645             // - negative (< REFCOUNT_NONE), meaning parent is in some stage of sweeping, hence it does not have
646             //   a reference to us
647             // - positive (> REFCOUNT_NONE), meaning parent has an explicit refcount which is holding us down
648             final int refs = parent.refcount;
649             return refs == REFCOUNT_NONE ? parent.noParentRefcount() : refs < REFCOUNT_NONE;
650         }
651         return true;
652     }
653
654     private boolean isAwaitingChildren() {
655         return refcount > REFCOUNT_SWEEPING && refcount < REFCOUNT_NONE;
656     }
657
658     private boolean isSweepable() {
659         return refcount == REFCOUNT_NONE && noImplictRef();
660     }
661
662     private void sweepOnChildDone() {
663         LOG.trace("Sweeping on child done {}", this);
664         final int current = refcount;
665         if (current >= REFCOUNT_NONE) {
666             // no-op, perhaps we want to handle some cases differently?
667             LOG.trace("Ignoring child sweep of {} for {}", this, current);
668             return;
669         }
670         verify(current != REFCOUNT_SWEPT, "Attempt to sweep a child of swept %s", this);
671
672         refcount = current + 1;
673         LOG.trace("Child refcount {}", refcount);
674         if (refcount == REFCOUNT_NONE) {
675             sweepDone();
676             final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
677             LOG.trace("Propagating to parent {}", parent);
678             if (parent != null && parent.isAwaitingChildren()) {
679                 parent.sweepOnChildDone();
680             }
681         }
682     }
683
684     private void sweepDone() {
685         LOG.trace("Sweep done for {}", this);
686         refcount = REFCOUNT_SWEPT;
687         sweepNamespaces();
688     }
689
690     private boolean sweepState() {
691         refcount = REFCOUNT_SWEEPING;
692         final int childRefs = sweepSubstatements();
693         if (childRefs == 0) {
694             sweepDone();
695             return true;
696         }
697         if (childRefs < 0 || childRefs >= REFCOUNT_DEFUNCT) {
698             LOG.warn("Negative child refcount {} cannot be stored, reference counting disabled for {}", childRefs, this,
699                 new Throwable());
700             refcount = REFCOUNT_DEFUNCT;
701         } else {
702             LOG.trace("Still {} outstanding children of {}", childRefs, this);
703             refcount = -childRefs;
704         }
705         return false;
706     }
707 }