extends StatementContextBase<A, D, E> implements OnDemandSchemaTreeStorageNode {
private static final Logger LOG = LoggerFactory.getLogger(InferredStatementContext.class);
+ // Sentinel object for 'substatements'
+ private static final Object SWEPT_SUBSTATEMENTS = new Object();
+
private final @NonNull StatementContextBase<A, D, E> prototype;
private final @NonNull StatementContextBase<?, ?, ?> parent;
private final @NonNull StmtContext<A, D, E> originalCtx;
private final A argument;
/**
- * Effective substatements, lazily materialized. This field can have three states:
+ * Effective substatements, lazily materialized. This field can have four states:
* <ul>
* <li>it can be {@code null}, in which case no materialization has taken place</li>
* <li>it can be a {@link HashMap}, in which case partial materialization has taken place</li>
* <li>it can be a {@link List}, in which case full materialization has taken place</li>
+ * <li>it can be {@link SWEPT_SUBSTATEMENTS}, in which case materialized state is no longer available</li>
* </ul>
*/
private Object substatements;
InferredStatementContext(final StatementContextBase<?, ?, ?> parent, final StatementContextBase<A, D, E> prototype,
final CopyType myCopyType, final CopyType childCopyType, final QNameModule targetModule) {
- super(prototype.definition(), CopyHistory.of(myCopyType, prototype.getCopyHistory()));
+ super(prototype.definition(), CopyHistory.of(myCopyType, prototype.history()));
this.parent = requireNonNull(parent);
this.prototype = requireNonNull(prototype);
- this.argument = targetModule == null ? prototype.getStatementArgument()
+ this.argument = targetModule == null ? prototype.argument()
: prototype.definition().adaptArgumentValue(prototype, targetModule);
this.childCopyType = requireNonNull(childCopyType);
this.targetModule = targetModule;
this.originalCtx = prototype.getOriginalCtx().orElse(prototype);
+
+ // Mark prototype as blocking statement cleanup
+ prototype.incRef();
}
@Override
}
@Override
- public D buildDeclared() {
+ public D declared() {
/*
* Share original instance of declared statement between all effective statements which have been copied or
* derived from this original declared statement.
*/
- return originalCtx.buildDeclared();
+ return originalCtx.declared();
}
@Override
} else {
template = prototype.allSubstatementsStream()
.filter(stmt -> stmt.producesEffective(SchemaTreeEffectiveStatement.class)
- && templateQName.equals(stmt.getStatementArgument()))
+ && templateQName.equals(stmt.argument()))
.findAny()
.orElse(null);
}
@SuppressWarnings("unchecked")
final Mutable<QName, Y, Z> ret = (Mutable<QName, Y, Z>) copySubstatement((Mutable<?, ?, ?>) template)
- .orElseThrow(() -> new InferenceException(getStatementSourceReference(),
- "Failed to materialize child %s template %s", qname, template));
+ .orElseThrow(
+ () -> new InferenceException(this, "Failed to materialize child %s template %s", qname, template));
ensureCompletedPhase(ret);
addMaterialized(template, ret);
// Instantiate this statement's effective substatements. Note this method has side-effects in namespaces and overall
// BuildGlobalContext, hence it must be called at most once.
- private List<StatementContextBase<?, ?, ?>> ensureEffectiveSubstatements() {
+ private List<ReactorStmtCtx<?, ?, ?>> ensureEffectiveSubstatements() {
+ accessSubstatements();
return substatements instanceof List ? castEffective(substatements)
: initializeSubstatements(castMaterialized(substatements));
}
@Override
- Iterable<StatementContextBase<?, ?, ?>> effectiveChildrenToComplete() {
+ Iterable<ReactorStmtCtx<?, ?, ?>> effectiveChildrenToComplete() {
// When we have not initialized, there are no statements to catch up: we will catch up when we are copying
// from prototype (which is already at ModelProcessingPhase.EFFECTIVE_MODEL).
if (substatements == null) {
return ImmutableList.of();
- } else if (substatements instanceof HashMap) {
+ }
+ accessSubstatements();
+ if (substatements instanceof HashMap) {
return castMaterialized(substatements).values();
} else {
return castEffective(substatements);
@Override
Stream<? extends StmtContext<?, ?, ?>> streamEffective() {
- // FIXME: YANGTOOLS-1184: do not force initialization
+ accessSubstatements();
return ensureEffectiveSubstatements().stream();
}
- private List<StatementContextBase<?, ?, ?>> initializeSubstatements(
- final Map<StmtContext<?, ?, ?>, StatementContextBase<?, ?, ?>> materializedSchemaTree) {
+ private void accessSubstatements() {
+ verify(substatements != SWEPT_SUBSTATEMENTS, "Attempted to access substatements of %s", this);
+ }
+
+ @Override
+ void markNoParentRef() {
+ final Object local = substatements;
+ if (local != null) {
+ markNoParentRef(castEffective(local));
+ }
+ }
+
+ @Override
+ int sweepSubstatements() {
+ final Object local = substatements;
+ substatements = SWEPT_SUBSTATEMENTS;
+ int count = 0;
+ if (local != null) {
+ final List<ReactorStmtCtx<?, ?, ?>> list = castEffective(local);
+ sweep(list);
+ count = countUnswept(list);
+ }
+ return count;
+ }
+
+ private List<ReactorStmtCtx<?, ?, ?>> initializeSubstatements(
+ final Map<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree) {
final Collection<? extends StatementContextBase<?, ?, ?>> declared = prototype.mutableDeclaredSubstatements();
final Collection<? extends Mutable<?, ?, ?>> effective = prototype.mutableEffectiveSubstatements();
- final List<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
+ final List<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
for (final Mutable<?, ?, ?> stmtContext : declared) {
if (stmtContext.isSupportedByFeatures()) {
copySubstatement(stmtContext, buffer, materializedSchemaTree);
copySubstatement(stmtContext, buffer, materializedSchemaTree);
}
- final List<StatementContextBase<?, ?, ?>> ret = beforeAddEffectiveStatementUnsafe(ImmutableList.of(),
- buffer.size());
+ final List<ReactorStmtCtx<?, ?, ?>> ret = beforeAddEffectiveStatementUnsafe(ImmutableList.of(), buffer.size());
ret.addAll((Collection) buffer);
substatements = ret;
+
+ prototype.decRef();
return ret;
}
YangStmtMapping.USES);
private void copySubstatement(final Mutable<?, ?, ?> substatement, final Collection<Mutable<?, ?, ?>> buffer,
- final Map<StmtContext<?, ?, ?>, StatementContextBase<?, ?, ?>> materializedSchemaTree) {
- final StatementDefinition def = substatement.getPublicDefinition();
+ final Map<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree) {
+ final StatementDefinition def = substatement.publicDefinition();
// FIXME: YANGTOOLS-652: formerly known as "isReusedByUses"
if (REUSED_DEF_SET.contains(def)) {
- LOG.debug("Reusing substatement {} for {}", substatement, this);
- buffer.add(substatement);
+ LOG.trace("Reusing substatement {} for {}", substatement, this);
+ buffer.add(substatement.replicaAsChildOf(this));
return;
}
//
// We could also perform a Map.containsKey() and perform a bulk add, but that would mean the statement order
// against parent would change -- and we certainly do not want that to happen.
- final StatementContextBase<?, ?, ?> materialized = findMaterialized(materializedSchemaTree, substatement);
+ final ReactorStmtCtx<?, ?, ?> materialized = findMaterialized(materializedSchemaTree, substatement);
if (materialized == null) {
copySubstatement(substatement).ifPresent(copy -> {
ensureCompletedPhase(copy);
}
private void addMaterialized(final StmtContext<?, ?, ?> template, final Mutable<?, ?, ?> copy) {
- final HashMap<StmtContext<?, ?, ?>, StatementContextBase<?, ?, ?>> materializedSchemaTree;
+ final HashMap<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree;
if (substatements == null) {
// Lazy initialization of backing map. We do not expect this to be used often or multiple times -- each hit
// here means an inference along schema tree, such as deviate/augment. HashMap requires power-of-two and
(StatementContextBase<?, ?, ?>) copy);
if (existing != null) {
throw new VerifyException(
- "Unexpected duplicate request for " + copy.getStatementArgument() + " previous result was " + existing);
+ "Unexpected duplicate request for " + copy.argument() + " previous result was " + existing);
}
}
- private static @Nullable StatementContextBase<?, ?, ?> findMaterialized(
- final Map<StmtContext<?, ?, ?>, StatementContextBase<?, ?, ?>> materializedSchemaTree,
+ private static @Nullable ReactorStmtCtx<?, ?, ?> findMaterialized(
+ final Map<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree,
final StmtContext<?, ?, ?> template) {
return materializedSchemaTree == null ? null : materializedSchemaTree.get(template);
}
@SuppressWarnings("unchecked")
- private static List<StatementContextBase<?, ?, ?>> castEffective(final Object substatements) {
- return (List<StatementContextBase<?, ?, ?>>) substatements;
+ private static List<ReactorStmtCtx<?, ?, ?>> castEffective(final Object substatements) {
+ return (List<ReactorStmtCtx<?, ?, ?>>) substatements;
}
@SuppressWarnings("unchecked")
- private static HashMap<StmtContext<?, ?, ?>, StatementContextBase<?, ?, ?>> castMaterialized(
- final Object substatements) {
- return (HashMap<StmtContext<?, ?, ?>, StatementContextBase<?, ?, ?>>) substatements;
+ private static HashMap<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> castMaterialized(final Object substatements) {
+ return (HashMap<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>>) substatements;
}
// Statement copy mess ends here
*/
@Override
@Deprecated
- public Optional<SchemaPath> getSchemaPath() {
+ public Optional<SchemaPath> schemaPath() {
return substatementGetSchemaPath();
}
@Override
- public A getStatementArgument() {
+ public A argument() {
return argument;
}
}
@Override
- public boolean isConfiguration() {
+ public boolean effectiveConfig() {
return isConfiguration(parent);
}