X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=parser%2Fyang-parser-reactor%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fstmt%2Freactor%2FStatementContextBase.java;h=d3ce39b693393568559d8179230e902ce683ebe1;hb=fe7aa9a567521a2aac3caf3f1076a263e1cdc6b4;hp=65656d4efa5ba85c3e4541b6cf033756f4170547;hpb=083ef931709258bed6e0fede5eea7fe3f63ddecc;p=yangtools.git diff --git a/parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java b/parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java index 65656d4efa..d3ce39b693 100644 --- a/parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java +++ b/parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java @@ -13,8 +13,6 @@ import static com.google.common.base.Verify.verify; import static com.google.common.base.Verify.verifyNotNull; import static java.util.Objects.requireNonNull; -import com.google.common.annotations.Beta; -import com.google.common.base.VerifyException; import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMultimap; @@ -31,6 +29,7 @@ import java.util.Map.Entry; import java.util.Optional; import java.util.stream.Stream; import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; @@ -50,7 +49,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport; import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport.CopyPolicy; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; -import org.opendaylight.yangtools.yang.parser.spi.source.ImplicitSubstatement; +import org.opendaylight.yangtools.yang.parser.spi.meta.UndeclaredStatementFactory; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; import org.opendaylight.yangtools.yang.parser.stmt.reactor.NamespaceBehaviourWithListeners.KeyedValueAddedListener; import org.opendaylight.yangtools.yang.parser.stmt.reactor.NamespaceBehaviourWithListeners.PredicateValueAddedListener; @@ -64,7 +63,7 @@ import org.slf4j.LoggerFactory; * @param Declared Statement representation * @param Effective Statement representation */ -public abstract class StatementContextBase, E extends EffectiveStatement> +abstract class StatementContextBase, E extends EffectiveStatement> extends ReactorStmtCtx implements CopyHistory { /** * Event listener when an item is added to model namespace. @@ -96,22 +95,45 @@ public abstract class StatementContextBase, E private static final Logger LOG = LoggerFactory.getLogger(StatementContextBase.class); - // - // {@link CopyHistory} encoded as a single byte. We still have 4 bits unused. - // + // Bottom 4 bits, encoding a CopyHistory, aight? + private static final byte COPY_ORIGINAL = 0x00; private static final byte COPY_LAST_TYPE_MASK = 0x03; + @Deprecated(since = "7.0.9", forRemoval = true) private static final byte COPY_ADDED_BY_USES = 0x04; private static final byte COPY_ADDED_BY_AUGMENTATION = 0x08; - private static final byte COPY_ORIGINAL = 0x00; - private final byte copyHistory; + // Top four bits, of which we define the topmost two to 0. We use the bottom two to encode last CopyType, aight? + private static final int COPY_CHILD_TYPE_SHIFT = 4; + + private static final CopyType @NonNull [] COPY_TYPE_VALUES = CopyType.values(); static { - final int copyTypes = CopyType.values().length; + final int copyTypes = COPY_TYPE_VALUES.length; // This implies CopyType.ordinal() is <= COPY_TYPE_MASK verify(copyTypes == COPY_LAST_TYPE_MASK + 1, "Unexpected %s CopyType values", copyTypes); } + /** + * 8 bits worth of instance storage. This is treated as a constant bit field with following structure: + *
+     *   
+     * |7|6|5|4|3|2|1|0|
+     * |0 0|cct|a|u|lst|
+     *   
+     * 
+ * + *

+ * The four allocated fields are: + *

    + *
  • {@code lst}, encoding the four states corresponding to {@link CopyHistory#getLastOperation()}
  • + *
  • {@code u}, encoding {@link #isAddedByUses()}
  • + *
  • {@code a}, encoding {@link #isAugmenting()}
  • + *
  • {@code cct} encoding {@link #childCopyType()}
  • + *
+ * We still have two unused bits. + */ + private final byte bitsAight; + // Note: this field can strictly be derived in InferredStatementContext, but it forms the basis of many of our // operations, hence we want to keep it close by. private final @NonNull StatementDefinitionContext definition; @@ -127,27 +149,41 @@ public abstract class StatementContextBase, E */ private byte executionOrder; + // TODO: we a single byte of alignment shadow left, we should think how we can use it to cache information we build + // during InferredStatementContext.tryToReusePrototype(). We usually end up being routed to + // copyAsChildOfImpl() -- which performs an eager instantiation and checks for changes afterwards. We should + // be able to capture how parent scope affects the copy in a few bits. If we can do that, than we can reap + // the benefits by just examining new parent context and old parent context contribution to the state. If + // their impact is the same, we can skip instantiation of statements and directly reuse them (individually, + // or as a complete file). + // + // Whatever we end up tracking, we need to track two views of that -- for the statement itself + // (sans substatements) and a summary of substatements. I think it should be possible to get this working + // with 2x5bits -- we have up to 15 mutable bits available if we share the field with implicitDeclaredFlag. + // Copy constructor used by subclasses to implement reparent() StatementContextBase(final StatementContextBase original) { super(original); - this.copyHistory = original.copyHistory; + this.bitsAight = original.bitsAight; this.definition = original.definition; this.executionOrder = original.executionOrder; } StatementContextBase(final StatementDefinitionContext def) { this.definition = requireNonNull(def); - this.copyHistory = COPY_ORIGINAL; + this.bitsAight = COPY_ORIGINAL; } StatementContextBase(final StatementDefinitionContext def, final CopyType copyType) { this.definition = requireNonNull(def); - this.copyHistory = (byte) copyFlags(copyType); + this.bitsAight = (byte) copyFlags(copyType); } - StatementContextBase(final StatementContextBase prototype, final CopyType copyType) { + StatementContextBase(final StatementContextBase prototype, final CopyType copyType, + final CopyType childCopyType) { this.definition = prototype.definition; - this.copyHistory = (byte) (copyFlags(copyType) | prototype.copyHistory & ~COPY_LAST_TYPE_MASK); + this.bitsAight = (byte) (copyFlags(copyType) + | prototype.bitsAight & ~COPY_LAST_TYPE_MASK | childCopyType.ordinal() << COPY_CHILD_TYPE_SHIFT); } private static int copyFlags(final CopyType copyType) { @@ -155,27 +191,21 @@ public abstract class StatementContextBase, E } private static byte historyFlags(final CopyType copyType) { - switch (copyType) { - case ADDED_BY_AUGMENTATION: - return COPY_ADDED_BY_AUGMENTATION; - case ADDED_BY_USES: - return COPY_ADDED_BY_USES; - case ADDED_BY_USES_AUGMENTATION: - return COPY_ADDED_BY_AUGMENTATION | COPY_ADDED_BY_USES; - case ORIGINAL: - return COPY_ORIGINAL; - default: - throw new VerifyException("Unhandled type " + copyType); - } + return switch (copyType) { + case ADDED_BY_AUGMENTATION -> COPY_ADDED_BY_AUGMENTATION; + case ADDED_BY_USES -> COPY_ADDED_BY_USES; + case ADDED_BY_USES_AUGMENTATION -> COPY_ADDED_BY_AUGMENTATION | COPY_ADDED_BY_USES; + case ORIGINAL -> COPY_ORIGINAL; + }; } @Override - public Collection> getEffectOfStatement() { + public final Collection> getEffectOfStatement() { return effectOfStatement; } @Override - public void addAsEffectOfStatement(final Collection> ctxs) { + public final void addAsEffectOfStatement(final Collection> ctxs) { if (ctxs.isEmpty()) { return; } @@ -196,18 +226,25 @@ public abstract class StatementContextBase, E } @Override + @Deprecated(since = "7.0.9", forRemoval = true) public final boolean isAddedByUses() { - return (copyHistory & COPY_ADDED_BY_USES) != 0; + return (bitsAight & COPY_ADDED_BY_USES) != 0; } @Override + @Deprecated(since = "8.0.0") public final boolean isAugmenting() { - return (copyHistory & COPY_ADDED_BY_AUGMENTATION) != 0; + return (bitsAight & COPY_ADDED_BY_AUGMENTATION) != 0; } @Override public final CopyType getLastOperation() { - return CopyType.values()[copyHistory & COPY_LAST_TYPE_MASK]; + return COPY_TYPE_VALUES[bitsAight & COPY_LAST_TYPE_MASK]; + } + + // This method exists only for space optimization of InferredStatementContext + final CopyType childCopyType() { + return COPY_TYPE_VALUES[bitsAight >> COPY_CHILD_TYPE_SHIFT & COPY_LAST_TYPE_MASK]; } // @@ -240,8 +277,6 @@ public abstract class StatementContextBase, E return effective.isEmpty() ? ImmutableList.of() : effective; } - public abstract void removeStatementFromEffectiveSubstatements(StatementDefinition statementDef); - static final List> removeStatementFromEffectiveSubstatements( final List> effective, final StatementDefinition statementDef) { if (effective.isEmpty()) { @@ -259,21 +294,6 @@ public abstract class StatementContextBase, E return shrinkEffective(effective); } - /** - * Removes a statement context from the effective substatements based on its statement definition (i.e statement - * keyword) and raw (in String form) statement argument. The statement context is removed only if both statement - * definition and statement argument match with one of the effective substatements' statement definition - * and argument. - * - *

- * If the statementArg parameter is null, the statement context is removed based only on its statement definition. - * - * @param statementDef statement definition of the statement context to remove - * @param statementArg statement argument of the statement context to remove - */ - public abstract void removeStatementFromEffectiveSubstatements(StatementDefinition statementDef, - String statementArg); - static final List> removeStatementFromEffectiveSubstatements( final List> effective, final StatementDefinition statementDef, final String statementArg) { @@ -296,28 +316,17 @@ public abstract class StatementContextBase, E return shrinkEffective(effective); } - // YANG example: RPC/action statements always have 'input' and 'output' defined - @Beta - public , Z extends EffectiveStatement> @NonNull Mutable - appendImplicitSubstatement(final StatementSupport support, final String rawArg) { - // FIXME: YANGTOOLS-652: This does not need to be a SubstatementContext, in can be a specialized - // StatementContextBase subclass. - final Mutable ret = new SubstatementContext<>(this, new StatementDefinitionContext<>(support), - ImplicitSubstatement.of(sourceReference()), rawArg); + @Override + public final , Z extends EffectiveStatement> + Mutable createUndeclaredSubstatement(final StatementSupport support, final X arg) { + requireNonNull(support); + checkArgument(support instanceof UndeclaredStatementFactory, "Unsupported statement support %s", support); + + final var ret = new UndeclaredStmtCtx<>(this, support, arg); support.onStatementAdded(ret); - addEffectiveSubstatement(ret); return ret; } - /** - * Adds an effective statement to collection of substatements. - * - * @param substatement substatement - * @throws IllegalStateException if added in declared phase - * @throws NullPointerException if statement parameter is null - */ - public abstract void addEffectiveSubstatement(Mutable substatement); - final List> addEffectiveSubstatement(final List> effective, final Mutable substatement) { verifyStatement(substatement); @@ -329,15 +338,20 @@ public abstract class StatementContextBase, E return resized; } - /** - * Adds an effective statement to collection of substatements. - * - * @param statements substatements - * @throws IllegalStateException - * if added in declared phase - * @throws NullPointerException - * if statement parameter is null - */ + static final void afterAddEffectiveSubstatement(final Mutable substatement) { + // Undeclared statements still need to have 'onDeclarationFinished()' triggered + if (substatement instanceof UndeclaredStmtCtx) { + finishDeclaration((UndeclaredStmtCtx) substatement); + } + } + + // Split out to keep generics working without a warning + private static , Z extends EffectiveStatement> void finishDeclaration( + final UndeclaredStmtCtx substatement) { + substatement.definition().onDeclarationFinished(substatement, ModelProcessingPhase.FULL_DECLARATION); + } + + @Override public final void addEffectiveSubstatements(final Collection> statements) { if (!statements.isEmpty()) { statements.forEach(StatementContextBase::verifyStatement); @@ -362,7 +376,7 @@ public abstract class StatementContextBase, E return resized; } - abstract Iterable> effectiveChildrenToComplete(); + abstract Iterator> effectiveChildrenToComplete(); // exposed for InferredStatementContext only final void ensureCompletedPhase(final Mutable stmt) { @@ -416,15 +430,7 @@ public abstract class StatementContextBase, E return result; } - @NonNull E createEffective(final StatementFactory factory) { - return createEffective(factory, this); - } - - // Creates EffectiveStatement through full materialization - static , E extends EffectiveStatement> @NonNull E createEffective( - final StatementFactory factory, final StatementContextBase ctx) { - return factory.createEffective(ctx, ctx.streamDeclared(), ctx.streamEffective()); - } + abstract @NonNull E createEffective(@NonNull StatementFactory factory); /** * Return a stream of declared statements which can be built into an {@link EffectiveStatement}, as per @@ -446,7 +452,7 @@ public abstract class StatementContextBase, E @Override final boolean doTryToCompletePhase(final byte targetOrder) { - final boolean finished = phaseMutation.isEmpty() ? true : runMutations(targetOrder); + final boolean finished = phaseMutation.isEmpty() || runMutations(targetOrder); if (completeChildren(targetOrder) && finished) { onPhaseCompleted(targetOrder); return true; @@ -459,8 +465,9 @@ public abstract class StatementContextBase, E for (final StatementContextBase child : mutableDeclaredSubstatements()) { finished &= child.tryToCompletePhase(targetOrder); } - for (final ReactorStmtCtx child : effectiveChildrenToComplete()) { - finished &= child.tryToCompletePhase(targetOrder); + final var it = effectiveChildrenToComplete(); + while (it.hasNext()) { + finished &= it.next().tryToCompletePhase(targetOrder); } return finished; } @@ -468,7 +475,7 @@ public abstract class StatementContextBase, E private boolean runMutations(final byte targetOrder) { final ModelProcessingPhase phase = verifyNotNull(ModelProcessingPhase.ofExecutionOrder(targetOrder)); final Collection openMutations = phaseMutation.get(phase); - return openMutations.isEmpty() ? true : runMutations(phase, openMutations); + return openMutations.isEmpty() || runMutations(phase, openMutations); } private boolean runMutations(final ModelProcessingPhase phase, final Collection openMutations) { @@ -581,13 +588,6 @@ public abstract class StatementContextBase, E } } - /** - * Ends declared section of current node. - */ - void endDeclared(final ModelProcessingPhase phase) { - definition.onDeclarationFinished(this, phase); - } - @Override final StatementDefinitionContext definition() { return definition; @@ -718,19 +718,19 @@ public abstract class StatementContextBase, E } @Override - public > void addContext(final Class<@NonNull N> namespace, - final KT key,final StmtContext stmt) { + public final > void addContext( + final Class<@NonNull N> namespace, final KT key,final StmtContext stmt) { addContextToNamespace(namespace, key, stmt); } @Override - public Optional> copyAsChildOf(final Mutable parent, final CopyType type, + public final Optional> copyAsChildOf(final Mutable parent, final CopyType type, final QNameModule targetModule) { checkEffectiveModelCompleted(this); return Optional.ofNullable(copyAsChildOfImpl(parent, type, targetModule)); } - private ReactorStmtCtx copyAsChildOfImpl(final Mutable parent, final CopyType type, + private @Nullable ReactorStmtCtx copyAsChildOfImpl(final Mutable parent, final CopyType type, final QNameModule targetModule) { final StatementSupport support = definition.support(); final CopyPolicy policy = support.copyPolicy(); @@ -768,7 +768,7 @@ public abstract class StatementContextBase, E return canReuseCurrent(copy) ? this : copy; } - private boolean canReuseCurrent(final ReactorStmtCtx copy) { + private boolean canReuseCurrent(final @NonNull ReactorStmtCtx copy) { // Defer to statement factory to see if we can reuse this object. If we can and have only context-independent // substatements we can reuse the object. More complex cases are handled indirectly via the copy. return definition.getFactory().canReuseCurrent(copy, this, buildEffective().effectiveSubstatements()) @@ -779,39 +779,33 @@ public abstract class StatementContextBase, E public final Mutable childCopyOf(final StmtContext stmt, final CopyType type, final QNameModule targetModule) { checkEffectiveModelCompleted(stmt); - checkArgument(stmt instanceof StatementContextBase, "Unsupported statement %s", stmt); - return childCopyOf((StatementContextBase) stmt, type, targetModule); + if (stmt instanceof StatementContextBase) { + return childCopyOf((StatementContextBase) stmt, type, targetModule); + } else if (stmt instanceof ReplicaStatementContext) { + return ((ReplicaStatementContext) stmt).replicaAsChildOf(this); + } else { + throw new IllegalArgumentException("Unsupported statement " + stmt); + } } private , Z extends EffectiveStatement> Mutable childCopyOf( final StatementContextBase original, final CopyType type, final QNameModule targetModule) { - final Optional> implicitParent = definition.getImplicitParentFor( - original.publicDefinition()); + final var implicitParent = definition.getImplicitParentFor(this, original.publicDefinition()); final StatementContextBase result; final InferredStatementContext copy; if (implicitParent.isPresent()) { - final StatementDefinitionContext def = new StatementDefinitionContext<>(implicitParent.get()); - result = new SubstatementContext(this, def, original.sourceReference(), original.rawArgument(), - original.argument(), type); - - final CopyType childCopyType; - switch (type) { - case ADDED_BY_AUGMENTATION: - childCopyType = CopyType.ORIGINAL; - break; - case ADDED_BY_USES_AUGMENTATION: - childCopyType = CopyType.ADDED_BY_USES; - break; - case ADDED_BY_USES: - case ORIGINAL: - default: - childCopyType = type; - } + result = new UndeclaredStmtCtx(this, implicitParent.orElseThrow(), original, type); + final CopyType childCopyType = switch (type) { + case ADDED_BY_AUGMENTATION -> CopyType.ORIGINAL; + case ADDED_BY_USES_AUGMENTATION -> CopyType.ADDED_BY_USES; + case ADDED_BY_USES, ORIGINAL -> type; + }; copy = new InferredStatementContext<>(result, original, childCopyType, type, targetModule); result.addEffectiveSubstatement(copy); + result.definition.onStatementAdded(result); } else { result = copy = new InferredStatementContext<>(this, original, type, type, targetModule); } @@ -831,26 +825,24 @@ public abstract class StatementContextBase, E "Attempted to copy statement %s which has completed phase %s", stmt, phase); } - @Beta - // FIXME: this information should be exposed as a well-known Namespace + @Override public final boolean hasImplicitParentSupport() { return definition.getFactory() instanceof ImplicitParentAwareStatementSupport; } - @Beta - public final StatementContextBase wrapWithImplicit(final StatementContextBase original) { - final Optional> optImplicit = definition.getImplicitParentFor( - original.publicDefinition()); + @Override + public final StmtContext wrapWithImplicit(final StmtContext original) { + final var optImplicit = definition.getImplicitParentFor(this, original.publicDefinition()); if (optImplicit.isEmpty()) { return original; } - final StatementDefinitionContext def = new StatementDefinitionContext<>(optImplicit.get()); - final CopyType type = original.history().getLastOperation(); - final SubstatementContext result = new SubstatementContext(original.getParentContext(), def, - original.sourceReference(), original.rawArgument(), original.argument(), type); + checkArgument(original instanceof StatementContextBase, "Unsupported original %s", original); + final var origBase = (StatementContextBase)original; - result.addEffectiveSubstatement(original.reparent(result)); + @SuppressWarnings({ "rawtypes", "unchecked" }) + final UndeclaredStmtCtx result = new UndeclaredStmtCtx(origBase, optImplicit.orElseThrow()); + result.addEffectiveSubstatement(origBase.reparent(result)); result.setCompletedPhase(original.getCompletedPhase()); return result; }