Add ReactorStmtCtx.getStorageNodeType()
[yangtools.git] / parser / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / ReactorStmtCtx.java
index 724bbf486fc7c71b1fd36fa2a6d4fd72f5c74276..c57cb2466e3a5a6050e8f4e31b6617dad4bef4b7 100644 (file)
@@ -34,6 +34,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
 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.ParserNamespaces;
 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;
@@ -42,13 +43,13 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.ExecutionOrder;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
+import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementFactory;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import org.opendaylight.yangtools.yang.parser.spi.source.SourceParserNamespaces;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -156,25 +157,18 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
     // hence improve memory layout.
     private byte flags;
 
-    // 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 boolFlag;
-
     ReactorStmtCtx() {
         // Empty on purpose
     }
 
     ReactorStmtCtx(final ReactorStmtCtx<A, D, E> original) {
         isSupportedToBuildEffective = original.isSupportedToBuildEffective;
-        boolFlag = original.boolFlag;
         flags = original.flags;
     }
 
     // Used by ReplicaStatementContext only
     ReactorStmtCtx(final ReactorStmtCtx<A, D, E> original, final Void dummy) {
-        boolFlag = isSupportedToBuildEffective = original.isSupportedToBuildEffective;
+        isSupportedToBuildEffective = original.isSupportedToBuildEffective;
         flags = original.flags;
     }
 
@@ -195,7 +189,7 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
     public abstract Collection<? extends @NonNull StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements();
 
     @Override
-    public final Registry getBehaviourRegistry() {
+    final Registry getBehaviourRegistry() {
         return getRoot().getBehaviourRegistryImpl();
     }
 
@@ -321,24 +315,28 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
     //
 
     @Override
-    public final <K, V, T extends K, N extends ParserNamespace<K, V>> V namespaceItem(final @NonNull N type,
-            final T key) {
-        return getBehaviourRegistry().getNamespaceBehaviour(type).getFrom(this, key);
+    public StorageNodeType getStorageNodeType() {
+        // Common to all subclasses except RootStatementContext
+        return StorageNodeType.STATEMENT_LOCAL;
+    }
+
+    @Override
+    public final <K, V> V namespaceItem(final ParserNamespace<K, V> namespace, final K key) {
+        return getBehaviourRegistry().getNamespaceBehaviour(namespace).getFrom(this, key);
     }
 
     @Override
-    public final <K, V, N extends ParserNamespace<K, V>> Map<K, V> namespace(final @NonNull N type) {
-        return getNamespace(type);
+    public final <K, V> Map<K, V> namespace(final ParserNamespace<K, V> namespace) {
+        return getNamespace(namespace);
     }
 
     @Override
-    public final <K, V, N extends ParserNamespace<K, V>> Map<K, V> localNamespacePortion(final @NonNull N type) {
-        return getLocalNamespace(type);
+    public final <K, V> Map<K, V> localNamespacePortion(final ParserNamespace<K, V> namespace) {
+        return getLocalNamespace(namespace);
     }
 
     @Override
-    protected <K, V, N extends ParserNamespace<K, V>> void onNamespaceElementAdded(final N type, final K key,
-            final V value) {
+    protected <K, V> void onNamespaceElementAdded(final ParserNamespace<K, V> type, final K key, final V value) {
         // definition().onNamespaceElementAdded(this, type, key, value);
     }
 
@@ -404,8 +402,8 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
      * @return Built effective stateue
      */
     abstract @NonNull E createInferredEffective(@NonNull StatementFactory<A, D, E> factory,
-        @NonNull InferredStatementContext<A, D, E> ctx, Stream<? extends StmtContext<?, ?, ?>> declared,
-        Stream<? extends StmtContext<?, ?, ?>> effective);
+        @NonNull InferredStatementContext<A, D, E> ctx, Stream<? extends ReactorStmtCtx<?, ?, ?>> declared,
+        Stream<? extends ReactorStmtCtx<?, ?, ?>> effective);
 
     /**
      * Attach an effective copy of this statement. This essentially acts as a map, where we make a few assumptions:
@@ -467,7 +465,7 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
     //
     //
 
-    // Non-final form ImplicitStmtCtx
+    // Non-final for ImplicitStmtCtx/InferredStatementContext
     @Override
     public boolean isSupportedToBuildEffective() {
         return isSupportedToBuildEffective;
@@ -475,7 +473,7 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
 
     @Override
     public final void setUnsupported() {
-        this.isSupportedToBuildEffective = false;
+        isSupportedToBuildEffective = false;
     }
 
     @Override
@@ -494,8 +492,7 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
          */
         if (isParentSupportedByFeatures()) {
             // If the set of supported features has not been provided, all features are supported by default.
-            final Set<QName> supportedFeatures = getFromNamespace(SourceParserNamespaces.SUPPORTED_FEATURES,
-                Empty.value());
+            final Set<QName> supportedFeatures = namespaceItem(ParserNamespaces.SUPPORTED_FEATURES, Empty.value());
             if (supportedFeatures == null || StmtContextUtils.checkFeatureSupport(this, supportedFeatures)) {
                 flags |= SET_SUPPORTED_BY_FEATURES;
                 return true;
@@ -589,36 +586,6 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
         return false;
     }
 
-    // These two exist only due to memory optimization, should live in AbstractResumedStatement.
-    final boolean fullyDefined() {
-        return boolFlag;
-    }
-
-    final void setFullyDefined() {
-        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
     // 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.
@@ -876,26 +843,26 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
 
     private byte calculateParentRefcount() {
         final ReactorStmtCtx<?, ?, ?> parent = getParentContext();
-        if (parent == null) {
-            return PARENTREF_ABSENT;
-        }
+        return parent == null ? PARENTREF_ABSENT : parent.refcountForChild();
+    }
 
+    private byte refcountForChild() {
         // A slight wrinkle here is that our machinery handles only PRESENT -> ABSENT invalidation and we can reach here
         // while inference is still ongoing and hence we may not have a complete picture about existing references. We
         // could therefore end up caching an ABSENT result and then that information becoming stale as a new reference
         // is introduced.
-        if (parent.executionOrder() < ExecutionOrder.EFFECTIVE_MODEL) {
+        if (executionOrder() < ExecutionOrder.EFFECTIVE_MODEL) {
             return PARENTREF_UNKNOWN;
         }
 
         // There are three possibilities:
-        // - REFCOUNT_NONE, in which case we need to search next parent
+        // - REFCOUNT_NONE, in which case we need to check if this statement or its parents are holding a reference
         // - 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;
+        final int refs = refcount;
         if (refs == REFCOUNT_NONE) {
-            return parent.parentRefcount();
+            return noImplictRef() && noParentRef() ? PARENTREF_ABSENT : PARENTREF_PRESENT;
         }
         return refs < REFCOUNT_NONE ? PARENTREF_ABSENT : PARENTREF_PRESENT;
     }