import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Verify.verify;
+import static com.google.common.base.Verify.verifyNotNull;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStatementState;
import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
*/
private static final int REFCOUNT_SWEPT = Integer.MIN_VALUE;
- private @Nullable E effectiveInstance;
+ /**
+ * Effective instance built from this context. This field as dual types. Under normal circumstances in matches the
+ * {@link #buildEffective()} instance. If this context is reused, it can be inflated to {@link EffectiveInstances}
+ * and also act as a common instance reuse site.
+ */
+ private @Nullable Object effectiveInstance;
// Master flag controlling whether this context can yield an effective statement
// FIXME: investigate the mechanics that are being supported by this, as it would be beneficial if we can get rid
// hence improve memory layout.
private byte flags;
- // Flag for use with AbstractResumedStatement. This is hiding in the alignment shadow created by above boolean
+ // Flag for use by AbstractResumedStatement, ReplicaStatementContext and InferredStatementContext. Each of them
+ // uses it to indicated a different condition. This is hiding in the alignment shadow created by
+ // 'isSupportedToBuildEffective'.
// FIXME: move this out once we have JDK15+
- private boolean fullyDefined;
+ private boolean boolFlag;
// SchemaPath cache for use with SubstatementContext and InferredStatementContext. This hurts RootStatementContext
// a bit in terms of size -- but those are only a few and SchemaPath is on its way out anyway.
- @Deprecated
- private volatile SchemaPath schemaPath;
+ // FIXME: this should become 'QName'
+ private SchemaPath schemaPath;
ReactorStmtCtx() {
// Empty on purpose
ReactorStmtCtx(final ReactorStmtCtx<A, D, E> original) {
isSupportedToBuildEffective = original.isSupportedToBuildEffective;
- fullyDefined = original.fullyDefined;
+ boolFlag = original.boolFlag;
+ flags = original.flags;
+ }
+
+ // Used by ReplicaStatementContext only
+ ReactorStmtCtx(final ReactorStmtCtx<A, D, E> original, final Void dummy) {
+ boolFlag = isSupportedToBuildEffective = original.isSupportedToBuildEffective;
flags = original.flags;
}
return getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
}
+ //
+ // In the next two methods we are looking for an effective statement. If we already have an effective instance,
+ // defer to it's implementation of the equivalent search. Otherwise we search our substatement contexts.
+ //
+ // Note that the search function is split, so as to allow InferredStatementContext to do its own thing first.
+ //
+
@Override
- // Non-final due to InferredStatementContext's override
- public <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgument(
+ public final <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgument(
+ final @NonNull Class<Z> type) {
+ final Object existing = effectiveInstance;
+ return existing != null ? EffectiveInstances.local(existing).findFirstEffectiveSubstatementArgument(type)
+ : findSubstatementArgumentImpl(type);
+ }
+
+ @Override
+ public final boolean hasSubstatement(final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
+ final Object existing = effectiveInstance;
+ return existing != null ? EffectiveInstances.local(existing).findFirstEffectiveSubstatement(type).isPresent()
+ : hasSubstatementImpl(type);
+ }
+
+ // Visible due to InferredStatementContext's override. At this point we do not have an effective instance available.
+ <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgumentImpl(
final @NonNull Class<Z> type) {
return allSubstatementsStream()
.filter(ctx -> ctx.isSupportedToBuildEffective() && ctx.producesEffective(type))
.map(ctx -> (X) ctx.getArgument());
}
- @Override
- // Non-final due to InferredStatementContext's override
- public boolean hasSubstatement(final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
+ // Visible due to InferredStatementContext's override. At this point we do not have an effective instance available.
+ boolean hasSubstatementImpl(final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
return allSubstatementsStream()
.anyMatch(ctx -> ctx.isSupportedToBuildEffective() && ctx.producesEffective(type));
}
@Override
public final E buildEffective() {
- final E existing;
- return (existing = effectiveInstance) != null ? existing : loadEffective();
+ final Object existing;
+ return (existing = effectiveInstance) != null ? EffectiveInstances.local(existing) : loadEffective();
}
- private E loadEffective() {
+ private @NonNull E loadEffective() {
// Creating an effective statement does not strictly require a declared instance -- there are statements like
// 'input', which are implicitly defined.
// Our implementation design makes an invariant assumption that buildDeclared() has been called by the time
// we attempt to create effective statement:
declared();
- final E ret = effectiveInstance = createEffective();
+ final E ret = createEffective();
+ effectiveInstance = ret;
// we have called createEffective(), substatements are no longer guarded by us. Let's see if we can clear up
// some residue.
if (refcount == REFCOUNT_NONE) {
abstract @NonNull E createEffective();
+
+ /**
+ * Attach an effective copy of this statement. This essentially acts as a map, where we make a few assumptions:
+ * <ul>
+ * <li>{@code copy} and {@code this} statement share {@link #getOriginalCtx()} if it exists</li>
+ * <li>{@code copy} did not modify any statements relative to {@code this}</li>
+ * </ul>
+ *
+ *
+ * @param state effective statement state, acting as a lookup key
+ * @param copy New copy to append
+ * @return {@code copy} or a previously-created instances with the same {@code state}
+ */
+ @SuppressWarnings("unchecked")
+ final @NonNull E attachCopy(final @NonNull EffectiveStatementState state, final @NonNull E copy) {
+ final Object effective = verifyNotNull(effectiveInstance, "Attaching copy to a unbuilt %s", this);
+ final EffectiveInstances<E> instances;
+ if (effective instanceof EffectiveInstances) {
+ instances = (EffectiveInstances<E>) effective;
+ } else {
+ effectiveInstance = instances = new EffectiveInstances<>((E) effective);
+ }
+ return instances.attachCopy(state, copy);
+ }
+
+ /**
+ * Walk this statement's copy history and return the statement closest to original which has not had its effective
+ * statements modified. This statement and returned substatement logically have the same set of substatements, hence
+ * share substatement-derived state.
+ *
+ * @return Closest {@link ReactorStmtCtx} with equivalent effective substatements
+ */
+ abstract @NonNull ReactorStmtCtx<A, D, E> unmodifiedEffectiveSource();
+
/**
* Try to execute current {@link ModelProcessingPhase} of source parsing. If the phase has already been executed,
* this method does nothing.
return false;
}
- // These two exist only due to memory optimization, should live in AbstractResumedStatement. We are also reusing
- // this for ReplicaStatementContext's refcount tracking.
+ // These two exist only due to memory optimization, should live in AbstractResumedStatement.
final boolean fullyDefined() {
- return fullyDefined;
+ return boolFlag;
}
final void setFullyDefined() {
- fullyDefined = true;
+ boolFlag = true;
+ }
+
+ // This exists only due to memory optimization, should live in ReplicaStatementContext. In this context the flag
+ // indicates the need to drop source's reference count when we are being swept.
+ final boolean haveSourceReference() {
+ return boolFlag;
+ }
+
+ // These three exist due to memory optimization, should live in InferredStatementContext. In this context the flag
+ // indicates whether or not this statement's substatement file was modified, i.e. it is not quite the same as the
+ // prototype's file.
+ final boolean isModified() {
+ return boolFlag;
+ }
+
+ final void setModified() {
+ boolFlag = true;
+ }
+
+ final void setUnmodified() {
+ boolFlag = false;
}
// These two exist only for StatementContextBase. Since we are squeezed for size, with only a single bit available
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.
// Exists only to support {SubstatementContext,InferredStatementContext}.schemaPath()
@Deprecated
- final @NonNull Optional<SchemaPath> 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<SchemaPath> 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());
}
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<StmtContext<A, D, E>> 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)
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);
}
//
}
if (current <= REFCOUNT_NONE) {
// Underflow, become defunct
+ // FIXME: add a global 'warn once' flag
LOG.warn("Statement refcount underflow, reference counting disabled for {}", this, new Throwable());
refcount = REFCOUNT_DEFUNCT;
return;
}
/**
- * Return {@code true} if this context has an outstanding reference.
+ * Return {@code true} if this context has no outstanding references.
*
- * @return True if this context has an outstanding reference.
+ * @return True if this context has no outstanding references.
*/
- final boolean haveRef() {
- return refcount > REFCOUNT_NONE;
+ final boolean noRefs() {
+ final int local = refcount;
+ return local < REFCOUNT_NONE || local == REFCOUNT_NONE && noParentRef();
}
private void lastDecRef() {
return true;
}
if (childRefs < 0 || childRefs >= REFCOUNT_DEFUNCT) {
+ // FIXME: add a global 'warn once' flag
LOG.warn("Negative child refcount {} cannot be stored, reference counting disabled for {}", childRefs, this,
new Throwable());
refcount = REFCOUNT_DEFUNCT;