optConfig = findSubstatementArgument(ConfigEffectiveStatement.class);
+ if (optConfig.isPresent()) {
+ if (optConfig.orElseThrow()) {
+ // Validity check: if parent is config=false this cannot be a config=true
+ InferenceException.throwIf(parentConfig == EffectiveConfig.FALSE, this,
"Parent node has config=false, this node must not be specifed as config=true");
+ myConfig = EffectiveConfig.TRUE;
+ } else {
+ myConfig = EffectiveConfig.FALSE;
+ }
+ } else {
+ // If "config" statement is not specified, the default is the same as the parent's "config" value.
+ myConfig = parentConfig;
}
} else {
- // If "config" statement is not specified, the default is the same as the parent's "config" value.
- isConfig = parent.effectiveConfig();
+ myConfig = EffectiveConfig.IGNORED;
}
- // Resolved, make sure we cache this return
- flags |= isConfig ? SET_CONFIGURATION : HAVE_CONFIGURATION;
- return isConfig;
+ flags = (byte) (flags & ~MASK_CONFIG | HAVE_CONFIG | myConfig.ordinal());
+ return myConfig;
}
protected abstract boolean isIgnoringConfig();
@@ -434,20 +482,10 @@ abstract class ReactorStmtCtx, E extends Effec
*
*
* Note: use of this method implies that {@link #isConfiguration()} is realized with
- * {@link #isConfiguration(StatementContextBase)}.
+ * {@link #effectiveConfig(StatementContextBase)}.
*/
final boolean isIgnoringConfig(final StatementContextBase, ?, ?> parent) {
- final int fl = flags & SET_IGNORE_CONFIG;
- if (fl != 0) {
- return fl == SET_IGNORE_CONFIG;
- }
- if (definition().support().isIgnoringConfig() || parent.isIgnoringConfig()) {
- flags |= SET_IGNORE_CONFIG;
- return true;
- }
-
- flags |= HAVE_IGNORE_CONFIG;
- return false;
+ return EffectiveConfig.IGNORED == effectiveConfig(parent);
}
protected abstract boolean isIgnoringIfFeatures();
@@ -471,7 +509,7 @@ abstract class ReactorStmtCtx, E extends Effec
return false;
}
- // These two exists only due to memory optimization, should live in AbstractResumedStatement. We are also reusing
+ // These two exist only due to memory optimization, should live in AbstractResumedStatement. We are also reusing
// this for ReplicaStatementContext's refcount tracking.
final boolean fullyDefined() {
return fullyDefined;
@@ -481,6 +519,37 @@ abstract class ReactorStmtCtx, E extends Effec
fullyDefined = true;
}
+ // These two exist only for StatementContextBase. Since we are squeezed for size, with only a single bit available
+ // in flags, we default to 'false' and only set the flag to true when we are absolutely sure -- and all other cases
+ // err on the side of caution by taking the time to evaluate each substatement separately.
+ final boolean allSubstatementsContextIndependent() {
+ return (flags & ALL_INDEPENDENT) != 0;
+ }
+
+ final void setAllSubstatementsContextIndependent() {
+ flags |= ALL_INDEPENDENT;
+ }
+
+ //
+ //
+ // Various functionality from AbstractTypeStatementSupport. This used to work on top of SchemaPath, now it still
+ // lives here. Ultimate future is either proper graduation or (more likely) move to AbstractTypeStatementSupport.
+ //
+ //
+
+ @Override
+ public final QName argumentAsTypeQName() {
+ final Object argument = argument();
+ verify(argument instanceof String, "Unexpected argument %s", argument);
+ return interpretAsQName((String) argument);
+ }
+
+ @Override
+ public final QNameModule effectiveNamespace() {
+ // FIXME: there has to be a better way to do this
+ return getSchemaPath().getLastComponent().getModule();
+ }
+
//
//
// Common SchemaPath cache. All of this is bound to be removed once YANGTOOLS-1066 is done.
@@ -489,26 +558,17 @@ abstract class ReactorStmtCtx, E extends Effec
// Exists only to support {SubstatementContext,InferredStatementContext}.schemaPath()
@Deprecated
- final @NonNull Optional substatementGetSchemaPath() {
- SchemaPath local = schemaPath;
- if (local == null) {
- synchronized (this) {
- local = schemaPath;
- if (local == null) {
- schemaPath = local = createSchemaPath((StatementContextBase, ?, ?>) coerceParentContext());
- }
- }
+ final @Nullable SchemaPath substatementGetSchemaPath() {
+ if (schemaPath == null) {
+ schemaPath = createSchemaPath((StatementContextBase, ?, ?>) coerceParentContext());
}
-
- return Optional.ofNullable(local);
+ return schemaPath;
}
+ // FIXME: 7.0.0: this method's logic needs to be moved to the respective StatementSupport classes
@Deprecated
private SchemaPath createSchemaPath(final StatementContextBase, ?, ?> parent) {
- final Optional maybeParentPath = parent.schemaPath();
- verify(maybeParentPath.isPresent(), "Parent %s does not have a SchemaPath", parent);
- final SchemaPath parentPath = maybeParentPath.get();
-
+ final SchemaPath parentPath = parent.getSchemaPath();
if (StmtContextUtils.isUnknownStatement(this)) {
return parentPath.createChild(publicDefinition().getStatementName());
}
@@ -516,16 +576,13 @@ abstract class ReactorStmtCtx, E extends Effec
if (argument instanceof QName) {
final QName qname = (QName) argument;
if (producesDeclared(UsesStatement.class)) {
- return maybeParentPath.orElse(null);
+ return parentPath;
}
return parentPath.createChild(qname);
}
if (argument instanceof String) {
- // FIXME: This may yield illegal argument exceptions
- final Optional> originalCtx = getOriginalCtx();
- final QName qname = StmtContextUtils.qnameFromArgument(originalCtx.orElse(this), (String) argument);
- return parentPath.createChild(qname);
+ return parentPath.createChild(interpretAsQName((String) argument));
}
if (argument instanceof SchemaNodeIdentifier
&& (producesDeclared(AugmentStatement.class) || producesDeclared(RefineStatement.class)
@@ -534,8 +591,13 @@ abstract class ReactorStmtCtx, E extends Effec
return parentPath.createChild(((SchemaNodeIdentifier) argument).getNodeIdentifiers());
}
- // FIXME: this does not look right
- return maybeParentPath.orElse(null);
+ // FIXME: this does not look right, investigate more?
+ return parentPath;
+ }
+
+ private @NonNull QName interpretAsQName(final String argument) {
+ // FIXME: This may yield illegal argument exceptions
+ return StmtContextUtils.qnameFromArgument(getOriginalCtx().orElse(this), argument);
}
//
@@ -545,6 +607,16 @@ abstract class ReactorStmtCtx, E extends Effec
//
//
+ /**
+ * Local knowledge of {@link #refcount} values up to statement root. We use this field to prevent recursive lookups
+ * in {@link #noParentRefs(StatementContextBase)} -- once we discover a parent reference once, we keep that
+ * knowledge and update it when {@link #sweep()} is invoked.
+ */
+ private byte parentRef = PARENTREF_UNKNOWN;
+ private static final byte PARENTREF_UNKNOWN = -1;
+ private static final byte PARENTREF_ABSENT = 0;
+ private static final byte PARENTREF_PRESENT = 1;
+
/**
* Acquire a reference on this context. As long as there is at least one reference outstanding,
* {@link #buildEffective()} will not result in {@link #effectiveSubstatements()} being discarded.
@@ -582,28 +654,69 @@ abstract class ReactorStmtCtx, E extends Effec
refcount = current - 1;
LOG.trace("Refcount {} on {}", refcount, this);
- if (isSweepable()) {
- // We are no longer guarded by effective instance
- sweepOnDecrement();
+
+ if (refcount == REFCOUNT_NONE) {
+ lastDecRef();
}
}
/**
- * Sweep this statement context as a result of {@link #sweepSubstatements()}, i.e. when parent is also being swept.
+ * Return {@code true} if this context has an outstanding reference.
+ *
+ * @return True if this context has an outstanding reference.
*/
- private void sweep() {
- if (isSweepable()) {
- LOG.trace("Releasing {}", this);
- sweepState();
+ final boolean haveRef() {
+ return refcount > REFCOUNT_NONE;
+ }
+
+ private void lastDecRef() {
+ if (noImplictRef()) {
+ // We are no longer guarded by effective instance
+ sweepOnDecrement();
+ return;
+ }
+
+ final byte prevRefs = parentRef;
+ if (prevRefs == PARENTREF_ABSENT) {
+ // We are the last reference towards root, any children who observed PARENTREF_PRESENT from us need to be
+ // updated
+ markNoParentRef();
+ } else if (prevRefs == PARENTREF_UNKNOWN) {
+ // Noone observed our parentRef, just update it
+ loadParentRefcount();
}
}
+ static final void markNoParentRef(final Collection extends ReactorStmtCtx, ?, ?>> substatements) {
+ for (ReactorStmtCtx, ?, ?> stmt : substatements) {
+ final byte prevRef = stmt.parentRef;
+ stmt.parentRef = PARENTREF_ABSENT;
+ if (prevRef == PARENTREF_PRESENT && stmt.refcount == REFCOUNT_NONE) {
+ // Child thinks it is pinned down, update its perspective
+ stmt.markNoParentRef();
+ }
+ }
+ }
+
+ abstract void markNoParentRef();
+
static final void sweep(final Collection extends ReactorStmtCtx, ?, ?>> substatements) {
for (ReactorStmtCtx, ?, ?> stmt : substatements) {
stmt.sweep();
}
}
+ /**
+ * Sweep this statement context as a result of {@link #sweepSubstatements()}, i.e. when parent is also being swept.
+ */
+ private void sweep() {
+ parentRef = PARENTREF_ABSENT;
+ if (refcount == REFCOUNT_NONE && noImplictRef()) {
+ LOG.trace("Releasing {}", this);
+ sweepState();
+ }
+ }
+
static final int countUnswept(final Collection extends ReactorStmtCtx, ?, ?>> substatements) {
int result = 0;
for (ReactorStmtCtx, ?, ?> stmt : substatements) {
@@ -629,7 +742,7 @@ abstract class ReactorStmtCtx, E extends Effec
// Called when this statement does not have an implicit reference and have reached REFCOUNT_NONE
private void sweepOnDecrement() {
LOG.trace("Sweeping on decrement {}", this);
- if (noParentRefcount()) {
+ if (noParentRef()) {
// No further parent references, sweep our state.
sweepState();
}
@@ -657,7 +770,7 @@ abstract class ReactorStmtCtx, E extends Effec
}
// parent is potentially reclaimable
- if (noParentRefcount()) {
+ if (noParentRef()) {
LOG.trace("Cleanup {} of parent {}", refcount, this);
if (sweepState()) {
final ReactorStmtCtx, ?, ?> parent = getParentContext();
@@ -672,29 +785,40 @@ abstract class ReactorStmtCtx, E extends Effec
return effectiveInstance != null || !isSupportedToBuildEffective();
}
- // FIXME: cache the resolution of this
- private boolean noParentRefcount() {
+ private boolean noParentRef() {
+ return parentRefcount() == PARENTREF_ABSENT;
+ }
+
+ private byte parentRefcount() {
+ final byte refs;
+ return (refs = parentRef) != PARENTREF_UNKNOWN ? refs : loadParentRefcount();
+ }
+
+ private byte loadParentRefcount() {
+ return parentRef = calculateParentRefcount();
+ }
+
+ private byte calculateParentRefcount() {
final ReactorStmtCtx, ?, ?> parent = getParentContext();
- if (parent != null) {
- // There are three possibilities:
- // - REFCOUNT_NONE, in which case we need to search next parent
- // - negative (< REFCOUNT_NONE), meaning parent is in some stage of sweeping, hence it does not have
- // a reference to us
- // - positive (> REFCOUNT_NONE), meaning parent has an explicit refcount which is holding us down
- final int refs = parent.refcount;
- return refs == REFCOUNT_NONE ? parent.noParentRefcount() : refs < REFCOUNT_NONE;
+ if (parent == null) {
+ return PARENTREF_ABSENT;
}
- return true;
+ // There are three possibilities:
+ // - REFCOUNT_NONE, in which case we need to search next parent
+ // - negative (< REFCOUNT_NONE), meaning parent is in some stage of sweeping, hence it does not have
+ // a reference to us
+ // - positive (> REFCOUNT_NONE), meaning parent has an explicit refcount which is holding us down
+ final int refs = parent.refcount;
+ if (refs == REFCOUNT_NONE) {
+ return parent.parentRefcount();
+ }
+ return refs < REFCOUNT_NONE ? PARENTREF_ABSENT : PARENTREF_PRESENT;
}
private boolean isAwaitingChildren() {
return refcount > REFCOUNT_SWEEPING && refcount < REFCOUNT_NONE;
}
- private boolean isSweepable() {
- return refcount == REFCOUNT_NONE && noImplictRef();
- }
-
private void sweepOnChildDone() {
LOG.trace("Sweeping on child done {}", this);
final int current = refcount;