2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import com.google.common.base.VerifyException;
16 import java.util.Collection;
18 import java.util.Optional;
19 import java.util.stream.Stream;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.common.YangVersion;
25 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
28 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
29 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.ConfigEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.DeviationStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
34 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStatementState;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.ExecutionOrder;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementFactory;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
47 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * Real "core" reactor statement implementation of {@link Mutable}, supporting basic reactor lifecycle.
54 * @param <A> Argument type
55 * @param <D> Declared Statement representation
56 * @param <E> Effective Statement representation
58 abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
59 extends AbstractNamespaceStorage implements Mutable<A, D, E>, Current<A, D> {
60 private static final Logger LOG = LoggerFactory.getLogger(ReactorStmtCtx.class);
63 * Substatement refcount tracking. This mechanics deals with retaining substatements for the purposes of
64 * instantiating their lazy copies in InferredStatementContext. It works in concert with {@link #buildEffective()}
65 * and {@link #declared()}: declared/effective statement views hold an implicit reference and refcount-based
66 * sweep is not activated until they are done (or this statement is not {@link #isSupportedToBuildEffective}).
69 * Reference count is hierarchical in that parent references also pin down their child statements and do not allow
73 * The counter's positive values are tracking incoming references via {@link #incRef()}/{@link #decRef()} methods.
74 * Once we transition to sweeping, this value becomes negative counting upwards to {@link #REFCOUNT_NONE} based on
75 * {@link #sweepOnChildDone()}. Once we reach that, we transition to {@link #REFCOUNT_SWEPT}.
77 private int refcount = REFCOUNT_NONE;
79 * No outstanding references, this statement is a potential candidate for sweeping, provided it has populated its
80 * declared and effective views and {@link #parentRef} is known to be absent.
82 private static final int REFCOUNT_NONE = 0;
84 * Reference count overflow or some other recoverable logic error. Do not rely on refcounts and do not sweep
88 * Note on value assignment:
89 * This allow our incRef() to naturally progress to being saturated. Others jump there directly.
90 * It also makes it it impossible to observe {@code Interger.MAX_VALUE} children, which we take advantage of for
91 * {@link #REFCOUNT_SWEEPING}.
93 private static final int REFCOUNT_DEFUNCT = Integer.MAX_VALUE;
95 * This statement is being actively swept. This is a transient value set when we are sweeping our children, so that
96 * we prevent re-entering this statement.
99 * Note on value assignment:
100 * The value is lower than any legal child refcount due to {@link #REFCOUNT_DEFUNCT} while still being higher than
101 * {@link #REFCOUNT_SWEPT}.
103 private static final int REFCOUNT_SWEEPING = -Integer.MAX_VALUE;
105 * This statement, along with its entire subtree has been swept and we positively know all our children have reached
106 * this state. We {@link #sweepNamespaces()} upon reaching this state.
109 * Note on value assignment:
110 * This is the lowest value observable, making it easier on checking others on equality.
112 private static final int REFCOUNT_SWEPT = Integer.MIN_VALUE;
115 * Effective instance built from this context. This field as dual types. Under normal circumstances in matches the
116 * {@link #buildEffective()} instance. If this context is reused, it can be inflated to {@link EffectiveInstances}
117 * and also act as a common instance reuse site.
119 private @Nullable Object effectiveInstance;
121 // Master flag controlling whether this context can yield an effective statement
122 // FIXME: investigate the mechanics that are being supported by this, as it would be beneficial if we can get rid
123 // of this flag -- eliminating the initial alignment shadow used by below gap-filler fields.
124 private boolean isSupportedToBuildEffective = true;
126 // EffectiveConfig mapping
127 private static final int MASK_CONFIG = 0x03;
128 private static final int HAVE_CONFIG = 0x04;
129 // Effective instantiation mechanics for StatementContextBase: if this flag is set all substatements are known not
130 // change when instantiated. This includes context-independent statements as well as any statements which are
131 // ignored during copy instantiation.
132 private static final int ALL_INDEPENDENT = 0x08;
133 // Flag bit assignments
134 private static final int IS_SUPPORTED_BY_FEATURES = 0x10;
135 private static final int HAVE_SUPPORTED_BY_FEATURES = 0x20;
136 private static final int IS_IGNORE_IF_FEATURE = 0x40;
137 private static final int HAVE_IGNORE_IF_FEATURE = 0x80;
138 // Have-and-set flag constants, also used as masks
139 private static final int SET_SUPPORTED_BY_FEATURES = HAVE_SUPPORTED_BY_FEATURES | IS_SUPPORTED_BY_FEATURES;
140 private static final int SET_IGNORE_IF_FEATURE = HAVE_IGNORE_IF_FEATURE | IS_IGNORE_IF_FEATURE;
142 private static final EffectiveConfig[] EFFECTIVE_CONFIGS;
145 final EffectiveConfig[] values = EffectiveConfig.values();
146 final int length = values.length;
147 verify(length == 4, "Unexpected EffectiveConfig cardinality %s", length);
148 EFFECTIVE_CONFIGS = values;
151 // Flags for use with SubstatementContext. These are hiding in the alignment shadow created by above boolean and
152 // hence improve memory layout.
159 ReactorStmtCtx(final ReactorStmtCtx<A, D, E> original) {
160 isSupportedToBuildEffective = original.isSupportedToBuildEffective;
161 flags = original.flags;
164 // Used by ReplicaStatementContext only
165 ReactorStmtCtx(final ReactorStmtCtx<A, D, E> original, final Void dummy) {
166 isSupportedToBuildEffective = original.isSupportedToBuildEffective;
167 flags = original.flags;
172 // Common public interface contracts with simple mechanics. Please keep this in one logical block, so we do not end
173 // up mixing concerns and simple details with more complex logic.
178 public abstract StatementContextBase<?, ?, ?> getParentContext();
181 public abstract RootStatementContext<?, ?, ?> getRoot();
184 public abstract Collection<? extends @NonNull StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements();
187 final <K, V> NamespaceAccess<K, V> accessNamespace(final ParserNamespace<K, V> type) {
188 return getRoot().getSourceContext().accessNamespace(type);
192 public final YangVersion yangVersion() {
193 return getRoot().getRootVersionImpl();
197 public final void setRootVersion(final YangVersion version) {
198 getRoot().setRootVersionImpl(version);
202 public final void addRequiredSource(final SourceIdentifier dependency) {
203 getRoot().addRequiredSourceImpl(dependency);
207 public final void setRootIdentifier(final SourceIdentifier identifier) {
208 getRoot().setRootIdentifierImpl(identifier);
212 public final ModelActionBuilder newInferenceAction(final ModelProcessingPhase phase) {
213 return getRoot().getSourceContext().newInferenceAction(phase);
217 public final StatementDefinition publicDefinition() {
218 return definition().getPublicView();
222 public final Parent effectiveParent() {
223 return getParentContext();
227 public final QName moduleName() {
228 final var root = getRoot();
229 return QName.create(StmtContextUtils.getModuleQName(root), root.getRawArgument());
233 // In the next two methods we are looking for an effective statement. If we already have an effective instance,
234 // defer to it's implementation of the equivalent search. Otherwise we search our substatement contexts.
236 // Note that the search function is split, so as to allow InferredStatementContext to do its own thing first.
240 public final <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgument(
241 final @NonNull Class<Z> type) {
242 final E existing = effectiveInstance();
243 return existing != null ? existing.findFirstEffectiveSubstatementArgument(type)
244 : findSubstatementArgumentImpl(type);
248 public final boolean hasSubstatement(final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
249 final E existing = effectiveInstance();
250 return existing != null ? existing.findFirstEffectiveSubstatement(type).isPresent() : hasSubstatementImpl(type);
253 private E effectiveInstance() {
254 final Object existing = effectiveInstance;
255 return existing != null ? EffectiveInstances.local(existing) : null;
258 // Visible due to InferredStatementContext's override. At this point we do not have an effective instance available.
259 <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgumentImpl(
260 final @NonNull Class<Z> type) {
261 return allSubstatementsStream()
262 .filter(ctx -> ctx.isSupportedToBuildEffective() && ctx.producesEffective(type))
264 .map(ctx -> (X) ctx.getArgument());
267 // Visible due to InferredStatementContext's override. At this point we do not have an effective instance available.
268 boolean hasSubstatementImpl(final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
269 return allSubstatementsStream()
270 .anyMatch(ctx -> ctx.isSupportedToBuildEffective() && ctx.producesEffective(type));
275 @SuppressWarnings("unchecked")
276 public final <Z extends EffectiveStatement<A, D>> StmtContext<A, D, Z> caerbannog() {
277 return (StmtContext<A, D, Z>) this;
281 public final String toString() {
282 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
285 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
286 return toStringHelper.add("definition", definition()).add("argument", argument()).add("refCount", refString());
289 private String refString() {
290 final int current = refcount;
291 return switch (current) {
292 case REFCOUNT_DEFUNCT -> "DEFUNCT";
293 case REFCOUNT_SWEEPING -> "SWEEPING";
294 case REFCOUNT_SWEPT -> "SWEPT";
295 default -> String.valueOf(refcount);
300 * Return the context in which this statement was defined.
302 * @return statement definition
304 abstract @NonNull StatementDefinitionContext<A, D, E> definition();
308 // AbstractNamespaceStorage/Mutable integration methods. Keep these together.
313 public StorageType getStorageType() {
314 // Common to all subclasses except RootStatementContext
315 return StorageType.STATEMENT_LOCAL;
319 public final <K, V> V namespaceItem(final ParserNamespace<K, V> namespace, final K key) {
320 return accessNamespace(namespace).valueFrom(this, key);
324 public final <K, V> Map<K, V> namespace(final ParserNamespace<K, V> namespace) {
325 return getNamespace(namespace);
329 public final <K, V> Map<K, V> localNamespacePortion(final ParserNamespace<K, V> namespace) {
330 return getLocalNamespace(namespace);
334 protected <K, V> void onNamespaceElementAdded(final ParserNamespace<K, V> type, final K key, final V value) {
335 // definition().onNamespaceElementAdded(this, type, key, value);
339 * Return the effective statement view of a copy operation. This method may return one of:
341 * <li>{@code this}, when the effective view did not change</li>
342 * <li>an InferredStatementContext, when there is a need for inference-equivalent copy</li>
343 * <li>{@code null}, when the statement failed to materialize</li>
346 * @param parent Proposed new parent
347 * @param type Copy operation type
348 * @param targetModule New target module
349 * @return {@link ReactorStmtCtx} holding effective view
351 abstract @Nullable ReactorStmtCtx<?, ?, ?> asEffectiveChildOf(StatementContextBase<?, ?, ?> parent, CopyType type,
352 QNameModule targetModule);
355 public final ReplicaStatementContext<A, D, E> replicaAsChildOf(final Mutable<?, ?, ?> parent) {
356 checkArgument(parent instanceof StatementContextBase, "Unsupported parent %s", parent);
357 final var ret = replicaAsChildOf((StatementContextBase<?, ?, ?>) parent);
358 definition().onStatementAdded(ret);
362 abstract @NonNull ReplicaStatementContext<A, D, E> replicaAsChildOf(@NonNull StatementContextBase<?, ?, ?> parent);
366 // Statement build entry points -- both public and package-private.
371 public final E buildEffective() {
372 final Object existing;
373 return (existing = effectiveInstance) != null ? EffectiveInstances.local(existing) : loadEffective();
376 private @NonNull E loadEffective() {
377 final E ret = createEffective();
378 effectiveInstance = ret;
379 // we have called createEffective(), substatements are no longer guarded by us. Let's see if we can clear up
381 if (refcount == REFCOUNT_NONE) {
387 abstract @NonNull E createEffective();
390 * Routing of the request to build an effective statement from {@link InferredStatementContext} towards the original
391 * definition site. This is needed to pick the correct instantiation method: for declared statements we will
392 * eventually land in {@link AbstractResumedStatement}, for underclared statements that will be
393 * {@link UndeclaredStmtCtx}.
395 * @param factory Statement factory
396 * @param ctx Inferred statement context, i.e. where the effective statement is instantiated
397 * @return Built effective stateue
399 abstract @NonNull E createInferredEffective(@NonNull StatementFactory<A, D, E> factory,
400 @NonNull InferredStatementContext<A, D, E> ctx, Stream<? extends ReactorStmtCtx<?, ?, ?>> declared,
401 Stream<? extends ReactorStmtCtx<?, ?, ?>> effective);
404 * Attach an effective copy of this statement. This essentially acts as a map, where we make a few assumptions:
406 * <li>{@code copy} and {@code this} statement share {@link #getOriginalCtx()} if it exists</li>
407 * <li>{@code copy} did not modify any statements relative to {@code this}</li>
410 * @param state effective statement state, acting as a lookup key
411 * @param stmt New copy to append
412 * @return {@code stmt} or a previously-created instances with the same {@code state}
414 @SuppressWarnings("unchecked")
415 final @NonNull E attachEffectiveCopy(final @NonNull EffectiveStatementState state, final @NonNull E stmt) {
416 final Object local = effectiveInstance;
417 final EffectiveInstances<E> instances;
418 if (local instanceof EffectiveInstances) {
419 instances = (EffectiveInstances<E>) local;
421 effectiveInstance = instances = new EffectiveInstances<>((E) local);
423 return instances.attachCopy(state, stmt);
427 * Walk this statement's copy history and return the statement closest to original which has not had its effective
428 * statements modified. This statement and returned substatement logically have the same set of substatements, hence
429 * share substatement-derived state.
431 * @return Closest {@link ReactorStmtCtx} with equivalent effective substatements
433 abstract @NonNull ReactorStmtCtx<A, D, E> unmodifiedEffectiveSource();
436 public final ModelProcessingPhase getCompletedPhase() {
437 return ModelProcessingPhase.ofExecutionOrder(executionOrder());
440 abstract byte executionOrder();
443 * Try to execute current {@link ModelProcessingPhase} of source parsing. If the phase has already been executed,
444 * this method does nothing. This must not be called with {@link ExecutionOrder#NULL}.
446 * @param phase to be executed (completed)
447 * @return true if phase was successfully completed
448 * @throws SourceException when an error occurred in source parsing
450 final boolean tryToCompletePhase(final byte executionOrder) {
451 return executionOrder() >= executionOrder || doTryToCompletePhase(executionOrder);
454 abstract boolean doTryToCompletePhase(byte targetOrder);
458 // Flags-based mechanics. These include public interfaces as well as all the crud we have lurking in our alignment
463 // Non-final for ImplicitStmtCtx/InferredStatementContext
465 public boolean isSupportedToBuildEffective() {
466 return isSupportedToBuildEffective;
470 public final void setUnsupported() {
471 isSupportedToBuildEffective = false;
475 public final boolean isSupportedByFeatures() {
476 final int fl = flags & SET_SUPPORTED_BY_FEATURES;
478 return fl == SET_SUPPORTED_BY_FEATURES;
480 if (isIgnoringIfFeatures()) {
481 flags |= SET_SUPPORTED_BY_FEATURES;
485 // If parent is supported, we need to check if-features statements of this context.
486 if (isParentSupportedByFeatures() && computeSupportedByFeatures()) {
487 flags |= SET_SUPPORTED_BY_FEATURES;
491 // Either parent is not supported or this statement is not supported
492 flags |= HAVE_SUPPORTED_BY_FEATURES;
497 * Compute whether this statement is supported by features. Returned value is combined with
498 * {@link #isParentSupportedByFeatures()} and cached.
500 * @return {@code true} if the current feature set matches {@code if-feature} of this statement
502 abstract boolean computeSupportedByFeatures();
504 protected abstract boolean isParentSupportedByFeatures();
507 * Config statements are not all that common which means we are performing a recursive search towards the root
508 * every time {@link #effectiveConfig()} is invoked. This is quite expensive because it causes a linear search
509 * for the (usually non-existent) config statement.
512 * This method maintains a resolution cache, so once we have returned a result, we will keep on returning the same
513 * result without performing any lookups, solely to support {@link #effectiveConfig()}.
516 * Note: use of this method implies that {@link #isIgnoringConfig()} is realized with
517 * {@link #isIgnoringConfig(StatementContextBase)}.
519 final @NonNull EffectiveConfig effectiveConfig(final ReactorStmtCtx<?, ?, ?> parent) {
520 return (flags & HAVE_CONFIG) != 0 ? EFFECTIVE_CONFIGS[flags & MASK_CONFIG] : loadEffectiveConfig(parent);
523 private @NonNull EffectiveConfig loadEffectiveConfig(final ReactorStmtCtx<?, ?, ?> parent) {
524 final EffectiveConfig parentConfig = parent.effectiveConfig();
526 final EffectiveConfig myConfig;
527 if (parentConfig != EffectiveConfig.IGNORED && !definition().support().isIgnoringConfig()) {
528 final Optional<Boolean> optConfig = findSubstatementArgument(ConfigEffectiveStatement.class);
529 if (optConfig.isPresent()) {
530 if (optConfig.orElseThrow()) {
531 // Validity check: if parent is config=false this cannot be a config=true
532 InferenceException.throwIf(parentConfig == EffectiveConfig.FALSE, this,
533 "Parent node has config=false, this node must not be specifed as config=true");
534 myConfig = EffectiveConfig.TRUE;
536 myConfig = EffectiveConfig.FALSE;
539 // If "config" statement is not specified, the default is the same as the parent's "config" value.
540 myConfig = parentConfig;
543 myConfig = EffectiveConfig.IGNORED;
546 flags = (byte) (flags & ~MASK_CONFIG | HAVE_CONFIG | myConfig.ordinal());
550 protected abstract boolean isIgnoringConfig();
553 * This method maintains a resolution cache for ignore config, so once we have returned a result, we will
554 * keep on returning the same result without performing any lookups. Exists only to support
555 * {@link SubstatementContext#isIgnoringConfig()}.
558 * Note: use of this method implies that {@link #isConfiguration()} is realized with
559 * {@link #effectiveConfig(StatementContextBase)}.
561 final boolean isIgnoringConfig(final StatementContextBase<?, ?, ?> parent) {
562 return EffectiveConfig.IGNORED == effectiveConfig(parent);
565 protected abstract boolean isIgnoringIfFeatures();
568 * This method maintains a resolution cache for ignore if-feature, so once we have returned a result, we will
569 * keep on returning the same result without performing any lookups. Exists only to support
570 * {@link SubstatementContext#isIgnoringIfFeatures()}.
572 final boolean isIgnoringIfFeatures(final StatementContextBase<?, ?, ?> parent) {
573 final int fl = flags & SET_IGNORE_IF_FEATURE;
575 return fl == SET_IGNORE_IF_FEATURE;
577 if (definition().support().isIgnoringIfFeatures() || parent.isIgnoringIfFeatures()) {
578 flags |= SET_IGNORE_IF_FEATURE;
582 flags |= HAVE_IGNORE_IF_FEATURE;
586 // These two exist only for StatementContextBase. Since we are squeezed for size, with only a single bit available
587 // in flags, we default to 'false' and only set the flag to true when we are absolutely sure -- and all other cases
588 // err on the side of caution by taking the time to evaluate each substatement separately.
589 final boolean allSubstatementsContextIndependent() {
590 return (flags & ALL_INDEPENDENT) != 0;
593 final void setAllSubstatementsContextIndependent() {
594 flags |= ALL_INDEPENDENT;
599 // Various functionality from AbstractTypeStatementSupport. This used to work on top of SchemaPath, now it still
600 // lives here. Ultimate future is either proper graduation or (more likely) move to AbstractTypeStatementSupport.
605 public final QName argumentAsTypeQName() {
606 // FIXME: This may yield illegal argument exceptions
607 return StmtContextUtils.qnameFromArgument(getOriginalCtx().orElse(this), getRawArgument());
611 public final QNameModule effectiveNamespace() {
612 if (StmtContextUtils.isUnknownStatement(this)) {
613 return publicDefinition().getStatementName().getModule();
615 if (producesDeclared(UsesStatement.class)) {
616 return coerceParent().effectiveNamespace();
619 final Object argument = argument();
620 if (argument instanceof QName qname) {
621 return qname.getModule();
623 if (argument instanceof String str) {
624 // FIXME: This may yield illegal argument exceptions
625 return StmtContextUtils.qnameFromArgument(getOriginalCtx().orElse(this), str).getModule();
627 if (argument instanceof SchemaNodeIdentifier sni
628 && (producesDeclared(AugmentStatement.class) || producesDeclared(RefineStatement.class)
629 || producesDeclared(DeviationStatement.class))) {
630 return sni.lastNodeIdentifier().getModule();
633 return coerceParent().effectiveNamespace();
636 private ReactorStmtCtx<?, ?, ?> coerceParent() {
637 return (ReactorStmtCtx<?, ?, ?>) coerceParentContext();
642 // Reference counting mechanics start. Please keep these methods in one block for clarity. Note this does not
643 // contribute to state visible outside of this package.
648 * Local knowledge of {@link #refcount} values up to statement root. We use this field to prevent recursive lookups
649 * in {@link #noParentRefs(StatementContextBase)} -- once we discover a parent reference once, we keep that
650 * knowledge and update it when {@link #sweep()} is invoked.
652 private byte parentRef = PARENTREF_UNKNOWN;
653 private static final byte PARENTREF_UNKNOWN = -1;
654 private static final byte PARENTREF_ABSENT = 0;
655 private static final byte PARENTREF_PRESENT = 1;
658 * Acquire a reference on this context. As long as there is at least one reference outstanding,
659 * {@link #buildEffective()} will not result in {@link #effectiveSubstatements()} being discarded.
661 * @throws VerifyException if {@link #effectiveSubstatements()} has already been discarded
663 final void incRef() {
664 final int current = refcount;
665 verify(current >= REFCOUNT_NONE, "Attempted to access reference count of %s", this);
666 if (current != REFCOUNT_DEFUNCT) {
667 // Note: can end up becoming REFCOUNT_DEFUNCT on overflow
668 refcount = current + 1;
670 LOG.debug("Disabled refcount increment of {}", this);
675 * Release a reference on this context. This call may result in {@link #effectiveSubstatements()} becoming
678 final void decRef() {
679 final int current = refcount;
680 if (current == REFCOUNT_DEFUNCT) {
682 LOG.debug("Disabled refcount decrement of {}", this);
685 if (current <= REFCOUNT_NONE) {
686 // Underflow, become defunct
687 // FIXME: add a global 'warn once' flag
688 LOG.warn("Statement refcount underflow, reference counting disabled for {}", this, new Throwable());
689 refcount = REFCOUNT_DEFUNCT;
693 refcount = current - 1;
694 LOG.trace("Refcount {} on {}", refcount, this);
696 if (refcount == REFCOUNT_NONE) {
702 * Return {@code true} if this context has no outstanding references.
704 * @return True if this context has no outstanding references.
706 final boolean noRefs() {
707 final int local = refcount;
708 return local < REFCOUNT_NONE || local == REFCOUNT_NONE && noParentRef();
711 private void lastDecRef() {
712 if (noImplictRef()) {
713 // We are no longer guarded by effective instance
718 final byte prevRefs = parentRef;
719 if (prevRefs == PARENTREF_ABSENT) {
720 // We are the last reference towards root, any children who observed PARENTREF_PRESENT from us need to be
723 } else if (prevRefs == PARENTREF_UNKNOWN) {
724 // Noone observed our parentRef, just update it
725 loadParentRefcount();
729 static final void markNoParentRef(final Collection<? extends ReactorStmtCtx<?, ?, ?>> substatements) {
730 for (ReactorStmtCtx<?, ?, ?> stmt : substatements) {
731 final byte prevRef = stmt.parentRef;
732 stmt.parentRef = PARENTREF_ABSENT;
733 if (prevRef == PARENTREF_PRESENT && stmt.refcount == REFCOUNT_NONE) {
734 // Child thinks it is pinned down, update its perspective
735 stmt.markNoParentRef();
740 abstract void markNoParentRef();
742 static final void sweep(final Collection<? extends ReactorStmtCtx<?, ?, ?>> substatements) {
743 for (ReactorStmtCtx<?, ?, ?> stmt : substatements) {
749 * Sweep this statement context as a result of {@link #sweepSubstatements()}, i.e. when parent is also being swept.
751 private void sweep() {
752 parentRef = PARENTREF_ABSENT;
753 if (refcount == REFCOUNT_NONE && noImplictRef()) {
754 LOG.trace("Releasing {}", this);
759 static final int countUnswept(final Collection<? extends ReactorStmtCtx<?, ?, ?>> substatements) {
761 for (ReactorStmtCtx<?, ?, ?> stmt : substatements) {
762 if (stmt.refcount > REFCOUNT_NONE || !stmt.noImplictRef()) {
770 * Implementation-specific sweep action. This is expected to perform a recursive {@link #sweep(Collection)} on all
771 * {@link #declaredSubstatements()} and {@link #effectiveSubstatements()} and report the result of the sweep
775 * {@link #effectiveSubstatements()} as well as namespaces may become inoperable as a result of this operation.
777 * @return True if the entire tree has been completely swept, false otherwise.
779 abstract int sweepSubstatements();
781 // Called when this statement does not have an implicit reference and have reached REFCOUNT_NONE
782 private void sweepOnDecrement() {
783 LOG.trace("Sweeping on decrement {}", this);
785 // No further parent references, sweep our state.
789 // Propagate towards parent if there is one
793 private void sweepParent() {
794 final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
795 if (parent != null) {
796 parent.sweepOnChildDecrement();
800 // Called from child when it has lost its final reference
801 private void sweepOnChildDecrement() {
802 if (isAwaitingChildren()) {
803 // We are a child for which our parent is waiting. Notify it and we are done.
808 // Check parent reference count
809 final int refs = refcount;
810 if (refs > REFCOUNT_NONE || refs <= REFCOUNT_SWEEPING || !noImplictRef()) {
815 // parent is potentially reclaimable
817 LOG.trace("Cleanup {} of parent {}", refs, this);
824 private boolean noImplictRef() {
825 return effectiveInstance != null || !isSupportedToBuildEffective();
828 private boolean noParentRef() {
829 return parentRefcount() == PARENTREF_ABSENT;
832 private byte parentRefcount() {
834 return (refs = parentRef) != PARENTREF_UNKNOWN ? refs : loadParentRefcount();
837 private byte loadParentRefcount() {
838 return parentRef = calculateParentRefcount();
841 private byte calculateParentRefcount() {
842 final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
843 return parent == null ? PARENTREF_ABSENT : parent.refcountForChild();
846 private byte refcountForChild() {
847 // A slight wrinkle here is that our machinery handles only PRESENT -> ABSENT invalidation and we can reach here
848 // while inference is still ongoing and hence we may not have a complete picture about existing references. We
849 // could therefore end up caching an ABSENT result and then that information becoming stale as a new reference
851 if (executionOrder() < ExecutionOrder.EFFECTIVE_MODEL) {
852 return PARENTREF_UNKNOWN;
855 // There are three possibilities:
856 // - REFCOUNT_NONE, in which case we need to check if this statement or its parents are holding a reference
857 // - negative (< REFCOUNT_NONE), meaning parent is in some stage of sweeping, hence it does not have
859 // - positive (> REFCOUNT_NONE), meaning parent has an explicit refcount which is holding us down
860 final int refs = refcount;
861 if (refs == REFCOUNT_NONE) {
862 return noImplictRef() && noParentRef() ? PARENTREF_ABSENT : PARENTREF_PRESENT;
864 return refs < REFCOUNT_NONE ? PARENTREF_ABSENT : PARENTREF_PRESENT;
867 private boolean isAwaitingChildren() {
868 return refcount > REFCOUNT_SWEEPING && refcount < REFCOUNT_NONE;
871 private void sweepOnChildDone() {
872 LOG.trace("Sweeping on child done {}", this);
873 final int current = refcount;
874 if (current >= REFCOUNT_NONE) {
875 // no-op, perhaps we want to handle some cases differently?
876 LOG.trace("Ignoring child sweep of {} for {}", this, current);
879 verify(current != REFCOUNT_SWEPT, "Attempt to sweep a child of swept %s", this);
881 refcount = current + 1;
882 LOG.trace("Child refcount {}", refcount);
883 if (refcount == REFCOUNT_NONE) {
885 final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
886 LOG.trace("Propagating to parent {}", parent);
887 if (parent != null && parent.isAwaitingChildren()) {
888 parent.sweepOnChildDone();
893 private void sweepDone() {
894 LOG.trace("Sweep done for {}", this);
895 refcount = REFCOUNT_SWEPT;
899 private boolean sweepState() {
900 refcount = REFCOUNT_SWEEPING;
901 final int childRefs = sweepSubstatements();
902 if (childRefs == 0) {
906 if (childRefs < 0 || childRefs >= REFCOUNT_DEFUNCT) {
907 // FIXME: add a global 'warn once' flag
908 LOG.warn("Negative child refcount {} cannot be stored, reference counting disabled for {}", childRefs, this,
910 refcount = REFCOUNT_DEFUNCT;
912 LOG.trace("Still {} outstanding children of {}", childRefs, this);
913 refcount = -childRefs;